Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve responsiveness of app launches #1311

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import type { SearchResultItemAction } from "@common/Core";
import { describe, expect, it, vi } from "vitest";
import type { BrowserWindowRegistry } from "../../BrowserWindowRegistry";
import type { CommandlineUtility } from "../Contract";
import { CommandlineActionHandler } from "./CommandlineActionHandler";

const browserRegistryMock = {
getById: () => {
return { show: vi.fn(), hide: vi.fn() };
},
} as unknown as BrowserWindowRegistry;

describe(CommandlineActionHandler, () => {
it("should execute commandline commands", async () => {
const executeCommandMock = vi.fn();
const commandlineUtility = <CommandlineUtility>{ executeCommand: (command) => executeCommandMock(command) };

const actionHandler = new CommandlineActionHandler(commandlineUtility);
const actionHandler = new CommandlineActionHandler(commandlineUtility, browserRegistryMock);
await actionHandler.invokeAction(<SearchResultItemAction>{ argument: "command" });

expect(actionHandler.id).toEqual("Commandline");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { SearchResultItemAction } from "@common/Core";
import type { ActionHandler } from "@Core/ActionHandler";
import type { BrowserWindowRegistry } from "../../BrowserWindowRegistry";
import type { CommandlineUtility } from "../Contract";

/**
Expand All @@ -8,14 +9,24 @@ import type { CommandlineUtility } from "../Contract";
export class CommandlineActionHandler implements ActionHandler {
public readonly id = "Commandline";

public constructor(private readonly commandlineUtility: CommandlineUtility) {}
public constructor(
private readonly commandlineUtility: CommandlineUtility,
private readonly browserRegistry: BrowserWindowRegistry,
) {}

/**
* Executes the given CLI command and waits until it finishes.
* The output of the command is ignored.
* Expects the given action's argument to be a valid CLI command, e.g.: `"ls -la"`.
*/
public async invokeAction(action: SearchResultItemAction): Promise<void> {
await this.commandlineUtility.executeCommand(action.argument);
const searchWindow = this.browserRegistry.getById("search");

searchWindow?.hide();
try {
await this.commandlineUtility.executeCommand(action.argument);
} catch (err) {
searchWindow?.show();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { ActionHandlerRegistry } from "@Core/ActionHandler";
import type { UeliModuleRegistry } from "@Core/ModuleRegistry";
import { describe, expect, it, vi } from "vitest";
import type { BrowserWindowRegistry } from "../BrowserWindowRegistry";
import { CommandlineActionHandler } from "./ActionHandler";
import { CommandlineUtilityModule } from "./CommandlineUtilityModule";
import { NodeJsCommandlineUtility } from "./NodeJsCommandlineUtility";
Expand All @@ -13,18 +14,25 @@ describe(CommandlineUtilityModule, () => {
getById: vi.fn(),
};

const browserRegistryMock = {
getAll: vi.fn(),
getById: vi.fn(),
register: vi.fn(),
} as unknown as BrowserWindowRegistry;

const moduleRegistry = <UeliModuleRegistry>{
get: vi.fn().mockReturnValue(actionHandlerRegistryMock),
get: vi.fn().mockReturnValueOnce(browserRegistryMock).mockReturnValueOnce(actionHandlerRegistryMock),
register: vi.fn(),
};

CommandlineUtilityModule.bootstrap(moduleRegistry);

expect(moduleRegistry.register).toHaveBeenCalledWith("CommandlineUtility", new NodeJsCommandlineUtility());

expect(moduleRegistry.get).toBeCalledWith("ActionHandlerRegistry");
expect(moduleRegistry.get).toHaveBeenNthCalledWith(1, "BrowserWindowRegistry");
expect(moduleRegistry.get).toHaveBeenNthCalledWith(2, "ActionHandlerRegistry");
expect(actionHandlerRegistryMock.register).toHaveBeenCalledWith(
new CommandlineActionHandler(new NodeJsCommandlineUtility()),
new CommandlineActionHandler(new NodeJsCommandlineUtility(), browserRegistryMock),
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import { NodeJsCommandlineUtility } from "./NodeJsCommandlineUtility";

export class CommandlineUtilityModule {
public static bootstrap(moduleRegistry: UeliModuleRegistry) {
const commandlineUtility = new NodeJsCommandlineUtility();
const browserRegistry = moduleRegistry.get("BrowserWindowRegistry");

const commandlineUtility = new NodeJsCommandlineUtility();
moduleRegistry.register("CommandlineUtility", commandlineUtility);
moduleRegistry.get("ActionHandlerRegistry").register(new CommandlineActionHandler(commandlineUtility));

moduleRegistry
.get("ActionHandlerRegistry")
.register(new CommandlineActionHandler(commandlineUtility, browserRegistry));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ export class ApplicationSearchModule implements ExtensionModule {
};

const actionHandlers: Record<OperatingSystem, () => ActionHandler[]> = {
Linux: () => [new LaunchDesktopFileActionHandler(moduleRegistry.get("CommandlineUtility"))],
Linux: () => [
new LaunchDesktopFileActionHandler(
moduleRegistry.get("CommandlineUtility"),
moduleRegistry.get("BrowserWindowRegistry"),
),
],
macOS: () => [],
Windows: () => [new OpenAsAdministrator(moduleRegistry.get("PowershellUtility"))],
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import type { CommandlineUtility } from "@Core/CommandlineUtility";
import type { SearchResultItemAction } from "@common/Core";
import { describe, expect, it, vi } from "vitest";
import type { BrowserWindowRegistry } from "../../../Core";
import { LaunchDesktopFileActionHandler } from "./LaunchDesktopFileActionHandler";

const browserRegistryMock = {
getById: () => {
return { show: vi.fn(), hide: vi.fn() };
},
} as unknown as BrowserWindowRegistry;

describe(LaunchDesktopFileActionHandler, () => {
it("should call `gio launch` when executing search result item with the argument file name", async () => {
const executeCommandMock = vi.fn().mockResolvedValue("");
const commandlineUtility = <CommandlineUtility>{ executeCommand: (path) => executeCommandMock(path) };

const actionHandler = new LaunchDesktopFileActionHandler(commandlineUtility);
const actionHandler = new LaunchDesktopFileActionHandler(commandlineUtility, browserRegistryMock);

await actionHandler.invokeAction(<SearchResultItemAction>{
argument: "/usr/share/applications/firefox.desktop",
Expand All @@ -25,7 +32,7 @@ describe(LaunchDesktopFileActionHandler, () => {

const commandlineUtility = <CommandlineUtility>{ executeCommand: (path) => executeCommandMock(path) };

const actionHandler = new LaunchDesktopFileActionHandler(commandlineUtility);
const actionHandler = new LaunchDesktopFileActionHandler(commandlineUtility, browserRegistryMock);

await expect(
actionHandler.invokeAction(<SearchResultItemAction>{ argument: "/usr/share/applications/firefox.desktop" }),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
import type { ActionHandler } from "@Core/ActionHandler";
import type { CommandlineUtility } from "@Core/CommandlineUtility";
import type { SearchResultItemAction } from "@common/Core";
import type { BrowserWindowRegistry } from "../../../Core";

/**
* Action handler for launching a `.desktop` file.
*/
export class LaunchDesktopFileActionHandler implements ActionHandler {
public readonly id = "LaunchDesktopFile";

public constructor(private readonly commandlineUtility: CommandlineUtility) {}
public constructor(
private readonly commandlineUtility: CommandlineUtility,
private readonly browserRegistry: BrowserWindowRegistry,
) {}

/**
* Launches the given `.desktop` file with the desktop environment's respective command.
* Expects the given action's argument to be a valid desktop file
* Throws an error if the file could not be launched or desktop environment isn't supported.
*/
public async invokeAction(action: SearchResultItemAction): Promise<void> {
await this.commandlineUtility.executeCommand(`gio launch ${action.argument}`, { ignoreStdErr: true });
const searchWindow = this.browserRegistry.getById("search");

searchWindow?.hide();
try {
await this.commandlineUtility.executeCommand(`gio launch ${action.argument}`, { ignoreStdErr: true });
} catch (err) {
searchWindow?.show();
throw err;
}
}
}
Loading