|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +import gi |
| 4 | +gi.require_version('Gtk', '3.0') |
| 5 | +gi.require_version('XApp', '1.0') |
| 6 | +from gi.repository import Gio, GLib, GObject, Gtk |
| 7 | +import sys |
| 8 | +import signal |
| 9 | +import json |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +signal.signal(signal.SIGINT, signal.SIG_DFL) |
| 13 | + |
| 14 | + |
| 15 | +DBUS_NAME = "org.x.StatusIcon" |
| 16 | +DBUS_PATH = "/org/x/StatusIcon" |
| 17 | + |
| 18 | +class LayoutRow(GObject.Object): |
| 19 | + def __init__(self, shortname, json_info): |
| 20 | + super(LayoutRow, self).__init__() |
| 21 | + |
| 22 | + self.shortname = shortname |
| 23 | + self.name = json_info["name"] |
| 24 | + self.locale = json_info["locale"] |
| 25 | + |
| 26 | +class OskLayoutTester(): |
| 27 | + def __init__(self): |
| 28 | + self.window = None |
| 29 | + self.window = Gtk.Window() |
| 30 | + self.window.set_default_size(300, 400) |
| 31 | + self.window.connect("destroy", self.on_window_destroy) |
| 32 | + self.window_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL) |
| 33 | + self.main_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, |
| 34 | + margin=6, |
| 35 | + spacing=0) |
| 36 | + |
| 37 | + self.window_box.pack_start(self.main_box, True, True, 0) |
| 38 | + |
| 39 | + # list stuff |
| 40 | + sw_frame = Gtk.Frame() |
| 41 | + sw = Gtk.ScrolledWindow(hadjustment=None, vadjustment=None) |
| 42 | + sw_frame.add(sw) |
| 43 | + |
| 44 | + self.store = Gio.ListStore(item_type=LayoutRow) |
| 45 | + |
| 46 | + self.list_box = Gtk.ListBox(activate_on_single_click=True) |
| 47 | + self.list_box.connect("row-activated", self.on_row_activated) |
| 48 | + self.list_box.bind_model(self.store, self.new_row_widget) |
| 49 | + |
| 50 | + sw.add(self.list_box) |
| 51 | + |
| 52 | + self.main_box.pack_start(sw_frame, True, True, 6) |
| 53 | + self.window.add(self.window_box) |
| 54 | + |
| 55 | + self.window.show_all() |
| 56 | + |
| 57 | + self.input_source_settings = Gio.Settings.new("org.cinnamon.desktop.input-sources") |
| 58 | + print("Saving original sources") |
| 59 | + self.orig_sources = self.input_source_settings.get_value("sources") |
| 60 | + |
| 61 | + self.shortname_size_group = Gtk.SizeGroup(mode=Gtk.SizeGroupMode.HORIZONTAL) |
| 62 | + self.load_layouts() |
| 63 | + |
| 64 | + def load_layouts(self): |
| 65 | + for file in Path.cwd().iterdir(): |
| 66 | + if file.suffix != ".json": |
| 67 | + continue |
| 68 | + |
| 69 | + with open(file, "r") as f: |
| 70 | + json_info = json.load(f) |
| 71 | + self.store.append(LayoutRow(file.stem, json_info)) |
| 72 | + |
| 73 | + self.store.sort(self.sort_rows) |
| 74 | + |
| 75 | + def sort_rows(self, a, b, data=None): |
| 76 | + if a.shortname < b.shortname: |
| 77 | + return -1 |
| 78 | + elif a.shortname > b.shortname: |
| 79 | + return 1 |
| 80 | + else: |
| 81 | + return 0 |
| 82 | + |
| 83 | + def new_row_widget(self, item, data=None): |
| 84 | + box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=2) |
| 85 | + |
| 86 | + label = Gtk.Label(label=f"<b>{item.shortname}</b>", visible=True, xalign=0, use_markup=True) |
| 87 | + self.shortname_size_group.add_widget(label) |
| 88 | + box.pack_start(label, False, False, 6) |
| 89 | + label = Gtk.Label(label=item.name, visible=True, xalign=0) |
| 90 | + box.pack_start(label, True, True, 0) |
| 91 | + label = Gtk.Label(label=f"<b>{item.locale}</b>", visible=True, xalign=0, use_markup=True) |
| 92 | + box.pack_end(label, False, False, 6) |
| 93 | + |
| 94 | + row = Gtk.ListBoxRow() |
| 95 | + row.add(box) |
| 96 | + row.name = item.shortname |
| 97 | + row.show_all() |
| 98 | + return row |
| 99 | + |
| 100 | + def on_row_activated(self, box, row, data=None): |
| 101 | + self.input_source_settings.set_value("sources", GLib.Variant("a(ss)", [("xkb", row.name)])) |
| 102 | + |
| 103 | + def on_window_destroy(self, widget, data=None): |
| 104 | + self.quit() |
| 105 | + |
| 106 | + def quit(self): |
| 107 | + if self.orig_sources is not None: |
| 108 | + print("Restoring original sources") |
| 109 | + self.input_source_settings.set_value("sources", self.orig_sources) |
| 110 | + |
| 111 | + Gtk.main_quit() |
| 112 | + |
| 113 | +if __name__ == '__main__': |
| 114 | + test = OskLayoutTester() |
| 115 | + Gtk.main() |
0 commit comments