|
| 1 | +/* |
| 2 | +* Copyright (c) 2018 elementary, Inc. (https://elementary.io) |
| 3 | +* |
| 4 | +* This program is free software; you can redistribute it and/or |
| 5 | +* modify it under the terms of the GNU General Public |
| 6 | +* License as published by the Free Software Foundation; either |
| 7 | +* version 2 of the License, or (at your option) any later version. |
| 8 | +* |
| 9 | +* This program is distributed in the hope that it will be useful, |
| 10 | +* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | +* General Public License for more details. |
| 13 | +* |
| 14 | +* You should have received a copy of the GNU General Public |
| 15 | +* License along with this program; if not, write to the |
| 16 | +* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | +* Boston, MA 02110-1301 USA |
| 18 | +*/ |
| 19 | + |
| 20 | +// From https://github.com/elementary/granite/blob/2066b377226cf327cb2d5399b6b40a2d36d47b11/lib/Widgets/ModeSwitch.vala |
| 21 | + |
| 22 | +/** |
| 23 | + * ModeSwitch is a selection control for choosing between two options that can be described with an icon. |
| 24 | + * |
| 25 | + * ''Example''<<BR>> |
| 26 | + * {{{ |
| 27 | + * var gtk_settings = Gtk.Settings.get_default (); |
| 28 | + * |
| 29 | + * var mode_switch = new ModeSwitch.from_icon_name ("display-brightness-symbolic", "weather-clear-night-symbolic"); |
| 30 | + * mode_switch.primary_icon_tooltip_text = _("Light background"); |
| 31 | + * mode_switch.secondary_icon_tooltip_text = _("Dark background"); |
| 32 | + * mode_switch.bind_property ("active", gtk_settings, "gtk_application_prefer_dark_theme"); |
| 33 | + * }}} |
| 34 | + */ |
| 35 | +public class Granite.ModeSwitch : Gtk.Grid { |
| 36 | + /** |
| 37 | + * Whether the {@link Gtk.Switch} widget is pointing to the secondary icon or not. |
| 38 | + */ |
| 39 | + public bool active { get; set; } |
| 40 | + |
| 41 | + /** |
| 42 | + * The {@link GLib.Icon} to use for the primary icon for the switch. |
| 43 | + */ |
| 44 | + public GLib.Icon primary_icon_gicon { get; construct set; } |
| 45 | + |
| 46 | + /** |
| 47 | + * The icon name to use for the primary icon for the switch. |
| 48 | + */ |
| 49 | + public string primary_icon_name { get; construct set; } |
| 50 | + |
| 51 | + /** |
| 52 | + * The contents of the tooltip on the primary icon. |
| 53 | + */ |
| 54 | + public string primary_icon_tooltip_text { get; set; } |
| 55 | + |
| 56 | + /** |
| 57 | + * The {@link GLib.Icon} to use for the secondary icon for the switch. |
| 58 | + */ |
| 59 | + public GLib.Icon secondary_icon_gicon { get; construct set; } |
| 60 | + |
| 61 | + /** |
| 62 | + * The icon name to use for the secondary icon for the switch. |
| 63 | + */ |
| 64 | + public string secondary_icon_name { get; construct set; } |
| 65 | + |
| 66 | + /** |
| 67 | + * The contents of the tooltip on the secondary icon. |
| 68 | + */ |
| 69 | + public string secondary_icon_tooltip_text { get; set; } |
| 70 | + |
| 71 | + /** |
| 72 | + * Constructs a new {@link Granite.ModeSwitch} using {@link GLib.Icon}s. |
| 73 | + * |
| 74 | + * @param primary_icon_gicon The {@link GLib.Icon} to use for the primary icon for the switch. |
| 75 | + * @param secondary_icon_gicon The {@link GLib.Icon} to use for the secondary icon for the switch. |
| 76 | + */ |
| 77 | + public ModeSwitch (GLib.Icon primary_icon_gicon, GLib.Icon secondary_icon_gicon) { |
| 78 | + Object ( |
| 79 | + primary_icon_gicon: primary_icon_gicon, |
| 80 | + secondary_icon_gicon: secondary_icon_gicon |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Constructs a new {@link Granite.ModeSwitch} from icon names. |
| 86 | + * |
| 87 | + * @param primary_icon_name The icon name to use for the primary icon for the switch. |
| 88 | + * @param secondary_icon_name The icon name to use for the secondary icon for the switch. |
| 89 | + */ |
| 90 | + public ModeSwitch.from_icon_name (string primary_icon_name, string secondary_icon_name) { |
| 91 | + Object ( |
| 92 | + primary_icon_gicon: new ThemedIcon (primary_icon_name), |
| 93 | + secondary_icon_gicon: new ThemedIcon (secondary_icon_name), |
| 94 | + primary_icon_name: primary_icon_name, |
| 95 | + secondary_icon_name: secondary_icon_name |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + construct { |
| 100 | + var primary_image = new Gtk.Image (); |
| 101 | + primary_image.pixel_size = 16; |
| 102 | + |
| 103 | + var primary_icon_box = new Gtk.EventBox (); |
| 104 | + primary_icon_box.add_events (Gdk.EventMask.BUTTON_RELEASE_MASK); |
| 105 | + primary_icon_box.add (primary_image); |
| 106 | + |
| 107 | + var mode_switch = new Gtk.Switch (); |
| 108 | + mode_switch.valign = Gtk.Align.CENTER; |
| 109 | + mode_switch.get_style_context ().add_class ("mode-switch"); |
| 110 | + |
| 111 | + var secondary_icon = new Gtk.Image (); |
| 112 | + secondary_icon.pixel_size = 16; |
| 113 | + |
| 114 | + var secondary_icon_box = new Gtk.EventBox (); |
| 115 | + secondary_icon_box.add_events (Gdk.EventMask.BUTTON_RELEASE_MASK); |
| 116 | + secondary_icon_box.add (secondary_icon); |
| 117 | + |
| 118 | + column_spacing = 6; |
| 119 | + add (primary_icon_box); |
| 120 | + add (mode_switch); |
| 121 | + add (secondary_icon_box); |
| 122 | + |
| 123 | + bind_property ("primary-icon-gicon", primary_image, "gicon", GLib.BindingFlags.SYNC_CREATE); |
| 124 | + bind_property ("primary-icon-name", primary_image, "icon-name", GLib.BindingFlags.SYNC_CREATE); |
| 125 | + bind_property ("primary-icon-tooltip-text", primary_image, "tooltip-text"); |
| 126 | + bind_property ("secondary-icon-gicon", secondary_icon, "gicon", GLib.BindingFlags.SYNC_CREATE); |
| 127 | + bind_property ("secondary-icon-name", secondary_icon, "icon_name", GLib.BindingFlags.SYNC_CREATE); |
| 128 | + bind_property ("secondary-icon-tooltip-text", secondary_icon, "tooltip-text"); |
| 129 | + |
| 130 | + this.notify["active"].connect (() => { |
| 131 | + if (Gtk.StateFlags.DIR_RTL in get_state_flags ()) { |
| 132 | + mode_switch.active = !active; |
| 133 | + } else { |
| 134 | + mode_switch.active = active; |
| 135 | + } |
| 136 | + }); |
| 137 | + |
| 138 | + mode_switch.notify["active"].connect (() => { |
| 139 | + if (Gtk.StateFlags.DIR_RTL in get_state_flags ()) { |
| 140 | + active = !mode_switch.active; |
| 141 | + } else { |
| 142 | + active = mode_switch.active; |
| 143 | + } |
| 144 | + }); |
| 145 | + |
| 146 | + primary_icon_box.button_release_event.connect (() => { |
| 147 | + active = false; |
| 148 | + return Gdk.EVENT_STOP; |
| 149 | + }); |
| 150 | + |
| 151 | + secondary_icon_box.button_release_event.connect (() => { |
| 152 | + active = true; |
| 153 | + return Gdk.EVENT_STOP; |
| 154 | + }); |
| 155 | + } |
| 156 | +} |
| 157 | + |
0 commit comments