nixos-configs/devices/wim/config/ags/js/applauncher/main.js

157 lines
4.1 KiB
JavaScript
Raw Normal View History

import App from 'resource:///com/github/Aylur/ags/app.js';
import Applications from 'resource:///com/github/Aylur/ags/service/applications.js';
import Hyprland from 'resource:///com/github/Aylur/ags/service/hyprland.js';
2023-12-07 01:18:47 -05:00
// TODO: find cleaner way to import this
import { Fzf } from '../../node_modules/fzf/dist/fzf.es.js';
2023-12-07 01:18:47 -05:00
import { Box, Entry, Icon, Label, ListBox, Scrollable } from 'resource:///com/github/Aylur/ags/widget.js';
import PopupWindow from '../misc/popup.js';
import AppItem from './app-item.js';
const Applauncher = ({ window_name = 'applauncher' } = {}) => {
2023-12-07 01:18:47 -05:00
let fzfResults;
const list = ListBox();
2023-12-18 18:00:30 -05:00
/** @param {String} text */
2023-12-07 01:18:47 -05:00
const setSort = (text) => {
const fzf = new Fzf(Applications.list, {
selector: (app) => app.name,
2023-12-18 18:00:30 -05:00
tiebreakers: [(a, b) => b.frequency -
a.frequency],
2023-12-07 01:18:47 -05:00
});
fzfResults = fzf.find(text);
list.set_sort_func((a, b) => {
2023-12-18 18:00:30 -05:00
// @ts-expect-error
const row1 = a.get_children()[0]?.attribute.app.name;
// @ts-expect-error
const row2 = b.get_children()[0]?.attribute.app.name;
2023-12-07 01:18:47 -05:00
if (!row1 || !row2) {
return 0;
}
return fzfResults.indexOf(row1) -
fzfResults.indexOf(row1) || 0;
});
};
const makeNewChildren = () => {
list.get_children().forEach((ch) => {
ch.destroy();
});
const children = Applications.query('')
.flatMap((app) => AppItem(app));
children.forEach((ch) => {
list.add(ch);
});
list.show_all();
2023-12-07 01:18:47 -05:00
};
2023-12-07 01:18:47 -05:00
makeNewChildren();
2023-12-18 18:00:30 -05:00
// FIXME: always visible
const placeholder = Label({
label: " Couldn't find a match",
2023-12-18 18:00:30 -05:00
class_name: 'placeholder',
});
const entry = Entry({
// Set some text so on-change works the first time
text: '-',
hexpand: true,
on_accept: ({ text }) => {
const appList = Applications.query(text || '');
if (appList[0]) {
2023-12-07 01:18:47 -05:00
App.closeWindow(window_name);
Hyprland.sendMessage(`dispatch exec sh -c
${appList[0].executable}`);
++appList[0].frequency;
}
},
on_change: ({ text }) => {
2023-12-18 18:00:30 -05:00
if (!text) {
return;
}
2023-12-07 01:18:47 -05:00
setSort(text);
let visibleApps = 0;
2023-12-07 01:18:47 -05:00
list.get_children().forEach((row) => {
2023-12-18 18:00:30 -05:00
// @ts-expect-error
2023-12-07 01:18:47 -05:00
row.changed();
2023-12-18 18:00:30 -05:00
// @ts-expect-error
const item = row.get_children()[0];
2023-12-07 01:18:47 -05:00
2023-12-18 18:00:30 -05:00
if (item?.attribute.app) {
2023-12-07 01:18:47 -05:00
const isMatching = fzfResults.find((r) => {
2023-12-18 18:00:30 -05:00
return r.item.name === item.attribute.app.name;
2023-12-07 01:18:47 -05:00
});
row.visible = isMatching;
2023-12-07 01:18:47 -05:00
if (isMatching) {
++visibleApps;
}
}
});
placeholder.visible = visibleApps <= 0;
},
});
return Box({
2023-12-18 18:00:30 -05:00
class_name: 'applauncher',
vertical: true,
setup: (self) => {
self.hook(App, (_, name, visible) => {
if (name !== window_name) {
return;
}
entry.text = '';
if (visible) {
entry.grab_focus();
}
else {
makeNewChildren();
}
});
},
children: [
Box({
2023-12-18 18:00:30 -05:00
class_name: 'header',
children: [
Icon('preferences-system-search-symbolic'),
entry,
],
}),
Scrollable({
hscroll: 'never',
vscroll: 'automatic',
child: Box({
vertical: true,
children: [list, placeholder],
}),
}),
],
});
};
export default () => PopupWindow({
name: 'applauncher',
focusable: true,
child: Applauncher(),
});