Skip to content

Commit f018f46

Browse files
committed
JS refactoring to proper private fields / methods
- Added also vscode project settings file
1 parent c1d8191 commit f018f46

11 files changed

+442
-473
lines changed

.vscode/settings.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"editor.tabSize": 2,
3+
"[javascript]": {
4+
"editor.defaultFormatter": "esbenp.prettier-vscode"
5+
},
6+
"eslint.format.enable": true,
7+
"editor.formatOnSave": true,
8+
"eslint.codeAction.showDocumentation": {
9+
"enable": true
10+
},
11+
"eslint.validate": [
12+
"javascript"
13+
],
14+
"editor.renderWhitespace": "all",
15+
"prettier.printWidth": 150,
16+
"editor.rulers": [
17+
150
18+
],
19+
"[json]": {
20+
"editor.defaultFormatter": "esbenp.prettier-vscode"
21+
},
22+
"[markdown]": {
23+
"editor.defaultFormatter": "esbenp.prettier-vscode"
24+
}
25+
}

src/events/SignalManager.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
import { logDebug } from "../utils.js";
44

55
export class SignalManager {
6+
#connections;
7+
68
constructor() {
7-
this._connections = [];
9+
this.#connections = [];
810
}
911

1012
connect(from, to, eventName, handlerName) {
11-
this._connections.push({
13+
this.#connections.push({
1214
from: from,
1315
to: to,
1416
id: to.connect(eventName, from[handlerName].bind(from)),
@@ -18,7 +20,7 @@ export class SignalManager {
1820
disable() {
1921
logDebug("Removing connections managed by SignalManager...");
2022

21-
this._connections.forEach((connection) => connection.to.disconnect(connection.id));
22-
this._connections.length = 0;
23+
this.#connections.forEach((connection) => connection.to.disconnect(connection.id));
24+
this.#connections.length = 0;
2325
}
2426
}

src/model/ColorTone.js

+20-14
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,46 @@ import Clutter from "gi://Clutter";
55
import { ColorTones as ColorTonePresets } from "./Presets.js";
66

77
export class ColorTone {
8+
#toneFactor;
9+
#preset;
10+
11+
#brightnessColor;
12+
#contrastColor;
13+
814
constructor(presetId, toneFactor) {
9-
this._toneFactor = toneFactor;
15+
this.#toneFactor = toneFactor;
1016

11-
this._preset = this._findPreset(presetId);
17+
this.#preset = this.#findPreset(presetId);
1218

13-
this._brightnessColor = new Clutter.Color(this._preset.brightness);
14-
this._contrastColor = new Clutter.Color(this._preset.contrast);
19+
this.#brightnessColor = new Clutter.Color(this.#preset.brightness);
20+
this.#contrastColor = new Clutter.Color(this.#preset.contrast);
1521
}
1622

1723
get brightnessColor() {
18-
return this._applyToneFactor(this._brightnessColor.copy());
24+
return this.#applyToneFactor(this.#brightnessColor.copy());
1925
}
2026

2127
get contrastColor() {
22-
return this._applyToneFactor(this._contrastColor.copy());
28+
return this.#applyToneFactor(this.#contrastColor.copy());
2329
}
2430

2531
set toneFactor(value) {
26-
this._toneFactor = value;
32+
this.#toneFactor = value;
2733
}
2834

29-
_applyToneFactor(color) {
30-
color.red = this._computeByFactor(color.red);
31-
color.green = this._computeByFactor(color.green);
32-
color.blue = this._computeByFactor(color.blue);
35+
#applyToneFactor(color) {
36+
color.red = this.#computeByFactor(color.red);
37+
color.green = this.#computeByFactor(color.green);
38+
color.blue = this.#computeByFactor(color.blue);
3339

3440
return color;
3541
}
3642

37-
_computeByFactor(colorValue) {
38-
return Math.round(127 + ((colorValue - 127) * this._toneFactor) / 100);
43+
#computeByFactor(colorValue) {
44+
return Math.round(127 + ((colorValue - 127) * this.#toneFactor) / 100);
3945
}
4046

41-
_findPreset(presetId) {
47+
#findPreset(presetId) {
4248
const preset = ColorTonePresets.find(({ id }) => id === presetId);
4349

4450
if (!preset) throw new Error(`Color Tone Preset id '${presetId}' is not configured`);

src/modules/Colorizer.js

+71-67
Original file line numberDiff line numberDiff line change
@@ -13,135 +13,139 @@ const COLORIZE_EFFECT_NAME = "bedtime-mode-colorize-effect";
1313
const DESATURATE_EFFECT_NAME = "bedtime-mode-desaturate-effect";
1414

1515
export class Colorizer extends ModuleBase {
16+
#transitions;
17+
#transitionDelay;
18+
19+
#transitionStep;
20+
#transitionLoopSource;
21+
22+
#colorTone;
23+
#colorizeEffect;
24+
#desaturateEffect;
25+
1626
constructor(extension) {
1727
super(extension);
1828

19-
this._transitions = this.extension.settings.colorToneFactor;
20-
this._transitionDelayMillis = 25;
29+
this.#transitions = this.extension.settings.colorToneFactor;
30+
this.#transitionDelay = 25;
2131

22-
this._transitionStep = 0;
23-
this._transitionLoopSource = null;
32+
this.#transitionStep = 0;
2433

25-
this._colorTone = new ColorTone(this.extension.settings.colorTonePreset, 0);
26-
this._colorizeEffect = new Clutter.BrightnessContrastEffect();
34+
this.#colorTone = new ColorTone(this.extension.settings.colorTonePreset, 0);
35+
this.#colorizeEffect = new Clutter.BrightnessContrastEffect();
2736

28-
this._desaturateEffect = new Clutter.DesaturateEffect();
29-
this._desaturateEffect.factor = 0;
37+
this.#desaturateEffect = new Clutter.DesaturateEffect();
38+
this.#desaturateEffect.factor = 0;
3039
}
3140

3241
enable() {
33-
this._createConnections();
42+
this.#createConnections();
3443

35-
this.extension.settings.bedtimeModeActive && this._turnOn();
44+
this.extension.settings.bedtimeModeActive && this.#turnOn();
3645
}
3746

3847
disable() {
39-
this.extension.settings.bedtimeModeActive ? this._turnOff() : this._cleanUp();
48+
this.extension.settings.bedtimeModeActive ? this.#turnOff() : this.#cleanUp();
4049
}
4150

42-
_createConnections() {
51+
#createConnections() {
4352
logDebug("Creating connections for Colorizer...");
4453

45-
this.extension.signalManager.connect(
46-
this,
47-
this.extension.settings,
48-
"bedtime-mode-active-changed",
49-
this._onBedtimeModeActiveChanged.name
50-
);
51-
this.extension.signalManager.connect(this, this.extension.settings, "color-tone-preset-changed", this._onColorTonePresetChanged.name);
52-
this.extension.signalManager.connect(this, this.extension.settings, "color-tone-factor-changed", this._onColorToneFactorChanged.name);
54+
this.createConnection(this.extension.settings, "bedtime-mode-active-changed", this.onBedtimeModeActiveChanged.name);
55+
this.createConnection(this.extension.settings, "color-tone-preset-changed", this.onColorTonePresetChanged.name);
56+
this.createConnection(this.extension.settings, "color-tone-factor-changed", this.onColorToneFactorChanged.name);
5357
}
5458

55-
_onBedtimeModeActiveChanged(_settings, _bedtimeModeActive) {
56-
_bedtimeModeActive ? this._turnOn() : this._turnOff();
59+
onBedtimeModeActiveChanged(_settings, _bedtimeModeActive) {
60+
_bedtimeModeActive ? this.#turnOn() : this.#turnOff();
5761
}
5862

59-
_onColorTonePresetChanged() {
60-
this._colorTone = new ColorTone(this.extension.settings.colorTonePreset, this.extension.settings.colorToneFactor);
61-
this._updateEffectsFactor();
63+
onColorTonePresetChanged() {
64+
this.#colorTone = new ColorTone(this.extension.settings.colorTonePreset, this.extension.settings.colorToneFactor);
65+
this.#updateEffectsFactor();
6266
}
6367

64-
_onColorToneFactorChanged() {
65-
this._transitions = this.extension.settings.colorToneFactor;
68+
onColorToneFactorChanged() {
69+
this.#transitions = this.extension.settings.colorToneFactor;
6670

67-
if (this._transitionInProgress()) return;
71+
if (this.#transitionInProgress()) return;
6872

69-
if (this.extension.settings.bedtimeModeActive) this._transitionStep = this._transitions;
73+
if (this.extension.settings.bedtimeModeActive) this.#transitionStep = this.#transitions;
7074

71-
this._updateEffectsFactor();
75+
this.#updateEffectsFactor();
7276
}
7377

74-
_turnOn() {
78+
#turnOn() {
7579
logDebug("Turning on Colorizer...");
7680

77-
this._destroyTransitionLoopSource();
78-
this._addColorEffects();
81+
this.#destroyTransitionLoopSource();
82+
this.#addColorEffects();
7983

80-
this._transitionLoopSource = loopRun(this._smoothOn.bind(this), this._transitionDelayMillis);
84+
this.#transitionLoopSource = loopRun(this.#smoothOn.bind(this), this.#transitionDelay);
8185
}
8286

83-
_turnOff() {
87+
#turnOff() {
8488
logDebug("Turning off Colorizer...");
8589

86-
this._destroyTransitionLoopSource();
90+
this.#destroyTransitionLoopSource();
8791

88-
this._transitionLoopSource = loopRun(this._smoothOff.bind(this), this._transitionDelayMillis);
92+
this.#transitionLoopSource = loopRun(this.#smoothOff.bind(this), this.#transitionDelay);
8993
}
9094

91-
_smoothOn() {
92-
this._transitionStep < this._transitions && this._transitionStep++;
93-
this._updateEffectsFactor();
95+
#smoothOn() {
96+
this.#transitionStep < this.#transitions && this.#transitionStep++;
97+
this.#updateEffectsFactor();
9498

95-
return this._transitionStep < this._transitions || this._destroyTransitionLoopSource();
99+
return this.#transitionStep < this.#transitions || this.#destroyTransitionLoopSource();
96100
}
97101

98-
_smoothOff() {
99-
this._transitionStep > 0 && this._transitionStep--;
100-
this._updateEffectsFactor();
102+
#smoothOff() {
103+
this.#transitionStep > 0 && this.#transitionStep--;
104+
this.#updateEffectsFactor();
101105

102-
return this._transitionStep > 0 || this._cleanUp();
106+
return this.#transitionStep > 0 || this.#cleanUp();
103107
}
104108

105-
_updateEffectsFactor() {
106-
this._colorTone.toneFactor = this._transitionStep;
107-
this._desaturateEffect.factor = this._transitionStep / 100;
109+
#updateEffectsFactor() {
110+
this.#colorTone.toneFactor = this.#transitionStep;
111+
this.#desaturateEffect.factor = this.#transitionStep / 100;
108112

109-
this._updateColorizeEffect();
113+
this.#updateColorizeEffect();
110114
}
111115

112-
_addColorEffects() {
113-
UiGroup.get_effect(COLORIZE_EFFECT_NAME) || UiGroup.add_effect_with_name(COLORIZE_EFFECT_NAME, this._colorizeEffect);
114-
UiGroup.get_effect(DESATURATE_EFFECT_NAME) || UiGroup.add_effect_with_name(DESATURATE_EFFECT_NAME, this._desaturateEffect);
116+
#addColorEffects() {
117+
UiGroup.get_effect(COLORIZE_EFFECT_NAME) || UiGroup.add_effect_with_name(COLORIZE_EFFECT_NAME, this.#colorizeEffect);
118+
UiGroup.get_effect(DESATURATE_EFFECT_NAME) || UiGroup.add_effect_with_name(DESATURATE_EFFECT_NAME, this.#desaturateEffect);
115119

116-
this._updateColorizeEffect();
120+
this.#updateColorizeEffect();
117121
}
118122

119-
_updateColorizeEffect() {
120-
this._colorizeEffect.brightness = this._colorTone.brightnessColor;
121-
this._colorizeEffect.contrast = this._colorTone.contrastColor;
123+
#updateColorizeEffect() {
124+
this.#colorizeEffect.brightness = this.#colorTone.brightnessColor;
125+
this.#colorizeEffect.contrast = this.#colorTone.contrastColor;
122126
}
123127

124-
_removeColorEffects() {
128+
#removeColorEffects() {
125129
UiGroup.get_effect(COLORIZE_EFFECT_NAME) && UiGroup.remove_effect_by_name(COLORIZE_EFFECT_NAME);
126130
UiGroup.get_effect(DESATURATE_EFFECT_NAME) && UiGroup.remove_effect_by_name(DESATURATE_EFFECT_NAME);
127131
}
128132

129-
_transitionInProgress() {
130-
return this._transitionLoopSource != null;
133+
#transitionInProgress() {
134+
return this.#transitionLoopSource != null;
131135
}
132136

133-
_destroyTransitionLoopSource() {
134-
if (this._transitionLoopSource) {
135-
logDebug(`Destroying Transition Loop Source ${this._transitionLoopSource.get_id()}`);
137+
#destroyTransitionLoopSource() {
138+
if (this.#transitionLoopSource) {
139+
logDebug(`Destroying Transition Loop Source ${this.#transitionLoopSource.get_id()}`);
136140

137-
this._transitionLoopSource.destroy();
138-
this._transitionLoopSource = null;
141+
this.#transitionLoopSource.destroy();
142+
this.#transitionLoopSource = null;
139143
}
140144
}
141145

142-
_cleanUp() {
146+
#cleanUp() {
143147
logDebug("Cleaning up Colorizer related changes...");
144-
this._removeColorEffects();
145-
this._destroyTransitionLoopSource();
148+
this.#removeColorEffects();
149+
this.#destroyTransitionLoopSource();
146150
}
147151
}

0 commit comments

Comments
 (0)