2022-01-10 19:37:22 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# input-remapper - GUI for device specific keyboard mappings
|
|
|
|
# Copyright (C) 2022 sezanzeb <proxima@sezanzeb.de>
|
|
|
|
#
|
|
|
|
# This file is part of input-remapper.
|
|
|
|
#
|
|
|
|
# input-remapper is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# input-remapper is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with input-remapper. If not, see <https://www.gnu.org/licenses/>.
|
2022-10-16 12:56:21 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-07-23 08:53:41 +00:00
|
|
|
import time
|
2022-10-16 12:56:21 +00:00
|
|
|
from typing import List
|
|
|
|
|
|
|
|
from gi.repository import Gtk, GLib, Gdk
|
2022-01-10 19:37:22 +00:00
|
|
|
|
2022-10-16 12:56:21 +00:00
|
|
|
from inputremapper.logger import logger
|
2022-01-10 19:37:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
# status ctx ids
|
2022-07-23 08:53:41 +00:00
|
|
|
|
2022-01-10 19:37:22 +00:00
|
|
|
CTX_SAVE = 0
|
|
|
|
CTX_APPLY = 1
|
|
|
|
CTX_KEYCODE = 2
|
|
|
|
CTX_ERROR = 3
|
|
|
|
CTX_WARNING = 4
|
|
|
|
CTX_MAPPING = 5
|
|
|
|
|
2022-05-11 21:17:42 +00:00
|
|
|
debounces = {}
|
|
|
|
|
|
|
|
|
|
|
|
def debounce(timeout):
|
|
|
|
"""Debounce a function call to improve performance.
|
|
|
|
|
|
|
|
Calling this creates the decorator, so use something like
|
|
|
|
|
|
|
|
@debounce(50)
|
|
|
|
def foo():
|
|
|
|
...
|
|
|
|
"""
|
|
|
|
|
|
|
|
def decorator(func):
|
|
|
|
def clear_debounce(self, *args):
|
|
|
|
debounces[func.__name__] = None
|
|
|
|
return func(self, *args)
|
|
|
|
|
|
|
|
def wrapped(self, *args):
|
|
|
|
if debounces.get(func.__name__) is not None:
|
|
|
|
GLib.source_remove(debounces[func.__name__])
|
|
|
|
|
|
|
|
debounces[func.__name__] = GLib.timeout_add(
|
|
|
|
timeout, lambda: clear_debounce(self, *args)
|
|
|
|
)
|
|
|
|
|
|
|
|
return wrapped
|
|
|
|
|
|
|
|
return decorator
|
|
|
|
|
2022-01-10 19:37:22 +00:00
|
|
|
|
|
|
|
class HandlerDisabled:
|
|
|
|
"""Safely modify a widget without causing handlers to be called.
|
|
|
|
|
2022-10-16 12:56:21 +00:00
|
|
|
Use in a `with` statement.
|
2022-01-10 19:37:22 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, widget, handler):
|
|
|
|
self.widget = widget
|
|
|
|
self.handler = handler
|
|
|
|
|
|
|
|
def __enter__(self):
|
2022-10-16 12:56:21 +00:00
|
|
|
try:
|
|
|
|
self.widget.handler_block_by_func(self.handler)
|
|
|
|
except TypeError as error:
|
|
|
|
# if nothing is connected to the given signal, it is not critical
|
|
|
|
# at all
|
|
|
|
logger.warning('HandlerDisabled entry failed: "%s"', error)
|
2022-01-10 19:37:22 +00:00
|
|
|
|
|
|
|
def __exit__(self, *_):
|
2022-10-16 12:56:21 +00:00
|
|
|
try:
|
|
|
|
self.widget.handler_unblock_by_func(self.handler)
|
|
|
|
except TypeError as error:
|
|
|
|
logger.warning('HandlerDisabled exit failed: "%s"', error)
|
2022-01-10 19:37:22 +00:00
|
|
|
|
|
|
|
|
2022-07-23 08:53:41 +00:00
|
|
|
def gtk_iteration(iterations=0):
|
2022-01-10 19:37:22 +00:00
|
|
|
"""Iterate while events are pending."""
|
|
|
|
while Gtk.events_pending():
|
|
|
|
Gtk.main_iteration()
|
2022-07-23 08:53:41 +00:00
|
|
|
for _ in range(iterations):
|
|
|
|
time.sleep(0.002)
|
|
|
|
while Gtk.events_pending():
|
|
|
|
Gtk.main_iteration()
|
2022-10-16 12:56:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Colors:
|
|
|
|
"""Looks up colors from the GTK theme.
|
|
|
|
|
|
|
|
Defaults to libadwaita-light theme colors if the lookup fails.
|
|
|
|
"""
|
|
|
|
|
|
|
|
fallback_accent = Gdk.RGBA(0.21, 0.52, 0.89, 1)
|
|
|
|
fallback_background = Gdk.RGBA(0.98, 0.98, 0.98, 1)
|
|
|
|
fallback_base = Gdk.RGBA(1, 1, 1, 1)
|
|
|
|
fallback_border = Gdk.RGBA(0.87, 0.87, 0.87, 1)
|
|
|
|
fallback_font = Gdk.RGBA(0.20, 0.20, 0.20, 1)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_color(names: List[str], fallback: Gdk.RGBA) -> Gdk.RGBA:
|
|
|
|
"""Get theme colors. Provide multiple names for fallback purposes."""
|
|
|
|
for name in names:
|
|
|
|
found, color = Gtk.StyleContext().lookup_color(name)
|
|
|
|
if found:
|
|
|
|
return color
|
|
|
|
|
|
|
|
return fallback
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_accent_color() -> Gdk.RGBA:
|
|
|
|
"""Look up the accent color from the current theme."""
|
|
|
|
return Colors.get_color(
|
|
|
|
["accent_bg_color", "theme_selected_bg_color"],
|
|
|
|
Colors.fallback_accent,
|
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_background_color() -> Gdk.RGBA:
|
|
|
|
"""Look up the background-color from the current theme."""
|
|
|
|
return Colors.get_color(
|
|
|
|
["theme_bg_color"],
|
|
|
|
Colors.fallback_background,
|
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_base_color() -> Gdk.RGBA:
|
|
|
|
"""Look up the base-color from the current theme."""
|
|
|
|
return Colors.get_color(
|
|
|
|
["theme_base_color"],
|
|
|
|
Colors.fallback_base,
|
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_border_color() -> Gdk.RGBA:
|
|
|
|
"""Look up the border from the current theme."""
|
|
|
|
return Colors.get_color(["borders"], Colors.fallback_border)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_font_color() -> Gdk.RGBA:
|
|
|
|
"""Look up the border from the current theme."""
|
|
|
|
return Colors.get_color(
|
|
|
|
["theme_fg_color"],
|
|
|
|
Colors.fallback_font,
|
|
|
|
)
|