diff --git a/crates/ui-egui/src/panels/dock.rs b/crates/ui-egui/src/panels/dock.rs index b620334408..267125d603 100644 --- a/crates/ui-egui/src/panels/dock.rs +++ b/crates/ui-egui/src/panels/dock.rs @@ -32,10 +32,19 @@ pub fn dock_panel(app: &mut PhotocraftApp, ui: &mut egui::Ui) { let rw = if rail_shown { rail_w } else { 0.0 }; let content = ui.available_rect_before_wrap(); let range = crate::panels::dock_width_range(content.width()); - let (min_dock, max_dock) = (*range.start(), *range.end()); + // The dock must leave the toolbox and the canvas's 200-point minimum room: the standard + // layout enforces the canvas minimum inside craft-ui, the fixed-size dock-edge panel does not. + let toolbar_w = { + let (w1, bx) = if t.pro { (40.0, 30.0) } else { (50.0, 36.0) }; + if app.ui.panels.toolbar_double { w1 + bx + 2.0 } else { w1 } + }; + const MIN_CANVAS_WIDTH: f32 = 200.0; + let max_dock = (*range.end()).min((content.width() - toolbar_w - MIN_CANVAS_WIDTH - rw).max(1.0)); + let min_dock = (*range.start()).min(max_dock); let locked = app.session.prefs().workspace_locked; let modal = crate::panel_docking::modal_open(app, ui.ctx()); - let empty = crate::panel_docking::dock_is_empty(app); + let metrics = crate::panel_docking::dock_metrics(app); + let empty = metrics.empty; if empty { // Nothing to resize or draw in the dock column, but Studio draws its floating Properties // group from the docking pass: keep that pass, on the full remaining area, with no column. @@ -53,27 +62,24 @@ pub fn dock_panel(app: &mut PhotocraftApp, ui: &mut egui::Ui) { } // The dock became empty: a drag start from before this transition can never finish. crate::panels::clear_dock_edge_drag(ui.ctx()); - crate::panel_docking::show_dock(app, ui); + // A child that does not consume the parent's layout space: the docking pass still draws + // floating windows, but the toolbox and canvas keep their room. + let mut dock_ui = ui.new_child(egui::UiBuilder::new().max_rect(ui.max_rect())); + crate::panel_docking::show_dock(app, &mut dock_ui); crate::dock::persist(app, ui.ctx()); crate::color_panel_ui::persist(app); return; } // An iconic-only dock keeps its narrow displayed width instead of being raised to the expanded // minimum; other docks keep the usual minimum and maximum. - let iconic = crate::panel_docking::dock_iconic_only(app); + let iconic = metrics.iconic_only; let min_visible = if iconic { 1.0 } else { min_dock }; - // An empty dock takes no width: the standard layout gives it 0 rather than a blank column. - let mut stored = if empty { - 0.0 - } else { - let width = crate::panel_docking::dock_width(app); - if iconic { width.clamp(1.0, max_dock) } else { width.clamp(min_dock, max_dock) } - }; + let mut stored = if iconic { metrics.width.clamp(1.0, max_dock) } else { metrics.width.clamp(min_dock, max_dock) }; let drag_id = egui::Id::unique("dock-edge-drag"); // Resizing is unavailable while locked, modal, or when the parent UI is disabled (screen // picking disables it). A disabled handle never receives drag_stopped, so drop any stored // start here; otherwise it blocks persistence and can apply to a later pointer. - let resizable = !empty && !locked && !modal && ui.is_enabled(); + let resizable = !locked && !modal && ui.is_enabled(); let mut drag = if resizable { ui.ctx().data(|d| d.get_temp::<(f32, f32)>(drag_id)) } else { @@ -95,9 +101,7 @@ pub fn dock_panel(app: &mut PhotocraftApp, ui: &mut egui::Ui) { drag = None; ui.ctx().data_mut(|d| d.remove::<(f32, f32)>(drag_id)); } - let panel_w = if empty { - rw - } else if let Some((start_x, start_w)) = drag { + let panel_w = if let Some((start_x, start_w)) = drag { let pointer = ui.ctx().input(|i| i.pointer.latest_pos()).map_or(start_x, |pos| pos.x); let dx = if crate::direction::is_rtl() { pointer - start_x } else { start_x - pointer }; (start_w + dx).clamp(min_visible + rw, max_dock + rw) @@ -138,14 +142,13 @@ pub fn dock_panel(app: &mut PhotocraftApp, ui: &mut egui::Ui) { // The docking pass also renders floating windows (Studio floats Properties over the // canvas), so it runs even when the dock column itself is empty. crate::panel_docking::show_dock(app, &mut dock_ui); - if !empty { - // Block the docking layout's column-edge handle at the dock's content edge. It is - // registered before the resize handle so the handle wins where they overlap when - // the rail is hidden and both edges coincide. - let dock_edge = if crate::direction::is_rtl() { dock_rect.right() } else { dock_rect.left() }; - let cover = egui::Rect::from_min_max(egui::pos2(dock_edge - 2.0, dock_rect.top()), egui::pos2(dock_edge + 2.0, dock_rect.bottom())); - let _ = ui.interact(cover, egui::Id::unique("dock-content-edge"), egui::Sense::click_and_drag()); - } + // Block the docking layout's column-edge handle at the dock's content edge. It is + // registered before the resize handle so the handle wins where they overlap when the + // rail is hidden and both edges coincide; one point either side keeps tab strips and + // scrollbars clear. + let dock_edge = if crate::direction::is_rtl() { dock_rect.right() } else { dock_rect.left() }; + let cover = egui::Rect::from_min_max(egui::pos2(dock_edge - 1.0, dock_rect.top()), egui::pos2(dock_edge + 1.0, dock_rect.bottom())); + let _ = ui.interact(cover, egui::Id::unique("dock-content-edge"), egui::Sense::click_and_drag()); // The panel's inner edge (between the toolbox and the rail) is the resize handle. let panel_rect = ui.max_rect(); let panel_edge = if crate::direction::is_rtl() { panel_rect.right() } else { panel_rect.left() }; @@ -166,7 +169,9 @@ pub fn dock_panel(app: &mut PhotocraftApp, ui: &mut egui::Ui) { let color = if active { t.accent } else { t.separator }; ui.ctx().layer_painter(line).vline(panel_edge, panel_rect.y_range(), Stroke::new(1.0, color)); if response.drag_started() { - let start_x = response.interact_pointer_pos().map_or(panel_edge, |pos| pos.x); + // The press origin, not the threshold-crossing frame: the dock then tracks the + // pointer exactly from where the user pressed. + let start_x = ui.ctx().input(|i| i.pointer.press_origin()).map_or(panel_edge, |pos| pos.x); ui.ctx().data_mut(|d| d.insert_temp(drag_id, (start_x, panel_w))); } if response.dragged() { diff --git a/crates/ui-egui/src/panels/panel_docking.rs b/crates/ui-egui/src/panels/panel_docking.rs index acb0d20b1e..09a70e33ad 100644 --- a/crates/ui-egui/src/panels/panel_docking.rs +++ b/crates/ui-egui/src/panels/panel_docking.rs @@ -91,7 +91,8 @@ fn default_width(pro: bool) -> f32 { } /// The end dock of `layout`: follow only the second child of horizontal splits, as craft-ui -/// keeps a side's dock at its window edge. `None` when the layout has no end dock. +/// keeps a side's dock at its window edge; this app builds that dock as `Side::End`. `None` when +/// the layout has no end dock. fn end_dock_node(node: &Node) -> Option<&Node> { match node { Node::Dock { .. } => Some(node), @@ -100,28 +101,40 @@ fn end_dock_node(node: &Node) -> Option<&Node> { } } -/// True when the end dock holds no panels at all (every panel is closed): the dock column should -/// then take no width, as the standard layout's empty dock does. -pub(crate) fn dock_is_empty(app: &PhotocraftApp) -> bool { +/// The end dock's layout facts, computed from one layout snapshot per frame. +pub(crate) struct DockMetrics { + pub(crate) empty: bool, + pub(crate) iconic_only: bool, + pub(crate) width: f32, +} + +/// The end dock's displayed width, whether it has no panels, and whether every column is iconic. +/// A layout built from the native dock is materialized once here rather than in each helper. +pub(crate) fn dock_metrics(app: &PhotocraftApp) -> DockMetrics { let layout = match &app.ui.docking { Some(workspace) => workspace.layout.clone(), None => legacy(app), }; match layout.root.as_ref().and_then(end_dock_node) { - Some(Node::Dock { columns, .. }) => columns.iter().all(|column| column.groups.iter().all(|group| group.panels.is_empty())), - // No end dock at the edge: there is no dock column to show, which is empty for our purpose. - _ => true, + Some(Node::Dock { columns, .. }) if !columns.is_empty() => DockMetrics { + empty: columns.iter().all(|column| column.groups.iter().all(|group| group.panels.is_empty())), + iconic_only: columns.iter().all(|column| column.iconic), + width: columns.iter().map(display_width).sum(), + }, + _ => DockMetrics { empty: true, iconic_only: false, width: default_width(app.ui.theme.is_pro()) }, } } -/// The end dock's stored width (all its columns), or the native default when the layout is -/// built from the native dock. +/// The end dock's displayed width (tests). +#[cfg(test)] pub(crate) fn dock_width(app: &PhotocraftApp) -> f32 { - let width = app.ui.docking.as_ref().and_then(|workspace| match workspace.layout.root.as_ref().and_then(end_dock_node) { - Some(Node::Dock { columns, .. }) if !columns.is_empty() => Some(columns.iter().map(display_width).sum::()), - _ => None, - }); - width.unwrap_or_else(|| default_width(app.ui.theme.is_pro())) + dock_metrics(app).width +} + +/// True when the end dock holds no panels at all (tests). +#[cfg(test)] +pub(crate) fn dock_is_empty(app: &PhotocraftApp) -> bool { + dock_metrics(app).empty } /// A column's width as craft-ui displays it: `iconic_width` while collapsed to icons. @@ -129,6 +142,18 @@ fn display_width(column: &Column) -> f32 { if column.iconic { column.iconic_width.unwrap_or(column.width) } else { column.width } } +/// The least displayed width of a column: the app's 220-point panel minimum, unless the column is +/// iconic (its icons fit in the narrow width) or has no panels. +const MIN_COLUMN_WIDTH: f32 = 220.0; + +fn column_minimum(column: &Column) -> f32 { + if column.iconic || column.groups.iter().all(|group| group.panels.is_empty()) { 1.0 } else { MIN_COLUMN_WIDTH } +} + +fn columns_minimum(columns: &[Column]) -> f32 { + columns.iter().map(column_minimum).sum() +} + /// Sets a column's displayed width. An iconic column keeps its expanded width for when the user /// expands it again; only `iconic_width` changes. fn set_display_width(column: &mut Column, width: f32) { @@ -140,44 +165,30 @@ fn set_display_width(column: &mut Column, width: f32) { } } -/// True when every end-dock column is collapsed to icons: such a dock takes its iconic width even -/// below the expanded minimum. -pub(crate) fn dock_iconic_only(app: &PhotocraftApp) -> bool { - let layout = match &app.ui.docking { - Some(workspace) => workspace.layout.clone(), - None => legacy(app), - }; - match layout.root.as_ref().and_then(end_dock_node) { - Some(Node::Dock { columns, .. }) => !columns.is_empty() && columns.iter().all(|column| column.iconic), - _ => false, - } -} - -/// Scales every column so their displayed widths add up to `total`, each at least one point. -/// Returns the resulting displayed total (rounding can differ by a point per column). +/// Scales every column so their displayed widths add up to `total`, respecting the per-column +/// panel minimum. Returns the resulting displayed total (rounding can differ by a point). fn scale_columns(columns: &mut [Column], total: f32) -> f32 { if columns.is_empty() { return 0.0; } let current: f32 = columns.iter().map(display_width).sum(); - let target = total.max(columns.len() as f32).max(1.0); + let minimum: f32 = columns_minimum(columns); + let target = total.max(minimum).max(1.0); if current > 0.0 && (current - target).abs() < 0.5 { return current; } - if current <= 0.0 { - let even = target / columns.len() as f32; - for column in columns.iter_mut() { - set_display_width(column, even); - } - } else { - let scale = target / current; - for column in columns.iter_mut() { - set_display_width(column, display_width(column) * scale); - } + // Every column keeps its minimum; the rest of the target is shared in proportion to each + // column's width above its minimum, so the total lands on the request. + let extra = (target - minimum).max(0.0); + let flexible: f32 = columns.iter().map(|column| (display_width(column) - column_minimum(column)).max(0.0)).sum(); + let count = columns.len() as f32; + for column in columns.iter_mut() { + let base = column_minimum(column); + let width = if flexible > 0.0 { base + extra * (display_width(column) - base).max(0.0) / flexible } else { base + extra / count }; + set_display_width(column, width); } columns.iter().map(display_width).sum() } - /// The end dock's columns, following the same horizontal second-child path as [`end_dock_node`]. fn dock_columns_mut(node: &mut Node) -> Option<&mut Vec>> { match node { @@ -936,7 +947,7 @@ pub(crate) fn set_dock_width(app: &mut PhotocraftApp, width: f32) { // A request for the width the dock already has must stay a no-op: committing the legacy // layout here would turn a later `ui.set {dockTabs}` into a customized-workspace update. let current: f32 = columns.iter().map(display_width).sum(); - if (current - width).abs() < 0.5 { + if current >= columns_minimum(columns) && (current - width).abs() < 0.5 { return; } let actual = scale_columns(columns, width); diff --git a/crates/ui-egui/src/panels/panel_docking/tests.rs b/crates/ui-egui/src/panels/panel_docking/tests.rs index 51f2c1c619..ad4e1a5592 100644 --- a/crates/ui-egui/src/panels/panel_docking/tests.rs +++ b/crates/ui-egui/src/panels/panel_docking/tests.rs @@ -1270,9 +1270,9 @@ fn ui_set_dock_width_scales_every_column() { } }; let before = total(&app); - set_dock_width(&mut app, 420.0); + set_dock_width(&mut app, 520.0); let after = total(&app); - assert!((after - 420.0).abs() < 1.0, "the whole dock should be 420 points, got {after} (was {before})"); + assert!((after - 520.0).abs() < 1.0, "the whole dock should be 520 points, got {after} (was {before})"); let widths: Vec = { let root = app.ui.docking.as_ref().unwrap().layout.root.as_ref().unwrap(); let dock = match root { @@ -1287,11 +1287,11 @@ fn ui_set_dock_width_scales_every_column() { assert_eq!(widths.len(), 2, "both columns are kept"); assert!(widths.iter().all(|width| *width > 0.0), "every column keeps a positive width: {widths:?}"); - // A target smaller than the other column's expanded width must scale every column, not starve - // the first one while leaving the second untouched. + // A target below the sum of the columns' 220-point panel minima must stop at that minimum, + // not starve a column while leaving the other untouched. set_dock_width(&mut app, 250.0); let after = total(&app); - assert!((after - 250.0).abs() < 1.0, "the whole dock should be 250 points, got {after}"); + assert!((after - 440.0).abs() < 1.0, "two columns keep the 440-point minimum, got {after}"); let widths: Vec = { let root = app.ui.docking.as_ref().unwrap().layout.root.as_ref().unwrap(); let dock = match root { @@ -1303,8 +1303,8 @@ fn ui_set_dock_width_scales_every_column() { _ => Vec::new(), } }; - assert!(widths.iter().all(|width| *width < 250.0), "every column is scaled below the total: {widths:?}"); - assert!(widths[1] < 180.0, "the second column must scale too: {widths:?}"); + assert!(widths.iter().all(|width| *width >= 219.5), "every column keeps the panel minimum: {widths:?}"); + assert!(widths.iter().all(|width| *width < 221.0), "the minimum is shared between the columns: {widths:?}"); } /// `ui.set {dockWidth}` travels through the queued request and the rendered dock pass, scaling a @@ -1326,7 +1326,7 @@ fn ui_set_dock_width_queues_and_scales_every_column_when_drawn() { } app.ui.docking = Some(Workspace::new(layout)); let mut h = dock_panel_harness(app, vec2(1280.0, 800.0), crate::theme::ThemeKind::ProMedium); - crate::panels::request_dock_width(&h.ctx, 420.0); + crate::panels::request_dock_width(&h.ctx, 520.0); h.run_steps(2); let root = h.state().ui.docking.as_ref().expect("a workspace").layout.root.as_ref().expect("root"); let dock = match root { @@ -1338,7 +1338,69 @@ fn ui_set_dock_width_queues_and_scales_every_column_when_drawn() { assert_eq!(widths.len(), 2, "both columns are kept"); assert!(widths.iter().all(|width| *width > 0.0), "every column keeps a positive width: {widths:?}"); let total: f32 = widths.iter().sum(); - assert!((total - 420.0).abs() < 1.0, "the queued request sets the whole dock to 420 points, got {total} from {widths:?}"); + assert!((total - 520.0).abs() < 1.0, "the queued request sets the whole dock to 520 points, got {total} from {widths:?}"); +} + +/// A narrow window must still give the canvas its 200-point minimum; the dock shrinks instead. +#[test] +fn a_narrow_window_keeps_the_canvas_minimum() { + let mut app = app(); + let mut layout = legacy(&app); + { + let root = layout.root.as_mut().expect("the legacy layout has a root"); + let dock = match root { + Node::Split { second, .. } => second.as_mut(), + _ => root, + }; + let Node::Dock { columns, .. } = dock else { panic!("the end dock is missing") }; + columns[0].width = 520.0; + } + app.ui.docking = Some(Workspace::new(layout)); + app.ui.panels.rail = true; + let h = dock_panel_harness(app, vec2(700.0, 600.0), crate::theme::ThemeKind::ProMedium); + let panel = crate::panels::dock_panel_width(&h.ctx); + let (w1, bx) = if h.state().ui.theme.is_pro() { (40.0, 30.0) } else { (50.0, 36.0) }; + let toolbar = if h.state().ui.panels.toolbar_double { w1 + bx + 2.0 } else { w1 }; + let canvas = 700.0 - panel - toolbar; + assert!(canvas >= 199.0, "the canvas keeps its 200-point minimum, got {canvas} (dock {panel}, toolbar {toolbar})"); +} + +/// The empty dock still renders its docking pass but must not consume the parent layout, so the +/// toolbar and canvas keep their space. +#[test] +fn an_empty_dock_keeps_the_parent_layout_for_toolbar_and_canvas() { + let mut app = app(); + let layout = Layout { + root: Some(Node::Split { + axis: SplitAxis::Horizontal, + size: SplitSize::FixedSecond(0.0), + first: Box::new(Node::Tabs { panels: vec![CANVAS.to_owned()], active: 0 }), + second: Box::new(Node::Dock { side: Side::End, columns: Vec::new() }), + }), + floating: Vec::new(), + }; + app.ui.docking = Some(Workspace::new(layout)); + app.ui.panels.rail = false; + let mut ready = false; + let mut h = Harness::builder().with_size(vec2(1280.0, 800.0)).with_step_dt(1.0 / 60.0).build_ui_state( + move |ui, app: &mut PhotocraftApp| { + if !ready { + PhotocraftApp::setup_context(ui.ctx(), crate::theme::ThemeKind::ProMedium); + ready = true; + return; + } + crate::panels::dock_panel(app, ui); + let remaining = ui.available_width(); + ui.ctx().data_mut(|data| data.insert_temp(egui::Id::unique("empty-dock-remaining"), remaining)); + crate::panels::toolbar(app, ui); + egui::CentralPanel::default().show(ui, |_| {}); + }, + app, + ); + h.run_steps(4); + let remaining = h.ctx.data(|data| data.get_temp::(egui::Id::unique("empty-dock-remaining")).unwrap_or(0.0)); + assert!(remaining > 1200.0, "the empty dock must not consume the parent layout, left {remaining}"); + assert!(h.get_by_label("Toolbar").rect().is_positive(), "the toolbar still draws"); } /// An empty dock must still run the docking pass, because Studio draws its floating Properties diff --git a/crates/ui-egui/src/panels/toolbar.rs b/crates/ui-egui/src/panels/toolbar.rs index 0b25453f27..12be1a32e4 100644 --- a/crates/ui-egui/src/panels/toolbar.rs +++ b/crates/ui-egui/src/panels/toolbar.rs @@ -161,8 +161,9 @@ pub fn toolbar(app: &mut PhotocraftApp, ui: &mut egui::Ui) { let side = app.ui.panels.toolbar_side.resolve(); let panel = if side == ToolbarSide::Right { egui::Panel::right("toolbar") } else { egui::Panel::left("toolbar") }; panel.resizable(false).exact_size(w).frame(egui::Frame::NONE.fill(t.chrome).inner_margin(egui::Margin::symmetric(m, 8))).show(ui, |ui| { - // Toolbar width is a user preference, not a Pro-only feature. - // Keep the same hit target in every theme (including RTL layouts). + // Toolbar width is a user preference, not a Pro-only feature. The two header buttons share + // the narrow toolbar column (14-point targets in every theme, including RTL layouts), so + // their tooltips carry the full names. ui.allocate_ui_with_layout(vec2(bx, 14.0), egui::Layout::left_to_right(egui::Align::Center), |ui| { ui.spacing_mut().item_spacing.x = 2.0; let (cr, resp) = ui.allocate_exact_size(vec2(bx.min(14.0), 14.0), Sense::click());