Skip to content

Commit

Permalink
feat(SystemSettings): Added Windows support
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverschwendener committed Feb 17, 2024
1 parent 1d08a3a commit 85d5b04
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 13 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion src/main/Extensions/SystemSettings/MacOsSystemSetting.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { SearchResultItemActionUtility, type SearchResultItem } from "@common/Core";
import type { SystemSetting } from "./SystemSetting";

export class MacOsSystemSetting {
export class MacOsSystemSetting implements SystemSetting {
public constructor(
private readonly name: string,
private readonly filePath: string,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { AssetPathResolver } from "@Core/AssetPathResolver";
import { MacOsSystemSetting } from "./MacOsSystemSetting";
import type { SystemSettingRepository } from "./SystemSettingRepository";

export class MacOsSystemSettingRepository {
export class MacOsSystemSettingRepository implements SystemSettingRepository {
public constructor(private readonly assetPathResolver: AssetPathResolver) {}

public getAll(): MacOsSystemSetting[] {
Expand Down
5 changes: 5 additions & 0 deletions src/main/Extensions/SystemSettings/SystemSetting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { SearchResultItem } from "@common/Core";

export interface SystemSetting {
toSearchResultItem(): SearchResultItem;
}
5 changes: 5 additions & 0 deletions src/main/Extensions/SystemSettings/SystemSettingRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { SystemSetting } from "./SystemSetting";

export interface SystemSettingRepository {
getAll(): SystemSetting[];
}
18 changes: 12 additions & 6 deletions src/main/Extensions/SystemSettings/SystemSettingsExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Extension } from "@Core/Extension";
import type { OperatingSystem, SearchResultItem } from "@common/Core";
import { Translations } from "@common/Core/Extension";
import type { Image } from "@common/Core/Image";
import type { MacOsSystemSettingRepository } from "./MacOsSystemSettingRepository";
import { SystemSettingRepository } from "./SystemSettingRepository";

export class SystemSettingsExtension implements Extension {
public readonly id = "SystemSettings";
Expand All @@ -20,26 +20,32 @@ export class SystemSettingsExtension implements Extension {
};

public constructor(
private readonly currentOperatingSystem: OperatingSystem,
private readonly macOsSystemSettingRepository: MacOsSystemSettingRepository,
private readonly operatingSystem: OperatingSystem,
private readonly systemSettingRepository: SystemSettingRepository,
private readonly assetPathResolver: AssetPathResolver,
) {}

public async getSearchResultItems(): Promise<SearchResultItem[]> {
return this.macOsSystemSettingRepository.getAll().map((s) => s.toSearchResultItem());
return this.systemSettingRepository.getAll().map((s) => s.toSearchResultItem());
}

public isSupported(): boolean {
return this.currentOperatingSystem === "macOS";
return (<OperatingSystem[]>["Windows", "macOS"]).includes(this.operatingSystem);
}

public getSettingDefaultValue<T>(): T {
return undefined;
}

public getImage(): Image {
const filenames: Record<OperatingSystem, string> = {
Linux: null, // not supported,
macOS: "macos-system-settings.png",
Windows: "windows-11-settings.png",
};

return {
url: `file://${this.assetPathResolver.getExtensionAssetPath(this.id, "macos-system-settings.png")}`,
url: `file://${this.assetPathResolver.getExtensionAssetPath(this.id, filenames[this.operatingSystem])}`,
};
}

Expand Down
18 changes: 13 additions & 5 deletions src/main/Extensions/SystemSettings/SystemSettingsModule.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
import type { Dependencies } from "@Core/Dependencies";
import type { DependencyRegistry } from "@Core/DependencyRegistry";
import type { OperatingSystem } from "@common/Core";
import type { ExtensionBootstrapResult } from "../ExtensionBootstrapResult";
import type { ExtensionModule } from "../ExtensionModule";
import { MacOsSystemSettingRepository } from "./MacOsSystemSettingRepository";
import type { SystemSettingRepository } from "./SystemSettingRepository";
import { SystemSettingsExtension } from "./SystemSettingsExtension";
import { WindowsSystemSettingsRepository } from "./WindowsSystemRepository";
import { WindowsSystemSettingActionHandler } from "./WindowsSystemSettingActionHandler";

export class SystemSettingsModule implements ExtensionModule {
public bootstrap(dependencyRegistry: DependencyRegistry<Dependencies>): ExtensionBootstrapResult {
const operatingSystem = dependencyRegistry.get("OperatingSystem");
const assetPathResolver = dependencyRegistry.get("AssetPathResolver");
const systemSettingRepositories: Record<OperatingSystem, SystemSettingRepository> = {
Linux: null, // not supported
macOS: new MacOsSystemSettingRepository(dependencyRegistry.get("AssetPathResolver")),
Windows: new WindowsSystemSettingsRepository(dependencyRegistry.get("AssetPathResolver")),
};

return {
extension: new SystemSettingsExtension(
operatingSystem,
new MacOsSystemSettingRepository(assetPathResolver),
assetPathResolver,
dependencyRegistry.get("OperatingSystem"),
systemSettingRepositories[dependencyRegistry.get("OperatingSystem")],
dependencyRegistry.get("AssetPathResolver"),
),
actionHandlers: [new WindowsSystemSettingActionHandler(dependencyRegistry.get("PowershellUtility"))],
};
}
}
16 changes: 16 additions & 0 deletions src/main/Extensions/SystemSettings/WindowsSystemRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { AssetPathResolver } from "@Core/AssetPathResolver";
import type { SystemSetting } from "./SystemSetting";
import type { SystemSettingRepository } from "./SystemSettingRepository";
import { WindowsSystemSetting } from "./WindowsSystemSetting";

export class WindowsSystemSettingsRepository implements SystemSettingRepository {
public constructor(private readonly assetPathResolver: AssetPathResolver) {}

public getAll(): SystemSetting[] {
return [new WindowsSystemSetting("System Settings", "ms-settings:", this.getGenericImageFilePath())];
}

private getGenericImageFilePath(): string {
return this.assetPathResolver.getExtensionAssetPath("SystemSettings", "windows-11-settings.png");
}
}
31 changes: 31 additions & 0 deletions src/main/Extensions/SystemSettings/WindowsSystemSetting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { type SearchResultItem } from "@common/Core";
import type { SystemSetting } from "./SystemSetting";

export class WindowsSystemSetting implements SystemSetting {
public constructor(
private readonly name: string,
private readonly settingsUrl: string,
private readonly imageFilePath: string,
) {}

public toSearchResultItem(): SearchResultItem {
return {
defaultAction: {
argument: this.settingsUrl,
description: "Open System Settings",
handlerId: "WindowsSystemSetting",
hideWindowAfterInvocation: true,
},
description: "System Setting",
id: this.getId(),
image: {
url: `file://${this.imageFilePath}`,
},
name: this.name,
};
}

private getId(): string {
return `WindowsSystemSetting:${this.name}`;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { ActionHandler } from "@Core/ActionHandler";
import type { PowershellUtility } from "@Core/PowershellUtility";
import type { SearchResultItemAction } from "@common/Core";

export class WindowsSystemSettingActionHandler implements ActionHandler {
public readonly id = "WindowsSystemSetting";

public constructor(private readonly powershellUtility: PowershellUtility) {}

public async invokeAction({ argument }: SearchResultItemAction): Promise<void> {
await this.powershellUtility.executeCommand(`Start-Process "${argument}"`);
}
}

0 comments on commit 85d5b04

Please sign in to comment.