|
| 1 | +const Applet = imports.ui.applet; |
| 2 | +const Gio = imports.gi.Gio; |
| 3 | +const PopupMenu = imports.ui.popupMenu; |
| 4 | +const Util = imports.misc.util; |
| 5 | +const St = imports.gi.St; |
| 6 | + |
| 7 | +class NightLightSwitch extends Applet.IconApplet { |
| 8 | + constructor(metadata, orientation, panelHeight, instance_id) { |
| 9 | + super(orientation, panelHeight, instance_id); |
| 10 | + |
| 11 | + this.gsettings = Gio.Settings.new("org.cinnamon.settings-daemon.plugins.color"); |
| 12 | + this.nightLightEnabled = this.gsettings.get_boolean("night-light-enabled"); |
| 13 | + this.connectColorID = this.gsettings.connect("changed", () => this.set_icon()); |
| 14 | + this.set_icon(); |
| 15 | + |
| 16 | + let items = this._applet_context_menu._getMenuItems(); |
| 17 | + if (this.context_menu_item_configure == null) { |
| 18 | + this.context_menu_item_configure = new PopupMenu.PopupIconMenuItem(_("Configure..."), |
| 19 | + "system-run", |
| 20 | + St.IconType.SYMBOLIC); |
| 21 | + this.context_menu_item_configure.connect('activate', |
| 22 | + () => { Util.spawnCommandLineAsync("cinnamon-settings nightlight"); } |
| 23 | + ); |
| 24 | + } |
| 25 | + if (items.indexOf(this.context_menu_item_configure) == -1) { |
| 26 | + this._applet_context_menu.addMenuItem(this.context_menu_item_configure); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + on_applet_clicked() { |
| 31 | + this.gsettings.set_boolean("night-light-enabled", !this.nightLightEnabled); |
| 32 | + this.set_icon(); |
| 33 | + } |
| 34 | + |
| 35 | + set_icon() { |
| 36 | + this.nightLightEnabled = this.gsettings.get_boolean("night-light-enabled"); |
| 37 | + if (this.nightLightEnabled) { |
| 38 | + this.set_applet_icon_symbolic_name("nightlight-symbolic"); |
| 39 | + } else { |
| 40 | + this.set_applet_icon_symbolic_name("nightlight-disabled-symbolic"); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + on_applet_removed_from_panel() { |
| 45 | + this.gsettings.disconnect(this.connectColorID); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +function main(metadata, orientation, panel_height, instance_id) { |
| 50 | + return new NightLightSwitch(metadata, orientation, panel_height, instance_id); |
| 51 | +} |
0 commit comments