perf(ags/runner): better code for runner, show results from multiple plugins, use interface to make plugins and more perfomance implementations

This commit is contained in:
retrozinndev
2025-03-12 14:11:42 -03:00
parent 0ce13b47be
commit f0cec3ff84
7 changed files with 244 additions and 220 deletions
+15 -9
View File
@@ -2,14 +2,20 @@ import AstalHyprland from "gi://AstalHyprland";
import { getAstalApps } from "../apps";
import { ResultWidget, ResultWidgetProps } from "../../widget/runner/ResultWidget";
import AstalApps from "gi://AstalApps";
import { Runner } from "../../window/Runner";
export function handleApplications(search: string): (Array<ResultWidget>|null) {
return getAstalApps().fuzzy_query(search).map((app: AstalApps.Application) =>
new ResultWidget({
title: app.get_name(),
description: app.get_description(),
icon: app.iconName,
onClick: () => AstalHyprland.get_default().dispatch("exec", app.get_executable())
} as ResultWidgetProps)
) || null;
export class PluginApps implements Runner.Plugin {
// Do not provide prefix, so it's always ran.
public readonly name = "Apps";
public handle(text: string) {
return getAstalApps().fuzzy_query(text).map((app: AstalApps.Application) =>
new ResultWidget({
title: app.get_name(),
description: app.get_description(),
icon: app.iconName,
onClick: () => AstalHyprland.get_default().dispatch("exec", app.get_executable())
} as ResultWidgetProps)
) || null;
}
}
+12 -8
View File
@@ -1,14 +1,18 @@
import { ResultWidget, ResultWidgetProps } from "../../widget/runner/ResultWidget";
import AstalHyprland from "gi://AstalHyprland";
import { GLib } from "astal";
import { Runner } from "../../window/Runner";
export function handleShell(command: string): ResultWidget {
const userShell = GLib.getenv("SHELL") || "/usr/bin/env bash";
export class PluginShell implements Runner.Plugin {
public readonly prefix = '!';
#shell = GLib.getenv("SHELL") || "/usr/bin/env sh";
return new ResultWidget({
onClick: () => AstalHyprland.get_default().dispatch("exec", `${userShell} -c "${command}"`),
title: `Run: \`${command}\``,
description: userShell,
icon: "utilities-terminal-symbolic"
} as ResultWidgetProps);
public handle(command: string): ResultWidget {
return new ResultWidget({
onClick: () => AstalHyprland.get_default().dispatch("exec", `${this.#shell} -c "${command}"`),
title: `Run: \`${command}\``,
description: this.#shell,
icon: "utilities-terminal-symbolic"
} as ResultWidgetProps)
}
}
+32 -34
View File
@@ -1,42 +1,40 @@
import AstalHyprland from "gi://AstalHyprland";
import { ResultWidget, ResultWidgetProps } from "../../widget/runner/ResultWidget";
import { Runner } from "../../window/Runner";
export enum SearchEngine {
GOOGLE,
DUCKDUCKGO,
YAHOO
}
const searchEngines = {
duckduckgo: "https://duckduckgo.com/?q=",
google: "https://google.com/search?q=",
yahoo: "https://search.yahoo.com/search?p="
};
export const SearchEngineMap: Map<SearchEngine, string> = new Map([
[ SearchEngine.DUCKDUCKGO, "https://duckduckgo.com/?q=" ],
[ SearchEngine.GOOGLE, "https://google.com/search?q=" ],
[ SearchEngine.YAHOO, "https://search.yahoo.com/search?p=" ]
]);
let engine: string = searchEngines.google;
let searchEngine: SearchEngine = SearchEngine.GOOGLE;
export function handleWebSearch(search: string): ResultWidget {
let engineString: string;
switch(searchEngine as SearchEngine) {
case SearchEngine.GOOGLE:
engineString = "Google";
case SearchEngine.YAHOO:
engineString = "Yahoo";
case SearchEngine.DUCKDUCKGO:
engineString = "DuckDuckGo";
default: engineString = "Web";
export class PluginWebSearch implements Runner.Plugin {
#engineString: string;
public readonly prefix = '?';
public readonly name = "Web Search";
constructor() {
switch(engine) {
case searchEngines.google:
this.#engineString = "Google";
case searchEngines.yahoo:
this.#engineString = "Yahoo";
case searchEngines.duckduckgo:
this.#engineString = "DuckDuckGo";
default: this.#engineString = "Web";
}
}
public handle(search: string): ResultWidget {
return new ResultWidget({
icon: "system-search-symbolic",
title: search || "",
description: `Search with ${this.#engineString}`,
onClick: () => AstalHyprland.get_default().dispatch(
"exec",
`xdg-open \"${engine + search}\"`
)
} as ResultWidgetProps);
}
return new ResultWidget({
icon: "system-search-symbolic",
title: search || "",
description: `Search with ${engineString}`,
onClick: () => AstalHyprland.get_default().dispatch(
"exec",
`xdg-open "${SearchEngineMap.get(searchEngine)! + search.replaceAll(" ", "%20")}"`
)
} as ResultWidgetProps);
}