-
Notifications
You must be signed in to change notification settings - Fork 11
/
export.js
131 lines (114 loc) · 4.32 KB
/
export.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
var screen = require('electron').screen;
var native = require('./build/Release/addon');
/**
* @param {BrowserWindow} browserWindow - reference to the browser window the OSK
* is shown in reference to
* @param {boolean} menuPresent - whether the browser window has a menu
* @param {number} keyboardHeightScreenPercentage - the height of the OSK in screen height
* percentage
* @constructor
*/
function OSK(browserWindow, menuPresent, keyboardHeightScreenPercentage) {
this.browserWindow = browserWindow;
this.menuPresent = menuPresent;
this.keyboardHeightScreenPercentage = keyboardHeightScreenPercentage || 30;
}
/**
* Makes the OSK to appear.
*
* @param {number} x - position at screen x
* @param {number} y - position at screen y
* @param {number} width - width of the OSK
* @param {number} height - height of the OSK
* @returns {boolean}
*/
OSK.prototype.show = function(x, y, width, height) {
// Close the OSK if it is open.
if (this.close()) {
// Set it's position and size.
if (native.OSKSetPosition(x,y,width,height)) {
// Lauch it.
return native.OSKShow();
}
}
return false;
};
/**
* Makes the OSK to close.
* @returns {boolean}
*/
OSK.prototype.close = function() {
if (this.isVisible()) {
return native.OSKClose();
}
return true;
};
/**
* Checks if the OSK is visible.
* @returns {boolean}
*/
OSK.prototype.isVisible = function() {
return native.OSKIsVisible();
};
/**
* Gets some system specific window metrics.
* @returns {{topBarThickness: number, borderThickness: number, menuHeight: number}}
*/
OSK.prototype.getSystem = function() {
return {
topBarThickness: native.GetSystemTopBarThickness(),
borderThickness: native.GetSystemBorderThickness(),
menuHeight: native.GetSystemMenuHeight()
};
};
/**
* Shows the OSK in regard to a html event. Tries to position the OSK according to event.target
* element.
* @param {number} inBrowserYOffset - browser event
* @param {number} height - height of the input element
* @param {number} padding - additional y offset for the OSK
* @returns {boolean}
*/
OSK.prototype.showFromEvent = function(inBrowserYOffset, height, padding) {
if (padding === undefined) {
padding = 0;
}
// We need to know where the browser window is.
var windowPosition = this.browserWindow.getPosition();
// Some system window metrics will be useful.
var system = this.getSystem();
var menuHeight = 0;
// In development mode Electron always has a menu. Otherwise we can not detect it so we rely on
// menuPresent flag to take the menu height into account.
if (process.env.ELECTRON_ENV === 'development' || this.menuPresent) {
menuHeight = system.menuHeight;
}
var topBarHeight = system.topBarThickness;
var borderCount = 2; // Normally we have two borders - one for the browser window itself and OSK.
if (this.browserWindow.isFullScreen()) {
topBarHeight = 0;
// In full screen there is no top bar so we only have one border.
borderCount = 1;
}
var top =
inBrowserYOffset + // the y position in the view part of the browser
windowPosition[1] + // the screen y of the browser window position
menuHeight + // height of the menu if present
topBarHeight + // height of the window top bar
(system.borderThickness * borderCount); // total height of the window borders
// Now we calculate the OSK height as the percentage of the screen height.
var display = screen.getPrimaryDisplay();
var oskHeight = Math.floor(
(display.workAreaSize.height * this.keyboardHeightScreenPercentage) / 100
);
// If the keyboard would go out of screen we need to position it above the element.
if (oskHeight + top + height + padding > display.workAreaSize.height) {
top -= padding + oskHeight - system.borderThickness;
} else {
top += height + padding;
}
// OSK will be as wide as possible by default.
var oskWidth = display.workAreaSize.width;
return this.show(0, top, oskWidth, oskHeight);
};
module.exports = OSK;