Codebase list xapp / upstream/1.0.2 test-scripts / xapp-kbd-layout-controller
upstream/1.0.2

Tree @upstream/1.0.2 (Download .tar.gz)

xapp-kbd-layout-controller @upstream/1.0.2raw · history · blame

#! /usr/bin/python3

"""
A demo/test script for the XAppKbdLayoutController class
"""
import sys, os
import signal
import gettext

import gi
gi.require_version('Gtk', '3.0')
gi.require_version('XApp', '1.0')

from gi.repository import Gtk, XApp, GObject, Gkbd, GdkPixbuf

signal.signal(signal.SIGINT, signal.SIG_DFL)

class Main:
    def __init__(self):
        win = Gtk.Window()
        frame = Gtk.Frame()
        frame.set_margin_start(2)
        frame.set_margin_end(2)
        frame.set_margin_top(2)
        frame.set_margin_bottom(2)

        win.add(frame)

        box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
        box.set_margin_start(2)
        box.set_margin_end(2)
        box.set_margin_top(2)
        box.set_margin_bottom(2)

        frame.add(box)

        self.use_caps = False

        self.controller = XApp.KbdLayoutController()
        self.controller.connect("layout-changed", self.on_layout_changed)
        self.controller.connect("config-changed", self.on_config_changed)

        self.label = Gtk.Label()
        self.label.set_text(self.controller.get_current_name())
        box.pack_start(self.label, True, True, 4)

        self.flag_button = Gtk.Button()
        box.pack_start(self.flag_button, True, True, 4)
        self.flag_button.connect("clicked", self.on_button_clicked)

        self.group_button = Gtk.Button()
        box.pack_start(self.group_button, True, True, 4)
        self.group_button.connect("clicked", self.on_button_clicked)

        self.variant_button = Gtk.Button()
        box.pack_start(self.variant_button, True, True, 4)
        self.variant_button.connect("clicked", self.on_button_clicked)

        check = Gtk.CheckButton.new_with_label("Use caps")
        check.connect("toggled", self.on_caps_toggled)
        box.pack_start(check, True, True, 4)

        frame.show_all()

        win.connect("delete-event", lambda w, e: Gtk.main_quit())

        self.on_layout_changed(self.controller)

        win.present()

        Gtk.main()

    def on_caps_toggled(self, widget):
        self.use_caps = widget.get_active()
        self.on_layout_changed(self.controller)

    def on_button_clicked(self, widget, data=None):
        self.controller.next_group()

    def on_layout_changed(self, controller, group=None):
        handled = False

        name = self.controller.get_current_icon_name()
        if name != None:
            filename = "/usr/share/flags/iso-4x3-svg/%s.svgz" % name

            valid, width, height = Gtk.IconSize.lookup(Gtk.IconSize.BUTTON)
            pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(filename, -1, height)

            image = Gtk.Image.new_from_pixbuf(pixbuf)
            self.flag_button.set_image(image)
            handled = True

        if not handled:
            name = self.controller.get_current_short_group_label()
            self.flag_button.set_label(name.upper())
            self.flag_button.set_image(None)

        self.label.set_text(self.controller.get_current_name())

        group_label = self.controller.get_current_short_group_label()
        variant_label = self.controller.get_current_variant_label()

        if self.use_caps:
            group_label = group_label.upper()
            variant_label = variant_label.upper()

        self.group_button.set_label(group_label)
        self.variant_button.set_label(variant_label)

    def on_config_changed(self, controller):
        GObject.idle_add(self.on_layout_changed, controller)

if __name__ == "__main__":
    main = Main()