def on_draw(self, widget, cr, data=None): """ GtkEntry always makes its icons menu-sized, no matter how much actual space is available for the image. So, we use a transparent icon in update_layout_icon(), just so GtkEntry thinks there's an icon there, that way it allocates space for it, and responds to clicks in the area. """ if not self.keyboard_controller.get_enabled(): return False icon_rect = widget.get_icon_area(Gtk.EntryIconPosition.PRIMARY) x = icon_rect.x y = icon_rect.y + 2 width = (icon_rect.width // 2) * 2 height = icon_rect.height - 4 handled = False if settings.get_show_flags(): name = self.keyboard_controller.get_current_icon_name() if name: filename = "/usr/share/iso-flag-png/%s.png" % name try: pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size( filename, -1, height) render_x = (x + (width / 2) - (pixbuf.get_width() / 2)) render_y = (y + (height / 2) - (pixbuf.get_height() / 2)) Gdk.cairo_set_source_pixbuf(cr, pixbuf, render_x, render_y) cr.paint() self.keyboard_controller.render_cairo_subscript( cr, render_x + (pixbuf.get_width() / 2), render_y + (pixbuf.get_height() / 2), pixbuf.get_width() / 2, pixbuf.get_height() / 2, self.keyboard_controller.get_current_flag_id()) handled = True except GLib.Error: pass if not handled: if settings.get_use_layout_variant_names(): name = self.keyboard_controller.get_current_variant_label() else: name = self.keyboard_controller.get_current_short_group_label() if settings.get_show_upper_case_layout(): name = name.upper() ctx = widget.get_style_context() ctx.save() ctx.set_state(Gtk.StateFlags.BACKDROP) font_size = ctx.get_property("font-size", Gtk.StateFlags.BACKDROP) family = ctx.get_property("font-family", Gtk.StateFlags.BACKDROP) cr.select_font_face(family[0], cairo.FONT_WEIGHT_NORMAL, cairo.FONT_SLANT_NORMAL) cr.set_font_size(font_size) (xb, yb, w, h, xa, ya) = cr.text_extents(name) # Drop shadow for visibility - 1px, 1px cr.set_source_rgba(0, 0, 0, 0.8) cr.move_to((x + (width / 2) - (w / 2)) + 1, (y + (height / 2) + (h / 2) + 1)) cr.show_text(name) # Text text_color = widget.get_style_context().get_color( Gtk.StateFlags.BACKDROP) Gdk.cairo_set_source_rgba(cr, text_color) cr.move_to((x + (width / 2) - (w / 2)), (y + (height / 2) + (h / 2))) cr.show_text(name) ctx.restore() return False
def on_draw(self, widget, cr, data=None): """ GtkEntry always makes its icons menu-sized, no matter how much actual space is available for the image. So, we use a transparent icon in update_layout_icon(), just so GtkEntry thinks there's an icon there, that way it allocates space for it, and responds to clicks in the area. """ icon_rect = widget.get_icon_area(Gtk.EntryIconPosition.PRIMARY) x = icon_rect.x y = icon_rect.y + 2 width = (icon_rect.width // 2) * 2 height = icon_rect.height - 4 handled = False if settings.get_show_flags(): name = self.keyboard_controller.get_current_icon_name() if name: filename = "/usr/share/iso-flag-png/%s.png" % name try: pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(filename, -1, height) render_x = (x + (width / 2) - (pixbuf.get_width() / 2)) render_y = (y + (height / 2) - (pixbuf.get_height() / 2)) Gdk.cairo_set_source_pixbuf(cr, pixbuf, render_x, render_y) cr.paint() self.keyboard_controller.render_cairo_subscript(cr, render_x + (pixbuf.get_width() / 2), render_y + (pixbuf.get_height() / 2), pixbuf.get_width() / 2, pixbuf.get_height() / 2, self.keyboard_controller.get_current_flag_id()) handled = True except GLib.Error: pass if not handled: if settings.get_use_layout_variant_names(): name = self.keyboard_controller.get_current_variant_label() else: name = self.keyboard_controller.get_current_short_group_label() if settings.get_show_upper_case_layout(): name = name.upper() ctx = widget.get_style_context() font_size = ctx.get_property("font-size", Gtk.StateFlags.BACKDROP) family = ctx.get_property("font-family", Gtk.StateFlags.BACKDROP) cr.select_font_face(family[0], cairo.FONT_WEIGHT_NORMAL, cairo.FONT_SLANT_NORMAL) cr.set_font_size(font_size) (xb, yb, w, h, xa, ya) = cr.text_extents(name) # Drop shadow for visibility - 1px, 1px cr.set_source_rgba(0, 0, 0, 0.8) cr.move_to((x + (width / 2) - (w / 2)) + 1, (y + (height / 2) + (h / 2) + 1)) cr.show_text(name) # Text text_color = widget.get_style_context().get_color(Gtk.StateFlags.BACKDROP) Gdk.cairo_set_source_rgba(cr, text_color) cr.move_to((x + (width / 2) - (w / 2)), (y + (height / 2) + (h / 2))) cr.show_text(name) return False