diff options
author | fujiwarat <takao.fujiwara1@gmail.com> | 2014-02-25 17:41:13 +0900 |
---|---|---|
committer | fujiwarat <takao.fujiwara1@gmail.com> | 2014-02-25 17:41:13 +0900 |
commit | b233f5796aa7eec7555a31d51ea065c978429759 (patch) | |
tree | 47b9d2600793bedaa11d2ec5a5986d1fb1076ca7 /ui | |
parent | b300a7f747ddfa5146ff6edd23f8747e37d1de46 (diff) | |
download | ibus-b233f5796aa7eec7555a31d51ea065c978429759.tar.gz |
Use XKB layout string instead of ibus-keyboard icon on panel.
Review URL: https://codereview.appspot.com/66330043
Diffstat (limited to 'ui')
-rw-r--r-- | ui/gtk3/Makefile.am | 3 | ||||
-rw-r--r-- | ui/gtk3/panel.vala | 99 | ||||
-rw-r--r-- | ui/gtk3/switcher.vala | 73 |
3 files changed, 147 insertions, 28 deletions
diff --git a/ui/gtk3/Makefile.am b/ui/gtk3/Makefile.am index 6012cfa0..b2fb8005 100644 --- a/ui/gtk3/Makefile.am +++ b/ui/gtk3/Makefile.am @@ -42,8 +42,6 @@ AM_CPPFLAGS = \ -include $(CONFIG_HEADER) \ $(NULL) -USE_SYMBOL_ICON = FALSE - AM_CFLAGS = \ @GLIB2_CFLAGS@ \ @GIO2_CFLAGS@ \ @@ -53,7 +51,6 @@ AM_CFLAGS = \ -DG_LOG_DOMAIN=\"IBUS\" \ -DBINDIR=\"$(bindir)\" \ -DIBUS_DISABLE_DEPRECATED \ - -DSWITCHER_USE_SYMBOL_ICON=$(USE_SYMBOL_ICON) \ -Wno-unused-variable \ -Wno-unused-but-set-variable \ -Wno-unused-function \ diff --git a/ui/gtk3/panel.vala b/ui/gtk3/panel.vala index 8cca3a7c..748cb32e 100644 --- a/ui/gtk3/panel.vala +++ b/ui/gtk3/panel.vala @@ -58,6 +58,11 @@ class Panel : IBus.PanelService { private Gtk.CssProvider m_css_provider; private int m_switcher_delay_time = 400; private bool m_use_system_keyboard_layout = false; + private GLib.HashTable<string, Gdk.Pixbuf> m_xkb_icon_pixbufs = + new GLib.HashTable<string, Gdk.Pixbuf>(GLib.str_hash, + GLib.str_equal); + private Gdk.RGBA m_xkb_icon_rgba = Gdk.RGBA(){ + red = 0.0, green = 0.0, blue = 0.0, alpha = 1.0 }; private GLib.List<Keybinding> m_keybindings = new GLib.List<Keybinding>(); @@ -171,6 +176,10 @@ class Panel : IBus.PanelService { .connect((key) => { set_follow_input_cursor_when_always_shown_property_panel(); }); + + m_settings_panel.changed["xkb-icon-rgba"].connect((key) => { + set_xkb_icon_rgba(); + }); } private void keybinding_manager_bind(KeybindingManager keybinding_manager, @@ -375,6 +384,26 @@ class Panel : IBus.PanelService { "follow-input-cursor-when-always-shown")); } + private void set_xkb_icon_rgba() { + string spec = m_settings_panel.get_string("xkb-icon-rgba"); + + Gdk.RGBA rgba = { 0, }; + + if (!rgba.parse(spec)) { + warning("invalid format of xkb-icon-rgba: %s", spec); + m_xkb_icon_rgba = Gdk.RGBA(){ + red = 0.0, green = 0.0, blue = 0.0, alpha = 1.0 }; + } else + m_xkb_icon_rgba = rgba; + + if (m_xkb_icon_pixbufs.size() > 0) { + m_xkb_icon_pixbufs.remove_all(); + + if (m_status_icon != null && m_switcher != null) + state_changed(); + } + } + private int compare_versions(string version1, string version2) { string[] version1_list = version1.split("."); string[] version2_list = version2.split("."); @@ -474,6 +503,7 @@ class Panel : IBus.PanelService { set_show_property_panel(); set_timeout_property_panel(); set_follow_input_cursor_when_always_shown_property_panel(); + set_xkb_icon_rgba(); set_version(); } @@ -664,6 +694,51 @@ class Panel : IBus.PanelService { } + private void context_render_string(Cairo.Context cr, + string symbol, + int image_width, + int image_height) { + int lwidth = 0; + int lheight = 0; + var desc = Pango.FontDescription.from_string("Monospace Bold 22"); + var layout = Pango.cairo_create_layout(cr); + + if (symbol.length >= 3) + desc = Pango.FontDescription.from_string("Monospace Bold 18"); + + layout.set_font_description(desc); + layout.set_text(symbol, -1); + layout.get_size(out lwidth, out lheight); + cr.move_to((image_width - lwidth / Pango.SCALE) / 2, + (image_height - lheight / Pango.SCALE) / 2); + cr.set_source_rgba(m_xkb_icon_rgba.red, + m_xkb_icon_rgba.green, + m_xkb_icon_rgba.blue, + m_xkb_icon_rgba.alpha); + Pango.cairo_show_layout(cr, layout); + } + + private Gdk.Pixbuf create_icon_pixbuf_with_string(string symbol) { + Gdk.Pixbuf pixbuf = m_xkb_icon_pixbufs[symbol]; + + if (pixbuf != null) + return pixbuf; + + var image = new Cairo.ImageSurface(Cairo.Format.ARGB32, 48, 48); + var cr = new Cairo.Context(image); + int width = image.get_width(); + int height = image.get_height(); + + cr.set_source_rgba(0.0, 0.0, 0.0, 0.0); + cr.set_operator(Cairo.Operator.SOURCE); + cr.paint(); + cr.set_operator(Cairo.Operator.OVER); + context_render_string(cr, symbol, width, height); + pixbuf = Gdk.pixbuf_get_from_surface(image, 0, 0, width, height); + m_xkb_icon_pixbufs.insert(symbol, pixbuf); + return pixbuf; + } + private void show_setup_dialog() { if (m_setup_pid != 0) { if (Posix.kill(m_setup_pid, Posix.SIGUSR1) == 0) @@ -699,8 +774,8 @@ class Panel : IBus.PanelService { m_about_dialog.set_version(Config.PACKAGE_VERSION); string copyright = - "Copyright © 2007-2013 Peng Huang\n" + - "Copyright © 2007-2013 Red Hat, Inc.\n"; + "Copyright © 2007-2014 Peng Huang\n" + + "Copyright © 2007-2014 Red Hat, Inc.\n"; m_about_dialog.set_copyright(copyright); m_about_dialog.set_license("LGPL"); @@ -930,11 +1005,23 @@ class Panel : IBus.PanelService { if (icon_name[0] == '/') m_status_icon.set_from_file(icon_name); else { - var theme = Gtk.IconTheme.get_default(); - if (theme.lookup_icon(icon_name, 48, 0) != null) { - m_status_icon.set_from_icon_name(icon_name); + string symbol = null; + + if (engine != null) { + var name = engine.get_name(); + if (name.length >= 4 && name[0:4] == "xkb:") + symbol = m_switcher.get_xkb_symbol(engine); + } + + if (symbol != null) { + Gdk.Pixbuf pixbuf = create_icon_pixbuf_with_string(symbol); + m_status_icon.set_from_pixbuf(pixbuf); } else { - m_status_icon.set_from_icon_name("ibus-engine"); + var theme = Gtk.IconTheme.get_default(); + if (theme.lookup_icon(icon_name, 48, 0) != null) + m_status_icon.set_from_icon_name(icon_name); + else + m_status_icon.set_from_icon_name("ibus-engine"); } } diff --git a/ui/gtk3/switcher.vala b/ui/gtk3/switcher.vala index 4b4c0639..e6e91661 100644 --- a/ui/gtk3/switcher.vala +++ b/ui/gtk3/switcher.vala @@ -2,7 +2,7 @@ * * ibus - The Input Bus * - * Copyright(c) 2011 Peng Huang <shawn.p.huang@gmail.com> + * Copyright(c) 2011-2014 Peng Huang <shawn.p.huang@gmail.com> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -21,12 +21,10 @@ */ class Switcher : Gtk.Window { - public extern const bool USE_SYMBOL_ICON; - private const int DEFAULT_FONT_SIZE = 16; private const int DESC_LABEL_MAX_LEN = 20; private class IBusEngineButton : Gtk.Button { - public IBusEngineButton(IBus.EngineDesc engine) { + public IBusEngineButton(IBus.EngineDesc engine, Switcher switcher) { GLib.Object(); this.longname = engine.get_longname(); @@ -34,28 +32,28 @@ class Switcher : Gtk.Window { Gtk.Alignment align = new Gtk.Alignment(0.5f, 0.5f, 0.0f, 0.0f); add(align); - if (!USE_SYMBOL_ICON) { + var name = engine.get_name(); + + if (name.length < 4 || name[0:4] != "xkb:") { IconWidget icon = new IconWidget(engine.get_icon(), Gtk.IconSize.DIALOG); align.add(icon); } else { - var language = engine.get_language(); - var symbol = engine.get_symbol(); - var id = language; + var symbol = switcher.get_xkb_symbol(engine); - if (id.length > 2) { - id = id[0:2]; - } + Gtk.Label label = new Gtk.Label(symbol); + string symbol_font = "Monospace Bold 16"; + string markup = "<span font=\"%s\">%s</span>". + printf(symbol_font, symbol); - if (symbol.length != 0) { - id = symbol; - } + label.set_markup(markup); - Gtk.Label label = new Gtk.Label(id); - string id_font = "%d".printf(DEFAULT_FONT_SIZE); - string markup = "<span font=\"%s\">%s</span>".printf(id_font, id); + int fixed_width, fixed_height; + Gtk.icon_size_lookup(Gtk.IconSize.DIALOG, + out fixed_width, + out fixed_height); + label.set_size_request(fixed_width, fixed_height); - label.set_markup(markup); align.add(label); } } @@ -90,6 +88,9 @@ class Switcher : Gtk.Window { private uint m_popup_delay_time_id = 0; private int m_root_x; private int m_root_y; + private GLib.HashTable<string, string> m_xkb_symbols = + new GLib.HashTable<string, string>(GLib.str_hash, + GLib.str_equal); public Switcher() { GLib.Object( @@ -246,7 +247,7 @@ class Switcher : Gtk.Window { for (int i = 0; i < m_engines.length; i++) { var index = i; var engine = m_engines[i]; - var button = new IBusEngineButton(engine); + var button = new IBusEngineButton(engine, this); var longname = engine.get_longname(); button.set_relief(Gtk.ReliefStyle.NONE); button.show(); @@ -424,4 +425,38 @@ class Switcher : Gtk.Window { public void set_popup_delay_time(uint popup_delay_time) { m_popup_delay_time = popup_delay_time; } + + public string get_xkb_symbol(IBus.EngineDesc engine) { + var name = engine.get_name(); + + assert(name[0:4] == "xkb:"); + + var symbol = m_xkb_symbols[name]; + + if (symbol != null) + return symbol; + + var layout = engine.get_layout(); + + /* Maybe invalid layout */ + if (layout.length < 2) + return layout; + + symbol = layout[0:2].up(); + + int index = 0; + + foreach (var saved_symbol in m_xkb_symbols.get_values()) { + if (symbol == saved_symbol[0:2]) + index++; + } + + if (index > 0) { + unichar u = 0x2081 + index; + symbol = "%s%s".printf(symbol, u.to_string()); + } + + m_xkb_symbols.insert(name, symbol); + return symbol; + } } |