-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstate.py
More file actions
292 lines (236 loc) · 8.76 KB
/
state.py
File metadata and controls
292 lines (236 loc) · 8.76 KB
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
"""All mutable application state — v0.8.0 PyWebView edition.
Plain Python dict + push() for JS synchronization. No tkinter dependencies.
"""
from __future__ import annotations
import json
from pathlib import Path
from typing import Any, Callable
# ---------------------------------------------------------------------------
# Core state dict
# ---------------------------------------------------------------------------
_state: dict[str, Any] = {
# Paths
"source": "",
"dest": "",
"active_dir": "",
# Selection
"selected_folders": [], # list of paths (was: set())
# Options
"move": False,
"dry": True,
"modify_names": False,
"custom_prefix": "",
"profile": "Generic",
"struct_mode": "flat", # "flat" | "mirror" | "parent"
# Audio conversion
"convert_enabled": False,
"convert_format": "wav", # "wav" | "aiff"
"convert_sample_rate": "keep",
"convert_bit_depth": "keep",
"convert_channels": "keep",
"convert_normalize": False,
"convert_follow_profile": True,
# BPM detection
"bpm_enabled": False,
"bpm_append": False,
"bpm_fresh": False,
# Key detection
"key_enabled": False,
"key_append": False,
"key_fresh": False,
# UI state
"status": "Ready",
"progress": 0,
"is_running": False,
"is_playing": False,
"playback_file": None,
"section_open": {
"struct": False,
"device": False,
"conversion": False,
"bpm": False,
"key": False,
"sync": False,
},
"is_dark": True,
"preview_filter": "",
# Data for UI
"dir_entries": [], # [{name, path, type, checked, icon}...]
"src_count": 0,
"preview_count": 0,
"preview_entries": [], # [{src_name, dest_name, bpm, key, length, srcpath}...]
"log_lines": [], # [{message, type, time}...]
# Sync system state
"sync_mode": "additive", # "mirror" | "additive"
"sync_plan": [], # list of plan entry dicts
"sync_plan_ready": False, # plan computed and ready to execute
"sync_plan_counts": {"add": 0, "update": 0, "delete": 0, "skip": 0},
"sync_in_progress": False,
"sync_show_plan": False, # whether to show sync plan in Deck B
"sync_auto_detected": False, # true when previous run detected in dest
# Duplicate detection
"dedup_enabled": True, # skip files with identical content
# Slicer state
"slicer_open": False,
"slicer_file": "", # currently loaded file path
"slicer_file_info": None, # {name, duration, sample_rate, channels}
"slicer_slices": [], # list of slice dicts
"slicer_waveform": None, # downsampled waveform data
"slicer_progress": 0, # 0-100 during export
"slicer_status": "", # status message
"slicer_exporting": False, # export in progress
"slicer_export_result": None, # result of last export
# Audition Stack state
"audition_open": False,
"audition_tracks": [None, None, None, None], # 4-element list; None = empty slot
"audition_master_bpm": 120.0,
"audition_loop": False,
"audition_rendering": False,
"audition_status": "",
"audition_selection": [], # Deck B srcpaths shift-selected (ordered, max 4)
}
# ---------------------------------------------------------------------------
# Window reference for JS sync
# ---------------------------------------------------------------------------
_window: Any = None # pywebview window
_push_callback: Callable | None = None # Optional callback for testing
def set_window(window) -> None:
"""Set the pywebview window reference for JS sync."""
global _window
_window = window
def set_push_callback(callback: Callable) -> None:
"""Set a callback for state push (useful for testing)."""
global _push_callback
_push_callback = callback
def get(key: str, default: Any = None) -> Any:
"""Get a state value."""
return _state.get(key, default)
def set(key: str, value: Any, push: bool = True) -> None:
"""Set a state value and optionally push to JS."""
_state[key] = value
if push:
push_keys([key])
def update(updates: dict[str, Any], push: bool = True) -> None:
"""Update multiple state values at once."""
_state.update(updates)
if push:
push_keys(list(updates.keys()))
def get_all() -> dict[str, Any]:
"""Get full state copy."""
# Deep copy to prevent accidental mutations
import copy
return copy.deepcopy(_state)
def push_keys(keys: list[str] | None = None) -> None:
"""Push state subset (or full state) to JS via window.evaluate_js().
Called automatically on state.set() and state.update().
Can also be called manually after batch operations.
"""
if _push_callback:
payload = {k: _state[k] for k in keys} if keys else _state
_push_callback(payload)
return
if not _window:
return
try:
payload = {k: _state[k] for k in keys} if keys else _state
json_str = json.dumps(payload)
_window.evaluate_js(f"window._onStateUpdate && window._onStateUpdate({json_str})")
except Exception as e:
print(f"State push failed: {e}")
def add_log(message: str, log_type: str = "info") -> None:
"""Add a log entry and push to UI."""
from datetime import datetime
entry = {
"message": message,
"type": log_type, # "info" | "success" | "error" | "warn"
"time": datetime.now().isoformat(),
}
_state["log_lines"].append(entry)
# Keep last 500 lines
if len(_state["log_lines"]) > 500:
_state["log_lines"] = _state["log_lines"][-500:]
push_keys(["log_lines"])
def clear_log() -> None:
"""Clear all log entries."""
_state["log_lines"] = []
push_keys(["log_lines"])
def set_status(text: str, progress: int | None = None) -> None:
"""Update status text and optional progress."""
updates = {"status": text}
if progress is not None:
updates["progress"] = max(0, min(100, progress))
update(updates)
def set_progress(value: int) -> None:
"""Update just the progress bar value (0-100)."""
set("progress", max(0, min(100, value)))
def register_refresh_callback(callback) -> None:
"""Register callback for preview refresh (used by operations)."""
global _refresh_preview_cb
_refresh_preview_cb = callback
# ---------------------------------------------------------------------------
# Legacy compatibility helpers
# ---------------------------------------------------------------------------
root = None # No tk root in webview edition
# For modules that still reference state.active_dir_var.get()
class _VarCompat:
"""Compatibility shim for old tk.StringVar-style access."""
def __init__(self, key: str):
self._key = key
def get(self) -> Any:
return _state.get(self._key, "")
def set(self, value: Any) -> None:
set(self._key, value)
# Expose compatibility vars
active_dir_var = _VarCompat("active_dir")
source_var = _VarCompat("source")
dest_var = _VarCompat("dest")
move_var = _VarCompat("move")
dry_var = _VarCompat("dry")
src_count_var = _VarCompat("src_count")
preview_count_var = _VarCompat("preview_count")
status_var = _VarCompat("status")
progress_var = _VarCompat("progress")
nav_path_var = _VarCompat("active_dir")
profile_var = _VarCompat("profile")
struct_mode_var = _VarCompat("struct_mode")
modify_names_var = _VarCompat("modify_names")
custom_prefix_var = _VarCompat("custom_prefix")
convert_enabled_var = _VarCompat("convert_enabled")
convert_format_var = _VarCompat("convert_format")
convert_sample_rate_var = _VarCompat("convert_sample_rate")
convert_bit_depth_var = _VarCompat("convert_bit_depth")
convert_channels_var = _VarCompat("convert_channels")
convert_normalize_var = _VarCompat("convert_normalize")
convert_follow_profile_var = _VarCompat("convert_follow_profile")
bpm_enabled_var = _VarCompat("bpm_enabled")
bpm_append_var = _VarCompat("bpm_append")
bpm_fresh_var = _VarCompat("bpm_fresh")
key_enabled_var = _VarCompat("key_enabled")
key_append_var = _VarCompat("key_append")
key_fresh_var = _VarCompat("key_fresh")
preview_filter_var = _VarCompat("preview_filter")
# Legacy widget references (now None)
dir_browser = None
preview_tree = None
log_text = None
run_btn = None
_status_dot = None
transport_prev_btn = None
transport_play_btn = None
transport_next_btn = None
# Internal state (not synced to JS)
import builtins
_selected_folders: set = builtins.set()
_preview_after = None
_is_dark = True
_dpi_scale = 1.0
_tooltip_win = None
_tooltip_item = None
_playback_file = None
_is_playing = False
_last_conversion_error = None
_section_open = _state["section_open"]
_refresh_preview_cb = None
def _sync_selected_folders() -> None:
"""Sync the internal _selected_folders set to state."""
_state["selected_folders"] = list(_selected_folders)