Skip to content

Commit

Permalink
Improve error status messages (#1024)
Browse files Browse the repository at this point in the history
  • Loading branch information
sezanzeb authored Dec 28, 2024
1 parent d902622 commit b4852f1
Show file tree
Hide file tree
Showing 13 changed files with 224 additions and 90 deletions.
2 changes: 1 addition & 1 deletion data/input-remapper.glade
Original file line number Diff line number Diff line change
Expand Up @@ -1798,7 +1798,7 @@ Macros allow multiple characters to be written with a single key-press. Informat
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="tooltip-text" translatable="yes">Map this input to an Analog Axis</property>
<property name="tooltip-text" translatable="yes">Map this input to an Analog Axis. Only possible for analog inputs and mice.</property>
<property name="opacity">0.5</property>
<property name="margin-start">18</property>
<property name="hexpand">True</property>
Expand Down
40 changes: 32 additions & 8 deletions inputremapper/configs/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
)
from packaging import version

from inputremapper.logging.logger import logger

try:
from pydantic.v1 import (
BaseModel,
Expand Down Expand Up @@ -71,8 +73,9 @@
OutputSymbolVariantError,
MacroButTypeOrCodeSetError,
SymbolAndCodeMismatchError,
MissingMacroOrKeyError,
WrongMappingTypeForKeyError,
MissingOutputAxisError,
MissingMacroOrKeyError,
)
from inputremapper.gui.gettext import _
from inputremapper.gui.messages.message_types import MessageType
Expand Down Expand Up @@ -152,7 +155,9 @@ class UIMapping(BaseModel):
target_uinput: Optional[Union[str, KnownUinput]] = None

# Either `output_symbol` or `output_type` and `output_code` is required
# Only set if output is "Key or Macro":
output_symbol: Optional[str] = None # The symbol or macro string if applicable
# "Analog Axis" or if preset edited manually to inject a code instead of a symbol:
output_type: Optional[int] = None # The event type of the mapped event
output_code: Optional[int] = None # The event code of the mapped event

Expand Down Expand Up @@ -322,11 +327,20 @@ def validate_mapping_type(cls, values):
output_code = values.get("output_code")
output_symbol = values.get("output_symbol")

if output_type is not None and output_code is not None and not output_symbol:
values["mapping_type"] = "analog"
if output_type is not None and output_symbol is not None:
# This is currently only possible when someone edits the preset file by
# hand. A key-output mapping without an output_symbol, but type and code
# instead, is valid as well.
logger.debug("Both output_type and output_symbol are set")

if output_type != EV_KEY and output_code is not None and not output_symbol:
values["mapping_type"] = MappingType.ANALOG.value

if output_type is None and output_code is None and output_symbol:
values["mapping_type"] = "key_macro"
values["mapping_type"] = MappingType.KEY_MACRO.value

if output_type == EV_KEY:
values["mapping_type"] = MappingType.KEY_MACRO.value

return values

