Files
openidea/src/workbench/distractionFree.test.tsx
T
lleviathe 8dd29bad41 feat(workbench): toggle the toolbar, status bar and tool window bars from View › Appearance (#2206)
The main toolbar and status bar could not be hidden, Tool Window Bars was a Settings row only, and widgets could be switched only from the status bar's context menu. View > Appearance now has Toolbar, Tool Window Bars, Status Bar and Status Bar Widgets, with check marks in the macOS and in-app menus, plus Settings toggles. Both new settings default to shown. With the toolbar hidden, the window keeps a compact header with the title, which Distraction Free now shares.

Controls stay reachable with a bar hidden: the Git branches popup and background tasks have their own hosts, and background tasks has an action.

Co-authored-by: lleviathe <95321986+lleviathe@users.noreply.github.com>
2026-11-09 07:20:54 +08:00

260 lines
11 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { act, render, screen } from "@testing-library/react";
import { executeAction, getAction } from "@/platform/actions";
import { getContext } from "@/platform/context";
import { bindingsFor, installKeyboardDispatcher, useKeymap } from "@/platform/keymap";
import type { Settings } from "@/platform/ipc";
import { useProject } from "@/platform/project";
import { registerStatusItem, registerToolWindow } from "./contributions";
import { clearNotifications, useNotifications } from "./notificationStore";
import { notificationActions } from "./Notifications";
import {
DISTRACTION_FREE_ACTION,
DISTRACTION_FREE_HINT_KEY,
distractionFreeHint,
distractionFreeWidth,
installDistractionFree,
isDistractionFree,
resetDistractionFree,
setDistractionFree,
useDistractionFree,
} from "./distractionFree";
import {
hideAllToolWindows,
isToolWindowVisible,
loadToolWindowLayout,
resetToolWindowLayout,
showToolWindow,
useToolWindowLayout,
} from "./toolwindows/store";
const Empty = () => null;
let disposers: Array<() => void> = [];
beforeEach(() => {
localStorage.clear();
sessionStorage.clear();
resetToolWindowLayout();
resetDistractionFree();
disposers = [
registerToolWindow({ id: "project", title: "Project", icon: "folder", anchor: "left", component: Empty }),
registerToolWindow({ id: "terminal", title: "Terminal", icon: "terminal", anchor: "bottom", component: Empty }),
registerToolWindow({ id: "structure", title: "Structure", icon: "structure", anchor: "right", component: Empty }),
];
loadToolWindowLayout("/work/df");
showToolWindow("terminal");
});
afterEach(() => {
disposers.reverse().forEach((d) => d());
resetDistractionFree();
useKeymap.setState({ overrides: {} });
});
const savedVisible = () => JSON.parse(localStorage.getItem("openidea.layout.v1:/work/df")!).visible;
describe("distraction free mode", () => {
it("toggles with one action whose title flips Enter / Exit, publishing a context key", async () => {
disposers.push(installDistractionFree());
expect(getAction(DISTRACTION_FREE_ACTION)?.title).toBe("Enter Distraction Free Mode");
expect(getContext().distractionFree).toBe(false);
await executeAction(DISTRACTION_FREE_ACTION);
expect(isDistractionFree()).toBe(true);
expect(getContext().distractionFree).toBe(true);
expect(getAction(DISTRACTION_FREE_ACTION)?.title).toBe("Exit Distraction Free Mode");
await executeAction(DISTRACTION_FREE_ACTION);
expect(isDistractionFree()).toBe(false);
expect(getAction(DISTRACTION_FREE_ACTION)?.title).toBe("Enter Distraction Free Mode");
});
it("hides tool windows and restores the previous layout on exit", () => {
setDistractionFree(true);
expect(isToolWindowVisible("project")).toBe(false);
expect(isToolWindowVisible("terminal")).toBe(false);
// A window opened meanwhile (by shortcut) is gone again after exit.
showToolWindow("structure");
expect(isToolWindowVisible("structure")).toBe(true);
setDistractionFree(false);
expect(isToolWindowVisible("project")).toBe(true);
expect(isToolWindowVisible("terminal")).toBe(true);
expect(isToolWindowVisible("structure")).toBe(false);
});
it("persists the layout to restore, not the hidden one", () => {
setDistractionFree(true);
showToolWindow("structure");
expect(savedVisible()).toMatchObject({ left: "project", bottom: "terminal", right: null });
// Leaving the mode (now or after a restart) loads the normal layout.
resetDistractionFree();
resetToolWindowLayout();
loadToolWindowLayout("/work/df");
expect(isToolWindowVisible("project")).toBe(true);
expect(isToolWindowVisible("terminal")).toBe(true);
});
it("does not disturb Hide All Tool Windows", () => {
setDistractionFree(true);
hideAllToolWindows();
setDistractionFree(false);
expect(useToolWindowLayout.getState().stashed).toBeNull();
expect(isToolWindowVisible("project")).toBe(true);
});
it("survives an app restart and re-applies to a newly loaded project", () => {
setDistractionFree(true);
expect(localStorage.getItem("openidea.distractionFree")).toBe("1");
// Restart: fresh module, layout and session state; only localStorage remains.
sessionStorage.clear();
useDistractionFree.setState({ active: false });
resetToolWindowLayout();
disposers.push(installDistractionFree());
expect(isDistractionFree()).toBe(true);
loadToolWindowLayout("/work/df");
expect(isToolWindowVisible("project")).toBe(false);
setDistractionFree(false);
expect(isToolWindowVisible("project")).toBe(true);
expect(localStorage.getItem("openidea.distractionFree")).toBeNull();
});
it("is unbound in the IntelliJ keymap (⌃⌘D is macOS Look Up) and Escape does not exit", () => {
disposers.push(installDistractionFree());
expect(bindingsFor(DISTRACTION_FREE_ACTION)).toEqual([]);
disposers.push(installKeyboardDispatcher(window));
const press = (code: string, init: KeyboardEventInit) =>
act(() => {
window.dispatchEvent(new KeyboardEvent("keydown", { code, bubbles: true, cancelable: true, ...init }));
});
press("KeyD", { key: "d", ctrlKey: true, metaKey: true });
expect(isDistractionFree()).toBe(false);
setDistractionFree(true);
press("Escape", { key: "Escape" });
expect(isDistractionFree()).toBe(true);
setDistractionFree(false);
});
describe("exit hint", () => {
const hint = () => useNotifications.getState().items.find((n) => n.key === DISTRACTION_FREE_HINT_KEY);
beforeEach(() => clearNotifications());
it("tells how to leave the mode on entering, with an Exit action, and expires on exit", () => {
setDistractionFree(true);
expect(hint()?.balloon).toBe(true);
expect(hint()?.body).toBe(distractionFreeHint());
expect(distractionFreeHint()).toContain("View | Appearance | Exit Distraction Free Mode");
// Unbound toggle (IntelliJ keymap): Find Action with its shortcut is the keyboard way out.
expect(distractionFreeHint()).toMatch(/Find Action \(.+\)/);
hint()!
.actions!.find((a) => a.label === "Exit Distraction Free Mode")!
.run();
expect(isDistractionFree()).toBe(false);
expect(hint()?.balloon ?? false).toBe(false);
});
it("names the toggle's own shortcut when it has one", () => {
useKeymap.setState({ overrides: { [DISTRACTION_FREE_ACTION]: ["Ctrl+Alt+Z"] } });
expect(distractionFreeHint()).not.toContain("Find Action");
});
it("shows again after a restart in the mode, once the project frame loads", () => {
setDistractionFree(true);
clearNotifications();
useDistractionFree.setState({ active: false });
resetToolWindowLayout();
disposers.push(installDistractionFree());
expect(isDistractionFree()).toBe(true);
expect(hint()).toBeUndefined();
loadToolWindowLayout("/work/df");
expect(hint()?.balloon).toBe(true);
});
it("offers Don't Show Again once, scoped to its own notification group", () => {
setDistractionFree(true);
const labels = notificationActions(hint()!).map((a) => a.label);
expect(labels.filter((l) => l === "Don't Show Again")).toHaveLength(1);
expect(hint()!.group).toBe("distractionFree");
});
it("stays off once its group shows no popup", () => {
const settings = useProject.getState().settings;
useProject.setState({
settings: { ...settings, notifications: { groups: { distractionFree: { display: "none" } } } } as never,
});
try {
setDistractionFree(true);
expect(hint()?.balloon ?? false).toBe(false);
} finally {
useProject.setState({ settings });
}
});
});
it("clamps the text width setting", () => {
expect(distractionFreeWidth(undefined)).toBe(80);
expect(distractionFreeWidth(100.4)).toBe(100);
expect(distractionFreeWidth(3)).toBe(20);
expect(distractionFreeWidth(Number.NaN)).toBe(80);
});
});
describe("distraction free shell", () => {
it("hides stripes, status bar and main toolbar contents, keeping the drag region", async () => {
const { Workbench } = await import("./Workbench");
disposers.push(
registerStatusItem({ id: "t.status", align: "left", priority: 0, component: () => <span>status-x</span> }),
);
act(() =>
useProject.setState({
project: { root: "/work/df", name: "df-demo", trusted: true, isGit: false, settings: {} },
}),
);
const { unmount } = render(<Workbench />);
expect(await screen.findByText("df-demo")).toBeInTheDocument();
expect(screen.getByText("status-x")).toBeInTheDocument();
expect(screen.getByRole("button", { name: "Project" })).toBeInTheDocument();
act(() => setDistractionFree(true));
expect(screen.queryByText("df-demo")).toBeNull();
expect(screen.queryByText("status-x")).toBeNull();
expect(screen.queryByRole("button", { name: "Project" })).toBeNull();
expect(screen.queryByRole("button", { name: "Search Everywhere" })).toBeNull();
const bar = document.querySelector("header[data-titlebar='compact']");
// macOS uses its own drag region attribute (window-drag.ts); jsdom reports the host OS.
expect(bar?.hasAttribute("data-tauri-drag-region") || bar?.hasAttribute("data-window-drag-region")).toBe(true);
act(() => setDistractionFree(false));
expect(screen.getByText("df-demo")).toBeInTheDocument();
expect(screen.getByText("status-x")).toBeInTheDocument();
unmount();
useProject.setState({ project: null });
});
it("View › Appearance hides the main toolbar and the status bar on their own", async () => {
const { Workbench } = await import("./Workbench");
disposers.push(
registerStatusItem({ id: "t.status", align: "left", priority: 0, component: () => <span>status-x</span> }),
);
const appearance = (a: Record<string, boolean>) =>
act(() => useProject.setState({ settings: { appearance: a } as unknown as Settings }));
act(() =>
useProject.setState({
project: { root: "/work/df", name: "df-demo", trusted: true, isGit: false, settings: {} },
}),
);
document.title = "df-demo – main.rs";
const { unmount } = render(<Workbench />);
expect(await screen.findByText("df-demo")).toBeInTheDocument();
appearance({ showMainToolbar: false });
expect(screen.queryByText("df-demo")).toBeNull();
expect(document.querySelector("header[data-titlebar='compact']")?.textContent).toBe("df-demo – main.rs");
expect(screen.getByText("status-x")).toBeInTheDocument();
expect(screen.getByRole("button", { name: "Project" })).toBeInTheDocument();
appearance({ showStatusBar: false });
expect(screen.getByText("df-demo")).toBeInTheDocument();
expect(screen.queryByText("status-x")).toBeNull();
unmount();
useProject.setState({ project: null, settings: null });
});
});