-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathextension.js
More file actions
209 lines (164 loc) · 6.13 KB
/
Copy pathextension.js
File metadata and controls
209 lines (164 loc) · 6.13 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import GLib from 'gi://GLib';
import St from 'gi://St';
import Clutter from 'gi://Clutter';
import { Extension, gettext as _ } from 'resource:///org/gnome/shell/extensions/extension.js';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import Gio from 'gi://Gio';
const LIGHT_BUTTON_CLASS = 'slide-button show';
const DARK_BUTTON_CLASS = 'slide-button show dark';
export default class Istighfar extends Extension {
constructor(metadata) {
super(metadata);
this.button = null;
this.timeoutId = null;
this.HidetimeoutId = null;
this.duration = null;
this.THEME = false;
this.counter = 0;
this.durationHandler = null;
this.hideHandler = null;
this.darkModeHandler = null;
}
enable() {
this.settings = this.getSettings();
this.duration = this.settings.get_int("duration") * 1000 * 60;
this.THEME = this.settings.get_boolean("dark-mode");
this.durationHandler = this.settings.connect('changed::duration', () => {
this.duration = this.settings.get_int("duration") * 1000 * 60;
this._startButtonPeriodic()
});
this.darkModeHandler = this.settings.connect("changed::dark-mode", () => {
this.THEME = this.settings.get_boolean("dark-mode");
});
let sentences = loadJSONFile(this.path);
if (sentences) {
if (sentences.length == 0) {
sentences = false;
}
}
this.sentences = sentences
this._startButtonPeriodic();
}
disable() {
if (this.hideHandler && this.button ){
this.button.disconnect(this.hideHandler);
this.hideHandler = null;
}
if (this.durationHandler && this.settings ) {
this.settings.disconnect(this.durationHandler);
this.durationHandler = null;
}
if (this.darkModeHandler && this.settings){
this.settings.disconnect(this.darkModeHandler);
this.darkModeHandler = null;
}
if (this.settings) {
this.settings = null;
}
if (this.timeoutId) {
GLib.source_remove(this.timeoutId);
this.timeoutId = null;
}
if (this.HidetimeoutId) {
GLib.source_remove(this.HidetimeoutId);
this.HidetimeoutId = null;
}
if (this.button) {
this.button.destroy();
this.button = null;
}
}
_startButtonPeriodic() {
if (this.timeoutId) {
GLib.source_remove(this.timeoutId);
this.timeoutId = null;
}
this.timeoutId = GLib.timeout_add(GLib.PRIORITY_DEFAULT,this.duration,()=>{
this._createButton();
return GLib.SOURCE_CONTINUE ;
})
}
_createButton() {
if (this.button) return;
// Position the button initially off-screen
const monitor = Main.layoutManager.primaryMonitor;
let style = this.THEME ? DARK_BUTTON_CLASS : LIGHT_BUTTON_CLASS;
let sentence = null;
sentence = this.sentences[this.counter];
if (!sentence){
// setting a default sentence
sentence ="استغفر الله و أتوب إليه";
}
this.button = new St.Button({
style_class: style,
label: sentence,
reactive: true,
can_focus: true,
track_hover: true,
});
this.hideHandler = this.button.connect('button-press-event', () => {
this._hideButton();
});
Main.layoutManager.uiGroup.add_child(this.button);
this.button.set_position(monitor.width - (this.button.width - 1) / 2 , Math.min(Math.floor( this.button.height * 3,monitor.height / 6)));
// Animate sliding in
this.button.ease({
translation_x: - (Math.floor(this.button.width / 2) + 10), // Moves left into view
duration: 500,
mode: Clutter.AnimationMode.EASE_OUT_QUAD,
});
// Hide after 5s unless clicked
if (this.HidetimeoutId){
GLib.source_remove(this.HidetimeoutId);
this.HidetimeoutId = null;
}
this.HidetimeoutId = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 5000, () => {
this._hideButton();
return GLib.SOURCE_REMOVE;
});
this.counter = (this.counter + 1) % this.sentences.length ;
}
_hideButton() {
if (!this.button) return;
let buttonRef = this.button;
this.button = null;
buttonRef.ease({
translation_x: Math.floor(buttonRef.width / 2 ) + 10,
duration: 500,
mode: Clutter.AnimationMode.EASE_IN_QUAD,
onComplete: () => {
buttonRef.destroy();
},
});
}
}
function loadJSONFile(path) {
try {
const filePath = GLib.build_filenamev([
GLib.get_user_data_dir(), // ~/.local/share
'istighfar', // Subdirectory for the extension
'sentences.json',
]);
let file = Gio.File.new_for_path(filePath);
let [success, contents] = file.load_contents(null);
if (success) {
let text = new TextDecoder().decode(contents);
return JSON.parse(text);
}
} catch (e) {
try {
console.log(`Error loading JSON file from .local/share/extensions/ ${e}`);
console.log(`loading from default file`);
const filePath = GLib.build_filenamev([path, "assets/default.json"]);
let file = Gio.File.new_for_path(filePath);
let [success, contents] = file.load_contents(null);
if (success) {
let text = new TextDecoder().decode(contents);
return JSON.parse(text); // Parse JSON
}
} catch(e) {
console.log(`ERROR FINDING assets/default.json : ${e}`)
}
return [];
}
}