fix(ags): move get_default calls inside functions

This commit is contained in:
matt1432 2024-11-20 23:38:41 -05:00
commit 4604dcb6fd
24 changed files with 438 additions and 382 deletions

View file

@ -1,45 +1,48 @@
import { bind } from 'astal';
import AstalBattery from 'gi://AstalBattery';
const Battery = AstalBattery.get_default();
import Separator from '../../misc/separator';
const LOW_BATT = 20;
export default () => (
<box className="bar-item battery">
<icon
setup={(self) => {
const update = () => {
const percent = Math.round(Battery.get_percentage() * 100);
const level = Math.floor(percent / 10) * 10;
const isCharging = Battery.get_charging();
const charged = percent === 100 && isCharging;
const iconName = charged ?
'battery-level-100-charged-symbolic' :
export default () => {
const battery = AstalBattery.get_default();
return (
<box className="bar-item battery">
<icon
setup={(self) => {
const update = () => {
const percent = Math.round(battery.get_percentage() * 100);
const level = Math.floor(percent / 10) * 10;
const isCharging = battery.get_charging();
const charged = percent === 100 && isCharging;
const iconName = charged ?
'battery-level-100-charged-symbolic' :
`battery-level-${level}${isCharging ?
'-charging' :
''}-symbolic`;
self.set_icon(iconName);
self.set_icon(iconName);
self.toggleClassName('charging', isCharging);
self.toggleClassName('charged', charged);
self.toggleClassName('low', percent < LOW_BATT);
};
self.toggleClassName('charging', isCharging);
self.toggleClassName('charged', charged);
self.toggleClassName('low', percent < LOW_BATT);
};
update();
update();
Battery.connect('notify::percentage', () => update());
Battery.connect('notify::icon-name', () => update());
Battery.connect('notify::battery-icon-name', () => update());
}}
/>
battery.connect('notify::percentage', () => update());
battery.connect('notify::icon-name', () => update());
battery.connect('notify::battery-icon-name', () => update());
}}
/>
<Separator size={8} />
<Separator size={8} />
<label label={bind(Battery, 'percentage').as((v) => `${Math.round(v * 100)}%`)} />
</box>
);
<label label={bind(battery, 'percentage').as((v) => `${Math.round(v * 100)}%`)} />
</box>
);
};

View file

