diff --git a/src/features/vcs/CommitView.tsx b/src/features/vcs/CommitView.tsx
index f444cab91..32580abc8 100644
--- a/src/features/vcs/CommitView.tsx
+++ b/src/features/vcs/CommitView.tsx
@@ -38,6 +38,7 @@ import {
registerDropTarget,
showContextMenu,
startPointerDrag,
+ tooltipAttrs,
type MenuItem,
type TabItem,
type TreeHandle,
@@ -884,7 +885,7 @@ export function ChangesTree({ mode }: { mode: "commit" | "local" | "dialog" }) {
n.type === "group" && n.changelist && !n.active && styles.inactiveList,
)}
style={n.type === "file" && n.kind ? { color: KIND_COLOR[n.kind] } : undefined}
- title={n.type === "file" && n.kind ? tx(KIND_LABEL[n.kind]) : undefined}
+ {...(n.type === "file" && n.kind && tooltipAttrs(KIND_LABEL[n.kind]))}
>
{nodeText(n)}
diff --git a/src/ui/Tooltip.test.tsx b/src/ui/Tooltip.test.tsx
index b2fc7e9fa..21b0525b0 100644
--- a/src/ui/Tooltip.test.tsx
+++ b/src/ui/Tooltip.test.tsx
@@ -92,3 +92,91 @@ describe("TooltipHost", () => {
}
});
});
+
+describe("TooltipHost on cut-off text", () => {
+ beforeEach(() => {
+ vi.useFakeTimers();
+ });
+
+ afterEach(() => {
+ cleanup();
+ vi.useRealTimers();
+ });
+
+ /** jsdom lays nothing out: give `el` the widths a browser would. */
+ const widths = (el: HTMLElement, scroll: number, client: number) => {
+ Object.defineProperty(el, "scrollWidth", { configurable: true, value: scroll });
+ Object.defineProperty(el, "clientWidth", { configurable: true, value: client });
+ };
+
+ const hover = (el: Element) => {
+ fireEvent.pointerOver(el);
+ act(() => {
+ vi.advanceTimersByTime(TOOLTIP_DELAY);
+ });
+ return screen.queryByRole("tooltip")?.textContent ?? null;
+ };
+
+ it("shows the whole text only while it is cut off", () => {
+ render(
+ <>
+
+
+ origin/feature/REQ-126-明细汇总多选
+
+ >,
+ );
+ const row = screen.getByTestId("row");
+ widths(row, 300, 120);
+ expect(hover(row.firstElementChild!)).toBe("origin/feature/REQ-126-明细汇总多选");
+ fireEvent.pointerOut(row);
+
+ widths(row, 120, 120);
+ expect(hover(row.firstElementChild!)).toBeNull();
+ });
+
+ it("leaves text with a title alone, and text without an ellipsis; adds its own tooltip beside it", () => {
+ render(
+ <>
+
+
+ long name
+
+
+ long name
+
+
+ long name
+
+ >,
+ );
+ for (const id of ["titled", "clipped", "own"]) widths(screen.getByTestId(id), 300, 120);
+ expect(hover(screen.getByTestId("titled"))).toBeNull();
+ expect(hover(screen.getByTestId("clipped"))).toBeNull();
+ expect(hover(screen.getByTestId("own"))).toBe("long nameOwn tooltip");
+ });
+});
+
+it("shows a tab's cut-off title whole, with the tab's own tooltip beside it", () => {
+ vi.useFakeTimers();
+ render(
+ <>
+
+
+
+ 历史记录: PscPrinted.js
+
+
+ >,
+ );
+ const label = screen.getByTestId("label");
+ Object.defineProperty(label, "scrollWidth", { configurable: true, value: 300 });
+ Object.defineProperty(label, "clientWidth", { configurable: true, value: 100 });
+ fireEvent.pointerOver(label);
+ act(() => {
+ vi.advanceTimersByTime(TOOLTIP_DELAY);
+ });
+ expect(screen.getByRole("tooltip").textContent).toBe("历史记录: PscPrinted.js/repo/src/history/PscPrinted.js");
+ cleanup();
+ vi.useRealTimers();
+});
diff --git a/src/ui/Tooltip.tsx b/src/ui/Tooltip.tsx
index 3e37ef62a..47939ead8 100644
--- a/src/ui/Tooltip.tsx
+++ b/src/ui/Tooltip.tsx
@@ -2,6 +2,9 @@
* Tooltips are attribute-driven: any element with `data-tooltip` (and optional
* `data-tooltip-shortcut`) gets one, shown by the single `` after
* 600ms of hover. Hidden on scroll, key press, pointer down or leave.
+ *
+ * Text cut off with an ellipsis gets one too, showing it whole, while it is cut off and
+ * has no tooltip or `title` of its own.
*/
import { useEffect, useRef, useState } from "react";
import clsx from "clsx";
@@ -22,6 +25,34 @@ export function tooltipAttrs(text: string | undefined, shortcut?: string, verbat
return attrs;
}
+/** How far up from the hovered node an ellipsised text is looked for. */
+const TRUNCATED_DEPTH = 4;
+
+/** The element whose text is cut off with an ellipsis around `node`, up to `stop`, if any. */
+function truncatedText(node: Element | null, stop: Element | null): HTMLElement | null {
+ for (let el = node, depth = 0; el instanceof HTMLElement && depth < TRUNCATED_DEPTH; el = el.parentElement, depth++) {
+ // A native tooltip shows instead.
+ if (el.hasAttribute("title")) return null;
+ if (isTruncated(el)) return el;
+ if (el === stop) break;
+ }
+ return null;
+}
+
+const isTruncated = (el: HTMLElement) =>
+ el.scrollWidth > el.clientWidth && getComputedStyle(el).textOverflow === "ellipsis" && !!el.textContent?.trim();
+
+/**
+ * What the tooltip of `el` shows: its own text, and the whole of `cut`'s text while that is
+ * cut off (with its own text beside it, when that says something else).
+ */
+function tooltipText(el: HTMLElement, cut: HTMLElement | null): { text: string; note?: string } | undefined {
+ const own = el.dataset.tooltipVerbatim === undefined ? tx(el.dataset.tooltip) : el.dataset.tooltip;
+ const whole = cut?.isConnected && isTruncated(cut) ? cut.textContent!.trim().replace(/\s+/g, " ") : undefined;
+ if (!whole) return own ? { text: own, note: el.dataset.tooltipShortcut } : undefined;
+ return { text: whole, note: own && !own.includes(whole) ? own : undefined };
+}
+
interface Shown {
text: string;
shortcut?: string;
@@ -49,15 +80,15 @@ export function TooltipHost() {
setShown(null);
};
- const show = (el: HTMLElement) => {
- const text = el.dataset.tooltipVerbatim === undefined ? tx(el.dataset.tooltip) : el.dataset.tooltip;
- if (!text || !el.isConnected) return;
+ const show = (el: HTMLElement, cut: HTMLElement | null) => {
+ const shows = el.isConnected ? tooltipText(el, cut) : undefined;
+ if (!shows) return;
visible = true;
anchorRef.current = el;
const stripe = el.dataset.tooltipStripe;
setShown({
- text,
- shortcut: el.dataset.tooltipShortcut,
+ text: shows.text,
+ shortcut: shows.note,
x: -9999,
y: -9999,
placed: false,
@@ -66,16 +97,20 @@ export function TooltipHost() {
};
const onOver = (e: PointerEvent) => {
- const el = (e.target as Element | null)?.closest?.("[data-tooltip]");
+ const target = e.target as Element | null;
+ const owner = target?.closest?.("[data-tooltip]") ?? null;
+ // Cut-off text inside an element with a tooltip shows in that tooltip.
+ const cut = truncatedText(target, owner);
+ const el = owner ?? cut;
if (!el) return;
if (el === anchorRef.current) return;
clearTimeout(timer);
if (visible) {
- show(el);
+ show(el, cut);
return;
}
const warm = performance.now() - lastHidden < WARM_MS;
- timer = setTimeout(() => show(el), warm ? 0 : TOOLTIP_DELAY);
+ timer = setTimeout(() => show(el, cut), warm ? 0 : TOOLTIP_DELAY);
anchorRef.current = el;
};