feat(plugins): add settings shortcut entry and drag edge avoidance
This commit is contained in:
1 parent
fd6b7bd502
commit
e444c15e0c
32 files changed
+483
-34
No files matched your search
@@ -1,7 +1,23 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { floatingArrowOffset, floatingViewportShift } from "@/lib/common/floatingViewportPosition";
|
||||
import { floatingArrowOffset, floatingCursorPosition, floatingViewportShift } from "@/lib/common/floatingViewportPosition";
|
||||
|
||||
describe("floating viewport position", () => {
|
||||
it.each([
|
||||
{ cursor: { x: 400, y: 300 }, side: "right", expected: { x: 412, y: 312 } },
|
||||
{ cursor: { x: 400, y: 300 }, side: "left", expected: { x: 188, y: 312 } },
|
||||
{ cursor: { x: 790, y: 590 }, side: "right", expected: { x: 578, y: 538 } },
|
||||
{ cursor: { x: 2, y: 2 }, side: "left", expected: { x: 14, y: 14 } },
|
||||
{ cursor: { x: -30, y: -30 }, side: "left", expected: { x: 8, y: 8 } },
|
||||
{ cursor: { x: 900, y: 700 }, side: "right", expected: { x: 592, y: 552 } },
|
||||
] as const)("follows and flips around $cursor with $side preference", ({ cursor, side, expected }) => {
|
||||
expect(floatingCursorPosition(cursor, { width: 200, height: 40 }, { width: 800, height: 600 }, side)).toEqual(expected);
|
||||
});
|
||||
|
||||
it("clamps when neither side has enough room, including oversized surfaces", () => {
|
||||
expect(floatingCursorPosition({ x: 150, y: 40 }, { width: 284, height: 64 }, { width: 300, height: 80 })).toEqual({ x: 8, y: 8 });
|
||||
expect(floatingCursorPosition({ x: 1, y: 1 }, { width: 200, height: 40 }, { width: 4, height: 4 })).toEqual({ x: 2, y: 2 });
|
||||
});
|
||||
|
||||
it("moves a floating element inside each viewport edge", () => {
|
||||
expect(floatingViewportShift({ left: -24, right: 296, top: 40, bottom: 80 }, { width: 800, height: 600 })).toEqual({ x: 32, y: 0 });
|
||||
expect(floatingViewportShift({ left: 600, right: 840, top: 40, bottom: 80 }, { width: 800, height: 600 })).toEqual({ x: -48, y: 0 });
|
||||
|
||||
@@ -10,6 +10,23 @@ export interface FloatingViewport {
|
||||
height: number;
|
||||
}
|
||||
|
||||
/** Place a cursor-following surface, flipping each axis before clamping to the viewport. */
|
||||
export function floatingCursorPosition(cursor: { x: number; y: number }, size: { width: number; height: number }, viewport: FloatingViewport, preferredSide: "left" | "right" = "right", gap = 12, margin = 8): { x: number; y: number } {
|
||||
function axis(pointer: number, length: number, extent: number, before: boolean) {
|
||||
const inset = Math.min(margin, Math.max(0, extent / 2));
|
||||
const maximum = Math.max(inset, extent - inset - length);
|
||||
const preferred = before ? pointer - gap - length : pointer + gap;
|
||||
const alternative = before ? pointer + gap : pointer - gap - length;
|
||||
const fits = (value: number) => value >= inset && value + length <= extent - inset;
|
||||
const position = fits(preferred) ? preferred : fits(alternative) ? alternative : preferred;
|
||||
return Math.min(maximum, Math.max(inset, position));
|
||||
}
|
||||
return {
|
||||
x: axis(cursor.x, size.width, viewport.width, preferredSide === "left"),
|
||||
y: axis(cursor.y, size.height, viewport.height, false),
|
||||
};
|
||||
}
|
||||
|
||||
export function floatingViewportShift(rect: FloatingRect, viewport: FloatingViewport, margin = 8): { x: number; y: number } {
|
||||
let x = 0;
|
||||
let y = 0;
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
import type { InjectionKey } from "vue";
|
||||
|
||||
export const OPEN_PLUGIN_SETTINGS: InjectionKey<() => void> = Symbol("open-plugin-settings");
|
||||
|
||||
export interface PluginCenterFocus {
|
||||
section?: "settings";
|
||||
pluginId?: string;
|
||||
providerId?: string;
|
||||
}
|
||||
+4
-2
@@ -45,8 +45,10 @@ describe("plugin shortcuts", () => {
|
||||
expect(clampPluginShortcutHeight(40, 130)).toBe(10);
|
||||
});
|
||||
it("normalizes old and malformed preferences with independent default arrays", () => {
|
||||
expect(normalizePluginShortcutSettings(null)).toEqual({ enabled: true, position: "right-top", order: [], hiddenPluginIds: [], sidebarHeight: null, toolbarCount: 3 });
|
||||
expect(normalizePluginShortcutSettings({ enabled: "false", position: "top", order: [null, "a", "", "a", "b"] })).toEqual({ enabled: true, position: "right-top", order: ["a", "b"], hiddenPluginIds: [], sidebarHeight: null, toolbarCount: 3 });
|
||||
expect(normalizePluginShortcutSettings({ showSettingsEntry: "false" }).showSettingsEntry).toBe(true);
|
||||
expect(normalizePluginShortcutSettings({ showSettingsEntry: false }).showSettingsEntry).toBe(false);
|
||||
expect(normalizePluginShortcutSettings(null)).toEqual({ enabled: true, position: "right-top", order: [], hiddenPluginIds: [], sidebarHeight: null, toolbarCount: 3, showSettingsEntry: true });
|
||||
expect(normalizePluginShortcutSettings({ enabled: "false", position: "top", order: [null, "a", "", "a", "b"] })).toEqual({ enabled: true, position: "right-top", order: ["a", "b"], hiddenPluginIds: [], sidebarHeight: null, toolbarCount: 3, showSettingsEntry: true });
|
||||
expect(normalizePluginShortcutSettings({ enabled: false, position: "sidebar-bottom" }).enabled).toBe(false);
|
||||
});
|
||||
it("includes legacy standalone workbenches and filesystems but not connection-bound surfaces", () => {
|
||||
|
||||
@@ -4,6 +4,7 @@ import type { PluginCommandContribution } from "@/types/database";
|
||||
export type PluginShortcutPosition = "left-top" | "left-bottom" | "right-top" | "right-bottom" | "sidebar-bottom" | "toolbar" | "plugin-center";
|
||||
export interface PluginShortcutSettings {
|
||||
enabled: boolean;
|
||||
showSettingsEntry: boolean;
|
||||
position: PluginShortcutPosition;
|
||||
order: string[];
|
||||
hiddenPluginIds: string[];
|
||||
@@ -17,6 +18,7 @@ export function normalizePluginShortcutSettings(value: unknown): PluginShortcutS
|
||||
const strings = (values: unknown): string[] => (Array.isArray(values) ? [...new Set(values.filter((id): id is string => typeof id === "string" && id.length > 0))] : []);
|
||||
return {
|
||||
enabled: typeof input.enabled === "boolean" ? input.enabled : true,
|
||||
showSettingsEntry: typeof input.showSettingsEntry === "boolean" ? input.showSettingsEntry : true,
|
||||
position: position === "left" ? "left-top" : position === "left-top" || position === "left-bottom" || position === "right-bottom" || position === "sidebar-bottom" || position === "toolbar" || position === "plugin-center" ? position : "right-top",
|
||||
order: strings(input.order),
|
||||
hiddenPluginIds: strings(input.hiddenPluginIds),
|
||||
|
||||
Reference in new issue
Block a user