ags(control-center/tiles): migrate from old Pages widget

This commit is contained in:
retrozinndev
2025-04-21 18:10:14 -03:00
parent 231f25addb
commit e04bb9150a
5 changed files with 86 additions and 57 deletions
+2 -2
View File
@@ -1,8 +1,8 @@
import { bind, Variable } from "astal";
import { Tile, TileProps } from "./Tile";
import AstalBluetooth from "gi://AstalBluetooth";
import { togglePage } from "../Pages";
import { BluetoothPage } from "../pages/Bluetooth";
import { TilesPages } from "../Tiles";
export const TileBluetooth = Tile({
title: "Bluetooth",
@@ -12,7 +12,7 @@ export const TileBluetooth = Tile({
}),
onToggledOn: () => AstalBluetooth.get_default().adapter.set_powered(true),
onToggledOff: () => AstalBluetooth.get_default().adapter.set_powered(false),
onClickMore: () => togglePage(BluetoothPage),
onClickMore: () => TilesPages?.toggle(BluetoothPage()),
enableOnClickMore: true,
icon: Variable.derive([
bind(AstalBluetooth.get_default().adapter, "powered"),
+4 -4
View File
@@ -2,9 +2,9 @@ import { bind, execAsync, Variable } from "astal";
import { Tile, TileProps } from "./Tile";
import AstalNetwork from "gi://AstalNetwork";
import { Widget } from "astal/gtk3";
import { togglePage } from "../Pages";
import { PageNetwork } from "../pages/Network";
import { tr } from "../../../i18n/intl";
import { TilesPages } from "../Tiles";
export const TileNetwork = () => new Widget.Box({
child: Variable.derive([
@@ -32,7 +32,7 @@ export const TileNetwork = () => new Widget.Box({
)(),
onToggledOn: () => wifi.set_enabled(true),
onToggledOff: () => wifi.set_enabled(false),
onClickMore: () => togglePage(PageNetwork),
onClickMore: () => TilesPages?.toggle(PageNetwork()),
icon: "󰤨",
iconSize: 16,
toggleState: bind(wifi, "enabled")
@@ -53,7 +53,7 @@ export const TileNetwork = () => new Widget.Box({
}),
onToggledOn: () => execAsync("nmcli n on"),
onToggledOff: () => execAsync("nmcli n off"),
onClickMore: () => togglePage(PageNetwork),
onClickMore: () => TilesPages?.toggle(PageNetwork()),
icon: bind(wired, "internet").as((internet: AstalNetwork.Internet) => {
switch(internet) {
case AstalNetwork.Internet.CONNECTED:
@@ -77,7 +77,7 @@ export const TileNetwork = () => new Widget.Box({
description: tr("control_center.tiles.network.disconnected") || "Disconnected",
onToggledOn: () => execAsync("nmcli n on"),
onToggledOff: () => execAsync("nmcli n off"),
onClickMore: () => togglePage(PageNetwork),
onClickMore: () => TilesPages?.toggle(PageNetwork()),
icon: "󰲛",
iconSize: 16,
toggleState: bind(wired, "internet").as((internet: AstalNetwork.Internet) =>
@@ -1,9 +1,9 @@
import { bind, Variable } from "astal";
import { Tile, TileProps } from "./Tile";
import { NightLight } from "../../../scripts/nightlight";
import { togglePage } from "../Pages";
import { PageNightLight } from "../pages/NightLight";
import { tr } from "../../../i18n/intl";
import { TilesPages } from "../Tiles";
export const TileNightLight = Tile({
title: tr("control_center.tiles.night_light.title"),
@@ -20,6 +20,6 @@ export const TileNightLight = Tile({
onToggledOff: () => NightLight.getDefault().identity = true,
onToggledOn: () => NightLight.getDefault().identity = false,
enableOnClickMore: true,
onClickMore: () => togglePage(PageNightLight),
onClickMore: () => TilesPages?.toggle(PageNightLight()),
toggleState: bind(NightLight.getDefault(), "identity").as(identity => !identity)
} as TileProps);
+54 -46
View File
@@ -16,7 +16,7 @@ export type TileProps = {
onClickMore?: () => void;
}
export function Tile(props: TileProps): (() => Widget.EventBox) {
export function Tile(props: TileProps): (() => Gtk.Widget) {
const toggled = new Variable<boolean>(props.toggleState instanceof Binding ?
(props.toggleState.get() || false) : (props.toggleState || false));
@@ -25,34 +25,42 @@ export function Tile(props: TileProps): (() => Widget.EventBox) {
if(props?.toggleState instanceof Binding)
subscription = props.toggleState.subscribe(val => toggled.set(val || false));
return () => new Widget.EventBox({
className: toggled().as((state: boolean) =>
state ? "tile-eventbox toggled" : "tile-eventbox"),
return () => new Widget.Box({
className: (props.className instanceof Binding) ?
Variable.derive([
props.className,
toggled()
], (className, isToggled) =>
`tile ${className} ${isToggled ? "toggled" : ""} ${
props.onClickMore ? "has-more" : ""
}`
)()
: toggled().as((state: boolean) =>
`tile ${state ? "toggled" : ""} ${
props.onClickMore ? "has-more" : ""
}`
),
expand: true,
onClick: () => {
if(toggled.get()) {
toggled.set(false);
console.log(toggled.get());
props.onToggledOff && props.onToggledOff();
return;
}
toggled.set(true);
props.onToggledOn && props.onToggledOn();
},
visible: props.visible,
onDestroy: () => subscription?.(),
child: new Widget.Box({
className: (props.className instanceof Binding) ?
props.className.as((clsName: (string|undefined)) =>
`tile ${clsName || ""}`)
: `tile ${props.className || ""}`,
visible: props.visible,
expand: true,
hexpand: true,
children: [
new Widget.Box({
children: [
new Widget.Button({
className: "toggle-button",
onClick: () => {
if(toggled.get()) {
toggled.set(false);
console.log(toggled.get());
props.onToggledOff && props.onToggledOff();
return;
}
toggled.set(true);
props.onToggledOn && props.onToggledOn();
},
child: new Widget.Box({
className: "content",
expand: true,
hexpand: true,
children: [
new Widget.Label({
className: "icon nf",
@@ -88,26 +96,26 @@ export function Tile(props: TileProps): (() => Widget.EventBox) {
]
} as Widget.BoxProps)
]
} as Widget.BoxProps),
new Widget.Button({
className: "more icon",
visible: props.onClickMore !== undefined,
halign: Gtk.Align.END,
tooltipText: tr("control_center.tiles.more") || "More",
image: new Widget.Icon({
icon: "go-next-symbolic",
css: "icon { font-size: 16px; }"
}),
onClick: () => {
((props.enableOnClickMore instanceof Binding) ?
props.enableOnClickMore.get()
: props.enableOnClickMore) && props?.onToggledOn();
} as Widget.BoxProps)
} as Widget.ButtonProps),
new Widget.Button({
className: "more icon",
visible: props.onClickMore !== undefined,
halign: Gtk.Align.END,
tooltipText: tr("control_center.tiles.more") || "More",
image: new Widget.Icon({
icon: "go-next-symbolic",
css: "icon { font-size: 16px; }"
}),
onClick: () => {
((props.enableOnClickMore instanceof Binding) ?
props.enableOnClickMore.get()
: props.enableOnClickMore) && props?.onToggledOn();
props.onClickMore && props?.onClickMore()
},
widthRequest: 32
})
]
})
} as Widget.EventBoxProps)
props.onClickMore && props?.onClickMore()
},
widthRequest: 32
})
]
});
}