-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
125 lines (101 loc) · 2.88 KB
/
extension.js
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
'use strict';
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const GObject = imports.gi.GObject;
const St = imports.gi.St;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Main = imports.ui.main;
const PanelMenu = imports.ui.panelMenu;
const Slider = imports.ui.slider;
const PopupMenu = imports.ui.popupMenu;
const MaxOpacity = 150;
const MinOpacity = 0;
var monitors = Main.layoutManager.monitors;
var indicator = null;
var box = null;
var currentOpacity = MaxOpacity;
var popup;
var slider;
var totalWidth = 0;
// Extend the Button class from Panel Menu and do some setup in the init() function.
var DimIndicator = class DimIndicator extends PanelMenu.Button
{
_init()
{
super._init(0.0, `${Me.metadata.name} Indicator`, false);
// Set an icon in the top menu
let icon = new St.Icon({
gicon: new Gio.ThemedIcon({name: 'display-brightness-symbolic'}),
style_class: 'system-status-icon'
});
this.actor.add_child(icon);
// Add a popup and append to the menu
this.popup = new PopupMenu.PopupBaseMenuItem({ activate: false });
this.menu.addMenuItem(this.popup);
// Create a slider and add to the popup
this.slider = new Slider.Slider(0);
this.slider.value = currentOpacity;
this.slider.connect('notify::value', this.sliderChanged.bind(this));
this.popup.actor.add(this.slider.actor);
// Add enable and disable buttons
this.menu.addAction('Enable', this.dimAction.bind(), null);
this.menu.addAction('Disable', this.undimAction.bind(), null);
// Generate box and set on the screen
this.generateBox();
// Set prefered opacity for the first time
this.updateAction();
}
generateBox()
{
if( ! box)
{
box = new St.Label({ style_class: 'dim-label', text: "" });
}
// Make sure the width spans all screens
for (let i = 0; i < monitors.length; i++) {
totalWidth = totalWidth + monitors[i].width;
}
box.set_position(0, 0);
box.set_width(totalWidth);
box.set_height(monitors[0].height);
Main.uiGroup.add_actor(box);
}
sliderChanged(slider)
{
currentOpacity = parseInt(slider.value * MaxOpacity);
this.updateAction();
}
updateAction()
{
box.opacity = currentOpacity;
}
dimAction()
{
box.opacity = MaxOpacity;
indicator.slider.value = MaxOpacity;
}
undimAction()
{
box.opacity = MinOpacity;
indicator.slider.value = MinOpacity;
}
}
function init()
{
DimIndicator = GObject.registerClass({GTypeName: 'DimIndicator'}, DimIndicator);
}
function enable()
{
indicator = new DimIndicator();
Main.panel.addToStatusArea(`${Me.metadata.name} Indicator`, indicator);
}
function disable()
{
Main.uiGroup.remove_actor(box);
if (indicator !== null)
{
indicator.destroy();
indicator = null;
}
}