Skip to content

Commit

Permalink
Merge branch 'glzr-io:main' into networkactivity_provider
Browse files Browse the repository at this point in the history
  • Loading branch information
CtByte authored Jul 29, 2024
2 parents 10c23ea + 77a6aa4 commit 15aab5b
Show file tree
Hide file tree
Showing 18 changed files with 337 additions and 108 deletions.
8 changes: 3 additions & 5 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,19 @@ jobs:
steps:
- uses: actions/checkout@v4

- uses: pnpm/action-setup@v2
with:
version: 9
- uses: pnpm/action-setup@v4

- uses: actions/setup-node@v4
with:
node-version: 20
cache: pnpm

- uses: dtolnay/rust-toolchain@be73d7920c329f220ce78e0234b8f96b7ae60248
- uses: dtolnay/rust-toolchain@21dc36fb71dd22e3317045c0c31a3f4249868b17
with:
toolchain: nightly
targets: ${{ matrix.rust-targets }}

- uses: swatinem/rust-cache@988c164c3d0e93c4dbab36aaf5bbeb77425b2894
- uses: swatinem/rust-cache@9bdad043e88c75890e36ad3bbc8d27f0090dd609
with:
shared-key: ${{ matrix.tauri-target }}-${{ hashFiles('Cargo.lock') }}

Expand Down
8 changes: 3 additions & 5 deletions .github/workflows/lint-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,19 @@ jobs:
steps:
- uses: actions/checkout@v4

- uses: pnpm/action-setup@v2
with:
version: 9
- uses: pnpm/action-setup@v4

- uses: actions/setup-node@v4
with:
node-version: 20
cache: pnpm

- uses: dtolnay/rust-toolchain@be73d7920c329f220ce78e0234b8f96b7ae60248
- uses: dtolnay/rust-toolchain@21dc36fb71dd22e3317045c0c31a3f4249868b17
with:
toolchain: nightly
components: rustfmt

- uses: swatinem/rust-cache@988c164c3d0e93c4dbab36aaf5bbeb77425b2894
- uses: swatinem/rust-cache@9bdad043e88c75890e36ad3bbc8d27f0090dd609

- run: pnpm i
- run: pnpm run lint
4 changes: 1 addition & 3 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ jobs:
pattern: bundle-**
path: tmp

- uses: pnpm/action-setup@v2
with:
version: 8
- uses: pnpm/action-setup@v4

