-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathextension.js
More file actions
140 lines (119 loc) · 4.2 KB
/
extension.js
File metadata and controls
140 lines (119 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import St from 'gi://St';
import GLib from 'gi://GLib';
import Meta from 'gi://Meta';
import Shell from 'gi://Shell';
import * as PanelMenu from 'resource:///org/gnome/shell/ui/panelMenu.js';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import { Extension, gettext as _ } from 'resource:///org/gnome/shell/extensions/extension.js';
import { schemaKeys } from "./const.js";
import { getAvailableLanguages } from "./languages.js";
import { ScreenOCR } from "./screenocr.js";
export default class extends Extension {
constructor(metadata) {
super(metadata);
this._button = null;
this._buttonSignalHandler = null;
this._keyboardShortcutChangeSignalHandler = null;
this._settings = null;
}
enable() {
this._ensureDependencies();
// Load settings
this._settings = this.getSettings();
// Manage button visibility
let showButton = this._settings.get_boolean(schemaKeys.showButton);
this._updateButton(showButton);
// Listen for changes to button visibility
this._buttonSignalHandler = this._settings.connect(`changed::${schemaKeys.showButton}`, () => {
this._updateButton(this._settings.get_boolean(schemaKeys.showButton));
});
// Set up keyboard shortcut
this._bindShortcut();
}
_ensureDependencies() {
// Throw an error if something is missging
const errorMessages = [];
// Check dependencies
const dependencies = [
'tesseract',
];
const missingDependencies = [];
for (const command of dependencies) {
if (!GLib.find_program_in_path(command)) {
missingDependencies.push(command);
}
}
// Build error message
if (missingDependencies.length !== 0) {
errorMessages.push(_('Missing dependencies: ') + missingDependencies.join(', ') + '.');
}
if (missingDependencies.includes("tesseract")) {
errorMessages.push(_('Ensure to install Tesseract with your language(s).'));
}
else if (getAvailableLanguages().length === 0) {
errorMessages.push(_('No known Tesseract languages installed.'));
}
// Throw if we found an error
if (errorMessages.length) {
throw new Error(errorMessages.join(' '));
}
}
_updateButton(show) {
if (show && !this._button) {
this._button = new PanelMenu.Button({ reactive: true, track_hover: true, style_class: 'panel-button' });
// Use an icon instead of text
let icon = new St.Icon({
icon_name: 'zoom-fit-best-symbolic',
style_class: 'system-status-icon'
});
this._button.add_child(icon);
this._button.connect('button-release-event', () => {
this._grabText();
});
Main.panel.addToStatusArea('textgrabber', this._button);
} else if (!show && this._button) {
this._button.destroy();
this._button = null;
}
}
_bindShortcut() {
// Add shortcut from settings (default: <Super>t from schema)
Main.wm.addKeybinding(
schemaKeys.textgrabberShortcut,
this._settings,
Meta.KeyBindingFlags.IGNORE_AUTOREPEAT,
Shell.ActionMode.NORMAL | Shell.ActionMode.OVERVIEW,
() => this._grabText()
);
// Listen for shortcut changes
this._keyboardShortcutChangeSignalHandler = this._settings.connect(`changed::${schemaKeys.textgrabberShortcut}`, () => {
Main.wm.removeKeybinding(schemaKeys.textgrabberShortcut);
Main.wm.addKeybinding(
schemaKeys.textgrabberShortcut,
this._settings,
Meta.KeyBindingFlags.NONE,
Shell.ActionMode.ALL,
() => this._grabText()
);
});
}
_grabText() {
let languages = this._settings.get_strv(schemaKeys.tesseractLanguages);
let langString = languages.join('+');
new ScreenOCR().grabText(langString).catch(_ => { });
}
disable() {
this._button?.destroy();
this._button = null;
if (this._buttonSignalHandler) {
this._settings.disconnect(this._buttonSignalHandler);
this._buttonSignalHandler = null;
}
if (this._keyboardShortcutChangeSignalHandler) {
this._settings.disconnect(this._keyboardShortcutChangeSignalHandler);
this._keyboardShortcutChangeSignalHandler = null;
}
Main.wm.removeKeybinding(schemaKeys.textgrabberShortcut);
this._settings = null;
}
}