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,36 +28,38 @@ 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 dropdown = Button({ const parsePasswd = (fileContent: string) => {
child: name, const splitUsers = fileContent.split('\n');
setup: () => { const parsedUsers = splitUsers.map((u) => {
idle(() => {
readFileAsync('/etc/passwd').then((out) => {
const users = out.split('\n')
.map((u) => {
const user = u.split(':'); const user = u.split(':');
let i = 2;
return { return {
name: user[0], name: user[0],
uid: Number(user[i++]), uid: Number(user[2]),
gid: Number(user[i++]), gid: Number(user[3]),
desc: user[i++], desc: user[4],
home: user[i++], home: user[5],
shell: user[i], shell: user[6],
}; };
}) });
.filter((u) => {
// Filter out system users, nixbld users and nobody
return parsedUsers.filter((u) => {
return u.uid >= 1000 && return u.uid >= 1000 &&
!u.name.includes('nixbld') && !u.name.includes('nixbld') &&
u.name !== 'nobody'; u.name !== 'nobody';
}); });
};
// FIXME: make menu scrollable // FIXME: make menu scrollable
menu = Menu({ const DropdownMenu = (users: User[]) => Menu({
attach_widget: dropdown, attach_widget: dropdown,
children: users.map((u) => MenuItem({ children: users.map((u) => MenuItem({
on_activate: () => { on_activate: () => {
@ -54,19 +69,27 @@ const dropdown = Button({
child: Label({ child: Label({
label: u.name, label: u.name,
justification: 'center', justification: 'center',
css: ` css: `min-width: ${dropdown.get_allocated_width() / 2}px;`,
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); }).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,