- uses: actions/setup-node@v4
with:
Expand Down
2 changes: 1 addition & 1 deletion packages/client-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@tauri-apps/api": "2.0.0-beta.15",
"@tauri-apps/plugin-dialog": "2.0.0-beta.7",
"@tauri-apps/plugin-shell": "2.0.0-beta.8",
"glazewm": "1.1.2",
"glazewm": "1.4.1",
"luxon": "3.4.4",
"solid-js": "1.8.14",
"yaml": "2.3.4",
Expand Down
4 changes: 2 additions & 2 deletions packages/client-api/src/providers/create-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createBatteryProvider } from './battery/create-battery-provider';
import { createCpuProvider } from './cpu/create-cpu-provider';
import { createDataProvider } from './data/create-data-provider';
import { createDateProvider } from './date/create-date-provider';
import { createGlazewmProvider } from './glazewm/create-glazewm-provider';
import { createGlazeWmProvider } from './glazewm/create-glazewm-provider';
import { createHostProvider } from './host/create-host-provider';
import { createIpProvider } from './ip/create-ip-provider';
import { createKomorebiProvider } from './komorebi/create-komorebi-provider';
Expand Down Expand Up @@ -35,7 +35,7 @@ export async function createProvider(
case ProviderType.DATE:
return createDateProvider(config, owner);
case ProviderType.GLAZEWM:
return createGlazewmProvider(config, owner);
return createGlazeWmProvider(config, owner);
case ProviderType.HOST:
return createHostProvider(config, owner);
case ProviderType.IP:
Expand Down
236 changes: 204 additions & 32 deletions packages/client-api/src/providers/glazewm/create-glazewm-provider.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,241 @@
import {
TilingDirection,
WmClient,
WmEventType,
type BindingModeConfig,
type BindingModesChangedEvent,
type Container,
type FocusChangedEvent,
type FocusedContainerMovedEvent,
type Monitor,
type TilingDirectionChangedEvent,
type Workspace,
type WorkspaceActivatedEvent,
type WorkspaceDeactivatedEvent,
type WorkspaceUpdatedEvent,
} from 'glazewm';
import { createEffect, on, runWithOwner, type Owner } from 'solid-js';
import { createStore } from 'solid-js/store';
import { GwmClient, GwmEventType, type Workspace } from 'glazewm';

import { getMonitors } from '~/desktop';
import type { GlazewmProviderConfig } from '~/user-config';
import { getCoordinateDistance } from '~/utils';

export async function createGlazewmProvider(
export interface GlazeWmProvider {
/**
* Workspace displayed on the current monitor.
*/
displayedWorkspace: Workspace;

/**
* Workspace that currently has focus (on any monitor).
*/
focusedWorkspace: Workspace;

/**
* Workspaces on the current monitor.
*/
currentWorkspaces: Workspace[];

/**
* Workspaces across all monitors.
*/
allWorkspaces: Workspace[];

/**
* All monitors.
*/
allMonitors: Monitor[];

/**
* Monitor that currently has focus.
*/
focusedMonitor: Monitor;

/**
* Monitor that is nearest to this Zebar window.
*/
currentMonitor: Monitor;

/**
* Container that currently has focus (on any monitor).
*/
focusedContainer: Container;

/**
* Tiling direction of the focused container.
*/
tilingDirection: TilingDirection;

/**
* Active binding modes;
*/
bindingModes: BindingModeConfig[];

/**
* Focus a workspace by name.
*/
focusWorkspace(name: string): void;
}

export async function createGlazeWmProvider(
_: GlazewmProviderConfig,
owner: Owner,
) {
): Promise<GlazeWmProvider> {
const monitors = await getMonitors();
const client = new GwmClient();
const client = new WmClient();

const [glazewmVariables, setGlazewmVariables] = createStore({
workspacesOnMonitor: [] as Workspace[],
// TODO
bindingMode: '',
});

client.onConnect(e => console.log('onOpen', e));
client.onMessage(e => console.log('onMessage', e));
client.onDisconnect(e => console.log('onClose', e));
client.onError(e => console.log('onError', e));

// Get initial workspaces.
await refetch();
const [glazeWmVariables, setGlazeWmVariables] = createStore(
await getInitialState(),
);

await client.subscribeMany(
[
GwmEventType.WORKSPACE_ACTIVATED,
GwmEventType.WORKSPACE_DEACTIVATED,
GwmEventType.FOCUS_CHANGED,
WmEventType.BINDING_MODES_CHANGED,
WmEventType.FOCUS_CHANGED,
WmEventType.FOCUSED_CONTAINER_MOVED,
WmEventType.TILING_DIRECTION_CHANGED,
WmEventType.WORKSPACE_ACTIVATED,
WmEventType.WORKSPACE_DEACTIVATED,
WmEventType.WORKSPACE_UPDATED,
],
refetch,
onEvent,
);

runWithOwner(owner, () => {
createEffect(on(() => monitors.currentMonitor, refetch));
createEffect(
on(
() => monitors.currentMonitor,
async () => setGlazeWmVariables({ ...(await getMonitorState()) }),
),
);
});

async function refetch() {
async function onEvent(
e:
| BindingModesChangedEvent
| FocusChangedEvent
| FocusedContainerMovedEvent
| TilingDirectionChangedEvent
| WorkspaceActivatedEvent
| WorkspaceDeactivatedEvent
| WorkspaceUpdatedEvent,
) {
switch (e.eventType) {
case WmEventType.BINDING_MODES_CHANGED: {
setGlazeWmVariables({ bindingModes: e.newBindingModes });
break;
}
case WmEventType.FOCUS_CHANGED:
case WmEventType.FOCUSED_CONTAINER_MOVED: {
setGlazeWmVariables({ focusedContainer: e.focusedContainer });
setGlazeWmVariables({ ...(await getMonitorState()) });
break;
}
case WmEventType.TILING_DIRECTION_CHANGED: {
setGlazeWmVariables({ tilingDirection: e.newTilingDirection });
break;
}
case WmEventType.WORKSPACE_ACTIVATED:
case WmEventType.WORKSPACE_DEACTIVATED:
case WmEventType.WORKSPACE_UPDATED: {
setGlazeWmVariables({ ...(await getMonitorState()) });
break;
}
}
}

async function getInitialState() {
const { focused } = await client.queryFocused();
const { bindingModes } = await client.queryBindingModes();
const { tilingDirection } = await client.queryTilingDirection();

return {
...(await getMonitorState()),
focusedContainer: focused,
tilingDirection,
bindingModes,
};
}

async function getMonitorState() {
const currentPosition = {
x: monitors.currentMonitor!.x,
y: monitors.currentMonitor!.y,
};

// Get GlazeWM monitor that corresponds to the bar's monitor.
const glazewmMonitor = (await client.getMonitors()).reduce((a, b) =>
const { monitors: glazeWmMonitors } = await client.queryMonitors();

// Get GlazeWM monitor that corresponds to the Zebar window's monitor.
const currentGlazeWmMonitor = glazeWmMonitors.reduce((a, b) =>
getCoordinateDistance(currentPosition, a) <
getCoordinateDistance(currentPosition, b)
? a
: b,
);

setGlazewmVariables({
workspacesOnMonitor: glazewmMonitor.children.sort(
(a, b) => Number(a.name) - Number(b.name),
),
});
const focusedGlazeWmMonitor = glazeWmMonitors.find(
monitor => monitor.hasFocus,
);

const allGlazeWmWorkspaces = glazeWmMonitors.flatMap(
monitor => monitor.children,
);

const focusedGlazeWmWorkspace = focusedGlazeWmMonitor?.children.find(
workspace => workspace.hasFocus,
);

const displayedGlazeWmWorkspace = currentGlazeWmMonitor.children.find(
workspace => workspace.isDisplayed,
);

return {
displayedWorkspace: displayedGlazeWmWorkspace!,
focusedWorkspace: focusedGlazeWmWorkspace!,
currentWorkspaces: currentGlazeWmMonitor.children,
allWorkspaces: allGlazeWmWorkspaces,
focusedMonitor: focusedGlazeWmMonitor!,
currentMonitor: currentGlazeWmMonitor,
allMonitors: glazeWmMonitors,
};
}

function focusWorkspace(name: string) {
client.runCommand(`focus --workspace ${name}`);
}

return {
get workspacesOnMonitor() {
return glazewmVariables.workspacesOnMonitor;
get displayedWorkspace() {
return glazeWmVariables.displayedWorkspace;
},
get focusedWorkspace() {
return glazeWmVariables.focusedWorkspace;
},
get currentWorkspaces() {
return glazeWmVariables.currentWorkspaces;
},
get allWorkspaces() {
return glazeWmVariables.allWorkspaces;
},
get allMonitors() {
return glazeWmVariables.allMonitors;
},
get focusedMonitor() {
return glazeWmVariables.focusedMonitor;
},
get currentMonitor() {
return glazeWmVariables.currentMonitor;
},
get focusedContainer() {
return glazeWmVariables.focusedContainer;
},
get tilingDirection() {
return glazeWmVariables.tilingDirection;
},
get bindingModes() {
return glazeWmVariables.bindingModes;
},
focusWorkspace,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export async function createKomorebiProvider(
y: monitors.currentMonitor!.y,
};

// Get Komorebi monitor that corresponds to the window's monitor.
// Get Komorebi monitor that corresponds to the Zebar window's monitor.
const currentKomorebiMonitor = state.allMonitors.reduce((a, b) =>
getCoordinateDistance(currentPosition, {
x: a.workAreaSize.left,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,5 +162,6 @@ export function renderTemplateNodes(
);
}

return visitAll(nodes);
// Render and trim any leading/trailing whitespace.
return visitAll(nodes).trim();
}
Loading

0 comments on commit 15aab5b

Please sign in to comment.