Skip to content

Commit

Permalink
v2 (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMarble authored Jun 9, 2024
1 parent a99d196 commit d49fb26
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 10 deletions.
11 changes: 10 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
{
"eslint.experimental.useFlatConfig": true
"eslint.experimental.useFlatConfig": true,
"files.exclude": {
"**/.git": true,
"**/.svn": true,
"**/.hg": true,
"**/CVS": true,
"**/.DS_Store": true,
"**/Thumbs.db": true,
"**/node_modules": true
}
}
3 changes: 2 additions & 1 deletion metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
"donations": {
"github": "mrmarble"
},
"shell-version": ["45", "46"]
"shell-version": ["45", "46"],
"settings-schema": "org.gnome.shell.extensions.vscode-search-provider"
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
"format": "prettier --check .",
"format:fix": "prettier --write .",
"clean": "rm -rf dist && rm -f ${npm_package_name}.zip",
"pack": "cp metadata.json dist && cd dist && zip ../${npm_package_name}.zip -9r .",
"pack": "npm run clean && npm run build && cp metadata.json dist && cp -r schemas dist && cd dist && zip ../${npm_package_name}.zip -9r .",
"install-extension": "touch $XDG_CONFIG_HOME/local/share/gnome-shell/extensions/${npm_package_name}@${npm_package_config_domain} && rm -rf $XDG_CONFIG_HOME/local/share/gnome-shell/extensions/${npm_package_name}@${npm_package_config_domain} && mv dist $XDG_CONFIG_HOME/local/share/gnome-shell/extensions/${npm_package_name}@${npm_package_config_domain}",
"setup": "npm run clean && npm run build && npm run pack && npm run install-extension",
"setup": "npm run pack && glib-compile-schemas ./dist/schemas && npm run install-extension",
"debug": "dbus-run-session -- gnome-shell --nested --wayland",
"prepare": "simple-git-hooks"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<schemalist>
<schema id="org.gnome.shell.extensions.vscode-search-provider" path="/org/gnome/shell/extensions/vscode-search-provider/">
<key name="suffix" type="b">
<default>true</default>
<summary>Boolean, whether to show a suffix next to the workspace name. (Remote, Codespaces...)</summary>
</key>
</schema>
</schemalist>
5 changes: 5 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import Gio from "gi://Gio";
import * as Main from "resource:///org/gnome/shell/ui/main.js";
import { Extension } from "resource:///org/gnome/shell/extensions/extension.js";
import { AppSearchProvider } from "resource:///org/gnome/shell/ui/appDisplay.js";
import VSCodeSearchProvider from "./provider.js";

export default class VSCodeSearchProviderExtension extends Extension {
provider: AppSearchProvider | null = null;
_settings: Gio.Settings | null = null;

enable() {
this._settings = this.getSettings();
this.provider = new VSCodeSearchProvider(this);
Main.overview.searchController.addProvider(this.provider);
}

disable() {
this._settings = null;
if (this.provider) {
Main.overview.searchController.removeProvider(this.provider);
this.provider = null;
Expand Down
49 changes: 49 additions & 0 deletions src/prefs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import Gio from "gi://Gio";
import Adw from "gi://Adw";

import {
ExtensionPreferences,
gettext as _,
} from "resource:///org/gnome/Shell/Extensions/js/extensions/prefs.js";

type Window = Adw.PreferencesWindow & {
_settings: Gio.Settings | null;
};

export default class ExamplePreferences extends ExtensionPreferences {
_settings: Gio.Settings | null = null;

fillPreferencesWindow(window: Window) {
// Create a preferences page, with a single group
const page = new Adw.PreferencesPage({
title: _("General"),
icon_name: "dialog-information-symbolic",
});
window.add(page);

const group = new Adw.PreferencesGroup({
title: _("Appearance"),
description: _("Configure the appearance of the extension"),
});

page.add(group);

// Create a new preferences row
const row = new Adw.SwitchRow({
title: _("Show Suffix"),
subtitle: _(
"Whether to show a suffix next to the workspace name. (Remote, Codespaces...)",
),
});
group.add(row);

// Create a settings object and bind the row to the `show-indicator` key
window._settings = this.getSettings();
window._settings.bind(
"suffix",
row,
"active",
Gio.SettingsBindFlags.DEFAULT,
);
}
}
39 changes: 33 additions & 6 deletions src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ interface VSStorage {
};
}

export default class VSCodeSearchProvider implements AppSearchProvider {
export default class VSCodeSearchProvider<
T extends Extension & { _settings: Gio.Settings | null },
> implements AppSearchProvider
{
workspaces: Record<string, { name: string; path: string }> = {};
extension: Extension;
extension: T;
app: Shell.App | null = null;
appInfo: Gio.DesktopAppInfo | undefined;

constructor(extension: Extension) {
constructor(extension: T) {
this.extension = extension;
this._findApp();
this._loadWorkspaces();
Expand Down Expand Up @@ -100,8 +103,11 @@ export default class VSCodeSearchProvider implements AppSearchProvider {

activateResult(result: string): void {
if (this.app) {
const path = this.workspaces[result].path;
if (path.startsWith("vscode-remote://")) {
const path = decodeURIComponent(this.workspaces[result].path);
if (
path.startsWith("vscode-remote://") ||
path.startsWith("vscode-vfs://")
) {
const lastSegment = path.split("/").pop();
const type = lastSegment?.slice(1)?.includes(".") ? "file" : "folder";

Expand All @@ -114,6 +120,26 @@ export default class VSCodeSearchProvider implements AppSearchProvider {
}
}

_customSuffix(path: string) {
if (!this.extension?._settings?.get_boolean("suffix")) {
return "";
}

const prefixes = {
"vscode-remote://codespaces": "[Codespaces]",
"vscode-remote://": "[Remote]",
"vscode-vfs://github": "[Github]",
};

for (const prefix of Object.keys(prefixes)) {
if (path.startsWith(prefix)) {
return " " + prefixes[prefix as keyof typeof prefixes];
}
}

return "";
}

filterResults(results: string[], maxResults: number) {
return results.slice(0, maxResults);
}
Expand All @@ -136,7 +162,8 @@ export default class VSCodeSearchProvider implements AppSearchProvider {
async getResultMetas(ids: string[]) {
return ids.map((id) => ({
id,
name: this.workspaces[id].name,
name:
this.workspaces[id].name + this._customSuffix(this.workspaces[id].path),
description: this.workspaces[id].path,
createIcon: (size: number) => this.app?.create_icon_texture(size),
}));
Expand Down

0 comments on commit d49fb26

Please sign in to comment.