-
Notifications
You must be signed in to change notification settings - Fork 0
/
send.pyw
1061 lines (956 loc) · 37.8 KB
/
send.pyw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import socket
import flet as ft
import subprocess
import threading
import os
import pyperclip
import re
import sys
import atexit
import time
import asyncio
import humanize
from receive import ReceiveApp
class CrocApp:
def __init__(self):
self.receive_text = None
self.output_text = []
self.current_process = None
self.process_running = False
self.output_control = None
self.page = None
self.selected_files = []
self.file_picker = None
self.file_list = None
self.croc_code = None
self.add_files_button = None
self.copy_button = None
self.send_button = None
self.code_text = None
self.output_expander = None
self.terminate_button = None
self.current_view = "send"
self.send_toggle = None
self.receive_toggle = None
self.main_column = None
self.code_note = None
self.terminate_button_visible = False
self.receive_app = None
atexit.register(self.cleanup)
def cleanup(self):
if self.current_process:
try:
self.current_process.terminate()
self.current_process.wait(timeout=1)
except:
if self.current_process:
self.current_process.kill()
self.close_croc_instances()
def close_croc_instances(self):
if sys.platform == "win32":
os.system("taskkill /F /IM croc.exe 2>NUL")
else:
os.system("pkill -9 croc 2>/dev/null")
def check_winget_available(self):
try:
subprocess.run(["winget", "--version"], capture_output=True, check=True)
return True
except (subprocess.CalledProcessError, FileNotFoundError):
return False
def check_curl_available(self):
try:
subprocess.run(["curl", "--version"], capture_output=True, check=True)
return True
except (subprocess.CalledProcessError, FileNotFoundError):
return False
def close_dialog(self, dialog):
dialog.open = False
self.page.update()
async def check_internet_connection(self):
"""Check if there's an internet connection available."""
try:
# Create and show loading dialog
loading_dialog = ft.AlertDialog(
modal=True,
title=ft.Text(
"Checking Connection", size=20, weight=ft.FontWeight.BOLD
),
content=ft.Column(
controls=[
ft.Container(
content=ft.Column(
controls=[
ft.ProgressRing(width=40, height=40),
ft.Container(height=10),
ft.Text(
"Checking internet connection...",
size=16,
text_align=ft.TextAlign.CENTER,
),
],
horizontal_alignment=ft.CrossAxisAlignment.CENTER,
),
padding=20,
)
],
horizontal_alignment=ft.CrossAxisAlignment.CENTER,
),
)
self.page.overlay.append(loading_dialog)
loading_dialog.open = True
self.page.update()
# Try to connect to a reliable host
socket.create_connection(("8.8.8.8", 53), timeout=3)
# Close loading dialog
loading_dialog.open = False
self.page.update()
return True
except OSError:
# Close loading dialog
loading_dialog.open = False
self.page.update()
# Show error dialog
error_dialog = ft.AlertDialog(
modal=True,
title=ft.Text(
"No Internet Connection", size=20, weight=ft.FontWeight.BOLD
),
content=ft.Column(
controls=[
ft.Icon(
name=ft.icons.WIFI_OFF_ROUNDED,
color=ft.colors.RED,
size=40,
),
ft.Container(height=10),
ft.Text(
"Please check your internet connection and try again.",
size=16,
text_align=ft.TextAlign.CENTER,
),
],
horizontal_alignment=ft.CrossAxisAlignment.CENTER,
),
actions=[
ft.TextButton("OK", on_click=lambda _: sys.exit()),
],
actions_alignment=ft.MainAxisAlignment.END,
)
self.page.overlay.append(error_dialog)
error_dialog.open = True
self.page.update()
return False
async def check_croc_installed(self):
"""Check if croc is installed on the system."""
loading_dialog = ft.AlertDialog(
modal=True,
title=ft.Text("Checking Installation", size=20, weight=ft.FontWeight.BOLD),
content=ft.Column(
controls=[
ft.Container(
content=ft.Column(
controls=[
ft.ProgressRing(width=40, height=40),
ft.Container(height=10),
ft.Text(
"Checking if Croc is installed...",
size=16,
text_align=ft.TextAlign.CENTER,
),
],
horizontal_alignment=ft.CrossAxisAlignment.CENTER,
),
padding=20,
)
],
horizontal_alignment=ft.CrossAxisAlignment.CENTER,
),
)
self.page.overlay.append(loading_dialog)
loading_dialog.open = True
self.page.update()
try:
# Run the command asynchronously
process = await asyncio.create_subprocess_exec(
"croc",
"--version",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
try:
# Wait for the process to complete with a timeout
await asyncio.wait_for(process.communicate(), timeout=5.0)
loading_dialog.open = False
self.page.update()
if process.returncode == 0:
return True
else:
await self.install_croc()
return False
except asyncio.TimeoutError:
# If the process times out, assume croc is not installed
loading_dialog.open = False
self.page.update()
await self.install_croc()
return False
except FileNotFoundError:
loading_dialog.open = False
self.page.update()
await self.install_croc()
return False
except Exception as e:
loading_dialog.open = False
self.page.update()
error_dialog = ft.AlertDialog(
modal=True,
title=ft.Text(
"Installation Check Failed", size=20, weight=ft.FontWeight.BOLD
),
content=ft.Column(
controls=[
ft.Icon(
name=ft.icons.ERROR_OUTLINE, color=ft.colors.RED, size=40
),
ft.Container(height=10),
ft.Text(
f"An error occurred while checking Croc installation:\n{str(e)}",
size=16,
text_align=ft.TextAlign.CENTER,
),
],
horizontal_alignment=ft.CrossAxisAlignment.CENTER,
),
actions=[
ft.TextButton(
"OK", on_click=lambda _: setattr(error_dialog, "open", False)
),
],
actions_alignment=ft.MainAxisAlignment.END,
)
self.page.overlay.append(error_dialog)
error_dialog.open = True
self.page.update()
return False
async def install_croc(self):
def close_dialog(dialog):
dialog.open = False
self.page.update()
pass
def handle_installation():
try:
if sys.platform == "win32":
if not self.check_winget_available():
raise Exception("Winget is not available on this system.")
subprocess.run(["winget", "install", "schollz.croc"], check=True)
else:
if not self.check_curl_available():
raise Exception("Curl is not available on this system.")
# Ask for sudo password
sudo_password = self.get_sudo_password()
if sudo_password is None:
raise Exception("Sudo password is required for installation.")
command = "curl https://getcroc.schollz.com | sudo -S bash"
process = subprocess.Popen(
command,
shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
stdout, stderr = process.communicate(input=f"{sudo_password}\n")
if process.returncode != 0:
raise Exception(f"Installation failed: {stderr}")
# Check if installation was successful
if self.check_croc_installed():
success_dialog = ft.AlertDialog(
modal=True,
title=ft.Text("Success"),
content=ft.Text("Croc has been successfully installed!"),
actions=[
ft.TextButton(
"OK", on_click=lambda _: close_dialog(success_dialog)
),
],
actions_alignment=ft.MainAxisAlignment.END,
)
self.page.overlay.append(success_dialog)
success_dialog.open = True
self.page.update()
else:
raise Exception("Installation verification failed")
except Exception as e:
error_dialog = ft.AlertDialog(
modal=True,
title=ft.Text("Installation Failed"),
content=ft.Text(
f"Failed to install Croc: {str(e)}\n"
"Please install it manually:\n"
"Windows: winget install schollz.croc\n"
"Linux/MacOS: curl https://getcroc.schollz.com | sudo bash\n"
"For more info, go to https://github.com/schollz/croc"
),
actions=[
ft.TextButton(
"OK", on_click=lambda _: close_dialog(error_dialog)
),
],
actions_alignment=ft.MainAxisAlignment.END,
)
self.page.overlay.append(error_dialog)
error_dialog.open = True
self.page.update()
install_dialog = ft.AlertDialog(
modal=True,
title=ft.Text("Croc Not Found"),
content=ft.Text(
"Croc is not installed on your system. Would you like to install it now?"
),
actions=[
ft.TextButton(
"Yes",
on_click=lambda _: (
close_dialog(install_dialog),
handle_installation(),
),
),
ft.TextButton(
"No", on_click=lambda _: (close_dialog(install_dialog), sys.exit())
),
],
actions_alignment=ft.MainAxisAlignment.END,
)
self.page.overlay.append(install_dialog)
install_dialog.open = True
self.page.update()
def get_sudo_password(self):
def close_dialog(dialog):
dialog.open = False
self.page.update()
password_input = ft.TextField(
label="Sudo Password", password=True, can_reveal_password=True
)
def on_submit(_):
close_dialog(password_dialog)
self.sudo_password = password_input.value
password_dialog = ft.AlertDialog(
modal=True,
title=ft.Text("Sudo Password Required"),
content=ft.Column(
[
ft.Text("Please enter your sudo password to install Croc:"),
password_input,
]
),
actions=[
ft.TextButton("Submit", on_click=on_submit),
ft.TextButton(
"Cancel", on_click=lambda _: close_dialog(password_dialog)
),
],
actions_alignment=ft.MainAxisAlignment.END,
)
self.sudo_password = None
self.page.overlay.append(password_dialog)
password_dialog.open = True
self.page.update()
# Wait for the dialog to close
while password_dialog.open:
time.sleep(0.1)
return self.sudo_password
def get_file_icon(self, file_path):
file_extension = os.path.splitext(file_path)[1].lower()
if file_extension in [".txt", ".doc", ".docx", ".pdf"]:
return ft.icons.DESCRIPTION
elif file_extension in [".jpg", ".jpeg", ".png", ".gif", ".bmp"]:
return ft.icons.IMAGE
elif file_extension in [".mp3", ".wav", ".ogg"]:
return ft.icons.AUDIO_FILE
elif file_extension in [".mp4", ".avi", ".mov"]:
return ft.icons.VIDEO_FILE
elif file_extension in [".zip", ".rar", ".7z"]:
return ft.icons.FOLDER_ZIP
else:
return ft.icons.INSERT_DRIVE_FILE
def switch_view(self, view):
self.current_view = view
if view == "send":
self.send_toggle.style.color = ft.colors.ON_SURFACE
self.send_toggle.style.bgcolor = ft.colors.TEAL
self.receive_toggle.style.bgcolor = ft.colors.BACKGROUND
self.receive_toggle.style.color = ft.colors.ON_SURFACE_VARIANT
self.main_column.visible = True
self.receive_text.visible = False
else: # receive view
self.send_toggle.style.color = ft.colors.ON_SURFACE_VARIANT
self.send_toggle.style.bgcolor = ft.colors.BACKGROUND
self.receive_toggle.style.bgcolor = ft.colors.TEAL
self.receive_toggle.style.color = ft.colors.ON_SURFACE
self.main_column.visible = False
self.receive_text.visible = True
self.page.update()
def toggle_switch_view_buttons(self, enabled):
if self.send_toggle and self.receive_toggle:
self.send_toggle.disabled = not enabled
self.receive_toggle.disabled = not enabled
self.page.update()
def run_croc_command(self, command):
try:
self.process_running = True
self.toggle_buttons(False)
self.toggle_switch_view_buttons(False)
print(f"Starting command: {command}")
self.current_process = subprocess.Popen(
command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
universal_newlines=True,
)
full_output = ""
for line in iter(self.current_process.stdout.readline, ""):
print(f"Raw output: {line}")
full_output += line
if line.strip():
self.output_text.append(line.strip())
if "Code is:" in line:
code_match = re.search(r"Code is: (.+)", line)
if code_match:
self.croc_code = code_match.group(1)
self.update_code_display()
if self.page:
self.page.update()
self.update_output()
self.current_process.wait()
self.output_text.append("Command completed.")
# Check different scenarios
if "cannot find the path" in full_output:
error_dialog = ft.AlertDialog(
modal=True,
title=ft.Text("Error"),
content=ft.Text("File not found. Please check if the file exists."),
actions=[
ft.TextButton(
"OK", on_click=lambda _: self.close_dialog(error_dialog)
),
],
)
self.page.overlay.append(error_dialog)
error_dialog.open = True
elif "peer error: refusing files" in full_output:
error_dialog = ft.AlertDialog(
modal=True,
title=ft.Text("Transfer Refused"),
content=ft.Text("The recipient refused the file transfer."),
actions=[
ft.TextButton(
"OK", on_click=lambda _: self.close_dialog(error_dialog)
),
],
)
self.page.overlay.append(error_dialog)
error_dialog.open = True
elif "100% |" in full_output:
success_dialog = ft.AlertDialog(
modal=True,
title=ft.Text("Success"),
content=ft.Text("File transfer completed successfully!"),
actions=[
ft.TextButton(
"OK", on_click=lambda _: self.close_dialog(success_dialog)
),
],
)
self.page.overlay.append(success_dialog)
success_dialog.open = True
# Clear UI elements after transfer
self.code_text.visible = False
self.copy_button.visible = False
self.code_note.visible = False
self.selected_files.clear()
self.update_file_list()
self.page.update()
if self.page:
self.page.update()
self.update_output()
except Exception as e:
error_message = f"Error: {str(e)}"
print(error_message)
self.output_text.append(error_message)
if self.page:
self.page.update()
self.update_output()
finally:
self.process_running = False
self.toggle_buttons(True)
self.toggle_switch_view_buttons(True)
self.current_process = None
def toggle_buttons(self, enabled):
if self.add_files_button and self.send_button:
self.add_files_button.disabled = not enabled
self.send_button.disabled = not enabled
self.terminate_button.disabled = enabled
self.terminate_button.visible = not enabled
self.toggle_switch_view_buttons(enabled)
self.page.update()
def update_code_display(self):
if self.code_text and self.croc_code:
self.code_text.value = f"Code: {self.croc_code}"
self.code_text.visible = True
self.copy_button.visible = True
self.code_note.value = (
"The copy button copies the code.\n"
"If you want to use the code in CLI, use the following:\n"
f"Windows: croc {self.croc_code}\n"
f'Linux/MacOS: CROC_SECRET="{self.croc_code}" croc'
)
self.code_note.visible = True
self.code_text.update()
self.copy_button.update()
self.code_note.update()
def start_croc_command(self, e):
if not self.process_running:
if not self.selected_files:
self.page.open(
ft.SnackBar(content=ft.Text("Please select files to send."))
)
return
self.output_text = ["Starting new command..."]
self.update_output()
file_paths = " ".join(f'"{file}"' for file in self.selected_files)
command = f"croc send {file_paths}"
threading.Thread(
target=self.run_croc_command, args=(command,), daemon=True
).start()
else:
self.page.open(
ft.SnackBar(
content=ft.Text(
"A command is already running. Please wait for it to finish."
)
)
)
def toggle_theme(self, e):
if self.page.theme_mode == ft.ThemeMode.LIGHT:
self.page.theme_mode = ft.ThemeMode.DARK
self.update_background()
else:
self.page.theme_mode = ft.ThemeMode.LIGHT
self.update_background()
self.page.update()
def update_background(self):
if self.page.theme_mode == ft.ThemeMode.LIGHT:
self.page.bgcolor = ft.colors.TEAL_50
else:
self.page.bgcolor = None
self.page.update()
def pick_files_result(self, e: ft.FilePickerResultEvent):
if not e.files:
return
# Add only files that aren't already in the list
new_files = [
file.path for file in e.files if file.path not in self.selected_files
]
self.selected_files.extend(new_files)
self.update_file_list()
def check_croc_running(self):
if sys.platform == "win32":
cmd = 'tasklist /FI "IMAGENAME eq croc.exe" /NH'
output = subprocess.check_output(cmd, shell=True).decode()
return "croc.exe" in output
else:
try:
cmd = "pgrep croc"
subprocess.check_output(cmd, shell=True)
return True
except subprocess.CalledProcessError:
return False
def show_croc_running_dialog(self):
def close_croc():
if sys.platform == "win32":
os.system("taskkill /F /IM croc.exe 2>NUL")
else:
os.system("pkill -9 croc 2>/dev/null")
dialog.open = False
self.page.update()
def close_warning_dialog(warning_dialog):
warning_dialog.open = False
self.page.update()
def show_warning():
dialog.open = False
self.page.update()
warning_dialog = ft.AlertDialog(
modal=True,
title=ft.Text("Warning"),
content=ft.Text(
"Multiple croc sessions running in background may impact system performance."
),
actions=[
ft.TextButton(
"OK",
on_click=lambda _: close_warning_dialog(warning_dialog),
),
],
actions_alignment=ft.MainAxisAlignment.END,
)
self.page.overlay.append(warning_dialog)
warning_dialog.open = True
# self.page.dialog = warning_dialog
self.page.update()
dialog = ft.AlertDialog(
modal=True,
title=ft.Text("Croc Process Running"),
content=ft.Text(
"A Croc process is already running in the background. "
"This might interfere with the application. "
"Would you like to close it?"
),
actions=[
ft.TextButton("Yes", on_click=lambda _: close_croc()),
ft.TextButton("No", on_click=lambda _: show_warning()),
],
actions_alignment=ft.MainAxisAlignment.END,
)
# self.page.dialog = dialog
self.page.overlay.append(dialog)
dialog.open = True
self.page.update()
def update_file_list(self):
self.file_list.controls.clear()
for index, file in enumerate(self.selected_files):
try:
file_size = os.path.getsize(file)
human_readable_size = humanize.naturalsize(file_size)
file_item = ft.Container(
content=ft.Column(
[
ft.Row(
[
ft.Icon(
self.get_file_icon(file),
color=ft.colors.ON_SURFACE_VARIANT,
),
ft.Column(
[
ft.Text(
os.path.basename(file),
size=14,
overflow=ft.TextOverflow.ELLIPSIS,
),
ft.Text(
human_readable_size,
size=12,
color=ft.colors.ON_SURFACE_VARIANT,
weight=ft.FontWeight.W_300,
),
],
spacing=2,
expand=True,
),
ft.IconButton(
icon=ft.icons.DELETE_OUTLINE,
on_click=lambda _, file=file: self.remove_file(
file
),
tooltip="Remove file",
icon_color=ft.colors.ON_SURFACE_VARIANT,
),
],
alignment=ft.MainAxisAlignment.SPACE_BETWEEN,
),
# Add divider if not the last item
(
ft.Divider(
height=1,
color=ft.colors.with_opacity(
0.1, ft.colors.ON_SURFACE
),
)
if index < len(self.selected_files) - 1
else ft.Container()
),
],
),
padding=ft.padding.symmetric(horizontal=10, vertical=5),
border_radius=8,
ink=True, # Adds ripple effect on click
)
self.file_list.controls.append(file_item)
except OSError as e:
print(f"Error accessing file {file}: {e}")
continue
self.page.update()
def remove_file(self, file):
self.selected_files.remove(file)
self.update_file_list()
def update_output(self):
self.output_control.controls = [
ft.Text(line, size=12) for line in self.output_text[-100:]
]
self.output_control.update()
def copy_code_to_clipboard(self, e):
if self.croc_code:
clipboard_text = self.croc_code
pyperclip.copy(clipboard_text)
self.page.open(ft.SnackBar(content=ft.Text("Code copied to clipboard!")))
else:
self.page.open(ft.SnackBar(content=ft.Text("No code available to copy.")))
def terminate_process(self, e):
if self.current_process:
self.code_text.visible = False
self.terminate_button.visible = False
self.copy_button.visible = False
self.code_note.visible = False
self.code_text.update()
self.copy_button.update()
self.code_note.update()
try:
self.current_process.terminate()
self.current_process.wait(timeout=1)
except:
if self.current_process:
self.current_process.kill()
self.output_text.append("Process terminated.")
self.update_output()
self.process_running = False
self.toggle_buttons(True)
self.current_process = None
if sys.platform == "win32":
os.system("taskkill /F /IM croc.exe 2>NUL")
else:
os.system("pkill -9 croc 2>/dev/null")
async def main(self, page: ft.Page):
self.page = page
page.window.width = 480
page.window.height = 700
page.window.resizable = False
page.window.maximizable = False
page.scroll = "adaptive"
page.title = "Croc GUI"
page.theme_mode = ft.ThemeMode.DARK
page.theme = ft.Theme(
color_scheme=ft.ColorScheme(
primary=ft.colors.TEAL,
)
)
page.window.prevent_close = False
page.window.icon = "./assets/crocodile.svg"
page.on_close = self.cleanup
self.receive_app = ReceiveApp(page, self.toggle_switch_view_buttons)
self.receive_text = self.receive_app.create_receive_layout()
self.receive_text.visible = False
if not await self.check_internet_connection():
return
if not await self.check_croc_installed():
self.install_croc()
else:
# Continue with the rest of your initialization
if self.check_croc_running():
self.show_croc_running_dialog()
if self.check_croc_running():
self.page = page
self.show_croc_running_dialog()
# File Picker
self.file_picker = ft.FilePicker(on_result=self.pick_files_result)
page.overlay.append(self.file_picker)
# Title Row with Toggle
title_row = ft.Row(
[
ft.Image(
src="./assets/crocodile.svg",
width=30,
height=30,
color=ft.colors.ON_SURFACE,
),
ft.Text("Croc GUI", size=24, weight=ft.FontWeight.BOLD),
ft.IconButton(
icon=ft.icons.LIGHT_MODE,
on_click=self.toggle_theme,
tooltip="Toggle theme",
),
],
alignment=ft.MainAxisAlignment.SPACE_BETWEEN,
)
# Toggle Buttons Row
self.send_toggle = ft.TextButton(
text="Send",
style=ft.ButtonStyle(
color=ft.colors.ON_SURFACE,
),
on_click=lambda _: self.switch_view("send"),
)
self.receive_toggle = ft.TextButton(
text="Receive",
style=ft.ButtonStyle(
color=ft.colors.ON_SURFACE_VARIANT,
),
on_click=lambda _: self.switch_view("receive"),
)
toggle_row = ft.Row(
[
self.send_toggle,
ft.Text("|", size=16, color=ft.colors.ON_SURFACE_VARIANT),
self.receive_toggle,
],
alignment=ft.MainAxisAlignment.CENTER,
)
# Files Label
files_label = ft.Text("Selected Files:", size=16, weight=ft.FontWeight.BOLD)
title_send = ft.Text("Send Files", size=24, weight=ft.FontWeight.BOLD)
self.add_files_button = ft.ElevatedButton(
"Add Files",
icon=ft.icons.ADD_ROUNDED,
on_click=lambda _: self.file_picker.pick_files(allow_multiple=True),
style=ft.ButtonStyle(
shape=ft.RoundedRectangleBorder(radius=8),
),
)
self.file_list = ft.Column(
scroll=ft.ScrollMode.AUTO,
height=150,
width=440,
)
file_list_container = ft.Container(
content=self.file_list,
bgcolor=ft.colors.with_opacity(0.1, ft.colors.ON_SURFACE),
padding=10,
border_radius=8,
width=440,
)
self.send_button = ft.ElevatedButton(
"Send",
icon=ft.icons.SEND_ROUNDED,
on_click=self.start_croc_command,
style=ft.ButtonStyle(
color=ft.colors.ON_PRIMARY,
bgcolor={ft.ControlState.DEFAULT: ft.colors.TEAL},
shape=ft.RoundedRectangleBorder(radius=8),
),
)
# Code Display Section
self.code_text = ft.Text(
"Code: ", size=16, weight=ft.FontWeight.BOLD, visible=False
)
self.copy_button = ft.IconButton(
icon=ft.icons.COPY,
on_click=self.copy_code_to_clipboard,
tooltip="Copy code",
visible=False,
)
self.code_note = ft.Text(
size=12,
color=ft.colors.ON_SURFACE_VARIANT,
italic=True,
visible=False,
selectable=True,
)
code_container = ft.Column(
[
ft.Row(
[self.code_text, self.copy_button],
alignment=ft.MainAxisAlignment.START,
),
self.code_note,
],
spacing=5,
)
# Terminate Button
self.terminate_button = ft.IconButton(
icon=ft.icons.CLOSE,
icon_color=ft.colors.RED,
on_click=self.terminate_process,
tooltip="Terminate process",
disabled=True,
visible=False,
)
# Output Section with Expander
self.output_control = ft.Column(
scroll=ft.ScrollMode.AUTO, height=300, spacing=2
)
self.output_container = ft.Container(
content=self.output_control,
bgcolor=ft.colors.with_opacity(0.1, ft.colors.ON_SURFACE),
padding=10,
border_radius=8,
width=440,
)
self.main_column = ft.Column(
[
ft.Container(
content=ft.Column(
[
ft.Text("Send Files", size=24, weight=ft.FontWeight.BOLD),
ft.Container(height=10),
files_label,
ft.Container(height=5),
ft.Row(
[
self.add_files_button,
ft.Container(width=10),
ft.Text(
"Click to add files for sending",
size=12,
italic=True,
color=ft.colors.ON_SURFACE_VARIANT,
),
],
alignment=ft.MainAxisAlignment.START,
),
ft.Container(height=10),
file_list_container,