|
9 | 9 | from rich.logging import RichHandler |
10 | 10 | import webbrowser |
11 | 11 | import json |
| 12 | +from json import JSONDecodeError |
12 | 13 | import os |
13 | 14 | from deepdiff import DeepDiff |
14 | 15 | import trio |
|
28 | 29 | src_folder = app_file_path.parent.absolute() |
29 | 30 | root_folder = src_folder.parent.absolute() |
30 | 31 |
|
| 32 | +config_file = os.path.join(root_folder, "config.json") |
| 33 | + |
31 | 34 | ############################################################################# |
32 | 35 | # ALL DPG CALLS MUST BE MADE AFTER 'dpg.create_context()! |
33 | 36 | dpg.create_context() |
|
38 | 41 | dpg.add_string_value(tag="loglevel", default_value="INFO") |
39 | 42 | # logger.setLevel(dpg.get_value("loglevel")) |
40 | 43 |
|
41 | | -global window_width |
42 | | -width = 450 |
43 | | - |
44 | | -global window_height |
45 | | -height = 300 |
| 44 | +def set_viewport_config() -> bool: |
| 45 | + |
| 46 | + logger.debug("[magenta]Loading viewport configuration") |
| 47 | + |
| 48 | + global config_file |
| 49 | + if not os.path.exists(config_file): |
| 50 | + logger.warning("[yellow]Config file does not exist") |
| 51 | + return False |
| 52 | + |
| 53 | + with open(config_file, "r") as json_file: |
| 54 | + |
| 55 | + try: |
| 56 | + json_data = json_file.read() |
| 57 | + config_data = json.loads(json_data) |
| 58 | + |
| 59 | + except FileNotFoundError: |
| 60 | + logger.warning("[yellow]Config file does not exist") |
| 61 | + return False |
| 62 | + |
| 63 | + except JSONDecodeError: |
| 64 | + logger.warning("[red]Config file contains malformed JSON!") |
| 65 | + return False |
| 66 | + |
| 67 | + viewport_config = config_data.get("viewport") |
| 68 | + if not viewport_config: |
| 69 | + logger.warning("[yellow]Config file contained no viewport configuration") |
| 70 | + return False |
| 71 | + |
| 72 | + |
| 73 | + global window_width |
| 74 | + global window_height |
| 75 | + window_width = viewport_config['width'] |
| 76 | + window_height = viewport_config['height'] |
| 77 | + |
| 78 | + dpg.set_viewport_width(viewport_config['width'] ) |
| 79 | + dpg.set_viewport_height(viewport_config["height"]) |
| 80 | + dpg.set_viewport_pos(viewport_config["position"]) |
| 81 | + |
| 82 | + return True |
| 83 | + |
| 84 | +window_width = 600 |
| 85 | +window_height = 400 |
46 | 86 |
|
47 | | -dpg.set_global_font_scale(1.5) |
| 87 | +dpg.set_global_font_scale(1.2) |
48 | 88 | dpg.configure_app(init_file=os.path.join(root_folder, "dpg.ini")) |
49 | | -dpg.create_viewport(title='Patchwork 0.1.0', width=450, height=250, resizable=False) |
| 89 | + |
| 90 | +dpg.create_viewport( |
| 91 | + title='Patchwork 0.1.0', |
| 92 | + width=window_width, |
| 93 | + height=window_height, |
| 94 | + min_width=window_width, |
| 95 | + min_height=window_height, |
| 96 | + resizable=True, |
| 97 | +) |
| 98 | +set_viewport_config() |
50 | 99 | dpg.set_viewport_always_top(True) |
51 | 100 | dpg.setup_dearpygui() |
52 | 101 | dpg.show_viewport() |
53 | | - |
54 | | -# Save init |
55 | | -def save_init(): |
56 | | - logger.info("[magenta]Saving gui configuration") |
57 | | - dpg.save_init_file(os.path.join(root_folder, "dpg.ini")) |
58 | 102 |
|
59 | 103 | # RENDER PRESET |
60 | 104 | def choose_render_preset_callback(): |
@@ -179,15 +223,19 @@ def new(self, patchfile_path): |
179 | 223 | class TrackPatchfile(): |
180 | 224 | def __init__(self): |
181 | 225 |
|
| 226 | + global window_width |
| 227 | + global window_height |
| 228 | + |
182 | 229 | with dpg.file_dialog( |
183 | 230 | directory_selector=False, |
184 | | - default_path="Z:\\@Finished Renders", |
| 231 | + default_path="Z:/@FinishedRenders", |
| 232 | + |
185 | 233 | show=False, |
186 | 234 | callback=self.patchfile_chosen_callback, |
187 | 235 | cancel_callback=self.patchfile_chosen_cancel_callback, |
188 | 236 | modal=True, |
189 | | - width=600, |
190 | | - height=400, |
| 237 | + width=window_width - 50, |
| 238 | + height=window_height - 200, |
191 | 239 | tag="track_file_dialog", |
192 | 240 | ): |
193 | 241 |
|
@@ -268,10 +316,21 @@ def picked_dir_cancel_callback(self, sender, app_data): |
268 | 316 | track_patch_file = TrackPatchfile() |
269 | 317 | render_patch_file = RenderPatchFile() |
270 | 318 |
|
271 | | -# Callback |
272 | | -def mouse_drag_callback(): |
273 | | - logger.info("Saving init file") |
274 | | - save_init() |
| 319 | +def get_current_viewport_config() -> dict: |
| 320 | + return { |
| 321 | + "viewport": { |
| 322 | + "width": dpg.get_viewport_client_width(), |
| 323 | + "height": dpg.get_viewport_client_height(), |
| 324 | + "position": dpg.get_viewport_pos(), |
| 325 | + } |
| 326 | + } |
| 327 | + |
| 328 | +def exit_callback(): |
| 329 | + |
| 330 | + global config_file |
| 331 | + logger.info("[magenta]Writing viewport config to file") |
| 332 | + with open(config_file, "w") as json_file: |
| 333 | + json_file.write(json.dumps(get_current_viewport_config())) |
275 | 334 |
|
276 | 335 | # Helpers |
277 | 336 | def should_refresh_now(): |
@@ -475,7 +534,9 @@ def init(): |
475 | 534 |
|
476 | 535 | # Timers |
477 | 536 | global half_a_second |
| 537 | + global five_seconds |
478 | 538 | half_a_second = routines.Timer(0.5) |
| 539 | + five_seconds = routines.Timer(5) |
479 | 540 |
|
480 | 541 | def setup_gui(): |
481 | 542 |
|
@@ -630,6 +691,7 @@ async def main(): |
630 | 691 | # LOOP |
631 | 692 | logger.debug("[magenta]Starting render cycle!") |
632 | 693 |
|
| 694 | + dpg.set_exit_callback(exit_callback) |
633 | 695 | while dpg.is_dearpygui_running(): |
634 | 696 | async with trio.open_nursery() as nursery: |
635 | 697 | nursery.start_soon(gui_render) |
|
0 commit comments