Skip to content

Commit

Permalink
Introduce GTK signal types
Browse files Browse the repository at this point in the history
Related #2273
  • Loading branch information
MattHag authored and pfps committed Jan 2, 2025
1 parent ab52c4a commit e9a58fb
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 94 deletions.
13 changes: 10 additions & 3 deletions lib/solaar/ui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import logging

from enum import Enum
from typing import Callable

import gi
Expand Down Expand Up @@ -48,6 +49,12 @@
APP_ID = "io.github.pwr_solaar.solaar"


class GtkSignal(Enum):
ACTIVATE = "activate"
COMMAND_LINE = "command-line"
SHUTDOWN = "shutdown"


def _startup(app, startup_hook, use_tray, show_window):
if logger.isEnabledFor(logging.DEBUG):
logger.debug("startup registered=%s, remote=%s", app.get_is_registered(), app.get_is_remote())
Expand Down Expand Up @@ -108,9 +115,9 @@ def run_loop(
lambda app, startup_hook: _startup(app, startup_hook, use_tray, show_window),
startup_hook,
)
application.connect("command-line", _command_line)
application.connect("activate", _activate)
application.connect("shutdown", _shutdown, shutdown_hook)
application.connect(GtkSignal.COMMAND_LINE.value, _command_line)
application.connect(GtkSignal.ACTIVATE.value, _activate)
application.connect(GtkSignal.SHUTDOWN.value, _shutdown, shutdown_hook)

application.register()
if application.get_is_remote():
Expand Down
9 changes: 6 additions & 3 deletions lib/solaar/ui/about/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
## You should have received a copy of the GNU General Public License along
## with this program; if not, write to the Free Software Foundation, Inc.,
## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


from enum import Enum
from typing import List
from typing import Tuple
from typing import Union
Expand All @@ -24,6 +23,10 @@
from solaar import NAME


class GtkSignal(Enum):
RESPONSE = "response"


class AboutView:
def __init__(self) -> None:
self.view: Union[Gtk.AboutDialog, None] = None
Expand All @@ -34,7 +37,7 @@ def init_ui(self) -> None:
self.view.set_icon_name(NAME.lower())
self.view.set_license_type(Gtk.License.GPL_2_0)

self.view.connect("response", lambda x, y: self.handle_close(x))
self.view.connect(GtkSignal.RESPONSE.value, lambda x, y: self.handle_close(x))

def update_version_info(self, version: str) -> None:
self.view.set_version(version)
Expand Down
11 changes: 8 additions & 3 deletions lib/solaar/ui/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
## You should have received a copy of the GNU General Public License along
## with this program; if not, write to the Free Software Foundation, Inc.,
## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
from enum import Enum

from gi.repository import Gdk
from gi.repository import Gtk
Expand All @@ -24,6 +25,10 @@
from . import pair_window


class GtkSignal(Enum):
ACTIVATE = "activate"


def make_image_menu_item(label, icon_name, function, *args):
box = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 6)
label = Gtk.Label(label=label)
Expand All @@ -33,7 +38,7 @@ def make_image_menu_item(label, icon_name, function, *args):
menu_item = Gtk.MenuItem()
menu_item.add(box)
menu_item.show_all()
menu_item.connect("activate", function, *args)
menu_item.connect(GtkSignal.ACTIVATE.value, function, *args)
menu_item.label = label
menu_item.icon = icon
return menu_item
Expand All @@ -45,7 +50,7 @@ def make(name, label, function, stock_id=None, *args):
if stock_id is not None:
action.set_stock_id(stock_id)
if function:
action.connect("activate", function, *args)
action.connect(GtkSignal.ACTIVATE.value, function, *args)
return action


Expand All @@ -54,7 +59,7 @@ def make_toggle(name, label, function, stock_id=None, *args):
action.set_icon_name(name)
if stock_id is not None:
action.set_stock_id(stock_id)
action.connect("activate", function, *args)
action.connect(GtkSignal.ACTIVATE.value, function, *args)
return action


Expand Down
39 changes: 25 additions & 14 deletions lib/solaar/ui/config_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import logging
import traceback

from enum import Enum
from threading import Timer

import gi
Expand All @@ -38,6 +39,16 @@
logger = logging.getLogger(__name__)


class GtkSignal(Enum):
ACTIVATE = "activate"
CHANGED = "changed"
CLICKED = "clicked"
MATCH_SELECTED = "match_selected"
NOTIFY_ACTIVE = "notify::active"
TOGGLED = "toggled"
VALUE_CHANGED = "value-changed"


def _read_async(setting, force_read, sbox, device_is_online, sensitive):
def _do_read(s, force, sb, online, sensitive):
try:
Expand Down Expand Up @@ -116,7 +127,7 @@ class ToggleControl(Gtk.Switch, Control):
def __init__(self, sbox, delegate=None):
super().__init__(halign=Gtk.Align.CENTER, valign=Gtk.Align.CENTER)
self.init(sbox, delegate)
self.connect("notify::active", self.changed)
self.connect(GtkSignal.NOTIFY_ACTIVE.value, self.changed)

