diff --git a/ui/src/components/notifications/NotificationStack.vue b/ui/src/components/notifications/NotificationStack.vue index c9d9bc3..a3aebc1 100644 --- a/ui/src/components/notifications/NotificationStack.vue +++ b/ui/src/components/notifications/NotificationStack.vue @@ -8,13 +8,65 @@ * toasts beyond the visible limit collapse into a "+n more" button. The * colours match the editor's in-place notices (`--lt-notice-bg`, the error * tint). + * + * The stack never covers an area registered with `keepClearOf` (the commit + * box): it moves above it ({@link stackBottom}) and is placed again when + * toasts come, the window resizes or a kept area changes size. */ import { CircleAlert, Info, TriangleAlert, X } from "@lucide/vue"; +import { onBeforeUnmount, onMounted, ref, watch } from "vue"; import { t } from "../../i18n"; -import { useNotificationsStore, type NotificationLevel } from "../../stores/notifications"; +import { STACK_BOTTOM, stackBottom, useNotificationsStore, type NotificationLevel } from "../../stores/notifications"; const notifications = useNotificationsStore(); +/** The stack's CSS `bottom`, in pixels. */ +const bottom = ref(STACK_BOTTOM); + +/** Watches the kept areas and their parents for size changes; null where unsupported. */ +const observer = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(place); + +/** Moves the stack above every kept area it would cover. */ +function place(): void { + const areas = notifications.keepClear.map((element) => element.getBoundingClientRect()); + bottom.value = stackBottom(window.innerWidth, window.innerHeight, areas); +} + +/** Observes the kept areas and the elements around them (a resized panel moves them). */ +function observe(): void { + if (!observer) { + return; + } + observer.disconnect(); + for (const element of notifications.keepClear) { + observer.observe(element); + if (element.parentElement) { + observer.observe(element.parentElement); + } + } +} + +watch( + () => notifications.keepClear, + () => { + observe(); + place(); + }, +); + +watch(() => notifications.toasts.length, place); + +onMounted(() => { + window.addEventListener("resize", place); + observe(); + place(); +}); + +onBeforeUnmount(() => { + window.removeEventListener("resize", place); + observer?.disconnect(); +}); + /** Icon of each level. */ const ICONS = { info: Info, warning: TriangleAlert, error: CircleAlert } as const; @@ -32,6 +84,7 @@ function roleOf(level: NotificationLevel): "status" | "alert" {
@@ -85,14 +138,12 @@ function roleOf(level: NotificationLevel): "status" | "alert" { .toasts { position: fixed; right: 16px; - bottom: 36px; z-index: 100; display: flex; flex-direction: column; align-items: flex-end; gap: 8px; width: min(420px, calc(100vw - 32px)); - max-height: calc(100vh - 72px); overflow-y: auto; user-select: text; } diff --git a/ui/src/components/vcs/changes/ChangesToolWindow.vue b/ui/src/components/vcs/changes/ChangesToolWindow.vue index 46c74d0..26d6d72 100644 --- a/ui/src/components/vcs/changes/ChangesToolWindow.vue +++ b/ui/src/components/vcs/changes/ChangesToolWindow.vue @@ -25,13 +25,14 @@ * Open, and Add to .gitignore for untracked files. * - **Commit box:** the message, an Amend checkbox and Commit. Commit * saves dirty buffers first (`flushDirty`); `Mod+Enter` in the box - * commits too. + * commits too. Toasts never cover the box: it is registered with the + * notifications store's `keepClearOf`. * - State lives in the Changes store (`changes.ts`), so the status and the * message survive hiding the window. Icons are Lucide's; colours are * tokens. */ import { Archive, ArchiveRestore, RefreshCw } from "@lucide/vue"; -import { computed, nextTick, onMounted, ref, watch } from "vue"; +import { computed, nextTick, onBeforeUnmount, onMounted, ref, watch } from "vue"; import { useCommandsStore } from "../../../commands/store"; import { isMac } from "../../../commands/keymap"; import { t } from "../../../i18n"; @@ -50,6 +51,12 @@ const list = ref(null); /** The commit message box. */ const messageBox = ref(null); +/** The commit box, kept clear of toasts. */ +const commitBox = ref(null); + +/** Releases the commit box from the toast stack; null while not registered. */ +let releaseCommitBox: (() => void) | null = null; + /** The open context menu: its row and where it shows; null when closed. */ const menu = ref<{ row: ChangeRow; x: number; y: number } | null>(null); @@ -317,6 +324,14 @@ watch(() => changes.focusRequest, takeFocus); onMounted(() => { void takeFocus(); + if (commitBox.value) { + releaseCommitBox = notifications.keepClearOf(commitBox.value); + } +}); + +onBeforeUnmount(() => { + releaseCommitBox?.(); + releaseCommitBox = null; }); watch( @@ -465,6 +480,7 @@ watch( @open="openRow" />
{ const busy = ref(false); /** Bumped on every load, so only the newest one lands. */ let generation = 0; + /** Id of the shown conflict warning toast; null when none is shown. */ + let conflictToast: string | null = null; /** The conflicted files, sorted by path. */ const conflicts = computed(() => @@ -177,6 +183,23 @@ export const useMergeStore = defineStore("merge", () => { .map((file) => file.path), ); + /** Closes the conflict warning toast, if one is shown. */ + function dismissConflictToast(): void { + if (conflictToast !== null) { + notifications.dismiss(conflictToast); + conflictToast = null; + } + } + + watch( + () => conflicts.value.length, + (count) => { + if (count === 0) { + dismissConflictToast(); + } + }, + ); + /** The current branch, null when detached or unknown. */ const currentBranch = computed(() => status.snapshot?.repo?.branch ?? null); @@ -254,8 +277,9 @@ export const useMergeStore = defineStore("merge", () => { return null; } await status.refresh(); + dismissConflictToast(); if (answer.result === "conflicts") { - notifications.notify({ + conflictToast = notifications.notify({ level: "warning", messageKey: "notification.vcs.mergeConflicts", params: { branch, count: String(answer.conflicts.length) }, @@ -292,6 +316,7 @@ export const useMergeStore = defineStore("merge", () => { return false; } await status.refresh(); + dismissConflictToast(); notifications.notify({ level: "info", messageKey: "notification.vcs.mergeAborted" }); close(); return true; diff --git a/ui/src/stores/notifications.ts b/ui/src/stores/notifications.ts index 9668dcf..b300a18 100644 --- a/ui/src/stores/notifications.ts +++ b/ui/src/stores/notifications.ts @@ -10,9 +10,12 @@ * Info toasts dismiss themselves after {@link INFO_TIMEOUT_MS}; warnings * and errors stay until closed. At most {@link MAX_VISIBLE} toasts show; * older ones collapse into a "+n more" entry. + * + * Areas the stack must never cover (the commit box) register themselves + * with `keepClear`; the stack then sits above them ({@link stackBottom}). */ import { defineStore } from "pinia"; -import { computed, ref } from "vue"; +import { computed, ref, shallowRef } from "vue"; import { hasMessage, t, type MessageParams } from "../i18n"; import { onEngineEvent, type EngineError, type EngineEventPayload } from "../ipc"; @@ -34,6 +37,55 @@ export const KEY_WATCH_FAILED = "notification.watch.failed"; /** Catalog key of the toast for a failed command. Params: `command`, `message`. */ export const KEY_COMMAND_FAILED = "notification.command.failed"; +/** Distance of the stack from the window's bottom edge, in pixels. */ +export const STACK_BOTTOM = 36; + +/** Distance of the stack from the window's right edge, in pixels. */ +export const STACK_RIGHT = 16; + +/** Widest the stack gets, in pixels. */ +export const STACK_WIDTH = 420; + +/** Gap kept between the stack and an area it must not cover, in pixels. */ +export const KEEP_CLEAR_GAP = 8; + +/** Where an area sits in the window, as `getBoundingClientRect` gives it. */ +export interface Area { + /** Left edge. */ + left: number; + /** Right edge. */ + right: number; + /** Top edge. */ + top: number; + /** Bottom edge. */ + bottom: number; +} + +/** + * The stack's distance from the window's bottom edge so that it covers + * none of the given areas, however tall it grows: above the highest one + * it overlaps horizontally, else {@link STACK_BOTTOM}. Hidden areas (no + * width or height, as under `v-show`) are skipped. + * + * @param width - the window's inner width. + * @param height - the window's inner height. + * @param areas - the areas to keep clear. + * @returns the CSS `bottom` in pixels. + */ +export function stackBottom(width: number, height: number, areas: readonly Area[]): number { + const right = width - STACK_RIGHT; + const left = right - Math.min(STACK_WIDTH, width - 2 * STACK_RIGHT); + let bottom = STACK_BOTTOM; + for (const area of areas) { + const hidden = area.right <= area.left || area.bottom <= area.top; + const beside = area.right <= left || area.left >= right; + if (!hidden && !beside) { + bottom = Math.max(bottom, height - area.top + KEEP_CLEAR_GAP); + } + } + return bottom; +} + /** A toast as the stack shows it. */ export interface Toast { /** Unique id: the engine's (`n1`…) or a frontend one (`ui1`…). */ @@ -134,6 +186,8 @@ export const useNotificationsStore = defineStore("notifications", () => { let made = 0; /** Auto-dismiss timers of info toasts, by id. */ const timers = new Map>(); + /** Elements the stack must not cover, in registration order. */ + const keepClear = shallowRef([]); /** Ends the engine event subscription; set while started. */ let unsubscribe: (() => void) | null = null; @@ -228,6 +282,19 @@ export const useNotificationsStore = defineStore("notifications", () => { expanded.value = false; } + /** + * Keeps the stack off an element until the returned function is called. + * + * @param element - the element, e.g. the commit box. + * @returns a function that releases the element again. + */ + function keepClearOf(element: HTMLElement): () => void { + keepClear.value = [...keepClear.value, element]; + return () => { + keepClear.value = keepClear.value.filter((kept) => kept !== element); + }; + } + /** Shows the toasts collapsed into "+n more". */ function expand(): void { expanded.value = true; @@ -262,6 +329,8 @@ export const useNotificationsStore = defineStore("notifications", () => { visible, hiddenCount, expanded, + keepClear, + keepClearOf, notify, notifyError, dismiss, diff --git a/ui/tests/notifications/notifications.test.ts b/ui/tests/notifications/notifications.test.ts index c0b490b..649d0e1 100644 --- a/ui/tests/notifications/notifications.test.ts +++ b/ui/tests/notifications/notifications.test.ts @@ -3,7 +3,8 @@ * a toast, auto-dismiss of info toasts with fake timers, the overflow into * "+n more", ARIA roles, keyboard closing, engine `notification` and * `watchFailed` events (including untrusted fields), and the replay - * duplicate guard. + * duplicate guard, and keeping the stack off registered areas such as the + * commit box. */ import { mount, type VueWrapper } from "@vue/test-utils"; import { createPinia, setActivePinia, type Pinia } from "pinia"; @@ -21,6 +22,8 @@ import { MAX_VISIBLE, notificationFromEvent, notificationText, + STACK_BOTTOM, + stackBottom, useNotificationsStore, } from "../../src/stores/notifications"; @@ -277,3 +280,33 @@ describe("NotificationStack", () => { expect(stack.find('[data-test="toast"]').exists()).toBe(false); }); }); + +describe("keeping areas clear", () => { + it("places the stack above an area it would cover, and ignores hidden or distant ones", () => { + const box = { left: 0, right: 1000, top: 700, bottom: 780 }; + expect(stackBottom(1000, 800, [])).toBe(STACK_BOTTOM); + expect(stackBottom(1000, 800, [box])).toBe(108); + expect(stackBottom(1000, 800, [{ left: 0, right: 0, top: 0, bottom: 0 }])).toBe(STACK_BOTTOM); + expect(stackBottom(1000, 800, [{ ...box, right: 500 }])).toBe(STACK_BOTTOM); + expect(stackBottom(1000, 800, [box, { ...box, top: 600 }])).toBe(208); + }); + + it("moves the shown stack above a registered element until it is released", async () => { + Object.assign(window, { innerWidth: 1000, innerHeight: 800 }); + const store = useNotificationsStore(); + const commit = document.createElement("form"); + commit.getBoundingClientRect = () => new DOMRect(0, 700, 1000, 80); + document.body.append(commit); + store.notify({ level: "warning", message: "conflicts" }); + const stack = mountStack(); + await stack.vm.$nextTick(); + expect(stack.get('[data-test="toasts"]').attributes("style")).toContain(`bottom: ${String(STACK_BOTTOM)}px`); + const release = store.keepClearOf(commit); + await stack.vm.$nextTick(); + expect(stack.get('[data-test="toasts"]').attributes("style")).toContain("bottom: 108px"); + release(); + await stack.vm.$nextTick(); + expect(stack.get('[data-test="toasts"]').attributes("style")).toContain(`bottom: ${String(STACK_BOTTOM)}px`); + commit.remove(); + }); +}); diff --git a/ui/tests/vcs/ChangesToolWindow.test.ts b/ui/tests/vcs/ChangesToolWindow.test.ts index 38c465a..ae5aee6 100644 --- a/ui/tests/vcs/ChangesToolWindow.test.ts +++ b/ui/tests/vcs/ChangesToolWindow.test.ts @@ -383,6 +383,15 @@ describe("the Changes store", () => { }); describe("the Changes window", () => { + it("keeps toasts off the commit box while it is mounted", async () => { + const wrapper = await mountChanges(); + const notifications = useNotificationsStore(pinia); + expect(notifications.keepClear).toEqual([wrapper.get("[data-test=changes-commit]").element]); + mounted = mounted.filter((m) => m !== wrapper); + wrapper.unmount(); + expect(notifications.keepClear).toEqual([]); + }); + it("shows the branch, the counts and the three groups", async () => { const wrapper = await mountChanges(); expect(wrapper.get("[data-test=changes-status]").text()).toBe("main: 3 staged, 2 changed, 1 untracked"); diff --git a/ui/tests/vcs/Merge.test.ts b/ui/tests/vcs/Merge.test.ts index a52dbb4..9deceef 100644 --- a/ui/tests/vcs/Merge.test.ts +++ b/ui/tests/vcs/Merge.test.ts @@ -416,6 +416,29 @@ describe("merging", () => { expect(useNotificationsStore(pinia).toasts.some((n) => n.text.includes("cancelled"))).toBe(true); }); + it("closes the conflict toast once no conflict is left, and a new conflicted merge replaces it", async () => { + await mountDialog(); + const notifications = useNotificationsStore(pinia); + const conflictToasts = (): number => notifications.toasts.filter((n) => n.text.includes("stopped on conflicts")).length; + await runCommand("vcs.merge", { branch: "feature" }); + await runCommand("vcs.merge", { branch: "feature" }); + expect(conflictToasts()).toBe(1); + gitStatus = { ...gitStatus, files: [{ path: "b.php", index: "modified", worktree: "unmodified" }] }; + await useVcsStatusStore(pinia).refresh(); + await flushPromises(); + expect(conflictToasts()).toBe(0); + }); + + it("closes the conflict toast when the merge is aborted", async () => { + const wrapper = await mountDialog(); + await runCommand("vcs.merge", { branch: "feature" }); + expect(useNotificationsStore(pinia).toasts.some((n) => n.text.includes("stopped on conflicts"))).toBe(true); + await runCommand("vcs.abortMerge"); + await wrapper.get("[data-test=merge-abort]").trigger("click"); + await flushPromises(); + expect(useNotificationsStore(pinia).toasts.some((n) => n.text.includes("stopped on conflicts"))).toBe(false); + }); + it("lists the other branches and merges the one chosen", async () => { const wrapper = await mountDialog(); await runCommand("vcs.merge");