From e444c15e0c9bba7875e0994edef824549eed1334 Mon Sep 17 00:00:00 2001 From: ezreal <71426102+Ezreal-byte@users.noreply.github.com> Date: Mon, 28 Sep 2026 21:02:37 +0800 Subject: [PATCH] feat(plugins): add settings shortcut entry and drag edge avoidance --- apps/desktop/src/App.vue | 4 +- .../plugins/PluginContributionsPanel.vue | 12 ++- .../plugins/PluginShortcutBar.spec.ts | 54 ++++++++++++- .../components/plugins/PluginShortcutBar.vue | 38 ++++++++-- .../plugins/PluginShortcutSettings.spec.ts | 14 +++- .../plugins/PluginShortcutSettings.vue | 4 + .../plugins/PluginShortcutToolbar.spec.ts | 76 ++++++++++++++++++- .../plugins/PluginShortcutToolbar.vue | 56 +++++++++++--- .../PluginContributionsPanelBatch.spec.ts | 49 +++++++++++- .../useCursorFloatingPosition.spec.ts | 67 ++++++++++++++++ .../__tests__/usePluginShortcutHeight.spec.ts | 10 +++ .../composables/useCursorFloatingPosition.ts | 37 +++++++++ .../composables/usePluginShortcutHeight.ts | 5 +- apps/desktop/src/i18n/locales/az.ts | 3 + apps/desktop/src/i18n/locales/en.ts | 3 + apps/desktop/src/i18n/locales/es.ts | 3 + apps/desktop/src/i18n/locales/it.ts | 3 + apps/desktop/src/i18n/locales/ja.ts | 3 + apps/desktop/src/i18n/locales/ko.ts | 3 + apps/desktop/src/i18n/locales/pt-BR.ts | 3 + apps/desktop/src/i18n/locales/ru.ts | 3 + apps/desktop/src/i18n/locales/tr.ts | 3 + apps/desktop/src/i18n/locales/zh-CN.ts | 3 + apps/desktop/src/i18n/locales/zh-TW.ts | 3 + .../common/floatingViewportPosition.spec.ts | 18 ++++- .../lib/common/floatingViewportPosition.ts | 17 +++++ .../src/lib/plugins/pluginCenterNavigation.ts | 5 ++ .../src/lib/plugins/pluginShortcuts.spec.ts | 6 +- .../src/lib/plugins/pluginShortcuts.ts | 2 + .../settingsStore.pluginShortcuts.spec.ts | 2 +- docs/content/docs/plugin-development.cn.mdx | 4 + docs/content/docs/plugin-development.mdx | 4 + 32 files changed, 483 insertions(+), 34 deletions(-) create mode 100644 apps/desktop/src/composables/__tests__/useCursorFloatingPosition.spec.ts create mode 100644 apps/desktop/src/composables/useCursorFloatingPosition.ts diff --git a/apps/desktop/src/App.vue b/apps/desktop/src/App.vue index c44ead14e2..d03aeafcaa 100644 --- a/apps/desktop/src/App.vue +++ b/apps/desktop/src/App.vue @@ -96,7 +96,7 @@ import { openQueryResultArchiveFile } from "@/lib/query/queryResultArchiveFile"; import { activeTabExternalSqlFileTarget, rememberExternalSqlFileTarget, resolveExternalSqlFileTarget, resolveExternalSqlFileTargetForActiveTab, unassociatedExternalSqlFileTarget, type ExternalSqlFileTarget } from "@/lib/sql/externalSqlFileTarget"; import { externalSqlFileOpenErrorMessage, externalSqlEditorMaxBytes, isSqlFilePath, readBrowserSqlFile, sqlFileTitleFromPath } from "@/lib/sql/sqlFileOpen"; import type { ConnectionConfig, DatabaseType, ObjectBrowserFilter, ObjectSourceKind, QueryTab, TabOutputView, TreeNode } from "@/types/database"; -import type { PluginCenterFocus } from "@/lib/plugins/pluginCenterNavigation"; +import { OPEN_PLUGIN_SETTINGS, type PluginCenterFocus } from "@/lib/plugins/pluginCenterNavigation"; import { parsePluginInstallDeepLink } from "@/lib/plugins/pluginInstallDeepLink"; import { createFrontendPluginRegistry } from "@/lib/plugins/frontendPlugin"; import { parseConnectionDeepLink, parseConnectionDeepLinkUpdate, type ConnectionDeepLinkDraft, type ConnectionDeepLinkUpdate } from "@/lib/connection/connectionDeepLink"; @@ -1276,6 +1276,8 @@ function openPluginCenterPage(focus?: PluginCenterFocus | null) { activateMainContentSurface("pluginCenter"); } +provide(OPEN_PLUGIN_SETTINGS, () => openPluginCenterPage({ section: "settings" })); + function closePluginCenterPage() { pluginCenterTabOpen.value = false; pluginCenterFocus.value = null; diff --git a/apps/desktop/src/components/plugins/PluginContributionsPanel.vue b/apps/desktop/src/components/plugins/PluginContributionsPanel.vue index 93768b978d..f669de9c8b 100644 --- a/apps/desktop/src/components/plugins/PluginContributionsPanel.vue +++ b/apps/desktop/src/components/plugins/PluginContributionsPanel.vue @@ -252,7 +252,9 @@ async function refresh(preferredPluginId = props.focusTarget?.pluginId || select try { [installedPlugins.value, trustedKeys.value, repositories.value] = await Promise.all([api.listPlugins(), api.listPluginTrustedKeys(), api.listPluginRepositories()]); await refreshMarketplace(); - if (props.focusTarget) applyFocusTarget(props.focusTarget); + // Settings navigation is handled immediately by the watcher; replaying it + // after loading would overwrite any subsequent navigation by the user. + if (props.focusTarget && props.focusTarget.section !== "settings") applyFocusTarget(props.focusTarget); else selectFirstProvider(preferredPluginId); } catch (cause) { error.value = cause instanceof Error ? cause.message : String(cause); @@ -278,6 +280,10 @@ async function refreshMarketplace() { } function applyFocusTarget(focus: PluginCenterFocus) { + if (focus.section === "settings") { + activeSection.value = "settings"; + return; + } activeSection.value = "installed"; if (!focus.pluginId) return selectFirstProvider(); const provider = connectionProviders.value.find((entry) => entry.plugin.manifest.id === focus.pluginId && (!focus.providerId || entry.contribution.id === focus.providerId)); @@ -907,9 +913,9 @@ watch(providerConnections, (connections) => { watch( () => props.focusTarget, (focus) => { - if (focus && installedPlugins.value.length) applyFocusTarget(focus); + if (focus && (focus.section === "settings" || installedPlugins.value.length)) applyFocusTarget(focus); }, - { deep: true }, + { deep: true, immediate: true }, ); let lastHandledInstallRequestId = 0; watch( diff --git a/apps/desktop/src/components/plugins/PluginShortcutBar.spec.ts b/apps/desktop/src/components/plugins/PluginShortcutBar.spec.ts index 3677a8aa36..93f6ca104b 100644 --- a/apps/desktop/src/components/plugins/PluginShortcutBar.spec.ts +++ b/apps/desktop/src/components/plugins/PluginShortcutBar.spec.ts @@ -1,10 +1,11 @@ // @vitest-environment happy-dom import { createApp, h, nextTick, reactive, ref } from "vue"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { OPEN_PLUGIN_SETTINGS } from "@/lib/plugins/pluginCenterNavigation"; import PluginShortcutBar from "./PluginShortcutBar.vue"; import { normalizePluginShortcutSettings, type PluginShortcutPosition } from "@/lib/plugins/pluginShortcuts"; -const mocks = vi.hoisted(() => ({ open: vi.fn(), save: vi.fn(), toast: vi.fn(), state: null as any, entries: null as any })); +const mocks = vi.hoisted(() => ({ openSettings: vi.fn(), open: vi.fn(), save: vi.fn(), toast: vi.fn(), state: null as any, entries: null as any })); vi.mock("vue-i18n", () => ({ useI18n: () => ({ t: (key: string) => key }) })); vi.mock("@/stores/settingsStore", () => ({ useSettingsStore: () => mocks.state })); vi.mock("@/composables/useToast", () => ({ useToast: () => ({ toast: mocks.toast }) })); @@ -27,6 +28,7 @@ function mount(position: PluginShortcutPosition) { container = document.createElement("div"); document.body.append(container); app = createApp({ render: () => h(PluginShortcutBar, { position }) }); + app.provide(OPEN_PLUGIN_SETTINGS, mocks.openSettings); app.mount(container); const scroll = container.querySelector("[data-shortcut-scroll]") as HTMLElement; scroll.getBoundingClientRect = () => box(0, 0, 100, 200); @@ -41,16 +43,62 @@ function pointer(element: EventTarget, type: string, x: number, y: number) { } beforeEach(() => { vi.clearAllMocks(); - mocks.state = reactive({ isEditorSettingsLoaded: true, editorSettings: { pluginShortcuts: normalizePluginShortcutSettings({}) }, updateEditorSettingsAndPersist: mocks.save }); + mocks.state = reactive({ isEditorSettingsLoaded: true, editorSettings: { pluginShortcuts: normalizePluginShortcutSettings({ showSettingsEntry: false }) }, updateEditorSettingsAndPersist: mocks.save }); mocks.entries = ref(["a", "b", "c"].map((id) => ({ id, pluginId: id, label: `Function ${id}`, pluginName: `Plugin ${id}`, kind: "workbench", targetId: id, disabled: false }))); mocks.save.mockResolvedValue(undefined); }); afterEach(() => { app?.unmount(); container?.remove(); + vi.restoreAllMocks(); }); describe("plugin shortcut bar", () => { + it.each(["left-top", "right-top", "sidebar-bottom"] as const)("keeps the measured preview inside both edges at %s", async (position) => { + const original = HTMLElement.prototype.getBoundingClientRect; + vi.spyOn(HTMLElement.prototype, "getBoundingClientRect").mockImplementation(function (this: HTMLElement) { + return this.hasAttribute("data-shortcut-drag-preview") ? box(0, 0, 180, 32) : original.call(this); + }); + const items = mount(position); + pointer(items[0], "pointerdown", 16, 16); + pointer(window, "pointermove", window.innerWidth - 2, window.innerHeight - 2); + await flush(); + const preview = document.querySelector("[data-shortcut-drag-preview]")!; + expect(preview.style.left).toBe(`${window.innerWidth - 194}px`); + expect(preview.style.top).toBe(`${window.innerHeight - 46}px`); + pointer(window, "pointermove", 2, 2); + await flush(); + expect(preview.style.left).toBe("14px"); + expect(preview.style.top).toBe("14px"); + expect(preview.style.visibility).not.toBe("hidden"); + }); + + it.each(["left-top", "left-bottom", "right-top", "right-bottom", "sidebar-bottom"] as const)("keeps settings last and outside sorting at %s", async (position) => { + mocks.state.editorSettings.pluginShortcuts.showSettingsEntry = true; + mount(position); + const button = container.querySelector("[data-plugin-shortcut-settings]")!; + const buttons = container.querySelectorAll("button"); + if (position === "sidebar-bottom") { + expect(container.querySelector("[data-shortcut-header]")?.contains(button)).toBe(true); + expect(container.querySelector("[data-shortcut-scroll] [data-plugin-shortcut-settings]")).toBeNull(); + } else expect(buttons[buttons.length - 1]).toBe(button); + expect(button.closest("[data-shortcut-id]")).toBeNull(); + pointer(button, "pointerdown", 16, 120); + pointer(window, "pointermove", 1, 1); + pointer(window, "pointerup", 1, 1); + await flush(); + expect(mocks.save).not.toHaveBeenCalled(); + expect(document.querySelector(".shortcut-drag-preview")).toBeNull(); + button.click(); + expect(mocks.openSettings).toHaveBeenCalledOnce(); + mocks.entries.value = []; + await flush(); + expect(container.querySelector("[data-plugin-shortcut-settings]")).toBeNull(); + expect(container.querySelector("nav")).toBeNull(); + mocks.state.editorSettings.pluginShortcuts.showSettingsEntry = false; + await flush(); + expect(container.querySelector("nav")).toBeNull(); + }); it.each(["left-top", "left-bottom", "right-top", "right-bottom", "sidebar-bottom"] as const)("sorts with pointer events at %s without launching on release", async (position) => { const items = mount(position); await nextTick(); @@ -62,7 +110,7 @@ describe("plugin shortcut bar", () => { expect(document.querySelector(".shortcut-drag-preview")).not.toBeNull(); pointer(window, "pointerup", 1, 1); await flush(); - expect(mocks.save).toHaveBeenCalledWith({ pluginShortcuts: { ...normalizePluginShortcutSettings({}), order: ["c", "a", "b"] } }); + expect(mocks.save).toHaveBeenCalledWith({ pluginShortcuts: { ...normalizePluginShortcutSettings({ showSettingsEntry: false }), order: ["c", "a", "b"] } }); (items[2].querySelector("button") as HTMLElement).click(); expect(mocks.open).not.toHaveBeenCalled(); expect(document.querySelector(".shortcut-drag-preview")).toBeNull(); diff --git a/apps/desktop/src/components/plugins/PluginShortcutBar.vue b/apps/desktop/src/components/plugins/PluginShortcutBar.vue index 7f84625807..8d1f18689c 100644 --- a/apps/desktop/src/components/plugins/PluginShortcutBar.vue +++ b/apps/desktop/src/components/plugins/PluginShortcutBar.vue @@ -1,5 +1,7 @@