-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.pyw
More file actions
1490 lines (1282 loc) · 54.7 KB
/
main.pyw
File metadata and controls
1490 lines (1282 loc) · 54.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
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
"""
QuickImage - 极简图片搜索 (Everything 风格)
使用 .pyw 扩展名隐藏控制台窗口
"""
import os
import queue
import re
import shutil
import threading
import subprocess
import tempfile
import tkinter as tk
import tkinter.font as tkfont
import urllib.error
import urllib.parse
import urllib.request
import zipfile
from tkinter import ttk, filedialog, messagebox
import ctypes
# 4K DPI
try:
ctypes.windll.shcore.SetProcessDpiAwareness(2)
except:
try:
ctypes.windll.user32.SetProcessDPIAware()
except:
pass
from config import load_config, save_config
from search_engine import (
search_images,
parse_keywords,
get_backend_label,
find_everything_dll,
find_es_exe,
find_everything_executable,
)
# 系统托盘支持
try:
import pystray
from PIL import Image
HAS_TRAY = True
except ImportError:
HAS_TRAY = False
OUTPUT_DIR = os.path.join(os.path.expanduser("~"), "Desktop", "每日JIT")
WINDOW_EDGE_MARGIN = 40
VOIDTOOLS_DOWNLOADS_URL = "https://www.voidtools.com/downloads/"
DOWNLOAD_TIMEOUT_SECONDS = 30
DOWNLOAD_BLOCK_SIZE = 262144
I18N = {
"zh": {
"not_set": "未设置",
"ready": "就绪",
"searching": "搜索中...",
"menu_file": "文件(F)",
"menu_edit": "编辑(E)",
"menu_help": "帮助(H)",
"set_source": "设置源目录(O)...",
"set_output": "设置保存目录(S)...",
"check_deps": "检查搜索组件 / Check Search Components",
"language": "语言 / Language (L)",
"lang_zh": "中文 / Chinese",
"lang_en": "English / 英文",
"exit": "退出(X)",
"copy_output": "复制到保存目录(C)",
"select_all": "全选(A)",
"topmost": "窗口置顶(T)",
"help": "帮助(H)",
"about": "关于 QuickImage(A)...",
"col_name": "名称",
"col_path": "路径",
"col_size": "大小",
"source_prefix": "源",
"output_prefix": "保存",
"engine_prefix": "引擎",
"backend_not_ready": "未就绪",
"source_updated": "源目录已更新",
"output_updated": "保存目录已更新",
"source_required": "请先设置源目录 (文件 -> 设置源目录)",
"display_results": "显示 {display:,} / {total:,} 个结果",
"results_count": "{count:,} 个结果",
"not_found": "未找到匹配图片",
"copied": "已复制 {count} 个文件到保存目录",
"help_title": "帮助",
"help_text": (
"使用方法:\n\n"
"1. 文件 -> 设置源目录\n"
"2. 文件 -> 设置保存目录 (可选)\n"
"3. 输入精确文件名 (空格分隔多个)\n"
"4. 按 Enter/Ctrl+C 复制到保存目录\n"
"\n"
"快捷键:\n"
"Ctrl+A - 全选\n"
"Ctrl+C/Enter - 复制\n"
"T - 切换置顶"
),
"about_title": "关于 QuickImage",
"about_text": "QuickImage v1.0\n\n极简图片搜索工具\n基于 Everything 搜索引擎\n\n© NerionX",
"tray_show": "显示主窗口",
"tray_exit": "退出",
"mini_source_required": "未设置源目录",
"mini_found": "找到 {count} 个文件,按 Enter 复制",
"mini_not_found": "未找到",
"mini_copied": "已复制 {count} 个文件",
"mini_empty": "无文件可复制",
"deps_prompt_title": "安装搜索组件",
"deps_prompt_body": (
"检测到本机还没有可用的 Everything 搜索环境。\n\n"
"QuickImage 现在可以自动为你完成:\n"
"1. 下载并安装 Everything(官方)\n"
"2. 自动下载加速组件 SDK\n"
"3. 自动准备命令行搜索组件\n\n"
"整个过程会使用官方 voidtools 下载地址。\n"
"是否现在开始?\n\n"
"English:\n"
"QuickImage can now download and install Everything, the SDK, and the CLI helper automatically.\n"
"Do you want to continue now?"
),
"deps_installing": "正在自动准备搜索组件,请稍候...",
"deps_ready": "搜索组件已准备完成",
"deps_declined": "已跳过自动安装,可稍后再次打开程序进行安装",
"deps_failed_title": "自动安装失败",
"deps_failed": (
"自动安装没有完成。\n\n"
"请确认网络可访问 voidtools 官网,或者稍后重试。\n\n"
"English:\n"
"Automatic setup did not complete. Please check your network and try again."
),
"deps_done_title": "安装完成",
"deps_done": (
"搜索组件已准备完成,现在可以直接使用。\n\n"
"English:\n"
"Search components are ready. You can use QuickImage now."
),
"deps_repair_title": "修复搜索组件",
"deps_repair_body": (
"QuickImage 将重新检查 Everything、SDK 和命令行搜索组件。\n"
"如果缺少内容,会自动下载并修复。\n\n"
"是否现在开始?\n\n"
"English:\n"
"QuickImage will re-check Everything, the SDK, and the CLI helper, then repair anything missing.\n"
"Do you want to continue now?"
),
"deps_missing_runtime_title": "搜索组件缺失",
"deps_missing_runtime": (
"当前没有可用的搜索组件,因此无法执行搜索。\n\n"
"是否现在自动修复?\n\n"
"English:\n"
"A working search component is missing, so search cannot continue.\n"
"Would you like QuickImage to repair it now?"
),
},
"en": {
"not_set": "Not set",
"ready": "Ready",
"searching": "Searching...",
"menu_file": "File(F)",
"menu_edit": "Edit(E)",
"menu_help": "Help(H)",
"set_source": "Set Source Directory(O)...",
"set_output": "Set Output Directory(S)...",
"check_deps": "Check Search Components / 检查搜索组件",
"language": "Language / 语言 (L)",
"lang_zh": "Chinese / 中文",
"lang_en": "English / 英文",
"exit": "Exit(X)",
"copy_output": "Copy to Output(C)",
"select_all": "Select All(A)",
"topmost": "Always on Top(T)",
"help": "Help(H)",
"about": "About QuickImage(A)...",
"col_name": "Name",
"col_path": "Path",
"col_size": "Size",
"source_prefix": "Source",
"output_prefix": "Output",
"engine_prefix": "Engine",
"backend_not_ready": "Not Ready",
"source_updated": "Source directory updated",
"output_updated": "Output directory updated",
"source_required": "Please set source directory first (File -> Set Source Directory)",
"display_results": "Showing {display:,} / {total:,} results",
"results_count": "{count:,} results",
"not_found": "No matching images found",
"copied": "Copied {count} files to output directory",
"help_title": "Help",
"help_text": (
"How to use:\n\n"
"1. File -> Set Source Directory\n"
"2. File -> Set Output Directory (optional)\n"
"3. Enter exact file names (space-separated for multiple)\n"
"4. Press Enter/Ctrl+C to copy to output directory\n"
"\n"
"Shortcuts:\n"
"Ctrl+A - Select all\n"
"Ctrl+C/Enter - Copy\n"
"T - Toggle always on top"
),
"about_title": "About QuickImage",
"about_text": "QuickImage v1.0\n\nMinimal image search tool\nPowered by Everything search engine\n\n© NerionX",
"tray_show": "Show Main Window",
"tray_exit": "Exit",
"mini_source_required": "Source directory is not set",
"mini_found": "Found {count} files, press Enter to copy",
"mini_not_found": "No results",
"mini_copied": "Copied {count} files",
"mini_empty": "No files to copy",
"deps_prompt_title": "Install Search Components",
"deps_prompt_body": (
"QuickImage could not find a working Everything search environment.\n\n"
"It can automatically:\n"
"1. Download and install Everything from voidtools\n"
"2. Download the SDK for faster search\n"
"3. Download the CLI helper automatically\n\n"
"Would you like to continue now?\n\n"
"中文:\n"
"QuickImage 可以自动帮你安装 Everything、SDK 和命令行组件。是否现在开始?"
),
"deps_installing": "Preparing search components automatically...",
"deps_ready": "Search components are ready",
"deps_declined": "Automatic setup skipped. You can reopen the app later to install it.",
"deps_failed_title": "Automatic Setup Failed",
"deps_failed": (
"Automatic setup did not complete.\n\n"
"Please check network access to the voidtools website and try again.\n\n"
"中文:\n"
"自动安装未完成,请检查网络后重试。"
),
"deps_done_title": "Setup Complete",
"deps_done": (
"Search components are ready. You can use QuickImage now.\n\n"
"中文:\n"
"搜索组件已准备完成,现在可以直接使用。"
),
"deps_repair_title": "Repair Search Components",
"deps_repair_body": (
"QuickImage will re-check Everything, the SDK, and the CLI helper.\n"
"Anything missing will be downloaded automatically.\n\n"
"Do you want to continue now?\n\n"
"中文:\n"
"QuickImage 会重新检查 Everything、SDK 和命令行组件,并自动修复缺失部分。"
),
"deps_missing_runtime_title": "Search Component Missing",
"deps_missing_runtime": (
"A working search component is missing, so search cannot continue.\n\n"
"Would you like QuickImage to repair it now?\n\n"
"中文:\n"
"当前缺少可用搜索组件,是否现在自动修复?"
),
},
}
class App(tk.Tk):
def __init__(self):
super().__init__()
self.cfg = load_config()
self.current_language = self.cfg.get("language", "zh")
if self.current_language not in I18N:
self.current_language = "zh"
self.lang_var = tk.StringVar(value=self.current_language)
self.colors = {
"bg_app": "#f6f7f9",
"bg_card": "#ffffff",
"bg_subtle": "#f3f4f6",
"border": "#d9dde3",
"text": "#1f2933",
"text_muted": "#6b7280",
"accent_soft": "#dbeafe",
"row_alt": "#fafbfc",
}
self.spacing = {
"outer": 6,
"section": 4,
"inner": 4,
"status_top": 3,
}
self.title("QuickImage")
self.geometry("920x580")
self.minsize(600, 400)
self.configure(bg=self.colors["bg_app"])
# 图标
self.icon_path = os.path.join(os.path.dirname(__file__), "i.png")
if os.path.exists(self.icon_path):
try:
img = tk.PhotoImage(file=self.icon_path)
self.iconphoto(True, img)
except:
pass
self.tree_font = tkfont.Font(family="Segoe UI", size=9)
self.heading_font = tkfont.Font(family="Microsoft YaHei", size=9)
self.status_font = tkfont.Font(family="Microsoft YaHei UI", size=9)
self.tree_rowheight = max(self.tree_font.metrics("linespace") + 10, 28)
self._setup_styles()
self.default_name_col_width = 180
self.max_name_col_width = 280
self.current_name_col_width = self.default_name_col_width
self.results = []
self.timer = None
self.search_delay_ms = 200
self.search_token = 0
self.last_search_signature = None
self.search_queue = queue.Queue(maxsize=1)
self.search_worker = threading.Thread(target=self._search_worker_loop, daemon=True)
self.search_worker.start()
self.size_update_token = 0
self.render_update_token = 0
self.render_job = None
self.geometry_save_job = None
self.max_display_results = 1000
self.max_search_results = 5000
self.render_chunk_size = 60
self.render_interval_ms = 10
self.size_preload_limit = 120
self.name_measure_limit = 120
self.is_topmost = tk.BooleanVar(value=True) # 默认置顶
self.tray_icon = None
self.bootstrap_in_progress = False
self._menu()
self._ui()
self._restore_window_geometry()
# 不使用系统托盘
# 关闭窗口直接退出
self.protocol("WM_DELETE_WINDOW", self._quit_app)
self.bind("<Configure>", self._on_window_configure)
self._update_output_status(self._ensure_output_dir())
# 默认置顶
self._toggle_topmost()
self.after(300, self._bootstrap_dependencies_on_first_run)
def _t(self, key):
lang_dict = I18N.get(self.current_language, I18N["zh"])
return lang_dict.get(key, I18N["zh"].get(key, key))
def _backend_label(self):
label = get_backend_label()
if label == "未就绪":
return self._t("backend_not_ready")
return label
def _set_language(self, lang, save=True):
lang = "en" if lang == "en" else "zh"
if lang == self.current_language and hasattr(self, "tree"):
return
self.current_language = lang
self.lang_var.set(lang)
if save:
self.cfg["language"] = lang
save_config(self.cfg)
if hasattr(self, "tree"):
self._menu()
self.tree.heading("name", text=self._t("col_name"), anchor="w")
self.tree.heading("path", text=self._t("col_path"), anchor="w")
self.tree.heading("size", text=self._t("col_size"), anchor="e")
self._update_source_status(self.cfg.get("source_path", ""))
self._update_output_status(self._get_output_dir())
self._update_engine_status()
if self.results:
total_count = len(self.results)
display_count = min(total_count, self.max_display_results)
if total_count > display_count:
self.status.set(self._t("display_results").format(display=display_count, total=total_count))
else:
self.status.set(self._t("results_count").format(count=total_count))
elif hasattr(self, "entry") and self.entry.get().strip():
self.status.set(self._t("not_found"))
else:
self.status.set(self._t("ready"))
def _on_language_changed(self):
self._set_language(self.lang_var.get(), save=True)
def _setup_tray(self):
"""设置系统托盘"""
if not HAS_TRAY:
return
try:
icon_img = Image.open(self.icon_path)
except:
# 创建简单图标
icon_img = Image.new('RGB', (64, 64), color='#0078d4')
menu = pystray.Menu(
pystray.MenuItem(self._t("tray_show"), self._show_from_tray),
pystray.MenuItem(self._t("tray_exit"), self._quit_app)
)
self.tray_icon = pystray.Icon("QuickImage", icon_img, "QuickImage", menu)
# 后台运行托盘图标
threading.Thread(target=self.tray_icon.run, daemon=True).start()
def _hide_to_tray(self):
"""隐藏到托盘"""
self.withdraw()
def _show_from_tray(self):
"""从托盘恢复"""
self.after(0, self._do_show)
def _do_show(self):
self.deiconify()
self.lift()
self.focus_force()
def _on_minimize(self, e):
"""最小化时隐藏到托盘"""
if self.state() == "iconic":
self.withdraw()
def _quit_app(self):
"""退出程序"""
self._save_window_geometry()
if self.tray_icon:
self.tray_icon.stop()
self.after(0, self.destroy)
def _is_valid_window_geometry(self, geometry):
if not isinstance(geometry, str):
return False
return bool(re.match(r"^\d+x\d+[+-]\d+[+-]\d+$", geometry.strip()))
def _parse_window_geometry(self, geometry):
if not self._is_valid_window_geometry(geometry):
return None
m = re.match(r"^(\d+)x(\d+)([+-]\d+)([+-]\d+)$", geometry.strip())
if not m:
return None
width, height, x, y = m.groups()
return int(width), int(height), int(x), int(y)
def _get_default_window_size(self):
self.update_idletasks()
screen_w = max(self.winfo_screenwidth(), 800)
screen_h = max(self.winfo_screenheight(), 600)
width = min(920, max(screen_w - WINDOW_EDGE_MARGIN * 2, 600))
height = min(580, max(screen_h - 160, 400))
return width, height
def _fit_geometry_to_screen(self, geometry):
parsed = self._parse_window_geometry(geometry)
if not parsed:
return None
width, height, x, y = parsed
screen_w = max(self.winfo_screenwidth(), 800)
screen_h = max(self.winfo_screenheight(), 600)
max_width = max(screen_w - WINDOW_EDGE_MARGIN * 2, 600)
max_height = max(screen_h - 120, 400)
width = min(max(width, 600), max_width)
height = min(max(height, 400), max_height)
min_x = 0
min_y = 0
max_x = max(screen_w - width, 0)
max_y = max(screen_h - height - 80, 0)
x = min(max(x, min_x), max_x)
y = min(max(y, min_y), max_y)
return f"{width}x{height}+{x}+{y}"
def _center_main_window(self):
self.update_idletasks()
width = self.winfo_width()
height = self.winfo_height()
screen_w = self.winfo_screenwidth()
screen_h = self.winfo_screenheight()
if width <= 1 or height <= 1:
width, height = self._get_default_window_size()
else:
width = min(width, max(screen_w - WINDOW_EDGE_MARGIN * 2, 600))
height = min(height, max(screen_h - 120, 400))
x = max((screen_w - width) // 2, 0)
y = max((screen_h - height) // 3, 0)
self.geometry(f"{width}x{height}+{x}+{y}")
def _restore_window_geometry(self):
saved_geometry = self.cfg.get("window_geometry", "")
fitted_geometry = self._fit_geometry_to_screen(saved_geometry)
if fitted_geometry:
self.geometry(fitted_geometry)
else:
width, height = self._get_default_window_size()
self.geometry(f"{width}x{height}")
self._center_main_window()
def _save_window_geometry(self):
if self.state() != "normal":
return
current_geometry = self.geometry()
fitted_geometry = self._fit_geometry_to_screen(current_geometry)
if fitted_geometry:
self.cfg["window_geometry"] = fitted_geometry
save_config(self.cfg)
def _on_window_configure(self, event):
if event.widget != self:
return
if self.state() != "normal":
return
if self.geometry_save_job:
self.after_cancel(self.geometry_save_job)
self.geometry_save_job = self.after(500, self._save_window_geometry_debounced)
def _save_window_geometry_debounced(self):
self.geometry_save_job = None
self._save_window_geometry()
def _clamp_popup_position(self, width, height, x, y, top_gap=20):
screen_w = max(self.winfo_screenwidth(), width)
screen_h = max(self.winfo_screenheight(), height)
max_x = max(screen_w - width - 10, 0)
max_y = max(screen_h - height - 50, 0)
clamped_x = min(max(x, 0), max_x)
clamped_y = min(max(y, top_gap), max_y)
return clamped_x, clamped_y
def _get_output_dir(self):
configured_output = str(self.cfg.get("output_path", "")).strip()
if configured_output:
return os.path.normpath(configured_output)
return OUTPUT_DIR
def _ensure_output_dir(self):
output_dir = self._get_output_dir()
try:
os.makedirs(output_dir, exist_ok=True)
return output_dir
except:
os.makedirs(OUTPUT_DIR, exist_ok=True)
self.cfg["output_path"] = ""
save_config(self.cfg)
return OUTPUT_DIR
def _is_64bit_python(self):
return ctypes.sizeof(ctypes.c_void_p) == 8
def _dependency_state(self):
return {
"everything_exe": find_everything_executable(),
"es_exe": find_es_exe(),
"sdk_dll": find_everything_dll(),
}
def _bootstrap_dependencies_on_first_run(self):
if self.cfg.get("dependencies_bootstrapped", False):
return
self._bootstrap_dependencies_if_needed(first_run=True)
def _bootstrap_dependencies_if_needed(self, first_run=False):
if self.bootstrap_in_progress:
return
state = self._dependency_state()
needs_everything = not state["everything_exe"]
needs_sdk = not state["sdk_dll"]
needs_es = not state["es_exe"]
if not (needs_everything or needs_sdk or needs_es):
if first_run:
self.cfg["dependencies_bootstrapped"] = True
save_config(self.cfg)
return
if needs_everything:
should_continue = messagebox.askyesno(
self._t("deps_prompt_title"),
self._t("deps_prompt_body"),
parent=self,
)
if not should_continue:
self.status.set(self._t("deps_declined"))
return
self._start_dependency_bootstrap(needs_everything, needs_sdk, needs_es, first_run=first_run)
def _start_dependency_bootstrap(self, needs_everything, needs_sdk, needs_es, first_run=False):
if self.bootstrap_in_progress:
return
self.bootstrap_in_progress = True
self.status.set(self._t("deps_installing"))
threading.Thread(
target=self._bootstrap_dependencies_worker,
args=(needs_everything, needs_sdk, needs_es, first_run),
daemon=True,
).start()
def _bootstrap_dependencies_worker(self, needs_everything, needs_sdk, needs_es, first_run):
try:
links = self._fetch_voidtools_links()
current_dir = os.path.dirname(os.path.abspath(__file__))
with tempfile.TemporaryDirectory(prefix="quickimage-setup-") as temp_dir:
if needs_everything:
installer_name = os.path.basename(urllib.parse.urlparse(links["everything_installer"]).path)
installer_path = os.path.join(temp_dir, installer_name)
self._download_file(links["everything_installer"], installer_path)
self._install_everything(installer_path)
if needs_es:
es_zip_name = os.path.basename(urllib.parse.urlparse(links["es_zip"]).path)
es_zip_path = os.path.join(temp_dir, es_zip_name)
self._download_file(links["es_zip"], es_zip_path)
self._extract_zip_member(es_zip_path, "es.exe", os.path.join(current_dir, "es.exe"))
if needs_sdk:
sdk_zip_name = os.path.basename(urllib.parse.urlparse(links["sdk_zip"]).path)
sdk_zip_path = os.path.join(temp_dir, sdk_zip_name)
self._download_file(links["sdk_zip"], sdk_zip_path)
dll_name = "Everything64.dll" if self._is_64bit_python() else "Everything32.dll"
self._extract_zip_member(sdk_zip_path, dll_name, os.path.join(current_dir, dll_name))
final_state = self._dependency_state()
if not final_state["everything_exe"]:
raise RuntimeError("Everything executable not found after installation.")
if not (final_state["sdk_dll"] or final_state["es_exe"]):
raise RuntimeError("No working search backend was prepared.")
self.after(0, lambda: self._bootstrap_dependencies_success(first_run))
except Exception as exc:
self.after(0, lambda err=str(exc): self._bootstrap_dependencies_failed(err))
def _bootstrap_dependencies_success(self, first_run):
self.bootstrap_in_progress = False
self.cfg["dependencies_bootstrapped"] = True
save_config(self.cfg)
self.status.set(self._t("deps_ready"))
self._update_engine_status()
if first_run:
messagebox.showinfo(self._t("deps_done_title"), self._t("deps_done"), parent=self)
def _bootstrap_dependencies_failed(self, error_text):
self.bootstrap_in_progress = False
self._update_engine_status()
self.status.set(self._t("backend_not_ready"))
detail_text = f"{self._t('deps_failed')}\n\n{error_text}"
messagebox.showerror(self._t("deps_failed_title"), detail_text, parent=self)
def _repair_dependencies(self):
if self.bootstrap_in_progress:
return
should_continue = messagebox.askyesno(
self._t("deps_repair_title"),
self._t("deps_repair_body"),
parent=self,
)
if not should_continue:
return
self.cfg["dependencies_bootstrapped"] = False
save_config(self.cfg)
state = self._dependency_state()
self._start_dependency_bootstrap(
not state["everything_exe"],
not state["sdk_dll"],
not state["es_exe"],
first_run=False,
)
def _ensure_dependencies_before_search(self):
state = self._dependency_state()
has_backend = bool(state["sdk_dll"] or state["es_exe"])
if state["everything_exe"] and has_backend:
return True
should_continue = messagebox.askyesno(
self._t("deps_missing_runtime_title"),
self._t("deps_missing_runtime"),
parent=self,
)
if not should_continue:
return False
self.cfg["dependencies_bootstrapped"] = False
save_config(self.cfg)
self._start_dependency_bootstrap(
not state["everything_exe"],
not state["sdk_dll"],
not state["es_exe"],
first_run=False,
)
return False
def _fetch_voidtools_links(self):
html = self._download_text(VOIDTOOLS_DOWNLOADS_URL)
arch = "x64" if self._is_64bit_python() else "x86"
everything_installer = self._find_download_link(
html,
rf'href="([^"]*Everything-[^"]+\.{arch}\.msi)"',
)
es_zip = self._find_download_link(
html,
rf'href="([^"]*ES-[^"]+\.{arch}\.zip)"',
)
sdk_zip = self._find_download_link(
html,
r'href="([^"]*Everything-SDK\.zip)"',
)
return {
"everything_installer": everything_installer,
"es_zip": es_zip,
"sdk_zip": sdk_zip,
}
def _find_download_link(self, html, pattern):
match = re.search(pattern, html, flags=re.IGNORECASE)
if not match:
raise RuntimeError(f"Download link not found for pattern: {pattern}")
return urllib.parse.urljoin(VOIDTOOLS_DOWNLOADS_URL, match.group(1))
def _download_text(self, url):
request = urllib.request.Request(url, headers={"User-Agent": "QuickImage/1.0"})
with urllib.request.urlopen(request, timeout=DOWNLOAD_TIMEOUT_SECONDS) as response:
return response.read().decode("utf-8", errors="ignore")
def _download_file(self, url, target_path):
request = urllib.request.Request(url, headers={"User-Agent": "QuickImage/1.0"})
with urllib.request.urlopen(request, timeout=DOWNLOAD_TIMEOUT_SECONDS) as response, open(target_path, "wb") as output:
while True:
block = response.read(DOWNLOAD_BLOCK_SIZE)
if not block:
break
output.write(block)
def _install_everything(self, installer_path):
completed = subprocess.run(
["msiexec", "/i", installer_path, "/passive", "/norestart"],
capture_output=True,
text=True,
check=False,
)
if completed.returncode != 0:
raise RuntimeError((completed.stderr or completed.stdout or "msiexec failed").strip())
def _extract_zip_member(self, zip_path, member_name, output_path):
wanted_name = member_name.lower()
with zipfile.ZipFile(zip_path) as archive:
for item in archive.infolist():
if os.path.basename(item.filename).lower() != wanted_name:
continue
with archive.open(item) as src, open(output_path, "wb") as dst:
shutil.copyfileobj(src, dst)
return
raise RuntimeError(f"{member_name} not found in archive.")
def _setup_styles(self):
self.style = ttk.Style()
try:
self.style.theme_use('clam')
except:
pass
self.style.configure("Treeview",
background=self.colors["bg_card"],
foreground=self.colors["text"],
fieldbackground=self.colors["bg_card"],
rowheight=self.tree_rowheight,
borderwidth=0,
relief="flat",
padding=(4, 2),
font=self.tree_font
)
self.style.configure("Treeview.Heading",
background=self.colors["bg_subtle"],
foreground=self.colors["text"],
borderwidth=0,
relief="flat",
font=self.heading_font,
padding=(8, 6)
)
self.style.map("Treeview.Heading",
background=[("active", self.colors["bg_subtle"])],
relief=[("pressed", "flat"), ("active", "flat")]
)
self.style.map("Treeview",
background=[("selected", self.colors["accent_soft"])],
foreground=[("selected", self.colors["text"])]
)
self.style.configure("Vertical.TScrollbar", width=10)
def _menu(self):
menu_bg = "#ffffff"
menu_fg = "#000000"
menu_active_bg = "#cce8ff"
menu_active_fg = "#000000"
m = tk.Menu(self, bg=menu_bg, fg=menu_fg,
activebackground=menu_active_bg, activeforeground=menu_active_fg,
relief="flat", bd=0)
f = tk.Menu(m, tearoff=0, bg=menu_bg, fg=menu_fg,
activebackground=menu_active_bg, activeforeground=menu_active_fg)
f.add_command(label=self._t("set_source"), command=self._browse)
f.add_command(label=self._t("set_output"), command=self._browse_output)
f.add_command(label=self._t("check_deps"), command=self._repair_dependencies)
lang_menu = tk.Menu(f, tearoff=0, bg=menu_bg, fg=menu_fg,
activebackground=menu_active_bg, activeforeground=menu_active_fg)
lang_menu.add_radiobutton(label=self._t("lang_zh"), value="zh", variable=self.lang_var, command=self._on_language_changed)
lang_menu.add_radiobutton(label=self._t("lang_en"), value="en", variable=self.lang_var, command=self._on_language_changed)
f.add_cascade(label=self._t("language"), menu=lang_menu)
f.add_separator()
f.add_command(label=self._t("exit"), command=self._quit_app, accelerator="Alt+F4")
m.add_cascade(label=self._t("menu_file"), menu=f, underline=0)
e = tk.Menu(m, tearoff=0, bg=menu_bg, fg=menu_fg,
activebackground=menu_active_bg, activeforeground=menu_active_fg)
e.add_command(label=self._t("copy_output"), command=self._copy, accelerator="Ctrl+C")
e.add_separator()
e.add_command(label=self._t("select_all"), command=self._select_all, accelerator="Ctrl+A")
e.add_checkbutton(label=self._t("topmost"), variable=self.is_topmost, command=self._toggle_topmost, accelerator="T")
m.add_cascade(label=self._t("menu_edit"), menu=e, underline=0)
h = tk.Menu(m, tearoff=0, bg=menu_bg, fg=menu_fg,
activebackground=menu_active_bg, activeforeground=menu_active_fg)
h.add_command(label=self._t("help"), command=self._help, accelerator="F1")
h.add_separator()
h.add_command(label=self._t("about"), command=self._about, accelerator="Ctrl+F1")
m.add_cascade(label=self._t("menu_help"), menu=h, underline=0)
self.config(menu=m)
self.bind("<Control-a>", lambda e: self._select_all())
self.bind("<Control-c>", lambda e: self._copy())
self.bind("<t>", lambda e: self._toggle_topmost_key())
self.bind("<T>", lambda e: self._toggle_topmost_key())
self.bind("<F1>", lambda e: self._help())
self.bind("<Control-F1>", lambda e: self._about())
def _ui(self):
body = tk.Frame(self, bg=self.colors["bg_app"])
body.pack(
fill="both",
expand=True,
padx=self.spacing["outer"],
pady=(self.spacing["outer"], 0)
)
search_card = tk.Frame(
body,
bg=self.colors["bg_card"],
highlightthickness=1,
highlightbackground=self.colors["border"]
)
search_card.pack(fill="x", pady=(0, self.spacing["section"]))
search_frame = tk.Frame(search_card, bg=self.colors["bg_card"])
search_frame.pack(
fill="x",
padx=self.spacing["inner"],
pady=self.spacing["inner"]
)
self.entry = tk.Entry(
search_frame,
font=("Segoe UI", 11),
bg=self.colors["bg_card"],
fg=self.colors["text"],
insertbackground=self.colors["text"],
relief="flat",
bd=0
)
self.entry.pack(fill="x", ipady=2)
self.entry.bind("<KeyRelease>", self._key)
self.entry.bind("<Return>", lambda e: self._copy())
self.entry.bind("<Button-1>", self._on_entry_focus)
self.entry.bind("<FocusIn>", self._on_entry_focus)
list_frame = tk.Frame(
body,
bg=self.colors["bg_card"],
highlightthickness=1,
highlightbackground=self.colors["border"]
)
list_frame.pack(fill="both", expand=True)
cols = ("name", "path", "size")
self.tree = ttk.Treeview(list_frame, columns=cols, show="headings", selectmode="extended")
self.tree.heading("name", text=self._t("col_name"), anchor="w")
self.tree.heading("path", text=self._t("col_path"), anchor="w")
self.tree.heading("size", text=self._t("col_size"), anchor="e")
self.tree.column("name", width=self.default_name_col_width, minwidth=120, anchor="w", stretch=False)
self.tree.column("path", width=420, minwidth=180, anchor="w", stretch=True)
self.tree.column("size", width=86, minwidth=70, anchor="e", stretch=False)
self.tree.tag_configure("base", background=self.colors["bg_card"])
self.tree.tag_configure("alt", background=self.colors["row_alt"])
self.vsb = ttk.Scrollbar(list_frame, orient="vertical", command=self.tree.yview)
self.tree.configure(yscrollcommand=self._on_tree_scroll)
self.tree.grid(row=0, column=0, sticky="nsew")
self.vsb.grid(row=0, column=1, sticky="ns")
self.tree.bind("<Configure>", self._resize_tree_columns)
list_frame.bind("<Configure>", self._resize_tree_columns)
list_frame.grid_rowconfigure(0, weight=1)
list_frame.grid_columnconfigure(0, weight=1)
status_frame = tk.Frame(
self,
bg=self.colors["bg_card"],
highlightthickness=1,
highlightbackground=self.colors["border"]
)
status_frame.pack(
side="bottom",
fill="x",
padx=self.spacing["outer"],
pady=(self.spacing["status_top"], self.spacing["outer"])
)
status_top = tk.Frame(status_frame, bg=self.colors["bg_card"])
status_top.pack(fill="x", padx=8, pady=(4, 0))
status_bottom = tk.Frame(status_frame, bg=self.colors["bg_card"])
status_bottom.pack(fill="x", padx=8, pady=(0, 4))
self.status = tk.StringVar(value=self._t("ready"))
self.source_info = tk.StringVar(value=f"{self._t('source_prefix')}: {self._t('not_set')}")
self.output_info = tk.StringVar(value=f"{self._t('output_prefix')}: {self._t('not_set')}")
self.engine_info = tk.StringVar(value=f"{self._t('engine_prefix')}: {self._t('backend_not_ready')}")
tk.Label(
status_top,
textvariable=self.status,
bg=self.colors["bg_card"],
fg=self.colors["text_muted"],
anchor="w",
font=self.status_font,
pady=1
).pack(side="left", fill="x", expand=True)
tk.Label(
status_top,
textvariable=self.engine_info,
bg=self.colors["bg_card"],
fg=self.colors["text_muted"],
anchor="e",
font=self.status_font,
pady=1
).pack(side="right")
tk.Label(
status_bottom,
textvariable=self.source_info,
bg=self.colors["bg_card"],
fg=self.colors["text_muted"],
anchor="w",
font=self.status_font,
pady=1
).pack(side="left", fill="x", expand=True)
tk.Label(
status_bottom,
textvariable=self.output_info,
bg=self.colors["bg_card"],
fg=self.colors["text_muted"],
anchor="e",
font=self.status_font,
pady=1
).pack(side="right")
self._update_source_status(self.cfg.get("source_path", ""))
self._update_output_status(self._get_output_dir())
self._update_engine_status()
self.after(0, self._resize_tree_columns)