feat: Add caching for keyboard widgets (#1610)

This commit is contained in:
Andrew Sun authored and GitHub committed 2025-08-17 08:46:46 -06:00
1 parent e03a6149b2
commit 50cb22f51c
2 files changed
+29 -7

No files matched your search

+14 -5
View File
@@ -9,7 +9,7 @@ from gi.repository import Gtk, GLib
class Keyboard(Gtk.Box):
langs = ["de", "en", "fr", "es"]
def __init__(self, screen, close_cb, entry=None, box=None):
def __init__(self, screen, close_cb, purpose, entry=None, box=None):
super().__init__(orientation=Gtk.Orientation.VERTICAL)
self.shift = []
self.shift_active = False
@@ -18,7 +18,7 @@ class Keyboard(Gtk.Box):
self.keyboard.set_direction(Gtk.TextDirection.LTR)
self.timeout = self.clear_timeout = None
self.entry = entry
self.purpose = self.entry.get_input_purpose()
self.purpose = purpose
self.box = box or None
language = self.detect_language(screen._config.get_main_config().get("language", None))
@@ -144,10 +144,16 @@ class Keyboard(Gtk.Box):
self.buttons[p][r][k].connect('button-release-event', self.release)
self.buttons[p][r][k].get_style_context().add_class("keyboard_pad")
self.pallet_nr = 0
self.set_pallet(self.pallet_nr)
self.pallet_nr = -1
self.set_pallet(0)
self.add(self.keyboard)
def reinit(self, close_cb, entry, box):
self.close_cb = close_cb
self.entry = entry
self.box = box
self.set_pallet(0)
def detect_language(self, language):
if language is None or language == "system_lang":
for language in self.langs:
@@ -159,9 +165,11 @@ class Keyboard(Gtk.Box):
return "en"
def set_pallet(self, p):
if p == self.pallet_nr:
return
self.pallet_nr = p
for _ in range(len(self.keys[self.pallet_nr]) + 1):
self.keyboard.remove_row(0)
self.pallet_nr = p
columns = 0
if self.purpose in (Gtk.InputPurpose.DIGITS, Gtk.InputPurpose.NUMBER):
@@ -218,6 +226,7 @@ class Keyboard(Gtk.Box):
if key == "⌫":
Gtk.Entry.do_backspace(self.entry)
elif key == "↓":
widget.get_style_context().remove_class("active")
self.close_cb(entry=self.entry, box=self.box)
return
elif key == "↑":
+15 -2
View File
@@ -62,6 +62,7 @@ class KlipperScreen(Gtk.Window):
connected_printer = None
files = None
keyboard = None
keyboard_cache = {}
panels = {}
popup_message = None
printers = None
@@ -1237,13 +1238,23 @@ class KlipperScreen(Gtk.Window):
kbd_grid.set_column_homogeneous(True)
kbd_width = 2 if purpose == Gtk.InputPurpose.DIGITS else 3
kbd_grid.attach(Gtk.Box(), 0, 0, 1, 1)
kbd = Keyboard(self, close_cb, entry=entry, box=box)
kbd = self._get_keyboard(entry.get_input_purpose())
kbd.reinit(close_cb=close_cb, entry=entry, box=box)
kbd_grid.attach(kbd, 1, 0, kbd_width, 1)
kbd_grid.attach(Gtk.Box(), kbd_width + 1, 0, 1, 1)
self.keyboard = {"box": kbd_grid}
self.keyboard = {
"box": kbd_grid,
"kbd": kbd
}
box.pack_end(kbd_grid, False, False, 0)
box.show_all()
def _get_keyboard(self, input_purpose):
if input_purpose in self.keyboard_cache:
return self.keyboard_cache[input_purpose]
k = self.keyboard_cache[input_purpose] = Keyboard(self, lambda *args, **kwargs: None, purpose=input_purpose)
return k
def _show_matchbox_keyboard(self, kbd_grid):
env = os.environ.copy()
usrkbd = os.path.expanduser("~/.matchbox/keyboard.xml")
@@ -1279,6 +1290,8 @@ class KlipperScreen(Gtk.Window):
box = self.base_panel.content
if 'process' in self.keyboard:
os.kill(self.keyboard['process'].pid, SIGTERM)
if 'kbd' in self.keyboard:
self.keyboard['box'].remove(self.keyboard['kbd'])
box.remove(self.keyboard['box'])
self.keyboard = None
if entry: