1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 cr.define('options', function() { 6 var OptionsPage = options.OptionsPage; 7 8 /** 9 * Encapsulated handling of the 'DisplayOverscan' page. 10 * @constructor 11 */ 12 function DisplayOverscan() { 13 OptionsPage.call(this, 'displayOverscan', 14 loadTimeData.getString('displayOverscanPageTabTitle'), 15 'display-overscan-page'); 16 } 17 18 cr.addSingletonGetter(DisplayOverscan); 19 20 DisplayOverscan.prototype = { 21 __proto__: OptionsPage.prototype, 22 23 /** 24 * The ID of the target display. 25 * @private 26 */ 27 id_: null, 28 29 /** 30 * The keyboard event handler function. 31 * @private 32 */ 33 keyHandler_: null, 34 35 /** 36 * Initialize the page. 37 */ 38 initializePage: function() { 39 OptionsPage.prototype.initializePage.call(this); 40 41 this.keyHandler_ = this.handleKeyevent_.bind(this); 42 $('display-overscan-operation-reset').onclick = function() { 43 chrome.send('reset'); 44 }; 45 $('display-overscan-operation-ok').onclick = function() { 46 chrome.send('commit'); 47 OptionsPage.closeOverlay(); 48 }; 49 $('display-overscan-operation-cancel').onclick = function() { 50 OptionsPage.cancelOverlay(); 51 }; 52 }, 53 54 /** @override */ 55 handleCancel: function() { 56 // signals the cancel event. 57 chrome.send('cancel'); 58 OptionsPage.closeOverlay(); 59 }, 60 61 /** @override */ 62 didShowPage: function() { 63 if (this.id_ == null) { 64 OptionsPage.cancelOverlay(); 65 return; 66 } 67 68 window.addEventListener('keydown', this.keyHandler_); 69 // Sets up the size of the overscan dialog based on DisplayOptions dialog. 70 var displayOptionsPage = $('display-options-page'); 71 var displayOverscanPage = $('display-overscan-page'); 72 displayOverscanPage.style.width = 73 displayOptionsPage.offsetWidth - 20 + 'px'; 74 displayOverscanPage.style.minWidth = displayOverscanPage.style.width; 75 displayOverscanPage.style.height = 76 displayOptionsPage.offsetHeight - 50 + 'px'; 77 78 // Moves the table to describe operation at the middle of the contents 79 // vertically. 80 var operationsTable = $('display-overscan-operations-table'); 81 var buttonsContainer = $('display-overscan-button-strip'); 82 operationsTable.style.top = buttonsContainer.offsetTop / 2 - 83 operationsTable.offsetHeight / 2 + 'px'; 84 85 $('display-overscan-operation-cancel').focus(); 86 chrome.send('start', [this.id_]); 87 }, 88 89 /** @override */ 90 didClosePage: function() { 91 window.removeEventListener('keydown', this.keyHandler_); 92 }, 93 94 /** 95 * Called when the overscan calibration is canceled at the system level, 96 * such like the display is disconnected. 97 * @private 98 */ 99 onOverscanCanceled_: function() { 100 if (OptionsPage.getTopmostVisiblePage() == this) 101 OptionsPage.cancelOverlay(); 102 }, 103 104 /** 105 * Sets the target display id. This method has to be called before 106 * navigating to this page. 107 * @param {string} id The target display id. 108 */ 109 setDisplayId: function(id) { 110 this.id_ = id; 111 }, 112 113 /** 114 * Key event handler to make the effect of display rectangle. 115 * @param {Event} event The keyboard event. 116 * @private 117 */ 118 handleKeyevent_: function(event) { 119 switch (event.keyCode) { 120 case 37: // left arrow 121 if (event.shiftKey) 122 chrome.send('move', ['horizontal', -1]); 123 else 124 chrome.send('resize', ['horizontal', -1]); 125 event.preventDefault(); 126 break; 127 case 38: // up arrow 128 if (event.shiftKey) 129 chrome.send('move', ['vertical', -1]); 130 else 131 chrome.send('resize', ['vertical', -1]); 132 event.preventDefault(); 133 break; 134 case 39: // right arrow 135 if (event.shiftKey) 136 chrome.send('move', ['horizontal', 1]); 137 else 138 chrome.send('resize', ['horizontal', 1]); 139 event.preventDefault(); 140 break; 141 case 40: // bottom arrow 142 if (event.shiftKey) 143 chrome.send('move', ['vertical', 1]); 144 else 145 chrome.send('resize', ['vertical', 1]); 146 event.preventDefault(); 147 break; 148 } 149 } 150 }; 151 152 DisplayOverscan.onOverscanCanceled = function() { 153 DisplayOverscan.getInstance().onOverscanCanceled_(); 154 }; 155 156 // Export 157 return { 158 DisplayOverscan: DisplayOverscan 159 }; 160 }); 161