Skip to content

Commit 5e050e7

Browse files
feat(desktop/extension)!: custom notify function
1 parent 42695f6 commit 5e050e7

File tree

1 file changed

+41
-18
lines changed

1 file changed

+41
-18
lines changed

modules/home/desktop/extension/extension.js

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,33 @@ import {
55
panel,
66
overview,
77
layoutManager,
8-
notify,
98
} from "resource:///org/gnome/shell/ui/main.js";
109
import {
1110
Extension,
1211
InjectionManager,
1312
} from "resource:///org/gnome/shell/extensions/extension.js";
1413
import {
15-
// Urgency,
14+
Urgency,
1615
// Source,
1716
MessageTray,
17+
Notification,
18+
getSystemSource,
1819
} from "resource:///org/gnome/shell/ui/messageTray.js";
1920

21+
// refer: notify function
22+
// source: https://gitlab.gnome.org/GNOME/gnome-shell/-/blob/main/js/ui/main.js#L634-647
23+
function notify(params) {
24+
const source = getSystemSource();
25+
const notification = new Notification({
26+
source,
27+
title: params.msg,
28+
body: params.details ?? null,
29+
isTransient: true,
30+
urgency: params.urgency ?? Urgency.NORMAL,
31+
});
32+
source.addNotification(notification);
33+
}
34+
2035
class ShowOverviewOnEnable {
2136
#states = new Set(["SHOWN", "SHOWING"]);
2237

@@ -88,24 +103,32 @@ class Overrides {
88103
class Battery {
89104
#MIN = 40;
90105
#MAX = 80;
106+
#INTERVAL = 5 * 60; // 5 * 60s = 5min
91107

92108
enable() {
93-
this._sourceId = GLib.timeout_add_seconds(GLib.PRIORITY_LOW, 5 * 60, () => {
94-
const { Percentage: level, State: state } =
95-
panel.statusArea.quickSettings._system._systemItem._powerToggle._proxy;
96-
97-
if (level < this.#MIN && state === UPower.DeviceState.DISCHARGING) {
98-
notify(
99-
`Battery is less than ${this.#MIN}% (${level}%) plug the charger.`,
100-
);
101-
} else if (level > this.#MAX && state === UPower.DeviceState.CHARGING) {
102-
notify(
103-
`Battery is greater than ${this.#MAX}% (${level}%) unplug the charger`,
104-
);
105-
}
106-
107-
return GLib.SOURCE_CONTINUE;
108-
});
109+
this._sourceId = GLib.timeout_add_seconds(
110+
GLib.PRIORITY_LOW,
111+
this.#INTERVAL,
112+
() => {
113+
const { Percentage: level, State: state } =
114+
panel.statusArea.quickSettings._system._systemItem._powerToggle
115+
._proxy;
116+
117+
if (level < this.#MIN && state === UPower.DeviceState.DISCHARGING) {
118+
notify({
119+
msg: `Battery is less than ${this.#MIN}% (${level}%) plug the charger.`,
120+
urgency: Urgency.CRITICAL,
121+
});
122+
} else if (level > this.#MAX && state === UPower.DeviceState.CHARGING) {
123+
notify({
124+
msg: `Battery is greater than ${this.#MAX}% (${level}%) unplug the charger`,
125+
urgency: Urgency.CRITICAL,
126+
});
127+
}
128+
129+
return GLib.SOURCE_CONTINUE;
130+
},
131+
);
109132
}
110133

111134
disable() {

0 commit comments

Comments
 (0)