optimization

This commit is contained in:
sankalp committed 2024-07-31 06:43:26 +05:45
1 parent 7289ca07fe
commit 5be5a41789
14 files changed
+353 -277

No files matched your search

+82 -63
View File
@@ -1,8 +1,9 @@
const notifications = await Service.import("notifications");
const network = await Service.import("network");
const bluetooth = await Service.import("bluetooth");
import { OpenSettings } from "apps/settings/main.ts";
import { WINDOW_NAME } from "./main.ts";
import { bluetooth_enabled, idle_inhibitor, night_light, theme } from "variables.ts";
import { idle_inhibitor, night_light, theme } from "variables.ts";
import { MaterialIcon } from "icons.ts";
import Gtk from "gi://Gtk?version=3.0";
@@ -10,32 +11,32 @@ import Gtk from "gi://Gtk?version=3.0";
const currentPage = Variable(0);
function WifiIndicator() {
const ssid = Widget.Label({
label: "Unknown",
visible: false,
class_name: "ssid",
xalign: 0,
vpack: "center",
truncate: "end"
});
const name = Widget.Box({
vertical: true,
vpack: "center",
children: [
Widget.Box([
Widget.Label({
label: "Internet",
vpack: "center"
})
]),
ssid
]
});
return Widget.Box({
css: "padding-left: 15px; padding-right: 15px; padding-top: 5px; padding-bottom: 5px;",
children: [
MaterialIcon("signal_wifi_4_bar", "20px"),
Widget.Box({
visible: network.wifi.bind("enabled"),
orientation: Gtk.Orientation.VERTICAL,
children: [
Widget.Box([
Widget.Label({
label: "Internet"
})
]),
Widget.Label({
label: network.wifi.bind("ssid").as((ssid) => ssid || "Unknown"),
class_name: "ssid",
xalign: 0,
vpack: "center",
truncate: "end"
})
]
}),
Widget.Label({
visible: network.wifi.bind("enabled").as((enabled) => !enabled),
label: "Internet"
}),
name,
Widget.Box({
hpack: "end",
hexpand: true,
@@ -43,7 +44,13 @@ function WifiIndicator() {
class_name: "material_icon icon arrow"
})
})
]
],
setup: (self) => {
self.hook(network, () => {
ssid.label = network.wifi.ssid || "Unknown";
ssid.visible = !!network.wifi.ssid;
});
}
});
}
@@ -106,9 +113,7 @@ function Page1() {
children: [
Widget.Button({
hexpand: true,
class_name: network.wifi
.bind("enabled")
.as((enabled) => (enabled ? "management_button active" : "management_button")),
class_name: "management_button",
child: NetworkIndicator(),
on_primary_click_release: () => {
network.toggleWifi();
@@ -116,26 +121,33 @@ function Page1() {
on_secondary_click_release: () => {
App.closeWindow(WINDOW_NAME);
OpenSettings("network");
},
setup: (self) => {
if (network.wifi)
self.hook(network.wifi, () => {
self.toggleClassName("active", network.wifi.enabled);
});
}
}),
Widget.Button({
hexpand: true,
class_name: bluetooth_enabled
.bind()
.as((state) => (state.trim() == "yes" ? "management_button active" : "management_button")),
class_name: "management_button",
child: IconAndName({
label: "Bluetooth",
icon: "bluetooth",
arrow: true
}),
on_primary_click_release: () => {
Utils.execAsync(`${App.configDir}/scripts/bluetooth.sh --toggle`).then((out) => {
bluetooth_enabled.setValue(out);
});
bluetooth.toggle();
},
on_secondary_click_release: () => {
OpenSettings("bluetooth");
App.closeWindow(WINDOW_NAME);
},
setup: (self) => {
self.hook(bluetooth, () => {
self.toggleClassName("active", bluetooth.enabled);
});
}
})
]
@@ -147,9 +159,7 @@ function Page1() {
children: [
Widget.Button({
hexpand: true,
class_name: theme.bind().as((str) => {
return str.trim() == "dark" ? "management_button active" : "management_button";
}),
class_name: "management_button",
on_clicked: () => {
Utils.execAsync(`${App.configDir}/scripts/dark-theme.sh --toggle`).catch(print);
theme.setValue(theme.value.trim() == "dark" ? "light" : "dark");
@@ -157,22 +167,31 @@ function Page1() {
child: IconAndName({
label: "Dark theme",
icon: "contrast"
})
}),
setup: (self) => {
self.hook(theme, () => {
self.toggleClassName("active", theme.value.trim() == "dark");
});
}
}),
Widget.Button({
hexpand: true,
class_name: notifications.bind("dnd").as((bool) => {
return bool ? "management_button active" : "management_button";
}),
class_name: "management_button",
on_clicked: () => {
notifications.dnd = notifications.dnd ? false : true;
notifications.dnd = !notifications.dnd;
},
child: IconAndName({
label: "Do Not Disturb",
icon: notifications
.bind("dnd")
.as((bool) => (bool ? "do_not_disturb_on" : "do_not_disturb_off"))
})
icon: "do_not_disturb_off"
}),
setup: (self) => {
notifications.connect("notify::dnd", (_) => {
self.toggleClassName("active", notifications.dnd);
self.child.children[0].label = notifications.dnd
? "do_not_disturb_on"
: "do_not_disturb_off";
});
}
})
]
}),
@@ -183,9 +202,7 @@ function Page1() {
children: [
Widget.Button({
hexpand: true,
class_name: idle_inhibitor
.bind()
.as((bool) => (bool ? "management_button active" : "management_button")),
class_name: "management_button",
child: IconAndName({
label: "Idle inhibitor",
icon: "schedule"
@@ -200,15 +217,16 @@ function Page1() {
]).catch(print);
else Utils.execAsync("pkill -f wayland-idle-inhibitor.py").catch(print);
},
setup: () => {
setup: (self) => {
idle_inhibitor.setValue(!!Utils.exec("pidof wayland-idle-inhibitor.py"));
self.hook(idle_inhibitor, () => {
self.toggleClassName("active", idle_inhibitor.value);
});
}
}),
Widget.Button({
hexpand: true,
class_name: night_light
.bind()
.as((mode) => (mode ? "management_button active" : "management_button")),
class_name: "management_button",
child: IconAndName({
label: "Night Light",
icon: "nightlight"
@@ -217,20 +235,16 @@ function Page1() {
night_light.setValue(!night_light.value);
Utils.execAsync(`${App.configDir}/scripts/night-light.sh --toggle`)
.then((out) => {
if (out.trim() == "enabled") night_light.setValue(true);
else if (out.trim() == "disabled") night_light.setValue(false);
night_light.setValue(out.trim() == "enabled");
})
.catch((err) => {
night_light.setValue(!night_light.value);
});
},
setup: () => {
Utils.execAsync(`${App.configDir}/scripts/night-light.sh --get`)
.then((out) => {
if (out.trim() == "enabled") night_light.setValue(true);
else if (out.trim() == "disabled") night_light.setValue(false);
})
.catch(print);
setup: (self) => {
self.hook(night_light, () => {
self.toggleClassName("active", night_light.value);
});
}
})
]
@@ -288,8 +302,13 @@ const createDotButton = (index: number) =>
Widget.Button({
label: "●",
onClicked: () => currentPage.setValue(index),
class_name: currentPage.bind().as((v) => (v == index ? "dotbutton active" : "dotbutton")),
hexpand: false
class_name: "dotbutton",
hexpand: false,
setup: (self) => {
self.hook(currentPage, () => {
self.toggleClassName("active", currentPage.value == index);
});
}
});
export function Management() {
+10 -3
View File
@@ -3,7 +3,6 @@ import { SystemBox } from "./system.ts";
let shown = Variable("Messages");
import Gtk from "gi://Gtk?version=3.0";
import { MaterialIcon } from "icons.ts";
import { enableClickThrough } from "modules/misc/clickthrough.js";
type ButtonType = {
page: string;
@@ -13,8 +12,9 @@ type ButtonType = {
function Button({ page, label, icon }: ButtonType) {
return Widget.Button({
class_name: shown.bind().as((_page) => (_page == page ? "navigation_button active" : "navigation_button")),
class_name: "navigation_button",
hexpand: true,
attribute: { page: page },
child: Widget.Box({
orientation: Gtk.Orientation.VERTICAL,
class_name: "container_outer",
@@ -72,7 +72,14 @@ export function Navigation() {
label: "System",
icon: "info"
})
]
],
setup: (self) => {
self.hook(shown, () => {
for (const button of self.children) {
button.toggleClassName("active", button.attribute.page == shown.value);
}
});
}
});
return Widget.Box({
vexpand: true,
+37 -27
View File
@@ -1,15 +1,8 @@
import {
cpu_cores,
cpu_name,
kernel_name,
amount_of_ram,
gpu_name,
cur_uptime,
current_brightness
} from "variables.ts";
import { cpu_cores, cpu_name, kernel_name, amount_of_ram, gpu_name, cur_uptime } from "variables.ts";
import Gtk from "gi://Gtk?version=3.0";
import { Variable as VariableType } from "types/variable";
import backlight_service from "services/backlight.ts";
import { sideright } from "./main";
type InfoType = {
cpu: string;
@@ -51,23 +44,40 @@ async function SystemInfo(): Promise<InfoType> {
};
}
const usage = Variable(
{
cpu: "0",
ram: "0",
swap: "0",
cpu_temp: "0"
} as unknown as Promise<InfoType>,
{
poll: [
1000,
async () => {
const result = await SystemInfo().then((result) => result);
return result;
}
]
}
);
function checkBrightness() {
const get = Utils.execAsync(`${App.configDir}/scripts/brightness.sh --get`)
.then((out) => Number(out.trim()))
.catch(print);
return get;
}
export const current_brightness = Variable(100, {
poll: [
500,
() => {
if (sideright?.visible) return checkBrightness();
else return current_brightness?.value || 100;
}
]
});
const usage_default = {
cpu: "0",
ram: "0",
swap: "0",
cpu_temp: "0"
};
const usage = Variable(usage_default);
Utils.interval(1000, async () => {
Utils.idle(() => {
if (sideright?.visible) {
SystemInfo().then((result) => {
usage.setValue(result);
});
}
});
});
const Usage = (name: string, var_name: keyof InfoType, class_name: string | undefined) => {
const usage_progress_bar = Widget.ProgressBar({
@@ -132,7 +142,7 @@ export function SystemBox() {
draw_value: false,
class_name: "system_scale backlight",
// @ts-ignore
value: backlight_service.bind("screen_value").as(n => n * 100),
value: backlight_service.bind("screen_value").as((n) => n * 100),
on_change: (self) => {
backlight_service.screen_value = self.value / 100;
},