-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathghub_macro_browser.py
More file actions
3232 lines (2882 loc) · 130 KB
/
Copy pathghub_macro_browser.py
File metadata and controls
3232 lines (2882 loc) · 130 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
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import copy
import json
import sqlite3
import sys
import tkinter as tk
import time
import uuid
from pathlib import Path
from tkinter import filedialog, messagebox, ttk
def bundled_path(name: str) -> Path:
if getattr(sys, "frozen", False):
base = Path(getattr(sys, "_MEIPASS", Path(sys.executable).resolve().parent))
else:
base = Path(__file__).resolve().parent
return base / name
def app_state_path(name: str) -> Path:
return Path(__file__).resolve().parent / name
class HoverToolTip:
"""Show a small delayed tooltip for a widget while the pointer is over it."""
def __init__(self, widget, text: str, delay_ms: int = 450) -> None:
self.widget = widget
self.text = text
self.delay_ms = delay_ms
self._after_id = None
self._tip_window = None
widget.bind("<Enter>", self._schedule, add="+")
widget.bind("<Leave>", self._hide, add="+")
widget.bind("<ButtonPress>", self._hide, add="+")
def _schedule(self, _event=None) -> None:
self._hide()
self._after_id = self.widget.after(self.delay_ms, self._show)
def _show(self) -> None:
if self._tip_window or not self.text:
return
x = self.widget.winfo_rootx() + 16
y = self.widget.winfo_rooty() + self.widget.winfo_height() + 6
self._tip_window = tip = tk.Toplevel(self.widget)
tip.wm_overrideredirect(True)
tip.wm_geometry(f"+{x}+{y}")
label = tk.Label(
tip,
text=self.text,
justify="left",
relief="solid",
borderwidth=1,
background="#fff8d7",
foreground="#202020",
padx=8,
pady=4,
)
label.pack()
def _hide(self, _event=None) -> None:
if self._after_id is not None:
self.widget.after_cancel(self._after_id)
self._after_id = None
if self._tip_window is not None:
self._tip_window.destroy()
self._tip_window = None
KEYBOARD_USAGE_TO_NAME = {
4: "A",
5: "B",
6: "C",
7: "D",
8: "E",
9: "F",
10: "G",
11: "H",
12: "I",
13: "J",
14: "K",
15: "L",
16: "M",
17: "N",
18: "O",
19: "P",
20: "Q",
21: "R",
22: "S",
23: "T",
24: "U",
25: "V",
26: "W",
27: "X",
28: "Y",
29: "Z",
30: "1",
31: "2",
32: "3",
33: "4",
34: "5",
35: "6",
36: "7",
37: "8",
38: "9",
39: "0",
40: "Enter",
41: "Escape",
42: "Backspace",
43: "Tab",
44: "Space",
45: "-",
46: "=",
47: "[",
48: "]",
49: "\\",
51: ";",
52: "'",
53: "`",
54: ",",
55: ".",
56: "/",
57: "Caps Lock",
58: "F1",
59: "F2",
60: "F3",
61: "F4",
62: "F5",
63: "F6",
64: "F7",
65: "F8",
66: "F9",
67: "F10",
68: "F11",
69: "F12",
70: "Print Screen",
71: "Scroll Lock",
72: "Pause",
73: "Insert",
74: "Home",
75: "Page Up",
76: "Delete",
77: "End",
78: "Page Down",
79: "Right",
80: "Left",
81: "Down",
82: "Up",
83: "Num Lock",
84: "Numpad /",
85: "Numpad *",
86: "Numpad -",
87: "Numpad +",
88: "Numpad Enter",
89: "Numpad 1",
90: "Numpad 2",
91: "Numpad 3",
92: "Numpad 4",
93: "Numpad 5",
94: "Numpad 6",
95: "Numpad 7",
96: "Numpad 8",
97: "Numpad 9",
98: "Numpad 0",
99: "Numpad .",
224: "Ctrl",
225: "Shift",
226: "Alt",
227: "GUI",
228: "Right Ctrl",
229: "Right Shift",
230: "Right Alt",
231: "Right GUI",
}
KEYBOARD_NAME_TO_USAGE = {
name.upper(): usage for usage, name in KEYBOARD_USAGE_TO_NAME.items()
}
KEYBOARD_NAME_TO_USAGE.update(
{
"CONTROL": 224,
"LEFT CTRL": 224,
"LEFT CONTROL": 224,
"LEFT SHIFT": 225,
"LEFT ALT": 226,
"LEFT GUI": 227,
"LEFT WINDOWS": 227,
"WIN": 227,
"WINDOWS": 227,
"ESC": 41,
"RETURN": 40,
"PGUP": 75,
"PGDN": 78,
"UP ARROW": 82,
"DOWN ARROW": 81,
"LEFT ARROW": 80,
"RIGHT ARROW": 79,
}
)
COMMON_MODIFIERS = [
("Ctrl", 224),
("Shift", 225),
("Alt", 226),
("GUI", 227),
("Right Ctrl", 228),
("Right Shift", 229),
("Right Alt", 230),
("Right GUI", 231),
]
SUMMARY_ARROW_SYMBOLS = {
"Up": "⮝",
"Down": "⮟",
"Left": "⮜",
"Right": "⮞",
}
class GHubMacroBrowserApp:
def __init__(self, root: tk.Tk) -> None:
self.root = root
self.root.title("Logitech G Hub Macro Browser")
self.root.geometry("1680x1080")
self.file_path: Path | None = None
self.source_kind = "json"
self.settings_db_path: Path | None = None
self.settings_db_row_id: int | None = None
self.data = {}
self.applications_by_id = {}
self.profile_assignments_by_card_id = {}
self.macro_records = []
self.filtered_indices = []
self.current_real_index = None
self.current_component_index = None
self.is_dirty = False
self.component_clipboard = None
self.undo_stack = []
self.redo_stack = []
self.max_history = 50
self.recording_window = None
self.recording_mode = None
self.recorded_components = []
self.record_last_event_time = None
self.record_pressed_usages = set()
self.component_drag_start_row = None
self.component_drag_active = False
self.component_press_selection = ()
self.preferences_path = app_state_path("ghub_macro_browser_prefs.json")
self.preferences = self._load_preferences()
self.vars = {
"application_filter": tk.StringVar(value="All Applications"),
"type_filter": tk.StringVar(value="All"),
"sort_mode": tk.StringVar(value="A-Z"),
"delete_originals_after_duplicate": tk.BooleanVar(value=False),
"search": tk.StringVar(),
"macro_name": tk.StringVar(),
"json_index": tk.StringVar(),
"cards_index": tk.StringVar(),
"onboardable": tk.StringVar(),
"assigned_slots": tk.StringVar(),
"assignment_device_prefix": tk.StringVar(),
"assignment_button_slot": tk.StringVar(),
"application_name": tk.StringVar(),
"application_id": tk.StringVar(),
"macro_type": tk.StringVar(),
"macro_id": tk.StringVar(),
"sequence_default_delay": tk.StringVar(),
"sequence_use_default_delay": tk.BooleanVar(value=True),
"sequence_use_simple_actions": tk.BooleanVar(value=False),
"show_up_down": tk.BooleanVar(value=False),
"component_kind": tk.StringVar(),
"component_key_name": tk.StringVar(),
"component_display_name": tk.StringVar(),
"component_hid_usage": tk.StringVar(),
"component_is_down": tk.BooleanVar(value=False),
"component_delay": tk.StringVar(),
"component_mouse_usage": tk.StringVar(),
"paste_include_state": tk.BooleanVar(value=True),
"record_use_actual_delay": tk.BooleanVar(value=True),
"replace_from": tk.StringVar(),
"replace_to": tk.StringVar(),
"replace_delay": tk.StringVar(),
"keystroke_key_name": tk.StringVar(),
"keystroke_code": tk.StringVar(),
"action_name": tk.StringVar(),
"sequence_summary": tk.StringVar(),
}
self.keystroke_modifier_vars = {
usage: tk.BooleanVar(value=False) for _, usage in COMMON_MODIFIERS
}
self.assignment_memory_vars = {
"m1": tk.BooleanVar(value=True),
"m2": tk.BooleanVar(value=False),
"m3": tk.BooleanVar(value=False),
}
self.assignment_shifted_var = tk.BooleanVar(value=False)
self.status_var = tk.StringVar(value="No file loaded")
self.sequence_info_var = tk.StringVar(value="")
self.component_info_var = tk.StringVar(value="")
self.assignment_status_var = tk.StringVar(value="")
self.filtered_count_var = tk.StringVar(value="")
self._build_ui()
self._bind_change_tracking()
self._bind_shortcuts()
default_path = bundled_path("ghub.json")
if default_path.exists():
self.load_file(default_path)
def _load_preferences(self) -> dict:
"""Load small UI preferences stored beside the script."""
try:
if self.preferences_path.exists():
return json.loads(self.preferences_path.read_text(encoding="utf-8"))
except (OSError, json.JSONDecodeError):
pass
return {}
def _save_preferences(self) -> None:
"""Persist lightweight UI preferences without interrupting the user on failure."""
try:
self.preferences_path.write_text(
json.dumps(self.preferences, indent=2, ensure_ascii=False),
encoding="utf-8",
)
except OSError:
pass
def _confirm_settings_db_write(self) -> bool:
"""Warn before writing directly into G Hub's sqlite database."""
if self.preferences.get("skip_settings_db_write_warning"):
return True
result = {"confirmed": False}
win = tk.Toplevel(self.root)
win.title("Write settings.db")
win.transient(self.root)
win.resizable(False, False)
win.grab_set()
container = ttk.Frame(win, padding=14)
container.grid(row=0, column=0, sticky="nsew")
container.columnconfigure(0, weight=1)
ttk.Label(
container,
text=(
"This writes directly to LG Hub settings.db.\n\n"
"Make sure G Hub is fully closed first, or it may overwrite the change.\n\n"
"Continue?"
),
justify="left",
).grid(row=0, column=0, sticky="w")
dont_remind_var = tk.BooleanVar(value=False)
ttk.Checkbutton(
container,
text="Don't remind me again",
variable=dont_remind_var,
).grid(row=1, column=0, sticky="w", pady=(10, 0))
buttons = ttk.Frame(container)
buttons.grid(row=2, column=0, sticky="e", pady=(12, 0))
def close_dialog(confirmed: bool) -> None:
result["confirmed"] = confirmed
if confirmed and dont_remind_var.get():
self.preferences["skip_settings_db_write_warning"] = True
self._save_preferences()
win.destroy()
ttk.Button(buttons, text="Yes", command=lambda: close_dialog(True)).pack(side="left", padx=(0, 6))
ttk.Button(buttons, text="No", command=lambda: close_dialog(False)).pack(side="left")
win.protocol("WM_DELETE_WINDOW", lambda: close_dialog(False))
win.update_idletasks()
x = self.root.winfo_rootx() + max((self.root.winfo_width() - win.winfo_width()) // 2, 0)
y = self.root.winfo_rooty() + max((self.root.winfo_height() - win.winfo_height()) // 2, 0)
win.geometry(f"+{x}+{y}")
self.root.wait_window(win)
return result["confirmed"]
def _add_tooltip(self, widget, text: str) -> None:
"""Attach a hover tooltip to a widget and keep the helper alive."""
widget._hover_tooltip = HoverToolTip(widget, text)
def _build_ui(self) -> None:
"""Build the main window layout, top controls, and the detail panes."""
self.root.columnconfigure(0, weight=1)
self.root.rowconfigure(1, weight=1)
top = ttk.Frame(self.root, padding=8)
top.grid(row=0, column=0, sticky="ew")
top.columnconfigure(0, weight=1)
action_row = ttk.Frame(top)
action_row.grid(row=0, column=0, sticky="w")
open_json_button = ttk.Button(action_row, text="Open JSON", command=self.open_file)
open_json_button.grid(row=0, column=0, padx=(0, 4), pady=2)
self._add_tooltip(open_json_button, "Open a ghub.json export for browsing and editing.")
open_db_button = ttk.Button(action_row, text="Open settings.db", command=self.open_settings_db)
open_db_button.grid(row=0, column=1, padx=4, pady=2)
self._add_tooltip(open_db_button, "Open Logitech G Hub's live settings.db database.")
self.save_button = ttk.Button(top, text="Save", command=self.save_file)
self.save_button.grid(in_=action_row, row=0, column=2, padx=4, pady=2)
self._add_tooltip(self.save_button, "Save the current data back to the loaded JSON file or settings.db.")
save_as_button = ttk.Button(action_row, text="Save As", command=self.save_file_as)
save_as_button.grid(row=0, column=3, padx=4, pady=2)
self._add_tooltip(save_as_button, "Write the current JSON data to a new file.")
reload_button = ttk.Button(action_row, text="Reload", command=self.reload_file)
reload_button.grid(row=0, column=4, padx=4, pady=2)
self._add_tooltip(reload_button, "Reload the current source from disk and discard unsaved edits.")
undo_button = ttk.Button(action_row, text="Undo", command=self.undo)
undo_button.grid(row=0, column=5, padx=4, pady=2)
self._add_tooltip(undo_button, "Undo the last in-app edit.")
redo_button = ttk.Button(action_row, text="Redo", command=self.redo)
redo_button.grid(row=0, column=6, padx=(4, 0), pady=2)
self._add_tooltip(redo_button, "Redo the last undone edit.")
new_macro_button = ttk.Button(action_row, text="New Macro", command=self.create_new_macro)
new_macro_button.grid(row=0, column=7, padx=(8, 0), pady=2)
self._add_tooltip(new_macro_button, "Create a new macro near the current editable macro selection.")
delete_macro_button = ttk.Button(action_row, text="Delete Macro", command=self.delete_selected_macro)
delete_macro_button.grid(row=0, column=8, padx=(8, 0), pady=2)
self._add_tooltip(delete_macro_button, "Delete the selected macro and any assignments that point to it.")
filter_row = ttk.Frame(top)
filter_row.grid(row=1, column=0, sticky="ew")
filter_row.columnconfigure(1, weight=2)
filter_row.columnconfigure(3, weight=3)
ttk.Label(filter_row, text="Application").grid(row=0, column=0, padx=(0, 4), pady=2, sticky="w")
self.application_filter = ttk.Combobox(
filter_row,
textvariable=self.vars["application_filter"],
state="normal",
width=24,
)
self.application_filter.grid(row=0, column=1, sticky="ew", padx=4, pady=2)
self.application_filter.bind("<<ComboboxSelected>>", lambda *_: self.refresh_macro_list())
self.application_filter.bind("<KeyRelease>", self.on_application_filter_typed)
self.application_filter.bind("<FocusIn>", lambda *_: self._update_application_filter_options())
ttk.Label(filter_row, text="Search").grid(row=0, column=2, padx=(8, 4), pady=2, sticky="e")
search_entry = ttk.Entry(filter_row, textvariable=self.vars["search"])
search_entry.grid(row=0, column=3, sticky="ew", padx=4, pady=2)
ttk.Label(filter_row, text="Type").grid(row=0, column=4, padx=(8, 4), pady=2)
self.type_filter = ttk.Combobox(
filter_row,
textvariable=self.vars["type_filter"],
values=["All", "SEQUENCE", "KEYSTROKE", "ACTION"],
state="readonly",
width=11,
)
self.type_filter.grid(row=0, column=5, padx=4, pady=2)
self.type_filter.bind("<<ComboboxSelected>>", lambda *_: self.refresh_macro_list())
ttk.Label(filter_row, text="Sort").grid(row=0, column=6, padx=(8, 4), pady=2)
sort_box = ttk.Combobox(
filter_row,
textvariable=self.vars["sort_mode"],
values=["A-Z", "Z-A"],
state="readonly",
width=7,
)
sort_box.grid(row=0, column=7, padx=4, pady=2)
reorder_button = ttk.Button(filter_row, text="Reorder Filtered", command=self.reorder_filtered_macros)
reorder_button.grid(row=0, column=8, padx=(8, 4), pady=2)
self._add_tooltip(reorder_button, "Sort the filtered macros by name within their existing JSON lists.")
compact_button = ttk.Button(filter_row, text="Compact Filtered", command=self.compact_filtered_macros)
compact_button.grid(row=0, column=9, padx=4, pady=2)
self._add_tooltip(compact_button, "Remove gaps left by filtered macros and renumber JSON indexes.")
duplicate_button = ttk.Button(filter_row, text="Duplicate Filtered", command=self.duplicate_filtered_macros)
duplicate_button.grid(row=0, column=10, padx=4, pady=2)
self._add_tooltip(duplicate_button, "Duplicate every currently filtered macro.")
delete_originals_check = ttk.Checkbutton(
filter_row,
text="Delete originals",
variable=self.vars["delete_originals_after_duplicate"],
)
delete_originals_check.grid(row=0, column=11, padx=(8, 0), pady=2)
self._add_tooltip(
delete_originals_check,
"When duplicating filtered macros, remove the originals after the copies are created.",
)
ttk.Label(top, textvariable=self.status_var).grid(
row=2, column=0, columnspan=12, sticky="w", pady=(6, 0)
)
content = ttk.Panedwindow(self.root, orient=tk.HORIZONTAL)
content.grid(row=1, column=0, sticky="nsew")
left = ttk.Frame(content, padding=(8, 0, 4, 8))
left.columnconfigure(0, weight=1)
left.columnconfigure(1, weight=0)
left.rowconfigure(1, weight=1)
list_header = ttk.Frame(left)
list_header.grid(row=0, column=0, sticky="ew")
list_header.columnconfigure(0, weight=1)
list_header.columnconfigure(1, weight=0)
ttk.Label(list_header, text="Macros").grid(row=0, column=0, sticky="w")
ttk.Label(list_header, textvariable=self.filtered_count_var).grid(row=0, column=1, sticky="e", padx=(8, 0))
self.macro_listbox = tk.Listbox(left, width=54, exportselection=False)
self.macro_listbox.grid(row=1, column=0, sticky="nsew")
self.macro_listbox.bind("<<ListboxSelect>>", self.on_macro_select)
list_scroll = ttk.Scrollbar(left, orient="vertical", command=self.macro_listbox.yview)
list_scroll.grid(row=1, column=1, sticky="ns")
self.macro_listbox.configure(yscrollcommand=list_scroll.set)
right = ttk.Frame(content, padding=(4, 0, 8, 8))
right.columnconfigure(1, weight=1)
right.columnconfigure(3, weight=1)
right.rowconfigure(6, weight=1)
content.add(left, weight=0)
content.add(right, weight=1)
fields = [
("Macro name", "macro_name", 0, 0),
("Application", "application_name", 0, 2),
("Macro type", "macro_type", 1, 0),
("Application ID", "application_id", 1, 2),
("Macro ID", "macro_id", 2, 0),
("JSON index", "json_index", 2, 2),
("Action name", "action_name", 3, 0),
("Cards index", "cards_index", 3, 2),
("Onboardable", "onboardable", 4, 0),
("Assigned slots", "assigned_slots", 4, 2),
]
for label, key, row, col in fields:
ttk.Label(right, text=label).grid(row=row, column=col, sticky="w", padx=4, pady=4)
entry = ttk.Entry(right, textvariable=self.vars[key])
if key in {
"json_index",
"cards_index",
"onboardable",
"assigned_slots",
"application_name",
"application_id",
"macro_type",
"macro_id",
}:
entry.configure(state="readonly")
entry.grid(row=row, column=col + 1, sticky="ew", padx=4, pady=4)
if key == "macro_name":
self.macro_name_entry = entry
ttk.Label(right, text="Sequence summary").grid(row=5, column=0, sticky="w", padx=4, pady=(4, 4))
self.summary_entry = ttk.Entry(
right,
textvariable=self.vars["sequence_summary"],
state="readonly",
font=("Segoe UI Symbol", 14, "bold"),
)
self.summary_entry.grid(row=5, column=1, columnspan=3, sticky="ew", padx=4, pady=(4, 4))
notebook = ttk.Notebook(right)
notebook.grid(row=6, column=0, columnspan=4, sticky="nsew", padx=4, pady=(8, 6))
self.sequence_tab = ttk.Frame(notebook, padding=6)
self.keystroke_tab = ttk.Frame(notebook, padding=6)
self.record_tab = ttk.Frame(notebook, padding=6)
self.raw_tab = ttk.Frame(notebook, padding=6)
notebook.add(self.sequence_tab, text="Sequence")
notebook.add(self.keystroke_tab, text="Keystroke")
notebook.add(self.record_tab, text="Record Keys")
notebook.add(self.raw_tab, text="Raw JSON")
self._build_sequence_tab()
self._build_keystroke_tab()
self._build_record_tab()
self._build_raw_tab()
buttons = ttk.Frame(right)
buttons.grid(row=7, column=0, columnspan=4, sticky="ew")
apply_button = ttk.Button(buttons, text="Apply Current Changes", command=self.apply_current_edits)
apply_button.pack(side="left", padx=4)
self._add_tooltip(apply_button, "Apply the values shown in the form to the selected macro.")
revert_button = ttk.Button(buttons, text="Revert Current View", command=self.reload_current)
revert_button.pack(side="left", padx=4)
self._add_tooltip(revert_button, "Reload the selected macro from the in-memory data and discard form edits.")
assignment_tools = ttk.Frame(right)
assignment_tools.grid(row=8, column=0, columnspan=4, sticky="ew", pady=(6, 0))
ttk.Label(
assignment_tools,
textvariable=self.assignment_status_var,
).pack(side="top", anchor="w", padx=4, pady=(0, 4))
assignment_controls = ttk.Frame(assignment_tools)
assignment_controls.pack(side="top", fill="x")
ttk.Label(assignment_controls, text="Device").pack(side="left", padx=(4, 4))
ttk.Entry(
assignment_controls,
textvariable=self.vars["assignment_device_prefix"],
width=10,
).pack(side="left", padx=4)
ttk.Label(assignment_controls, text="G key").pack(side="left", padx=(4, 4))
ttk.Entry(
assignment_controls,
textvariable=self.vars["assignment_button_slot"],
width=8,
).pack(side="left", padx=4)
m1_check = ttk.Checkbutton(
assignment_controls,
text="M1",
variable=self.assignment_memory_vars["m1"],
)
m1_check.pack(side="left", padx=2)
self._add_tooltip(m1_check, "Assign the selected macro to the M1 memory profile.")
m2_check = ttk.Checkbutton(
assignment_controls,
text="M2",
variable=self.assignment_memory_vars["m2"],
)
m2_check.pack(side="left", padx=2)
self._add_tooltip(m2_check, "Assign the selected macro to the M2 memory profile.")
m3_check = ttk.Checkbutton(
assignment_controls,
text="M3",
variable=self.assignment_memory_vars["m3"],
)
m3_check.pack(side="left", padx=2)
self._add_tooltip(m3_check, "Assign the selected macro to the M3 memory profile.")
gshift_check = ttk.Checkbutton(
assignment_controls,
text="G-Shift",
variable=self.assignment_shifted_var,
)
gshift_check.pack(side="left", padx=(8, 2))
self._add_tooltip(gshift_check, "Assign the selected macro to the G-Shift layer for the chosen slot.")
assign_button = ttk.Button(
assignment_controls,
text="Assign Selected Macro",
command=self.assign_selected_macro_to_slots,
)
assign_button.pack(side="left", padx=4)
self._add_tooltip(assign_button, "Assign the selected macro to the specified device slot(s).")
clear_assignments_button = ttk.Button(
assignment_controls,
text="Clear Macro Assignments",
command=self.clear_selected_macro_assignments,
)
clear_assignments_button.pack(side="left", padx=4)
self._add_tooltip(clear_assignments_button, "Remove slot assignments that point at the selected macro.")
def _build_sequence_tab(self) -> None:
"""Build the sequence editor, component list, and bulk sequence tools."""
frame = self.sequence_tab
frame.columnconfigure(0, weight=3)
frame.columnconfigure(1, weight=2)
frame.rowconfigure(2, weight=1)
meta = ttk.Frame(frame)
meta.grid(row=0, column=0, columnspan=2, sticky="ew")
for col in range(6):
meta.columnconfigure(col, weight=1 if col % 2 else 0)
ttk.Label(meta, text="Default delay").grid(row=0, column=0, sticky="w", padx=4, pady=4)
ttk.Entry(meta, textvariable=self.vars["sequence_default_delay"], width=10).grid(
row=0, column=1, sticky="w", padx=4, pady=4
)
use_default_delay_check = ttk.Checkbutton(
meta,
text="Use default delay",
variable=self.vars["sequence_use_default_delay"],
)
use_default_delay_check.grid(row=0, column=2, sticky="w", padx=4, pady=4)
self._add_tooltip(
use_default_delay_check,
"Use the sequence default delay value when the macro runs and when recording with default timing.",
)
use_simple_actions_check = ttk.Checkbutton(
meta,
text="Use simple actions",
variable=self.vars["sequence_use_simple_actions"],
)
use_simple_actions_check.grid(row=0, column=3, sticky="w", padx=4, pady=4)
self._add_tooltip(use_simple_actions_check, "Set the sequence to use G Hub simple action playback mode.")
show_up_down_check = ttk.Checkbutton(
meta,
text="Show up/down",
variable=self.vars["show_up_down"],
)
show_up_down_check.grid(row=0, column=4, sticky="w", padx=4, pady=4)
self._add_tooltip(show_up_down_check, "Tell G Hub to show key/button up and down events in the sequence.")
paste_state_check = ttk.Checkbutton(
meta,
text="Paste includes up/down state",
variable=self.vars["paste_include_state"],
)
paste_state_check.grid(row=0, column=5, sticky="w", padx=4, pady=4)
self._add_tooltip(
paste_state_check,
"When pasting over a component, include the copied up/down press state instead of preserving the target state.",
)
ttk.Label(meta, textvariable=self.sequence_info_var).grid(
row=1, column=0, columnspan=6, sticky="w", padx=4, pady=(0, 4)
)
self.component_tree = ttk.Treeview(
frame,
columns=("index", "type", "summary", "state"),
show="headings",
height=20,
)
self.component_tree.grid(row=2, column=0, rowspan=2, sticky="nsew", padx=(0, 8))
for col, text, width in [
("index", "#", 50),
("type", "Type", 90),
("summary", "Summary", 260),
("state", "State / Value", 200),
]:
self.component_tree.heading(col, text=text)
self.component_tree.column(col, width=width, stretch=(col == "summary"))
self.component_tree.bind("<ButtonPress-1>", self.on_component_tree_press, add="+")
self.component_tree.bind("<B1-Motion>", self.on_component_tree_drag, add="+")
self.component_tree.bind("<ButtonRelease-1>", self.on_component_tree_release, add="+")
self.component_tree.bind("<<TreeviewSelect>>", self.on_component_select)
tree_scroll = ttk.Scrollbar(frame, orient="vertical", command=self.component_tree.yview)
tree_scroll.grid(row=2, column=0, rowspan=2, sticky="nse")
self.component_tree.configure(yscrollcommand=tree_scroll.set)
editor = ttk.LabelFrame(frame, text="Selected Component", padding=8)
editor.grid(row=2, column=1, sticky="nsew")
for col in range(2):
editor.columnconfigure(col, weight=1)
ttk.Label(editor, textvariable=self.component_info_var).grid(
row=0, column=0, columnspan=2, sticky="w", pady=(0, 8)
)
ttk.Label(editor, text="Component type").grid(row=1, column=0, sticky="w", padx=4, pady=4)
type_box = ttk.Combobox(
editor,
textvariable=self.vars["component_kind"],
state="readonly",
values=["keyboard", "delay", "mouse"],
)
type_box.grid(row=1, column=1, sticky="ew", padx=4, pady=4)
type_box.bind("<<ComboboxSelected>>", lambda *_: self._toggle_component_editor_sections())
self.keyboard_editor = ttk.LabelFrame(editor, text="Keyboard", padding=6)
self.keyboard_editor.grid(row=2, column=0, columnspan=2, sticky="ew", padx=4, pady=4)
for col in range(2):
self.keyboard_editor.columnconfigure(col, weight=1)
ttk.Label(self.keyboard_editor, text="Known key").grid(row=0, column=0, sticky="w", padx=4, pady=4)
self.key_name_box = ttk.Combobox(
self.keyboard_editor,
textvariable=self.vars["component_key_name"],
values=sorted(KEYBOARD_USAGE_TO_NAME.values(), key=str.upper),
)
self.key_name_box.grid(row=0, column=1, sticky="ew", padx=4, pady=4)
self.key_name_box.bind("<<ComboboxSelected>>", self.on_known_key_selected)
ttk.Label(self.keyboard_editor, text="Display name").grid(
row=1, column=0, sticky="w", padx=4, pady=4
)
ttk.Entry(
self.keyboard_editor,
textvariable=self.vars["component_display_name"],
).grid(row=1, column=1, sticky="ew", padx=4, pady=4)
ttk.Label(self.keyboard_editor, text="HID usage").grid(row=2, column=0, sticky="w", padx=4, pady=4)
ttk.Entry(
self.keyboard_editor,
textvariable=self.vars["component_hid_usage"],
).grid(row=2, column=1, sticky="ew", padx=4, pady=4)
key_down_check = ttk.Checkbutton(
self.keyboard_editor,
text="Key down event",
variable=self.vars["component_is_down"],
)
key_down_check.grid(row=3, column=0, columnspan=2, sticky="w", padx=4, pady=4)
self._add_tooltip(key_down_check, "Checked means this keyboard component is a key-down event; unchecked means key-up.")
self.delay_editor = ttk.LabelFrame(editor, text="Delay", padding=6)
self.delay_editor.grid(row=3, column=0, columnspan=2, sticky="ew", padx=4, pady=4)
self.delay_editor.columnconfigure(1, weight=1)
ttk.Label(self.delay_editor, text="Duration ms").grid(row=0, column=0, sticky="w", padx=4, pady=4)
ttk.Entry(self.delay_editor, textvariable=self.vars["component_delay"]).grid(
row=0, column=1, sticky="ew", padx=4, pady=4
)
self.mouse_editor = ttk.LabelFrame(editor, text="Mouse Button", padding=6)
self.mouse_editor.grid(row=4, column=0, columnspan=2, sticky="ew", padx=4, pady=4)
self.mouse_editor.columnconfigure(1, weight=1)
ttk.Label(self.mouse_editor, text="Button hidUsage").grid(
row=0, column=0, sticky="w", padx=4, pady=4
)
ttk.Entry(self.mouse_editor, textvariable=self.vars["component_mouse_usage"]).grid(
row=0, column=1, sticky="ew", padx=4, pady=4
)
button_down_check = ttk.Checkbutton(
self.mouse_editor,
text="Button down event",
variable=self.vars["component_is_down"],
)
button_down_check.grid(row=1, column=0, columnspan=2, sticky="w", padx=4, pady=4)
self._add_tooltip(
button_down_check,
"Checked means this mouse component is a button-down event; unchecked means button-up.",
)
actions = ttk.Frame(editor)
actions.grid(row=5, column=0, columnspan=2, sticky="ew", pady=(8, 0))
actions.columnconfigure(0, weight=1)
action_row_1 = ttk.Frame(actions)
action_row_1.grid(row=0, column=0, sticky="w")
update_component_button = ttk.Button(action_row_1, text="Update Component", command=self.update_component)
update_component_button.pack(side="left", padx=4, pady=2)
self._add_tooltip(update_component_button, "Write the editor values back into the selected component.")
add_keyboard_button = ttk.Button(action_row_1, text="Add Keyboard", command=self.add_keyboard_component)
add_keyboard_button.pack(side="left", padx=4, pady=2)
self._add_tooltip(add_keyboard_button, "Insert a new keyboard component after the current selection.")
add_delay_button = ttk.Button(action_row_1, text="Add Delay", command=self.add_delay_component)
add_delay_button.pack(side="left", padx=4, pady=2)
self._add_tooltip(add_delay_button, "Insert a new delay component after the current selection.")
delete_component_button = ttk.Button(action_row_1, text="Delete Component", command=self.delete_component)
delete_component_button.pack(side="left", padx=4, pady=2)
self._add_tooltip(delete_component_button, "Delete the selected component from the sequence.")
action_row_2 = ttk.Frame(actions)
action_row_2.grid(row=1, column=0, sticky="w")
move_up_button = ttk.Button(action_row_2, text="Move Up", command=lambda: self.move_component(-1))
move_up_button.pack(side="left", padx=4, pady=2)
self._add_tooltip(move_up_button, "Move the selected component one row earlier.")
move_down_button = ttk.Button(action_row_2, text="Move Down", command=lambda: self.move_component(1))
move_down_button.pack(side="left", padx=4, pady=2)
self._add_tooltip(move_down_button, "Move the selected component one row later.")
record_replace_button = ttk.Button(
action_row_2,
text="Record Replace",
command=lambda: self.start_keystroke_recording("replace"),
)
record_replace_button.pack(side="left", padx=4, pady=2)
self._add_tooltip(record_replace_button, "Record keys and replace the current sequence components.")
record_append_button = ttk.Button(
action_row_2,
text="Record Append",
command=lambda: self.start_keystroke_recording("append"),
)
record_append_button.pack(side="left", padx=4, pady=2)
self._add_tooltip(record_append_button, "Record keys and append them to the current sequence.")
tools = ttk.LabelFrame(frame, text="Replace / Delay Tools", padding=8)
tools.grid(row=3, column=1, sticky="new", pady=(10, 0))
for col in range(4):
tools.columnconfigure(col, weight=1)
ttk.Label(tools, text="Replace keyboard key in sequence macros").grid(
row=0, column=0, columnspan=4, sticky="w", padx=4, pady=(0, 6)
)
ttk.Label(tools, text="From").grid(row=1, column=0, sticky="w", padx=4, pady=4)
from_box = ttk.Combobox(
tools,
textvariable=self.vars["replace_from"],
values=sorted(KEYBOARD_USAGE_TO_NAME.values(), key=str.upper),
)
from_box.grid(row=1, column=1, sticky="ew", padx=4, pady=4)
ttk.Label(tools, text="To").grid(row=1, column=2, sticky="w", padx=4, pady=4)
to_box = ttk.Combobox(
tools,
textvariable=self.vars["replace_to"],
values=sorted(KEYBOARD_USAGE_TO_NAME.values(), key=str.upper),
)
to_box.grid(row=1, column=3, sticky="ew", padx=4, pady=4)
replace_current_button = ttk.Button(
tools,
text="Replace In Current Macro",
command=lambda: self.replace_key_in_scope(filtered_only=False),
)
replace_current_button.grid(row=2, column=0, columnspan=2, sticky="w", padx=4, pady=6)
self._add_tooltip(replace_current_button, "Replace one keyboard key everywhere in the selected macro.")
replace_filtered_button = ttk.Button(
tools,
text="Replace In Filtered Macros",
command=lambda: self.replace_key_in_scope(filtered_only=True),
)
replace_filtered_button.grid(row=2, column=2, columnspan=2, sticky="w", padx=4, pady=6)
self._add_tooltip(replace_filtered_button, "Replace one keyboard key across all filtered macros.")
ttk.Separator(tools, orient="horizontal").grid(row=3, column=0, columnspan=4, sticky="ew", padx=4, pady=8)
ttk.Label(tools, text="Delay utilities").grid(
row=4, column=0, columnspan=4, sticky="w", padx=4, pady=(0, 6)
)
ttk.Label(tools, text="New delay ms").grid(row=5, column=0, sticky="w", padx=4, pady=4)
ttk.Entry(tools, textvariable=self.vars["replace_delay"]).grid(
row=5, column=1, sticky="ew", padx=4, pady=4
)
set_current_delays_button = ttk.Button(
tools, text="Set All Delays In Current Macro", command=self.set_all_delays_current
)
set_current_delays_button.grid(row=6, column=0, sticky="w", padx=4, pady=6)
self._add_tooltip(set_current_delays_button, "Set every delay component in the selected macro to the new value.")
set_filtered_delays_button = ttk.Button(
tools,
text="Set All Delays In Filtered Macros",
command=lambda: self.set_all_delays_in_scope(filtered_only=True),
)
set_filtered_delays_button.grid(row=6, column=1, sticky="w", padx=4, pady=6)
self._add_tooltip(set_filtered_delays_button, "Set every delay component across the filtered macros.")
set_default_delay_button = ttk.Button(
tools, text="Set Sequence Default Delay", command=self.set_sequence_default_delay
)
set_default_delay_button.grid(row=6, column=2, sticky="w", padx=4, pady=6)
self._add_tooltip(set_default_delay_button, "Update the selected macro's sequence default delay field.")
set_filtered_default_delay_button = ttk.Button(
tools,
text="Set Default Delay In Filtered Macros",
command=lambda: self.set_sequence_default_delay_in_scope(filtered_only=True),
)
set_filtered_default_delay_button.grid(row=6, column=3, sticky="w", padx=4, pady=6)
self._add_tooltip(
set_filtered_default_delay_button,
"Update the sequence default delay field across all filtered macros.",
)
ttk.Label(
tools,
text=(
"These tools only operate on SEQUENCE macros. Filter first if you want to target a specific subset."
),
wraplength=1100,
justify="left",
).grid(row=7, column=0, columnspan=4, sticky="w", padx=4, pady=(8, 0))
def _build_keystroke_tab(self) -> None:
"""Build the simple keystroke macro editor."""
frame = self.keystroke_tab
for col in range(2):
frame.columnconfigure(col, weight=1)
ttk.Label(
frame,
text="For simple G Hub keystrokes, edit the key code and modifier list here.",
).grid(row=0, column=0, columnspan=2, sticky="w", pady=(0, 8))
ttk.Label(frame, text="Known key").grid(row=1, column=0, sticky="w", padx=4, pady=4)
self.keystroke_key_box = ttk.Combobox(
frame,
textvariable=self.vars["keystroke_key_name"],
values=sorted(KEYBOARD_USAGE_TO_NAME.values(), key=str.upper),
)
self.keystroke_key_box.grid(row=1, column=1, sticky="ew", padx=4, pady=4)
self.keystroke_key_box.bind("<<ComboboxSelected>>", self.on_keystroke_key_selected)
ttk.Label(frame, text="Code / HID usage").grid(row=2, column=0, sticky="w", padx=4, pady=4)
ttk.Entry(frame, textvariable=self.vars["keystroke_code"]).grid(
row=2, column=1, sticky="ew", padx=4, pady=4
)
modifier_frame = ttk.LabelFrame(frame, text="Modifiers", padding=6)