Expand Down Expand Up @@ -463,17 +477,27 @@ def output_matches_input(cls, values: Dict[str, Any]) -> Dict[str, Any]:
And vice versa."""
assert isinstance(values.get("input_combination"), InputCombination)
combination: InputCombination = values["input_combination"]
analog_input_config = combination.find_analog_input_config()
use_as_analog = analog_input_config is not None

analog_input_config = combination.find_analog_input_config()
defines_analog_input = analog_input_config is not None
output_type = values.get("output_type")
output_code = values.get("output_code")
mapping_type = values.get("mapping_type")
output_symbol = values.get("output_symbol")
output_key_set = output_symbol or (output_type == EV_KEY and output_code)

if mapping_type is None:
# Empty mapping most likely
return values

if not defines_analog_input and mapping_type != MappingType.KEY_MACRO.value:
raise WrongMappingTypeForKeyError()

if not use_as_analog and not output_symbol and output_type != EV_KEY:
if not defines_analog_input and not output_key_set:
raise MissingMacroOrKeyError()

if (
use_as_analog
defines_analog_input
and output_type not in (EV_ABS, EV_REL)
and output_symbol != DISABLE_NAME
):
Expand Down
7 changes: 6 additions & 1 deletion inputremapper/configs/validation_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,14 @@ def __init__(self, symbol, code):
)


class WrongMappingTypeForKeyError(ValueError):
def __init__(self):
super().__init__(f"Wrong mapping_type for key input")


class MissingMacroOrKeyError(ValueError):
def __init__(self):
super().__init__("missing macro or key")
super().__init__("Missing macro or key")


class MissingOutputAxisError(ValueError):
Expand Down
24 changes: 12 additions & 12 deletions inputremapper/gui/components/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@
BTN_EXTRA,
BTN_SIDE,
)

from gi.repository import Gtk, GtkSource, Gdk

from inputremapper.configs.mapping import MappingData
from inputremapper.configs.input_config import InputCombination, InputConfig
from inputremapper.configs.keyboard_layout import keyboard_layout, XKB_KEYCODE_OFFSET
from inputremapper.configs.mapping import MappingData, MappingType
from inputremapper.groups import DeviceType
from inputremapper.gui.components.output_type_names import OutputTypeNames
from inputremapper.gui.controller import Controller
from inputremapper.gui.gettext import _
from inputremapper.gui.messages.message_broker import (
Expand All @@ -57,7 +58,6 @@
from inputremapper.gui.utils import HandlerDisabled, Colors
from inputremapper.injection.mapping_handlers.axis_transform import Transformation
from inputremapper.input_event import InputEvent
from inputremapper.configs.keyboard_layout import keyboard_layout, XKB_KEYCODE_OFFSET
from inputremapper.utils import get_evdev_constant_name

Capabilities = Dict[int, List]
Expand Down Expand Up @@ -1037,12 +1037,12 @@ def __init__(
self._message_broker.subscribe(MessageType.mapping, self._on_mapping_message)

def _set_active(self, mapping_type: Literal["key_macro", "analog"]):
if mapping_type == "analog":
self._stack.set_visible_child_name("Analog Axis")
if mapping_type == MappingType.ANALOG.value:
self._stack.set_visible_child_name(OutputTypeNames.analog_axis)
active = self._analog_toggle
inactive = self._key_macro_toggle
else:
self._stack.set_visible_child_name("Key or Macro")
self._stack.set_visible_child_name(OutputTypeNames.key_or_macro)
active = self._key_macro_toggle
inactive = self._analog_toggle

Expand All @@ -1053,11 +1053,11 @@ def _set_active(self, mapping_type: Literal["key_macro", "analog"]):

def _on_mapping_message(self, mapping: MappingData):
# fist check the actual mapping
if mapping.mapping_type == "analog":
self._set_active("analog")
if mapping.mapping_type == MappingType.ANALOG.value:
self._set_active(MappingType.ANALOG.value)

if mapping.mapping_type == "key_macro":
self._set_active("key_macro")
if mapping.mapping_type == MappingType.KEY_MACRO.value:
self._set_active(MappingType.KEY_MACRO.value)

def _on_gtk_toggle(self, btn: Gtk.ToggleButton):
# get_active returns the new toggle state already
Expand All @@ -1070,9 +1070,9 @@ def _on_gtk_toggle(self, btn: Gtk.ToggleButton):
return

if btn is self._key_macro_toggle:
self._controller.update_mapping(mapping_type="key_macro")
self._controller.update_mapping(mapping_type=MappingType.KEY_MACRO.value)
else:
self._controller.update_mapping(mapping_type="analog")
self._controller.update_mapping(mapping_type=MappingType.ANALOG.value)


class TransformationDrawArea:
Expand Down
3 changes: 3 additions & 0 deletions inputremapper/gui/components/output_type_names.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class OutputTypeNames:
analog_axis = "Analog Axis"
key_or_macro = "Key or Macro"
Loading

0 comments on commit b4852f1

Please sign in to comment.