From 99c48857ec5e44e8c171a17b39b9ff8a0146e882 Mon Sep 17 00:00:00 2001 From: Lars Berger Date: Sun, 17 Mar 2024 03:27:20 +0800 Subject: [PATCH] feat: change glazewm + komorebi providers to react to current monitor changes --- packages/client-api/src/desktop/monitors.ts | 33 +++++-------------- .../glazewm/create-glazewm-provider.ts | 24 +++++++++----- .../komorebi/create-komorebi-provider.ts | 30 ++++++++++------- 3 files changed, 44 insertions(+), 43 deletions(-) diff --git a/packages/client-api/src/desktop/monitors.ts b/packages/client-api/src/desktop/monitors.ts index 970708e1..4bdb8dcf 100644 --- a/packages/client-api/src/desktop/monitors.ts +++ b/packages/client-api/src/desktop/monitors.ts @@ -18,12 +18,6 @@ interface MonitorCache { allMonitors: MonitorInfo[]; } -const [monitorCache, setMonitorCache] = createStore<{ - value: MonitorCache | null; -}>({ - value: null, -}); - export async function getMonitors() { return createCachePromise ?? (createCachePromise = createMonitorCache()); } @@ -43,17 +37,11 @@ async function createMonitorCache() { // return value, and refresh it in an effect when displays are changed. // Ref https://github.com/tauri-apps/tauri/issues/8405 - setMonitorCache({ - value: { - currentMonitor: currentMonitor - ? toMonitorInfo(currentMonitor) - : null, - primaryMonitor: primaryMonitor - ? toMonitorInfo(primaryMonitor) - : null, - secondaryMonitors: secondaryMonitors.map(toMonitorInfo), - allMonitors: allMonitors.map(toMonitorInfo), - }, + const [monitorCache, setMonitorCache] = createStore({ + currentMonitor: currentMonitor ? toMonitorInfo(currentMonitor) : null, + primaryMonitor: primaryMonitor ? toMonitorInfo(primaryMonitor) : null, + secondaryMonitors: secondaryMonitors.map(toMonitorInfo), + allMonitors: allMonitors.map(toMonitorInfo), }); getCurrentWindow().onResized(() => updateCurrentMonitor()); @@ -64,16 +52,13 @@ async function createMonitorCache() { const currentMonitor = await getCurrentMonitor(); setMonitorCache({ - value: { - ...monitorCache.value!, - currentMonitor: currentMonitor - ? toMonitorInfo(currentMonitor) - : null, - }, + currentMonitor: currentMonitor + ? toMonitorInfo(currentMonitor) + : null, }); } - return monitorCache.value!; + return monitorCache; } function isMatch(monitorA: Monitor, monitorB: Monitor) { diff --git a/packages/client-api/src/providers/glazewm/create-glazewm-provider.ts b/packages/client-api/src/providers/glazewm/create-glazewm-provider.ts index af61de73..e4ba7d14 100644 --- a/packages/client-api/src/providers/glazewm/create-glazewm-provider.ts +++ b/packages/client-api/src/providers/glazewm/create-glazewm-provider.ts @@ -1,4 +1,4 @@ -import type { Owner } from 'solid-js'; +import { createEffect, on, type Owner } from 'solid-js'; import { createStore } from 'solid-js/store'; import { GwmClient, GwmEventType, type Workspace } from 'glazewm'; @@ -10,7 +10,7 @@ export async function createGlazewmProvider( _: GlazewmProviderConfig, __: Owner, ) { - const { currentMonitor } = await getMonitors(); + const monitors = await getMonitors(); const client = new GwmClient(); const [glazewmVariables, setGlazewmVariables] = createStore({ @@ -29,26 +29,34 @@ export async function createGlazewmProvider( await client.subscribeMany( [ - GwmEventType.WORKSPACE_ACTIVATED, + GwmEventType.WORKSPACE_ACTIVATED, GwmEventType.WORKSPACE_DEACTIVATED, - GwmEventType.FOCUS_CHANGED + GwmEventType.FOCUS_CHANGED, ], refetch, ); + createEffect(on(() => monitors.currentMonitor, refetch)); + async function refetch() { - const monitors = await client.getMonitors(); - const currentPosition = { x: currentMonitor!.x, y: currentMonitor!.y }; + const currentPosition = { + x: monitors.currentMonitor!.x, + y: monitors.currentMonitor!.y, + }; // Get GlazeWM monitor that corresponds to the bar's monitor. - const monitor = monitors.reduce((a, b) => + const glazewmMonitor = (await client.getMonitors()).reduce((a, b) => getCoordinateDistance(currentPosition, a) < getCoordinateDistance(currentPosition, b) ? a : b, ); - setGlazewmVariables({ workspacesOnMonitor: monitor.children.sort((a, b) => Number(a.name) - Number(b.name)) }); + setGlazewmVariables({ + workspacesOnMonitor: glazewmMonitor.children.sort( + (a, b) => Number(a.name) - Number(b.name), + ), + }); } return { diff --git a/packages/client-api/src/providers/komorebi/create-komorebi-provider.ts b/packages/client-api/src/providers/komorebi/create-komorebi-provider.ts index 9f5ffcf9..954ae16d 100644 --- a/packages/client-api/src/providers/komorebi/create-komorebi-provider.ts +++ b/packages/client-api/src/providers/komorebi/create-komorebi-provider.ts @@ -110,7 +110,7 @@ export async function createKomorebiProvider( config: KomorebiProviderConfig, owner: Owner, ): Promise { - const { currentMonitor } = await getMonitors(); + const monitors = await getMonitors(); const providerListener = await createProviderListener< KomorebiProviderConfig, @@ -125,7 +125,11 @@ export async function createKomorebiProvider( async function getVariables() { const state = providerListener(); - const currentPosition = { x: currentMonitor!.x, y: currentMonitor!.y }; + + const currentPosition = { + x: monitors.currentMonitor!.x, + y: monitors.currentMonitor!.y, + }; // Get Komorebi monitor that corresponds to the window's monitor. const currentKomorebiMonitor = state.allMonitors.reduce((a, b) => @@ -141,25 +145,29 @@ export async function createKomorebiProvider( : b, ); - const displayedWorkspace = + const displayedKomorebiWorkspace = currentKomorebiMonitor.workspaces[ currentKomorebiMonitor.focusedWorkspaceIndex ]!; - const allWorkspaces = state.allMonitors.flatMap( + const allKomorebiWorkspaces = state.allMonitors.flatMap( monitor => monitor.workspaces, ); - const focusedMonitor = state.allMonitors[state.focusedMonitorIndex]!; - const focusedWorkspace = - focusedMonitor.workspaces[focusedMonitor.focusedWorkspaceIndex]!; + const focusedKomorebiMonitor = + state.allMonitors[state.focusedMonitorIndex]!; + + const focusedKomorebiWorkspace = + focusedKomorebiMonitor.workspaces[ + focusedKomorebiMonitor.focusedWorkspaceIndex + ]!; return { - displayedWorkspace, - focusedWorkspace, + displayedWorkspace: displayedKomorebiWorkspace, + focusedWorkspace: focusedKomorebiWorkspace, currentWorkspaces: currentKomorebiMonitor.workspaces, - allWorkspaces, - focusedMonitor, + allWorkspaces: allKomorebiWorkspaces, + focusedMonitor: focusedKomorebiMonitor, currentMonitor: currentKomorebiMonitor, allMonitors: state.allMonitors, };