-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdearpygui_wrapper.py
407 lines (350 loc) · 11.1 KB
/
dearpygui_wrapper.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
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
"""Wrapper class for Dear PyGui."""
from __future__ import annotations
from pathlib import Path
from typing import Any, Callable
import dearpygui.dearpygui as dpg
from cli2gui.gui import helpers
from cli2gui.gui.abstract_gui import AbstractGUI
from cli2gui.models import SEP, FullBuildSpec, Group, Item, ItemType
THISDIR = Path(__file__).resolve().parent
def hex_to_rgb(hex_code: str) -> tuple[int, int, int, int]:
"""Convert a color hex code to a tuple of integers (r, g, b)."""
hex_code = hex_code.lstrip("#")
r = int(hex_code[0:2], 16)
g = int(hex_code[2:4], 16)
b = int(hex_code[4:6], 16)
return (r, g, b, 255)
class DearPyGuiWrapper(AbstractGUI):
"""Wrapper class for Dear PyGui."""
def __init__(self, base24Theme: list[str]) -> None:
"""Dearpygui wrapper class.
:param list[str] base24Theme: list representing a base24 theme. Containing 24 elements
(of hex strings like "#e7e7e9")
"""
self.base24Theme = base24Theme
super().__init__()
def _helpText(self, item: Item) -> None:
dpg.add_text(
helpers.stringSentencecase(f"\n- {item.dest}: {item.commands}"),
color=hex_to_rgb(self.base24Theme[13]),
)
dpg.add_text(helpers.stringSentencecase(item.help))
def _helpFlagWidget(self, item: Item) -> None:
with dpg.group(horizontal=False):
self._helpText(item)
dpg.add_checkbox(tag=item.dest, default_value=(item.default or False))
def _helpTextWidget(self, item: Item) -> None:
with dpg.group(horizontal=False):
self._helpText(item)
dpg.add_input_text(tag=item.dest, default_value=(item.default or ""))
def _helpFloatCounterWidget(self, item: Item) -> None:
with dpg.group(horizontal=False):
self._helpText(item)
dpg.add_input_float(
format="%.9f",
tag=item.dest,
default_value=float(item.default or 0),
min_value=-(2**16),
max_value=2**16,
step=1,
step_fast=10,
)
def _helpIntCounterWidget(self, item: Item) -> None:
with dpg.group(horizontal=False):
self._helpText(item)
dpg.add_input_int(
tag=item.dest,
default_value=int(item.default or 0),
min_value=-(2**16),
max_value=2**16,
step=1,
step_fast=10,
)
def _helpFileWidget(self, item: Item) -> None:
"""Create a UI element with an input text field and a file picker."""
def file_picker_callback(_sender: str, app_data: dict[str, Any]) -> None:
"""Update the input text field with the selected file path."""
file_path = ""
# User may have selected and edited, or just written a name
user_input_path = Path(app_data.get("file_path_name", ""))
# Get the selection if possible
selected_path = Path(next(iter(app_data.get("selections", {}).values()), ""))
if user_input_path.stem == selected_path.stem:
file_path = selected_path
# User may have selected and edited, or just written a name
else:
file_path = user_input_path.stem + Path(item.default or "").suffix
dpg.set_value(item.dest, str(file_path))
with dpg.group(horizontal=False):
self._helpText(item)
dpg.add_input_text(tag=item.dest, default_value=(item.default or ""))
dpg.add_button(
label="Browse", callback=lambda: dpg.show_item(f"{item.dest}_file_dialog")
)
with dpg.file_dialog(
directory_selector=False,
show=False,
callback=file_picker_callback,
id=f"{item.dest}_file_dialog",
width=650,
height=400,
file_count=1,
):
dpg.add_file_extension(".*", color=hex_to_rgb(self.base24Theme[13]))
def _helpDropdownWidget(self, item: Item) -> None:
with dpg.group(horizontal=False):
self._helpText(item)
dpg.add_combo(tag=item.dest, items=item.additional_properties["choices"])
def addWidgetFromItem(self, item: Item) -> None:
"""Select a widget based on the item type.
:param Item item: the item
"""
functionMap = {
ItemType.Bool: self._helpFlagWidget,
ItemType.File: self._helpFileWidget,
ItemType.FileWrite: self._helpFileWidget,
ItemType.Path: self._helpFileWidget,
ItemType.Choice: self._helpDropdownWidget,
ItemType.Int: self._helpIntCounterWidget,
ItemType.Text: self._helpTextWidget,
ItemType.Float: self._helpFloatCounterWidget,
ItemType.List: self._helpTextWidget,
ItemType.Tuple: self._helpTextWidget,
ItemType.DateTime: self._helpTextWidget,
}
if item.type in functionMap:
return functionMap[item.type](item)
return None
def addItemsAndGroups(
self,
section: Group,
) -> list[Item]:
"""Items and groups and return a list of these so we can get values from the dpg widgets.
:param Group section: section with a name to display and items
:return list[Item]: flattened list of items
"""
dpg.add_text(
f"=== {helpers.stringTitlecase(section.name, ' ')} ===",
color=hex_to_rgb(self.base24Theme[14]),
)
items = []
for item in section.arg_items:
if item.type == ItemType.RadioGroup:
rGroup = item.additional_properties["radio"]
for rElement in rGroup:
self.addWidgetFromItem(rElement)
items.append(rElement)
else:
self.addWidgetFromItem(item)
items.append(item)
for group in section.groups:
items.extend(self.addItemsAndGroups(group))
return items
def open_menu_item(self, sender: str, _app_data: None) -> None:
"""Open a menu item.
:param _type_ sender: file to open
:param _type_ _app_data: [unused]
"""
f = Path(sender)
if f.is_file():
with dpg.window(label=f.name, width=825, height=400, horizontal_scrollbar=True):
dpg.add_text(helpers.read_file(sender))
else:
with dpg.window(label="Error"):
dpg.add_text(f"File {sender} Not Found :(")
def main(
self,
buildSpec: FullBuildSpec,
quit_callback: Callable[[], None],
run_callback: Callable[[dict[str, Any]], None],
) -> None:
"""Run the gui (dpg) with a given buildSpec, quit_callback, and run_callback.
- Theming + Configure dpg
- Menu Prep
- Create Window, set up Menu and Widgets
- Then, start dpg
:param FullBuildSpec buildSpec: Full cli parse/ build spec
:param Callable[[], None] quit_callback: generic callable used to quit
:param Callable[[dict[str, Any]], None] run_callback: generic callable used to run
"""
################
# Theming + Configure dpg
################
dpg.create_context()
with dpg.font_registry():
default_font = dpg.add_font(f"{THISDIR}/FiraCode-Regular.ttf", 17)
with dpg.theme() as global_theme, dpg.theme_component(dpg.mvAll):
dpg.add_theme_color(
dpg.mvThemeCol_WindowBg,
hex_to_rgb(self.base24Theme[16]),
category=dpg.mvThemeCat_Core,
)
dpg.add_theme_color(
dpg.mvThemeCol_FrameBg,
hex_to_rgb(self.base24Theme[17]),
category=dpg.mvThemeCat_Core,
)
dpg.add_theme_color(
dpg.mvThemeCol_PopupBg,
hex_to_rgb(self.base24Theme[17]),
category=dpg.mvThemeCat_Core,
)
dpg.add_theme_color(
dpg.mvThemeCol_FrameBgActive,
hex_to_rgb(self.base24Theme[17]),
category=dpg.mvThemeCat_Core,
)
dpg.add_theme_color(
dpg.mvThemeCol_FrameBgHovered,
hex_to_rgb(self.base24Theme[17]),
category=dpg.mvThemeCat_Core,
)
dpg.add_theme_color(
dpg.mvThemeCol_Text,
hex_to_rgb(self.base24Theme[6]),
category=dpg.mvThemeCat_Core,
)
dpg.add_theme_color(
dpg.mvThemeCol_MenuBarBg,
hex_to_rgb(self.base24Theme[0]),
category=dpg.mvThemeCat_Core,
)
dpg.add_theme_color(
dpg.mvThemeCol_ScrollbarBg,
hex_to_rgb(self.base24Theme[2]),
category=dpg.mvThemeCat_Core,
)
dpg.add_theme_color(
dpg.mvThemeCol_ScrollbarGrab,
hex_to_rgb(self.base24Theme[17]),
category=dpg.mvThemeCat_Core,
)
dpg.add_theme_color(
dpg.mvThemeCol_Header,
hex_to_rgb(self.base24Theme[0]),
category=dpg.mvThemeCat_Core,
)
dpg.add_theme_color(
dpg.mvThemeCol_HeaderHovered,
hex_to_rgb(self.base24Theme[1]),
category=dpg.mvThemeCat_Core,
)
dpg.add_theme_color(
dpg.mvThemeCol_HeaderActive,
hex_to_rgb(self.base24Theme[1]),
category=dpg.mvThemeCat_Core,
)
dpg.add_theme_color(
dpg.mvThemeCol_ScrollbarGrabActive,
hex_to_rgb(self.base24Theme[1]),
category=dpg.mvThemeCat_Core,
)
dpg.add_theme_color(
dpg.mvThemeCol_ScrollbarGrabHovered,
hex_to_rgb(self.base24Theme[1]),
category=dpg.mvThemeCat_Core,
)
dpg.add_theme_color(
dpg.mvThemeCol_Button,
hex_to_rgb(self.base24Theme[1]),
category=dpg.mvThemeCat_Core,
)
dpg.add_theme_color(
dpg.mvThemeCol_ButtonHovered,
hex_to_rgb(self.base24Theme[2]),
category=dpg.mvThemeCat_Core,
)
dpg.add_theme_color(
dpg.mvThemeCol_ButtonActive,
hex_to_rgb(self.base24Theme[2]),
category=dpg.mvThemeCat_Core,
)
dpg.add_theme_color(
dpg.mvThemeCol_Border,
hex_to_rgb(self.base24Theme[2]),
category=dpg.mvThemeCat_Core,
)
dpg.add_theme_color(
dpg.mvThemeCol_BorderShadow,
hex_to_rgb(self.base24Theme[2]),
category=dpg.mvThemeCat_Core,
)
dpg.add_theme_color(
dpg.mvThemeCol_CheckMark,
hex_to_rgb(self.base24Theme[14]),
category=dpg.mvThemeCat_Core,
)
dpg.add_theme_color(
dpg.mvThemeCol_TitleBg,
hex_to_rgb(self.base24Theme[1]),
category=dpg.mvThemeCat_Core,
)
dpg.add_theme_color(
dpg.mvThemeCol_TitleBgActive,
hex_to_rgb(self.base24Theme[14]),
category=dpg.mvThemeCat_Core,
)
dpg.add_theme_style(dpg.mvStyleVar_FrameRounding, 5, category=dpg.mvThemeCat_Core)
dpg.add_theme_style(dpg.mvStyleVar_FrameBorderSize, 1, category=dpg.mvThemeCat_Core)
dpg.bind_theme(global_theme)
dpg.bind_font(default_font)
################
# Menu Prep
################
if len(buildSpec.menu) == 0:
buildSpec.menu = {}
elif isinstance(buildSpec.menu, str):
buildSpec.menu = {"File": buildSpec.menu}
################
# Create Window, set up Menu and Widgets
################
# Define "Run" and "Exit" buttons
def _run_callback() -> None:
_items: list[Item] = [item for item in items if item.dest]
myd = {}
for item in _items:
value = dpg.get_value(item.dest)
key = f"{item.dest}{SEP}{item.type}"
if item.type in [ItemType.File, ItemType.FileWrite]:
prop = item.additional_properties
key += f";{prop.get('file_mode')};{prop.get('file_encoding')}"
myd[key] = value
run_callback(myd)
def close_dpg() -> None:
dpg.destroy_context()
quit_callback()
dpg.create_viewport(
title=buildSpec.program_name,
width=875,
height=min(max(400, 120 * buildSpec.max_args_shown), 1080),
)
dpg.set_exit_callback(close_dpg)
with dpg.window(label="", tag="primary", on_close=close_dpg):
if len(buildSpec.menu) > 0:
with dpg.menu_bar(), dpg.menu(label="Open"):
for menu_item in buildSpec.menu:
dpg.add_menu_item(
label=menu_item,
tag=buildSpec.menu[menu_item],
callback=self.open_menu_item,
)
# Program Description
dpg.add_text(
helpers.stringSentencecase(
buildSpec.program_description or buildSpec.parser_description
)
)
# Add widgets
items = []
for widget in buildSpec.widgets:
items.extend(self.addItemsAndGroups(widget))
dpg.add_button(label="Run", callback=_run_callback)
dpg.add_button(label="Exit", callback=close_dpg)
################
# Start dpg
################
dpg.setup_dearpygui()
dpg.show_viewport()
dpg.set_primary_window(window="primary", value=True)
dpg.start_dearpygui()
dpg.destroy_context()