-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathOBSActionBase.py
More file actions
335 lines (281 loc) · 12.3 KB
/
Copy pathOBSActionBase.py
File metadata and controls
335 lines (281 loc) · 12.3 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
from src.backend.PluginManager.ActionBase import ActionBase
from src.backend.PluginManager.PluginBase import PluginBase
from src.backend.DeckManagement.DeckController import DeckController
from src.backend.PageManagement.Page import Page
# Import gtk modules
import gi
gi.require_version("Gtk", "4.0")
gi.require_version("Adw", "1")
from gi.repository import Gtk, Adw, GLib
import globals as gl
import threading
import os
import time
from loguru import logger as log
CLASS_ICON_MAP = {
"ToggleStream": [
("stream_inactive.png", "Stream Off Icon"),
("stream_active.png", "Stream On Icon"),
("stream_reconnecting.png", "Stream Reconnecting Icon"),
],
"ToggleRecord": [
("record_inactive.png", "Record Off Icon"),
("record_active.png", "Record On Icon"),
("record_resume.png", "Record Paused Icon"),
],
"RecPlayPause": [
("record_inactive.png", "Record Off Icon"),
("record_pause.png", "Record Pause Button Icon"),
("record_resume.png", "Record Resume Button Icon"),
],
"ToggleReplayBuffer": [
("replay_buffer_disabled.png", "Replay Buffer Off Icon"),
("replay_buffer_enabled.png", "Replay Buffer On Icon"),
],
"SaveReplayBuffer": [
("replay_buffer_save.png", "Save Replay Buffer Icon"),
],
"ToggleVirtualCamera": [
("virtual_camera_disabled.png", "Virtual Camera Off Icon"),
("virtual_camera_enabled.png", "Virtual Camera On Icon"),
],
"ToggleStudioMode": [
("studio_mode_disabled.png", "Studio Mode Off Icon"),
("studio_mode_enabled.png", "Studio Mode On Icon"),
],
"TriggerTransition": [
("transition.png", "Transition Icon"),
],
"ToggleInputMute": [
("input_unmuted.png", "Input Unmuted Icon"),
("input_muted.png", "Input Muted Icon"),
],
"SetInputMute": [
("input_unmuted.png", "Input Unmuted Icon"),
("input_muted.png", "Input Muted Icon"),
],
"InputDial": [
("input_unmuted.png", "Input Unmuted Icon"),
("input_muted.png", "Input Muted Icon"),
],
"ToggleSceneItemEnabled": [
("scene_item_enabled.png", "Scene Item Enabled Icon"),
("scene_item_disabled.png", "Scene Item Disabled Icon"),
],
"SetSceneItemEnabled": [
("scene_item_enabled.png", "Scene Item Enabled Icon"),
("scene_item_disabled.png", "Scene Item Disabled Icon"),
],
"ToggleFilter": [
("scene_item_enabled.png", "Filter Enabled Icon"),
("scene_item_disabled.png", "Filter Disabled Icon"),
],
"SetFilter": [
("scene_item_enabled.png", "Filter Enabled Icon"),
("scene_item_disabled.png", "Filter Disabled Icon"),
],
"SwitchScene": [
("scene_active.png", "Active Scene Icon"),
("scene_inactive.png", "Inactive Scene Icon"),
],
"SwitchSceneCollection": [
("scene.png", "Switch Scene Collection Icon"),
],
"OBSStats": [
("stats.png", "OBS Stats Icon"),
],
}
class BackendProxy:
def __init__(self, plugin_base, action):
self._plugin_base = plugin_base
self._action = action
def __getattr__(self, name):
backend = self._plugin_base.backend
if backend is None:
if name == "get_connected":
return lambda *args, **kwargs: False
return lambda *args, **kwargs: None
attr = getattr(backend, name)
if callable(attr):
def wrapper(*args, **kwargs):
return attr(*args, connection_id=self._action.connection_id, **kwargs)
return wrapper
return attr
class OBSActionBase(ActionBase):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.has_configuration = True
self.custom_icon_entries = {}
self.custom_icon_clear_buttons = {}
self.validated_custom_icons = {}
self.custom_icon_cache_initialized = False
self.status_label = Gtk.Label(
label=self.plugin_base.lm.get("actions.base.status.no-connection"), css_classes=["bold", "red"]
)
self.update_custom_icon_cache()
if self.plugin_base.backend is not None:
if not self.backend.get_connected():
self.reconnect_obs()
@property
def connection_id(self):
return self.get_settings().get("connection_id", "default")
@property
def backend(self):
if not hasattr(self, "_backend_proxy"):
self._backend_proxy = BackendProxy(self.plugin_base, self)
return self._backend_proxy
@backend.setter
def backend(self, value):
pass
def update_custom_icon_cache(self):
self.validated_custom_icons = {}
settings = self.get_settings()
custom_icons = CLASS_ICON_MAP.get(self.__class__.__name__, [])
for default_filename, _ in custom_icons:
path = settings.get(f"custom_icon_{default_filename}", "").strip()
if path and os.path.isfile(path):
self.validated_custom_icons[default_filename] = path
if getattr(self, "on_ready_called", False):
self.custom_icon_cache_initialized = True
def get_config_rows(self) -> list:
self.connection_model = Gtk.StringList()
self.connection_row = Adw.ComboRow(
model=self.connection_model,
title="OBS Connection Profile"
)
self.load_config_defaults()
# Connect signals
self.connection_row.connect("notify::selected", self.on_change_connection)
rows = [self.connection_row]
# Add custom icon rows
custom_icons = CLASS_ICON_MAP.get(self.__class__.__name__, [])
for default_filename, label_text in custom_icons:
entry = Adw.ActionRow(title=label_text)
current_val = self.get_settings().get(f"custom_icon_{default_filename}", "")
# Set subtitle to filename without extension
if current_val:
filename = os.path.basename(current_val)
name_without_ext = os.path.splitext(filename)[0]
entry.set_subtitle(name_without_ext)
else:
entry.set_subtitle("Default Icon")
# Create choose button
btn = Gtk.Button.new_from_icon_name("document-open-symbolic")
btn.connect("clicked", lambda button, df=default_filename: self.on_select_custom_icon(df))
entry.add_suffix(btn)
# Create clear button
clear_btn = Gtk.Button.new_from_icon_name("edit-clear-symbolic")
clear_btn.set_sensitive(bool(current_val))
clear_btn.connect("clicked", lambda button, df=default_filename: self.on_clear_custom_icon(df))
entry.add_suffix(clear_btn)
self.custom_icon_entries[default_filename] = entry
self.custom_icon_clear_buttons[default_filename] = clear_btn
rows.append(entry)
return rows
def on_select_custom_icon(self, default_filename):
current_val = self.get_settings().get(f"custom_icon_{default_filename}", "")
def on_select_callback(path):
if not path:
return
# Save setting
settings = self.get_settings()
if settings.get(f"custom_icon_{default_filename}") != path:
settings[f"custom_icon_{default_filename}"] = path
self.set_settings(settings)
self.update_custom_icon_cache()
# Update subtitle
if default_filename in self.custom_icon_entries:
filename = os.path.basename(path)
name_without_ext = os.path.splitext(filename)[0]
self.custom_icon_entries[default_filename].set_subtitle(name_without_ext)
# Enable clear button
if default_filename in self.custom_icon_clear_buttons:
self.custom_icon_clear_buttons[default_filename].set_sensitive(True)
# Trigger update immediately
if hasattr(self, "_current_state"):
self._current_state = None
else:
self.current_state = None
if hasattr(self, "on_tick"):
self.on_tick()
elif hasattr(self, "on_ready"):
self.on_ready()
GLib.idle_add(gl.app.let_user_select_asset, current_val, on_select_callback)
def on_clear_custom_icon(self, default_filename):
settings = self.get_settings()
if settings.get(f"custom_icon_{default_filename}"):
settings[f"custom_icon_{default_filename}"] = ""
self.set_settings(settings)
self.update_custom_icon_cache()
if default_filename in self.custom_icon_entries:
self.custom_icon_entries[default_filename].set_subtitle("Default Icon")
if default_filename in self.custom_icon_clear_buttons:
self.custom_icon_clear_buttons[default_filename].set_sensitive(False)
# Trigger update immediately
if hasattr(self, "_current_state"):
self._current_state = None
else:
self.current_state = None
if hasattr(self, "on_tick"):
self.on_tick()
elif hasattr(self, "on_ready"):
self.on_ready()
def set_media(self, media_path=None, *args, **kwargs):
if not getattr(self, "custom_icon_cache_initialized", False) and getattr(self, "on_ready_called", False):
self.update_custom_icon_cache()
if media_path:
filename = os.path.basename(media_path)
if hasattr(self, "validated_custom_icons") and filename in self.validated_custom_icons:
media_path = self.validated_custom_icons[filename]
super().set_media(media_path=media_path, *args, **kwargs)
def load_config_defaults(self):
self.connections_list = self.plugin_base.get_settings().get("connections", [])
while self.connection_model.get_n_items() > 0:
self.connection_model.remove(0)
selected_idx = 0
active_id = self.connection_id
for i, conn in enumerate(self.connections_list):
self.connection_model.append(conn.get("name", "Unnamed"))
if conn.get("id") == active_id:
selected_idx = i
self.connection_row.set_selected(selected_idx)
self.update_status_label()
def on_change_connection(self, combo, *args):
selected_idx = combo.get_selected()
if 0 <= selected_idx < len(self.connections_list):
settings = self.get_settings()
settings["connection_id"] = self.connections_list[selected_idx]["id"]
self.set_settings(settings)
self.reconnect_obs()
self.on_connection_changed()
def on_connection_changed(self):
# Virtual method to be overridden by subclasses
pass
def reconnect_obs(self):
threading.Thread(target=self._reconnect_obs, daemon=True, name="reconnect_obs").start()
def _reconnect_obs(self):
# Let backend process sync
time.sleep(0.5)
self.update_status_label()
GLib.idle_add(self.on_connection_established)
def on_connection_established(self):
pass
def update_status_label(self) -> None:
if not hasattr(self, "status_label") or self.status_label is None:
return
connected = self.backend.get_connected()
GLib.idle_add(self._update_status_label, connected)
def _update_status_label(self, connected):
if not hasattr(self, "status_label") or self.status_label is None:
return
if connected:
self.status_label.set_label(self.plugin_base.lm.get("actions.base.status.connected"))
self.status_label.remove_css_class("red")
self.status_label.add_css_class("green")
else:
self.status_label.set_label(self.plugin_base.lm.get("actions.base.status.no-connection"))
self.status_label.remove_css_class("green")
self.status_label.add_css_class("red")
def get_custom_config_area(self):
self.update_status_label()
return self.status_label