дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-04 09:11:10 +02:00
Порівняти коміти
| Автор | SHA1 | Дата | |
|---|---|---|---|
|
|
20319340a6 | ||
|
|
2d69128b9c |
No files matched your search
@@ -285,8 +285,6 @@ pub struct AppearanceSettings {
|
||||
pub project_colors_in_toolbar: bool,
|
||||
pub dnd_with_alt_only: bool,
|
||||
pub menu_icons: bool,
|
||||
/// `hamburger` | `merged`: main menu presentation in the frameless title bar.
|
||||
pub main_menu_style: String,
|
||||
/// `subpixel` | `greyscale` | `none`
|
||||
pub ide_antialiasing: String,
|
||||
pub editor_antialiasing: String,
|
||||
@@ -333,7 +331,6 @@ impl Default for AppearanceSettings {
|
||||
project_colors_in_toolbar: true,
|
||||
dnd_with_alt_only: false,
|
||||
menu_icons: true,
|
||||
main_menu_style: "hamburger".into(),
|
||||
ide_antialiasing: "greyscale".into(),
|
||||
editor_antialiasing: "greyscale".into(),
|
||||
show_tool_window_bars: true,
|
||||
@@ -716,15 +713,6 @@ impl Settings {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn main_menu_style_defaults_and_round_trips() {
|
||||
let old: Settings = serde_json::from_str(r#"{"appearance":{"compactMode":true}}"#).unwrap();
|
||||
assert_eq!(old.appearance.main_menu_style, "hamburger");
|
||||
let merged: Settings = serde_json::from_str(r#"{"appearance":{"mainMenuStyle":"merged"}}"#).unwrap();
|
||||
assert_eq!(merged.appearance.main_menu_style, "merged");
|
||||
assert_eq!(serde_json::to_value(merged).unwrap()["appearance"]["mainMenuStyle"], "merged");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn editor_font_weights_default_to_regular_and_bold() {
|
||||
let json = serde_json::to_value(Settings::default()).unwrap();
|
||||
|
||||
@@ -752,15 +752,9 @@ pub fn open_settings_window(
|
||||
if let Err(e) = fit_settings_window(&window, &parent) {
|
||||
tracing::warn!("cannot fit settings window: {e}");
|
||||
}
|
||||
// Elsewhere the remembered size is kept and the position follows the parent.
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
if let (Ok(pos), Ok(size), Ok(own), Ok(scale)) =
|
||||
(parent.outer_position(), parent.outer_size(), window.outer_size(), parent.scale_factor())
|
||||
{
|
||||
let (pos, size, own) =
|
||||
(pos.to_logical::<f64>(scale), size.to_logical::<f64>(scale), own.to_logical::<f64>(scale));
|
||||
let at = centered(pos.x, pos.y, size.width, size.height, own.width, own.height);
|
||||
let _ = window.set_position(tauri::LogicalPosition::new(at.0, at.1));
|
||||
if let Err(e) = place_settings_window(&window, &parent) {
|
||||
tracing::warn!("cannot place settings window: {e}");
|
||||
}
|
||||
focus(&window);
|
||||
Ok(())
|
||||
@@ -805,7 +799,45 @@ fn fit_settings_window(window: &WebviewWindow, parent: &WebviewWindow) -> tauri:
|
||||
))
|
||||
}
|
||||
|
||||
#[cfg_attr(not(target_os = "macos"), allow(dead_code))]
|
||||
/// Centres the settings window over its owner, inside the owner's monitor work area. The window
|
||||
/// may still be on another display with a different scale, so its size is converted with its own
|
||||
/// scale and its position is given in physical pixels.
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
fn place_settings_window(window: &WebviewWindow, parent: &WebviewWindow) -> tauri::Result<()> {
|
||||
let Some(monitor) = parent.current_monitor()?.or(parent.primary_monitor()?) else {
|
||||
return Ok(());
|
||||
};
|
||||
let scale = monitor.scale_factor();
|
||||
let area = monitor.work_area();
|
||||
let origin = area.position.to_logical::<f64>(scale);
|
||||
let available = area.size.to_logical::<f64>(scale);
|
||||
let pos = parent.outer_position()?.to_logical::<f64>(scale);
|
||||
let size = parent.outer_size()?.to_logical::<f64>(scale);
|
||||
let own_scale = window.scale_factor()?;
|
||||
let own = window.outer_size()?.to_logical::<f64>(own_scale);
|
||||
let inner = window.inner_size()?.to_logical::<f64>(own_scale);
|
||||
let frame = (own.width - inner.width, own.height - inner.height);
|
||||
let bounds = fit_bounds(
|
||||
(pos.x, pos.y, size.width, size.height),
|
||||
(own.width, own.height),
|
||||
(origin.x, origin.y, available.width, available.height),
|
||||
);
|
||||
window.set_position(tauri::PhysicalPosition::new(
|
||||
(bounds.0 * scale).round() as i32,
|
||||
(bounds.1 * scale).round() as i32,
|
||||
))?;
|
||||
if (bounds.2, bounds.3) != (own.width, own.height) {
|
||||
// Sizes are set on the client area, and even an undecorated window has a resize frame.
|
||||
let (width, height) = ((bounds.2 - frame.0).max(1.0), (bounds.3 - frame.1).max(1.0));
|
||||
window.set_min_size(Some(tauri::LogicalSize::new(
|
||||
SETTINGS_MIN_SIZE.0.min(width),
|
||||
SETTINGS_MIN_SIZE.1.min(height),
|
||||
)))?;
|
||||
window.set_size(tauri::LogicalSize::new(width, height))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn fit_bounds(parent: (f64, f64, f64, f64), size: (f64, f64), work: (f64, f64, f64, f64)) -> (f64, f64, f64, f64) {
|
||||
let width = size.0.min(work.2);
|
||||
let height = size.1.min(work.3);
|
||||
@@ -1094,6 +1126,8 @@ mod tests {
|
||||
(0.0, 0.0, 1280.0, 1040.0 / 1.5),
|
||||
(150.0, 0.0, 980.0, 1040.0 / 1.5),
|
||||
),
|
||||
// 3840×2160 at 250% scaling: an owner taller than the work area must not push it off screen.
|
||||
((100.0, 60.0, 1440.0, 900.0), (980.0, 720.0), (0.0, 0.0, 1536.0, 816.0), (330.0, 96.0, 980.0, 720.0)),
|
||||
// Restored geometry from a larger display.
|
||||
((100.0, 50.0, 1440.0, 900.0), (2400.0, 1600.0), (0.0, 24.0, 1920.0, 1016.0), (0.0, 24.0, 1920.0, 1016.0)),
|
||||
// Secondary display with a negative origin and reserved top/left space.
|
||||
|
||||
@@ -1407,8 +1407,6 @@ export type AppearanceSettings = {
|
||||
projectColorsInToolbar?: boolean,
|
||||
dndWithAltOnly?: boolean,
|
||||
menuIcons?: boolean,
|
||||
/** `hamburger` | `merged`: main menu presentation in the frameless title bar. */
|
||||
mainMenuStyle?: string,
|
||||
/** `subpixel` | `greyscale` | `none` */
|
||||
ideAntialiasing?: string,
|
||||
editorAntialiasing?: string,
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
import { act, fireEvent, render, screen } from "@testing-library/react";
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import { usePlatform } from "@/platform/platformInfo";
|
||||
import { validateAppearanceExtras } from "../model/behaviorSchema";
|
||||
import { useSettingsDialog } from "../model/staging";
|
||||
import { BEHAVIOR_PAGES } from "../pages/behavior";
|
||||
|
||||
const savedPlatform = usePlatform.getState();
|
||||
const savedDialog = useSettingsDialog.getState();
|
||||
afterEach(() => {
|
||||
usePlatform.setState(savedPlatform);
|
||||
useSettingsDialog.setState(savedDialog);
|
||||
});
|
||||
const option = BEHAVIOR_PAGES.find((p) => p.id === "appearance")!
|
||||
.sections!.find((s) => s.title === "UI Options")!
|
||||
.options.find((o) => o.kind === "custom")!;
|
||||
if (option.kind !== "custom") throw new Error("UI Options missing");
|
||||
const MainMenuStyleRow = option.component;
|
||||
|
||||
describe("Main menu appearance setting", () => {
|
||||
it.each(["windows", "linux"])("offers both styles on a frameless %s title bar", async (os) => {
|
||||
usePlatform.setState({ os, framelessTitleBar: true });
|
||||
render(<MainMenuStyleRow />);
|
||||
const select = screen.getByRole("button", { name: "Main menu" });
|
||||
expect(select).toHaveTextContent("Hamburger button");
|
||||
fireEvent.click(select);
|
||||
fireEvent.click(await screen.findByRole("option", { name: "Merged with main toolbar" }));
|
||||
expect(select).toHaveTextContent("Merged with main toolbar");
|
||||
expect(useSettingsDialog.getState().staged["global|appearance.mainMenuStyle"]?.value).toBe("merged");
|
||||
});
|
||||
|
||||
it.each(["macos", "web", "linux"])("hides the setting without the frameless menu (%s)", (os) => {
|
||||
usePlatform.setState({ os, framelessTitleBar: false });
|
||||
render(<MainMenuStyleRow />);
|
||||
expect(screen.queryByText("Main menu")).toBeNull();
|
||||
expect(screen.queryByRole("button", { name: "Main menu" })).toBeNull();
|
||||
act(() => usePlatform.setState({ os: "windows", framelessTitleBar: true }));
|
||||
expect(screen.getByRole("button", { name: "Main menu" })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("preserves the hamburger default for existing or invalid settings", () => {
|
||||
expect(validateAppearanceExtras({}).mainMenuStyle).toBe("hamburger");
|
||||
expect(validateAppearanceExtras({ mainMenuStyle: "unknown" }).mainMenuStyle).toBe("hamburger");
|
||||
expect(validateAppearanceExtras({ mainMenuStyle: "merged" }).mainMenuStyle).toBe("merged");
|
||||
});
|
||||
});
|
||||
@@ -27,7 +27,6 @@ export interface AppearanceExtras {
|
||||
projectColorsInToolbar: boolean;
|
||||
dndWithAltOnly: boolean;
|
||||
menuIcons: boolean;
|
||||
mainMenuStyle: "hamburger" | "merged";
|
||||
ideAntialiasing: Antialiasing;
|
||||
editorAntialiasing: Antialiasing;
|
||||
showToolWindowBars: boolean;
|
||||
@@ -79,7 +78,6 @@ export const DEFAULT_APPEARANCE_EXTRAS: AppearanceExtras = {
|
||||
projectColorsInToolbar: true,
|
||||
dndWithAltOnly: false,
|
||||
menuIcons: true,
|
||||
mainMenuStyle: "hamburger",
|
||||
ideAntialiasing: "greyscale",
|
||||
editorAntialiasing: "greyscale",
|
||||
showToolWindowBars: true,
|
||||
@@ -129,7 +127,6 @@ export function validateAppearanceExtras(raw: unknown): AppearanceExtras {
|
||||
projectColorsInToolbar: b("projectColorsInToolbar"),
|
||||
dndWithAltOnly: b("dndWithAltOnly"),
|
||||
menuIcons: b("menuIcons"),
|
||||
mainMenuStyle: oneOf(a.mainMenuStyle, ["hamburger", "merged"], d.mainMenuStyle),
|
||||
ideAntialiasing: oneOf(a.ideAntialiasing, AA, d.ideAntialiasing),
|
||||
editorAntialiasing: oneOf(a.editorAntialiasing, AA, d.editorAntialiasing),
|
||||
showToolWindowBars: b("showToolWindowBars"),
|
||||
|
||||
@@ -9,7 +9,6 @@ import { FontPicker } from "../components/FontPicker";
|
||||
import { useId, useRef, useState, type ReactNode } from "react";
|
||||
import clsx from "clsx";
|
||||
import { shortcutLabel } from "@/platform/keymap";
|
||||
import { usePlatform } from "@/platform/platformInfo";
|
||||
import { isTauri } from "@/platform/ipc";
|
||||
import { SCHEMES as EDITOR_SCHEMES, listSchemes, customSchemes } from "@/platform/colorScheme";
|
||||
import { formatTimestamp } from "@/platform/dateFormat";
|
||||
@@ -224,25 +223,6 @@ function Grid({ left, right }: { left: ReactNode; right: ReactNode }) {
|
||||
);
|
||||
}
|
||||
|
||||
function MainMenuStyleRow() {
|
||||
const frameless = usePlatform((s) => s.framelessTitleBar);
|
||||
const [mainMenuStyle, setMainMenuStyle] = useSetting<"hamburger" | "merged">("appearance.mainMenuStyle");
|
||||
if (!frameless) return null;
|
||||
return (
|
||||
<Row label="Main menu">
|
||||
<Select
|
||||
value={mainMenuStyle ?? "hamburger"}
|
||||
options={[
|
||||
{ value: "hamburger", label: "Hamburger button" },
|
||||
{ value: "merged", label: "Merged with main toolbar" },
|
||||
]}
|
||||
onChange={setMainMenuStyle}
|
||||
aria-label="Main menu"
|
||||
/>
|
||||
</Row>
|
||||
);
|
||||
}
|
||||
|
||||
function UiOptionsGrid() {
|
||||
return (
|
||||
<Grid
|
||||
@@ -541,12 +521,6 @@ export const BEHAVIOR_PAGES: PageSpec[] = [
|
||||
{
|
||||
title: "UI Options",
|
||||
options: [
|
||||
{
|
||||
kind: "custom",
|
||||
label: "Main menu",
|
||||
keywords: ["Hamburger button", "Merged with main toolbar"],
|
||||
component: MainMenuStyleRow,
|
||||
},
|
||||
{
|
||||
kind: "custom",
|
||||
label: "Compact mode",
|
||||
|
||||
@@ -2189,9 +2189,6 @@
|
||||
"TypeScript version": "TypeScript 版本",
|
||||
"Types": "类型",
|
||||
"Typography Settings": "排版设置",
|
||||
"Main menu": "主菜单",
|
||||
"Hamburger button": "汉堡菜单按钮",
|
||||
"Merged with main toolbar": "与主工具栏合并",
|
||||
"UI Options": "UI 选项",
|
||||
"UI elements take up less screen space": "UI 元素占用更少的屏幕空间",
|
||||
"UI font": "UI 字体",
|
||||
|
||||
@@ -1,240 +0,0 @@
|
||||
import { act, fireEvent, render, screen, within } from "@testing-library/react";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { commands, type Settings } from "@/platform/ipc";
|
||||
import { useProject } from "@/platform/project";
|
||||
import { registerActions } from "@/platform/actions";
|
||||
import { MainMenu } from "./MainMenu";
|
||||
|
||||
let available = 500;
|
||||
let resize: () => void;
|
||||
const savedProject = useProject.getState();
|
||||
let dispose: () => void;
|
||||
const setStyle = (style: string) =>
|
||||
useProject.setState({ settings: { appearance: { mainMenuStyle: style } } as Settings });
|
||||
const alt = () => {
|
||||
fireEvent.keyDown(window, { key: "Alt", altKey: true });
|
||||
fireEvent.keyUp(window, { key: "Alt" });
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
available = 500;
|
||||
vi.stubGlobal(
|
||||
"ResizeObserver",
|
||||
class {
|
||||
constructor(private callback: () => void) {}
|
||||
observe(element: HTMLElement) {
|
||||
if (element.className.includes("mainMenuSlot")) resize = this.callback;
|
||||
}
|
||||
disconnect() {}
|
||||
},
|
||||
);
|
||||
vi.spyOn(HTMLElement.prototype, "clientWidth", "get").mockImplementation(() => available);
|
||||
vi.spyOn(HTMLElement.prototype, "scrollWidth", "get").mockReturnValue(300);
|
||||
vi.spyOn(commands, "menuModel").mockResolvedValue([
|
||||
{
|
||||
kind: "submenu",
|
||||
id: "file",
|
||||
label: "File",
|
||||
children: [{ kind: "action", id: "test.open", label: "Open", hint: null, arg: null }],
|
||||
},
|
||||
{
|
||||
kind: "submenu",
|
||||
id: "edit",
|
||||
label: "Edit",
|
||||
children: [{ kind: "action", id: "test.undo", label: "Undo", hint: null, arg: null }],
|
||||
},
|
||||
]);
|
||||
dispose = registerActions([
|
||||
{ id: "test.open", title: "Open", category: "Test", run: vi.fn() },
|
||||
{ id: "test.undo", title: "Undo", category: "Test", run: vi.fn() },
|
||||
]);
|
||||
setStyle("hamburger");
|
||||
});
|
||||
afterEach(() => {
|
||||
dispose();
|
||||
useProject.setState(savedProject);
|
||||
vi.restoreAllMocks();
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
const titles = () => within(screen.getByRole("menubar"));
|
||||
|
||||
describe("main menu presentation", () => {
|
||||
it("keeps the hamburger default, expanding on click and collapsing on Escape", async () => {
|
||||
const expanded = vi.fn();
|
||||
render(<MainMenu onExpandedChange={expanded} />);
|
||||
expect(screen.queryByRole("menubar")).toBeNull();
|
||||
fireEvent.click(screen.getByRole("button", { name: "Main Menu" }));
|
||||
expect(await screen.findByRole("menu", { name: "File" })).toBeInTheDocument();
|
||||
expect(expanded).toHaveBeenLastCalledWith(true);
|
||||
fireEvent.keyDown(document.activeElement!, { key: "ArrowRight" });
|
||||
expect(await screen.findByRole("menu", { name: "Edit" })).toBeInTheDocument();
|
||||
fireEvent.keyDown(document.activeElement!, { key: "Escape" });
|
||||
expect(screen.queryByRole("menubar")).toBeNull();
|
||||
expect(screen.getByRole("button", { name: "Main Menu" })).toBeInTheDocument();
|
||||
expect(expanded).toHaveBeenLastCalledWith(false);
|
||||
alt();
|
||||
expect(await screen.findByRole("menu", { name: "File" })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("shows merged titles, switches menus with arrows, and retains titles after dismissal or an action", async () => {
|
||||
setStyle("merged");
|
||||
const expanded = vi.fn();
|
||||
render(
|
||||
<>
|
||||
<MainMenu onExpandedChange={expanded} />
|
||||
<button>Toolbar control</button>
|
||||
</>,
|
||||
);
|
||||
await screen.findByRole("menubar");
|
||||
expect(screen.queryByRole("button", { name: "Main Menu" })).toBeNull();
|
||||
fireEvent.click(titles().getByRole("menuitem", { name: "File" }));
|
||||
expect(await screen.findByRole("menu", { name: "File" })).toBeInTheDocument();
|
||||
fireEvent.keyDown(document.activeElement!, { key: "ArrowRight" });
|
||||
expect(await screen.findByRole("menu", { name: "Edit" })).toBeInTheDocument();
|
||||
fireEvent.keyDown(document.activeElement!, { key: "ArrowLeft" });
|
||||
expect(await screen.findByRole("menu", { name: "File" })).toBeInTheDocument();
|
||||
fireEvent.keyDown(document.activeElement!, { key: "Escape" });
|
||||
expect(screen.queryByRole("menu")).toBeNull();
|
||||
expect(screen.getByRole("menubar")).toBeInTheDocument();
|
||||
fireEvent.click(titles().getByRole("menuitem", { name: "Edit" }));
|
||||
fireEvent.click(await screen.findByRole("menuitem", { name: "Undo" }));
|
||||
expect(screen.queryByRole("menu")).toBeNull();
|
||||
expect(screen.getByRole("menubar")).toBeInTheDocument();
|
||||
fireEvent.click(titles().getByRole("menuitem", { name: "File" }));
|
||||
fireEvent.pointerDown(screen.getByRole("button", { name: "Toolbar control" }));
|
||||
await act(() => new Promise((resolve) => setTimeout(resolve)));
|
||||
expect(screen.queryByRole("menu")).toBeNull();
|
||||
expect(expanded).not.toHaveBeenCalledWith(true);
|
||||
});
|
||||
|
||||
it.each(["hamburger", "merged"])(
|
||||
"%s switches on title clicks without an outside dismissal, then closes for toolbar clicks",
|
||||
async (style) => {
|
||||
setStyle(style);
|
||||
render(
|
||||
<>
|
||||
<MainMenu />
|
||||
<button>Toolbar control</button>
|
||||
</>,
|
||||
);
|
||||
if (style === "hamburger") fireEvent.click(screen.getByRole("button", { name: "Main Menu" }));
|
||||
await screen.findByRole("menubar");
|
||||
if (style === "merged") fireEvent.click(titles().getByRole("menuitem", { name: "File" }));
|
||||
const edit = titles().getByRole("menuitem", { name: "Edit" });
|
||||
fireEvent.pointerDown(edit);
|
||||
fireEvent.click(edit);
|
||||
await act(() => new Promise((resolve) => setTimeout(resolve)));
|
||||
expect(screen.getByRole("menu", { name: "Edit" })).toBeInTheDocument();
|
||||
fireEvent.pointerDown(screen.getByRole("button", { name: "Toolbar control" }));
|
||||
await act(() => new Promise((resolve) => setTimeout(resolve)));
|
||||
expect(screen.queryByRole("menu")).toBeNull();
|
||||
},
|
||||
);
|
||||
|
||||
it("focuses the merged titles with lone Alt and navigates before opening", async () => {
|
||||
setStyle("merged");
|
||||
render(
|
||||
<>
|
||||
<button>Editor</button>
|
||||
<MainMenu />
|
||||
</>,
|
||||
);
|
||||
await screen.findByRole("menubar");
|
||||
const editor = screen.getByRole("button", { name: "Editor" });
|
||||
editor.focus();
|
||||
alt();
|
||||
expect(titles().getByRole("menuitem", { name: "File" })).toHaveFocus();
|
||||
expect(screen.queryByRole("menu")).toBeNull();
|
||||
fireEvent.keyDown(document.activeElement!, { key: "ArrowLeft" });
|
||||
expect(titles().getByRole("menuitem", { name: "Edit" })).toHaveFocus();
|
||||
fireEvent.keyDown(document.activeElement!, { key: "ArrowDown" });
|
||||
expect(await screen.findByRole("menu", { name: "Edit" })).toBeInTheDocument();
|
||||
fireEvent.keyDown(document.activeElement!, { key: "Escape" });
|
||||
expect(titles().getByRole("menuitem", { name: "Edit" })).toHaveFocus();
|
||||
fireEvent.keyDown(document.activeElement!, { key: "Escape" });
|
||||
expect(editor).toHaveFocus();
|
||||
});
|
||||
|
||||
it("returns focus on a second lone Alt", async () => {
|
||||
setStyle("merged");
|
||||
render(
|
||||
<>
|
||||
<button>Editor</button>
|
||||
<MainMenu />
|
||||
</>,
|
||||
);
|
||||
await screen.findByRole("menubar");
|
||||
const editor = screen.getByRole("button", { name: "Editor" });
|
||||
editor.focus();
|
||||
alt();
|
||||
expect(titles().getByRole("menuitem", { name: "File" })).toHaveFocus();
|
||||
alt();
|
||||
expect(editor).toHaveFocus();
|
||||
alt();
|
||||
fireEvent.keyDown(document.activeElement!, { key: "Escape" });
|
||||
expect(editor).toHaveFocus();
|
||||
});
|
||||
|
||||
it("does not reopen a title's menu after the titles collapse and return", async () => {
|
||||
setStyle("merged");
|
||||
render(<MainMenu />);
|
||||
await screen.findByRole("menubar");
|
||||
fireEvent.click(titles().getByRole("menuitem", { name: "Edit" }));
|
||||
expect(await screen.findByRole("menu", { name: "Edit" })).toBeInTheDocument();
|
||||
act(() => {
|
||||
available = 100;
|
||||
resize();
|
||||
});
|
||||
expect(screen.queryByRole("menu")).toBeNull();
|
||||
act(() => {
|
||||
available = 500;
|
||||
resize();
|
||||
});
|
||||
expect(screen.getByRole("menubar")).toBeInTheDocument();
|
||||
expect(screen.queryByRole("menu")).toBeNull();
|
||||
});
|
||||
|
||||
it("falls back to a hamburger popup when narrow and restores titles when space returns", async () => {
|
||||
available = 100;
|
||||
setStyle("merged");
|
||||
const expanded = vi.fn();
|
||||
render(<MainMenu onExpandedChange={expanded} />);
|
||||
await act(async () => {});
|
||||
expect(screen.queryByRole("menubar")).toBeNull();
|
||||
fireEvent.click(screen.getByRole("button", { name: "Main Menu" }));
|
||||
const root = await screen.findByRole("menu", { name: "Main Menu" });
|
||||
fireEvent.click(within(root).getByRole("menuitem", { name: /File/ }));
|
||||
expect(await screen.findByRole("menuitem", { name: "Open" })).toBeInTheDocument();
|
||||
fireEvent.click(screen.getByRole("menuitem", { name: "Open" }));
|
||||
expect(screen.getByRole("button", { name: "Main Menu" })).toBeInTheDocument();
|
||||
expect(expanded).not.toHaveBeenCalledWith(true);
|
||||
act(() => {
|
||||
available = 500;
|
||||
resize();
|
||||
});
|
||||
expect(screen.getByRole("menubar")).toBeInTheDocument();
|
||||
expect(screen.queryByRole("button", { name: "Main Menu" })).toBeNull();
|
||||
act(() => {
|
||||
available = 100;
|
||||
resize();
|
||||
});
|
||||
expect(screen.queryByRole("menubar")).toBeNull();
|
||||
expect(screen.getByRole("button", { name: "Main Menu" })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("applies the setting immediately and restores toolbar visibility when switching from an expanded hamburger", async () => {
|
||||
const expanded = vi.fn();
|
||||
render(<MainMenu onExpandedChange={expanded} />);
|
||||
fireEvent.click(screen.getByRole("button", { name: "Main Menu" }));
|
||||
await screen.findByRole("menu");
|
||||
expect(expanded).toHaveBeenLastCalledWith(true);
|
||||
act(() => setStyle("merged"));
|
||||
await screen.findByRole("menubar");
|
||||
expect(screen.queryByRole("menu")).toBeNull();
|
||||
expect(expanded).toHaveBeenLastCalledWith(false);
|
||||
act(() => setStyle("hamburger"));
|
||||
expect(screen.queryByRole("menubar")).toBeNull();
|
||||
expect(screen.getByRole("button", { name: "Main Menu" })).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
@@ -1,8 +1,13 @@
|
||||
/** Main menu for the frameless title bar, with hamburger and persistent title modes. */
|
||||
import { useEffect, useLayoutEffect, useRef, useState, type KeyboardEvent } from "react";
|
||||
/**
|
||||
* Frameless title bar (Windows, Linux): IDEA's main menu button. Clicking ☰ expands the menu bar
|
||||
* in place (File Edit View …) with File open; hovering another title switches
|
||||
* menus, ←/→ walk them, and closing the menu collapses the bar again. The
|
||||
* entries come from the same spec as the native menu (`menu_model`). As in
|
||||
* Windows apps and IDEA, pressing and releasing Alt alone opens it too.
|
||||
*/
|
||||
import { useEffect, useRef, useState, type KeyboardEvent } from "react";
|
||||
import clsx from "clsx";
|
||||
import { useProject } from "@/platform/project";
|
||||
import { lookup, t, useI18n } from "@/platform/i18n";
|
||||
import { lookup, t } from "@/platform/i18n";
|
||||
import { commands, type MenuNode } from "@/platform/ipc";
|
||||
import { usePlatform } from "@/platform/platformInfo";
|
||||
import { Icon, MenuList, Popup, type MenuItem } from "@/ui";
|
||||
@@ -65,172 +70,77 @@ export function onLoneAlt(target: Window, onAlt: () => void): () => void {
|
||||
};
|
||||
}
|
||||
|
||||
async function loadMenus(): Promise<Submenu[]> {
|
||||
const model: MenuNode[] = await commands.menuModel().catch(() => []);
|
||||
return model.filter(isSubmenu);
|
||||
}
|
||||
|
||||
type MainMenuProps = { onExpandedChange?: (expanded: boolean) => void };
|
||||
|
||||
export function MainMenu(props: MainMenuProps) {
|
||||
const merged = useProject((s) => s.settings?.appearance?.mainMenuStyle === "merged");
|
||||
return <MainMenuBar key={String(merged)} merged={merged} {...props} />;
|
||||
}
|
||||
|
||||
function MainMenuBar({ merged, onExpandedChange }: MainMenuProps & { merged: boolean }) {
|
||||
useI18n((s) => s.locale);
|
||||
const [menus, setMenus] = useState<Submenu[]>([]);
|
||||
const [expanded, setExpanded] = useState(false);
|
||||
export function MainMenu({ onExpandedChange }: { onExpandedChange?: (expanded: boolean) => void }) {
|
||||
const [menus, setMenus] = useState<Submenu[] | null>(null);
|
||||
const [open, setOpen] = useState<number | null>(null);
|
||||
const [focused, setFocused] = useState(0);
|
||||
const [bar, setBar] = useState<HTMLDivElement | null>(null);
|
||||
const slot = useRef<HTMLDivElement>(null);
|
||||
const measure = useRef<HTMLDivElement>(null);
|
||||
const hamburger = useRef<HTMLButtonElement>(null);
|
||||
const previousFocus = useRef<HTMLElement | null>(null);
|
||||
const [fits, setFits] = useState(false);
|
||||
// A press on another title closes the open menu first (outside click); keep the bar expanded.
|
||||
const pressedTitle = useRef(false);
|
||||
const persistent = merged && fits;
|
||||
const showTitles = persistent || (!merged && expanded);
|
||||
|
||||
useEffect(() => {
|
||||
let disposed = false;
|
||||
if (merged)
|
||||
void loadMenus().then((top) => {
|
||||
if (!disposed) setMenus(top);
|
||||
});
|
||||
return () => {
|
||||
disposed = true;
|
||||
};
|
||||
}, [merged]);
|
||||
useEffect(() => {
|
||||
if (!merged || open === null) return;
|
||||
let disposed = false;
|
||||
void loadMenus().then((top) => {
|
||||
if (!disposed && top.length) setMenus(top);
|
||||
});
|
||||
return () => {
|
||||
disposed = true;
|
||||
};
|
||||
}, [merged, open]);
|
||||
useEffect(() => {
|
||||
onExpandedChange?.(!merged && expanded);
|
||||
return () => onExpandedChange?.(false);
|
||||
}, [merged, expanded, onExpandedChange]);
|
||||
|
||||
// The invisible copy keeps the natural flex basis even while the slot shrinks.
|
||||
// Measuring the allocated slot includes all neighbouring toolbar/caption controls.
|
||||
useLayoutEffect(() => {
|
||||
if (!merged || !slot.current || !measure.current) return;
|
||||
const update = () => {
|
||||
const next = menus.length > 0 && slot.current!.clientWidth >= measure.current!.scrollWidth;
|
||||
setFits(next);
|
||||
// Each presentation's menu closes with it, and must not reopen when it returns.
|
||||
if (next) setExpanded(false);
|
||||
else setOpen(null);
|
||||
};
|
||||
const observer = new ResizeObserver(update);
|
||||
observer.observe(slot.current);
|
||||
observer.observe(measure.current);
|
||||
update();
|
||||
return () => observer.disconnect();
|
||||
}, [merged, menus]);
|
||||
|
||||
const close = () => {
|
||||
setOpen(null);
|
||||
setExpanded(false);
|
||||
};
|
||||
const expand = async () => {
|
||||
const top = await loadMenus();
|
||||
const model: MenuNode[] = await commands.menuModel().catch(() => []);
|
||||
const top = model.filter(isSubmenu);
|
||||
if (!top.length) return;
|
||||
setMenus(top);
|
||||
setExpanded(true);
|
||||
setOpen(0);
|
||||
onExpandedChange?.(true);
|
||||
};
|
||||
const focusTitle = (i: number) => {
|
||||
setFocused(i);
|
||||
(bar?.children[i] as HTMLElement | undefined)?.focus();
|
||||
const collapse = () => {
|
||||
setOpen(null);
|
||||
setMenus(null);
|
||||
onExpandedChange?.(false);
|
||||
};
|
||||
const onMenuClose = (reason?: string) => {
|
||||
if (reason !== "outside") {
|
||||
if (reason === "escape" && persistent) focusTitle(open ?? focused);
|
||||
return close();
|
||||
}
|
||||
if (reason !== "outside") return collapse();
|
||||
setTimeout(() => {
|
||||
if (pressedTitle.current) pressedTitle.current = false;
|
||||
else close();
|
||||
else collapse();
|
||||
});
|
||||
};
|
||||
useEffect(() =>
|
||||
onLoneAlt(window, () => {
|
||||
if (open !== null || expanded) {
|
||||
close();
|
||||
if (persistent) focusTitle(focused);
|
||||
} else if (persistent) {
|
||||
if (bar?.contains(document.activeElement)) previousFocus.current?.focus();
|
||||
else {
|
||||
previousFocus.current = document.activeElement as HTMLElement;
|
||||
focusTitle(0);
|
||||
}
|
||||
} else void expand();
|
||||
}),
|
||||
);
|
||||
useEffect(() => onLoneAlt(window, () => (menus ? collapse() : void expand())));
|
||||
const step = (delta: number) => {
|
||||
if (!menus || open === null) return;
|
||||
setOpen((open + delta + menus.length) % menus.length);
|
||||
};
|
||||
// ←/→ not used by the menu (top level, or a leaf item) move to the neighbouring menu.
|
||||
const onKeyDown = (e: KeyboardEvent) => {
|
||||
if (e.key === "ArrowLeft" || e.key === "ArrowRight") {
|
||||
if (!menus.length) return;
|
||||
e.preventDefault();
|
||||
const next = ((open ?? focused) + (e.key === "ArrowRight" ? 1 : -1) + menus.length) % menus.length;
|
||||
if (open !== null) setOpen(next);
|
||||
else focusTitle(next);
|
||||
} else if (e.key === "ArrowDown" && open === null) {
|
||||
e.preventDefault();
|
||||
setOpen(focused);
|
||||
} else if (e.key === "Escape" && open === null) {
|
||||
close();
|
||||
previousFocus.current?.focus();
|
||||
}
|
||||
if (e.key !== "ArrowLeft" && e.key !== "ArrowRight") return;
|
||||
e.preventDefault();
|
||||
step(e.key === "ArrowRight" ? 1 : -1);
|
||||
};
|
||||
|
||||
const button = (
|
||||
<button
|
||||
ref={hamburger}
|
||||
type="button"
|
||||
className={styles.mainMenuButton}
|
||||
aria-label={t("menu.mainMenu")}
|
||||
title={t("menu.mainMenu")}
|
||||
aria-haspopup="menu"
|
||||
aria-expanded={expanded}
|
||||
onClick={() => {
|
||||
if (expanded) close();
|
||||
else void expand();
|
||||
}}
|
||||
>
|
||||
<Icon name="main-menu" size={16} />
|
||||
</button>
|
||||
);
|
||||
const titles = (
|
||||
<div ref={setBar} role="menubar" className={styles.menuBar} onKeyDown={onKeyDown}>
|
||||
if (!menus)
|
||||
return (
|
||||
<button
|
||||
type="button"
|
||||
className={styles.mainMenuButton}
|
||||
aria-label={t("menu.mainMenu")}
|
||||
title={t("menu.mainMenu")}
|
||||
onClick={() => void expand()}
|
||||
>
|
||||
<Icon name="main-menu" size={16} />
|
||||
</button>
|
||||
);
|
||||
|
||||
return (
|
||||
<div ref={setBar} role="menubar" className={styles.menuBar}>
|
||||
{menus.map((m, i) => (
|
||||
<button
|
||||
key={m.id}
|
||||
type="button"
|
||||
role="menuitem"
|
||||
tabIndex={focused === i ? 0 : -1}
|
||||
aria-haspopup="menu"
|
||||
aria-expanded={open === i}
|
||||
className={clsx(styles.menuTitle, open === i && styles.menuTitleOpen)}
|
||||
onFocus={() => setFocused(i)}
|
||||
onPointerDown={() => {
|
||||
pressedTitle.current = true;
|
||||
setTimeout(() => {
|
||||
pressedTitle.current = false;
|
||||
});
|
||||
}}
|
||||
onPointerEnter={() => {
|
||||
if (open !== null) setOpen(i);
|
||||
}}
|
||||
onClick={() => {
|
||||
if (open === i) close();
|
||||
pressedTitle.current = false;
|
||||
if (open === i) collapse();
|
||||
else setOpen(i);
|
||||
}}
|
||||
>
|
||||
@@ -246,41 +156,16 @@ function MainMenuBar({ merged, onExpandedChange }: MainMenuProps & { merged: boo
|
||||
offset={2}
|
||||
role="presentation"
|
||||
autoFocus={false}
|
||||
restoreFocus={!persistent}
|
||||
onClose={onMenuClose}
|
||||
className={menuStyles.popup}
|
||||
>
|
||||
<MenuList
|
||||
items={menuItems(menus[open].children)}
|
||||
onDone={close}
|
||||
aria-label={label(menus[open].id, menus[open].label)}
|
||||
/>
|
||||
</Popup>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
if (!merged) return showTitles ? titles : button;
|
||||
return (
|
||||
<div ref={slot} className={styles.mainMenuSlot}>
|
||||
<div ref={measure} className={clsx(styles.menuBar, styles.menuMeasure)} aria-hidden="true" inert>
|
||||
{menus.map((m) => (
|
||||
<span key={m.id} className={styles.menuTitle}>
|
||||
{label(m.id, m.label)}
|
||||
</span>
|
||||
))}
|
||||
</div>
|
||||
<div className={styles.menuPresentation}>{showTitles ? titles : button}</div>
|
||||
{!showTitles && expanded && (
|
||||
<Popup
|
||||
open
|
||||
anchor={hamburger}
|
||||
onClose={close}
|
||||
placement="bottom-start"
|
||||
role="presentation"
|
||||
autoFocus={false}
|
||||
className={menuStyles.popup}
|
||||
>
|
||||
<MenuList items={menuItems(menus)} onDone={close} aria-label={t("menu.mainMenu")} />
|
||||
<div onKeyDown={onKeyDown}>
|
||||
<MenuList
|
||||
items={menuItems(menus[open].children)}
|
||||
onDone={collapse}
|
||||
aria-label={label(menus[open].id, menus[open].label)}
|
||||
/>
|
||||
</div>
|
||||
</Popup>
|
||||
)}
|
||||
</div>
|
||||
|
||||
@@ -125,32 +125,7 @@
|
||||
outline: 2px solid var(--focus-ring);
|
||||
outline-offset: -2px;
|
||||
}
|
||||
/* Preserve the menu's natural width while allowing its slot to yield to toolbar controls. */
|
||||
.mainMenuSlot {
|
||||
position: relative;
|
||||
flex: 0 1 auto;
|
||||
min-width: 34px;
|
||||
height: 30px;
|
||||
overflow: hidden;
|
||||
}
|
||||
.mainMenuSlot ~ .item,
|
||||
.mainMenuSlot ~ .tools {
|
||||
flex-shrink: 0;
|
||||
}
|
||||
.menuMeasure {
|
||||
width: max-content;
|
||||
visibility: hidden;
|
||||
pointer-events: none;
|
||||
}
|
||||
.menuPresentation {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
height: 100%;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
/* The main menu titles. */
|
||||
/* The expanded main menu: File Edit View … in place of the project widgets. */
|
||||
.menuBar {
|
||||
flex: none;
|
||||
display: flex;
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
vi.mock("./WindowControls", () => ({ WindowControls: () => null }));
|
||||
import { act, fireEvent, render, screen } from "@testing-library/react";
|
||||
import { fireEvent, render, screen } from "@testing-library/react";
|
||||
import { registerActions } from "@/platform/actions";
|
||||
import { commands, type Settings } from "@/platform/ipc";
|
||||
import { useProject } from "@/platform/project";
|
||||
import { usePlatform } from "@/platform/platformInfo";
|
||||
import { useThemeOverride } from "./theme";
|
||||
import { customizeToolbarItems, gearMenuItems, TitleBar } from "./TitleBar";
|
||||
@@ -105,32 +103,3 @@ describe("title bar in Distraction Free Mode (bare)", () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("merged title bar menu", () => {
|
||||
it.each(["windows", "linux", "macos"])(
|
||||
"%s keeps toolbar controls available and uses the menu only when frameless",
|
||||
async (os) => {
|
||||
const savedPlatform = usePlatform.getState();
|
||||
const savedProject = useProject.getState();
|
||||
const model = vi
|
||||
.spyOn(commands, "menuModel")
|
||||
.mockResolvedValue([{ kind: "submenu", id: "file", label: "File", children: [] }]);
|
||||
usePlatform.setState({ os, framelessTitleBar: os !== "macos", overlayTitleBar: os === "macos" });
|
||||
useProject.setState({ settings: { appearance: { mainMenuStyle: "merged" } } as Settings });
|
||||
try {
|
||||
render(<TitleBar />);
|
||||
await act(async () => {});
|
||||
expect(!!screen.queryByRole("menubar")).toBe(os !== "macos");
|
||||
expect(screen.queryByRole("button", { name: "Main Menu" })).toBeNull();
|
||||
if (os !== "macos") fireEvent.click(screen.getByRole("menuitem", { name: "File" }));
|
||||
expect(screen.getByRole("button", { name: "OpenIDEA" })).toBeInTheDocument();
|
||||
fireEvent.click(screen.getByRole("button", { name: "IDE and Project Settings" }));
|
||||
expect(await screen.findByRole("menu", { name: "IDE and Project Settings" })).toBeInTheDocument();
|
||||
} finally {
|
||||
usePlatform.setState(savedPlatform);
|
||||
useProject.setState(savedProject);
|
||||
model.mockRestore();
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
Reference in new issue
Заблокувати користувача