-
Notifications
You must be signed in to change notification settings - Fork 195
/
main.py
147 lines (124 loc) · 4.39 KB
/
main.py
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
import logging
import os
from itertools import islice
import flet
from flet import (
Column,
Container,
GridView,
Icon,
IconButton,
Page,
Row,
SnackBar,
Text,
TextButton,
TextField,
UserControl,
alignment,
colors,
icons,
)
# logging.basicConfig(level=logging.INFO)
os.environ["FLET_WS_MAX_MESSAGE_SIZE"] = "8000000"
class IconBrowser(UserControl):
def __init__(self, expand=False, height=500):
super().__init__()
if expand:
self.expand = expand
else:
self.height = height
def build(self):
def batches(iterable, batch_size):
iterator = iter(iterable)
while batch := list(islice(iterator, batch_size)):
yield batch
# fetch all icon constants from icons.py module
icons_list = []
list_started = False
for key, value in vars(icons).items():
if key == "TEN_K":
list_started = True
if list_started and isinstance(value, str):
icons_list.append(value)
search_txt = TextField(
expand=1,
hint_text="Enter keyword and press search button",
autofocus=True,
on_submit=lambda e: display_icons(e.control.value),
)
def search_click(e):
display_icons(search_txt.value)
search_query = Row(
[search_txt, IconButton(icon=icons.SEARCH, on_click=search_click)]
)
search_results = GridView(
expand=1,
runs_count=10,
max_extent=150,
spacing=5,
run_spacing=5,
child_aspect_ratio=1,
)
status_bar = Text()
def copy_to_clipboard(e):
icon_key = e.control.data
print("Copy to clipboard:", icon_key)
self.page.set_clipboard(e.control.data)
self.page.show_snack_bar(SnackBar(Text(f"Copied {icon_key}"), open=True))
def search_icons(search_term: str):
for icon_name in icons_list:
if search_term != "" and search_term in icon_name:
yield icon_name
def display_icons(search_term: str):
# clean search results
search_query.disabled = True
self.update()
search_results.clean()
for batch in batches(search_icons(search_term.lower()), 200):
for icon_name in batch:
icon_key = f"icons.{icon_name.upper()}"
search_results.controls.append(
TextButton(
content=Container(
content=Column(
[
Icon(name=icon_name, size=30),
Text(
value=f"{icon_name}",
size=12,
width=100,
no_wrap=True,
text_align="center",
color=colors.ON_SURFACE_VARIANT,
),
],
spacing=5,
alignment="center",
horizontal_alignment="center",
),
alignment=alignment.center,
),
tooltip=f"{icon_key}\nClick to copy to a clipboard",
on_click=copy_to_clipboard,
data=icon_key,
)
)
status_bar.value = f"Icons found: {len(search_results.controls)}"
self.update()
if len(search_results.controls) == 0:
self.page.show_snack_bar(SnackBar(Text("No icons found"), open=True))
search_query.disabled = False
self.update()
return Column(
[
search_query,
search_results,
status_bar,
],
expand=True,
)
def main(page: Page):
page.title = "Flet icons browser"
page.add(IconBrowser(expand=True))
flet.app(target=main)