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