ref(ags): type stuff

This commit is contained in:
Updater 2023-12-20 03:45:05 -05:00
parent ae615b08f5
commit c7558cd6ef
4 changed files with 94 additions and 50 deletions

View file

@ -9,17 +9,25 @@ import { Box, Entry, Icon, Label, ListBox, Revealer, Scrollable } from 'resource
import PopupWindow from '../misc/popup.js';
import AppItem from './app-item.js';
/** @typedef {import('types/service/applications.js').Application} App */
const Applauncher = ({ window_name = 'applauncher' } = {}) => {
/** @type Array<any> */
let fzfResults;
const list = ListBox();
/** @param {String} text */
const setSort = (text) => {
const fzf = new Fzf(Applications.list, {
selector: (app) => app.name,
tiebreakers: [(a, b) => b.frequency -
a.frequency],
selector: /** @param {App} app */ (app) => app.name,
tiebreakers: [
/**
* @param {App} a
* @param {App} b
*/
(a, b) => b.frequency - a.frequency,
],
});
fzfResults = fzf.find(text);
@ -38,8 +46,11 @@ const Applauncher = ({ window_name = 'applauncher' } = {}) => {
};
const makeNewChildren = () => {
/** @type Array<typeof imports.gi.Gtk.ListBoxRow> */
// @ts-expect-error
Array.from(list.get_children()).forEach((ch) => {
const rows = list.get_children();
rows.forEach((ch) => {
ch.destroy();
});
@ -85,14 +96,17 @@ const Applauncher = ({ window_name = 'applauncher' } = {}) => {
setSort(text);
let visibleApps = 0;
/** @type Array<typeof imports.gi.Gtk.ListBoxRow> */
// @ts-expect-error
Array.from(list.get_children()).forEach((row) => {
const rows = list.get_children();
rows.forEach((row) => {
row.changed();
const item = row.get_children()[0];
if (item?.attribute.app) {
const isMatching = Array.from(fzfResults).find((r) => {
const isMatching = fzfResults.find((r) => {
return r.item.name === item.attribute.app.name;
});

View file

@ -7,6 +7,8 @@ import CursorBox from '../../misc/cursorbox.js';
const URGENT_DURATION = 1000;
/** @typedef {import('types/widgets/revealer.js').default} Revealer */
/** @property {number} id */
const Workspace = ({ id }) => {
@ -32,7 +34,7 @@ const Workspace = ({ id }) => {
*/
const update = (_, addr) => {
const workspace = Hyprland.getWorkspace(id);
const occupied = workspace && workspace['windows'] > 0;
const occupied = workspace && workspace.windows > 0;
self.toggleClassName('occupied', occupied);
@ -43,7 +45,7 @@ const Workspace = ({ id }) => {
// Deal with urgent windows
const client = Hyprland.getClient(addr);
const isThisUrgent = client &&
client['workspace']['id'] === id;
client.workspace.id === id;
if (isThisUrgent) {
self.toggleClassName('urgent', true);
@ -81,9 +83,12 @@ export default () => {
/** @param {import('types/widgets/box').default} self */
const updateHighlight = (self) => {
const currentId = Hyprland.active.workspace.id;
/** @type Array<Revealer> */
// @ts-expect-error
const indicators = self?.get_parent()?.child.child.child.children;
const currentIndex = Array.from(indicators)
const currentIndex = indicators
.findIndex((w) => w.attribute.id === currentId);
if (currentIndex < 0) {
@ -107,37 +112,46 @@ export default () => {
child: Box({
class_name: 'workspaces',
attribute: { workspaces: [] },
attribute: {
/** @type Array<Revealer> */
workspaces: [],
},
setup: (self) => {
/** @type function(void):Array<Revealer> */
const workspaces = () => self.attribute.workspaces;
const refresh = () => {
Array.from(self.children).forEach((rev) => {
// @ts-expect-error
self.children.forEach((rev) => {
// @ts-expect-error they are in fact revealers
rev.reveal_child = false;
});
Array.from(self.attribute.workspaces).forEach((ws) => {
workspaces().forEach((ws) => {
ws.reveal_child = true;
});
};
const updateWorkspaces = () => {
Hyprland.workspaces.forEach((ws) => {
const currentWs = Array.from(self.children)
// @ts-expect-error
.find((ch) => ch.attribute.id === ws['id']);
const currentWs = self.children.find((ch) => {
return ch.attribute.id === ws.id;
});
if (!currentWs && ws['id'] > 0) {
self.add(Workspace({ id: ws['id'] }));
if (!currentWs && ws.id > 0) {
self.add(Workspace({ id: ws.id }));
}
});
self.show_all();
// Make sure the order is correct
Array.from(self.attribute.workspaces)
.forEach((workspace, i) => {
workspace.get_parent()
.reorder_child(workspace, i);
});
workspaces().forEach((workspace, i) => {
// @ts-expect-error
workspace?.get_parent()?.reorder_child(
workspace,
i,
);
});
};
self.hook(Hyprland, () => {
@ -145,7 +159,7 @@ export default () => {
self.children.filter((ch) => {
return Hyprland.workspaces.find((ws) => {
// @ts-expect-error
return ws['id'] === ch.attribute.id;
return ws.id === ch.attribute.id;
});
// @ts-expect-error
}).sort((a, b) => a.attribute.id - b.attribute.id);

View file

@ -6,7 +6,7 @@ import Variable from 'resource:///com/github/Aylur/ags/variable.js';
import { Box, Icon, Label, Button } from 'resource:///com/github/Aylur/ags/widget.js';
import { lookUpIcon } from 'resource:///com/github/Aylur/ags/utils.js';
import GLib from 'gi://GLib';
const { GLib } = imports.gi;
const setTime = (time) => {
return GLib.DateTime

View file

@ -1,4 +1,5 @@
import App from 'resource:///com/github/Aylur/ags/app.js';
// @ts-expect-error
import Bluetooth from 'resource:///com/github/Aylur/ags/service/bluetooth.js';
import Network from 'resource:///com/github/Aylur/ags/service/network.js';
import Variable from 'resource:///com/github/Aylur/ags/variable.js';
@ -14,17 +15,29 @@ import { NetworkMenu } from './network.js';
import { BluetoothMenu } from './bluetooth.js';
const SPACING = 28;
const ButtonStates = [];
/** @typedef {import('types/widgets/widget').default} Widget */
/**
* @param {{
* command?: function
* secondary_command?: function
* onOpen?: function(Widget):void
* icon: any
* indicator?: any
* menu?: any
* }} o
*/
const GridButton = ({
command = () => { /**/ },
secondaryCommand = () => { /**/ },
onOpen = () => { /**/ },
command = () => {/**/},
secondary_command = () => {/**/},
onOpen = () => {/**/},
icon,
indicator,
menu,
} = {}) => {
}) => {
const Activated = Variable(false);
ButtonStates.push(Activated);
@ -32,7 +45,7 @@ const GridButton = ({
// Allow setting icon dynamically or statically
if (typeof icon === 'string') {
icon = Icon({
className: 'grid-label',
class_name: 'grid-label',
icon,
setup: (self) => {
self.hook(Activated, () => {
@ -45,7 +58,7 @@ const GridButton = ({
}
else {
icon = Icon({
className: 'grid-label',
class_name: 'grid-label',
setup: (self) => {
self
.hook(...icon)
@ -60,7 +73,7 @@ const GridButton = ({
if (indicator) {
indicator = Label({
className: 'sub-label',
class_name: 'sub-label',
justification: 'left',
truncate: 'end',
maxWidthChars: 12,
@ -82,15 +95,15 @@ const GridButton = ({
vertical: true,
children: [
Box({
className: 'grid-button',
class_name: 'grid-button',
children: [
CursorBox({
className: 'left-part',
class_name: 'left-part',
on_primary_click_release: () => {
if (Activated.value) {
secondaryCommand();
secondary_command();
}
else {
command();
@ -101,7 +114,7 @@ const GridButton = ({
}),
CursorBox({
className: 'right-part',
class_name: 'right-part',
on_primary_click_release: () => {
ButtonStates.forEach((state) => {
@ -112,10 +125,13 @@ const GridButton = ({
Activated.value = !Activated.value;
},
onHover: (self) => {
on_hover: (self) => {
if (menu) {
const rowMenu = self.get_parent().get_parent()
.get_parent().get_parent().children[1];
const rowMenu =
self.get_parent()
?.get_parent()?.get_parent()
?.get_parent()?.get_parent()
?.children[1];
const isSetup = rowMenu.get_children()
.find((ch) => ch === menu);
@ -129,7 +145,7 @@ const GridButton = ({
child: Icon({
icon: `${App.configDir }/icons/down-large.svg`,
className: 'grid-chev',
class_name: 'grid-chev',
setup: (self) => {
self.hook(Activated, () => {
@ -162,7 +178,7 @@ const Row = ({ buttons } = {}) => {
children: [
Box({
className: 'button-row',
class_name: 'button-row',
hpack: 'center',
}),
@ -189,7 +205,7 @@ const FirstRow = () => Row({
GridButton({
command: () => Network.toggleWifi(),
secondaryCommand: () => {
secondary_command: () => {
// TODO: connection editor
},
@ -211,7 +227,7 @@ const FirstRow = () => Row({
//
},
secondaryCommand: () => {
secondary_command: () => {
//
},
@ -221,7 +237,7 @@ const FirstRow = () => Row({
GridButton({
command: () => Bluetooth.toggle(),
secondaryCommand: () => {
secondary_command: () => {
// TODO: bluetooth connection editor
},
@ -261,7 +277,7 @@ const SecondRow = () => Row({
'@DEFAULT_SINK@', 'toggle']).catch(print);
},
secondaryCommand: () => {
secondary_command: () => {
execAsync(['bash', '-c', 'pavucontrol'])
.catch(print);
},
@ -277,7 +293,7 @@ const SecondRow = () => Row({
'@DEFAULT_SOURCE@', 'toggle']).catch(print);
},
secondaryCommand: () => {
secondary_command: () => {
execAsync(['bash', '-c', 'pavucontrol'])
.catch(print);
},
@ -291,14 +307,14 @@ const SecondRow = () => Row({
command: () => {
execAsync(['lock']).catch(print);
},
secondaryCommand: () => App.openWindow('powermenu'),
secondary_command: () => App.openWindow('powermenu'),
icon: 'system-lock-screen-symbolic',
}),
],
});
export default () => Box({
className: 'button-grid',
class_name: 'button-grid',
vertical: true,
hpack: 'center',
children: [