summaryrefslogtreecommitdiffstats
path: root/multi-monitors-add-on/multi-monitors-add-on@spin83/indicator.js
blob: 8b500eeb6d12426e8358a36e623a9f6f1c1b699c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
Copyright (C) 2014  spin83

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, visit https://www.gnu.org/licenses/.
*/

const { St, Gio, GLib, GObject } = imports.gi;

const Util = imports.misc.util;
const Main = imports.ui.main;
const PanelMenu = imports.ui.panelMenu;

const Gettext = imports.gettext.domain('multi-monitors-add-on');
const _ = Gettext.gettext;
const CE = imports.misc.extensionUtils.getCurrentExtension();
const MultiMonitors = CE.imports.extension;
const Convenience = CE.imports.convenience;
const extensionPath = CE.path;

var MultiMonitorsIndicator = (() => {
	let MultiMonitorsIndicator = class MultiMonitorsIndicator extends PanelMenu.Button {
		_init() {
			super._init(0.0, "MultiMonitorsAddOn", false);

			Convenience.initTranslations();

			this.text = null;
			this._mmStatusIcon = new St.BoxLayout({ style_class: 'multimonitor-status-indicators-box' });
			this._mmStatusIcon.hide();
			this.add_child(this._mmStatusIcon);
			this._leftRightIcon = true;
			this.menu.addAction(_("Preferences"), this._onPreferences.bind(this));
			this._viewMonitorsId = Main.layoutManager.connect('monitors-changed', this._viewMonitors.bind(this));
			this._viewMonitors();
		}
			
		_onDestroy() {
			Main.layoutManager.disconnect(this._viewMonitorsId);
			super._onDestroy();
		}

	    _syncIndicatorsVisible() {
	        this._mmStatusIcon.visible = this._mmStatusIcon.get_children().some(a => a.visible);
	    }

	    _icon_name (icon, iconName) {
	    	icon.set_gicon(Gio.icon_new_for_string(extensionPath+"/icons/"+iconName+".svg"));
	    }

		_viewMonitors() {
			let monitors = this._mmStatusIcon.get_children();
		
			let monitorChange = Main.layoutManager.monitors.length - monitors.length;
			if(monitorChange>0){
				global.log("Add Monitors ...");
				for(let idx = 0; idx<monitorChange; idx++){
					let icon;
					icon = new St.Icon({style_class: 'system-status-icon multimonitor-status-icon'});
					this._mmStatusIcon.add_child(icon);
					icon.connect('notify::visible', this._syncIndicatorsVisible.bind(this));
					
					if (this._leftRightIcon)
						this._icon_name(icon, 'multi-monitors-l-symbolic');
					else
						this._icon_name(icon, 'multi-monitors-r-symbolic');
					this._leftRightIcon = !this._leftRightIcon;
				}
				this._syncIndicatorsVisible();
			}
			else if(monitorChange<0){
				global.log("Remove Monitors ...");
				monitorChange = -monitorChange;
				
				for(let idx = 0; idx<monitorChange; idx++){
					let icon = this._mmStatusIcon.get_last_child();
					this._mmStatusIcon.remove_child(icon);
					icon.destroy();
					this._leftRightIcon = !this._leftRightIcon;
				}
			}
		}

		_onPreferences() {
			const uuid = "multi-monitors-add-on@spin83";
			Gio.DBus.session.call(
				'org.gnome.Shell.Extensions',
				'/org/gnome/Shell/Extensions',
				'org.gnome.Shell.Extensions',
				'OpenExtensionPrefs',
				new GLib.Variant('(ssa{sv})', [uuid, '', {}]),
				null,
				Gio.DBusCallFlags.NONE,
				-1,
				null);
		}
	};
	return GObject.registerClass(MultiMonitorsIndicator);
})();