extension.js 60 KB

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