| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 cr.define('options', function() { | 5 cr.define('options', function() { |
| 6 var OptionsPage = options.OptionsPage; | 6 var OptionsPage = options.OptionsPage; |
| 7 | 7 |
| 8 // The scale ratio of the display rectangle to its original size. | 8 // The scale ratio of the display rectangle to its original size. |
| 9 /** @const */ var VISUAL_SCALE = 1 / 10; | 9 /** @const */ var VISUAL_SCALE = 1 / 10; |
| 10 // The number of pixels for sticky dragging. |
| 11 /** @const */ var STICKY_OFFSET = 5; |
| 10 | 12 |
| 11 /** | 13 /** |
| 12 * Enumeration of secondary display layout. The value has to be same as the | 14 * Enumeration of secondary display layout. The value has to be same as the |
| 13 * values in ash/monitor/monitor_controller.cc. | 15 * values in ash/monitor/monitor_controller.cc. |
| 14 * @enum {number} | 16 * @enum {number} |
| 15 */ | 17 */ |
| 16 var SecondaryDisplayLayout = { | 18 var SecondaryDisplayLayout = { |
| 17 TOP: 0, | 19 TOP: 0, |
| 18 RIGHT: 1, | 20 RIGHT: 1, |
| 19 BOTTOM: 2, | 21 BOTTOM: 2, |
| (...skipping 30 matching lines...) Expand all Loading... |
| 50 | 52 |
| 51 $('display-options-toggle-mirroring').onclick = (function() { | 53 $('display-options-toggle-mirroring').onclick = (function() { |
| 52 this.mirroring_ = !this.mirroring_; | 54 this.mirroring_ = !this.mirroring_; |
| 53 chrome.send('setMirroring', [this.mirroring_]); | 55 chrome.send('setMirroring', [this.mirroring_]); |
| 54 }).bind(this); | 56 }).bind(this); |
| 55 | 57 |
| 56 chrome.send('getDisplayInfo'); | 58 chrome.send('getDisplayInfo'); |
| 57 }, | 59 }, |
| 58 | 60 |
| 59 /** | 61 /** |
| 62 * Collects the current data and sends it to Chrome. |
| 63 * @private |
| 64 */ |
| 65 applyResult_: function() { |
| 66 // Offset is calculated from top or left edge. |
| 67 var primary = this.displays_[0]; |
| 68 var secondary = this.displays_[1]; |
| 69 var offset = null; |
| 70 if (this.layout_ == SecondaryDisplayLayout.LEFT || |
| 71 this.layout_ == SecondaryDisplayLayout.RIGHT) { |
| 72 offset = secondary.div.offsetTop - primary.div.offsetTop; |
| 73 } else { |
| 74 offset = secondary.div.offsetLeft - primary.div.offsetLeft; |
| 75 } |
| 76 chrome.send('setDisplayLayout', [this.layout_, offset / VISUAL_SCALE]); |
| 77 }, |
| 78 |
| 79 /** |
| 60 * Mouse move handler for dragging display rectangle. | 80 * Mouse move handler for dragging display rectangle. |
| 61 * @private | 81 * @private |
| 62 * @param {Event} e The mouse move event. | 82 * @param {Event} e The mouse move event. |
| 63 */ | 83 */ |
| 64 onMouseMove_: function(e) { | 84 onMouseMove_: function(e) { |
| 65 if (!this.dragging_) | 85 if (!this.dragging_) |
| 66 return true; | 86 return true; |
| 67 | 87 |
| 68 var index = -1; | 88 var index = -1; |
| 69 for (var i = 0; i < this.displays_.length; i++) { | 89 for (var i = 0; i < this.displays_.length; i++) { |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 return false; | 213 return false; |
| 194 }, | 214 }, |
| 195 | 215 |
| 196 /** | 216 /** |
| 197 * Mouse up handler for dragging display rectangle. | 217 * Mouse up handler for dragging display rectangle. |
| 198 * @private | 218 * @private |
| 199 * @param {Event} e The mouse up event. | 219 * @param {Event} e The mouse up event. |
| 200 */ | 220 */ |
| 201 onMouseUp_: function(e) { | 221 onMouseUp_: function(e) { |
| 202 if (this.dragging_) { | 222 if (this.dragging_) { |
| 223 // Checks the dragging location and moves it back if needed. |
| 224 var primary_div = this.displays_[0].div; |
| 225 var dragging_div = this.dragging_.display.div; |
| 226 if (this.layout_ == SecondaryDisplayLayout.LEFT || |
| 227 this.layout_ == SecondaryDisplayLayout.RIGHT) { |
| 228 var top_offset = Math.abs( |
| 229 dragging_div.offsetTop - primary_div.offsetTop); |
| 230 var bottom_offset = Math.abs( |
| 231 (dragging_div.offsetTop + dragging_div.offsetHeight) - |
| 232 (primary_div.offsetTop + dragging_div.offsetHeight)); |
| 233 |
| 234 console.log([top_offset, bottom_offset]); |
| 235 if (top_offset > primary_div.offsetHeight) { |
| 236 // dragging is below of the primary. |
| 237 dragging_div.style.top = primary_div.offsetTop + 'px'; |
| 238 } else if (bottom_offset < -primary_div.offsetHeight) { |
| 239 // dragging is over the primary. |
| 240 dragging_div.style.top = primary_div.offsetTop + 'px'; |
| 241 } else if (top_offset < STICKY_OFFSET && |
| 242 top_offset <= bottom_offset) { |
| 243 // The top edges are very close. |
| 244 dragging_div.style.top = primary_div.offsetTop + 'px'; |
| 245 } else if (bottom_offset < STICKY_OFFSET && |
| 246 bottom_offset < top_offset) { |
| 247 // The bottom edges are very close. |
| 248 dragging_div.style.top = primary_div.offsetTop + |
| 249 primary_div.offsetHeight - dragging_div.offsetHeight + 'px'; |
| 250 } |
| 251 } else { |
| 252 var left_offset = Math.abs( |
| 253 dragging_div.offsetLeft - primary_div.offsetLeft); |
| 254 var right_offset = Math.abs( |
| 255 (dragging_div.offsetLeft + dragging_div.offsetWidth) - |
| 256 (primary_div.offsetLeft + dragging_div.offsetWidth)); |
| 257 |
| 258 console.log([left_offset, right_offset]); |
| 259 if (left_offset > primary_div.offsetWidth) { |
| 260 // dragging exceeds the right boundary of the primary. |
| 261 dragging_div.style.left = primary_div.offsetLeft + 'px'; |
| 262 } else if (right_offset < -primary_div.offsetWidth) { |
| 263 // dragging exceeds the left boundary of the primary. |
| 264 dragging_div.style.left = primary_div.offsetLeft + 'px'; |
| 265 } else if (left_offset < STICKY_OFFSET && |
| 266 left_offset <= right_offset) { |
| 267 // The left edges are very close. |
| 268 dragging_div.style.left = primary_div.offsetLeft + 'px'; |
| 269 } else if (right_offset < STICKY_OFFSET && |
| 270 right_offset < left_offset) { |
| 271 // The right edges are very close. |
| 272 dragging_div.style.left = primary_div.offsetLeft + |
| 273 primary_div.offsetWidth - dragging_div.offsetWidth + 'px'; |
| 274 } |
| 275 } |
| 203 this.dragging_ = null; | 276 this.dragging_ = null; |
| 204 chrome.send('setDisplayLayout', [this.layout_]); | 277 this.applyResult_(); |
| 205 } | 278 } |
| 206 return false; | 279 return false; |
| 207 }, | 280 }, |
| 208 | 281 |
| 209 /** | 282 /** |
| 210 * Clears the drawing area for display rectangles. | 283 * Clears the drawing area for display rectangles. |
| 211 * @private | 284 * @private |
| 212 */ | 285 */ |
| 213 resetDisplaysView_: function() { | 286 resetDisplaysView_: function() { |
| 214 var displays_view_host = $('display-options-displays-view-host'); | 287 var displays_view_host = $('display-options-displays-view-host'); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 div.className = 'displays-display'; | 357 div.className = 'displays-display'; |
| 285 if (i == 0) | 358 if (i == 0) |
| 286 div.className += ' displays-primary'; | 359 div.className += ' displays-primary'; |
| 287 else if (i == this.focused_index_) | 360 else if (i == this.focused_index_) |
| 288 div.className += ' displays-focused'; | 361 div.className += ' displays-focused'; |
| 289 div.style.width = display.width * VISUAL_SCALE + 'px'; | 362 div.style.width = display.width * VISUAL_SCALE + 'px'; |
| 290 div.style.height = display.height * VISUAL_SCALE + 'px'; | 363 div.style.height = display.height * VISUAL_SCALE + 'px'; |
| 291 div.style.lineHeight = div.style.height; | 364 div.style.lineHeight = div.style.height; |
| 292 switch (this.layout_) { | 365 switch (this.layout_) { |
| 293 case SecondaryDisplayLayout.RIGHT: | 366 case SecondaryDisplayLayout.RIGHT: |
| 294 display.div.style.top = '0'; | 367 if (i == 0) |
| 368 display.div.style.top = '0'; |
| 369 else |
| 370 display.div.style.top = this.offset_ * VISUAL_SCALE + 'px'; |
| 295 display.div.style.left = base_point.x + 'px'; | 371 display.div.style.left = base_point.x + 'px'; |
| 296 base_point.x += display.width * VISUAL_SCALE; | 372 base_point.x += display.width * VISUAL_SCALE; |
| 297 break; | 373 break; |
| 298 case SecondaryDisplayLayout.LEFT: | 374 case SecondaryDisplayLayout.LEFT: |
| 299 display.div.style.top = '0'; | 375 if (i == 0) |
| 376 display.div.style.top = '0'; |
| 377 else |
| 378 display.div.style.top = this.offset_ * VISUAL_SCALE + 'px'; |
| 300 base_point.x -= display.width * VISUAL_SCALE; | 379 base_point.x -= display.width * VISUAL_SCALE; |
| 301 display.div.style.left = base_point.x + 'px'; | 380 display.div.style.left = base_point.x + 'px'; |
| 302 break; | 381 break; |
| 303 case SecondaryDisplayLayout.TOP: | 382 case SecondaryDisplayLayout.TOP: |
| 304 display.div.style.left = '0'; | 383 if (i == 0) |
| 384 display.div.style.left = '0'; |
| 385 else |
| 386 display.div.style.left = this.offset_ * VISUAL_SCALE + 'px'; |
| 305 base_point.y -= display.height * VISUAL_SCALE; | 387 base_point.y -= display.height * VISUAL_SCALE; |
| 306 display.div.style.top = base_point.y + 'px'; | 388 display.div.style.top = base_point.y + 'px'; |
| 307 break; | 389 break; |
| 308 case SecondaryDisplayLayout.BOTTOM: | 390 case SecondaryDisplayLayout.BOTTOM: |
| 309 display.div.style.left = '0'; | 391 if (i == 0) |
| 392 display.div.style.left = '0'; |
| 393 else |
| 394 display.div.style.left = this.offset_ * VISUAL_SCALE + 'px'; |
| 310 display.div.style.top = base_point.y + 'px'; | 395 display.div.style.top = base_point.y + 'px'; |
| 311 base_point.y += display.height * VISUAL_SCALE; | 396 base_point.y += display.height * VISUAL_SCALE; |
| 312 break; | 397 break; |
| 313 } | 398 } |
| 314 | 399 |
| 315 this.displays_view_.appendChild(div); | 400 this.displays_view_.appendChild(div); |
| 316 } | 401 } |
| 317 }, | 402 }, |
| 318 | 403 |
| 319 /** | 404 /** |
| 320 * Called when the display arrangement has changed. | 405 * Called when the display arrangement has changed. |
| 321 * @private | 406 * @private |
| 322 * @param {boolean} mirroring Whether current mode is mirroring or not. | 407 * @param {boolean} mirroring Whether current mode is mirroring or not. |
| 323 * @param {Array} displays The list of the display information. | 408 * @param {Array} displays The list of the display information. |
| 324 * @param {SecondaryDisplayLayout} layout The layout strategy. | 409 * @param {SecondaryDisplayLayout} layout The layout strategy. |
| 410 * @param {number} offset The offset of the secondary display. |
| 325 */ | 411 */ |
| 326 onDisplayChanged_: function(mirroring, displays, layout) { | 412 onDisplayChanged_: function(mirroring, displays, layout, offset) { |
| 327 this.mirroring_ = mirroring; | 413 this.mirroring_ = mirroring; |
| 328 this.layout_ = layout; | 414 this.layout_ = layout; |
| 415 this.offset_ = offset; |
| 329 | 416 |
| 330 $('display-options-toggle-mirroring').textContent = | 417 $('display-options-toggle-mirroring').textContent = |
| 331 loadTimeData.getString( | 418 loadTimeData.getString( |
| 332 this.mirroring_ ? 'stopMirroring' : 'startMirroring'); | 419 this.mirroring_ ? 'stopMirroring' : 'startMirroring'); |
| 333 | 420 |
| 334 // Focus to the first display next to the primary one when |displays| list | 421 // Focus to the first display next to the primary one when |displays| list |
| 335 // is updated. | 422 // is updated. |
| 336 if (this.displays_.length != displays.length) | 423 if (this.displays_.length != displays.length) |
| 337 this.focused_index_ = 1; | 424 this.focused_index_ = 1; |
| 338 | 425 |
| 339 this.displays_ = displays; | 426 this.displays_ = displays; |
| 340 | 427 |
| 341 this.resetDisplaysView_(); | 428 this.resetDisplaysView_(); |
| 342 if (this.mirroring_) | 429 if (this.mirroring_) |
| 343 this.layoutMirroringDisplays_(); | 430 this.layoutMirroringDisplays_(); |
| 344 else | 431 else |
| 345 this.layoutDisplays_(); | 432 this.layoutDisplays_(); |
| 346 }, | 433 }, |
| 347 }; | 434 }; |
| 348 | 435 |
| 349 DisplayOptions.setDisplayInfo = function(mirroring, displays, layout) { | 436 DisplayOptions.setDisplayInfo = function( |
| 350 DisplayOptions.getInstance().onDisplayChanged_(mirroring, displays, layout); | 437 mirroring, displays, layout, offset) { |
| 438 DisplayOptions.getInstance().onDisplayChanged_( |
| 439 mirroring, displays, layout, offset); |
| 351 }; | 440 }; |
| 352 | 441 |
| 353 // Export | 442 // Export |
| 354 return { | 443 return { |
| 355 DisplayOptions: DisplayOptions | 444 DisplayOptions: DisplayOptions |
| 356 }; | 445 }; |
| 357 }); | 446 }); |
| OLD | NEW |