ags: auto dark mode config option (#447)

This commit is contained in:
end-4
2024-05-30 20:51:31 +07:00
parent df6c8f1f7a
commit 2345ff8fee
4 changed files with 84 additions and 33 deletions
+6 -1
View File
@@ -6,7 +6,8 @@ import App from 'resource:///com/github/Aylur/ags/app.js'
import * as Utils from 'resource:///com/github/Aylur/ags/utils.js'
// Stuff
import userOptions from './modules/.configuration/user_options.js';
import { firstRunWelcome } from './services/messages.js';
import { firstRunWelcome, startBatteryWarningService } from './services/messages.js';
import { startAutoDarkModeService } from './services/darkmode.js';
// Widgets
import { Bar, BarCornerTopleft, BarCornerTopright } from './modules/bar/main.js';
import Cheatsheet from './modules/cheatsheet/main.js';
@@ -32,7 +33,11 @@ function forMonitorsAsync(widget) {
return range(n, 0).forEach((n) => widget(n).catch(print))
}
// Start stuff
handleStyles(true);
startAutoDarkModeService().catch(print);
firstRunWelcome().catch(print);
startBatteryWarningService().catch(print)
const Windows = () => [
// forMonitors(DesktopBackground),
@@ -20,6 +20,11 @@ let configOptions = {
'durationLarge': 180,
},
'appearance': {
'autoDarkMode': { // Turns on dark mode in certain hours. Time in 24h format
'enabled': false,
'from': "18:10",
'to': "6:10",
},
'keyboardUseFlag': false, // Use flag emoji instead of abbreviation letters
'layerSmoke': false,
'layerSmokeStrength': 0.2,
+41
View File
@@ -0,0 +1,41 @@
const { Gio, GLib } = imports.gi;
import Service from 'resource:///com/github/Aylur/ags/service.js';
import * as Utils from 'resource:///com/github/Aylur/ags/utils.js';
import { darkMode } from '../modules/.miscutils/system.js';
const { exec, execAsync } = Utils;
const timeBefore = (time1, time2) => { // Arrays of [hour, minute]
if (time1[0] == time2[0]) return time1[1] < time2[1];
return time1[0] < time2[0];
}
const timeSame = (time1, time2) => // Arrays of [hour, minute]
(time1[0] == time2[0] && time1[1] == time2[1]);
const timeBeforeOrSame = (time1, time2) => // Arrays of [hour, minute]
(timeBefore(time1, time2) || timeSame(time1, time2));
const timeInRange = (time, rangeStart, rangeEnd) => { // Arrays of [hour, minute]
if (timeBefore(rangeStart, rangeEnd))
return (timeBeforeOrSame(rangeStart, time) && timeBeforeOrSame(time, rangeEnd))
else { // rangeEnd < rangeStart, meaning it ends the following day
rangeEnd[0] += 24;
if (timeBefore(time, rangeStart)) time[0] += 24;
return (timeBeforeOrSame(rangeStart, time) && timeBeforeOrSame(time, rangeEnd))
}
}
export async function startAutoDarkModeService() {
Utils.interval(userOptions.time.interval, () => {
if ((!userOptions.appearance.autoDarkMode.enabled)) return;
const fromTime = (userOptions.appearance.autoDarkMode.from).split(':').map(Number);
const toTime = (userOptions.appearance.autoDarkMode.to).split(':').map(Number);
if (fromTime == toTime) return;
const currentDateTime = GLib.DateTime.new_now_local();
const currentTime = [currentDateTime.get_hour(), currentDateTime.get_minute()];
// console.log(currentTime, fromTime, toTime);
darkMode.value = timeInRange(currentTime, fromTime, toTime);
})
}
+32 -32
View File
@@ -14,6 +14,37 @@ const APP_NAME = "illogical-impulse";
const FIRST_RUN_NOTIF_TITLE = "Welcome!";
const FIRST_RUN_NOTIF_BODY = `First run? 👀 <span foreground="#FF0202" font_weight="bold">CTRL+SUPER+T</span> to pick a wallpaper (or styles will break!)\nFor a list of keybinds, hit <span foreground="#c06af1" font_weight="bold">Super + /</span>.`;
var batteryWarned = false;
async function batteryMessage() {
const perc = Battery.percent;
const charging = Battery.charging;
if (charging) {
batteryWarned = false;
return;
}
for (let i = userOptions.battery.warnLevels.length - 1; i >= 0; i--) {
if (perc <= userOptions.battery.warnLevels[i] && !charging && !batteryWarned) {
batteryWarned = true;
Utils.execAsync(['bash', '-c',
`notify-send "${userOptions.battery.warnTitles[i]}" "${userOptions.battery.warnMessages[i]}" -u critical -a '${APP_NAME}' -t 69420 &`
]).catch(print);
break;
}
}
if (perc <= userOptions.battery.suspendThreshold) {
Utils.execAsync(['bash', '-c',
`notify-send "Suspending system" "Critical battery level (${perc}% remaining)" -u critical -a '${APP_NAME}' -t 69420 &`
]).catch(print);
Utils.execAsync('systemctl suspend').catch(print);
}
}
export async function startBatteryWarningService() {
Utils.timeout(1, () => {
Battery.connect('changed', () => batteryMessage().catch(print));
})
}
export async function firstRunWelcome() {
GLib.mkdir_with_parents(`${GLib.get_user_state_dir()}/ags/user`, 755);
if (!fileExists(FIRST_RUN_PATH)) {
@@ -27,35 +58,4 @@ export async function firstRunWelcome() {
})
.catch(print);
}
}
var batteryWarned = false;
async function batteryMessage() {
const perc = Battery.percent;
const charging = Battery.charging;
if(charging) {
batteryWarned = false;
return;
}
for (let i = userOptions.battery.warnLevels.length - 1; i >= 0; i--) {
if (perc <= userOptions.battery.warnLevels[i] && !charging && !batteryWarned) {
batteryWarned = true;
Utils.execAsync(['bash', '-c',
`notify-send "${userOptions.battery.warnTitles[i]}" "${userOptions.battery.warnMessages[i]}" -u critical -a '${APP_NAME}' -t 69420 &`
]).catch(print);
break;
}
}
if(perc <= userOptions.battery.suspendThreshold) {
Utils.execAsync(['bash', '-c',
`notify-send "Suspending system" "Critical battery level (${perc}% remaining)" -u critical -a '${APP_NAME}' -t 69420 &`
]).catch(print);
Utils.execAsync('systemctl suspend').catch(print);
}
}
// Run them
firstRunWelcome().catch(print);
Utils.timeout(1, () => {
Battery.connect('changed', () => batteryMessage().catch(print));
})
}