extension.js 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  1. import Clutter from 'gi://Clutter';
  2. import Gio from 'gi://Gio';
  3. import GLib from 'gi://GLib';
  4. import GObject from 'gi://GObject';
  5. import St from 'gi://St';
  6. import Shell from 'gi://Shell';
  7. import * as EdgeDragAction from 'resource:///org/gnome/shell/ui/edgeDragAction.js';
  8. import * as Main from 'resource:///org/gnome/shell/ui/main.js';
  9. import * as PanelMenu from 'resource:///org/gnome/shell/ui/panelMenu.js';
  10. import * as PopupMenu from 'resource:///org/gnome/shell/ui/popupMenu.js';
  11. import * as QuickSettings from 'resource:///org/gnome/shell/ui/quickSettings.js';
  12. import * as KeyboardManager from 'resource:///org/gnome/shell/misc/keyboardManager.js';
  13. import * as KeyboardUI from 'resource:///org/gnome/shell/ui/keyboard.js';
  14. import { Dialog } from 'resource:///org/gnome/shell/ui/dialog.js';
  15. import { Extension, gettext as _ } from 'resource:///org/gnome/shell/extensions/extension.js';
  16. const State = {
  17. OPENED: 0,
  18. CLOSED: 1,
  19. OPENING: 2,
  20. CLOSING: 3,
  21. };
  22. class KeyboardMenuToggle extends QuickSettings.QuickMenuToggle {
  23. static {
  24. GObject.registerClass(this);
  25. }
  26. constructor(extensionObject) {
  27. super({
  28. title: _('Screen Keyboard'),
  29. iconName: 'input-keyboard-symbolic',
  30. toggleMode: true,
  31. });
  32. this.extensionObject = extensionObject;
  33. this.settings = extensionObject.getSettings();
  34. this.menu.setHeader('input-keyboard-symbolic', _('Screen Keyboard'), _('Opening Mode'));
  35. this._itemsSection = new PopupMenu.PopupMenuSection();
  36. this._itemsSection.addMenuItem(new PopupMenu.PopupImageMenuItem(_('Never'), this.settings.get_int("enable-tap-gesture") == 0 ? 'emblem-ok-symbolic' : null));
  37. this._itemsSection.addMenuItem(new PopupMenu.PopupImageMenuItem(_("Only on Touch"), this.settings.get_int("enable-tap-gesture") == 1 ? 'emblem-ok-symbolic' : null));
  38. this._itemsSection.addMenuItem(new PopupMenu.PopupImageMenuItem(_("Always"), this.settings.get_int("enable-tap-gesture") == 2 ? 'emblem-ok-symbolic' : null));
  39. for (var i in this._itemsSection._getMenuItems()) {
  40. const item = this._itemsSection._getMenuItems()[i]
  41. const num = i
  42. item.connect('activate', () => this.settings.set_int("enable-tap-gesture", num))
  43. }
  44. this.menu.addMenuItem(this._itemsSection);
  45. this.settings.bind('indicator-enabled',
  46. this, 'checked',
  47. Gio.SettingsBindFlags.DEFAULT);
  48. this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
  49. const settingsItem = this.menu.addAction(_('More Settings'),
  50. () => this.extensionObject.openPreferences());
  51. settingsItem.visible = Main.sessionMode.allowSettings;
  52. this.menu._settingsActions[this.extensionObject.uuid] = settingsItem;
  53. }
  54. _refresh() {
  55. for (var i in this._itemsSection._getMenuItems()) {
  56. this._itemsSection._getMenuItems()[i].setIcon(this.settings.get_int("enable-tap-gesture") == i ? 'emblem-ok-symbolic' : null)
  57. }
  58. }
  59. };
  60. let keycodes;
  61. let layouts;
  62. export default class GjsOskExtension extends Extension {
  63. _openKeyboard(instant) {
  64. if (this.Keyboard.state == State.CLOSED) {
  65. this.Keyboard.open(null, !instant ? null : true);
  66. }
  67. }
  68. _closeKeyboard(instant) {
  69. if (this.Keyboard.state == State.OPENED) {
  70. this.Keyboard.close(!instant ? null : true);
  71. }
  72. }
  73. _toggleKeyboard(instant = false) {
  74. if (!this.Keyboard.opened) {
  75. this._openKeyboard(instant);
  76. this.Keyboard.openedFromButton = true;
  77. this.Keyboard.closedFromButton = false
  78. } else {
  79. this._closeKeyboard(instant);
  80. this.Keyboard.openedFromButton = false;
  81. this.Keyboard.closedFromButton = true;
  82. }
  83. }
  84. open_interval() {
  85. global.stage.disconnect(this.tapConnect)
  86. if (this.openInterval !== null) {
  87. clearInterval(this.openInterval);
  88. this.openInterval = null;
  89. }
  90. this.openInterval = setInterval(() => {
  91. if (global.stage.key_focus == this.Keyboard && this.Keyboard.prevKeyFocus != null) {
  92. global.stage.key_focus = this.Keyboard.prevKeyFocus
  93. }
  94. this.Keyboard.get_parent().set_child_at_index(this.Keyboard, this.Keyboard.get_parent().get_n_children() - 1);
  95. this.Keyboard.set_child_at_index(this.Keyboard.box, this.Keyboard.get_n_children() - 1);
  96. if (!this.Keyboard.openedFromButton && this.lastInputMethod) {
  97. if (Main.inputMethod.currentFocus != null && Main.inputMethod.currentFocus.is_focused() && !this.Keyboard.closedFromButton) {
  98. this._openKeyboard();
  99. } else if (!this.Keyboard.closedFromButton && !this.Keyboard._dragging) {
  100. this._closeKeyboard();
  101. this.Keyboard.closedFromButton = false
  102. } else if (Main.inputMethod.currentFocus == null) {
  103. this.Keyboard.closedFromButton = false
  104. }
  105. }
  106. }, 300);
  107. this.tapConnect = global.stage.connect("event", (_actor, event) => {
  108. if (event.type() !== 4 && event.type() !== 5) {
  109. this.lastInputMethod = [false, event.type() >= 9 && event.type() <= 12, true][this.settings.get_int("enable-tap-gesture")]
  110. }
  111. })
  112. }
  113. enable() {
  114. this.settings = this.getSettings();
  115. this.darkSchemeSettings = this.getSettings("org.gnome.desktop.interface");
  116. this.inputLanguageSettings = this.getSettings('org.gnome.desktop.input-sources')
  117. this.gnomeKeyboardSettings = this.getSettings('org.gnome.desktop.a11y.applications');
  118. this.isGnomeKeyboardEnabled = this.gnomeKeyboardSettings.get_boolean('screen-keyboard-enabled');
  119. this.gnomeKeyboardSettings.set_boolean('screen-keyboard-enabled', false)
  120. this.isGnomeKeyboardEnabledHandler = this.gnomeKeyboardSettings.connect('changed', () => {
  121. this.gnomeKeyboardSettings.set_boolean('screen-keyboard-enabled', false)
  122. });
  123. this.settings.scheme = ""
  124. if (this.darkSchemeSettings.get_string("color-scheme") == "prefer-dark")
  125. this.settings.scheme = "-dark"
  126. this.openBit = this.settings.get_child("indicator");
  127. this.openPrefs = () => { this.openPreferences() }
  128. let [okL, contentsL] = GLib.file_get_contents(this.path + '/physicalLayouts.json');
  129. if (okL) {
  130. layouts = JSON.parse(contentsL);
  131. }
  132. let refresh = () => {
  133. if (!Gio.File.new_for_path(this.path + "/keycodes").query_exists(null)) {
  134. Gio.File.new_for_path(this.path + "/keycodes").make_directory(null);
  135. let [status, out, err, code] = GLib.spawn_command_line_sync("tar -Jxf " + this.path + "/keycodes.tar.xz -C " + this.path + "/keycodes")
  136. if (err != "" || code != 0) {
  137. throw new Error(err);
  138. }
  139. }
  140. if (this.Keyboard)
  141. this.Keyboard.destroy();
  142. let [ok, contents] = GLib.file_get_contents(this.path + '/keycodes/' + KeyboardManager.getKeyboardManager().currentLayout.id + '.json');
  143. if (ok) {
  144. keycodes = JSON.parse(contents);
  145. }
  146. this.Keyboard = new Keyboard(this.settings, this);
  147. this.Keyboard.refresh = refresh
  148. }
  149. refresh()
  150. this._originalLastDeviceIsTouchscreen = KeyboardUI.KeyboardManager.prototype._lastDeviceIsTouchscreen;
  151. KeyboardUI.KeyboardManager.prototype._lastDeviceIsTouchscreen = () => { return false };
  152. this._indicator = null;
  153. this.openInterval = null;
  154. if (this.settings.get_boolean("indicator-enabled")) {
  155. this._indicator = new PanelMenu.Button(0.0, "GJS OSK Indicator", false);
  156. let icon = new St.Icon({
  157. gicon: new Gio.ThemedIcon({
  158. name: 'input-keyboard-symbolic'
  159. }),
  160. style_class: 'system-status-icon'
  161. });
  162. this._indicator.add_child(icon);
  163. this._indicator.connect("button-press-event", () => this._toggleKeyboard());
  164. this._indicator.connect("touch-event", (_actor, event) => {
  165. if (event.type() == Clutter.EventType.TOUCH_END) this._toggleKeyboard()
  166. });
  167. Main.panel.addToStatusArea("GJS OSK Indicator", this._indicator);
  168. }
  169. this._toggle = new KeyboardMenuToggle(this);
  170. this._quick_settings_indicator = new QuickSettings.SystemIndicator();
  171. this._quick_settings_indicator.quickSettingsItems.push(this._toggle);
  172. Main.panel.statusArea.quickSettings.addExternalIndicator(this._quick_settings_indicator);
  173. this.open_interval();
  174. this.openFromCommandHandler = this.openBit.connect("changed", () => {
  175. this.openBit.set_boolean("opened", false)
  176. this._toggleKeyboard();
  177. })
  178. let settingsChanged = () => {
  179. let opened = this.Keyboard.opened
  180. if (this.darkSchemeSettings.get_string("color-scheme") == "prefer-dark")
  181. this.settings.scheme = "-dark"
  182. else
  183. this.settings.scheme = ""
  184. this.Keyboard.openedFromButton = false;
  185. refresh()
  186. this._toggle._refresh();
  187. if (this.settings.get_boolean("indicator-enabled")) {
  188. if (this._indicator != null) {
  189. this._indicator.destroy();
  190. this._indicator = null;
  191. }
  192. this._indicator = new PanelMenu.Button(0.0, "GJS OSK Indicator", false);
  193. let icon = new St.Icon({
  194. gicon: new Gio.ThemedIcon({
  195. name: 'input-keyboard-symbolic'
  196. }),
  197. style_class: 'system-status-icon'
  198. });
  199. this._indicator.add_child(icon);
  200. this._indicator.connect("button-press-event", () => this._toggleKeyboard());
  201. this._indicator.connect("touch-event", (_actor, event) => {
  202. if (event.type() == Clutter.EventType.TOUCH_END) this._toggleKeyboard()
  203. });
  204. Main.panel.addToStatusArea("GJS OSK Indicator", this._indicator);
  205. } else {
  206. if (this._indicator != null) {
  207. this._indicator.destroy();
  208. this._indicator = null;
  209. }
  210. }
  211. global.stage.disconnect(this.tapConnect)
  212. if (this.openInterval !== null) {
  213. clearInterval(this.openInterval);
  214. this.openInterval = null;
  215. }
  216. this.open_interval();
  217. if (opened) {
  218. this._toggleKeyboard(true);
  219. }
  220. }
  221. this.settingsHandlers = [
  222. this.settings.connect("changed", settingsChanged),
  223. this.darkSchemeSettings.connect("changed", (_, key) => { if (key == "color-scheme") settingsChanged() }),
  224. this.inputLanguageSettings.connect("changed", settingsChanged)
  225. ];
  226. }
  227. disable() {
  228. this.gnomeKeyboardSettings.disconnect(this.isGnomeKeyboardEnabledHandler)
  229. this.gnomeKeyboardSettings.set_boolean('screen-keyboard-enabled', this.isGnomeKeyboardEnabled);
  230. this._quick_settings_indicator.quickSettingsItems.forEach(item => item.destroy());
  231. this._quick_settings_indicator.destroy();
  232. this._quick_settings_indicator = null;
  233. if (this._indicator !== null) {
  234. this._indicator.destroy();
  235. this._indicator = null;
  236. }
  237. this.Keyboard.destroy();
  238. this.settings.disconnect(this.settingsHandlers[0]);
  239. this.darkSchemeSettings.disconnect(this.settingsHandlers[1])
  240. this.inputLanguageSettings.disconnect(this.settingsHandlers[2])
  241. this.settings = null;
  242. this.darkSchemeSettings = null;
  243. this.inputLanguageSettings = null;
  244. this.gnomeKeyboardSettings = null;
  245. this.openBit.disconnect(this.openFromCommandHandler);
  246. this.openBit = null;
  247. global.stage.disconnect(this.tapConnect)
  248. if (this.openInterval !== null) {
  249. clearInterval(this.openInterval);
  250. this.openInterval = null;
  251. }
  252. this._toggle.destroy()
  253. this._toggle = null
  254. this.settings = null
  255. this.Keyboard = null
  256. keycodes = null
  257. if (this._originalLastDeviceIsTouchscreen !== null) {
  258. KeyboardUI.KeyboardManager.prototype._lastDeviceIsTouchscreen = this._originalLastDeviceIsTouchscreen;
  259. this._originalLastDeviceIsTouchscreen = null;
  260. }
  261. }
  262. }
  263. class Keyboard extends Dialog {
  264. static [GObject.signals] = {
  265. 'drag-begin': {},
  266. 'drag-end': {}
  267. };
  268. static {
  269. GObject.registerClass(this);
  270. }
  271. _init(settings, extensionObject) {
  272. this.settingsOpenFunction = extensionObject.openPrefs
  273. this.inputDevice = Clutter.get_default_backend().get_default_seat().create_virtual_device(Clutter.InputDeviceType.KEYBOARD_DEVICE);
  274. this.settings = settings;
  275. let monitor = Main.layoutManager.primaryMonitor;
  276. super._init(Main.layoutManager.modalDialogGroup, 'db-keyboard-content');
  277. this.box = new St.Widget({
  278. layout_manager: new Clutter.GridLayout({
  279. orientation: Clutter.Orientation.HORIZONTAL,
  280. row_spacing: settings.get_int("border-spacing-px") * 2,
  281. column_spacing: settings.get_int("border-spacing-px") * 2,
  282. })
  283. });
  284. this.widthPercent = (monitor.width > monitor.height) ? settings.get_int("landscape-width-percent") / 100 : settings.get_int("portrait-width-percent") / 100;
  285. this.heightPercent = (monitor.width > monitor.height) ? settings.get_int("landscape-height-percent") / 100 : settings.get_int("portrait-height-percent") / 100;
  286. this.buildUI();
  287. this.draggable = false;
  288. this.add_child(this.box);
  289. this.close();
  290. this.box.set_name("osk-gjs")
  291. this.mod = [];
  292. this.modBtns = [];
  293. this.capsL = false;
  294. this.shift = false;
  295. this.alt = false;
  296. this.opened = false;
  297. this.state = State.CLOSED;
  298. this.delta = [];
  299. this.checkMonitor();
  300. this._dragging = false;
  301. let side = null;
  302. switch (this.settings.get_int("default-snap")) {
  303. case 0:
  304. case 1:
  305. case 2:
  306. side = St.Side.TOP;
  307. break;
  308. case 3:
  309. side = St.Side.LEFT;
  310. break;
  311. case 5:
  312. side = St.Side.RIGHT;
  313. break;
  314. case 6:
  315. case 7:
  316. case 8:
  317. side = St.Side.BOTTOM;
  318. break;
  319. }
  320. this.oldBottomDragAction = global.stage.get_action('osk');
  321. if (this.oldBottomDragAction !== null && this.oldBottomDragAction instanceof Clutter.Action)
  322. global.stage.remove_action(this.oldBottomDragAction);
  323. if (side != null) {
  324. const mode = Shell.ActionMode.ALL & ~Shell.ActionMode.LOCK_SCREEN;
  325. const bottomDragAction = new EdgeDragAction.EdgeDragAction(side, mode);
  326. bottomDragAction.connect('activated', () => {
  327. this.open(true);
  328. this.openedFromButton = true;
  329. this.closedFromButton = false;
  330. this.gestureInProgress = false;
  331. });
  332. bottomDragAction.connect('progress', (_action, progress) => {
  333. if (!this.gestureInProgress)
  334. this.open(false)
  335. this.setOpenState(Math.min(Math.max(0, (progress / (side % 2 == 0 ? this.box.height : this.box.width)) * 100), 100))
  336. this.gestureInProgress = true;
  337. });
  338. bottomDragAction.connect('gesture-cancel', () => {
  339. if (this.gestureInProgress) {
  340. this.close()
  341. this.openedFromButton = false;
  342. this.closedFromButton = true;
  343. }
  344. this.gestureInProgress = false;
  345. return Clutter.EVENT_PROPAGATE;
  346. });
  347. global.stage.add_action_full('osk', Clutter.EventPhase.CAPTURE, bottomDragAction);
  348. this.bottomDragAction = bottomDragAction;
  349. } else {
  350. this.bottomDragAction = null;
  351. }
  352. this._oldMaybeHandleEvent = Main.keyboard.maybeHandleEvent
  353. Main.keyboard.maybeHandleEvent = (e) => {
  354. let lastInputMethod = [e.type() == 11, e.type() == 11, e.type() == 7 || e.type() == 11][this.settings.get_int("enable-tap-gesture")]
  355. let ac = global.stage.get_event_actor(e)
  356. if (this.contains(ac)) {
  357. ac.event(e, true);
  358. ac.event(e, false);
  359. return true;
  360. } else if (ac instanceof Clutter.Text && lastInputMethod && !this.opened) {
  361. this.open();
  362. }
  363. return false
  364. }
  365. }
  366. destroy() {
  367. Main.keyboard.maybeHandleEvent = this._oldMaybeHandleEvent
  368. global.stage.remove_action_by_name('osk')
  369. if (this.oldBottomDragAction !== null && this.oldBottomDragAction instanceof Clutter.Action)
  370. global.stage.add_action_full('osk', Clutter.EventPhase.CAPTURE, this.oldBottomDragAction)
  371. if (this.monitorChecker !== null) {
  372. clearInterval(this.monitorChecker);
  373. this.monitorChecker = null;
  374. }
  375. if (this.textboxChecker !== null) {
  376. clearInterval(this.textboxChecker);
  377. this.textboxChecker = null;
  378. }
  379. if (this.stateTimeout !== null) {
  380. clearTimeout(this.stateTimeout);
  381. this.stateTimeout = null;
  382. }
  383. if (this.keyTimeout !== null) {
  384. clearTimeout(this.keyTimeout);
  385. this.keyTimeout = null;
  386. }
  387. this.keymap.disconnect(this.capslockConnect);
  388. this.keymap.disconnect(this.numLockConnect);
  389. super.destroy();
  390. }
  391. startDragging(event, delta) {
  392. if (this.draggable) {
  393. if (this._dragging)
  394. return Clutter.EVENT_PROPAGATE;
  395. this._dragging = true;
  396. this.box.set_opacity(255);
  397. this.box.ease({
  398. opacity: 200,
  399. duration: 100,
  400. mode: Clutter.AnimationMode.EASE_OUT_QUAD,
  401. onComplete: () => { }
  402. });
  403. let device = event.get_device();
  404. let sequence = event.get_event_sequence();
  405. this._grab = global.stage.grab(this);
  406. this._grabbedDevice = device;
  407. this._grabbedSequence = sequence;
  408. this.emit('drag-begin');
  409. let [absX, absY] = event.get_coords();
  410. this.snapMovement(absX - delta[0], absY - delta[1]);
  411. return Clutter.EVENT_STOP;
  412. } else {
  413. return Clutter.EVENT_PROPAGATE;
  414. }
  415. }
  416. endDragging() {
  417. if (this.draggable) {
  418. if (this._dragging) {
  419. if (this._releaseId) {
  420. this.disconnect(this._releaseId);
  421. this._releaseId = 0;
  422. }
  423. if (this._grab) {
  424. this._grab.dismiss();
  425. this._grab = null;
  426. }
  427. this.box.set_opacity(200);
  428. this.box.ease({
  429. opacity: 255,
  430. duration: 100,
  431. mode: Clutter.AnimationMode.EASE_OUT_QUAD,
  432. onComplete: () => { }
  433. });
  434. this._grabbedSequence = null;
  435. this._grabbedDevice = null;
  436. this._dragging = false;
  437. this.delta = [];
  438. this.emit('drag-end');
  439. this._dragging = false;
  440. }
  441. this.draggable = false;
  442. return Clutter.EVENT_STOP;
  443. } else {
  444. return Clutter.EVENT_STOP;
  445. }
  446. }
  447. motionEvent(event) {
  448. if (this.draggable) {
  449. let [absX, absY] = event.get_coords();
  450. this.snapMovement(absX - this.delta[0], absY - this.delta[1]);
  451. return Clutter.EVENT_STOP
  452. } else {
  453. return Clutter.EVENT_STOP
  454. }
  455. }
  456. snapMovement(xPos, yPos) {
  457. let monitor = Main.layoutManager.primaryMonitor
  458. let snap_px = this.settings.get_int("snap-spacing-px")
  459. if (Math.abs(xPos - ((monitor.width * .5) - ((this.width * .5)))) <= 50) {
  460. xPos = ((monitor.width * .5) - ((this.width * .5)));
  461. } else if (Math.abs(xPos - snap_px) <= 50) {
  462. xPos = snap_px;
  463. } else if (Math.abs(xPos - (monitor.width - this.width - snap_px)) <= 50) {
  464. xPos = monitor.width - this.width - snap_px
  465. }
  466. if (Math.abs(yPos - (monitor.height - this.height - snap_px)) <= 50) {
  467. yPos = monitor.height - this.height - snap_px;
  468. } else if (Math.abs(yPos - snap_px) <= 50) {
  469. yPos = snap_px;
  470. } else if (Math.abs(yPos - ((monitor.height * .5) - (this.height * .5))) <= 50) {
  471. yPos = (monitor.height * .5) - (this.height * .5);
  472. }
  473. this.set_translation(xPos, yPos, 0);
  474. }
  475. checkMonitor() {
  476. let monitor = Main.layoutManager.primaryMonitor;
  477. let oldMonitorDimensions = [monitor.width, monitor.height];
  478. this.monitorChecker = setInterval(() => {
  479. monitor = Main.layoutManager.primaryMonitor;
  480. if (oldMonitorDimensions[0] != monitor.width || oldMonitorDimensions[1] != monitor.height) {
  481. this.refresh()
  482. oldMonitorDimensions = [monitor.width, monitor.height];
  483. }
  484. }, 200);
  485. }
  486. setOpenState(percent) {
  487. let monitor = Main.layoutManager.primaryMonitor;
  488. let posX = [this.settings.get_int("snap-spacing-px"), ((monitor.width * .5) - ((this.width * .5))), monitor.width - this.width - this.settings.get_int("snap-spacing-px")][(this.settings.get_int("default-snap") % 3)];
  489. let posY = [this.settings.get_int("snap-spacing-px"), ((monitor.height * .5) - ((this.height * .5))), monitor.height - this.height - this.settings.get_int("snap-spacing-px")][Math.floor((this.settings.get_int("default-snap") / 3))];
  490. let mX = [-this.box.width, 0, this.box.width][(this.settings.get_int("default-snap") % 3)];
  491. let mY = [-this.box.height, 0, this.box.height][Math.floor((this.settings.get_int("default-snap") / 3))]
  492. let [dx, dy] = [posX + mX * ((100 - percent) / 100), posY + mY * ((100 - percent) / 100)]
  493. let op = 255 * (percent / 100);
  494. this.set_translation(dx, dy, 0)
  495. this.box.set_opacity(op)
  496. }
  497. open(noPrep = null, instant = null) {
  498. if (noPrep == null || !noPrep) {
  499. this.prevKeyFocus = global.stage.key_focus
  500. this.inputDevice = Clutter.get_default_backend().get_default_seat().create_virtual_device(Clutter.InputDeviceType.KEYBOARD_DEVICE);
  501. this.state = State.OPENING
  502. this.show();
  503. }
  504. if (noPrep == null || noPrep) {
  505. let monitor = Main.layoutManager.primaryMonitor;
  506. let posX = [this.settings.get_int("snap-spacing-px"), ((monitor.width * .5) - ((this.width * .5))), monitor.width - this.width - this.settings.get_int("snap-spacing-px")][(this.settings.get_int("default-snap") % 3)];
  507. let posY = [this.settings.get_int("snap-spacing-px"), ((monitor.height * .5) - ((this.height * .5))), monitor.height - this.height - this.settings.get_int("snap-spacing-px")][Math.floor((this.settings.get_int("default-snap") / 3))];
  508. if (noPrep == null) {
  509. let mX = [-this.box.width, 0, this.box.width][(this.settings.get_int("default-snap") % 3)];
  510. let mY = [-this.box.height, 0, this.box.height][Math.floor((this.settings.get_int("default-snap") / 3))]
  511. this.set_translation(posX + mX, posY + mY, 0)
  512. }
  513. this.box.ease({
  514. opacity: 255,
  515. duration: instant == null || !instant ? 100 : 0,
  516. mode: Clutter.AnimationMode.EASE_OUT_QUAD,
  517. onComplete: () => {
  518. if (this.stateTimeout !== null) {
  519. clearTimeout(this.stateTimeout);
  520. this.stateTimeout = null;
  521. }
  522. this.stateTimeout = setTimeout(() => {
  523. this.state = State.OPENED
  524. }, 500);
  525. }
  526. });
  527. this.ease({
  528. translation_x: posX,
  529. translation_y: posY,
  530. duration: instant == null || !instant ? 100 : 0,
  531. mode: Clutter.AnimationMode.EASE_OUT_QUAD
  532. })
  533. this.opened = true;
  534. }
  535. }
  536. close(instant = null) {
  537. this.prevKeyFocus = null;
  538. let monitor = Main.layoutManager.primaryMonitor;
  539. let posX = [this.settings.get_int("snap-spacing-px"), ((monitor.width * .5) - ((this.width * .5))), monitor.width - this.width - this.settings.get_int("snap-spacing-px")][(this.settings.get_int("default-snap") % 3)];
  540. let posY = [this.settings.get_int("snap-spacing-px"), ((monitor.height * .5) - ((this.height * .5))), monitor.height - this.height - this.settings.get_int("snap-spacing-px")][Math.floor((this.settings.get_int("default-snap") / 3))];
  541. let mX = [-this.box.width, 0, this.box.width][(this.settings.get_int("default-snap") % 3)];
  542. let mY = [-this.box.height, 0, this.box.height][Math.floor((this.settings.get_int("default-snap") / 3))]
  543. this.state = State.CLOSING
  544. this.box.ease({
  545. opacity: 0,
  546. duration: instant == null || !instant ? 100 : 0,
  547. mode: Clutter.AnimationMode.EASE_OUT_QUAD,
  548. onComplete: () => {
  549. this.opened = false;
  550. this.hide();
  551. if (this.stateTimeout !== null) {
  552. clearTimeout(this.stateTimeout);
  553. this.stateTimeout = null;
  554. }
  555. this.stateTimeout = setTimeout(() => {
  556. this.state = State.CLOSED
  557. }, 500);
  558. },
  559. });
  560. this.ease({
  561. translation_x: posX + mX,
  562. translation_y: posY + mY,
  563. duration: instant == null || !instant ? 100 : 0,
  564. mode: Clutter.AnimationMode.EASE_OUT_QUAD
  565. })
  566. this.openedFromButton = false
  567. this.releaseAllKeys();
  568. }
  569. vfunc_button_press_event() {
  570. this.delta = [Clutter.get_current_event().get_coords()[0] - this.translation_x, Clutter.get_current_event().get_coords()[1] - this.translation_y];
  571. return this.startDragging(Clutter.get_current_event(), this.delta)
  572. }
  573. vfunc_button_release_event() {
  574. if (this._dragging && !this._grabbedSequence) {
  575. return this.endDragging();
  576. }
  577. return Clutter.EVENT_PROPAGATE;
  578. }
  579. vfunc_motion_event() {
  580. let event = Clutter.get_current_event();
  581. if (this._dragging && !this._grabbedSequence) {
  582. this.motionEvent(event);
  583. }
  584. return Clutter.EVENT_PROPAGATE;
  585. }
  586. vfunc_touch_event() {
  587. let event = Clutter.get_current_event();
  588. let sequence = event.get_event_sequence();
  589. if (!this._dragging && event.type() == Clutter.EventType.TOUCH_BEGIN) {
  590. this.delta = [event.get_coords()[0] - this.translation_x, event.get_coords()[1] - this.translation_y];
  591. this.startDragging(event, this.delta);
  592. return Clutter.EVENT_STOP;
  593. } else if (this._grabbedSequence && sequence.get_slot() === this._grabbedSequence.get_slot()) {
  594. if (event.type() == Clutter.EventType.TOUCH_UPDATE) {
  595. return this.motionEvent(event);
  596. } else if (event.type() == Clutter.EventType.TOUCH_END) {
  597. return this.endDragging();
  598. }
  599. }
  600. return Clutter.EVENT_PROPAGATE;
  601. }
  602. buildUI() {
  603. this.box.set_opacity(0);
  604. this.keys = [];
  605. let layoutName = Object.keys(layouts)[this.settings.get_int("layout")];
  606. let monitor = Main.layoutManager.primaryMonitor
  607. this.box.width = Math.round((monitor.width - this.settings.get_int("snap-spacing-px") * 2) * (layoutName.includes("Split") ? 1 : this.widthPercent))
  608. this.box.height = Math.round((monitor.height - this.settings.get_int("snap-spacing-px") * 2) * this.heightPercent)
  609. const grid = this.box.layout_manager
  610. grid.set_row_homogeneous(true)
  611. grid.set_column_homogeneous(!layoutName.includes("Split"))
  612. let gridLeft;
  613. let gridRight;
  614. let currentGrid = grid;
  615. let left;
  616. let right;
  617. if (layoutName.includes("Split")) {
  618. left = new St.Widget({
  619. layout_manager: new Clutter.GridLayout({
  620. orientation: Clutter.Orientation.HORIZONTAL,
  621. row_spacing: this.settings.get_int("border-spacing-px") * 2,
  622. column_spacing: this.settings.get_int("border-spacing-px") * 2,
  623. row_homogeneous: true,
  624. column_homogeneous: true
  625. }),
  626. width: Math.round((monitor.width - this.settings.get_int("snap-spacing-px") * 2) * this.widthPercent) / 2
  627. })
  628. gridLeft = left.layout_manager;
  629. let middle = new St.Widget({
  630. width: this.box.width * (1 - this.widthPercent) - 10 + this.settings.get_int("border-spacing-px")
  631. });
  632. right = new St.Widget({
  633. layout_manager: new Clutter.GridLayout({
  634. orientation: Clutter.Orientation.HORIZONTAL,
  635. row_spacing: this.settings.get_int("border-spacing-px") * 2,
  636. column_spacing: this.settings.get_int("border-spacing-px") * 2,
  637. row_homogeneous: true,
  638. column_homogeneous: true
  639. }),
  640. width: Math.round((monitor.width - this.settings.get_int("snap-spacing-px") * 2) * this.widthPercent) / 2
  641. })
  642. gridRight = right.layout_manager;
  643. this.box.add_child(left)
  644. this.box.add_child(middle)
  645. this.box.add_child(right)
  646. }
  647. this.shiftButtons = [];
  648. let currentLayout = layouts[layoutName];
  649. let width = 0;
  650. for (const c of currentLayout[0]) {
  651. width += (Object.hasOwn(c, "width") ? c.width : 1)
  652. }
  653. let rowSize;
  654. let halfSize;
  655. let r = 0;
  656. let c;
  657. const doAddKey = (keydef) => {
  658. const i = Object.hasOwn(keydef, "key") ? keycodes[keydef.key] : Object.hasOwn(keydef, "split") ? "split" : "empty space";
  659. if (i != null && typeof i !== 'string') {
  660. if (i.layers.default == null) {
  661. for (var key of Object.keys(i.layers)) {
  662. i.layers[key] = i.layers["_" + key]
  663. }
  664. }
  665. let params = {
  666. x_expand: true,
  667. y_expand: true
  668. }
  669. let iconKeys = ["left", "up", "right", "down", "space"]
  670. if (this.settings.get_boolean("show-icons")) {
  671. iconKeys = ["left", "up", "right", "down", "backspace", "tab", "capslock", "shift", "enter", "ctrl", "super", "alt", "space"]
  672. }
  673. if (iconKeys.some(j => { return i.layers.default.toLowerCase() == j })) {
  674. params.style_class = i.layers.default.toLowerCase() + "_btn"
  675. for (var key of Object.keys(i.layers)) {
  676. i.layers["_" + key] = i.layers[key]
  677. i.layers[key] = null
  678. }
  679. } else {
  680. params.label = i.layers.default
  681. }
  682. i.isMod = false
  683. if ([42, 54, 29, 125, 56, 100, 97, 58, 69].some(j => { return i.code == j })) {
  684. i.isMod = true;
  685. }
  686. const keyBtn = new St.Button(params)
  687. keyBtn.add_style_class_name('key')
  688. keyBtn.char = i
  689. if (i.code == 58) {
  690. this.keymap = Clutter.get_default_backend().get_default_seat().get_keymap()
  691. this.capslockConnect = this.keymap.connect("state-changed", (a, e) => {
  692. this.setCapsLock(keyBtn, this.keymap.get_caps_lock_state())
  693. })
  694. } else if (i.code == 69) {
  695. this.keymap = Clutter.get_default_backend().get_default_seat().get_keymap()
  696. this.numLockConnect = this.keymap.connect("state-changed", (a, e) => {
  697. this.setNumLock(keyBtn, this.keymap.get_num_lock_state())
  698. })
  699. } else if (i.code == 42 || i.code == 54) {
  700. this.shiftButtons.push(keyBtn)
  701. }
  702. currentGrid.attach(keyBtn, c, 6 + r, (Object.hasOwn(keydef, "width") ? keydef.width : 1) * 8, r == 0 ? 6 : (Object.hasOwn(keydef, "height") ? keydef.height : 1) * 8)
  703. keyBtn.visible = true
  704. c += (Object.hasOwn(keydef, "width") ? keydef.width : 1) * 8
  705. this.keys.push(keyBtn)
  706. } else if (i == "empty space") {
  707. c += (Object.hasOwn(keydef, "width") ? keydef.width : 1) * 8
  708. } else if (i == "split") {
  709. currentGrid = gridRight
  710. const size = c
  711. if (!halfSize) halfSize = size
  712. }
  713. }
  714. for (const kRow of currentLayout) {
  715. c = 0;
  716. if (layoutName.includes("Split")) {
  717. currentGrid = gridLeft;
  718. }
  719. for (const keydef of kRow) {
  720. if (keydef instanceof Array) {
  721. keydef.forEach(i => {doAddKey(i); r += 4; c -= 8});
  722. c += 8;
  723. r -= 8;
  724. } else {
  725. doAddKey(keydef)
  726. }
  727. }
  728. const size = c;
  729. if (!rowSize) rowSize = size;
  730. r += r == 0 ? 6 : 8
  731. }
  732. if (left != null) {
  733. this.set_reactive(false)
  734. left.add_style_class_name("boxLay");
  735. left.set_style("background-color: rgba(" + this.settings.get_double("background-r" + this.settings.scheme) + "," + this.settings.get_double("background-g" + this.settings.scheme) + "," + this.settings.get_double("background-b" + this.settings.scheme) + ", " + this.settings.get_double("background-a" + this.settings.scheme) + ");")
  736. right.add_style_class_name("boxLay");
  737. right.set_style("background-color: rgba(" + this.settings.get_double("background-r" + this.settings.scheme) + "," + this.settings.get_double("background-g" + this.settings.scheme) + "," + this.settings.get_double("background-b" + this.settings.scheme) + ", " + this.settings.get_double("background-a" + this.settings.scheme) + ");")
  738. if (this.lightOrDark(this.settings.get_double("background-r" + this.settings.scheme), this.settings.get_double("background-g" + this.settings.scheme), this.settings.get_double("background-b" + this.settings.scheme))) {
  739. left.add_style_class_name("inverted");
  740. right.add_style_class_name("inverted");
  741. } else {
  742. left.add_style_class_name("regular");
  743. right.add_style_class_name("regular");
  744. }
  745. const settingsBtn = new St.Button({
  746. x_expand: true,
  747. y_expand: true
  748. })
  749. settingsBtn.add_style_class_name("settings_btn")
  750. settingsBtn.add_style_class_name("key")
  751. settingsBtn.connect("clicked", () => {
  752. this.settingsOpenFunction();
  753. })
  754. gridLeft.attach(settingsBtn, 0, 0, 8, 5)
  755. this.keys.push(settingsBtn)
  756. const closeBtn = new St.Button({
  757. x_expand: true,
  758. y_expand: true
  759. })
  760. closeBtn.add_style_class_name("close_btn")
  761. closeBtn.add_style_class_name("key")
  762. closeBtn.connect("clicked", () => {
  763. this.close();
  764. this.closedFromButton = true;
  765. })
  766. gridRight.attach(closeBtn, (rowSize - 8), 0, 8, 5)
  767. this.keys.push(closeBtn)
  768. let moveHandleLeft = new St.Button({
  769. x_expand: true,
  770. y_expand: true
  771. })
  772. moveHandleLeft.add_style_class_name("moveHandle")
  773. moveHandleLeft.set_style("font-size: " + this.settings.get_int("font-size-px") + "px; border-radius: " + (this.settings.get_boolean("round-key-corners") ? "5px;" : "0;") + "background-size: " + this.settings.get_int("font-size-px") + "px;");
  774. if (this.lightOrDark(this.settings.get_double("background-r" + this.settings.scheme), this.settings.get_double("background-g" + this.settings.scheme), this.settings.get_double("background-b" + this.settings.scheme))) {
  775. moveHandleLeft.add_style_class_name("inverted");
  776. } else {
  777. moveHandleLeft.add_style_class_name("regular");
  778. }
  779. moveHandleLeft.connect("event", (actor, event) => {
  780. if (event.type() == Clutter.EventType.BUTTON_PRESS || event.type() == Clutter.EventType.TOUCH_BEGIN) {
  781. this.draggable = this.settings.get_boolean("enable-drag");
  782. }
  783. this.event(event, false)
  784. })
  785. gridLeft.attach(moveHandleLeft, 8, 0, (halfSize - 8), 5)
  786. let moveHandleRight = new St.Button({
  787. x_expand: true,
  788. y_expand: true
  789. })
  790. moveHandleRight.add_style_class_name("moveHandle")
  791. moveHandleRight.set_style("font-size: " + this.settings.get_int("font-size-px") + "px; border-radius: " + (this.settings.get_boolean("round-key-corners") ? "5px;" : "0;") + "background-size: " + this.settings.get_int("font-size-px") + "px;");
  792. if (this.lightOrDark(this.settings.get_double("background-r" + this.settings.scheme), this.settings.get_double("background-g" + this.settings.scheme), this.settings.get_double("background-b" + this.settings.scheme))) {
  793. moveHandleRight.add_style_class_name("inverted");
  794. } else {
  795. moveHandleRight.add_style_class_name("regular");
  796. }
  797. moveHandleRight.connect("event", (actor, event) => {
  798. if (event.type() == Clutter.EventType.BUTTON_PRESS || event.type() == Clutter.EventType.TOUCH_BEGIN) {
  799. this.draggable = this.settings.get_boolean("enable-drag");
  800. }
  801. this.event(event, false)
  802. })
  803. gridRight.attach(moveHandleRight, (rowSize - halfSize), 0, (rowSize - halfSize - 4), 5)
  804. gridLeft.attach(new St.Widget({x_expand: true, y_expand: true}), 0, 5, halfSize, 1)
  805. gridRight.attach(new St.Widget({x_expand: true, y_expand: true}), (rowSize - halfSize), 5, (rowSize - halfSize + 4), 1)
  806. } else {
  807. this.box.add_style_class_name("boxLay");
  808. this.box.set_style("background-color: rgba(" + this.settings.get_double("background-r" + this.settings.scheme) + "," + this.settings.get_double("background-g" + this.settings.scheme) + "," + this.settings.get_double("background-b" + this.settings.scheme) + ", " + this.settings.get_double("background-a" + this.settings.scheme) + ");")
  809. if (this.lightOrDark(this.settings.get_double("background-r" + this.settings.scheme), this.settings.get_double("background-g" + this.settings.scheme), this.settings.get_double("background-b" + this.settings.scheme))) {
  810. this.box.add_style_class_name("inverted");
  811. } else {
  812. this.box.add_style_class_name("regular");
  813. }
  814. const settingsBtn = new St.Button({
  815. x_expand: true,
  816. y_expand: true
  817. })
  818. settingsBtn.add_style_class_name("settings_btn")
  819. settingsBtn.add_style_class_name("key")
  820. settingsBtn.connect("clicked", () => {
  821. this.settingsOpenFunction();
  822. })
  823. grid.attach(settingsBtn, 0, 0, 8, 5)
  824. this.keys.push(settingsBtn)
  825. const closeBtn = new St.Button({
  826. x_expand: true,
  827. y_expand: true
  828. })
  829. closeBtn.add_style_class_name("close_btn")
  830. closeBtn.add_style_class_name("key")
  831. closeBtn.connect("clicked", () => {
  832. this.close();
  833. this.closedFromButton = true;
  834. })
  835. grid.attach(closeBtn, (rowSize - 8), 0, 8, 5)
  836. this.keys.push(closeBtn)
  837. let moveHandle= new St.Button({
  838. x_expand: true,
  839. y_expand: true
  840. })
  841. moveHandle.add_style_class_name("moveHandle")
  842. moveHandle.set_style("font-size: " + this.settings.get_int("font-size-px") + "px; border-radius: " + (this.settings.get_boolean("round-key-corners") ? "5px;" : "0;") + "background-size: " + this.settings.get_int("font-size-px") + "px;");
  843. if (this.lightOrDark(this.settings.get_double("background-r" + this.settings.scheme), this.settings.get_double("background-g" + this.settings.scheme), this.settings.get_double("background-b" + this.settings.scheme))) {
  844. moveHandle.add_style_class_name("inverted");
  845. } else {
  846. moveHandle.add_style_class_name("regular");
  847. }
  848. moveHandle.connect("event", (actor, event) => {
  849. if (event.type() == Clutter.EventType.BUTTON_PRESS || event.type() == Clutter.EventType.TOUCH_BEGIN) {
  850. this.draggable = this.settings.get_boolean("enable-drag");
  851. }
  852. this.event(event, false)
  853. })
  854. grid.attach(moveHandle, 8, 0, (rowSize - 16), 5)
  855. grid.attach(new St.Widget({x_expand: true, y_expand: true}), 0, 5, rowSize, 1)
  856. }
  857. this.keys.forEach(item => {
  858. item.set_style("font-size: " + this.settings.get_int("font-size-px") + "px; border-radius: " + (this.settings.get_boolean("round-key-corners") ? "5px;" : "0;") + "background-size: " + this.settings.get_int("font-size-px") + "px; font-weight: " + (this.settings.get_boolean("font-bold") ? "bold" : "normal") + ";");
  859. if (this.lightOrDark(this.settings.get_double("background-r" + this.settings.scheme), this.settings.get_double("background-g" + this.settings.scheme), this.settings.get_double("background-b" + this.settings.scheme))) {
  860. item.add_style_class_name("inverted");
  861. } else {
  862. item.add_style_class_name("regular");
  863. }
  864. item.set_pivot_point(0.5, 0.5)
  865. item.connect("destroy", () => {
  866. if (item.button_pressed !== null) {
  867. clearTimeout(item.button_pressed)
  868. item.button_pressed == null
  869. }
  870. if (item.button_repeat !== null) {
  871. clearInterval(item.button_repeat)
  872. item.button_repeat == null
  873. }
  874. if (item.tap_pressed !== null) {
  875. clearTimeout(item.tap_pressed)
  876. item.tap_pressed == null
  877. }
  878. if (item.tap_repeat !== null) {
  879. clearInterval(item.tap_repeat)
  880. item.tap_repeat == null
  881. }
  882. })
  883. let pressEv = (evType) => {
  884. item.space_motion_handler = null
  885. item.set_scale(1.2, 1.2)
  886. item.add_style_pseudo_class("pressed")
  887. let player
  888. if (this.settings.get_boolean("play-sound")) {
  889. player = global.display.get_sound_player();
  890. player.play_from_theme("dialog-information", "tap", null)
  891. }
  892. if (["delete_btn", "backspace_btn", "up_btn", "down_btn", "left_btn", "right_btn"].some(e => item.has_style_class_name(e))) {
  893. item.button_pressed = setTimeout(() => {
  894. const oldModBtns = this.modBtns
  895. item.button_repeat = setInterval(() => {
  896. if (this.settings.get_boolean("play-sound")) {
  897. player.play_from_theme("dialog-information", "tap", null)
  898. }
  899. this.decideMod(item.char)
  900. for (var i of oldModBtns) {
  901. this.decideMod(i.char, i)
  902. }
  903. }, 100);
  904. }, 750);
  905. } else if (item.has_style_class_name("space_btn")) {
  906. item.button_pressed = setTimeout(() => {
  907. let lastPos = (item.get_transformed_position()[0] + item.get_transformed_size()[0] / 2)
  908. if (evType == "mouse") {
  909. item.space_motion_handler = item.connect("motion_event", (actor, event) => {
  910. let absX = event.get_coords()[0];
  911. if (Math.abs(absX - lastPos) > 20) {
  912. if (absX > lastPos) {
  913. this.sendKey([106])
  914. } else {
  915. this.sendKey([105])
  916. }
  917. lastPos = absX
  918. }
  919. })
  920. } else {
  921. item.space_motion_handler = item.connect("touch_event", (actor, event) => {
  922. if (event.type() == Clutter.EventType.TOUCH_UPDATE) {
  923. let absX = event.get_coords()[0];
  924. if (Math.abs(absX - lastPos) > 20) {
  925. if (absX > lastPos) {
  926. this.sendKey([106])
  927. } else {
  928. this.sendKey([105])
  929. }
  930. lastPos = absX
  931. }
  932. }
  933. })
  934. }
  935. }, 750)
  936. } else {
  937. item.key_pressed = true;
  938. item.button_pressed = setTimeout(() => {
  939. releaseEv()
  940. }, 1000);
  941. }
  942. }
  943. let releaseEv = () => {
  944. item.remove_style_pseudo_class("pressed")
  945. item.ease({
  946. scale_x: 1,
  947. scale_y: 1,
  948. duration: 100,
  949. mode: Clutter.AnimationMode.EASE_OUT_QUAD,
  950. onComplete: () => { item.set_scale(1, 1); }
  951. })
  952. if (item.button_pressed !== null) {
  953. clearTimeout(item.button_pressed)
  954. item.button_pressed == null
  955. }
  956. if (item.button_repeat !== null) {
  957. clearInterval(item.button_repeat)
  958. item.button_repeat == null
  959. }
  960. if (item.space_motion_handler !== null) {
  961. item.disconnect(item.space_motion_handler)
  962. item.space_motion_handler = null;
  963. } else if (item.key_pressed == true || item.space_motion_handler == null) {
  964. try {
  965. if (!item.char.isMod) {
  966. this.decideMod(item.char)
  967. } else {
  968. const modButton = item;
  969. this.decideMod(item.char, modButton)
  970. }
  971. } catch { }
  972. }
  973. item.key_pressed = false;
  974. }
  975. item.connect("button-press-event", () => pressEv("mouse"))
  976. item.connect("button-release-event", releaseEv)
  977. item.connect("touch-event", () => {
  978. if (Clutter.get_current_event().type() == Clutter.EventType.TOUCH_BEGIN) {
  979. pressEv("touch")
  980. } else if (Clutter.get_current_event().type() == Clutter.EventType.TOUCH_END || Clutter.get_current_event().type() == Clutter.EventType.TOUCH_CANCEL) {
  981. releaseEv()
  982. }
  983. })
  984. });
  985. }
  986. lightOrDark(r, g, b) {
  987. var hsp;
  988. hsp = Math.sqrt(
  989. 0.299 * (r * r) +
  990. 0.587 * (g * g) +
  991. 0.114 * (b * b)
  992. );
  993. if (hsp > 127.5) {
  994. return true;
  995. } else {
  996. return false;
  997. }
  998. }
  999. releaseAllKeys() {
  1000. let instances = [];
  1001. function traverse(obj) {
  1002. for (let key in obj) {
  1003. if (obj.hasOwnProperty(key)) {
  1004. if (key === "code") {
  1005. instances.push(obj[key]);
  1006. } else if (typeof obj[key] === 'object' && obj[key] !== null) {
  1007. traverse(obj[key]);
  1008. }
  1009. }
  1010. }
  1011. }
  1012. traverse(keycodes);
  1013. instances.forEach(i => {
  1014. this.inputDevice.notify_key(Clutter.get_current_event_time(), i, Clutter.KeyState.RELEASED);
  1015. })
  1016. this.keys.forEach(item => {
  1017. item.key_pressed = false;
  1018. if (item.button_pressed !== null) {
  1019. clearTimeout(item.button_pressed)
  1020. item.button_pressed == null
  1021. }
  1022. if (item.button_repeat !== null) {
  1023. clearInterval(item.button_repeat)
  1024. item.button_repeat == null
  1025. }
  1026. if (item.space_motion_handler !== null) {
  1027. item.disconnect(item.space_motion_handler)
  1028. item.space_motion_handler = null;
  1029. }
  1030. })
  1031. }
  1032. sendKey(keys) {
  1033. try {
  1034. for (var i = 0; i < keys.length; i++) {
  1035. this.inputDevice.notify_key(Clutter.get_current_event_time(), keys[i], Clutter.KeyState.PRESSED);
  1036. }
  1037. if (this.keyTimeout !== null) {
  1038. clearTimeout(this.keyTimeout);
  1039. this.keyTimeout = null;
  1040. }
  1041. this.keyTimeout = setTimeout(() => {
  1042. for (var j = keys.length - 1; j >= 0; j--) {
  1043. this.inputDevice.notify_key(Clutter.get_current_event_time(), keys[j], Clutter.KeyState.RELEASED);
  1044. }
  1045. }, 100);
  1046. } catch (err) {
  1047. throw new Error("GJS-OSK: An unknown error occured. Please report this bug to the Issues page (https://github.com/Vishram1123/gjs-osk/issues):\n\n" + err + "\n\nKeys Pressed: " + keys);
  1048. }
  1049. }
  1050. decideMod(i, mBtn) {
  1051. if (i.code == 29 || i.code == 56 || i.code == 97 || i.code == 125) {
  1052. this.setNormMod(mBtn);
  1053. } else if (i.code == 100) {
  1054. this.setAlt(mBtn);
  1055. } else if (i.code == 42 || i.code == 54) {
  1056. this.setShift(mBtn);
  1057. } else if (i.code == 58 || i.code == 69) {
  1058. this.sendKey([mBtn.char.code]);
  1059. } else {
  1060. this.mod.push(i.code);
  1061. this.sendKey(this.mod);
  1062. this.mod = [];
  1063. this.modBtns.forEach(button => {
  1064. button.remove_style_class_name("selected");
  1065. });
  1066. this.shiftButtons.forEach(i => { i.remove_style_class_name("selected") })
  1067. this.resetAllMod();
  1068. this.modBtns = [];
  1069. }
  1070. }
  1071. setCapsLock(button, state) {
  1072. if (state) {
  1073. button.add_style_class_name("selected");
  1074. this.capsL = true;
  1075. } else {
  1076. button.remove_style_class_name("selected");
  1077. this.capsL = false;
  1078. }
  1079. this.updateKeyLabels();
  1080. }
  1081. setNumLock(button, state) {
  1082. if (state) {
  1083. button.add_style_class_name("selected");
  1084. this.numsL = true;
  1085. } else {
  1086. button.remove_style_class_name("selected");
  1087. this.numsL = false;
  1088. }
  1089. this.updateKeyLabels();
  1090. }
  1091. setAlt(button) {
  1092. this.alt = !this.alt;
  1093. this.updateKeyLabels();
  1094. if (!this.alt) {
  1095. this.sendKey([button.char.code]);
  1096. }
  1097. this.setNormMod(button);
  1098. }
  1099. setShift(button) {
  1100. this.shift = !this.shift;
  1101. this.updateKeyLabels();
  1102. if (!this.shift) {
  1103. this.sendKey([button.char.code]);
  1104. this.shiftButtons.forEach(i => { i.remove_style_class_name("selected") })
  1105. } else {
  1106. this.shiftButtons.forEach(i => { i.add_style_class_name("selected") })
  1107. }
  1108. this.setNormMod(button);
  1109. }
  1110. updateKeyLabels() {
  1111. this.keys.forEach(key => {
  1112. if (key.char != undefined) {
  1113. let layer = (this.alt ? 'alt' : '') + (this.shift ? 'shift' : '') + (this.numsL ? 'num' : '') + (this.capsL ? 'caps' : '') + (this.numsL || this.capsL ? 'lock' : '')
  1114. if (layer == '') layer = 'default'
  1115. key.label = key.char.layers[layer];
  1116. }
  1117. });
  1118. }
  1119. setNormMod(button) {
  1120. if (this.mod.includes(button.char.code)) {
  1121. this.mod.splice(this.mod.indexOf(button.char.code), this.mod.indexOf(button.char.code) + 1);
  1122. if (!(button.char.code == 42) && !(button.char.code == 54))
  1123. button.remove_style_class_name("selected");
  1124. this.modBtns.splice(this.modBtns.indexOf(button), this.modBtns.indexOf(button) + 1);
  1125. this.sendKey([button.char.code]);
  1126. } else {
  1127. if (!(button.char.code == 42) && !(button.char.code == 54))
  1128. button.add_style_class_name("selected");
  1129. this.mod.push(button.char.code);
  1130. this.modBtns.push(button);
  1131. }
  1132. }
  1133. resetAllMod() {
  1134. this.shift = false;
  1135. this.alt = false;
  1136. this.updateKeyLabels()
  1137. }
  1138. }