From a06d3186460f8df7fa260e19b1161bada7831199 Mon Sep 17 00:00:00 2001 From: abakkk Date: Thu, 10 Sep 2020 15:55:19 +0200 Subject: [PATCH] rework of shortcuts.js --- helper.js | 12 +++--- prefs.js | 12 +++--- shortcuts.js | 116 +++++++++++++++++++++++++-------------------------- 3 files changed, 70 insertions(+), 70 deletions(-) diff --git a/helper.js b/helper.js index 70d93d7..91d77fe 100644 --- a/helper.js +++ b/helper.js @@ -31,7 +31,7 @@ const Tweener = imports.ui.tweener; const Me = ExtensionUtils.getCurrentExtension(); const Convenience = ExtensionUtils.getSettings ? ExtensionUtils : Me.imports.convenience; -const Shortcuts = Me.imports.shortcuts.Shortcuts; +const Shortcuts = Me.imports.shortcuts; const _ = imports.gettext.domain(Me.metadata['gettext-domain']).gettext; const GS_VERSION = Config.PACKAGE_VERSION; @@ -106,17 +106,17 @@ var DrawingHelper = new Lang.Class({ this.vbox.add_child(new St.BoxLayout({ vertical: false, style_class: 'draw-on-your-screen-helper-separator' })); this.vbox.add_child(new St.Label({ text: _("Internal") })); - Shortcuts.OTHERS.forEach((pairs, index) => { + Shortcuts.OTHERS.forEach((object, index) => { if (index) this.vbox.add_child(new St.BoxLayout({ vertical: false, style_class: 'draw-on-your-screen-helper-separator' })); - pairs.forEach(pair => { + for (let key in object) { let hbox = new St.BoxLayout({ vertical: false }); - hbox.add_child(new St.Label({ text: pair[0] })); - hbox.add_child(new St.Label({ text: pair[1], x_expand: true })); + hbox.add_child(new St.Label({ text: key })); + hbox.add_child(new St.Label({ text: object[key], x_expand: true })); hbox.get_children()[0].get_clutter_text().set_use_markup(true); this.vbox.add_child(hbox); - }); + } }); this.vbox.add_child(new St.BoxLayout({ vertical: false, style_class: 'draw-on-your-screen-helper-separator' })); diff --git a/prefs.js b/prefs.js index dea0c8f..a8d4c30 100644 --- a/prefs.js +++ b/prefs.js @@ -30,7 +30,7 @@ const Gtk = imports.gi.Gtk; const ExtensionUtils = imports.misc.extensionUtils; const Me = ExtensionUtils.getCurrentExtension(); const Convenience = ExtensionUtils.getSettings && ExtensionUtils.initTranslations ? ExtensionUtils : Me.imports.convenience; -const Shortcuts = Me.imports.shortcuts.Shortcuts; +const Shortcuts = Me.imports.shortcuts; const gettext = imports.gettext.domain(Me.metadata['gettext-domain']).gettext; const _ = function(string) { if (!string) @@ -421,19 +421,19 @@ const PrefsPage = new GObject.Class({ listBox.get_style_context().add_class('background'); internalFrame.add(listBox); - Shortcuts.OTHERS.forEach((pairs, index) => { + Shortcuts.OTHERS.forEach((object, index) => { if (index) listBox.add(new Gtk.Box(ROWBOX_MARGIN_PARAMS)); - pairs.forEach(pair => { + for (let key in object) { let otherBox = new Gtk.Box({ margin_left: MARGIN, margin_right: MARGIN }); - let otherLabel = new Gtk.Label({ label: pair[0], use_markup: true }); + let otherLabel = new Gtk.Label({ label: key, use_markup: true }); otherLabel.set_halign(1); - let otherLabel2 = new Gtk.Label({ label: pair[1] }); + let otherLabel2 = new Gtk.Label({ label: object[key] }); otherBox.pack_start(otherLabel, true, true, 4); otherBox.pack_start(otherLabel2, false, false, 4); listBox.add(otherBox); - }); + } }); listBox.add(new Gtk.Box(ROWBOX_MARGIN_PARAMS)); diff --git a/shortcuts.js b/shortcuts.js index 6e9a07c..f99b448 100644 --- a/shortcuts.js +++ b/shortcuts.js @@ -1,5 +1,5 @@ /* jslint esversion: 6 */ -/* exported Shortcuts */ +/* exported GLOBAL_KEYBINDINGS, INTERNAL_KEYBINDINGS, OTHERS */ /* * Copyright 2019 Abakkk @@ -36,67 +36,67 @@ const getKeyLabel = function(accel) { return Gtk.accelerator_get_label(keyval, mods); }; -// It is used by helper and prefs. -var Shortcuts = { - - // The setting keys of the "org.gnome.shell.extensions.draw-on-your-screen" schema. - GLOBAL_KEYBINDINGS: [ - ['toggle-drawing', 'toggle-modal', 'erase-drawings'] - ], - - // The setting keys of the "org.gnome.shell.extensions.draw-on-your-screen.internal-shortcuts" schema. - INTERNAL_KEYBINDINGS: [ - ['undo', 'redo', 'delete-last-element', 'smooth-last-element'], - ['select-none-shape', 'select-line-shape', 'select-ellipse-shape', 'select-rectangle-shape', 'select-polygon-shape', 'select-polyline-shape', - 'select-text-shape', 'select-image-shape', 'select-move-tool', 'select-resize-tool', 'select-mirror-tool'], - ['switch-fill', 'switch-fill-rule', 'switch-color-palette', 'switch-color-palette-reverse'], - ['increment-line-width', 'increment-line-width-more', 'decrement-line-width', 'decrement-line-width-more', - 'switch-linejoin', 'switch-linecap', 'switch-dash'], - ['switch-font-family', 'switch-font-family-reverse', 'switch-font-weight', 'switch-font-style', 'switch-text-alignment', 'switch-image-file'], - ['toggle-panel-and-dock-visibility', 'toggle-background', 'toggle-grid', 'toggle-square-area'], - ['open-next-json', 'open-previous-json', 'save-as-json', 'save-as-svg', 'open-preferences', 'toggle-help'] - ], +// The setting keys of the "org.gnome.shell.extensions.draw-on-your-screen" schema. +var GLOBAL_KEYBINDINGS = [ + ['toggle-drawing', 'toggle-modal', 'erase-drawings'], +]; - // Use functions to get the translations "at runtime". - _otherFunctions: [[ - () => [_("Draw"), _("Left click")], - () => [_("Menu"), _("Right click")], - () => [internalShortcutsSchema.get_key('switch-fill').get_summary(), _("Center click")], - () => [_("Increment/decrement line width"), _("Scroll")], - // Translators: %s are key labels (Ctrl+F1 and Ctrl+F9) - () => [_("Select color"), _("%s … %s").format(getKeyLabel('1'), getKeyLabel('9'))], - // Translators: %s is a key label - () => [_("Ignore pointer movement"), _("%s held").format(getKeyLabel('space'))], - () => [_("Leave"), getKeyLabel('Escape')], - ], [ - () => [_("Select eraser (while starting drawing)"), getKeyLabel('')], - () => [_("Duplicate (while starting handling)"), getKeyLabel('')], - () => [_("Rotate rectangle, polygon, polyline"), getKeyLabel('')], - () => [_("Extend circle to ellipse"), getKeyLabel('')], - () => [_("Curve line"), getKeyLabel('')], - () => [_("Smooth free drawing outline"), getKeyLabel('')], - () => [_("Rotate (while moving)"), getKeyLabel('')], - () => [_("Stretch (while resizing)"), getKeyLabel('')], - () => [_("Inverse (while mirroring)"), getKeyLabel('')], - ]], - - get OTHERS() { - if (!this._others) { - this._others = []; - this._otherFunctions.forEach(getPairFunctions => { - let pairs = []; - getPairFunctions.forEach(getPairFunction => pairs.push(getPairFunction())); - this._others.push(pairs); - }); - } - return this._others; - } -}; +// The setting keys of the "org.gnome.shell.extensions.draw-on-your-screen.internal-shortcuts" schema. +var INTERNAL_KEYBINDINGS = [ + ['undo', 'redo', 'delete-last-element', 'smooth-last-element'], + ['select-none-shape', 'select-line-shape', 'select-ellipse-shape', 'select-rectangle-shape', 'select-polygon-shape', 'select-polyline-shape', + 'select-text-shape', 'select-image-shape', 'select-move-tool', 'select-resize-tool', 'select-mirror-tool'], + ['switch-fill', 'switch-fill-rule', 'switch-color-palette', 'switch-color-palette-reverse'], + ['increment-line-width', 'increment-line-width-more', 'decrement-line-width', 'decrement-line-width-more', + 'switch-linejoin', 'switch-linecap', 'switch-dash'], + ['switch-font-family', 'switch-font-family-reverse', 'switch-font-weight', 'switch-font-style', 'switch-text-alignment', 'switch-image-file'], + ['toggle-panel-and-dock-visibility', 'toggle-background', 'toggle-grid', 'toggle-square-area'], + ['open-next-json', 'open-previous-json', 'save-as-json', 'save-as-svg', 'open-preferences', 'toggle-help'], +]; -if (GS_VERSION < '3.36') +if (GS_VERSION < '3.36') { // Remove 'open-preferences' keybinding. - Shortcuts.INTERNAL_KEYBINDINGS.forEach(settingKeys => { + INTERNAL_KEYBINDINGS.forEach(settingKeys => { let index = settingKeys.indexOf('open-preferences'); if (index != -1) settingKeys.splice(index, 1); }); +} + +const getOthers = function() { + return [ + { + [_("Draw")]: _("Left click"), + [_("Menu")]: _("Right click"), + [internalShortcutsSchema.get_key('switch-fill').get_summary()]: _("Center click"), + [_("Increment/decrement line width")]: _("Scroll"), + // Translators: %s are key labels (Ctrl+F1 and Ctrl+F9) + [_("Select color")]: _("%s … %s").format(getKeyLabel('1'), getKeyLabel('9')), + // Translators: %s is a key label + [_("Ignore pointer movement")]: _("%s held").format(getKeyLabel('space')), + [_("Leave")]: getKeyLabel('Escape'), + }, { + [_("Select eraser (while starting drawing)")]: getKeyLabel(''), + [_("Duplicate (while starting handling)")]: getKeyLabel(''), + [_("Rotate rectangle, polygon, polyline")]: getKeyLabel(''), + [_("Extend circle to ellipse")]: getKeyLabel(''), + [_("Curve line")]: getKeyLabel(''), + [_("Smooth free drawing outline")]: getKeyLabel(''), + [_("Rotate (while moving)")]: getKeyLabel(''), + [_("Stretch (while resizing)")]: getKeyLabel(''), + [_("Inverse (while mirroring)")]: getKeyLabel(''), + }, + ]; +}; + +let _OTHERS; +// Equivalent to "var OTHERS = [[ ... ]]", but as a getter so the translations are got after the initTranslations call. +// 'this' is the module. +Object.defineProperty(this, 'OTHERS', { + get: function() { + if (!_OTHERS) + _OTHERS = getOthers(); + return _OTHERS; + } +}); +