@ -3,17 +3,19 @@ import { bind } from 'astal';
import Brightness from '../../../services/brightness';
export default () => {
const brightness = Brightness.get_default();
return (
<box className="bar-item brightness">
<overlay>
<circularprogress
startAt={0.75}
endAt={0.75}
value={bind(Brightness, 'screen')}
value={bind(brightness, 'screen')}
rounded
/>
<icon icon={bind(Brightness, 'screenIcon')} />
<icon icon={bind(brightness, 'screenIcon')} />
</overlay>
</box>
);

View file

@ -1,16 +1,16 @@
import { bind, Variable } from 'astal';
import AstalApps from 'gi://AstalApps';
const Applications = AstalApps.Apps.new();
import AstalHyprland from 'gi://AstalHyprland';
const Hyprland = AstalHyprland.get_default();
import Separator from '../../misc/separator';
import { hyprMessage } from '../../../lib';
export default () => {
const applications = AstalApps.Apps.new();
const hyprland = AstalHyprland.get_default();
const visibleIcon = Variable<boolean>(false);
const focusedIcon = Variable<string>('');
const focusedTitle = Variable<string>('');
@ -18,10 +18,10 @@ export default () => {
let lastFocused: string | undefined;
const updateVars = (
client: AstalHyprland.Client | null = Hyprland.get_focused_client(),
client: AstalHyprland.Client | null = hyprland.get_focused_client(),
) => {
lastFocused = client?.get_address();
const app = Applications.fuzzy_query(
const app = applications.fuzzy_query(
client?.get_class() ?? '',
)[0];
@ -45,11 +45,11 @@ export default () => {
};
updateVars();
Hyprland.connect('notify::focused-client', () => updateVars());
Hyprland.connect('client-removed', () => updateVars());
Hyprland.connect('client-added', async() => {
hyprland.connect('notify::focused-client', () => updateVars());
hyprland.connect('client-removed', () => updateVars());
hyprland.connect('client-added', async() => {
try {
updateVars(Hyprland.get_client(JSON.parse(await hyprMessage('j/activewindow')).address));
updateVars(hyprland.get_client(JSON.parse(await hyprMessage('j/activewindow')).address));
}
catch (e) {
console.log(e);

View file

@ -1,25 +1,25 @@
import { bind, Variable } from 'astal';
import AstalApps from 'gi://AstalApps';
const Applications = AstalApps.Apps.new();
import AstalHyprland from 'gi://AstalHyprland';
const Hyprland = AstalHyprland.get_default();
import { hyprMessage } from '../../../lib';
export default () => {
const applications = AstalApps.Apps.new();
const hyprland = AstalHyprland.get_default();
const visibleIcon = Variable<boolean>(false);
const focusedIcon = Variable<string>('');
let lastFocused: string | undefined;
const updateVars = (
client: AstalHyprland.Client | null = Hyprland.get_focused_client(),
client: AstalHyprland.Client | null = hyprland.get_focused_client(),
) => {
lastFocused = client?.get_address();
const app = Applications.fuzzy_query(
const app = applications.fuzzy_query(
client?.get_class() ?? '',
)[0];
@ -41,11 +41,11 @@ export default () => {
};
updateVars();
Hyprland.connect('notify::focused-client', () => updateVars());
Hyprland.connect('client-removed', () => updateVars());
Hyprland.connect('client-added', async() => {
hyprland.connect('notify::focused-client', () => updateVars());
hyprland.connect('client-removed', () => updateVars());
hyprland.connect('client-added', async() => {
try {
updateVars(Hyprland.get_client(JSON.parse(await hyprMessage('j/activewindow')).address));
updateVars(hyprland.get_client(JSON.parse(await hyprMessage('j/activewindow')).address));
}
catch (e) {
console.log(e);

View file

@ -5,10 +5,10 @@ import AstalNetwork from 'gi://AstalNetwork';
export default () => {
const Hovered = Variable(false);
// TODO: do this everywhere else
const network = AstalNetwork.get_default();
const Hovered = Variable(false);
return (
<button
className="bar-item network"

View file

@ -2,7 +2,6 @@ import { bind } from 'astal';
import { App } from 'astal/gtk3';
import AstalNotifd from 'gi://AstalNotifd';
const Notifications = AstalNotifd.get_default();
import Separator from '../../misc/separator';
@ -12,48 +11,52 @@ const SPACING = 4;
import PopupWindow from '../../misc/popup-window';
export default () => (
<button
className="bar-item"
cursor="pointer"
export default () => {
const notifications = AstalNotifd.get_default();
onButtonReleaseEvent={(self) => {
const win = App.get_window('win-notif-center') as PopupWindow;
return (
<button
className="bar-item"
cursor="pointer"
win.set_x_pos(
self.get_allocation(),
'right',
);
onButtonReleaseEvent={(self) => {
const win = App.get_window('win-notif-center') as PopupWindow;
win.visible = !win.visible;
}}
win.set_x_pos(
self.get_allocation(),
'right',
);
setup={(self) => {
App.connect('window-toggled', (_, win) => {
if (win.name === 'win-notif-center') {
self.toggleClassName('toggle-on', win.visible);
}
});
}}
>
<box>
<icon
icon={bind(Notifications, 'notifications').as((notifs) => {
if (Notifications.dontDisturb) {
return 'notification-disabled-symbolic';
win.visible = !win.visible;
}}
setup={(self) => {
App.connect('window-toggled', (_, win) => {
if (win.name === 'win-notif-center') {
self.toggleClassName('toggle-on', win.visible);
}
else if (notifs.length > 0) {
return 'notification-new-symbolic';
}
else {
return 'notification-symbolic';
}
})}
/>
});
}}
>
<box>
<icon
icon={bind(notifications, 'notifications').as((notifs) => {
if (notifications.dontDisturb) {
return 'notification-disabled-symbolic';
}
else if (notifs.length > 0) {
return 'notification-new-symbolic';
}
else {
return 'notification-symbolic';
}
})}
/>
<Separator size={SPACING} />
<Separator size={SPACING} />
<label label={bind(Notifications, 'notifications').as((n) => String(n.length))} />
</box>
</button>
);
<label label={bind(notifications, 'notifications').as((n) => String(n.length))} />
</box>
</button>
);
};

View file

@ -2,7 +2,6 @@ import { App, Gdk, Gtk, Widget } from 'astal/gtk3';
import { bind, idle } from 'astal';
import AstalTray from 'gi://AstalTray';
const Tray = AstalTray.get_default();
const SKIP_ITEMS = ['.spotify-wrapped'];
@ -35,20 +34,22 @@ const TrayItem = (item: AstalTray.TrayItem) => {
};
export default () => {
const tray = AstalTray.get_default();
const itemMap = new Map<string, Widget.Revealer>();
return (
<box
className="bar-item system-tray"
visible={bind(Tray, 'items').as((items) => items.length !== 0)}
visible={bind(tray, 'items').as((items) => items.length !== 0)}
setup={(self) => {
self
.hook(Tray, 'item-added', (_, item: string) => {
if (itemMap.has(item) || SKIP_ITEMS.includes(Tray.get_item(item).title)) {
.hook(tray, 'item-added', (_, item: string) => {
if (itemMap.has(item) || SKIP_ITEMS.includes(tray.get_item(item).title)) {
return;
}
const widget = TrayItem(Tray.get_item(item)) as Widget.Revealer;
const widget = TrayItem(tray.get_item(item)) as Widget.Revealer;
itemMap.set(item, widget);
@ -59,7 +60,7 @@ export default () => {
});
})
.hook(Tray, 'item-removed', (_, item: string) => {
.hook(tray, 'item-removed', (_, item: string) => {
if (!itemMap.has(item)) {
return;
}

View file

@ -2,78 +2,83 @@ import { Gtk, Widget } from 'astal/gtk3';
import { timeout } from 'astal';
import AstalHyprland from 'gi://AstalHyprland';
const Hyprland = AstalHyprland.get_default();
import { hyprMessage } from '../../../lib';
const URGENT_DURATION = 1000;
const Workspace = ({ id = 0 }) => (
<revealer
name={id.toString()}
transitionType={Gtk.RevealerTransitionType.SLIDE_RIGHT}
>
<eventbox
cursor="pointer"
tooltip_text={id.toString()}
const Workspace = ({ id = 0 }) => {
const hyprland = AstalHyprland.get_default();
onClickRelease={() => {
hyprMessage(`dispatch workspace ${id}`).catch(console.log);
}}
return (
<revealer
name={id.toString()}
transitionType={Gtk.RevealerTransitionType.SLIDE_RIGHT}
>
<box
valign={Gtk.Align.CENTER}
className="button"
<eventbox
cursor="pointer"
tooltip_text={id.toString()}
setup={(self) => {
const update = (
_: Widget.Box,
client?: AstalHyprland.Client,
) => {
const workspace = Hyprland.get_workspace(id);
const occupied = workspace && workspace.get_clients().length > 0;
onClickRelease={() => {
hyprMessage(`dispatch workspace ${id}`).catch(console.log);
}}
>
<box
valign={Gtk.Align.CENTER}
className="button"
self.toggleClassName('occupied', occupied);
setup={(self) => {
const update = (
_: Widget.Box,
client?: AstalHyprland.Client,
) => {
const workspace = hyprland.get_workspace(id);
const occupied = workspace && workspace.get_clients().length > 0;
if (!client) {
return;
}
self.toggleClassName('occupied', occupied);
const isUrgent = client &&
if (!client) {
return;
}
const isUrgent = client &&
client.get_workspace().get_id() === id;
if (isUrgent) {
self.toggleClassName('urgent', true);
if (isUrgent) {
self.toggleClassName('urgent', true);
// Only show for a sec when urgent is current workspace
if (Hyprland.get_focused_workspace().get_id() === id) {
timeout(URGENT_DURATION, () => {
self.toggleClassName('urgent', false);
});
if (hyprland.get_focused_workspace().get_id() === id) {
timeout(URGENT_DURATION, () => {
self.toggleClassName('urgent', false);
});
}
}
}
};
};
update(self);
self
.hook(Hyprland, 'event', () => update(self))
update(self);
self
.hook(hyprland, 'event', () => update(self))
// Deal with urgent windows
.hook(Hyprland, 'urgent', update)
.hook(hyprland, 'urgent', update)
.hook(Hyprland, 'notify::focused-workspace', () => {
if (Hyprland.get_focused_workspace().get_id() === id) {
self.toggleClassName('urgent', false);
}
});
}}
/>
</eventbox>
</revealer>
);
.hook(hyprland, 'notify::focused-workspace', () => {
if (hyprland.get_focused_workspace().get_id() === id) {
self.toggleClassName('urgent', false);
}
});
}}
/>
</eventbox>
</revealer>
);
};
export default () => {
const Hyprland = AstalHyprland.get_default();
const L_PADDING = 2;
const WS_WIDTH = 30;