piper: fix optional access warnings

This commit is contained in:
Yaroslav Chvanov
2023-03-04 14:58:42 +03:00
parent 6f4f127a3f
commit 96d3e40789
3 changed files with 15 additions and 2 deletions

View File

@@ -321,11 +321,14 @@ class ButtonDialog(Gtk.Dialog):
type = RatbagdButton.Macro.KEY_PRESS
elif event.type == Gdk.EventType.KEY_RELEASE:
type = RatbagdButton.Macro.KEY_RELEASE
else:
raise ValueError("Incorrect event type")
# TODO: this needs to be checked for its Wayland support.
if not self._XORG_KEYCODE_OFFSET <= event.hardware_keycode <= 255:
print("Keycode is not within the valid range.", file=sys.stderr)
else:
assert self._current_macro is not None
self._current_macro.append(
type, event.hardware_keycode - self._XORG_KEYCODE_OFFSET
)
@@ -369,6 +372,7 @@ class ButtonDialog(Gtk.Dialog):
@Gtk.Template.Callback("_on_apply_button_clicked")
def _on_apply_button_clicked(self, button: Gtk.Button) -> None:
if self.stack.get_visible_child_name() == "capture":
assert self._current_macro is not None
self._current_macro.accept()
return Gdk.EVENT_PROPAGATE

View File

@@ -79,6 +79,7 @@ class MousePerspective(Gtk.Overlay):
)
active_profile = device.active_profile
assert active_profile is not None
self._set_profile(active_profile)
self.button_profile.set_visible(len(device.profiles) > 1)
@@ -106,6 +107,8 @@ class MousePerspective(Gtk.Overlay):
self.listbox_profiles.select_row(row)
def _set_profile(self, profile: RatbagdProfile) -> None:
assert self._device is not None
self.stack.foreach(Gtk.Widget.destroy)
if profile.resolutions:
self.stack.add_titled(
@@ -148,6 +151,7 @@ class MousePerspective(Gtk.Overlay):
@Gtk.Template.Callback("_on_save_button_clicked")
def _on_save_button_clicked(self, _button: Gtk.Button) -> None:
assert self._device is not None
self._device.commit()
@Gtk.Template.Callback("_on_notification_error_close_clicked")
@@ -163,6 +167,7 @@ class MousePerspective(Gtk.Overlay):
@Gtk.Template.Callback("_on_add_profile_button_clicked")
def _on_add_profile_button_clicked(self, button: Gtk.Button) -> None:
assert self._device is not None
# Enable the first disabled profile we find.
for profile in self._device.profiles:
if not profile.disabled:
@@ -175,6 +180,7 @@ class MousePerspective(Gtk.Overlay):
def _on_profile_notify_enabled(
self, profile: RatbagdProfile, pspec: Optional[GObject.ParamSpec]
) -> None:
assert self._device is not None
# We're only interested in the case where the last profile is disabled,
# so that we can reset the sensitivity of the add button.
if profile.disabled and profile == self._device.profiles[-1]:

View File

@@ -208,8 +208,11 @@ class Window(Gtk.ApplicationWindow):
def _on_device_selected(self, perspective, device: RatbagdDevice) -> None:
self._present_mouse_perspective(device)
def _get_child(self, name: str) -> Optional[Gtk.Widget]:
return self.stack_perspectives.get_child_by_name(name)
def _get_child(self, name: str) -> Gtk.Widget:
child = self.stack_perspectives.get_child_by_name(name)
if child is None:
raise ValueError(f"Child `{name}` was not found")
return child
def _perspective_add_back_button(self, perspective, ratbag: Ratbagd) -> None:
button_back = Gtk.Button.new_from_icon_name(