Skip to content

Commit

Permalink
fix: crash with paused state unavailable on glazewm provider
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-berger committed Dec 3, 2024
1 parent bed0044 commit f2cc263
Showing 1 changed file with 14 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
type WorkspaceDeactivatedEvent,
type WorkspaceUpdatedEvent,
type PauseChangedEvent,
type WmEvent,
} from 'glazewm';
import { z } from 'zod';

Expand Down Expand Up @@ -44,37 +45,15 @@ export function createGlazeWmProvider(
let state = await getInitialState();
queue.output(state);

unlistenEvents ??= await client.subscribeMany(
[
WmEventType.BINDING_MODES_CHANGED,
WmEventType.FOCUS_CHANGED,
WmEventType.FOCUSED_CONTAINER_MOVED,
WmEventType.TILING_DIRECTION_CHANGED,
WmEventType.WORKSPACE_ACTIVATED,
WmEventType.WORKSPACE_DEACTIVATED,
WmEventType.WORKSPACE_UPDATED,
WmEventType.PAUSE_CHANGED,
],
onEvent,
);
unlistenEvents ??= await client.subscribe(WmEventType.ALL, onEvent);

// TODO: Update state when monitors change.
// monitors.onChange(async () => {
// state = { ...state, ...(await getMonitorState()) };
// queue.value(state);
// });

async function onEvent(
e:
| BindingModesChangedEvent
| FocusChangedEvent
| FocusedContainerMovedEvent
| TilingDirectionChangedEvent
| WorkspaceActivatedEvent
| WorkspaceDeactivatedEvent
| WorkspaceUpdatedEvent
| PauseChangedEvent,
) {
async function onEvent(e: WmEvent) {
switch (e.eventType) {
case WmEventType.BINDING_MODES_CHANGED: {
state = { ...state, bindingModes: e.newBindingModes };
Expand Down Expand Up @@ -124,7 +103,7 @@ export function createGlazeWmProvider(
const { focused: focusedContainer } = await client.queryFocused();
const { bindingModes } = await client.queryBindingModes();
const { tilingDirection } = await client.queryTilingDirection();
const { paused: isPaused } = await client.queryPaused();
const isPaused = await getIsPaused();

return {
...(await getMonitorState()),
Expand All @@ -136,6 +115,16 @@ export function createGlazeWmProvider(
};
}

// Paused state is only available on v3.7.0+ of GlazeWM.
async function getIsPaused() {
try {
const { paused } = await client.queryPaused();
return paused;
} catch {
return false;
}
}

async function getMonitorState() {
const currentPosition = {
x: monitors.currentMonitor!.x,
Expand Down

0 comments on commit f2cc263

Please sign in to comment.