-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwx_explorer.py
More file actions
1467 lines (1221 loc) · 57.1 KB
/
wx_explorer.py
File metadata and controls
1467 lines (1221 loc) · 57.1 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
# -*- coding: utf-8 -*-
import wx
import wx.adv
import os
import send2trash
import win32api
import win32con
import win32gui
import win32com.client
import win32com.shell.shell as shell
import win32com.shell.shellcon as shellcon
import time
import shutil
from collections import deque
from datetime import datetime
import pythoncom
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
# 版本信息
VERSION = "0.2"
APP_NAME = "多标签文件浏览器"
pythoncom.CoInitialize() # 添加在模块初始化处
class FileChangeHandler(FileSystemEventHandler):
def __init__(self, callback):
super().__init__()
self.callback = callback
def on_created(self, event):
wx.CallAfter(self.callback, "创建: " + event.src_path)
def on_deleted(self, event):
wx.CallAfter(self.callback, "删除: " + event.src_path)
def on_modified(self, event):
if not event.is_directory:
wx.CallAfter(self.callback, "修改: " + event.src_path)
def on_moved(self, event):
wx.CallAfter(self.callback, f"移动: {event.src_path} -> {event.dest_path}")
class FileExplorerFrame(wx.Frame):
def __init__(self):
super().__init__(None, title=f"{APP_NAME} v{VERSION}", size=(1024, 768))
# 初始化基本变量
self.current_path = os.path.expanduser("~")
self.history = deque(maxlen=10)
self.clipboard = {"type": None, "paths": []}
self.observer = Observer()
self.closed_tabs = {"left": deque(maxlen=10), "right": deque(maxlen=10)}
self._icon_cache = {}
self.splitter_ratio = 0.5 # 保存分割比例
# 设置窗口样式
self.SetBackgroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOW))
# 创建主面板
self.main_panel = wx.Panel(self)
# 创建分割窗口
self.splitter = wx.SplitterWindow(self.main_panel, style=wx.SP_3D | wx.SP_LIVE_UPDATE | wx.SP_PERMIT_UNSPLIT)
# 创建左右两个标签页面板
self.left_notebook = wx.Notebook(self.splitter)
self.right_notebook = wx.Notebook(self.splitter)
self.tabs = {"left": [], "right": []}
# 设置分割窗口
self.splitter.SplitVertically(self.left_notebook, self.right_notebook)
# 绑定分割窗口事件
self.splitter.Bind(wx.EVT_SPLITTER_SASH_POS_CHANGED, self.on_splitter_changed)
self.splitter.Bind(wx.EVT_SPLITTER_SASH_POS_CHANGING, self.on_splitter_changing)
# 主布局
main_sizer = wx.BoxSizer(wx.VERTICAL)
main_sizer.Add(self.splitter, 1, wx.EXPAND)
self.main_panel.SetSizer(main_sizer)
# 加载系统图标
self.load_system_icons()
# 创建状态栏
self.status_bar = self.CreateStatusBar(1)
# 初始化菜单
self.init_menu()
# 绑定事件
self.left_notebook.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, lambda evt: self.on_tab_switch(evt, "left"))
self.right_notebook.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, lambda evt: self.on_tab_switch(evt, "right"))
self.left_notebook.Bind(wx.EVT_LEFT_DCLICK, lambda evt: self.on_notebook_dclick(evt, "left"))
self.right_notebook.Bind(wx.EVT_LEFT_DCLICK, lambda evt: self.on_notebook_dclick(evt, "right"))
self.Bind(wx.EVT_SIZE, self.on_size)
self.Bind(wx.EVT_CLOSE, self.OnClose)
# 初始化标签页
self.init_notebooks()
# 调整大小和显示
self.main_panel.Layout()
self.Centre()
# 设置初始分割位置为窗口宽度的一半
wx.CallAfter(self.init_splitter_position)
self.Show()
# 开始监控文件系统变化
self.start_watching(self.current_path)
def init_splitter_position(self):
"""初始化分割窗口位置"""
width = self.GetClientSize().GetWidth()
self.splitter.SetSashPosition(int(width * self.splitter_ratio))
self.splitter.SetMinimumPaneSize(200)
self.splitter.SetSashGravity(0.5)
def init_theme_menu(self):
"""初始化主题菜单"""
menubar = wx.MenuBar()
view_menu = wx.Menu()
self.theme_items = {
'light': view_menu.AppendRadioItem(wx.ID_ANY, "浅色主题"),
'dark': view_menu.AppendRadioItem(wx.ID_ANY, "深色主题"),
'system': view_menu.AppendRadioItem(wx.ID_ANY, "系统默认")
}
menubar.Append(view_menu, "&视图")
self.SetMenuBar(menubar)
# 绑定主题切换事件
for item in self.theme_items.values():
self.Bind(wx.EVT_MENU, self.on_change_theme, item)
def get_selected_paths(self):
"""获取选中的文件路径列表"""
selected_paths = []
current_tab = self.get_current_tab()
if not current_tab:
return selected_paths
list_ctrl = current_tab['list']
item = -1
while True:
item = list_ctrl.GetNextItem(item, wx.LIST_NEXT_ALL, wx.LIST_STATE_SELECTED)
if item == -1:
break
name = list_ctrl.GetItem(item, 1).GetText()
path = os.path.join(current_tab['path'], name)
selected_paths.append(path)
return selected_paths
def on_tab_switch(self, event, side):
"""切换标签页时更新监控路径"""
notebook = self.left_notebook if side == "left" else self.right_notebook
index = event.GetSelection()
# 如果切换到"+"标签页,则创建新标签页并选中它
if notebook.GetPageText(index) == "+":
# 获取当前活动标签页的路径
current_tab = self.get_current_tab(side)
path = current_tab['path'] if current_tab else os.path.expanduser("~")
self.add_tab(path, side)
# 确保"+"标签页保持不选中状态
notebook.SetSelection(notebook.GetPageCount() - 2)
event.Veto() # 阻止切换到"+"标签页
return
current = self.get_current_tab(side)
if current:
self.start_watching(current['path'])
event.Skip()
def navigate_to(self, path, side=None):
"""导航到指定路径"""
try:
# 规范化路径
path = os.path.normpath(path)
# 检查路径是否存在且可访问
if not os.path.exists(path):
wx.MessageBox(f"路径不存在: {path}", "错误", wx.OK | wx.ICON_ERROR)
return
if not os.access(path, os.R_OK):
wx.MessageBox(f"无法访问路径: {path}", "错误", wx.OK | wx.ICON_ERROR)
return
# 获取当前标签页
if side is None:
current_tab = self.get_current_tab()
else:
current_tab = self.get_current_tab(side)
if not current_tab:
return
# 如果是相同路径,直接刷新
if current_tab['path'] == path:
self.refresh_file_list(current_tab)
return
# 更新路径
current_tab['path'] = path
current_tab['history'].append(path)
# 更新标签页标题
notebook = self.left_notebook if current_tab in self.tabs['left'] else self.right_notebook
index = notebook.GetSelection()
if index != -1:
title = os.path.basename(path) or path
notebook.SetPageText(index, title)
# 更新路径输入框
current_tab['path_ctrl'].SetValue(path)
# 刷新文件列表
self.refresh_file_list(current_tab)
# 更新监控
self.start_watching(path)
except Exception as e:
wx.LogError(f"导航失败: {str(e)}")
def OnClose(self, event):
"""窗口关闭时清理资源"""
if self.observer and self.observer.is_alive():
self.observer.stop()
self.observer.join()
self.clear_icon_cache()
self.Destroy()
def init_notebooks(self):
"""初始化左右标签页"""
# 创建左侧默认标签页
self.add_tab(os.path.expanduser("~"), "left")
# 创建左侧"+"标签页
plus_panel = wx.Panel(self.left_notebook)
self.left_notebook.AddPage(plus_panel, "+", False)
# 创建右侧默认标签页
self.add_tab(os.path.expanduser("~"), "right")
# 创建右侧"+"标签页
plus_panel = wx.Panel(self.right_notebook)
self.right_notebook.AddPage(plus_panel, "+", False)
def add_tab(self, initial_path, side="left"):
"""创建新标签页"""
notebook = self.left_notebook if side == "left" else self.right_notebook
# 创建标签页面板
panel = wx.Panel(notebook)
sizer = wx.BoxSizer(wx.VERTICAL)
# 创建工具栏
toolbar = wx.ToolBar(panel)
# 添加工具栏按钮并设置提示
back_tool = toolbar.AddTool(wx.ID_BACKWARD, "后退",
wx.ArtProvider.GetBitmap(wx.ART_GO_BACK, size=(16, 16)))
forward_tool = toolbar.AddTool(wx.ID_FORWARD, "前进",
wx.ArtProvider.GetBitmap(wx.ART_GO_FORWARD, size=(16, 16)))
up_tool = toolbar.AddTool(wx.ID_UP, "上级",
wx.ArtProvider.GetBitmap(wx.ART_GO_UP, size=(16, 16)))
toolbar.AddSeparator()
new_folder_tool = toolbar.AddTool(wx.ID_NEW, "新建文件夹",
wx.ArtProvider.GetBitmap(wx.ART_NEW_DIR, size=(16, 16)))
refresh_tool = toolbar.AddTool(wx.ID_REFRESH, "刷新",
wx.ArtProvider.GetBitmap(wx.ART_REDO, size=(16, 16)))
# 设置工具栏按钮提示
toolbar.SetToolShortHelp(wx.ID_BACKWARD, "后退 (Alt+←)")
toolbar.SetToolShortHelp(wx.ID_FORWARD, "前进 (Alt+→)")
toolbar.SetToolShortHelp(wx.ID_UP, "上级目录 (Alt+↑)")
toolbar.SetToolShortHelp(wx.ID_NEW, "新建文件夹 (Ctrl+N)")
toolbar.SetToolShortHelp(wx.ID_REFRESH, "刷新 (F5)")
toolbar.Realize()
# 路径输入框
path_ctrl = wx.TextCtrl(panel, style=wx.TE_PROCESS_ENTER)
path_ctrl.SetValue(initial_path)
# 创建图标列表
icon_list = wx.ImageList(16, 16)
# 文件列表
file_list = wx.ListCtrl(panel, style=wx.LC_REPORT|wx.LC_SINGLE_SEL)
file_list.SetImageList(icon_list, wx.IMAGE_LIST_SMALL)
# 添加列
file_list.InsertColumn(0, "", width=30)
file_list.InsertColumn(1, "名称", width=200)
file_list.InsertColumn(2, "大小", width=100)
file_list.InsertColumn(3, "修改日期", width=150)
# 布局
sizer.Add(toolbar, 0, wx.EXPAND)
sizer.Add(path_ctrl, 0, wx.EXPAND|wx.ALL, 5)
sizer.Add(file_list, 1, wx.EXPAND|wx.ALL, 5)
panel.SetSizer(sizer)
# 绑定事件
path_ctrl.Bind(wx.EVT_TEXT_ENTER, self.on_path_enter)
file_list.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.on_item_activated)
file_list.Bind(wx.EVT_LIST_ITEM_SELECTED, self.on_item_selected)
file_list.Bind(wx.EVT_LIST_ITEM_RIGHT_CLICK, self.on_item_right_click)
file_list.Bind(wx.EVT_SIZE, lambda evt: self.adjust_list_columns(file_list))
toolbar.Bind(wx.EVT_TOOL, self.on_back, id=wx.ID_BACKWARD)
toolbar.Bind(wx.EVT_TOOL, self.on_forward, id=wx.ID_FORWARD)
toolbar.Bind(wx.EVT_TOOL, self.on_up, id=wx.ID_UP)
toolbar.Bind(wx.EVT_TOOL, self.new_folder, id=wx.ID_NEW)
toolbar.Bind(wx.EVT_TOOL, lambda evt: self.refresh_file_list(), id=wx.ID_REFRESH)
# 记录标签页状态
tab_data = {
"panel": panel,
"path": initial_path,
"path_ctrl": path_ctrl,
"list": file_list,
"icon_list": icon_list,
"history": deque([initial_path], maxlen=10)
}
# 如果是第一个标签页,直接添加
if not self.tabs[side]:
self.tabs[side].append(tab_data)
notebook.InsertPage(0, panel, os.path.basename(initial_path) or initial_path, True)
else:
# 在"+"标签页之前插入新标签页
self.tabs[side].append(tab_data)
notebook.InsertPage(notebook.GetPageCount() - 1, panel, os.path.basename(initial_path) or initial_path, True)
# 确保"+"标签页保持不选中状态
notebook.SetSelection(notebook.GetPageCount() - 2)
# 刷新文件列表
self.refresh_file_list(tab_data)
# 调整布局
panel.Layout()
def on_add_tab(self, event):
"""添加新标签页"""
default_path = os.path.expanduser("~")
self.add_tab(default_path)
def on_close_tab(self, event):
"""关闭当前标签页"""
current_tab = self.get_current_tab()
if not current_tab:
return
# 获取当前标签页所在的notebook和索引
if current_tab in self.tabs['left']:
notebook = self.left_notebook
side = 'left'
index = self.tabs['left'].index(current_tab)
else:
notebook = self.right_notebook
side = 'right'
index = self.tabs['right'].index(current_tab)
self.close_tab(index, side)
def get_current_tab(self, side=None):
"""获取当前活动标签页数据"""
if side is None:
# 获取当前焦点所在的标签页
focused = wx.Window.FindFocus()
if focused:
# 向上查找父窗口,直到找到标签页
parent = focused.GetParent()
while parent and parent != self.left_notebook and parent != self.right_notebook:
parent = parent.GetParent()
if parent == self.left_notebook:
side = "left"
elif parent == self.right_notebook:
side = "right"
else:
# 如果找不到,默认使用左侧标签页
side = "left"
else:
side = "left"
notebook = self.left_notebook if side == "left" else self.right_notebook
current = notebook.GetSelection()
if current != -1 and current < len(self.tabs[side]):
return self.tabs[side][current]
return None
def init_menu(self):
"""初始化菜单栏"""
menubar = wx.MenuBar()
# 文件菜单
file_menu = wx.Menu()
file_menu.Append(wx.ID_NEW, "新建文件夹\tCtrl+N")
file_menu.AppendSeparator()
file_menu.Append(wx.ID_CLOSE, "关闭标签页\tCtrl+W")
restore_tab_item = file_menu.Append(wx.ID_ANY, "恢复关闭的标签页\tCtrl+Shift+T")
file_menu.AppendSeparator()
file_menu.Append(wx.ID_EXIT, "退出\tAlt+F4")
menubar.Append(file_menu, "文件(&F)")
# 编辑菜单
edit_menu = wx.Menu()
edit_menu.Append(wx.ID_CUT, "剪切\tCtrl+X")
edit_menu.Append(wx.ID_COPY, "复制\tCtrl+C")
edit_menu.Append(wx.ID_PASTE, "粘贴\tCtrl+V")
edit_menu.AppendSeparator()
edit_menu.Append(wx.ID_DELETE, "删除\tDel")
menubar.Append(edit_menu, "编辑(&E)")
# 视图菜单
view_menu = wx.Menu()
view_menu.Append(wx.ID_REFRESH, "刷新\tF5")
# 主题子菜单
theme_menu = wx.Menu()
self.theme_items = {
'light': theme_menu.AppendRadioItem(wx.ID_ANY, "浅色主题"),
'dark': theme_menu.AppendRadioItem(wx.ID_ANY, "深色主题"),
'system': theme_menu.AppendRadioItem(wx.ID_ANY, "系统默认")
}
view_menu.AppendSubMenu(theme_menu, "主题")
menubar.Append(view_menu, "视图(&V)")
self.SetMenuBar(menubar)
# 绑定菜单事件
self.Bind(wx.EVT_MENU, self.new_folder, id=wx.ID_NEW)
self.Bind(wx.EVT_MENU, self.on_close_tab, id=wx.ID_CLOSE)
self.Bind(wx.EVT_MENU, lambda evt: self.Close(), id=wx.ID_EXIT)
self.Bind(wx.EVT_MENU, self.on_cut, id=wx.ID_CUT)
self.Bind(wx.EVT_MENU, self.on_copy, id=wx.ID_COPY)
self.Bind(wx.EVT_MENU, self.on_paste, id=wx.ID_PASTE)
self.Bind(wx.EVT_MENU, self.delete_items, id=wx.ID_DELETE)
self.Bind(wx.EVT_MENU, lambda evt: self.refresh_file_list(), id=wx.ID_REFRESH)
self.Bind(wx.EVT_MENU, self.restore_closed_tab, id=restore_tab_item.GetId())
# 绑定主题切换事件
for item in self.theme_items.values():
self.Bind(wx.EVT_MENU, self.on_change_theme, id=item.GetId())
# 设置默认主题
self.theme_items['system'].Check(True)
def refresh_all_tabs(self):
"""刷新所有标签页"""
for side in self.tabs:
for tab in self.tabs[side]:
self.refresh_file_list(tab)
def start_watching(self, path):
"""启动目录监控"""
try:
if self.observer and self.observer.is_alive():
if self.watch_dog:
self.observer.unschedule(self.watch_dog)
handler = FileChangeHandler(self.on_file_change)
self.watch_dog = self.observer.schedule(handler, path, recursive=False)
if not self.observer.is_alive():
self.observer.start()
except Exception as e:
wx.LogError(f"监控启动失败: {str(e)}")
def on_file_change(self, msg):
"""文件变化回调"""
wx.CallAfter(self.status_bar.SetStatusText, msg, 0)
wx.CallAfter(self.refresh_file_list)
def sync_directory_changes(self):
"""监控目录变化并同步"""
# 使用watchdog库实现文件系统监控
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class ChangeHandler(FileSystemEventHandler):
def __init__(self, callback):
self.callback = callback
def on_modified(self, event):
self.callback()
self.observer = Observer()
for side in self.tabs:
for tab in self.tabs[side]:
handler = ChangeHandler(lambda: wx.CallAfter(self.refresh_all_tabs))
self.observer.schedule(handler, tab['path'], recursive=False)
self.observer.start()
def on_change_theme(self, event):
"""切换应用程序主题"""
selected = next(k for k,v in self.theme_items.items() if v.IsChecked())
self.apply_theme(selected)
def apply_theme(self, theme_name):
"""应用主题配色方案"""
themes = {
'light': {
'bg': wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOW),
'fg': wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT),
'list_bg': wx.SystemSettings.GetColour(wx.SYS_COLOUR_LISTBOX),
'list_fg': wx.SystemSettings.GetColour(wx.SYS_COLOUR_LISTBOXTEXT),
'toolbar_bg': wx.SystemSettings.GetColour(wx.SYS_COLOUR_BTNFACE),
'textctrl_bg': wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOW),
'textctrl_fg': wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT),
'notebook_bg': wx.SystemSettings.GetColour(wx.SYS_COLOUR_BTNFACE),
'notebook_fg': wx.SystemSettings.GetColour(wx.SYS_COLOUR_BTNTEXT)
},
'dark': {
'bg': wx.Colour(53, 53, 53),
'fg': wx.Colour(240, 240, 240),
'list_bg': wx.Colour(30, 30, 30),
'list_fg': wx.Colour(240, 240, 240),
'toolbar_bg': wx.Colour(45, 45, 45),
'textctrl_bg': wx.Colour(30, 30, 30),
'textctrl_fg': wx.Colour(240, 240, 240),
'notebook_bg': wx.Colour(45, 45, 45),
'notebook_fg': wx.Colour(240, 240, 240)
},
'system': {
'bg': wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOW),
'fg': wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT),
'list_bg': wx.SystemSettings.GetColour(wx.SYS_COLOUR_LISTBOX),
'list_fg': wx.SystemSettings.GetColour(wx.SYS_COLOUR_LISTBOXTEXT),
'toolbar_bg': wx.SystemSettings.GetColour(wx.SYS_COLOUR_BTNFACE),
'textctrl_bg': wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOW),
'textctrl_fg': wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT),
'notebook_bg': wx.SystemSettings.GetColour(wx.SYS_COLOUR_BTNFACE),
'notebook_fg': wx.SystemSettings.GetColour(wx.SYS_COLOUR_BTNTEXT)
}
}
theme = themes.get(theme_name, themes['system'])
# 设置主窗口颜色
self.SetBackgroundColour(theme['bg'])
self.SetForegroundColour(theme['fg'])
# 设置面板颜色
self.main_panel.SetBackgroundColour(theme['bg'])
self.main_panel.SetForegroundColour(theme['fg'])
# 设置标签页颜色
for side in ['left', 'right']:
notebook = self.left_notebook if side == 'left' else self.right_notebook
notebook.SetBackgroundColour(theme['notebook_bg'])
notebook.SetForegroundColour(theme['notebook_fg'])
# 设置每个标签页的颜色
for tab in self.tabs[side]:
# 设置面板颜色
tab['panel'].SetBackgroundColour(theme['bg'])
tab['panel'].SetForegroundColour(theme['fg'])
# 设置工具栏颜色
toolbar = tab['panel'].GetChildren()[0]
if isinstance(toolbar, wx.ToolBar):
toolbar.SetBackgroundColour(theme['toolbar_bg'])
# 设置路径输入框颜色
path_ctrl = tab['path_ctrl']
path_ctrl.SetBackgroundColour(theme['textctrl_bg'])
path_ctrl.SetForegroundColour(theme['textctrl_fg'])
# 设置列表控件颜色
list_ctrl = tab['list']
list_ctrl.SetBackgroundColour(theme['list_bg'])
list_ctrl.SetForegroundColour(theme['list_fg'])
# 刷新控件
tab['panel'].Refresh()
path_ctrl.Refresh()
list_ctrl.Refresh()
# 刷新界面
self.Refresh()
self.Update()
def init_ui(self):
"""初始化用户界面 - 已弃用,功能已移至add_tab方法"""
pass
def on_search(self, event):
current_tab = self.get_current_tab()
if not current_tab:
return
keyword = self.search_ctrl.GetValue().lower()
list_ctrl = current_tab['list']
for i in range(list_ctrl.GetItemCount()):
name = list_ctrl.GetItemText(i, 1).lower()
list_ctrl.SetItemState(i, wx.LIST_STATE_SELECTED if keyword in name else 0, wx.LIST_STATE_SELECTED)
def new_folder(self, event):
"""创建新文件夹"""
current_tab = self.get_current_tab()
if not current_tab:
return
dlg = wx.TextEntryDialog(self, "请输入文件夹名称:", "新建文件夹")
if dlg.ShowModal() == wx.ID_OK:
name = dlg.GetValue()
if not name:
return
path = os.path.join(current_tab['path'], name)
try:
os.makedirs(path, exist_ok=True)
self.refresh_file_list(current_tab)
except Exception as e:
wx.MessageBox(f"创建文件夹失败: {str(e)}", "错误", wx.OK | wx.ICON_ERROR)
dlg.Destroy()
def load_system_icons(self):
"""加载系统图标"""
try:
# 获取系统图标
self.folder_icon = self.get_file_icon(os.path.expanduser("~"), True)
self.file_icon = self.get_file_icon("dummy.txt", False)
except Exception as e:
print(f"系统图标加载失败: {str(e)}")
# 使用默认图标
self.folder_icon = wx.ArtProvider.GetBitmap(wx.ART_FOLDER, wx.ART_OTHER, (16, 16))
self.file_icon = wx.ArtProvider.GetBitmap(wx.ART_NORMAL_FILE, wx.ART_OTHER, (16, 16))
def get_file_icon(self, path, is_folder=False):
"""获取文件或文件夹的系统图标"""
try:
# 从注册表获取图标
if is_folder:
key_path = "folder\\DefaultIcon"
else:
ext = os.path.splitext(path)[1].lower()
key_path = f"{ext}\\DefaultIcon" if ext else "*\\DefaultIcon"
try:
key = win32api.RegOpenKey(win32con.HKEY_CLASSES_ROOT, key_path, 0, win32con.KEY_READ)
icon_path, _ = win32api.RegQueryValueEx(key, "")
win32api.RegCloseKey(key)
except:
# 如果没有DefaultIcon,尝试获取关联程序
if not is_folder:
try:
ext = os.path.splitext(path)[1].lower()
key = win32api.RegOpenKey(win32con.HKEY_CLASSES_ROOT, ext, 0, win32con.KEY_READ)
file_type, _ = win32api.RegQueryValueEx(key, "")
win32api.RegCloseKey(key)
key = win32api.RegOpenKey(win32con.HKEY_CLASSES_ROOT, f"{file_type}\\DefaultIcon", 0, win32con.KEY_READ)
icon_path, _ = win32api.RegQueryValueEx(key, "")
win32api.RegCloseKey(key)
except:
raise Exception("No icon found in registry")
else:
raise Exception("No folder icon found in registry")
# 解析图标路径
if "," in icon_path:
icon_path, icon_index = icon_path.rsplit(",", 1)
icon_index = int(icon_index)
else:
icon_index = 0
# 移除引号
icon_path = icon_path.strip('"')
# 展开环境变量
icon_path = os.path.expandvars(icon_path)
try:
# 加载图标
large, small = win32gui.ExtractIconEx(icon_path, icon_index)
if small and len(small) > 0:
# 转换为wx.Bitmap
icon = wx.Icon()
icon.SetHandle(small[0])
bitmap = wx.Bitmap(icon)
# 释放图标句柄
for handle in small:
if handle:
win32gui.DestroyIcon(handle)
for handle in large:
if handle:
win32gui.DestroyIcon(handle)
return bitmap
except Exception as e:
print(f"加载图标失败: {str(e)}")
except Exception as e:
print(f"获取图标失败: {str(e)}")
# 如果获取失败,返回默认图标
return wx.ArtProvider.GetBitmap(
wx.ART_FOLDER if is_folder else wx.ART_NORMAL_FILE,
wx.ART_OTHER,
(16, 16)
)
def get_file_type_icon(self, file_path):
"""获取文件类型的系统图标"""
try:
# 如果是目录,返回文件夹图标
if os.path.isdir(file_path):
return self.folder_icon
# 从缓存获取图标
ext = os.path.splitext(file_path)[1].lower()
cache_key = ext if ext else os.path.basename(file_path).lower()
if cache_key in self._icon_cache:
return self._icon_cache[cache_key]
# 从注册表获取图标
try:
# 获取文件类型关联的程序路径
key_path = f"{ext}\\DefaultIcon" if ext else "*\\DefaultIcon"
try:
key = win32api.RegOpenKey(win32con.HKEY_CLASSES_ROOT, key_path, 0, win32con.KEY_READ)
icon_path, _ = win32api.RegQueryValueEx(key, "")
win32api.RegCloseKey(key)
except:
# 如果没有DefaultIcon,尝试获取关联程序
try:
key = win32api.RegOpenKey(win32con.HKEY_CLASSES_ROOT, ext, 0, win32con.KEY_READ)
file_type, _ = win32api.RegQueryValueEx(key, "")
win32api.RegCloseKey(key)
key = win32api.RegOpenKey(win32con.HKEY_CLASSES_ROOT, f"{file_type}\\DefaultIcon", 0, win32con.KEY_READ)
icon_path, _ = win32api.RegQueryValueEx(key, "")
win32api.RegCloseKey(key)
except:
raise Exception("No icon found in registry")
# 解析图标路径
if "," in icon_path:
icon_path, icon_index = icon_path.rsplit(",", 1)
icon_index = int(icon_index)
else:
icon_index = 0
# 移除引号
icon_path = icon_path.strip('"')
# 展开环境变量
icon_path = os.path.expandvars(icon_path)
# 加载图标
large, small = win32gui.ExtractIconEx(icon_path, icon_index)
if small:
# 转换为wx.Bitmap
icon = wx.Icon()
icon.SetHandle(small[0])
bitmap = wx.Bitmap(icon)
# 释放图标句柄
for handle in small:
win32gui.DestroyIcon(handle)
for handle in large:
win32gui.DestroyIcon(handle)
# 缓存并返回图标
self._icon_cache[cache_key] = bitmap
return bitmap
except Exception as e:
print(f"从注册表获取图标失败: {str(e)}")
# 如果无法从注册表获取,使用默认图标
default_icons = {
'.txt': wx.ArtProvider.GetBitmap(wx.ART_NORMAL_FILE, size=(16, 16)),
'.doc': wx.ArtProvider.GetBitmap(wx.ART_NORMAL_FILE, size=(16, 16)),
'.docx': wx.ArtProvider.GetBitmap(wx.ART_NORMAL_FILE, size=(16, 16)),
'.xls': wx.ArtProvider.GetBitmap(wx.ART_NORMAL_FILE, size=(16, 16)),
'.xlsx': wx.ArtProvider.GetBitmap(wx.ART_NORMAL_FILE, size=(16, 16)),
'.pdf': wx.ArtProvider.GetBitmap(wx.ART_NORMAL_FILE, size=(16, 16)),
'.jpg': wx.ArtProvider.GetBitmap(wx.ART_NORMAL_FILE, size=(16, 16)),
'.jpeg': wx.ArtProvider.GetBitmap(wx.ART_NORMAL_FILE, size=(16, 16)),
'.png': wx.ArtProvider.GetBitmap(wx.ART_NORMAL_FILE, size=(16, 16)),
'.gif': wx.ArtProvider.GetBitmap(wx.ART_NORMAL_FILE, size=(16, 16)),
'.exe': wx.ArtProvider.GetBitmap(wx.ART_EXECUTABLE_FILE, size=(16, 16)),
'.dll': wx.ArtProvider.GetBitmap(wx.ART_NORMAL_FILE, size=(16, 16)),
}
default_icon = default_icons.get(ext, self.file_icon)
self._icon_cache[cache_key] = default_icon
return default_icon
except Exception as e:
print(f"获取文件类型图标失败: {str(e)}")
return self.file_icon
def refresh_file_list(self, tab=None):
"""刷新指定标签页或当前标签页的文件列表"""
if tab is None:
tab = self.get_current_tab()
if not tab:
return
list_ctrl = tab['list']
icon_list = tab['icon_list']
current_path = tab['path']
path_ctrl = tab['path_ctrl']
# 保存当前滚动位置和选中项
top_item = list_ctrl.GetTopItem()
selected_items = []
item = -1
while True:
item = list_ctrl.GetNextItem(item, wx.LIST_NEXT_ALL, wx.LIST_STATE_SELECTED)
if item == -1:
break
selected_items.append(list_ctrl.GetItem(item, 1).GetText())
# 更新路径显示
path_ctrl.SetValue(current_path)
# 清空列表
list_ctrl.DeleteAllItems()
icon_list.RemoveAll()
try:
items = []
# 添加上级目录项
parent = os.path.dirname(current_path)
if parent and parent != current_path:
items.append(("..", True, 0, "", parent))
# 获取目录内容
for item in os.listdir(current_path):
full_path = os.path.join(current_path, item)
is_dir = os.path.isdir(full_path)
try:
size = os.path.getsize(full_path) if not is_dir else 0
mtime = os.path.getmtime(full_path)
modified = datetime.fromtimestamp(mtime).strftime('%Y-%m-%d %H:%M:%S')
items.append((item, is_dir, size, modified, full_path))
except OSError:
continue
# 排序:文件夹优先,然后按名称排序
items.sort(key=lambda x: (not x[1], x[0].lower()))
# 添加到列表
for idx, (name, is_dir, size, modified, full_path) in enumerate(items):
if name == "..":
icon = wx.ArtProvider.GetBitmap(wx.ART_GO_UP, wx.ART_OTHER, (16, 16))
else:
icon = self.folder_icon if is_dir else self.get_file_type_icon(full_path)
icon_idx = icon_list.Add(icon)
list_ctrl.InsertItem(idx, "")
list_ctrl.SetItemImage(idx, icon_idx)
list_ctrl.SetItem(idx, 1, name)
list_ctrl.SetItem(idx, 2, self.format_size(size) if not is_dir and name != ".." else "")
list_ctrl.SetItem(idx, 3, modified if name != ".." else "")
# 恢复选中状态
if name in selected_items:
list_ctrl.SetItemState(idx, wx.LIST_STATE_SELECTED, wx.LIST_STATE_SELECTED)
# 恢复滚动位置
if top_item >= 0 and top_item < list_ctrl.GetItemCount():
list_ctrl.EnsureVisible(top_item)
# 更新状态栏
total_items = len(items) - (1 if items and items[0][0] == ".." else 0)
folders = sum(1 for item in items if item[1] and item[0] != "..")
files = total_items - folders
self.status_bar.SetStatusText(f"文件夹: {folders}, 文件: {files}", 0)
except Exception as e:
wx.LogError(f"无法访问目录 {current_path}:{str(e)}")
# 调整列宽
self.adjust_list_columns(list_ctrl)
def format_size(self, size):
"""将文件大小转换为人类可读的格式"""
for unit in ['B', 'KB', 'MB', 'GB', 'TB']:
if size < 1024.0:
if unit == 'B':
return f"{int(size)} {unit}"
return f"{size:.2f} {unit}"
size /= 1024.0
return f"{size:.2f} PB"
# 其余方法实现(new_folder, delete_items等)...
def on_context_menu(self, event):
"""显示上下文菜单"""
current_tab = self.get_current_tab()
if not current_tab:
return
# 创建菜单
menu = wx.Menu()
menu.Append(wx.ID_NEW, "新建文件夹\tCtrl+N")
menu.AppendSeparator()
menu.Append(wx.ID_CUT, "剪切\tCtrl+X")
menu.Append(wx.ID_COPY, "复制\tCtrl+C")
menu.Append(wx.ID_PASTE, "粘贴\tCtrl+V")
menu.AppendSeparator()
menu.Append(wx.ID_DELETE, "删除\tDel")
# 绑定事件
menu.Bind(wx.EVT_MENU, self.new_folder, id=wx.ID_NEW)
menu.Bind(wx.EVT_MENU, self.on_copy, id=wx.ID_COPY)
menu.Bind(wx.EVT_MENU, self.on_paste, id=wx.ID_PASTE)
menu.Bind(wx.EVT_MENU, self.delete_items, id=wx.ID_DELETE)
# 显示菜单
self.PopupMenu(menu)
menu.Destroy()
def on_size(self, event):
"""处理窗口大小变化事件"""
if self.main_panel:
size = self.GetClientSize()
self.main_panel.SetSize(size)
# 根据保存的比例设置分割位置
window_width = self.splitter.GetSize().GetWidth()
new_pos = int(window_width * self.splitter_ratio)
# 确保不超出最小/最大限制
min_pos = 200
max_pos = window_width - 200
new_pos = max(min_pos, min(new_pos, max_pos))
self.splitter.SetSashPosition(new_pos)
self.main_panel.Layout()
# 调整当前标签页的列宽
for side in ['left', 'right']:
for tab in self.tabs[side]:
self.adjust_list_columns(tab['list'])
event.Skip()
def on_copy(self, event):
selected = self.get_selected_paths()
if selected:
self.clipboard = {"type": "copy", "paths": selected}
self.status_bar.SetStatusText(f"已复制 {len(selected)} 项", 0)
def on_paste(self, event):
"""粘贴文件"""
if not self.clipboard:
return
current_tab = self.get_current_tab()
if not current_tab:
return
dest = current_tab['path']
try:
for src in self.clipboard["paths"]:
if self.clipboard["type"] == "copy":
shutil.copy2(src, dest)
else: # 剪切操作
shutil.move(src, dest)
self.refresh_file_list()
except Exception as e:
wx.LogError(f"操作失败:{str(e)}")
def on_forward(self, event):
"""前进到下一个目录"""
current_tab = self.get_current_tab()
if not current_tab:
return
history = current_tab['history']
if len(history) > 1:
current_path = history[-1]
next_path = history[0] # 获取最早的路径
if os.path.exists(next_path) and next_path != current_path:
history.rotate(-1) # 循环移动历史记录
self.navigate_to(next_path)
def on_item_activated(self, event):
"""处理项目双击事件"""
try:
current_tab = self.get_current_tab()
if not current_tab:
return
list_ctrl = current_tab['list']
# 获取选中项
if isinstance(event, wx.ListEvent):
index = event.GetIndex()
else: