refactor(greetd): clarify code
All checks were successful
Discord / discord commits (push) Has been skipped

This commit is contained in:
matt1432 2024-02-07 14:22:19 -05:00
parent 9ddec92f41
commit 606348d77c

View file

@ -1,3 +1,5 @@
/* eslint no-magic-numbers: 0 */
const { Box, Button, Entry, Label, Menu, MenuItem, Window } = Widget;
const { execAsync, idle, readFileAsync } = Utils;
@ -7,7 +9,18 @@ const { Gdk } = imports.gi;
const DEFAULT_NAME = 'matt';
// Types
type User = {
name: string;
uid: number;
gid: number;
desc: string;
home: string;
shell: string;
};
// Run Wallpaper daemon here to not cause issues at startup
execAsync(['swww', 'init', '--no-cache']).then(() => {
execAsync([
'swww', 'img', '-t', 'none',
@ -15,36 +28,38 @@ execAsync(['swww', 'init', '--no-cache']).then(() => {
]).catch(print);
}).catch(print);
// Put ref of Label here to change it easily later
const name = Label(DEFAULT_NAME);
// Initiate menu here to not have garbage collection take it away
// TODO: figure out type
let menu;
const dropdown = Button({
child: name,
setup: () => {
idle(() => {
readFileAsync('/etc/passwd').then((out) => {
const users = out.split('\n')
.map((u) => {
const parsePasswd = (fileContent: string) => {
const splitUsers = fileContent.split('\n');
const parsedUsers = splitUsers.map((u) => {
const user = u.split(':');
let i = 2;
return {
name: user[0],
uid: Number(user[i++]),
gid: Number(user[i++]),
desc: user[i++],
home: user[i++],
shell: user[i],
uid: Number(user[2]),
gid: Number(user[3]),
desc: user[4],
home: user[5],
shell: user[6],
};
})
.filter((u) => {
});
// Filter out system users, nixbld users and nobody
return parsedUsers.filter((u) => {
return u.uid >= 1000 &&
!u.name.includes('nixbld') &&
u.name !== 'nobody';
});
};
// FIXME: make menu scrollable
menu = Menu({
// FIXME: make menu scrollable
const DropdownMenu = (users: User[]) => Menu({
attach_widget: dropdown,
children: users.map((u) => MenuItem({
on_activate: () => {
@ -54,19 +69,27 @@ const dropdown = Button({
child: Label({
label: u.name,
justification: 'center',
css: `
min-width: ${dropdown.get_allocated_width() / 2}px;
`,
css: `min-width: ${dropdown.get_allocated_width() / 2}px;`,
}),
})),
});
});
const dropdown = Button({
child: name,
setup: () => {
idle(() => {
readFileAsync('/etc/passwd').then((out) => {
const users = parsePasswd(out);
menu = DropdownMenu(users);
}).catch(print);
});
},
on_primary_click_release: (_, event) => {
on_primary_click_release: (self, event) => {
menu.popup_at_widget(
dropdown,
self,
Gdk.Gravity.SOUTH,
Gdk.Gravity.NORTH,
event,
@ -77,6 +100,7 @@ const dropdown = Button({
const password = Entry({
placeholder_text: 'Password',
visibility: false,
on_accept: () => {
greetd.login(
name.label || '',
@ -109,6 +133,7 @@ const win = Window({
vpack: 'center',
hexpand: true,
vexpand: true,
children: [
dropdown,
password,