Package list xapp / 4227ba0
xapp-kbd-layout-controller - update test script to reflect current api/intent Michael Webster 4 years ago
1 changed file(s) with 63 addition(s) and 11 deletion(s). Raw diff Collapse all Expand all
1010 gi.require_version('Gtk', '3.0')
1111 gi.require_version('XApp', '1.0')
1212
13 from gi.repository import Gtk, XApp, GObject, Gkbd, GdkPixbuf
13 from gi.repository import Gtk, XApp, GObject
14 import cairo
1415
1516 signal.signal(signal.SIGINT, signal.SIG_DFL)
1617
1718 class Main:
1819 def __init__(self):
1920 win = Gtk.Window()
21
22 win.set_default_size(320, 200)
23
2024 frame = Gtk.Frame()
2125 frame.set_margin_start(2)
2226 frame.set_margin_end(2)
4347 self.label.set_text(self.controller.get_current_name())
4448 box.pack_start(self.label, True, True, 4)
4549
50 hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
51 box.pack_start(hbox, True, True, 4)
52
4653 self.flag_button = Gtk.Button()
47 box.pack_start(self.flag_button, True, True, 4)
54 hbox.pack_start(self.flag_button, True, True, 4)
4855 self.flag_button.connect("clicked", self.on_button_clicked)
4956
5057 self.group_button = Gtk.Button()
51 box.pack_start(self.group_button, True, True, 4)
58 hbox.pack_start(self.group_button, True, True, 4)
5259 self.group_button.connect("clicked", self.on_button_clicked)
5360
5461 self.variant_button = Gtk.Button()
55 box.pack_start(self.variant_button, True, True, 4)
62 hbox.pack_start(self.variant_button, True, True, 4)
5663 self.variant_button.connect("clicked", self.on_button_clicked)
5764
5865 check = Gtk.CheckButton.new_with_label("Use caps")
8188
8289 name = self.controller.get_current_icon_name()
8390 if name != None:
84 filename = "/usr/share/flags/iso-4x3-svg/%s.svgz" % name
91 filename = "/usr/share/iso-flag-png/%s.png" % name
8592
86 valid, width, height = Gtk.IconSize.lookup(Gtk.IconSize.BUTTON)
87 pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(filename, -1, height)
93 if os.path.exists(filename):
94 valid, width, height = Gtk.IconSize.lookup(Gtk.IconSize.LARGE_TOOLBAR)
8895
89 image = Gtk.Image.new_from_pixbuf(pixbuf)
90 self.flag_button.set_image(image)
91 handled = True
96 surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
97 flag_surface = cairo.ImageSurface.create_from_png(filename)
98
99 cr = cairo.Context(surface)
100 cr.save()
101
102 factor = width / flag_surface.get_width()
103 true_width = flag_surface.get_width() * factor
104 true_height = flag_surface.get_height() * factor
105
106 x_offset = y_offset = 0
107
108 if flag_surface.get_width() >= flag_surface.get_height():
109 x_offset = 0
110 y_offset = ((height * (1 / factor)) - flag_surface.get_height()) / 2
111 else:
112 x_offset = ((width * (1 / factor)) - flag_surface.get_width()) / 2
113 y_offset = 0
114
115 true_x_offset = (width - true_width) / 2;
116 true_y_offset = (height - true_height) / 2;
117
118 cr.scale(factor, factor);
119
120 cr.set_source_surface(flag_surface, x_offset, y_offset);
121
122 cr.get_source().set_filter(cairo.FILTER_BEST);
123 cr.set_operator(cairo.OPERATOR_SOURCE);
124
125 cr.paint();
126 cr.restore()
127
128 XApp.KbdLayoutController.render_cairo_subscript(cr,
129 true_x_offset + (true_width / 2),
130 true_y_offset + (true_height / 2),
131 true_width / 2,
132 true_height / 2,
133 self.controller.get_current_flag_id())
134
135 image = Gtk.Image.new_from_surface(cr.get_target())
136 self.flag_button.set_image(image)
137 handled = True
138 else:
139 print("Missing /usr/share/iso-flag-png flag image - text only")
92140
93141 if not handled:
94142 name = self.controller.get_current_short_group_label()
95 self.flag_button.set_label(name.upper())
143 if self.use_caps:
144 self.flag_button.set_label(name.upper())
145 else:
146 self.flag_button.set_label(name)
147
96148 self.flag_button.set_image(None)
97149
98150 self.label.set_text(self.controller.get_current_name())