-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathextension.js
More file actions
914 lines (789 loc) · 32.7 KB
/
Copy pathextension.js
File metadata and controls
914 lines (789 loc) · 32.7 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
import GObject from 'gi://GObject';
import St from 'gi://St';
import GLib from 'gi://GLib';
import Clutter from 'gi://Clutter';
import Gio from 'gi://Gio';
import Soup from 'gi://Soup';
import Meta from 'gi://Meta';
import Shell from 'gi://Shell';
import { Extension, gettext as _ } from 'resource:///org/gnome/shell/extensions/extension.js';
import * as PanelMenu from 'resource:///org/gnome/shell/ui/panelMenu.js';
import * as PopupMenu from 'resource:///org/gnome/shell/ui/popupMenu.js';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import { fetchTypingActivity, getDates } from './helpers/monkeytypeService.js';
import { ExtensionSettings } from './helpers/settings.js';
// Visual constants for the test boxes in the top bar
const BOX_SIZE = 14; // Size of each test box in pixels
const BOX_MARGIN = 4; // Space between each box
const BORDER_RADIUS = 3; // Rounded corners for the boxes
const COLORS = {
ACTIVE: '#e2b714', // Monkeytype yellow color for days with tests
INACTIVE: '#8e8e8e', // Gray color for days without tests
DEFAULT: '#888888' // Default fallback color
};
// All available color themes - these match what users see in settings
const THEME_NAMES = {
standard: "Monkeytype", // Classic Monkeytype yellow theme
githubDark: "GitHub Green", // GitHub green gradient theme
halloween: "Halloween", // Orange and dark spooky colors
teal: "Teal", // Calming teal/aqua colors
leftPad: "@left_pad", // Grayscale theme
dracula: "Dracula", // Popular dark theme with purple/pink accents
blue: "Blue", // Cool blue gradient theme
panda: "Panda 🐼", // Black and white with colorful accents
sunny: "Sunny", // Bright yellow/gold theme
pink: "Pink", // Pink/magenta gradient theme
solarizedDark: 'Solarized Dark', // Popular developer theme (dark)
solarizedLight: 'Solarized Light' // Popular developer theme (light)
};
const THEMES = {
standard: {
text: "#000000",
meta: "#666666",
grade4: "#e2b714",
grade3: "#f0c730",
grade2: "#f5d65b",
grade1: "#fae588",
grade0: "#ebedf0"
},
githubDark: {
text: "#ffffff",
meta: "#dddddd",
grade4: "#27d545",
grade3: "#10983d",
grade2: "#00602d",
grade1: "#003820",
grade0: "#161b22"
},
halloween: {
text: "#000000",
meta: "#666666",
grade4: "#03001C",
grade3: "#FE9600",
grade2: "#FFC501",
grade1: "#FFEE4A",
grade0: "#ebedf0"
},
teal: {
text: "#000000",
meta: "#666666",
grade4: "#458B74",
grade3: "#66CDAA",
grade2: "#76EEC6",
grade1: "#7FFFD4",
grade0: "#ebedf0"
},
leftPad: {
text: "#ffffff",
meta: "#999999",
grade4: "#F6F6F6",
grade3: "#DDDDDD",
grade2: "#A5A5A5",
grade1: "#646464",
grade0: "#2F2F2F"
},
dracula: {
text: "#f8f8f2",
meta: "#666666",
grade4: "#ff79c6",
grade3: "#bd93f9",
grade2: "#6272a4",
grade1: "#44475a",
grade0: "#282a36"
},
blue: {
text: "#C0C0C0",
meta: "#666666",
grade4: "#4F83BF",
grade3: "#416895",
grade2: "#344E6C",
grade1: "#263342",
grade0: "#222222"
},
panda: {
text: "#E6E6E6",
meta: "#676B79",
grade4: "#FF4B82",
grade3: "#19f9d8",
grade2: "#6FC1FF",
grade1: "#34353B",
grade0: "#242526"
},
sunny: {
text: "#000000",
meta: "#666666",
grade4: "#a98600",
grade3: "#dab600",
grade2: "#e9d700",
grade1: "#f8ed62",
grade0: "#fff9ae"
},
pink: {
text: "#000000",
meta: "#666666",
grade4: "#61185f",
grade3: "#a74aa8",
grade2: "#ca5bcc",
grade1: "#e48bdc",
grade0: "#ebedf0"
},
solarizedDark: {
text: "#93a1a1",
meta: "#586e75",
grade4: "#d33682",
grade3: "#b58900",
grade2: "#2aa198",
grade1: "#268bd2",
grade0: "#073642"
},
solarizedLight: {
text: "#586e75",
meta: "#93a1a1",
grade4: "#6c71c4",
grade3: "#dc322f",
grade2: "#cb4b16",
grade1: "#b58900",
grade0: "#eee8d5"
}
};
// Test count thresholds for grade-based coloring
const TEST_THRESHOLDS = {
grade1: 1, // 1-2 tests
grade2: 3, // 3-5 tests
grade3: 6, // 6-10 tests
grade4: 11 // 11+ tests
};
const MESSAGES = {
NO_DATA: 'No data available',
NO_TESTS: 'No test data available',
MISSING_CREDENTIALS: 'Missing Monkeytype username or ApeKey',
PREFS_ERROR: 'Failed to open extension preferences.'
};
// Display and animation settings
const DATE_FORMAT = { month: 'short' }; // How dates appear in the menu
const DEFAULT_OPACITY = 50; // Base opacity for boxes with no tests
const MAX_OPACITY_INCREASE = 205; // Maximum opacity boost for active boxes
const OPACITY_PER_TEST = 20; // How much opacity increases per test
const Indicator = GObject.registerClass(
class Indicator extends PanelMenu.Button {
_init(preferences, extension) {
super._init(0.0, _('Monkeytype Streak'));
this.menu.setSourceAlignment(0);
this._preferences = preferences;
this._extension = extension;
this._prefsChangedId = null;
this._boxes = [];
this._refreshTimeoutId = null;
this._testSection = null;
this._separator = null;
this._soupSession = new Soup.Session();
this._buildUI();
this._setupMenuItems();
this._updateTypingDisplay();
this._prefsChangedId = this._preferences.connectChanged(() => {
this._clearTestInfoItems();
this._updateTypingDisplay().finally(() => {
this._refreshMenu();
});
});
// Listen for days-to-show changes to rebuild UI
this._daysChangedId = this._preferences._settings.connect('changed::days-to-show', () => {
this._rebuildBoxes();
this._clearTestInfoItems();
this._updateTypingDisplay().finally(() => {
this._refreshMenu();
});
});
}
_buildUI() {
// Create the main container that holds our week of test boxes
const containerBox = new St.BoxLayout({
vertical: true,
x_expand: false,
y_expand: true,
y_align: Clutter.ActorAlign.CENTER,
reactive: true
});
// Horizontal row that will contain all day boxes
const hbox = new St.BoxLayout({
x_expand: false,
y_expand: false,
y_align: Clutter.ActorAlign.CENTER
});
this._hbox = hbox; // Store reference to update later
this._containerBox = containerBox; // Store reference for event handling
this._rebuildBoxes();
containerBox.add_child(hbox);
this.add_child(containerBox); // Add to the panel button
// Add right-click handler to open Monkeytype
containerBox.connect('button-press-event', (actor, event) => {
if (event.get_button() === 3) { // Right mouse button
this._openMonkeytype();
return Clutter.EVENT_STOP;
}
return Clutter.EVENT_PROPAGATE;
});
}
_rebuildBoxes() {
// Clear existing boxes
if (this._boxes) {
this._boxes.forEach(box => {
if (box.get_parent()) {
box.get_parent().destroy();
}
});
}
this._boxes = [];
// Get number of days to show from settings
const daysToShow = this._preferences.daysToShow || 7;
// Create boxes for the specified number of days
for (let i = 0; i < daysToShow; i++) {
// Container for each individual day box
const boxContainer = new St.Widget({
layout_manager: new Clutter.BinLayout(),
x_expand: false,
y_expand: false,
height: BOX_SIZE,
width: BOX_SIZE,
style: `margin-right: ${BOX_MARGIN}px;`
});
// The actual visual box that shows typing activity
const box = new St.Widget({
style_class: 'test-box',
height: BOX_SIZE,
width: BOX_SIZE,
style: this._getBoxStyle(COLORS.DEFAULT, true), // Start with empty styling
opacity: DEFAULT_OPACITY,
});
boxContainer.add_child(box);
this._boxes.push(box); // Keep track of all boxes for updates
this._hbox.add_child(boxContainer);
}
}
_setupMenuItems() {
// Add "Refresh Now" button to the dropdown menu
const refreshItem = new PopupMenu.PopupMenuItem(_('Refresh Now'));
refreshItem.connect('activate', () => {
// Show user we're working on it
refreshItem.label.text = _('Refreshing...');
// Clear old test info and fetch new data
this._clearTestInfoItems();
this._updateTypingDisplay().finally(() => {
// Reset button text when done
refreshItem.label.text = _('Refresh Now');
this._refreshMenu();
});
});
this.menu.addMenuItem(refreshItem);
// Add "Settings" button to open extension preferences
const settingsItem = new PopupMenu.PopupMenuItem(_('Settings'));
settingsItem.connect('activate', () => {
this._openPreferences()
});
this.menu.addMenuItem(settingsItem);
}
async _openPreferences() {
try {
await this._extension.openPreferences();
} catch (e) {
console.error('Failed to open preferences:', e);
Main.notify(_('Error'), _(MESSAGES.PREFS_ERROR));
}
}
_openMonkeytype() {
try {
const rightClickAction = this._preferences.rightClickAction;
const username = this._preferences.monkeytypeUsername;
let url = 'https://monkeytype.com';
if (rightClickAction === 'profile' && username) {
url = `https://monkeytype.com/profile/${username}`;
}
Gio.AppInfo.launch_default_for_uri(url, null);
} catch (e) {
console.error('Failed to open Monkeytype:', e);
}
}
_openUserProfile() {
try {
const username = this._preferences.monkeytypeUsername;
if (!username) {
Main.notify(_('MonkeyBar'), _('Please set your Monkeytype username in settings'));
return;
}
const url = `https://monkeytype.com/profile/${username}`;
Gio.AppInfo.launch_default_for_uri(url, null);
} catch (e) {
console.error('Failed to open user profile:', e);
}
}
_refreshNow() {
this._clearTestInfoItems();
this._updateTypingDisplay().finally(() => {
this._refreshMenu();
Main.notify(_('MonkeyBar'), _('Typing activity refreshed'));
});
}
_getBoxStyle(bgColor, isEmpty = false) {
// Create the CSS styling for each test activity box
let style = `background-color: ${bgColor}; width: ${BOX_SIZE}px; height: ${BOX_SIZE}px; border-radius: ${BORDER_RADIUS}px;`;
// Add a very subtle border so boxes are always visible, even on pure black backgrounds
style += ' border: 1px solid rgba(255, 255, 255, 0.08);';
return style;
}
_getTestGrade(count) {
// Determine how "intense" the color should be based on test count
if (count === 0) return 'grade0'; // No tests = lightest/empty
if (count < TEST_THRESHOLDS.grade2) return 'grade1'; // 1-2 tests = light
if (count < TEST_THRESHOLDS.grade3) return 'grade2'; // 3-5 tests = medium
if (count < TEST_THRESHOLDS.grade4) return 'grade3'; // 6-10 tests = dark
return 'grade4'; // 11+ tests = darkest
}
_getThemedColor(count, themeName, colorMode) {
// Get the color scheme for the current theme
const theme = THEMES[themeName] || THEMES.standard;
if (colorMode === 'grade') {
// Grade mode: different colors for different activity levels
const grade = this._getTestGrade(count);
return theme[grade];
} else {
// Opacity mode: same color for all, just varies transparency
return count > 0 ? theme.grade3 : theme.grade0;
}
}
_formatDateWithTests(date, count) {
const today = new Date();
const isToday = date.getDate() === today.getDate() &&
date.getMonth() === today.getMonth() &&
date.getFullYear() === today.getFullYear();
const testText = count === 1 ? 'test' : 'tests';
if (isToday) {
return {
label: `Today: ${count} ${testText}`,
};
} else {
const monthName = date.toLocaleString('en-US', DATE_FORMAT);
const day = date.getDate();
return {
label: `${monthName} ${day}: ${count} ${testText}`,
};
}
}
_updateTestInfoSection(dates, counts) {
if (!this._testSection) {
this._testSection = new PopupMenu.PopupMenuSection();
this.menu.addMenuItem(this._testSection, 0);
this._testItems = [];
}
// Clear existing items if count doesn't match
if (this._testItems.length !== dates.length) {
this._testItems.forEach(item => {
if (item.bin && item.bin.get_parent()) {
item.bin.get_parent().remove_child(item.bin);
}
});
this._testItems = [];
// Create new items for the current days count
for (let i = 0; i < dates.length; i++) {
const textItem = new St.Label({
text: '',
style_class: 'test-text-item',
x_align: Clutter.ActorAlign.START,
y_align: Clutter.ActorAlign.CENTER,
style: 'font-family: monospace;'
});
const itemBin = new St.BoxLayout({
style_class: 'popup-menu-item',
reactive: false,
can_focus: false,
track_hover: false,
style: 'padding-top: 2px; padding-bottom: 2px;'
});
itemBin.add_child(textItem);
this._testSection.box.add_child(itemBin);
this._testItems.push({ bin: itemBin, label: textItem });
}
if (!this._separator) {
this._separator = new PopupMenu.PopupSeparatorMenuItem();
this.menu.addMenuItem(this._separator, 1);
}
}
if (this._testItems) {
dates.forEach((date, index) => {
const count = counts[index];
const { label } = this._formatDateWithTests(date, count);
if (this._testItems[index]) {
this._testItems[index].label.text = label;
}
});
}
}
async _updateTypingDisplay() {
try {
// Make sure we have boxes to update
if (!this._boxes || !this._boxes.length) {
return;
}
// Get user's settings from the preferences
const {
monkeytypeUsername: username,
monkeytypeApeKey: apeKey,
showCurrentWeekOnly,
weekStartDay,
highlightCurrentDay,
daysToShow
} = this._preferences;
// Can't do anything without at least a username
if (!username) {
console.warn('Monkeytype Streak Extension: Username is not configured.');
this._setDefaultBoxAppearance();
return;
}
// Fetch typing activity data from Monkeytype API
const result = await fetchTypingActivity(this._soupSession, username, apeKey, showCurrentWeekOnly, weekStartDay, daysToShow);
// Double-check boxes still exist (user might have disabled extension)
if (!this._boxes || !this._boxes.length) {
return;
}
// Check if we got streak-only data (no ApeKey provided)
if (result && result.isStreakOnly) {
this._displayStreakOnly(result.streak, result.maxStreak);
} else if (result && result.length === daysToShow) {
// We have detailed daily activity data
const dates = getDates(false, showCurrentWeekOnly, weekStartDay, daysToShow);
this._updateTestInfoSection(dates, result);
// Update each box with its test count and styling
result.forEach((count, index) => {
if (this._boxes[index]) {
const isToday = this._isToday(dates[index]);
const shouldHighlight = highlightCurrentDay && isToday;
this._setBoxAppearance(this._boxes[index], count, shouldHighlight);
}
});
} else {
// Something went wrong with the Monkeytype API
console.error('Monkeytype Streak Extension: Failed to get valid data.');
this._setDefaultBoxAppearance();
}
} catch (e) {
// Handle errors
console.error(`Monkeytype Streak Extension: Error updating display - ${e.message}`);
if (this._boxes && this._boxes.length) {
this._setDefaultBoxAppearance();
}
}
// Set up the next automatic refresh
this._scheduleNextRefresh();
return Promise.resolve();
}
_isToday(date) {
const today = new Date();
return date.getDate() === today.getDate() &&
date.getMonth() === today.getMonth() &&
date.getFullYear() === today.getFullYear();
}
_setBoxAppearance(box, count = 0, highlight = false) {
// Get current theme settings - map enum index to theme key according to schema
const themeKeys = [
'standard', 'githubDark', 'halloween', 'teal', 'leftPad',
'dracula', 'blue', 'panda', 'sunny', 'pink',
'solarizedDark', 'solarizedLight'
];
const currentThemeName = themeKeys[this._preferences.themeName] || 'standard';
// Convert user's color mode preference (number from settings) to mode name
const colorModeNames = ['opacity', 'grade'];
const currentColorMode = colorModeNames[this._preferences.colorMode] || 'opacity';
// Get the appropriate color for this day's commit count
let color = this._getThemedColor(count, currentThemeName, currentColorMode);
const isEmpty = count === 0;
// Special case: empty boxes get a subtle white fill so they're visible on dark backgrounds
if (isEmpty) {
color = 'rgba(255, 255, 255, 0.12)'; // Just enough white to see on pure black
}
let opacity = 255; // Default to full opacity
if (currentColorMode === 'opacity') {
// In opacity mode, boxes get more opaque with more tests
opacity = count > 0
? DEFAULT_OPACITY + Math.min(count * OPACITY_PER_TEST, MAX_OPACITY_INCREASE)
: 255; // Empty boxes stay fully opaque so the subtle fill is visible
}
if (highlight) {
box.opacity = opacity;
box.style = `${this._getBoxStyle(color, isEmpty)} border: 2px solid rgba(255, 255, 255, 0.6); box-shadow: 0 0 4px rgba(255, 255, 255, 0.3);`;
} else {
// Regular days just get the themed color and opacity
box.opacity = opacity;
box.style = this._getBoxStyle(color, isEmpty);
}
}
_scheduleNextRefresh() {
if (this._refreshTimeoutId) {
GLib.Source.remove(this._refreshTimeoutId);
}
const interval = this._preferences.refreshInterval;
this._refreshTimeoutId = GLib.timeout_add_seconds(
GLib.PRIORITY_DEFAULT,
interval,
() => {
this._updateContributionDisplay();
return GLib.SOURCE_CONTINUE;
}
);
}
_clearTestInfoItems() {
if (this._testItems) {
this._testItems = [];
}
if (this._testSection) {
try {
this._testSection.destroy();
} catch (e) {
console.log('Error destroying test section:', e);
}
this._testSection = null;
}
if (this._separator) {
try {
this._separator.destroy();
} catch (e) {
console.log('Error destroying separator:', e);
}
this._separator = null;
}
}
_displayStreakOnly(streak, maxStreak) {
// Display streak as a simple number instead of weekly boxes
this._boxes.forEach(box => {
this._setBoxAppearance(box, 0, false);
});
this._clearTestInfoItems();
const testSection = new PopupMenu.PopupMenuSection();
const streakItem = new St.Label({
text: `🔥 Current Streak: ${streak} day${streak !== 1 ? 's' : ''}`,
style_class: 'test-text-item',
x_align: Clutter.ActorAlign.START,
y_align: Clutter.ActorAlign.CENTER,
style: 'font-size: 14px; font-weight: bold;'
});
const maxStreakItem = new St.Label({
text: `📊 Max Streak: ${maxStreak} day${maxStreak !== 1 ? 's' : ''}`,
style_class: 'test-text-item',
x_align: Clutter.ActorAlign.START,
y_align: Clutter.ActorAlign.CENTER,
style: 'font-size: 12px; padding-top: 4px;'
});
const noteItem = new St.Label({
text: 'ℹ️ Add ApeKey in settings for detailed daily activity',
style_class: 'test-text-item',
x_align: Clutter.ActorAlign.START,
y_align: Clutter.ActorAlign.CENTER,
style: 'font-size: 10px; padding-top: 8px; color: #888888;'
});
const streakBin = new St.BoxLayout({
style_class: 'popup-menu-item',
reactive: false,
can_focus: false,
track_hover: false,
style: 'padding-top: 4px; padding-bottom: 2px;'
});
const maxStreakBin = new St.BoxLayout({
style_class: 'popup-menu-item',
reactive: false,
can_focus: false,
track_hover: false,
style: 'padding-top: 2px; padding-bottom: 2px;'
});
const noteBin = new St.BoxLayout({
style_class: 'popup-menu-item',
reactive: false,
can_focus: false,
track_hover: false,
style: 'padding-top: 2px; padding-bottom: 4px;'
});
streakBin.add_child(streakItem);
maxStreakBin.add_child(maxStreakItem);
noteBin.add_child(noteItem);
testSection.box.add_child(streakBin);
testSection.box.add_child(maxStreakBin);
testSection.box.add_child(noteBin);
this.menu.addMenuItem(testSection, 0);
this._testSection = testSection;
if (!this._separator) {
this._separator = new PopupMenu.PopupSeparatorMenuItem();
this.menu.addMenuItem(this._separator, 1);
}
}
_setDefaultBoxAppearance() {
this._boxes.forEach(box => {
this._setBoxAppearance(box, 0, false);
});
this._clearTestInfoItems();
const testSection = new PopupMenu.PopupMenuSection();
const item = new PopupMenu.PopupMenuItem(MESSAGES.NO_TESTS);
testSection.addMenuItem(item);
this.menu.addMenuItem(testSection, 0);
this._testSection = testSection;
if (!this._separator) {
this._separator = new PopupMenu.PopupSeparatorMenuItem();
this.menu.addMenuItem(this._separator, 1);
}
}
_refreshMenu() {
if (this.menu.isOpen) {
this.menu.close();
this.menu.open();
}
}
destroy() {
this._boxes.forEach(box => {
box.remove_all_transitions();
});
if (this._refreshTimeoutId) {
GLib.Source.remove(this._refreshTimeoutId);
this._refreshTimeoutId = null;
}
if (this._prefsChangedId) {
this._preferences.disconnectChanged(this._prefsChangedId);
this._prefsChangedId = null;
}
if (this._daysChangedId) {
this._preferences._settings.disconnect(this._daysChangedId);
this._daysChangedId = null;
}
if (this._soupSession) {
this._soupSession.abort();
this._soupSession = null;
}
this._clearTestInfoItems();
this._boxes = null;
this._cache = null;
this._testItems = null;
super.destroy();
}
});
export default class MonkeytypeStreakExtension extends Extension {
enable() {
// Set up user preferences and settings
this._preferences = new ExtensionSettings(this);
// Listen for changes to panel position settings so we can move the indicator
this._positionChangedId = this._preferences._settings.connect('changed', (settings, key) => {
if (key === 'panel-position' || key === 'panel-index') {
this._updateIndicatorPosition();
} else if (key.startsWith('shortcut-')) {
// Reapply shortcuts when they change
this._setupShortcuts();
}
});
// Set up keyboard shortcuts
this._setupShortcuts();
// Wait a bit before creating the indicator to ensure GNOME Shell is ready
// This prevents issues during login/startup
this._enableTimeoutId = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 2000, () => {
if (this._preferences) {
this._indicator = new Indicator(this._preferences, this);
this._updateIndicatorPosition();
}
this._enableTimeoutId = null;
return GLib.SOURCE_REMOVE; // Don't repeat this timeout
});
}
_setupShortcuts() {
// Remove existing shortcuts first
this._removeShortcuts();
// Get the settings
const settings = this._preferences._settings;
// Set up the three keyboard shortcuts
this._shortcutActions = [];
// Shortcut 1: Refresh Now
const refreshShortcut = settings.get_strv('shortcut-refresh');
if (refreshShortcut && refreshShortcut.length > 0 && refreshShortcut[0]) {
const refreshAction = Main.wm.addKeybinding(
'shortcut-refresh',
settings,
Meta.KeyBindingFlags.NONE,
Shell.ActionMode.ALL,
() => {
if (this._indicator) {
this._indicator._refreshNow();
}
}
);
this._shortcutActions.push('shortcut-refresh');
}
// Shortcut 2: Open Monkeytype (homepage or profile based on right-click action)
const openMonkeytypeShortcut = settings.get_strv('shortcut-open-monkeytype');
if (openMonkeytypeShortcut && openMonkeytypeShortcut.length > 0 && openMonkeytypeShortcut[0]) {
const openMonkeytypeAction = Main.wm.addKeybinding(
'shortcut-open-monkeytype',
settings,
Meta.KeyBindingFlags.NONE,
Shell.ActionMode.ALL,
() => {
if (this._indicator) {
this._indicator._openMonkeytype();
}
}
);
this._shortcutActions.push('shortcut-open-monkeytype');
}
// Shortcut 3: Open User Profile
const openProfileShortcut = settings.get_strv('shortcut-open-profile');
if (openProfileShortcut && openProfileShortcut.length > 0 && openProfileShortcut[0]) {
const openProfileAction = Main.wm.addKeybinding(
'shortcut-open-profile',
settings,
Meta.KeyBindingFlags.NONE,
Shell.ActionMode.ALL,
() => {
if (this._indicator) {
this._indicator._openUserProfile();
}
}
);
this._shortcutActions.push('shortcut-open-profile');
}
}
_removeShortcuts() {
if (this._shortcutActions) {
this._shortcutActions.forEach(action => {
Main.wm.removeKeybinding(action);
});
this._shortcutActions = [];
}
}
_updateIndicatorPosition() {
// Don't do anything if there's no indicator yet
if (!this._indicator) return;
// Convert user's position preference to actual panel position
const position = ['left', 'center', 'right'][this._preferences.panelPosition] || 'right';
const index = this._preferences.panelIndex || 0;
// Remove the old indicator from the panel
this._indicator.destroy();
this._indicator = null;
// Create a new indicator in the new position
if (this._preferences) {
this._indicator = new Indicator(this._preferences, this);
Main.panel.addToStatusArea(this.uuid, this._indicator, index, position);
}
}
disable() {
// Clean up any pending timeout from the enable phase
if (this._enableTimeoutId) {
GLib.Source.remove(this._enableTimeoutId);
this._enableTimeoutId = null;
}
// Remove keyboard shortcuts
this._removeShortcuts();
// Stop listening for settings changes
if (this._positionChangedId) {
this._preferences._settings.disconnect(this._positionChangedId);
this._positionChangedId = null;
}
// Remove the indicator from the panel
if (this._indicator) {
this._indicator.destroy();
this._indicator = null;
}
// Clean up preferences
this._preferences = null;
}
}