def set_value(self, value):
if value is not None:
Expand All @@ -135,7 +146,7 @@ def __init__(self, sbox, delegate=None):
self.set_round_digits(0)
self.set_digits(0)
self.set_increments(1, 5)
self.connect("value-changed", self.changed)
self.connect(GtkSignal.VALUE_CHANGED.value, self.changed)

def get_value(self):
return int(super().get_value())
Expand Down Expand Up @@ -167,7 +178,7 @@ def __init__(self, sbox, delegate=None, choices=None):
self.choices = choices if choices is not None else sbox.setting.choices
for entry in self.choices:
self.append(str(int(entry)), str(entry))
self.connect("changed", self.changed)
self.connect(GtkSignal.CHANGED.value, self.changed)

def get_value(self):
return int(self.get_active_id()) if self.get_active_id() is not None else None
Expand Down Expand Up @@ -205,9 +216,9 @@ def norm(s):
completion.set_match_func(lambda completion, key, it: norm(key) in norm(completion.get_model()[it][1]))
completion.set_text_column(1)
self.set_completion(completion)
self.connect("changed", self.changed)
self.connect("activate", self.activate)
completion.connect("match_selected", self.select)
self.connect(GtkSignal.CHANGED.value, self.changed)
self.connect(GtkSignal.ACTIVATE.value, self.activate)
completion.connect(GtkSignal.MATCH_SELECTED.value, self.select)

def get_value(self):
choice = self.get_choice()
Expand Down Expand Up @@ -253,7 +264,7 @@ def __init__(self, sbox, delegate=None):
self.valueBox = _create_choice_control(sbox.setting, choices=self.value_choices, delegate=self)
self.pack_start(self.keyBox, False, False, 0)
self.pack_end(self.valueBox, False, False, 0)
self.keyBox.connect("changed", self.map_value_notify_key)
self.keyBox.connect(GtkSignal.CHANGED.value, self.map_value_notify_key)

def get_value(self):
key_choice = int(self.keyBox.get_active_id())
Expand Down Expand Up @@ -301,7 +312,7 @@ def __init__(self, sbox, change, button_label="...", delegate=None):
self._showing = True
self.setup(sbox.setting) # set up the data and boxes for the sub-controls
btn = Gtk.Button(label=button_label)
btn.connect("clicked", self.toggle_display)
btn.connect(GtkSignal.CLICKED.value, self.toggle_display)
self._button = btn
hbox = Gtk.HBox(homogeneous=False, spacing=6)
hbox.pack_end(change, False, False, 0)
Expand Down Expand Up @@ -349,7 +360,7 @@ def setup(self, setting):
h.set_tooltip_text(lbl_tooltip or " ")
control = Gtk.Switch()
control._setting_key = int(k)
control.connect("notify::active", self.toggle_notify)
control.connect(GtkSignal.NOTIFY_ACTIVE.value, self.toggle_notify)
h.pack_start(lbl, False, False, 0)
h.pack_end(control, False, False, 0)
lbl.set_margin_start(30)
Expand Down Expand Up @@ -426,7 +437,7 @@ def setup(self, setting):
h.pack_end(control, False, False, 0)
else:
raise NotImplementedError
control.connect("value-changed", self.changed, item, sub_item)
control.connect(GtkSignal.VALUE_CHANGED.value, self.changed, item, sub_item)
item_lb.add(h)
h._setting_sub_item = sub_item
h._label, h._control = sub_item_lbl, control
Expand Down Expand Up @@ -487,7 +498,7 @@ def setup(self, setting):
control = Gtk.Scale.new_with_range(Gtk.Orientation.HORIZONTAL, validator.min_value, validator.max_value, 1)
control.set_round_digits(0)
control.set_digits(0)
control.connect("value-changed", self.changed, validator.keys[item])
control.connect(GtkSignal.VALUE_CHANGED.value, self.changed, validator.keys[item])
h.pack_start(lbl, False, False, 0)
h.pack_end(control, True, True, 0)
h._setting_item = validator.keys[item]
Expand Down Expand Up @@ -548,15 +559,15 @@ def __init__(self, sbox, delegate=None):
for entry in item["choices"]:
item_box.append(str(int(entry)), str(entry))
item_box.set_active(0)
item_box.connect("changed", self.changed)
item_box.connect(GtkSignal.CHANGED.value, self.changed)
self.pack_start(item_box, False, False, 0)
elif item["kind"] == settings.Kind.RANGE:
item_box = Scale()
item_box.set_range(item["min"], item["max"])
item_box.set_round_digits(0)
item_box.set_digits(0)
item_box.set_increments(1, 5)
item_box.connect("value-changed", self.changed)
item_box.connect(GtkSignal.VALUE_CHANGED.value, self.changed)
self.pack_start(item_box, True, True, 0)
item_box.set_visible(False)
self._items[str(item["name"])] = (item_lblbox, item_box)
Expand Down Expand Up @@ -664,7 +675,7 @@ def _create_sbox(s, _device):
change.set_relief(Gtk.ReliefStyle.NONE)
change.add(change_icon)
change.set_sensitive(True)
change.connect("clicked", _change_click, sbox)
change.connect(GtkSignal.CLICKED.value, _change_click, sbox)

if s.kind == settings.Kind.TOGGLE:
control = ToggleControl(sbox)
Expand Down
Loading

0 comments on commit e9a58fb

Please sign in to comment.