|
| 1 | +import os |
| 2 | +import gi |
| 3 | +from gi.repository import Adw, Gtk |
| 4 | + |
| 5 | +from loguru import logger as log |
| 6 | + |
| 7 | +from plugins.com_imdevinc_StreamControllerTwitchPlugin.TwitchActionBase import TwitchActionBase |
| 8 | + |
| 9 | +gi.require_version("Gtk", "4.0") |
| 10 | +gi.require_version("Adw", "1") |
| 11 | + |
| 12 | +options = [30, 60, 90, 120] |
| 13 | + |
| 14 | + |
| 15 | +class PlayAd(TwitchActionBase): |
| 16 | + _time: int = 30 |
| 17 | + |
| 18 | + def on_ready(self): |
| 19 | + self.set_media(media_path=os.path.join( |
| 20 | + self.plugin_base.PATH, "assets", "money.png"), size=0.85) |
| 21 | + |
| 22 | + def _load_config(self): |
| 23 | + settings = self.get_settings() |
| 24 | + time = settings.get("time") |
| 25 | + for value in options: |
| 26 | + if value == time: |
| 27 | + self._time = value |
| 28 | + return |
| 29 | + self._time = 30 |
| 30 | + |
| 31 | + def _on_change_time(self, *_): |
| 32 | + settings = self.get_settings() |
| 33 | + selected_index = self.time_row.get_selected() |
| 34 | + time = self.action_model[selected_index].get_string() |
| 35 | + settings["time"] = int(time) |
| 36 | + self.set_settings(settings) |
| 37 | + |
| 38 | + def get_config_rows(self): |
| 39 | + super_rows = super().get_config_rows() |
| 40 | + self.action_model = Gtk.StringList() |
| 41 | + self.time_row = Adw.ComboRow( |
| 42 | + model=self.action_model, title=self.plugin_base.lm.get("actions.play_ad.time.label")) |
| 43 | + self._load_config() |
| 44 | + |
| 45 | + index = 0 |
| 46 | + found = -1 |
| 47 | + for value in options: |
| 48 | + self.action_model.append(str(value)) |
| 49 | + if value == self._time: |
| 50 | + found = index |
| 51 | + index += 1 |
| 52 | + if found < 0: |
| 53 | + self.time_row.set_selected(0) |
| 54 | + |
| 55 | + self.time_row.connect("notify::selected", self._on_change_time) |
| 56 | + self.time_row.set_selected(found) |
| 57 | + super_rows.append(self.time_row) |
| 58 | + return super_rows |
| 59 | + |
| 60 | + def on_key_down(self): |
| 61 | + try: |
| 62 | + self.plugin_base.backend.play_ad(self._time) |
| 63 | + except Exception as ex: |
| 64 | + log.error(ex) |
| 65 | + self.show_error(3) |
0 commit comments