Skip to content

Commit

Permalink
feat: add tiling direction query; add response types
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-berger committed Jul 26, 2024
1 parent 44c66ae commit 1e14159
Show file tree
Hide file tree
Showing 15 changed files with 106 additions and 39 deletions.
16 changes: 14 additions & 2 deletions src/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { WmClient } from './client';
import { TilingDirection } from './types';

describe.sequential('[CLIENT]', async () => {
const client = new WmClient();
Expand All @@ -22,8 +23,8 @@ describe.sequential('[CLIENT]', async () => {
expect(workspaces.length).toBeGreaterThan(0);
});

it.concurrent('focused container', async () => {
const focused = await client.queryFocused();
it.concurrent('focused', async () => {
const { focused } = await client.queryFocused();
expect(focused).toBeDefined();
});

Expand All @@ -36,6 +37,17 @@ describe.sequential('[CLIENT]', async () => {
const { version } = await client.queryAppMetadata();
expect(typeof version).toBe('string');
});

it.concurrent('tiling direction', async () => {
const { directionContainer, tilingDirection } =
await client.queryTilingDirection();

expect(directionContainer).toBeDefined();
expect(
tilingDirection === TilingDirection.HORIZONTAL ||
tilingDirection === TilingDirection.VERTICAL,
).toBeTruthy();
});
});

describe('(command)', () => {
Expand Down
72 changes: 40 additions & 32 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,21 @@ import {
} from 'ws';

import {
type BindingModeConfig,
WmEventType,
type WmEventData,
type Monitor,
type ServerMessage,
type Workspace,
type Window,
type AppMetadata,
type AppMetadataResponse,
type TilingDirectionResponse,
type BindingModesResponse,
type FocusedResponse,
type WindowsResponse,
type MonitorsResponse,
type WorkspacesResponse,
type RunCommandResponse,
type SubscribeResponse,
} from './types';

export interface WmClientOptions {
Expand Down Expand Up @@ -58,55 +65,57 @@ export class WmClient {
/**
* Gets all monitors. {@link Monitor}
*/
async queryMonitors(): Promise<{ monitors: Monitor[] }> {
return this._sendAndWaitReply<{ monitors: Monitor[] }>(
'query monitors',
);
async queryMonitors(): Promise<MonitorsResponse> {
return this._sendAndWaitReply<MonitorsResponse>('query monitors');
}

/**
* Gets all active workspaces. {@link Workspace}
*/
async queryWorkspaces(): Promise<{ workspaces: Workspace[] }> {
return this._sendAndWaitReply<{ workspaces: Workspace[] }>(
'query workspaces',
);
async queryWorkspaces(): Promise<WorkspacesResponse> {
return this._sendAndWaitReply<WorkspacesResponse>('query workspaces');
}

/**
* Gets all managed windows. {@link Window}
*/
async queryWindows(): Promise<{ windows: Window[] }> {
return this._sendAndWaitReply<{ windows: Window[] }>('query windows');
async queryWindows(): Promise<WindowsResponse> {
return this._sendAndWaitReply<WindowsResponse>('query windows');
}

/**
* Gets the currently focused container. This can either be a
* {@link Window} or a {@link Workspace} without any descendant windows.
*/
async queryFocused(): Promise<{ focused: Window | Workspace }> {
return this._sendAndWaitReply<{ focused: Window | Workspace }>(
'query focused',
);
async queryFocused(): Promise<FocusedResponse> {
return this._sendAndWaitReply<FocusedResponse>('query focused');
}

/**
* Gets the active binding modes. {@link BindingModeConfig}
* Gets the active binding modes.
*/
async queryBindingModes(): Promise<{
bindingModes: BindingModeConfig[];
}> {
return this._sendAndWaitReply<{ bindingModes: BindingModeConfig[] }>(
async queryBindingModes(): Promise<BindingModesResponse> {
return this._sendAndWaitReply<BindingModesResponse>(
'query binding-modes',
);
}

/**
* Gets metadata about the running GlazeWM application.
* {@link AppMetadata}
*/
async queryAppMetadata(): Promise<AppMetadata> {
return this._sendAndWaitReply<AppMetadata>('query app-metadata');
async queryAppMetadata(): Promise<AppMetadataResponse> {
return this._sendAndWaitReply<AppMetadataResponse>(
'query app-metadata',
);
}

/**
* Gets the tiling direction of the focused container.
*/
async queryTilingDirection(): Promise<TilingDirectionResponse> {
return this._sendAndWaitReply<TilingDirectionResponse>(
'query tiling-direction',
);
}

/**
Expand All @@ -120,8 +129,8 @@ export class WmClient {
async runCommand(
command: string,
subjectContainerId?: string,
): Promise<void> {
await this._sendAndWaitReply<{ subjectContainerId: string }>(
): Promise<RunCommandResponse> {
return this._sendAndWaitReply<RunCommandResponse>(
subjectContainerId
? `command --id ${subjectContainerId} ${command}`
: `command ${command}`,
Expand Down Expand Up @@ -185,9 +194,10 @@ export class WmClient {
events: T,
callback: SubscribeCallback<T[number]>,
): Promise<UnlistenFn> {
const { subscriptionId } = await this._sendAndWaitReply<{
subscriptionId: string;
}>(`sub --events ${events.join(' ')}`);
const { subscriptionId } =
await this._sendAndWaitReply<SubscribeResponse>(
`sub --events ${events.join(' ')}`,
);

const unlisten = this.onMessage(e => {
const serverMessage: ServerMessage<WmEventData> = JSON.parse(
Expand All @@ -206,9 +216,7 @@ export class WmClient {
return async () => {
unlisten();

await this._sendAndWaitReply<{ subscriptionId: string }>(
`unsub --id ${subscriptionId}`,
);
await this._sendAndWaitReply<void>(`unsub --id ${subscriptionId}`);
};
}

Expand Down
3 changes: 0 additions & 3 deletions src/types/common/app-metadata.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/types/common/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './app-metadata';
export * from './display-state';
export * from './length-unit';
export * from './rect-delta';
Expand Down
3 changes: 2 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './common';
export * from './config';
export * from './containers';
export * from './events';
export * from './responses';
export * from './server-message';
export * from './common';
export * from './wm-events';
3 changes: 3 additions & 0 deletions src/types/responses/app-metadata-response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface AppMetadataResponse {
version: string;
}
5 changes: 5 additions & 0 deletions src/types/responses/binding-modes-response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { BindingModeConfig } from '../config';

export interface BindingModesResponse {
bindingModes: BindingModeConfig[];
}
5 changes: 5 additions & 0 deletions src/types/responses/focused-response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { Window, Workspace } from '../containers';

export interface FocusedResponse {
focused: Window | Workspace;
}
9 changes: 9 additions & 0 deletions src/types/responses/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export * from './app-metadata-response';
export * from './binding-modes-response';
export * from './focused-response';
export * from './monitors-response';
export * from './run-command-response';
export * from './subscribe-response';
export * from './tiling-direction-response';
export * from './windows-response';
export * from './workspaces-response';
5 changes: 5 additions & 0 deletions src/types/responses/monitors-response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { Monitor } from '../containers';

export interface MonitorsResponse {
monitors: Monitor[];
}
3 changes: 3 additions & 0 deletions src/types/responses/run-command-response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface RunCommandResponse {
subjectContainerId: string;
}
3 changes: 3 additions & 0 deletions src/types/responses/subscribe-response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface SubscribeResponse {
subscriptionId: string;
}
7 changes: 7 additions & 0 deletions src/types/responses/tiling-direction-response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { TilingDirection } from '../common';
import type { SplitContainer, Workspace } from '../containers';

export interface TilingDirectionResponse {
tilingDirection: TilingDirection;
directionContainer: Workspace | SplitContainer;
}
5 changes: 5 additions & 0 deletions src/types/responses/windows-response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { Window } from '../containers';

export interface WindowsResponse {
windows: Window[];
}
5 changes: 5 additions & 0 deletions src/types/responses/workspaces-response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { Workspace } from '../containers';

export interface WorkspacesResponse {
workspaces: Workspace[];
}

0 comments on commit 1e14159

Please sign in to comment.