forked from MattByName/color-tint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
188 lines (160 loc) · 4.84 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
"use strict";
import Clutter from "gi://Clutter";
import Gio from "gi://Gio";
import GObject from "gi://GObject";
import St from "gi://St";
import * as Main from "resource:///org/gnome/shell/ui/main.js";
import * as PopupMenu from "resource:///org/gnome/shell/ui/popupMenu.js";
import * as PanelMenu from "resource:///org/gnome/shell/ui/panelMenu.js";
import * as Slider from "resource:///org/gnome/shell/ui/slider.js";
import { Extension } from "resource:///org/gnome/shell/extensions/extension.js";
const BrightnessEffectName = "brightness-effect";
let tinter = null;
let metadata = null;
let menu = null;
let overlay = {
active: false,
brightness: 100,
};
export default class AlphaTinter extends Extension {
constructor(metadata) {
super(metadata);
}
enable() {
tinter = this;
metadata = this.metadata;
this.start_up();
menu = new MenuButton();
Main.panel.addToStatusArea("Tint", menu, 0, "right");
}
disable() {
tinter.stop_now();
menu.destroy();
menu = null;
metadata = null;
tinter = null;
}
// Create Tint Overlay
createOverlay() {
this._effect = new Clutter.BrightnessContrastEffect();
this.setOverlayBrightness();
}
// Update color of overlay
setOverlayBrightness() {
this._effect.brightness = Clutter.Color.new(
overlay["brightness"],
overlay["brightness"],
overlay["brightness"],
255
);
if (overlay.active) {
Main.uiGroup.remove_effect_by_name(BrightnessEffectName);
Main.uiGroup.add_effect_with_name(BrightnessEffectName, this._effect);
}
this.saveState();
}
// Hide overlay
hide() {
overlay.active = false;
Main.uiGroup.remove_effect_by_name(BrightnessEffectName);
this.saveState();
}
// Show overlay
show() {
Main.uiGroup.add_effect_with_name(BrightnessEffectName, this._effect);
overlay.active = true;
this.saveState();
}
// Load state
loadState() {
// Load last from json
this._file = Gio.file_new_for_path(`${metadata.path}/settings.json`);
if (this._file.query_exists(null)) {
let [flag, data] = this._file.load_contents(null);
if (flag) {
const textDecoder = new TextDecoder();
let prepData =
data instanceof Uint8Array
? textDecoder.decode(data)
: data.toString();
overlay = JSON.parse(prepData);
}
}
}
// Save state
saveState() {
this._file = Gio.file_new_for_path(`${metadata.path}/settings.json`);
this._file.replace_contents(JSON.stringify(overlay), null, false, 0, null);
}
// Enable
start_up() {
overlay.active = false;
this.loadState();
this.createOverlay();
}
// Disable
stop_now() {
if (overlay.active == true) {
Main.uiGroup.remove_effect_by_name(BrightnessEffectName);
}
}
}
const MenuButton = GObject.registerClass(
{
GTypeName: "MenuButton",
},
class MenuButton extends PanelMenu.Button {
// Constructor
_init() {
super._init(1, "AlphaTintMenu", false);
let icon = new St.Icon({
icon_name: "display-brightness-symbolic",
style_class: "system-status-icon",
});
// We add the icon to the button
// It will be showed in the Top Panel
this.add_child(icon);
let popupMenuExpander = new PopupMenu.PopupSubMenuMenuItem(
"PopupSubMenuMenuItem"
);
// This is an example of PopupMenuItem, a menu item. We will use this to add as a submenu
let submenu = new PopupMenu.PopupMenuItem("PopupMenuItem");
popupMenuExpander.menu.addMenuItem(submenu);
let tintSwitch = new PopupMenu.PopupSwitchMenuItem(
"Tint",
overlay.active
);
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
this.menu.addMenuItem(tintSwitch);
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
let toggleTint = function (_object, value) {
if (value) {
tinter.show();
} else {
tinter.hide();
}
};
tintSwitch.connect("toggled", toggleTint.bind(tintSwitch));
this._alphaSlider = new Slider.Slider(0);
let _alphaLabel = new St.Label({ text: "Brightness" });
this._alphaSliderContainer = new PopupMenu.PopupBaseMenuItem({
activate: false,
});
this._alphaSliderContainer.add_child(_alphaLabel);
this._alphaSliderContainer.add_child(this._alphaSlider);
this.menu.addMenuItem(this._alphaSliderContainer);
this._alphaSlider.connect(
"notify::value",
this._setBrightness.bind(this._alphaSlider)
);
this._getBrightness(this._alphaSlider);
}
_getBrightness(slider) {
slider._setCurrentValue(slider, (overlay["brightness"] - 64) / 63);
}
_setBrightness(slider) {
overlay["brightness"] = 64 + slider._getCurrentValue() * 63;
tinter.setOverlayBrightness();
}
}
);