mirror of
https://github.com/zen-browser/components.git
synced 2025-07-08 17:09:59 +02:00
Refactor ZenKeyboardShortcuts to add support for accelerator keys
This commit is contained in:
parent
b48e947798
commit
e629fdfaa0
1 changed files with 51 additions and 32 deletions
|
@ -157,8 +157,8 @@ class KeyShortcutModifiers {
|
||||||
}
|
}
|
||||||
|
|
||||||
// used to avoid any future changes to the object
|
// used to avoid any future changes to the object
|
||||||
static fromObject({ctrl = false, alt = false, shift = false, meta = false}) {
|
static fromObject({ctrl = false, alt = false, shift = false, meta = false, accel = false}) {
|
||||||
return new KeyShortcutModifiers(ctrl, alt, shift, meta);
|
return new KeyShortcutModifiers(ctrl, alt, shift, meta, accel);
|
||||||
}
|
}
|
||||||
|
|
||||||
toUserString() {
|
toUserString() {
|
||||||
|
@ -231,6 +231,26 @@ class KeyShortcutModifiers {
|
||||||
areAnyActive() {
|
areAnyActive() {
|
||||||
return this.#control || this.#alt || this.#shift || this.#meta || this.#accel;
|
return this.#control || this.#alt || this.#shift || this.#meta || this.#accel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get control() {
|
||||||
|
return this.#control;
|
||||||
|
}
|
||||||
|
|
||||||
|
get alt() {
|
||||||
|
return this.#alt;
|
||||||
|
}
|
||||||
|
|
||||||
|
get shift() {
|
||||||
|
return this.#shift;
|
||||||
|
}
|
||||||
|
|
||||||
|
get meta() {
|
||||||
|
return this.#meta;
|
||||||
|
}
|
||||||
|
|
||||||
|
get accel() {
|
||||||
|
return this.#accel;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class KeyShortcut {
|
class KeyShortcut {
|
||||||
|
@ -481,7 +501,6 @@ class KeyShortcut {
|
||||||
}
|
}
|
||||||
|
|
||||||
class ZenKeyboardShortcutsLoader {
|
class ZenKeyboardShortcutsLoader {
|
||||||
#shortcutsDirtyCache = undefined;
|
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
}
|
}
|
||||||
|
@ -492,16 +511,11 @@ class ZenKeyboardShortcutsLoader {
|
||||||
|
|
||||||
async save(data) {
|
async save(data) {
|
||||||
await IOUtils.writeJSON(this.shortcutsFile, data);
|
await IOUtils.writeJSON(this.shortcutsFile, data);
|
||||||
this.#shortcutsDirtyCache = data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async loadObject() {
|
async loadObject() {
|
||||||
if (this.#shortcutsDirtyCache) {
|
|
||||||
return this.#shortcutsDirtyCache;
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
this.#shortcutsDirtyCache = await IOUtils.readJSON(this.shortcutsFile);
|
return await IOUtils.readJSON(this.shortcutsFile);
|
||||||
return this.#shortcutsDirtyCache;
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Error loading shortcuts file', e);
|
console.error('Error loading shortcuts file', e);
|
||||||
return null;
|
return null;
|
||||||
|
@ -540,7 +554,7 @@ function zenGetDefaultShortcuts() {
|
||||||
'C',
|
'C',
|
||||||
'',
|
'',
|
||||||
ZEN_COMPACT_MODE_SHORTCUTS_GROUP,
|
ZEN_COMPACT_MODE_SHORTCUTS_GROUP,
|
||||||
KeyShortcutModifiers.fromObject({ctrl: true, alt: true}),
|
KeyShortcutModifiers.fromObject({accel: true, alt: true}),
|
||||||
'code:gZenCompactModeManager.toggle()',
|
'code:gZenCompactModeManager.toggle()',
|
||||||
'zen-compact-mode-shortcut-toggle'
|
'zen-compact-mode-shortcut-toggle'
|
||||||
)
|
)
|
||||||
|
@ -551,7 +565,7 @@ function zenGetDefaultShortcuts() {
|
||||||
'S',
|
'S',
|
||||||
'',
|
'',
|
||||||
ZEN_COMPACT_MODE_SHORTCUTS_GROUP,
|
ZEN_COMPACT_MODE_SHORTCUTS_GROUP,
|
||||||
KeyShortcutModifiers.fromObject({ctrl: true, alt: true}),
|
KeyShortcutModifiers.fromObject({accel: true, alt: true}),
|
||||||
'code:gZenCompactModeManager.toggleSidebar()',
|
'code:gZenCompactModeManager.toggleSidebar()',
|
||||||
'zen-compact-mode-shortcut-show-sidebar'
|
'zen-compact-mode-shortcut-show-sidebar'
|
||||||
)
|
)
|
||||||
|
@ -562,7 +576,7 @@ function zenGetDefaultShortcuts() {
|
||||||
'T',
|
'T',
|
||||||
'',
|
'',
|
||||||
ZEN_COMPACT_MODE_SHORTCUTS_GROUP,
|
ZEN_COMPACT_MODE_SHORTCUTS_GROUP,
|
||||||
KeyShortcutModifiers.fromObject({ctrl: true, alt: true}),
|
KeyShortcutModifiers.fromObject({accel: true, alt: true}),
|
||||||
'code:gZenCompactModeManager.toggleToolbar()',
|
'code:gZenCompactModeManager.toggleToolbar()',
|
||||||
'zen-compact-mode-shortcut-show-toolbar'
|
'zen-compact-mode-shortcut-show-toolbar'
|
||||||
)
|
)
|
||||||
|
@ -588,7 +602,7 @@ function zenGetDefaultShortcuts() {
|
||||||
'E',
|
'E',
|
||||||
'',
|
'',
|
||||||
ZEN_WORKSPACE_SHORTCUTS_GROUP,
|
ZEN_WORKSPACE_SHORTCUTS_GROUP,
|
||||||
KeyShortcutModifiers.fromObject({ctrl: true, alt: true}),
|
KeyShortcutModifiers.fromObject({accel: true, alt: true}),
|
||||||
'code:ZenWorkspaces.changeWorkspaceShortcut()',
|
'code:ZenWorkspaces.changeWorkspaceShortcut()',
|
||||||
'zen-workspace-shortcut-forward'
|
'zen-workspace-shortcut-forward'
|
||||||
)
|
)
|
||||||
|
@ -599,7 +613,7 @@ function zenGetDefaultShortcuts() {
|
||||||
'Q',
|
'Q',
|
||||||
'',
|
'',
|
||||||
ZEN_WORKSPACE_SHORTCUTS_GROUP,
|
ZEN_WORKSPACE_SHORTCUTS_GROUP,
|
||||||
KeyShortcutModifiers.fromObject({ctrl: true, alt: true}),
|
KeyShortcutModifiers.fromObject({accel: true, alt: true}),
|
||||||
'code:ZenWorkspaces.changeWorkspaceShortcut(-1)',
|
'code:ZenWorkspaces.changeWorkspaceShortcut(-1)',
|
||||||
'zen-workspace-shortcut-backward'
|
'zen-workspace-shortcut-backward'
|
||||||
)
|
)
|
||||||
|
@ -636,7 +650,7 @@ function zenGetDefaultShortcuts() {
|
||||||
'G',
|
'G',
|
||||||
'',
|
'',
|
||||||
ZEN_SPLIT_VIEW_SHORTCUTS_GROUP,
|
ZEN_SPLIT_VIEW_SHORTCUTS_GROUP,
|
||||||
KeyShortcutModifiers.fromObject({ctrl: true, alt: true}),
|
KeyShortcutModifiers.fromObject({accel: true, alt: true}),
|
||||||
'code:gZenViewSplitter.toggleShortcut(\'grid\')',
|
'code:gZenViewSplitter.toggleShortcut(\'grid\')',
|
||||||
'zen-split-view-shortcut-grid'
|
'zen-split-view-shortcut-grid'
|
||||||
)
|
)
|
||||||
|
@ -647,7 +661,7 @@ function zenGetDefaultShortcuts() {
|
||||||
'V',
|
'V',
|
||||||
'',
|
'',
|
||||||
ZEN_SPLIT_VIEW_SHORTCUTS_GROUP,
|
ZEN_SPLIT_VIEW_SHORTCUTS_GROUP,
|
||||||
KeyShortcutModifiers.fromObject({ctrl: true, alt: true}),
|
KeyShortcutModifiers.fromObject({accel: true, alt: true}),
|
||||||
'code:gZenViewSplitter.toggleShortcut(\'vsep\')',
|
'code:gZenViewSplitter.toggleShortcut(\'vsep\')',
|
||||||
'zen-split-view-shortcut-vertical'
|
'zen-split-view-shortcut-vertical'
|
||||||
)
|
)
|
||||||
|
@ -658,7 +672,7 @@ function zenGetDefaultShortcuts() {
|
||||||
'H',
|
'H',
|
||||||
'',
|
'',
|
||||||
ZEN_SPLIT_VIEW_SHORTCUTS_GROUP,
|
ZEN_SPLIT_VIEW_SHORTCUTS_GROUP,
|
||||||
KeyShortcutModifiers.fromObject({ctrl: true, alt: true}),
|
KeyShortcutModifiers.fromObject({accel: true, alt: true}),
|
||||||
'code:gZenViewSplitter.toggleShortcut(\'hsep\')',
|
'code:gZenViewSplitter.toggleShortcut(\'hsep\')',
|
||||||
'zen-split-view-shortcut-horizontal'
|
'zen-split-view-shortcut-horizontal'
|
||||||
)
|
)
|
||||||
|
@ -669,7 +683,7 @@ function zenGetDefaultShortcuts() {
|
||||||
'U',
|
'U',
|
||||||
'',
|
'',
|
||||||
ZEN_SPLIT_VIEW_SHORTCUTS_GROUP,
|
ZEN_SPLIT_VIEW_SHORTCUTS_GROUP,
|
||||||
KeyShortcutModifiers.fromObject({ctrl: true, alt: true}),
|
KeyShortcutModifiers.fromObject({accel: true, alt: true}),
|
||||||
'code:gZenViewSplitter.toggleShortcut(\'unsplit\')',
|
'code:gZenViewSplitter.toggleShortcut(\'unsplit\')',
|
||||||
'zen-split-view-shortcut-unsplit'
|
'zen-split-view-shortcut-unsplit'
|
||||||
)
|
)
|
||||||
|
@ -679,27 +693,31 @@ function zenGetDefaultShortcuts() {
|
||||||
}
|
}
|
||||||
|
|
||||||
class ZenKeyboardShortcutsVersioner {
|
class ZenKeyboardShortcutsVersioner {
|
||||||
static LATEST_KBS_VERSION = 1.0;
|
static LATEST_KBS_VERSION = 1;
|
||||||
|
|
||||||
#loadedVersion = 0.0;
|
constructor() {
|
||||||
|
}
|
||||||
|
|
||||||
constructor(versionedData) {
|
get version() {
|
||||||
this.#loadedVersion = versionedData.version ?? 0.0;
|
return Services.prefs.getIntPref('zen.keyboard.shortcuts.version', 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
set version(version) {
|
||||||
|
Services.prefs.setIntPref('zen.keyboard.shortcuts.version', version);
|
||||||
}
|
}
|
||||||
|
|
||||||
getVersionedData(data) {
|
getVersionedData(data) {
|
||||||
return {
|
return {
|
||||||
version: this.#loadedVersion,
|
|
||||||
shortcuts: data,
|
shortcuts: data,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
isVersionUpToDate() {
|
isVersionUpToDate() {
|
||||||
return this.#loadedVersion == ZenKeyboardShortcutsVersioner.LATEST_KBS_VERSION;
|
return this.version == ZenKeyboardShortcutsVersioner.LATEST_KBS_VERSION;
|
||||||
}
|
}
|
||||||
|
|
||||||
isVersionOutdated() {
|
isVersionOutdated() {
|
||||||
return this.#loadedVersion < ZenKeyboardShortcutsVersioner.LATEST_KBS_VERSION;
|
return this.version < ZenKeyboardShortcutsVersioner.LATEST_KBS_VERSION;
|
||||||
}
|
}
|
||||||
|
|
||||||
migrateIfNeeded(data) {
|
migrateIfNeeded(data) {
|
||||||
|
@ -708,21 +726,22 @@ class ZenKeyboardShortcutsVersioner {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.isVersionOutdated()) {
|
if (this.isVersionOutdated()) {
|
||||||
console.info('Zen CKS: Migrating shortcuts from version', this.#loadedVersion, 'to', ZenKeyboardShortcutsVersioner.LATEST_KBS_VERSION);
|
const version = this.version;
|
||||||
const newData = this.migrate(data);
|
console.info('Zen CKS: Migrating shortcuts from version', version, 'to', ZenKeyboardShortcutsVersioner.LATEST_KBS_VERSION);
|
||||||
this.#loadedVersion = ZenKeyboardShortcutsVersioner.LATEST_KBS_VERSION;
|
const newData = this.migrate(data, version);
|
||||||
|
this.version = ZenKeyboardShortcutsVersioner.LATEST_KBS_VERSION;
|
||||||
return newData;
|
return newData;
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Error('Unknown keyboar shortcuts version');
|
throw new Error('Unknown keyboar shortcuts version');
|
||||||
}
|
}
|
||||||
|
|
||||||
migrate(data) {
|
migrate(data, version) {
|
||||||
if (this.#loadedVersion < 1.0) {
|
if (version < 1) {
|
||||||
// Migrate from 0.0 to 1.0
|
// Migrate from 0 to 1
|
||||||
// Here, we do a complet reset of the shortcuts,
|
// Here, we do a complet reset of the shortcuts,
|
||||||
// since nothing seems to work properly.
|
// since nothing seems to work properly.
|
||||||
return zenGetDefaultShortcuts();
|
data = zenGetDefaultShortcuts();
|
||||||
}
|
}
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue