-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.py
More file actions
149 lines (116 loc) · 4.91 KB
/
settings.py
File metadata and controls
149 lines (116 loc) · 4.91 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
#settings.py
import os, configparser, pygetwindow as gw
from constants import CONFIG_FILE, SECTION, DEFAULTS
import config
import state
from utils import get_path
settings_path = get_path(CONFIG_FILE)
def save_settings():
# Use existing values from memory
# NOTE: zoom_var and theme are NOT saved here - they come from USER_PROFILE database
department = config.cfg.get("department", "")
# Load existing settings.ini
parser = configparser.ConfigParser()
if os.path.exists(settings_path):
parser.read(settings_path)
if not parser.has_section(SECTION):
parser.add_section(SECTION)
# Only update department (window positions are saved separately)
parser[SECTION]['department'] = department
with open(settings_path, 'w') as configfile:
parser.write(configfile)
def load_settings():
config = configparser.ConfigParser()
# read existing config; missing file is okay
if os.path.exists(settings_path):
config.read(settings_path)
# ensure the section exists
if not config.has_section(SECTION):
config.add_section(SECTION)
section = config[SECTION]
# populate defaults for missing keys
for key, val in DEFAULTS.items():
if key not in section:
section[key] = val
# Convert SectionProxy to a regular dictionary so runtime updates work correctly
# SectionProxy lookups can be confusing and don't always reflect in-memory changes
settings_dict = dict(section)
# Set runtime defaults for theme and zoom (will be overridden by database on login)
settings_dict['theme'] = 'dark' # Default theme
settings_dict['zoom_var'] = '200' # Default zoom
return settings_dict
def save_position(x, y):
# Update in-memory config
config.cfg['win_x'] = str(x)
config.cfg['win_y'] = str(y)
# Use a different name for the parser instance
parser = configparser.ConfigParser()
if os.path.exists(settings_path):
parser.read(settings_path)
if not parser.has_section(SECTION):
parser.add_section(SECTION)
parser[SECTION]['win_x'] = str(x)
parser[SECTION]['win_y'] = str(y)
with open(settings_path, 'w') as configfile:
parser.write(configfile)
def get_window_state(driver, exclude_window=None):
try:
tolerance = 2
pos = driver.get_window_position()
size = driver.get_window_size()
target = (pos['x'], pos['y'], size['width'], size['height'])
for w in gw.getWindowsWithTitle('Google Chrome'):
if exclude_window and w == exclude_window:
continue
# Allow for slight mismatch
if (abs(w.left - target[0]) <= tolerance and
abs(w.top - target[1]) <= tolerance and
abs(w.width - target[2]) <= tolerance and
abs(w.height - target[3]) <= tolerance):
if w.isMinimized:
return 'minimized', w
elif w.isMaximized:
return 'maximized', w
else:
return 'normal', w
print(f"[DEBUG] can't find matching Chrome window — assuming minimized")
return 'minimized', None # Fallback
except Exception as e:
print(f"[DEBUG] get_window_state error: {e}")
return 'unknown', None
def save_window_geometry():
if not state.driver_dc or not state.driver_sc:
return "Window(s) not found"
# Get positions and sizes
dc_pos = state.driver_dc.get_window_position()
dc_size = state.driver_dc.get_window_size()
sc_pos = state.driver_sc.get_window_position()
sc_size = state.driver_sc.get_window_size()
# Determine window state
dc_state, dc_win = get_window_state(state.driver_dc)
sc_state, _ = get_window_state(state.driver_sc, exclude_window=dc_win)
# Save DC geometry if normal
if dc_state == "normal":
config.cfg["dc_x"] = str(dc_pos["x"])
config.cfg["dc_y"] = str(dc_pos["y"])
config.cfg["dc_width"] = str(dc_size["width"])
config.cfg["dc_height"] = str(dc_size["height"])
# Save SC geometry if normal
if sc_state == "normal":
config.cfg["sc_x"] = str(sc_pos["x"])
config.cfg["sc_y"] = str(sc_pos["y"])
config.cfg["sc_width"] = str(sc_size["width"])
config.cfg["sc_height"] = str(sc_size["height"])
# Always save state
config.cfg["dc_state"] = dc_state
config.cfg["sc_state"] = sc_state
# Load + update .ini
parser = configparser.ConfigParser()
if os.path.exists(settings_path):
parser.read(settings_path)
if not parser.has_section(SECTION):
parser.add_section(SECTION)
for key in config.cfg:
parser[SECTION][key] = config.cfg[key]
with open(settings_path, "w") as configfile:
parser.write(configfile)