|
| 1 | +import '@girs/gjs'; |
| 2 | +import { gettext as _ } from 'gettext'; |
| 3 | + |
| 4 | +import GLib from '@girs/glib-2.0'; |
| 5 | +import St from '@girs/st-18'; |
| 6 | +import Meta from '@girs/meta-18'; |
| 7 | +import Shell from '@girs/shell-18'; |
| 8 | +import * as Main from '@girs/gnome-shell/ui/main'; |
| 9 | + |
| 10 | +import type { ExtensionContext } from '~/core/context.ts'; |
| 11 | +import { logger } from '~/core/logger.ts'; |
| 12 | +import { Module } from '~/module.ts'; |
| 13 | +import type { ModuleDefinition } from '~/module.ts'; |
| 14 | + |
| 15 | +import type { ClipboardEntry } from '~/modules/clipboardHistory/clipboardStore.ts'; |
| 16 | +import { ClipboardStore } from '~/modules/clipboardHistory/clipboardStore.ts'; |
| 17 | +import { ClipboardMonitor } from '~/modules/clipboardHistory/clipboardMonitor.ts'; |
| 18 | +import { ClipboardPanel } from '~/modules/clipboardHistory/clipboardPanel.ts'; |
| 19 | + |
| 20 | +const KEYBINDING_KEY = 'clipboard-history-shortcut'; |
| 21 | +const LOG_PREFIX = 'ClipboardHistory'; |
| 22 | + |
| 23 | +export class ClipboardHistory extends Module { |
| 24 | + private _store: ClipboardStore | null = null; |
| 25 | + private _monitor: ClipboardMonitor | null = null; |
| 26 | + private _panel: ClipboardPanel | null = null; |
| 27 | + private _settingsIds: number[] = []; |
| 28 | + private _startupIdleId: number = 0; |
| 29 | + |
| 30 | + constructor(context: ExtensionContext) { |
| 31 | + super(context); |
| 32 | + } |
| 33 | + |
| 34 | + override enable(): void { |
| 35 | + const configDir = GLib.get_user_config_dir() + '/aurora-shell'; |
| 36 | + const filePath = configDir + '/clipboard-history.json'; |
| 37 | + const rawSettings = this.context.settings.getRawSettings(); |
| 38 | + const maxItems = rawSettings.get_int('clipboard-history-max-items'); |
| 39 | + const pollMs = rawSettings.get_int('clipboard-history-poll-interval'); |
| 40 | + |
| 41 | + this._store = new ClipboardStore(filePath, maxItems); |
| 42 | + this._store.load(); |
| 43 | + |
| 44 | + this._panel = new (ClipboardPanel as unknown as new ( |
| 45 | + store: ClipboardStore, |
| 46 | + callbacks: { |
| 47 | + onActivate: (entry: ClipboardEntry) => void; |
| 48 | + onRemove: (id: string) => void; |
| 49 | + onTogglePin: (id: string) => void; |
| 50 | + }, |
| 51 | + ) => ClipboardPanel)(this._store, { |
| 52 | + onActivate: (entry) => this._onActivate(entry), |
| 53 | + onRemove: (id) => this._onRemove(id), |
| 54 | + onTogglePin: (id) => this._onTogglePin(id), |
| 55 | + }); |
| 56 | + |
| 57 | + this._monitor = new ClipboardMonitor(pollMs, (text) => { |
| 58 | + this._store?.addText(text); |
| 59 | + }); |
| 60 | + |
| 61 | + this._startupIdleId = GLib.idle_add(GLib.PRIORITY_DEFAULT_IDLE, () => { |
| 62 | + this._startupIdleId = 0; |
| 63 | + this._monitor?.start(); |
| 64 | + return GLib.SOURCE_REMOVE; |
| 65 | + }); |
| 66 | + |
| 67 | + try { |
| 68 | + Main.wm.addKeybinding( |
| 69 | + KEYBINDING_KEY, |
| 70 | + rawSettings, |
| 71 | + Meta.KeyBindingFlags.IGNORE_AUTOREPEAT, |
| 72 | + Shell.ActionMode.ALL, |
| 73 | + () => this._togglePanel(), |
| 74 | + ); |
| 75 | + } catch (e) { |
| 76 | + logger.error('Failed to register keybinding:', { prefix: LOG_PREFIX }, e as Error); |
| 77 | + } |
| 78 | + |
| 79 | + this._settingsIds = [ |
| 80 | + rawSettings.connect('changed::clipboard-history-max-items', () => { |
| 81 | + this._store?.setMaxItems(rawSettings.get_int('clipboard-history-max-items')); |
| 82 | + }), |
| 83 | + rawSettings.connect('changed::clipboard-history-poll-interval', () => { |
| 84 | + this._monitor?.setInterval(rawSettings.get_int('clipboard-history-poll-interval')); |
| 85 | + }), |
| 86 | + ]; |
| 87 | + } |
| 88 | + |
| 89 | + override disable(): void { |
| 90 | + if (this._startupIdleId !== 0) { |
| 91 | + GLib.source_remove(this._startupIdleId); |
| 92 | + this._startupIdleId = 0; |
| 93 | + } |
| 94 | + |
| 95 | + try { |
| 96 | + Main.wm.removeKeybinding(KEYBINDING_KEY); |
| 97 | + } catch (_e) { |
| 98 | + // ignore if not registered |
| 99 | + } |
| 100 | + |
| 101 | + this._panel?.close(); |
| 102 | + this._panel?.destroy(); |
| 103 | + this._panel = null; |
| 104 | + |
| 105 | + this._monitor?.stop(); |
| 106 | + this._monitor = null; |
| 107 | + |
| 108 | + this._store?.save(); |
| 109 | + this._store = null; |
| 110 | + |
| 111 | + const rawSettings = this.context.settings.getRawSettings(); |
| 112 | + for (const id of this._settingsIds) { |
| 113 | + rawSettings.disconnect(id); |
| 114 | + } |
| 115 | + this._settingsIds = []; |
| 116 | + } |
| 117 | + |
| 118 | + private _togglePanel(): void { |
| 119 | + if (this._panel?.isOpen) { |
| 120 | + this._panel.close(); |
| 121 | + } else { |
| 122 | + this._panel?.open(); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + private _onActivate(entry: ClipboardEntry): void { |
| 127 | + St.Clipboard.get_default().set_text(St.ClipboardType.CLIPBOARD, entry.text); |
| 128 | + this._panel?.close(); |
| 129 | + logger.debug(`Restored clipboard entry: ${entry.text.slice(0, 40)}`, { prefix: LOG_PREFIX }); |
| 130 | + } |
| 131 | + |
| 132 | + private _onRemove(id: string): void { |
| 133 | + this._store?.remove(id); |
| 134 | + this._panel?.refresh(); |
| 135 | + } |
| 136 | + |
| 137 | + private _onTogglePin(id: string): void { |
| 138 | + if (!this._store) return; |
| 139 | + const pinned = this._store.getPinned(); |
| 140 | + if (pinned.find((e) => e.id === id)) { |
| 141 | + this._store.unpin(id); |
| 142 | + } else { |
| 143 | + this._store.pin(id); |
| 144 | + } |
| 145 | + this._panel?.refresh(); |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +export const definition: ModuleDefinition = { |
| 150 | + key: 'clipboard-history', |
| 151 | + settingsKey: 'module-clipboard-history', |
| 152 | + title: _('Clipboard History'), |
| 153 | + subtitle: _('Searchable clipboard history with pinning and keyboard navigation'), |
| 154 | + options: [ |
| 155 | + { |
| 156 | + key: 'clipboard-history-max-items', |
| 157 | + title: _('Max History Items'), |
| 158 | + subtitle: _('Number of non-pinned entries to retain'), |
| 159 | + type: 'spin', |
| 160 | + min: 10, |
| 161 | + max: 200, |
| 162 | + }, |
| 163 | + { |
| 164 | + key: 'clipboard-history-poll-interval', |
| 165 | + title: _('Poll Interval (ms)'), |
| 166 | + subtitle: _('How often to check the clipboard for changes'), |
| 167 | + type: 'spin', |
| 168 | + min: 250, |
| 169 | + max: 5000, |
| 170 | + }, |
| 171 | + ], |
| 172 | + factory: (ctx) => new ClipboardHistory(ctx), |
| 173 | +}; |
0 commit comments