From da0299f06bfcd79d4c7ac86940146f31097a33dd Mon Sep 17 00:00:00 2001 From: Jas Singh Date: Sat, 21 Dec 2024 14:53:26 -0800 Subject: [PATCH] Log undefined menu models and action groups. Fix null errors for workspaces and update array property accessors for services. (#583) * Handle null values for actionGroup. * Add logs * more logs * Fix property accessors and workspace null errors. --- src/components/bar/modules/systray/index.tsx | 11 ++++++++--- .../bar/modules/workspaces/helpers/index.ts | 17 +++++++++++------ .../bar/modules/workspaces/helpers/utils.ts | 10 +++++----- .../bar/modules/workspaces/workspaces.tsx | 2 +- .../shortcuts/buttons/RecordingButton.tsx | 2 +- src/lib/behaviors/autoHide.ts | 6 +++--- 6 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/components/bar/modules/systray/index.tsx b/src/components/bar/modules/systray/index.tsx index ad47277..2343f6d 100644 --- a/src/components/bar/modules/systray/index.tsx +++ b/src/components/bar/modules/systray/index.tsx @@ -10,7 +10,7 @@ const systemtray = AstalTray.get_default(); const { ignore, customIcons } = options.bar.systray; //TODO: Connect to `notify::menu-model` and `notify::action-group` to have up to date menu and action group -const createMenu = (menuModel: Gio.MenuModel, actionGroup: Gio.ActionGroup): Gtk.Menu => { +const createMenu = (menuModel: Gio.MenuModel, actionGroup: Gio.ActionGroup | null): Gtk.Menu => { const menu = Gtk.Menu.new_from_model(menuModel); menu.insert_action_group('dbusmenu', actionGroup); @@ -38,9 +38,14 @@ const MenuEntry = ({ item, child }: MenuEntryProps): JSX.Element => { const entryBinding = Variable.derive( [bind(item, 'menuModel'), bind(item, 'actionGroup')], (menuModel, actionGroup) => { - if (menuModel && actionGroup) { - menu = createMenu(menuModel, actionGroup); + if (!menuModel) { + return console.error(`Menu Model not found for ${item.id}`); } + if (!actionGroup) { + return console.error(`Action Group not found for ${item.id}`); + } + + menu = createMenu(menuModel, actionGroup); }, ); diff --git a/src/components/bar/modules/workspaces/helpers/index.ts b/src/components/bar/modules/workspaces/helpers/index.ts index eb12b69..8403117 100644 --- a/src/components/bar/modules/workspaces/helpers/index.ts +++ b/src/components/bar/modules/workspaces/helpers/index.ts @@ -33,9 +33,13 @@ export const getWorkspacesForMonitor = ( const monitorMap: MonitorMap = {}; - const workspaceMonitorList = workspaceList.map((m) => { - return { id: m.monitor.id, name: m.monitor.name }; - }); + const wsList = workspaceList ?? []; + + const workspaceMonitorList = wsList + .filter((m) => m !== null) + .map((m) => { + return { id: m.monitor?.id, name: m.monitor?.name }; + }); const monitors = [...new Map([...workspaceMonitorList, ...monitorList].map((item) => [item.id, item])).values()]; @@ -150,7 +154,7 @@ const navigateWorkspace = ( if (workspacesList.length === 0) return; - const currentIndex = workspacesList.indexOf(hyprlandService.focusedWorkspace.id); + const currentIndex = workspacesList.indexOf(hyprlandService.focusedWorkspace?.id); const step = direction === 'next' ? 1 : -1; let newIndex = (currentIndex + step + workspacesList.length) % workspacesList.length; let attempts = 0; @@ -283,7 +287,8 @@ export const getWorkspacesToRender = ( let allWorkspaces = range(totalWorkspaces || 8); const activeWorkspaces = workspaceList.map((ws) => ws.id); - const workspaceMonitorList = workspaceList.map((ws) => { + const wsList = workspaceList ?? []; + const workspaceMonitorList = wsList.map((ws) => { return { id: ws.monitor?.id || -1, name: ws.monitor?.name || '', @@ -310,7 +315,7 @@ export const getWorkspacesToRender = ( if (isMonitorSpecific) { const workspacesInRange = range(totalWorkspaces).filter((ws) => { - return getWorkspacesForMonitor(ws, workspaceRules, monitor, workspaceList, monitorList); + return getWorkspacesForMonitor(ws, workspaceRules, monitor, wsList, monitorList); }); allWorkspaces = [...new Set([...activesForMonitor, ...workspacesInRange])]; diff --git a/src/components/bar/modules/workspaces/helpers/utils.ts b/src/components/bar/modules/workspaces/helpers/utils.ts index 945729e..955537c 100644 --- a/src/components/bar/modules/workspaces/helpers/utils.ts +++ b/src/components/bar/modules/workspaces/helpers/utils.ts @@ -21,7 +21,7 @@ const { showWsIcons, showAllActive, numbered_active_indicator: wsActiveIndicator * @returns True if the workspace is active on the monitor, false otherwise. */ const isWorkspaceActiveOnMonitor = (monitor: number, i: number): boolean => { - return showAllActive.get() && hyprlandService.get_monitor(monitor).activeWorkspace.id === i; + return showAllActive.get() && hyprlandService.get_monitor(monitor)?.activeWorkspace?.id === i; }; /** @@ -84,7 +84,7 @@ export const getWsColor = ( showWsIcons.get() && smartHighlight && wsActiveIndicator.get() === 'highlight' && - (hyprlandService.focusedWorkspace.id === i || isWorkspaceActiveOnMonitor(monitor, i)) + (hyprlandService.focusedWorkspace?.id === i || isWorkspaceActiveOnMonitor(monitor, i)) ) { const iconColor = monochrome.get() ? background.get() : wsBackground.get(); const iconBackground = hasColor && isValidGjsColor(iconEntry.color) ? iconEntry.color : active.get(); @@ -122,7 +122,7 @@ export const getAppIcon = ( const clients = hyprlandService .get_clients() - .filter((client) => client.workspace.id === workspaceIndex) + .filter((client) => client?.workspace?.id === workspaceIndex) .map((client) => [client.class, client.title]); if (!clients.length) { @@ -200,7 +200,7 @@ export const renderClassnames = ( if (showNumbered || showWsIcons) { const numActiveInd = - hyprlandService.focusedWorkspace.id === i || isWorkspaceActiveOnMonitor(monitor, i) + hyprlandService.focusedWorkspace?.id === i || isWorkspaceActiveOnMonitor(monitor, i) ? numberedActiveIndicator : ''; @@ -255,7 +255,7 @@ export const renderLabel = ( } if (showIcons) { - if (hyprlandService.focusedWorkspace.id === i || isWorkspaceActiveOnMonitor(monitor, i)) { + if (hyprlandService.focusedWorkspace?.id === i || isWorkspaceActiveOnMonitor(monitor, i)) { return activeIndicator; } if ((hyprlandService.get_workspace(i)?.get_clients().length || 0) > 0) { diff --git a/src/components/bar/modules/workspaces/workspaces.tsx b/src/components/bar/modules/workspaces/workspaces.tsx index 61914e3..65122d4 100644 --- a/src/components/bar/modules/workspaces/workspaces.tsx +++ b/src/components/bar/modules/workspaces/workspaces.tsx @@ -86,7 +86,7 @@ export const WorkspaceModule = ({ monitor }: WorkspaceModuleProps): JSX.Element smartHighlightEnabled: boolean, monitorList: AstalHyprland.Monitor[], ) => { - const activeWorkspace = hyprlandService.focusedWorkspace.id; + const activeWorkspace = hyprlandService.focusedWorkspace?.id || -99999; const workspacesToRender = getWorkspacesToRender( totalWorkspaces, diff --git a/src/components/menus/dashboard/shortcuts/buttons/RecordingButton.tsx b/src/components/menus/dashboard/shortcuts/buttons/RecordingButton.tsx index 92e3397..8f26406 100644 --- a/src/components/menus/dashboard/shortcuts/buttons/RecordingButton.tsx +++ b/src/components/menus/dashboard/shortcuts/buttons/RecordingButton.tsx @@ -10,7 +10,7 @@ const MonitorListDropdown = (): JSX.Element => { const monitorList: Variable = Variable([]); const monitorBinding = Variable.derive([bind(hyprlandService, 'monitors')], () => - monitorList.set(hyprlandService.monitors), + monitorList.set(hyprlandService.get_monitors()), ); return ( diff --git a/src/lib/behaviors/autoHide.ts b/src/lib/behaviors/autoHide.ts index d3ab6d3..a678bcf 100644 --- a/src/lib/behaviors/autoHide.ts +++ b/src/lib/behaviors/autoHide.ts @@ -24,12 +24,12 @@ const focusedClient = (focusedClient: AstalHyprland.Client): void => { export const initializeAutoHide = (): void => { Variable.derive([bind(autoHide), bind(forceUpdater), bind(hyprlandService, 'workspaces')], (shouldAutohide) => { if (shouldAutohide === 'never') { - hyprlandService.monitors.forEach((monitor) => { + hyprlandService.get_monitors().forEach((monitor) => { App.get_window(`bar-${monitor.id}`)?.set_visible(true); }); } - hyprlandService.workspaces.map((workspace) => { + hyprlandService.get_workspaces().map((workspace) => { if (autoHide.get() === 'single-window') { App.get_window(`bar-${workspace.monitor.id}`)?.set_visible(workspace.get_clients().length !== 1); } @@ -42,7 +42,7 @@ export const initializeAutoHide = (): void => { Variable.derive([bind(autoHide)], (shouldAutohide) => { if (shouldAutohide === 'fullscreen') { - hyprlandService.workspaces.forEach((workspace) => { + hyprlandService.get_workspaces().forEach((workspace) => { App.get_window(`bar-${workspace.monitor.id}`)?.set_visible(!workspace.hasFullscreen); }); }