Skip to content

Commit ea9d9a9

Browse files
committed
feat: Working persistent viewport config
1 parent 30e2ed0 commit ea9d9a9

3 files changed

Lines changed: 88 additions & 19 deletions

File tree

src/config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"viewport": {"width": 808, "height": 518, "position": [1515, 228]}}

src/patchwork/app.py

Lines changed: 81 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from rich.logging import RichHandler
1010
import webbrowser
1111
import json
12+
from json import JSONDecodeError
1213
import os
1314
from deepdiff import DeepDiff
1415
import trio
@@ -28,6 +29,8 @@
2829
src_folder = app_file_path.parent.absolute()
2930
root_folder = src_folder.parent.absolute()
3031

32+
config_file = os.path.join(root_folder, "config.json")
33+
3134
#############################################################################
3235
# ALL DPG CALLS MUST BE MADE AFTER 'dpg.create_context()!
3336
dpg.create_context()
@@ -38,23 +41,64 @@
3841
dpg.add_string_value(tag="loglevel", default_value="INFO")
3942
# logger.setLevel(dpg.get_value("loglevel"))
4043

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
4686

47-
dpg.set_global_font_scale(1.5)
87+
dpg.set_global_font_scale(1.2)
4888
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()
5099
dpg.set_viewport_always_top(True)
51100
dpg.setup_dearpygui()
52101
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"))
58102

59103
# RENDER PRESET
60104
def choose_render_preset_callback():
@@ -179,15 +223,19 @@ def new(self, patchfile_path):
179223
class TrackPatchfile():
180224
def __init__(self):
181225

226+
global window_width
227+
global window_height
228+
182229
with dpg.file_dialog(
183230
directory_selector=False,
184-
default_path="Z:\\@Finished Renders",
231+
default_path="Z:/@FinishedRenders",
232+
185233
show=False,
186234
callback=self.patchfile_chosen_callback,
187235
cancel_callback=self.patchfile_chosen_cancel_callback,
188236
modal=True,
189-
width=600,
190-
height=400,
237+
width=window_width - 50,
238+
height=window_height - 200,
191239
tag="track_file_dialog",
192240
):
193241

@@ -268,10 +316,21 @@ def picked_dir_cancel_callback(self, sender, app_data):
268316
track_patch_file = TrackPatchfile()
269317
render_patch_file = RenderPatchFile()
270318

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()))
275334

276335
# Helpers
277336
def should_refresh_now():
@@ -475,7 +534,9 @@ def init():
475534

476535
# Timers
477536
global half_a_second
537+
global five_seconds
478538
half_a_second = routines.Timer(0.5)
539+
five_seconds = routines.Timer(5)
479540

480541
def setup_gui():
481542

@@ -630,6 +691,7 @@ async def main():
630691
# LOOP
631692
logger.debug("[magenta]Starting render cycle!")
632693

694+
dpg.set_exit_callback(exit_callback)
633695
while dpg.is_dearpygui_running():
634696
async with trio.open_nursery() as nursery:
635697
nursery.start_soon(gui_render)

src/patchwork/routines.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ def has_passed(self):
111111
return True
112112
return False
113113

114+
# def get_viewport_config():
115+
116+
# async def load_viewport_config():
117+
118+
# async def set_viewport_config():
119+
114120
async def get_markers_at_playhead(current_markers:MarkerCollection, current_frame:int) -> list[Marker]:
115121

116122
logger.debug("[magenta]Checking for marker at playhead")

0 commit comments

Comments
 (0)