forked from JackKenney/gnome-true-color-invert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
177 lines (153 loc) · 4.52 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
const Main = imports.ui.main;
const GObject = imports.gi.GObject;
const Meta = imports.gi.Meta;
const Shell = imports.gi.Shell;
const Clutter = imports.gi.Clutter;
const ExtensionUtils = imports.misc.extensionUtils;
const Self = ExtensionUtils.getCurrentExtension();
const SHORTCUT = 'invert-window-shortcut';
const levels = [
{
"r": 1.00000000,
"g": 0.54360078,
"b": 0.08679949,
},
{
"r": 1.00000000,
"g": 0.82854786,
"b": 0.64816570,
}, // 4K
{
"r": 1.00000000,
"g": 1.00000000,
"b": 1.00000000,
}, // 6.5K
]
const TrueInvertWindowEffect = new GObject.registerClass({
Name: 'TrueInvertWindowEffect',
Properties: {
'level': GObject.ParamSpec.double(
'level',
'Number Property',
'A property holding a JavaScript Number',
GObject.ParamFlags.READWRITE,
Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER,
0),
},
}, class TrueInvertWindowEffect extends Clutter.ShaderEffect {
vfunc_get_static_shader_source() {
return `
uniform bool invert_color;
uniform float opacity = 1.0;
uniform sampler2D tex;
/**
* based on shift_whitish.glsl https://github.com/vn971/linux-color-inversion with minor edits
*/
void main() {
vec4 c = texture2D(tex, cogl_tex_coord_in[0].st);
float white_bias = c.a * 0.1; // lower -> higher contrast
float m = 1.0 + white_bias;
float shift = white_bias + c.a - min(c.r, min(c.g, c.b)) - max(c.r, max(c.g, c.b));
c = vec4( c.r*${levels[this.level]["r"]}, c.g*${levels[this.level]["g"]}, c.b*${levels[this.level]["b"]} , c.a);
cogl_color_out = c;
}
`;
}
vfunc_paint_target(paint_node = null, paint_context = null) {
this.set_uniform_value("tex", 0);
if (paint_node && paint_context)
super.vfunc_paint_target(paint_node, paint_context);
else if (paint_node)
super.vfunc_paint_target(paint_node);
else
super.vfunc_paint_target();
}
});
let invert_window;
function onWindowDestroyed(display, window) {
// Handle window destruction
log("Window destroyed: " + window.get_title());
}
function InvertWindow() {
this.settings = ExtensionUtils.getSettings(Self.metadata["settings-schema"]);
this.monitor = -1
this.level = 0
}
InvertWindow.prototype = {
toggle_effect: function () {
global.get_window_actors().forEach(function (actor) {
let meta_window = actor.get_meta_window();
if(this.level === -1 || meta_window.get_monitor() !== this.monitor) {
actor.remove_effect_by_name(`invert-color-0`);
actor.remove_effect_by_name(`invert-color-1`);
actor.remove_effect_by_name(`invert-color-2`);
delete meta_window._invert_window_tag;
}else {
let effect = new TrueInvertWindowEffect({
level: this.level
});
if(actor.get_effect(`invert-color-${this.level - 1}`)) {
actor.remove_effect_by_name(`invert-color-${this.level - 1}`)
}
if(!actor.get_effect(`invert-color-${this.level}`) && meta_window.get_monitor() === this.monitor) {
actor.add_effect_with_name(`invert-color-${this.level}`, effect);
meta_window._invert_window_tag = true;
}
}
}, this);
},
toggle_effect_shortcut: function () {
if(this.level === undefined) {
this.level = -1
}
const focusedWindow = global.display.get_focus_window();
this.monitor = focusedWindow.get_monitor();
if(this.level === 2) {
this.level = -1;
}else {
this.level += 1;
}
invert_window.level = this.level
invert_window.monitor = this.monitor
invert_window.toggle_effect()
},
enable: function () {
this.level = -1
Main.wm.addKeybinding(
SHORTCUT,
this.settings,
Meta.KeyBindingFlags.NONE,
Shell.ActionMode.NORMAL,
this.toggle_effect_shortcut
);
global.display.connect('window-created', this.toggle_effect);
global.display.connect('window-entered-monitor', this.toggle_effect);
global.display.connect('window-left-monitor', this.toggle_effect);
global.get_window_actors().forEach(function (actor) {
let meta_window = actor.get_meta_window();
if (meta_window.hasOwnProperty('_invert_window_tag')) {
let effect = new TrueInvertWindowEffect({
level: this.level
});
actor.add_effect_with_name(`invert-color-${this.level}`, effect);
}
}, this);
},
disable: function () {
Main.wm.removeKeybinding(SHORTCUT);
global.display.disconnect('window-created', onWindowDestroyed);
global.get_window_actors().forEach(function (actor) {
actor.remove_effect_by_name('invert-color');
}, this);
}
};
function init() {
}
function enable() {
invert_window = new InvertWindow();
invert_window.enable();
}
function disable() {
invert_window.disable();
invert_window = null;
}