-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforzeos_core.py
More file actions
1157 lines (1077 loc) · 43.9 KB
/
forzeos_core.py
File metadata and controls
1157 lines (1077 loc) · 43.9 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
"""
forzeos_core.py
Core window manager, animation, command palette, and desktop widget helpers
Designed to integrate non-invasively with the large ForzeOS file.
"""
import tkinter as tk
from tkinter import messagebox
import threading
import time
import weakref
import urllib.request
import json
import os
import sys
from pathlib import Path
import logging
logger = logging.getLogger(__name__)
# Keep weak refs to managed windows
_WINDOW_BASE_INSTANCES = weakref.WeakSet()
class AnimationManager:
def __init__(self):
self._locks = weakref.WeakKeyDictionary()
def fade_in(self, toplevel, duration=0.28, steps=14, target_alpha=1.0):
try:
if not hasattr(toplevel, 'wm_attributes'):
return
try:
toplevel.wm_attributes('-alpha', 0.0)
except Exception:
pass
step = max(1, steps)
interval = max(0.01, float(duration) / step)
def _run(i=0):
try:
a = float(i) / step * target_alpha
toplevel.wm_attributes('-alpha', a)
if i < step:
toplevel.after(int(interval * 1000), lambda: _run(i+1))
except Exception:
pass
_run(0)
except Exception:
pass
def fade_out(self, toplevel, duration=0.28, steps=12, on_complete=None):
try:
if not hasattr(toplevel, 'wm_attributes'):
if on_complete:
try: on_complete()
except Exception: pass
return
try:
cur = toplevel.wm_attributes('-alpha')
except Exception:
cur = 1.0
step = max(1, steps)
interval = max(0.01, float(duration) / step)
def _run(i=step):
try:
a = float(i) / step * cur
toplevel.wm_attributes('-alpha', a)
if i > 0:
toplevel.after(int(interval * 1000), lambda: _run(i-1))
else:
if on_complete:
try: on_complete()
except Exception: pass
except Exception:
if on_complete:
try: on_complete()
except Exception: pass
_run(step)
except Exception:
if on_complete:
try: on_complete()
except Exception: pass
# single shared manager
_ANIM = AnimationManager()
class WindowBase:
"""Manager attached to an existing Toplevel. Keeps a common header, opacity
and animation handling. The original Toplevel object is left intact and
returned to existing code to preserve compatibility.
"""
def __init__(self, toplevel: tk.Toplevel, title: str = None, forze=None):
self.window = toplevel
self.title = title or getattr(toplevel, 'title', '')
self.forze = forze
self._closed = False
_WINDOW_BASE_INSTANCES.add(self)
try:
# Add a simple in-window header bar (keeps system titlebar intact)
header = tk.Frame(self.window, height=26)
header.pack(side='top', fill='x')
header.configure(bg=(getattr(forze, 'colors', {}).get('accent', '#333') if forze else '#333'))
lbl = tk.Label(header, text=self.title or '', bg=header['bg'], fg='white')
lbl.pack(side='left', padx=6)
# close button
def _close():
try:
self.close()
except Exception:
try: self.window.destroy()
except Exception: pass
btn = tk.Button(header, text='✕', bg=header['bg'], fg='white', bd=0, command=_close)
btn.pack(side='right', padx=6)
except Exception:
pass
# override WM_DELETE to run fade-out animation
try:
orig = None
try:
orig = self.window.protocol('WM_DELETE_WINDOW')
except Exception:
orig = None
def _on_close():
if self._closed:
try:
if orig and callable(orig): orig()
except Exception:
pass
return
self._closed = True
try:
_ANIM.fade_out(self.window, duration=0.22, steps=10, on_complete=lambda: self._final_destroy(orig))
except Exception:
self._final_destroy(orig)
try:
self.window.protocol('WM_DELETE_WINDOW', _on_close)
except Exception:
pass
except Exception:
pass
# apply initial opacity
try:
self.apply_opacity_from_config()
except Exception:
pass
# run fade-in if enabled
try:
animations_on = True
if forze:
try:
animations_on = bool(forze.config.get('settings', {}).get('animations', True))
except Exception:
animations_on = True
if animations_on:
try:
_ANIM.fade_in(self.window, duration=0.28, steps=12, target_alpha=float(self.window.wm_attributes('-alpha') if hasattr(self.window, 'wm_attributes') else 1.0))
except Exception:
pass
except Exception:
pass
def _final_destroy(self, orig_protocol=None):
try:
try:
# Always try to unregister this window from ForzeOS taskbar first
try:
if getattr(self, 'forze', None) and hasattr(self.forze, 'unregister_window'):
try:
# primary unregister by object (if used as key)
try:
self.forze.unregister_window(self.window)
except Exception:
pass
# additionally remove any taskbar buttons that reference this window object
try:
tb = getattr(self.forze, '_taskbar_buttons', {}) or {}
for tname, entry in list(tb.items()):
try:
wref = entry.get('window') if isinstance(entry, dict) else None
if wref is self.window:
try:
self.forze.remove_taskbar_button(tname)
except Exception:
pass
except Exception:
pass
except Exception:
pass
# also clear taskbar_icons entries mapping to this window
try:
icons = getattr(self.forze, 'taskbar_icons', {}) or {}
for tname, meta in list(icons.items()):
try:
if meta and meta.get('window') is self.window:
try:
self.forze.remove_taskbar_button(tname)
except Exception:
pass
except Exception:
pass
except Exception:
pass
except Exception:
pass
except Exception:
pass
# Then call original protocol if present, otherwise destroy
if orig_protocol and callable(orig_protocol):
try:
orig_protocol()
except Exception:
try:
self.window.destroy()
except Exception:
pass
else:
try:
self.window.destroy()
except Exception:
pass
except Exception:
try: self.window.destroy()
except Exception: pass
finally:
try:
_WINDOW_BASE_INSTANCES.discard(self)
except Exception:
pass
def set_opacity(self, alpha: float):
try:
if hasattr(self.window, 'wm_attributes'):
try:
self.window.wm_attributes('-alpha', float(alpha))
except Exception:
pass
except Exception:
pass
def apply_opacity_from_config(self):
try:
if not self.forze:
return
cfg = self.forze.config.get('settings', {})
val = cfg.get('opacity_window', None)
if val is None:
val = cfg.get('window_opacity', 1.0)
try:
val = float(val)
except Exception:
val = 1.0
self.set_opacity(val)
except Exception:
pass
# allow attribute proxying to underlying toplevel for compatibility
def __getattr__(self, item):
return getattr(self.window, item)
# Public helper to register an existing Toplevel
def register_window_by_toplevel(toplevel, title=None, forze=None):
try:
# If there's already a WindowBase for this toplevel, skip
for w in list(_WINDOW_BASE_INSTANCES):
try:
if getattr(w, 'window', None) is toplevel:
return w
except Exception:
pass
wb = WindowBase(toplevel, title=title, forze=forze)
return wb
except Exception:
return None
# Apply current settings to all managed windows and widgets
def apply_live_settings(forze):
try:
cfg = getattr(forze, 'config', {}).get('settings', {})
# opacity values
win_op = cfg.get('opacity_window', cfg.get('window_opacity', 1.0))
pop_op = cfg.get('opacity_popup', 0.95)
task_op = cfg.get('opacity_taskbar', 1.0)
try:
win_op = float(win_op)
except Exception:
win_op = 1.0
# update windows
for w in list(_WINDOW_BASE_INSTANCES):
try:
w.set_opacity(win_op)
except Exception:
pass
# update any known popup windows by scanning for Toplevels that are not managed
# (best-effort; callers can also manually set)
except Exception:
pass
# Command palette
class CommandPalette:
def __init__(self, forze):
self.forze = forze
self.window = None
def show(self):
try:
if self.window and getattr(self.window, 'winfo_exists', lambda:False)():
try:
self.window.deiconify()
self.window.lift()
return
except Exception:
pass
root = getattr(self.forze, 'root', None)
if not root:
return
w = tk.Toplevel(root)
w.overrideredirect(False)
w.transient(root)
w.geometry('480x60')
sw = root.winfo_screenwidth()
sh = root.winfo_screenheight()
x = int((sw - 480) / 2)
y = int((sh - 60) / 2)
w.geometry(f'+{x}+{y}')
w.title('Run')
# apply popup opacity
try:
pop_op = float(self.forze.config.get('settings', {}).get('opacity_popup', 0.95))
w.wm_attributes('-alpha', pop_op)
except Exception:
pass
ent = tk.Entry(w, font=('Segoe UI', 12))
ent.pack(fill='both', expand=True, padx=8, pady=8)
ent.focus_set()
def _on_enter(event=None):
cmd = ent.get().strip()
if not cmd:
try: w.destroy()
except Exception: pass
return
# Try engine hooks on ForzeOS
try:
# first: known internal command invocations
if hasattr(self.forze, '_start_menu_invoke'):
try:
self.forze._start_menu_invoke(cmd, name=cmd)
w.destroy()
return
except Exception:
pass
# second: execute as OS/terminal command
if hasattr(self.forze, 'execute_real_command'):
try:
self.forze.execute_real_command(cmd)
w.destroy()
return
except Exception:
pass
# last resorts: call common system methods
lc = cmd.lower()
if lc in ('shutdown', 'poweroff') and hasattr(self.forze, 'shutdown_system'):
try: self.forze.shutdown_system(); w.destroy(); return
except Exception: pass
if lc in ('restart', 'reboot') and hasattr(self.forze, 'restart_system'):
try: self.forze.restart_system(); w.destroy(); return
except Exception: pass
except Exception:
pass
try:
# fallback: spawn subprocess
import subprocess
subprocess.Popen(cmd, shell=True)
except Exception:
pass
try:
w.destroy()
except Exception:
pass
ent.bind('<Return>', _on_enter)
ent.bind('<Escape>', lambda e: w.destroy())
self.window = w
except Exception:
pass
# Simple weather widget embedded into desktop area
class WeatherWidget:
def __init__(self, forze, x=None, y=None):
self.forze = forze
self.parent = getattr(forze, 'desktop', None) or getattr(forze, 'root', None)
self.frame = None
self.x = x
self.y = y
try:
self._create()
# schedule a refresh every 10 minutes
try:
self._schedule_refresh()
except Exception:
pass
except Exception:
pass
def _create(self):
try:
sw = self.forze.screen_width if hasattr(self.forze, 'screen_width') else self.forze.root.winfo_screenwidth()
# nicer compact frame
self.frame = tk.Frame(self.parent, bg='#1f1f1f', bd=0, relief='flat')
# icon + main text area
container = tk.Frame(self.frame, bg=self.frame['bg'])
container.pack(padx=6, pady=6)
self._icon = tk.Label(container, text='⛅', font=('Segoe UI', 18), bg=self.frame['bg'], fg='white')
self._icon.pack(side='left')
textcol = '#ffffff'
txt_frame = tk.Frame(container, bg=self.frame['bg'])
txt_frame.pack(side='left', padx=(8,0))
self._temp = tk.Label(txt_frame, text='--°', font=('Segoe UI', 12, 'bold'), bg=self.frame['bg'], fg=textcol)
self._temp.pack(anchor='w')
self._desc = tk.Label(txt_frame, text='Loading...', font=('Segoe UI', 9), bg=self.frame['bg'], fg='#cfcfcf')
self._desc.pack(anchor='w')
# clickable: open details popup
def _on_click(ev=None):
try:
self._show_details()
except Exception:
pass
for w in (self.frame, container, self._icon, self._temp, self._desc):
try:
w.bind('<Button-1>', _on_click)
w.config(cursor='hand2')
except Exception:
pass
# default position: snug to top-right with small margin
x = self.x if self.x is not None else sw - 12
y = self.y if self.y is not None else 12
try:
if hasattr(self.forze, 'desktop_canvas') and getattr(self.forze, 'desktop_canvas'):
c = self.forze.desktop_canvas
try:
self._canvas_window = c.create_window(x, y, window=self.frame, anchor='ne')
except Exception:
self.frame.place(x=x-200, y=y)
else:
self.frame.place(x=x-200, y=y)
except Exception:
try:
self.frame.pack()
except Exception:
pass
# keep references
self._label = self._desc
self._last_data = None
self.refresh()
except Exception:
pass
def refresh(self):
try:
# quick placeholders
try:
self._icon.config(text='⛅')
self._temp.config(text='--°')
self._desc.config(text='Updating...')
except Exception:
pass
# background fetch (JSON preferred) to populate rich UI
def _worker():
data = None
# prefer JSON
try:
with urllib.request.urlopen('https://wttr.in/?format=j1', timeout=6) as fh:
raw = fh.read().decode('utf-8')
data = json.loads(raw)
except Exception:
data = None
# fallback to simple text if JSON not available
if not data:
txt = None
endpoints = [
'https://wttr.in/?format=%c+%t',
'https://wttr.in/?format=1'
]
for ep in endpoints:
for attempt in range(2):
try:
with urllib.request.urlopen(ep, timeout=4 + attempt*2) as fh:
ttxt = fh.read().decode('utf-8').strip()
if ttxt:
txt = ttxt
break
except Exception:
time.sleep(0.2)
if txt:
break
if txt:
# try to split icon/temperature
try:
parts = txt.split()
icon = parts[0]
temp = ' '.join(parts[1:])
except Exception:
icon = '🌤️'
temp = txt
main = {'icon': icon, 'temp': temp, 'desc': txt}
self._last_data = main
else:
self._last_data = None
else:
try:
cur = data.get('current_condition', [{}])[0]
temp = cur.get('temp_C')
desc = (cur.get('weatherDesc') or [{'value':''}])[0].get('value','')
# choose emoji from desc
icon = self._cond_to_emoji(desc)
self._last_data = {'icon': icon, 'temp': f"{temp}°C" if temp is not None else '', 'desc': desc, 'json': data}
except Exception:
self._last_data = None
# update UI on main thread
try:
def _upd():
try:
if not self._last_data:
self._icon.config(text='N/A')
self._temp.config(text='N/A')
self._desc.config(text='No data')
return
self._icon.config(text=self._last_data.get('icon','⛅'))
self._temp.config(text=self._last_data.get('temp', '--°'))
self._desc.config(text=self._last_data.get('desc',''))
except Exception:
pass
if getattr(self, 'forze', None) and getattr(self.forze, 'root', None):
try:
self.forze.root.after(0, _upd)
except Exception:
_upd()
else:
_upd()
except Exception:
pass
t = threading.Thread(target=_worker, daemon=True)
t.start()
except Exception:
try:
self._label.config(text='Weather: N/A')
except Exception:
pass
def _cond_to_emoji(self, desc: str):
try:
if not desc:
return '🌤️'
d = desc.lower()
if 'sun' in d or 'clear' in d:
return '☀️'
if 'part' in d or 'fair' in d:
return '⛅'
if 'cloud' in d or 'overcast' in d:
return '☁️'
if 'rain' in d or 'shower' in d or 'drizzle' in d:
return '🌧️'
if 'thunder' in d or 'storm' in d:
return '⛈️'
if 'snow' in d or 'sleet' in d:
return '❄️'
return '🌤️'
except Exception:
return '🌤️'
def _show_details(self):
try:
root = getattr(self.forze, 'root', None)
# reuse if exists
if getattr(self, '_details_win', None) and getattr(self._details_win, 'winfo_exists', lambda:False)():
try:
self._details_win.lift()
return
except Exception:
pass
w = tk.Toplevel(root if root else None)
w.title('Weather Details')
w.transient(root)
w.resizable(False, False)
w.geometry('+{}+{}'.format(max(20, (root.winfo_screenwidth()-360) if root else 20), 80))
frame = tk.Frame(w, bg='#1f1f1f', padx=8, pady=8)
frame.pack(fill='both', expand=True)
# populate details
data = getattr(self, '_last_data', None)
if data and data.get('json'):
j = data['json']
# nearest area if available
try:
area = (j.get('nearest_area') or [{}])[0]
name = (area.get('areaName') or [{'value':''}])[0].get('value','')
except Exception:
name = ''
tk.Label(frame, text=(name or 'Weather'), font=('Segoe UI', 11, 'bold'), bg=frame['bg'], fg='white').pack(anchor='w')
tk.Label(frame, text=data.get('desc',''), bg=frame['bg'], fg='#cfcfcf').pack(anchor='w')
# show up to 3-day forecast
try:
days = j.get('weather', [])[:3]
for day in days:
hdr = f"{day.get('date','')} {day.get('maxtempC','')}°/{day.get('mintempC','')}°"
tk.Label(frame, text=hdr, bg=frame['bg'], fg='white').pack(anchor='w')
sub = (day.get('hourly') or [])
if sub:
desc = (sub[0].get('weatherDesc') or [{'value':''}])[0].get('value','')
tk.Label(frame, text=f" {desc}", bg=frame['bg'], fg='#cfcfcf').pack(anchor='w')
except Exception:
pass
else:
# simple text fallback
txt = None
try:
txt = self._last_data.get('desc') if self._last_data else None
except Exception:
txt = None
if txt:
tk.Label(frame, text=txt, bg=frame['bg'], fg='white').pack(anchor='w')
else:
tk.Label(frame, text='Detailed forecast unavailable', bg=frame['bg'], fg='white').pack(anchor='w')
w.bind('<Escape>', lambda e: w.destroy())
self._details_win = w
except Exception:
pass
def _schedule_refresh(self):
try:
self.forze.root.after(10 * 60 * 1000, self._scheduled)
except Exception:
pass
def _scheduled(self):
try:
self.refresh()
self._schedule_refresh()
except Exception:
pass
# Convenient global functions for ForzeOS to call
_CMD_PALETTES = weakref.WeakKeyDictionary()
_WIDGETS = weakref.WeakKeyDictionary()
def show_command_palette(forze):
try:
cp = _CMD_PALETTES.get(forze)
if cp is None:
cp = CommandPalette(forze)
_CMD_PALETTES[forze] = cp
cp.show()
except Exception:
pass
def ensure_weather_widget(forze):
try:
w = _WIDGETS.get(forze)
if w is not None:
return w
# create with retries: wait until desktop_canvas (or desktop) is ready
max_tries = 60
interval_ms = 250
def _attempt(i=0):
try:
if forze is None:
return
canvas = getattr(forze, 'desktop_canvas', None)
root = getattr(forze, 'root', None)
# Consider ready when canvas exists and has a reasonable size, or
# when root is mapped and some desktop attributes exist.
ready = False
try:
if canvas and getattr(canvas, 'winfo_width', lambda:0)() > 120:
ready = True
except Exception:
pass
try:
if not ready and root and root.winfo_ismapped():
ready = True
except Exception:
pass
if ready or i >= max_tries:
try:
w2 = WeatherWidget(forze)
_WIDGETS[forze] = w2
# Try to bring widget to front
try:
if hasattr(w2, 'frame') and w2.frame:
try:
w2.frame.lift()
except Exception:
pass
if hasattr(forze, 'desktop_canvas') and getattr(forze, 'desktop_canvas'):
try:
forze.desktop_canvas.tag_raise(getattr(w2, '_canvas_window', None))
except Exception:
pass
except Exception:
pass
except Exception:
pass
return
# schedule another attempt
try:
if hasattr(forze, 'root') and getattr(forze, 'root'):
forze.root.after(interval_ms, lambda: _attempt(i+1))
else:
threading.Timer(interval_ms/1000.0, lambda: _attempt(i+1)).start()
except Exception:
try:
threading.Timer(interval_ms/1000.0, lambda: _attempt(i+1)).start()
except Exception:
pass
except Exception:
pass
try:
_attempt(0)
except Exception:
pass
return None
except Exception:
return None
# ----------------------- Desktop shortcut helper (Windows) -----------------------
def _get_windows_desktop_path():
try:
# Prefer winshell/Desktop (handles OneDrive and redirected folders)
try:
import winshell
d = Path(winshell.desktop())
if d.exists():
return d
except Exception:
pass
# Fallback to USERPROFILE\Desktop to handle common Windows layouts
up = os.environ.get('USERPROFILE')
if up:
d = Path(up) / 'Desktop'
if d.exists():
return d
except Exception:
pass
# Fallback to home/Desktop
return Path.home() / 'Desktop'
def create_windows_shortcut(target, shortcut_path, working_dir=None, icon=None, hotkey=None, description=''):
"""Create a Windows .lnk shortcut. Tries pywin32 (WScript.Shell) first, then winshell if available.
Returns True on success, False otherwise.
"""
try:
import shutil
targ_str = str(target)
targ_path = None
try:
targ_path = Path(targ_str)
except Exception:
targ_path = None
# Detect Python script targets and prepare launcher+args
is_script = False
try:
if targ_path and targ_path.suffix.lower() in ('.py', '.pyw'):
is_script = True
except Exception:
is_script = False
if is_script:
# Prefer the py launcher, then pythonw/python; otherwise fall back to cmd+python
launcher = shutil.which('py') or shutil.which('pythonw.exe') or shutil.which('python.exe')
if launcher:
lnk_target = str(launcher)
lnk_args = f'"{targ_str}"'
else:
# Use cmd.exe to invoke the user's python alias
lnk_target = str(Path(os.environ.get('WINDIR', 'C:\\Windows')) / 'System32' / 'cmd.exe')
lnk_args = f'/c python "{targ_str}"'
else:
lnk_target = targ_str
lnk_args = ''
# Try pywin32 first
try:
from win32com.client import Dispatch
shell = Dispatch('WScript.Shell')
lnk = shell.CreateShortcut(str(shortcut_path))
lnk.TargetPath = lnk_target
if lnk_args:
try:
lnk.Arguments = lnk_args
except Exception:
pass
if working_dir:
lnk.WorkingDirectory = str(working_dir)
if icon:
try:
lnk.IconLocation = str(icon)
except Exception:
pass
if description:
try:
lnk.Description = description
except Exception:
pass
if hotkey:
try:
lnk.Hotkey = hotkey
except Exception:
pass
try:
lnk.Save()
except Exception:
try:
lnk.save()
except Exception:
raise
return True
except Exception as e_pywin:
# Fallback to winshell, using the same launcher+arguments logic
try:
import winshell
launcher = None
if is_script:
launcher = shutil.which('py') or shutil.which('pythonw.exe') or shutil.which('python.exe')
if launcher:
winshell.CreateShortcut(str(shortcut_path), str(launcher), Arguments=f'"{targ_str}"', StartIn=str(working_dir) if working_dir else '', Icon=(str(icon) if icon else '', 0), Description=description)
else:
cmd = str(Path(os.environ.get('WINDIR', 'C:\\Windows')) / 'System32' / 'cmd.exe')
winshell.CreateShortcut(str(shortcut_path), cmd, Arguments=f'/c python "{targ_str}"', StartIn=str(working_dir) if working_dir else '', Icon=(str(icon) if icon else '', 0), Description=description)
else:
kw = {}
if working_dir:
kw['StartIn'] = str(working_dir)
if icon:
kw['Icon'] = (str(icon), 0) if not isinstance(icon, tuple) else icon
if description:
kw['Description'] = description
winshell.CreateShortcut(str(shortcut_path), str(target), Arguments='', **kw)
return True
except Exception as e_winsh:
logger.debug('pywin32 error: %s', e_pywin)
logger.debug('winshell error: %s', e_winsh)
return False
except Exception:
logger.exception('create_windows_shortcut failed')
return False
def ensure_desktop_shortcut_prompt(name='ForzeOS', hotkey='Ctrl+Alt+F', icon_path=None, forze=None):
"""If running on Windows, ask for (or respect) a stored permission to
create a desktop shortcut for `name`. If permission is granted, attempt
to create the .lnk and assign the requested hotkey.
Behaviour:
- If `forze` is provided, read/write the setting at
`forze.config['settings']['allow_desktop_shortcuts']` and call
`forze.save_config()` to persist changes.
- If the setting is True, create the shortcut immediately.
- If the setting is False, do nothing.
- If the setting is missing/None, prompt the user once and persist
their response.
Returns True if a shortcut was created, False otherwise.
"""
try:
if not sys.platform.startswith('win'):
return False
# Read stored permission (prefer ForzeOS instance if provided)
perm = None
try:
if forze is not None:
perm = forze.config.get('settings', {}).get('allow_desktop_shortcuts', None)
else:
# best-effort: try to read a nearby config file if present
try:
cfg_path = Path(__file__).parent.parent / 'forzeos_config.json'
if cfg_path.exists():
with open(cfg_path, 'r', encoding='utf-8') as f:
cfg = json.load(f)
perm = cfg.get('settings', {}).get('allow_desktop_shortcuts', None)
except Exception:
perm = None
except Exception:
perm = None
# If explicitly denied, stop
if perm is False:
return False
desktop = _get_windows_desktop_path()
shortcut_file = desktop / f"{name}.lnk"
# Helper to determine target and icon (reuse existing logic)
try:
if getattr(sys, 'frozen', False):
target = Path(sys.executable).resolve()
else:
target = Path(sys.argv[0]).resolve()
except Exception:
target = Path.cwd()
icon = None
if icon_path:
icon = Path(icon_path)
else:
try:
cand = Path(__file__).with_name('..').resolve() / 'assets' / 'icons' / 'FORZE_CYBER.png'
if cand.exists():
icon = cand
except Exception:
icon = None
def _create_shortcut():
ok = create_windows_shortcut(target=target, shortcut_path=shortcut_file, working_dir=target.parent if target.is_file() else None, icon=icon, hotkey=hotkey, description=name)
if ok:
logger.info('Desktop shortcut created: %s', shortcut_file)
else:
logger.warning('Failed to create desktop shortcut: %s', shortcut_file)
return bool(ok)
# If already allowed, create and return (use persisted helper that handles existing files)
if perm is True:
return create_desktop_shortcut_and_persist(forze, name=name, hotkey=hotkey, icon_path=icon_path)
# Permission unknown — ask the user and persist choice
created_root = False
try:
root = tk._default_root
if not root:
root = tk.Tk()
root.withdraw()
created_root = True
except Exception:
root = None
resp = False
try:
resp = messagebox.askyesno('Masaüstü Kısayolu', 'ForzeOS masaüstüne bir kısayol oluşturmak istiyor. İzin verilsin mi? (Bu ayarı sonradan Ayarlar uygulamasından değiştirebilirsiniz.)')
except Exception:
resp = False
if created_root and root:
try:
root.destroy()
except Exception:
pass
# Persist the user's decision if we have a ForzeOS instance
try:
if forze is not None:
s = forze.config.setdefault('settings', {})
s['allow_desktop_shortcuts'] = bool(resp)
try:
forze.save_config()
except Exception:
pass
else:
try:
cfg_path = Path(__file__).parent.parent / 'forzeos_config.json'
if cfg_path.exists():
with open(cfg_path, 'r', encoding='utf-8') as f:
cfg = json.load(f)
else:
cfg = {}
cfg.setdefault('settings', {})['allow_desktop_shortcuts'] = bool(resp)
with open(cfg_path, 'w', encoding='utf-8') as f:
json.dump(cfg, f, indent=2, ensure_ascii=False)
except Exception:
pass
except Exception:
pass
if not resp:
return False
return create_desktop_shortcut_and_persist(forze, name=name, hotkey=hotkey, icon_path=icon_path)
except Exception:
logger.exception('ensure_desktop_shortcut_prompt failed')
return False
def create_desktop_shortcut_and_persist(forze, name='ForzeOS', hotkey='Ctrl+Alt+F', icon_path=None):
"""Create a desktop shortcut immediately and persist the permission in
`forze.config['settings']['allow_desktop_shortcuts'] = True` if successful.