-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathezswitch.py
More file actions
2629 lines (2155 loc) · 116 KB
/
Copy pathezswitch.py
File metadata and controls
2629 lines (2155 loc) · 116 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 tkinter as tk
from tkinter import ttk, messagebox
import subprocess
import os
import sys
import threading
import json
from pathlib import Path
import platform
# Platform detection and module imports
IS_WINDOWS = platform.system() == "Windows"
IS_LINUX = platform.system() == "Linux"
IS_MACOS = platform.system() == "Darwin"
# Try to import Windows-specific modules
try:
import win32gui
import win32con
from ctypes import windll, c_int, byref
WIN32_AVAILABLE = True
except ImportError:
WIN32_AVAILABLE = False
# Font management for Poppins
class FontManager:
def __init__(self):
self.primary_font = "Poppins"
# Platform-specific font preferences
if IS_LINUX:
self.fallback_fonts = ["Ubuntu", "DejaVu Sans", "Liberation Sans", "Noto Sans", "Cantarell", "Arial", "Helvetica", "sans-serif"]
elif IS_MACOS:
self.fallback_fonts = ["SF Pro", "San Francisco", "Helvetica Neue", "Arial", "Helvetica", "sans-serif"]
else:
self.fallback_fonts = ["Segoe UI", "Arial", "Helvetica", "sans-serif"]
self.available_font = None
self._font_detection_attempted = False
self.scaling_factor = 1.0
def get_available_font(self):
"""Get the best available font, preferring Poppins"""
if self.available_font:
return self.available_font
if self._font_detection_attempted:
return "TkDefaultFont"
try:
import tkinter.font as tkfont
# Check if root window exists
try:
root = tk._default_root
if root is None:
# No root window yet, return fallback
self.available_font = "TkDefaultFont"
return self.available_font
except:
pass
# Get list of available font families
available_fonts = tkfont.families()
# Check if Poppins is available
if self.primary_font in available_fonts:
self.available_font = self.primary_font
return self.available_font
# Check fallback fonts in order of preference
for font in self.fallback_fonts:
if font in available_fonts:
self.available_font = font
return self.available_font
except Exception:
pass
finally:
self._font_detection_attempted = True
# Ultimate fallback
self.available_font = "TkDefaultFont"
return self.available_font
def get_font(self, size=10, weight="normal", slant="roman"):
"""Get a font tuple with the best available font and platform-appropriate scaling"""
# Ensure we have detected fonts
if not self.available_font:
self.get_available_font()
# Platform-specific scaling
if IS_LINUX:
# Linux often needs smaller fonts for the same perceived size
scaled_size = max(size - 1, 8) # Reduce by 1 but keep minimum of 8
elif IS_MACOS:
# macOS handles font scaling well, use original size
scaled_size = size
else:
scaled_size = size
# Handle special font styles
weight_val = "bold" if weight == "bold" else "normal"
slant_val = "italic" if slant == "italic" else "roman"
# Return standard 4-element font tuple
return (self.available_font, scaled_size, weight_val, slant_val)
# Initialize font manager
font_manager = FontManager()
class ClaudeConfigSwitcher:
def __init__(self, root):
self.root = root
self.root.title("")
# Platform-specific window settings
if IS_LINUX:
# Linux: More conservative size and allow resizing
self.root.geometry("550x850")
self.root.resizable(True, True)
self.root.minsize(450, 700)
else:
# Windows: Original size and no resizing
self.root.geometry("600x1000")
self.root.resizable(False, False)
# Keep native window decorations (title bar with minimize/maximize/close buttons)
# Platform-specific window handling
if IS_WINDOWS:
# Keep native window decorations on Windows
pass
elif IS_MACOS:
# On macOS, use native window decorations for best integration
# Don't override redirect to maintain proper macOS behavior
pass
else:
# On Linux, skip window override to avoid focus issues
# Keep native window decorations for better compatibility
pass
# Set window styles
self.set_window_style()
# Path for storing API keys persistently
self.config_dir = Path.home() / ".claude_ez_switch"
self.config_dir.mkdir(exist_ok=True)
self.config_file = self.config_dir / "config.json"
# Path for Claude Code settings.json
self.claude_settings_dir = Path.home() / ".claude"
self.claude_settings_file = self.claude_settings_dir / "settings.json"
# Configure style
style = ttk.Style()
style.theme_use('clam')
# Color scheme
self.bg_color = "#1e1e1e"
self.fg_color = "#ffffff"
self.accent_color = "#007acc"
self.radio_orange_color = "#FFB366" # Lighter orange for radio buttons
self.button_bg = "#0e639c"
self.button_hover = "#1177bb"
self.entry_bg = "#2d2d2d"
self.success_color = "#4caf50"
self.error_color = "#f44336"
self.close_button_bg = "#555555"
self.close_button_hover = "#666666"
self.refresh_button_bg = "#2d5f2d"
self.refresh_button_hover = "#3d7f3d"
self.root.configure(bg=self.bg_color)
# Configure styles
style.configure('Title.TLabel', background=self.bg_color, foreground=self.fg_color,
font=font_manager.get_font(22, 'bold'))
style.configure('Subtitle.TLabel', background=self.bg_color, foreground=self.fg_color,
font=font_manager.get_font(13))
style.configure('TLabel', background=self.bg_color, foreground=self.fg_color,
font=font_manager.get_font(10))
# Linux-specific radio button styling - use custom approach for larger indicators
if IS_LINUX:
# Custom radio button variables and storage
self.custom_radio_buttons = {}
# Don't use ttk radio buttons on Linux - we'll create custom ones
else:
style.configure('TRadiobutton', background=self.bg_color, foreground=self.fg_color,
font=font_manager.get_font(10))
style.map('TRadiobutton', background=[('active', self.bg_color)])
# Linux-specific combobox styling for larger dropdown arrow
if IS_LINUX:
# Keep the original theme but enhance combobox visibility
style.configure('TCombobox',
fieldbackground=self.entry_bg,
background=self.entry_bg,
foreground=self.fg_color,
arrowsize=25, # Much larger arrow for better visibility
padding=(8, 4), # More padding
borderwidth=1,
relief="solid")
style.map('TCombobox',
background=[('readonly', self.entry_bg), ('active', "#5a5a5a")],
fieldbackground=[('readonly', self.entry_bg)],
arrowcolor=[('readonly', self.fg_color), ('active', "#ffffff")])
# Linux-specific checkbox styling for better visibility
style.configure('TCheckbutton', background=self.bg_color, foreground=self.fg_color,
font=font_manager.get_font(12), # Larger font
padding=(10, 6)) # More padding
style.map('TCheckbutton', background=[('active', self.bg_color)])
self.create_widgets()
self.load_saved_api_keys()
self.load_existing_api_keys()
# Update UI to match loaded configuration
self.on_config_change()
self.on_claude_mode_change()
self.check_current_status()
# Set up focus management for Linux
if IS_LINUX:
# Schedule focus setup after window is fully displayed
self.root.after(1000, self.setup_linux_focus)
def set_window_style(self):
"""Set window styles for borderless window with shadow (platform-specific)"""
if IS_WINDOWS and WIN32_AVAILABLE:
try:
# Get the window handle
hwnd = self.root.winfo_id()
if hwnd == 0:
# Window not yet created, try again later
self.root.after(100, self.set_window_style)
return
# Set window styles for borderless window with shadow
style = c_int(win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE))
style.value |= win32con.WS_EX_APPWINDOW | win32con.WS_EX_TOOLWINDOW
win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE, style.value)
# Add window shadow
try:
windll.dwmapi.DwmSetWindowAttribute(hwnd, 2, byref(c_int(2)), 4)
except:
# DWM might not be available, continue without shadow
pass
except Exception:
pass
elif IS_LINUX:
# On Linux, be more careful with window decorations to avoid display issues
try:
# Check if we successfully removed decorations earlier
if self.root.overrideredirect():
# Try to set some window manager hints for better integration
try:
self.root.attributes('-type', 'dialog')
self.root.attributes('-topmost', False)
# Try to enable window compositing for better visuals
self.root.tk.call('tkwait', 'visibility', self.root._w)
except:
pass
else:
# If we have window decorations, try to make them minimal
try:
self.root.attributes('-toolpalette', True)
except:
pass
except Exception:
pass
elif IS_MACOS:
# On macOS, ensure proper window behavior and appearance
try:
# macOS-specific window attributes for better integration
self.root.attributes('-alpha', 1.0) # Ensure full opacity
self.root.attributes('-topmost', False)
# Try to disable window animations for smoother experience
try:
self.root.tk.call('tk::mac::standardAboutPanel')
except:
pass
except Exception:
pass
else:
# For other platforms, just use basic borderless mode
pass
def minimize_window(self):
"""Minimize the window by hiding it and showing a restore notification"""
try:
# Simple approach: hide the window and show a notification
self.root.withdraw()
# Schedule the restore notification to run after the window is hidden
self.root.after(200, self._show_minimize_notification)
except Exception:
pass
# Fallback: try to at least hide the window
try:
self.root.withdraw()
except:
pass
def _show_minimize_notification(self):
"""Show a notification that the app is minimized"""
try:
# Create a small restore dialog
self.restore_dialog = tk.Toplevel()
self.restore_dialog.title("")
self.restore_dialog.geometry("300x120")
self.restore_dialog.resizable(False, False)
# Center the restore dialog
self.restore_dialog.update_idletasks()
x = (self.restore_dialog.winfo_screenwidth() // 2) - (150)
y = (self.restore_dialog.winfo_screenheight() // 2) - (60)
self.restore_dialog.geometry(f'+{x}+{y}')
# Style the restore dialog
self.restore_dialog.configure(bg=self.bg_color)
# Add content
title_label = tk.Label(self.restore_dialog, text="Application Minimized",
bg=self.bg_color, fg=self.fg_color,
font=font_manager.get_font(12, 'bold'))
title_label.pack(pady=(15, 5))
info_label = tk.Label(self.restore_dialog, text="Claude Code EZ Switch is running in the background",
bg=self.bg_color, fg=self.fg_color,
font=font_manager.get_font(9))
info_label.pack(pady=(0, 10))
restore_button = tk.Button(self.restore_dialog, text="Restore Window",
bg=self.button_bg, fg=self.fg_color,
font=font_manager.get_font(10, 'bold'),
relief=tk.FLAT, bd=0, pady=8,
command=self._restore_from_notification,
cursor="hand2")
restore_button.pack(pady=(0, 15))
# Bind hover effects
restore_button.bind('<Enter>', lambda e: restore_button.configure(bg=self.button_hover))
restore_button.bind('<Leave>', lambda e: restore_button.configure(bg=self.button_bg))
# Make the dialog stay on top
self.restore_dialog.attributes('-topmost', True)
# Handle window close
self.restore_dialog.protocol("WM_DELETE_WINDOW", self._restore_from_notification)
except Exception:
pass
# If dialog creation fails, just restore the main window
self._restore_main_window()
def _restore_from_notification(self):
"""Restore the main window from the notification"""
try:
# Close the restore dialog if it exists
if hasattr(self, 'restore_dialog') and self.restore_dialog:
self.restore_dialog.destroy()
self.restore_dialog = None
# Restore the main window
self._restore_main_window()
except Exception:
pass
self._restore_main_window()
def _restore_main_window(self):
"""Restore the main window to its original state"""
try:
self.root.deiconify()
self.root.lift()
self.root.focus_force()
except Exception:
pass
def close_window(self):
"""Close the window"""
self.close_application()
def create_widgets(self):
# Main container with slightly more padding since we don't have custom title bar
main_frame = tk.Frame(self.root, bg=self.bg_color, padx=30, pady=20)
main_frame.pack(fill=tk.BOTH, expand=True)
# Content frame (everything except footer)
content_frame = tk.Frame(main_frame, bg=self.bg_color)
content_frame.pack(fill=tk.BOTH, expand=True)
# Message area (hidden by default)
self.message_frame = tk.Frame(content_frame, bg=self.entry_bg, relief=tk.RAISED, bd=1)
self.message_label = tk.Label(self.message_frame, bg=self.entry_bg, fg=self.fg_color,
font=font_manager.get_font(10), wraplength=400, justify=tk.LEFT)
self.message_label.pack(padx=15, pady=10)
# Close button for message
self.message_close_btn = tk.Button(self.message_frame, text="✕", bg=self.close_button_bg, fg=self.fg_color,
font=font_manager.get_font(8), relief=tk.FLAT, cursor="hand2",
bd=0, width=2, height=1, command=self.hide_message)
self.message_close_btn.place(relx=0.98, rely=0.02, anchor='ne')
# Modern Logo Container
logo_container = tk.Frame(content_frame, bg=self.bg_color)
logo_container.pack(pady=(0, 15))
# Logo frame with styling
logo_frame = tk.Frame(logo_container, bg=self.bg_color)
logo_frame.pack()
# Orange code symbol
code_symbol = tk.Label(logo_frame, text="</>", bg=self.bg_color, fg="#FF8C00",
font=font_manager.get_font(28, 'bold'))
code_symbol.pack(side=tk.LEFT, padx=(0, 8))
claude_code_label = tk.Label(logo_frame, text="Claude Code", bg=self.bg_color, fg="#FF8C00",
font=font_manager.get_font(28, 'bold'))
claude_code_label.pack(side=tk.LEFT)
ez_switch_label = tk.Label(logo_frame, text=" EZ Switch", bg=self.bg_color, fg=self.fg_color,
font=font_manager.get_font(28, 'bold'))
ez_switch_label.pack(side=tk.LEFT)
# Modern separation line
separator_frame = tk.Frame(logo_container, bg=self.bg_color)
separator_frame.pack(pady=(8, 5))
# Create gradient effect with multiple lines
separator_top = tk.Frame(separator_frame, bg="#FF8C00", height=2)
separator_top.pack(fill=tk.X, padx=50)
separator_middle = tk.Frame(separator_frame, bg="#FFA500", height=1)
separator_middle.pack(fill=tk.X, padx=70, pady=1)
separator_bottom = tk.Frame(separator_frame, bg="#FFB84D", height=1)
separator_bottom.pack(fill=tk.X, padx=90)
# Subtitle with modern styling
subtitle_label = ttk.Label(logo_container,
text="Switch between z.ai and custom configurations",
style='Subtitle.TLabel')
subtitle_label.pack(pady=(5, 0))
# Current Status Frame
status_frame = tk.Frame(content_frame, bg=self.entry_bg, relief=tk.FLAT, bd=0)
status_frame.pack(fill=tk.X, pady=(0, 20), padx=2)
status_title = ttk.Label(status_frame, text="Current Status:",
font=font_manager.get_font(13, 'bold'))
status_title.pack(anchor=tk.W, padx=15, pady=(15, 10))
self.status_label = tk.Label(status_frame, text="Checking...",
bg=self.entry_bg, fg=self.fg_color,
font=font_manager.get_font(12, 'bold'), anchor=tk.W, justify=tk.LEFT)
self.status_label.pack(anchor=tk.W, padx=15, pady=(0, 15), fill=tk.X)
# Loading indicator (hidden by default)
self.loading_frame = tk.Frame(status_frame, bg=self.entry_bg)
self.loading_label = tk.Label(self.loading_frame, text="⟳ Applying configuration...",
bg=self.entry_bg, fg=self.accent_color,
font=font_manager.get_font(12, 'bold'), anchor=tk.W)
self.loading_label.pack(side=tk.LEFT, padx=15)
self.progress_bar = ttk.Progressbar(self.loading_frame, mode='indeterminate', length=200)
self.progress_bar.pack(side=tk.LEFT, padx=10)
# Configuration Selection
config_label = ttk.Label(content_frame, text="Select Configuration:",
font=font_manager.get_font(11, 'bold'))
config_label.pack(anchor=tk.W, pady=(0, 10))
self.config_var = tk.StringVar(value="zai")
# Configuration radio buttons container
radio_container = tk.Frame(content_frame, bg=self.bg_color)
radio_container.pack(fill=tk.X, pady=(0, 10))
if IS_LINUX:
# Create custom radio buttons for Linux with larger indicators
self.create_custom_radio_button(radio_container, "Z.ai", "zai")
self.create_custom_radio_button(radio_container, "Claude", "claude")
self.create_custom_radio_button(radio_container, "Custom", "custom")
else:
# Use standard ttk radio buttons for other platforms
# Create radio buttons horizontally
zai_radio = ttk.Radiobutton(radio_container, text="Z.ai",
variable=self.config_var, value="zai",
command=self.on_config_change, style='TRadiobutton')
zai_radio.pack(side=tk.LEFT, padx=(0, 20))
claude_radio = ttk.Radiobutton(radio_container, text="Claude",
variable=self.config_var, value="claude",
command=self.on_config_change, style='TRadiobutton')
claude_radio.pack(side=tk.LEFT, padx=(0, 20))
custom_radio = ttk.Radiobutton(radio_container, text="Custom",
variable=self.config_var, value="custom",
command=self.on_config_change, style='TRadiobutton')
custom_radio.pack(side=tk.LEFT)
# Dynamic configuration container (where different configs will be shown)
self.dynamic_config_container = tk.Frame(content_frame, bg=self.bg_color)
self.dynamic_config_container.pack(fill=tk.BOTH, expand=False)
# Create all configuration frames but don't pack them yet
self.create_zai_frame()
self.create_claude_frame()
self.create_custom_frame()
# Show/Hide Password Checkbutton
self.show_password_var = tk.BooleanVar()
show_password_check = tk.Checkbutton(content_frame, text="Show API Keys",
variable=self.show_password_var,
command=self.toggle_password_visibility,
bg=self.bg_color, fg=self.fg_color,
selectcolor=self.bg_color,
activebackground=self.bg_color,
activeforeground=self.fg_color,
highlightthickness=0, bd=0,
font=font_manager.get_font(9 if not IS_LINUX else 11),
padx=5, pady=5)
show_password_check.pack(anchor=tk.W, pady=(10, 0))
# Show Claude Settings Checkbutton
self.show_env_vars_var = tk.BooleanVar()
show_env_vars_check = tk.Checkbutton(content_frame, text="Show Claude Settings",
variable=self.show_env_vars_var,
command=self.toggle_env_vars_visibility,
bg=self.bg_color, fg=self.fg_color,
selectcolor=self.bg_color,
activebackground=self.bg_color,
activeforeground=self.fg_color,
highlightthickness=0, bd=0,
font=font_manager.get_font(9 if not IS_LINUX else 11),
padx=5, pady=5)
show_env_vars_check.pack(anchor=tk.W, pady=(5, 0))
# Buttons Frame using Grid for better layout
button_container = tk.Frame(content_frame, bg=self.bg_color)
button_container.pack(fill=tk.X, pady=(20, 0))
# Configure grid weights for responsive layout
button_container.grid_columnconfigure(0, weight=2)
button_container.grid_columnconfigure(1, weight=1)
button_container.grid_columnconfigure(2, weight=1)
# Apply Configuration Button (spans 2 columns)
self.apply_button = tk.Button(button_container, text="Apply Configuration",
bg=self.button_bg, fg=self.fg_color,
activebackground=self.button_bg, activeforeground=self.fg_color,
font=font_manager.get_font(11, 'bold'), relief=tk.FLAT,
cursor="hand2", bd=0, pady=12,
command=self.apply_configuration)
self.apply_button.grid(row=0, column=0, columnspan=2, sticky="ew", padx=(0, 5), pady=(0, 8))
# Hover effects removed
# Refresh Status Button
self.refresh_button = tk.Button(button_container, text="Refresh",
bg=self.refresh_button_bg, fg=self.fg_color,
activebackground=self.refresh_button_bg, activeforeground=self.fg_color,
font=font_manager.get_font(10, 'bold'), relief=tk.FLAT,
cursor="hand2", bd=0, pady=12,
command=self.check_current_status)
self.refresh_button.grid(row=0, column=2, sticky="ew", padx=(5, 0), pady=(0, 8))
# Hover effects removed
# Close Button (spans all columns)
self.close_button = tk.Button(button_container, text="Close Application",
bg=self.close_button_bg, fg=self.fg_color,
activebackground=self.close_button_bg, activeforeground=self.fg_color,
font=font_manager.get_font(10), relief=tk.FLAT,
cursor="hand2", bd=0, pady=10,
command=self.close_application)
self.close_button.grid(row=1, column=0, columnspan=3, sticky="ew")
# Hover effects removed
# Universal Claude Settings Display (hidden by default)
self.universal_settings_frame = tk.Frame(main_frame, bg=self.entry_bg)
# Hide button for universal settings
universal_hide_frame = tk.Frame(self.universal_settings_frame, bg=self.entry_bg)
universal_hide_frame.pack(fill=tk.X, padx=15, pady=(10, 5))
universal_hide_btn = tk.Button(universal_hide_frame, text="✕ Hide Settings",
bg=self.close_button_bg, fg=self.fg_color,
font=font_manager.get_font(9, 'bold'), relief=tk.FLAT,
cursor="hand2", bd=0, pady=5,
command=self.hide_universal_settings)
universal_hide_btn.pack(side=tk.RIGHT)
# Create grid layout for settings display
universal_settings_container = tk.Frame(self.universal_settings_frame, bg=self.entry_bg)
universal_settings_container.pack(fill=tk.X, padx=15, pady=(0, 10))
# Settings title
settings_title = tk.Label(universal_settings_container, text="Current Claude Code Settings:",
bg=self.entry_bg, fg=self.accent_color,
font=font_manager.get_font(10, 'bold'), anchor=tk.W)
settings_title.grid(row=0, column=0, columnspan=2, sticky=tk.W, pady=(0, 8))
# Define all possible ANTHROPIC variables to display
universal_env_vars = [
('ANTHROPIC_AUTH_TOKEN', 'Authentication Token'),
('ANTHROPIC_BASE_URL', 'Base URL'),
('ANTHROPIC_DEFAULT_OPUS_MODEL', 'Default Opus Model'),
('ANTHROPIC_DEFAULT_SONNET_MODEL', 'Default Sonnet Model'),
('ANTHROPIC_DEFAULT_HAIKU_MODEL', 'Default Haiku Model'),
('ANTHROPIC_API_KEY', 'API Key'),
('API_TIMEOUT_MS', 'API Timeout (ms)')
]
# Create labels for each variable
self.universal_value_labels = {}
for i, (var_name, display_name) in enumerate(universal_env_vars, start=1):
# Variable name label
var_label = tk.Label(universal_settings_container, text=f"{display_name}:",
bg=self.entry_bg, fg=self.fg_color,
font=font_manager.get_font(9), anchor=tk.W)
var_label.grid(row=i, column=0, sticky=tk.W, pady=(2, 2))
# Value label
value_label = tk.Label(universal_settings_container, text="Not set",
bg=self.entry_bg, fg="#888888",
font=font_manager.get_font(9, 'italic'), anchor=tk.W)
value_label.grid(row=i, column=1, sticky=tk.W, padx=(10, 0), pady=(2, 2))
self.universal_value_labels[var_name] = value_label
# Note at the bottom
note_label = tk.Label(universal_settings_container,
text="Note: These settings are stored in ~/.claude/settings.json",
bg=self.entry_bg, fg="#888888",
font=font_manager.get_font(8, "italic"), anchor=tk.W)
note_label.grid(row=len(universal_env_vars) + 1, column=0, columnspan=2, sticky=tk.W, pady=(8, 0))
# Footer
footer_frame = tk.Frame(main_frame, bg=self.bg_color)
footer_frame.pack(fill=tk.X, side=tk.BOTTOM, pady=(20, 10))
footer_text = "Claude Code EZ Switch is open source under the MIT license"
footer_label = tk.Label(footer_frame, text=footer_text,
bg=self.bg_color, fg="#888888",
font=font_manager.get_font(8))
footer_label.pack()
# GitHub link
github_link = tk.Label(footer_frame, text="Source Code",
bg=self.bg_color, fg=self.accent_color,
font=font_manager.get_font(8), cursor="hand2")
github_link.pack()
github_link.bind("<Button-1>", lambda e: self.open_github_link())
def create_custom_radio_button(self, parent, text, value):
"""Create a custom radio button with larger indicator for Linux"""
# Create frame for the radio button
radio_frame = tk.Frame(parent, bg=self.bg_color)
radio_frame.pack(side=tk.LEFT, padx=(0, 20))
# Create canvas for drawing the radio button circle
canvas_size = 24 # Larger than default
canvas = tk.Canvas(radio_frame, width=canvas_size, height=canvas_size,
bg=self.bg_color, highlightthickness=0, bd=0)
canvas.pack(side=tk.LEFT, padx=(0, 10))
# Draw the radio button circle (outer ring)
outer_circle = canvas.create_oval(2, 2, canvas_size-2, canvas_size-2,
outline=self.fg_color, width=3)
# Draw the inner circle (filled when selected)
inner_circle = canvas.create_oval(8, 8, canvas_size-8, canvas_size-8,
fill="", outline="")
# Create the text label
label = tk.Label(radio_frame, text=text, bg=self.bg_color, fg=self.fg_color,
font=font_manager.get_font(11), # Smaller font size for radio button text
bd=0, relief=tk.FLAT, highlightthickness=0)
# Force background color to override any theme defaults
label.configure(bg=self.bg_color)
label.pack(side=tk.LEFT, padx=0, pady=5)
# Determine which variable and callback to use
if value in ['subscription', 'api']:
# Claude mode radio buttons
var_name = 'claude_mode_var'
callback = self.on_claude_mode_change
else:
# Config radio buttons (zai, claude, custom)
var_name = 'config_var'
callback = self.on_config_change
# Store radio button info
self.custom_radio_buttons[value] = {
'canvas': canvas,
'outer_circle': outer_circle,
'inner_circle': inner_circle,
'label': label,
'selected': False,
'var_name': var_name,
'callback': callback
}
# Bind click events
for widget in [canvas, label, radio_frame]:
widget.bind('<Button-1>', lambda e, val=value: self.on_custom_radio_click(val))
widget.bind('<Enter>', lambda e, val=value: self.on_custom_radio_hover(val, True))
widget.bind('<Leave>', lambda e, val=value: self.on_custom_radio_hover(val, False))
# Set initial selection if this is the default value
variable = getattr(self, var_name)
if variable.get() == value:
self.update_custom_radio_display(value, True)
def on_custom_radio_click(self, value):
"""Handle custom radio button click"""
if value not in self.custom_radio_buttons:
return
radio_info = self.custom_radio_buttons[value]
var_name = radio_info['var_name']
callback = radio_info['callback']
# Update the appropriate variable
variable = getattr(self, var_name)
variable.set(value)
# Update all radio button displays
for radio_value in self.custom_radio_buttons:
self.update_custom_radio_display(radio_value, radio_value == value)
# Trigger the appropriate callback
callback()
def update_custom_radio_display(self, value, is_selected):
"""Update the visual display of a custom radio button"""
if value not in self.custom_radio_buttons:
return
radio_info = self.custom_radio_buttons[value]
radio_info['selected'] = is_selected
canvas = radio_info['canvas']
inner_circle = radio_info['inner_circle']
if is_selected:
# Fill the inner circle and change text color
# Use lighter orange for all radio buttons
color = self.radio_orange_color
canvas.itemconfig(inner_circle, fill=color)
canvas.itemconfig(radio_info['outer_circle'], outline=color)
radio_info['label'].config(fg=color)
else:
# Clear the inner circle and reset text color
canvas.itemconfig(inner_circle, fill="")
canvas.itemconfig(radio_info['outer_circle'], outline=self.fg_color)
radio_info['label'].config(fg=self.fg_color)
def on_custom_radio_hover(self, value, is_hovering):
"""Handle hover effect for custom radio buttons"""
if value not in self.custom_radio_buttons:
return
radio_info = self.custom_radio_buttons[value]
canvas = radio_info['canvas']
if not radio_info['selected']: # Only apply hover if not selected
if is_hovering:
# Use lighter orange for all radio buttons
color = self.radio_orange_color
canvas.itemconfig(radio_info['outer_circle'], outline=color)
else:
canvas.itemconfig(radio_info['outer_circle'], outline=self.fg_color)
def on_entry_click(self, event):
"""Handle entry field click to ensure proper focus on Linux"""
widget = event.widget
# Ensure the widget can receive focus and input
widget.focus_set()
widget.focus_force()
# Make sure the widget is enabled and can receive input
if widget.cget('state') == tk.DISABLED:
widget.configure(state=tk.NORMAL)
# Ensure the cursor is visible and positioned correctly
widget.icursor(tk.END)
def setup_linux_focus(self):
"""Set up proper focus handling for Linux"""
try:
# Enhanced focus setup for Linux to ensure input fields work
if hasattr(self, 'zai_key_entry'):
# Set up all entry fields for proper focus
entry_fields = [
self.zai_key_entry,
self.zai_key_name_entry,
self.claude_key_entry,
self.custom_url_entry,
self.custom_key_entry
]
for entry in entry_fields:
if entry:
# Ensure entry is enabled and can receive focus
if entry.cget('state') == tk.DISABLED:
entry.configure(state=tk.NORMAL)
# Set focus and ensure cursor is at end
entry.focus_set()
entry.icursor(tk.END)
# Bind additional events to ensure focus works
entry.bind('<FocusIn>', lambda e, widget=entry: widget.icursor(tk.END))
entry.bind('<Button-1>', lambda e, widget=entry: self._ensure_entry_focus(widget))
# Set initial focus to zai_key_entry after a short delay
self.root.after(200, lambda: self._ensure_entry_focus(self.zai_key_entry))
except Exception:
pass
def _ensure_entry_focus(self, entry):
"""Helper method to ensure an entry field has proper focus"""
try:
if entry and entry.winfo_exists():
# Make sure entry is enabled
if entry.cget('state') == tk.DISABLED:
entry.configure(state=tk.NORMAL)
# Set focus and move cursor to end
entry.focus_set()
entry.focus_force()
entry.icursor(tk.END)
# Make sure the window is active
self.root.lift()
self.root.focus_force()
except Exception:
pass
def create_zai_frame(self):
"""Create Z.ai configuration frame"""
self.zai_frame = tk.LabelFrame(self.dynamic_config_container, text="", bg=self.entry_bg,
fg=self.fg_color, relief=tk.FLAT, bd=2)
# Key selection section
key_select_label = ttk.Label(self.zai_frame, text="Select Saved Z.ai Key:")
key_select_label.pack(anchor=tk.W, padx=15, pady=(10, 2))
# Frame for key selection and management
key_management_frame = tk.Frame(self.zai_frame, bg=self.entry_bg)
key_management_frame.pack(fill=tk.X, padx=15, pady=(0, 5))
# Combobox for selecting existing keys
self.zai_key_var = tk.StringVar()
if IS_LINUX:
combobox_width = 32 # Make it larger on Linux for better visibility
else:
combobox_width = 40
self.zai_key_combo = ttk.Combobox(key_management_frame, textvariable=self.zai_key_var,
state="readonly", width=combobox_width)
self.zai_key_combo.pack(side=tk.LEFT, fill=tk.X, expand=True, padx=(0, 5))
self.zai_key_combo.bind('<<ComboboxSelected>>', self.on_zai_key_selected)
# Add/Delete buttons
button_frame = tk.Frame(key_management_frame, bg=self.entry_bg)
button_frame.pack(side=tk.RIGHT)
add_key_btn = tk.Button(button_frame, text="+", bg=self.refresh_button_bg, fg=self.fg_color,
activebackground=self.refresh_button_bg, activeforeground=self.fg_color,
font=font_manager.get_font(10, 'bold'), relief=tk.FLAT, cursor="hand2",
bd=0, width=3, command=self.add_zai_key)
add_key_btn.pack(side=tk.LEFT, padx=(0, 2))
delete_key_btn = tk.Button(button_frame, text="−", bg="#8b4513", fg=self.fg_color,
activebackground="#8b4513", activeforeground=self.fg_color,
font=font_manager.get_font(10, 'bold'), relief=tk.FLAT, cursor="hand2",
bd=0, width=3, command=self.delete_zai_key)
delete_key_btn.pack(side=tk.LEFT)
# Current key entry
zai_key_label = ttk.Label(self.zai_frame, text="Current Z.ai API Key:")
zai_key_label.pack(anchor=tk.W, padx=15, pady=(10, 2))
self.zai_key_entry = tk.Entry(self.zai_frame, bg=self.entry_bg, fg=self.fg_color,
insertbackground=self.fg_color, relief=tk.FLAT,
font=font_manager.get_font(10), bd=0, show="*")
self.zai_key_entry.pack(fill=tk.X, padx=15, pady=(0, 2), ipady=8)
# Bind focus events for proper input handling
self.zai_key_entry.bind('<Button-1>', self.on_entry_click)
# Add border to entry
entry_border = tk.Frame(self.zai_frame, bg=self.accent_color, height=2)
entry_border.pack(fill=tk.X, padx=15, pady=(0, 5))
# Key name entry
key_name_label = ttk.Label(self.zai_frame, text="Key Name (for saving):")
key_name_label.pack(anchor=tk.W, padx=15, pady=(5, 2))
self.zai_key_name_entry = tk.Entry(self.zai_frame, bg=self.entry_bg, fg=self.fg_color,
insertbackground=self.fg_color, relief=tk.FLAT,
font=font_manager.get_font(10), bd=0)
self.zai_key_name_entry.pack(fill=tk.X, padx=15, pady=(0, 2), ipady=8)
# Bind focus events for proper input handling
self.zai_key_name_entry.bind('<Button-1>', self.on_entry_click)
# Add border to key name entry
name_border = tk.Frame(self.zai_frame, bg=self.accent_color, height=2)
name_border.pack(fill=tk.X, padx=15, pady=(0, 10))
# Save current key button
save_key_btn = tk.Button(self.zai_frame, text="Save Current Key",
bg=self.refresh_button_bg, fg=self.fg_color,
activebackground=self.refresh_button_bg, activeforeground=self.fg_color,
font=font_manager.get_font(9, 'bold'), relief=tk.FLAT,
cursor="hand2", bd=0, pady=8,
command=self.save_current_zai_key)
save_key_btn.pack(fill=tk.X, padx=15, pady=(0, 10))
# Model selection section
model_selection_label = ttk.Label(self.zai_frame, text="Model Selection:")
model_selection_label.pack(anchor=tk.W, padx=15, pady=(10, 5))
# Create a frame for model selection dropdowns
model_frame = tk.Frame(self.zai_frame, bg=self.entry_bg)
model_frame.pack(fill=tk.X, padx=15, pady=(0, 10))
# Define available GLM models
self.available_models = ["GLM-4.6", "GLM-4.5", "GLM-4.5-Air"]
# Opus Model Selection
opus_label = ttk.Label(model_frame, text="Opus Model:")
opus_label.grid(row=0, column=0, sticky=tk.W, pady=(2, 5))
self.zai_opus_model_var = tk.StringVar(value="GLM-4.6")
self.zai_opus_combo = ttk.Combobox(model_frame, textvariable=self.zai_opus_model_var,
values=self.available_models, state="readonly", width=20)
self.zai_opus_combo.grid(row=0, column=1, sticky=tk.W, padx=(10, 0), pady=(2, 5))
# Sonnet Model Selection
sonnet_label = ttk.Label(model_frame, text="Sonnet Model:")
sonnet_label.grid(row=1, column=0, sticky=tk.W, pady=(2, 5))
self.zai_sonnet_model_var = tk.StringVar(value="GLM-4.6")
self.zai_sonnet_combo = ttk.Combobox(model_frame, textvariable=self.zai_sonnet_model_var,
values=self.available_models, state="readonly", width=20)
self.zai_sonnet_combo.grid(row=1, column=1, sticky=tk.W, padx=(10, 0), pady=(2, 5))
# Haiku Model Selection
haiku_label = ttk.Label(model_frame, text="Haiku Model:")
haiku_label.grid(row=2, column=0, sticky=tk.W, pady=(2, 5))
self.zai_haiku_model_var = tk.StringVar(value="GLM-4.6")
self.zai_haiku_combo = ttk.Combobox(model_frame, textvariable=self.zai_haiku_model_var,
values=self.available_models, state="readonly", width=20)
self.zai_haiku_combo.grid(row=2, column=1, sticky=tk.W, padx=(10, 0), pady=(2, 5))
# Bind combobox changes to update function
self.zai_opus_combo.bind('<<ComboboxSelected>>', lambda e: self.update_zai_env_display())
self.zai_sonnet_combo.bind('<<ComboboxSelected>>', lambda e: self.update_zai_env_display())
self.zai_haiku_combo.bind('<<ComboboxSelected>>', lambda e: self.update_zai_env_display())
# Initialize storage for multiple keys
self.zai_keys = {} # Format: {"name": "key"}
self.current_zai_key_name = None
# Environment variables display (hidden by default)
self.zai_env_frame = tk.Frame(self.zai_frame, bg=self.entry_bg)
# Hide button for z.ai environment variables
zai_hide_frame = tk.Frame(self.zai_env_frame, bg=self.entry_bg)
zai_hide_frame.pack(fill=tk.X, padx=15, pady=(10, 5))
zai_hide_btn = tk.Button(zai_hide_frame, text="✕ Hide Settings",
bg=self.close_button_bg, fg=self.fg_color,
font=font_manager.get_font(9, 'bold'), relief=tk.FLAT,
cursor="hand2", bd=0, pady=5,
command=lambda: self.hide_env_vars('zai'))
zai_hide_btn.pack(side=tk.RIGHT)
zai_hide_btn.bind('<Enter>', lambda e: zai_hide_btn.configure(bg=self.close_button_hover))
zai_hide_btn.bind('<Leave>', lambda e: zai_hide_btn.configure(bg=self.close_button_bg))
# Create a nice grid layout for environment variables
zai_env_container = tk.Frame(self.zai_env_frame, bg=self.entry_bg)
zai_env_container.pack(fill=tk.X, padx=15, pady=(0, 10))
# Environment variables title
env_title = tk.Label(zai_env_container, text="Environment Variables Set:",
bg=self.bg_color, fg="#cccccc",
font=font_manager.get_font(11, 'bold'), anchor=tk.W)
env_title.grid(row=0, column=0, columnspan=2, sticky=tk.W, pady=(0, 8))
# Environment variables data
zai_env_vars = [
("ANTHROPIC_AUTH_TOKEN", "<your-zai-api-key>", "User level"),
("ANTHROPIC_BASE_URL", "https://api.z.ai/api/anthropic", "User level"),