-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.py
More file actions
executable file
·131 lines (107 loc) · 3.1 KB
/
Copy pathsettings.py
File metadata and controls
executable file
·131 lines (107 loc) · 3.1 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
from abc import ABC
from collections.abc import Callable
from dataclasses import dataclass
from typing import Any
from loguru import logger
@dataclass
class AbstractSetting(ABC):
id: str
name: str
default_value: Any
value: Any
apply_callback: Callable
def apply(self):
"""Apply the setting by calling its callback."""
logger.debug(f"Applying setting {self.id}: {self.value}")
self.apply_callback(self.value)
def to_dict(self):
"""Convert to dict for JSON serialization, excluding callback."""
d = {"id": self.id, "value": self.value}
return d
@classmethod
def from_dict(cls, data, apply_callback):
"""Create Setting from dict, reattaching callback."""
data["apply_callback"] = apply_callback
return cls(**data)
@dataclass
class FloatSetting(AbstractSetting):
id: str
name: str
min_value: float
max_value: float
default_value: float
value: float
apply_callback: Callable
precision: int = 1
step: float = 0.1
suffix: str = ""
@dataclass
class IntSetting(AbstractSetting):
id: str
name: str
min_value: int
max_value: int
default_value: int
value: int
apply_callback: Callable
step: int = 5
suffix: str = ""
@dataclass
class StringOptionSetting(AbstractSetting):
id: str
name: str
options: list[str]
default_value: str
value: str
apply_callback: Callable[[str], None]
suffix: str = ""
@dataclass
class ButtonMenuSetting(AbstractSetting):
id: str
name: str
default_value: None
value: None
apply_callback: Callable[[], Any]
def apply(self):
"""Apply the setting by calling its callback."""
logger.debug(f"Applying setting {self.id}: {self.value}")
self.apply_callback()
@dataclass
class GroupSetting(AbstractSetting):
id: str
name: str
children: list[AbstractSetting]
def apply(self):
for child in self.children:
child.apply()
def to_dict(self):
return {
"id": self.id,
"name": self.name,
"children": [child.to_dict() for child in self.children],
}
def get_visible_menu_items(menu_list, current_index, visible_count=2):
"""
Get up to visible_count menu items centered around current_index,
without wrapping around.
Returns:
list of (index, item): Actual indices with corresponding menu items.
"""
if not menu_list:
return []
total = len(menu_list)
visible_count = min(visible_count, total)
# Clamp current index to valid range
current_index = max(0, min(current_index, total - 1))
# Calculate bounds for visible items
half = visible_count // 2
start = current_index - half
end = start + visible_count
# Clamp window to list bounds
if start < 0:
start = 0
end = visible_count
if end > total:
end = total
start = total - visible_count
return [(i, menu_list[i]) for i in range(start, end)]