diff --git a/crates/ui-egui/src/browser.rs b/crates/ui-egui/src/browser.rs index 96d248348..013734fc2 100644 --- a/crates/ui-egui/src/browser.rs +++ b/crates/ui-egui/src/browser.rs @@ -498,14 +498,24 @@ pub fn browser_below_timeline(app: &mut SolveApp, ui: &mut egui::Ui) { browser_panel(app, ui, true); } +/// How narrow the browser sidebar may be dragged, and how wide (the viewport keeps the rest). +const MIN_BROWSER_WIDTH: f32 = 170.0; +const MAX_BROWSER_WIDTH: f32 = 620.0; +/// How wide a grip the sidebar's right edge offers, counted inwards from the edge. +const BROWSER_RAIL: f32 = 6.0; +/// How opaque the floating browser's card is; the workspace shows through the rest. +const BROWSER_FLOAT_ALPHA: f32 = 0.4; + fn browser_panel(app: &mut SolveApp, ui: &mut egui::Ui, timeline_above: bool) { let t = Tokens::get(); app.tree.order.clear(); DRAWN.with(|d| d.borrow_mut().clear()); OCCURRENCES.with(|d| d.borrow_mut().clear()); let mut acts = Actions::default(); - egui::Panel::left("sc_browser") - .exact_size(250.0) + let remembered = app.preferences.browser_width; + let width = if remembered.is_finite() { remembered.clamp(MIN_BROWSER_WIDTH, MAX_BROWSER_WIDTH) } else { crate::prefs::DEFAULT_BROWSER_WIDTH }; + let panel = egui::Panel::left("sc_browser") + .exact_size(width) .resizable(false) .frame(egui::Frame::NONE.fill(t.panel).inner_margin(egui::Margin::symmetric(6, 6))) .show(ui, |ui| { @@ -548,6 +558,52 @@ fn browser_panel(app: &mut SolveApp, ui: &mut egui::Ui, timeline_above: bool) { browser_contents(app, ui, &mut acts); } }); + // The sidebar's right edge is a drag handle; the width it is dragged to is remembered. + let rect = panel.response.rect; + let rail = egui::Rect::from_x_y_ranges(rect.right() - BROWSER_RAIL..=rect.right(), rect.y_range()); + let response = ui.interact(rail, egui::Id::new("sc_browser_rail"), egui::Sense::drag()).on_hover_cursor(egui::CursorIcon::ResizeHorizontal); + if response.dragged() { + let delta = ui.input(|input| input.pointer.delta().x); + if delta.is_finite() { + app.preferences.browser_width = (width + delta).clamp(MIN_BROWSER_WIDTH, MAX_BROWSER_WIDTH); + } + } + crate::scenario::publish_handle("left-browser-rail", egui::pos2(rect.right() - BROWSER_RAIL / 2.0, rect.center().y)); + finish_browser(app, ui, acts); +} + +/// The tree on a translucent card over the workspace, instead of a column that takes width from +/// it (Preferences: "Float it over the workspace"). Drawn before the central view, in a layer +/// above it, so the viewport keeps the whole window and the card sits on top of it. +pub fn floating(app: &mut SolveApp, ui: &mut egui::Ui, viewport: egui::Rect) { + let t = Tokens::get(); + app.tree.order.clear(); + DRAWN.with(|d| d.borrow_mut().clear()); + OCCURRENCES.with(|d| d.borrow_mut().clear()); + let mut acts = Actions::default(); + let remembered = app.preferences.browser_width; + let width = if remembered.is_finite() { remembered.clamp(MIN_BROWSER_WIDTH, MAX_BROWSER_WIDTH) } else { crate::prefs::DEFAULT_BROWSER_WIDTH }; + // Tall enough for the tree, short enough to leave the view room; the rows scroll inside. + let height = (viewport.height() * 0.7).clamp(160.0, 560.0); + let card = crate::frame::area(egui::Id::new("sc_browser_float")).fixed_pos(viewport.min + vec2(8.0, 8.0)).order(egui::Order::Middle).show( + ui.ctx(), + |ui| { + egui::Frame::new() + .fill(t.panel.gamma_multiply(BROWSER_FLOAT_ALPHA)) + .stroke(egui::Stroke::new(1.0, t.border)) + .corner_radius(6) + .inner_margin(egui::Margin::symmetric(8, 6)) + .show(ui, |ui| { + ui.set_width(width - 16.0); + ui.set_max_height(height); + ui.label(RichText::new(crate::i18n::tr_label("BROWSER")).size(11.0).strong().color(t.text_dim)); + ui.add_space(4.0); + // The card hugs the tree, so it hides as little of the view as it can. + browser_rows_hugging(app, ui, &mut acts); + }); + }, + ); + crate::scenario::publish_rect("browser:float", card.response.rect); finish_browser(app, ui, acts); } @@ -611,9 +667,19 @@ fn model_feature_indices(app: &SolveApp, component: u64, sketches: bool) -> Vec< } fn browser_rows(app: &mut SolveApp, ui: &mut egui::Ui, acts: &mut Actions) { + rows_scrolling(app, ui, acts, false); +} + +/// As [browser_rows], but the area hugs the rows it drew (the floating card), up to the height +/// the caller allowed. +fn browser_rows_hugging(app: &mut SolveApp, ui: &mut egui::Ui, acts: &mut Actions) { + rows_scrolling(app, ui, acts, true); +} + +fn rows_scrolling(app: &mut SolveApp, ui: &mut egui::Ui, acts: &mut Actions, shrink_height: bool) { prepare_model(app); egui::ScrollArea::vertical() - .auto_shrink([false, false]) + .auto_shrink([false, shrink_height]) .scroll_source(egui::scroll_area::ScrollSource { scroll_bar: true, mouse_wheel: true, ..egui::scroll_area::ScrollSource::NONE }) .show(ui, |ui| { ui.spacing_mut().item_spacing.y = 0.0; diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index a0e31d1bf..7a7da3d6f 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -1387,7 +1387,9 @@ impl SolveApp { timeline::timeline(self, ui); } palette::palette_bar(self, ui); - if self.ui.show_browser { + // A floating browser keeps the workspace the whole width: it is drawn over the view below. + let browser_float = self.ui.show_browser && self.preferences.browser_float; + if self.ui.show_browser && !browser_float { if left_timeline { browser::browser_below_timeline(self, ui); } else { @@ -1401,6 +1403,11 @@ impl SolveApp { } agent_chat::show(self, ui); preview::update(self, ui.ctx()); + if browser_float { + // The room the central view is about to take, before it takes it. + let viewport = ui.available_rect_before_wrap(); + browser::floating(self, ui, viewport); + } egui::CentralPanel::default().frame(egui::Frame::NONE).show(ui, |ui| { if render_workspace::active(self) { render_workspace::show(self, ui); diff --git a/crates/ui-egui/src/prefs.rs b/crates/ui-egui/src/prefs.rs index 9c62d3e2d..48b12d3bb 100644 --- a/crates/ui-egui/src/prefs.rs +++ b/crates/ui-egui/src/prefs.rs @@ -251,6 +251,10 @@ pub enum OrbitAround { Cursor, } +/// The browser sidebar's first width, and the width [`Prefs::default`] restores; the panel +/// itself clamps what the user drags to its own range. +pub const DEFAULT_BROWSER_WIDTH: f32 = 250.0; + #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[serde(default)] pub struct Prefs { @@ -318,6 +322,12 @@ pub struct Prefs { pub timeline_left_height: f32, /// Whether the browser below the left feature history is collapsed. pub timeline_left_browser_collapsed: bool, + /// Width of the left sidebar holding the browser (and the feature history above it), which + /// the user drags wider or narrower at its right edge. + pub browser_width: f32, + /// Draw the parts browser on a translucent card over the 3D view instead of a column of its + /// own, so the workspace keeps the whole window width. + pub browser_float: bool, /// The wheel zooms toward the point under the cursor (else toward the view's centre). pub zoom_to_cursor: bool, /// Spread discrete wheel input across frames (off: apply each event immediately). @@ -440,6 +450,8 @@ impl Default for Prefs { timeline_left: false, timeline_left_height: 360.0, timeline_left_browser_collapsed: false, + browser_width: DEFAULT_BROWSER_WIDTH, + browser_float: false, zoom_to_cursor: true, smooth_wheel_zoom: true, trackpad_pan: true, @@ -695,6 +707,8 @@ fn defaults(app: &mut SolveApp, page: usize) { p.timeline_left = d.timeline_left; p.timeline_left_height = d.timeline_left_height; p.timeline_left_browser_collapsed = d.timeline_left_browser_collapsed; + p.browser_width = d.browser_width; + p.browser_float = d.browser_float; p.default_units = d.default_units; p.length_decimals = d.length_decimals; p.angle_decimals = d.angle_decimals; @@ -1219,6 +1233,11 @@ fn page(app: &mut SolveApp, ui: &mut egui::Ui, t: &Tokens) { }); ui.end_row(); note(ui, app.tr("ui.timeline_left_note")); + ui.label(app.tr_label("Parts browser")); + let float_label = app.tr_label("Float it over the workspace").to_string(); + ui.checkbox(&mut app.preferences.browser_float, float_label); + ui.end_row(); + note(ui, app.tr_label("The tree draws on a translucent card over the 3D view instead of a column of its own.")); ui.label(app.tr("ui.units_of_new_designs")); egui::ComboBox::from_id_salt("pref_units").selected_text(app.preferences.default_units.clone()).show_ui(ui, |ui| { for u in UNITS { diff --git a/crates/ui-egui/src/timeline_left.rs b/crates/ui-egui/src/timeline_left.rs index d9792ad23..192cca55b 100644 --- a/crates/ui-egui/src/timeline_left.rs +++ b/crates/ui-egui/src/timeline_left.rs @@ -957,6 +957,38 @@ mod tests { assert!(handle_at("row:Untitled").is_some(), "expanding the browser should restore its rows"); } + #[test] + fn browser_sidebar_drags_wider_and_narrower() { + let mut harness = Harness::new(); + harness.frames(4); + let before = harness.app.preferences.browser_width; + let rail = handle_at("left-browser-rail").expect("the browser sidebar's right edge is drawn"); + harness.step(&json!({"drag": [[rail.x, rail.y], [rail.x + 90.0, rail.y]]})).unwrap(); + let wider = harness.app.preferences.browser_width; + assert!(wider > before + 40.0, "dragging the right edge right should widen the browser: {before} -> {wider}"); + + let rail = handle_at("left-browser-rail").expect("the browser sidebar's right edge is drawn"); + harness.step(&json!({"drag": [[rail.x, rail.y], [rail.x - 40.0, rail.y]]})).unwrap(); + let narrower = harness.app.preferences.browser_width; + assert!(narrower < wider - 20.0, "dragging it left should narrow it again: {wider} -> {narrower}"); + } + + #[test] + fn floating_browser_sits_over_the_workspace_and_docks_again() { + let mut harness = Harness::new(); + harness.app.preferences.browser_float = true; + harness.frames(4); + let card = crate::scenario::rect_of("browser:float").expect("the floating browser is drawn"); + assert!(card.min.x < 40.0, "the card sits at the view's left edge: {card:?}"); + assert!(handle_at("left-browser-rail").is_none(), "a floating browser must not take a column of the window"); + assert!(handle_at("row:Untitled").is_some(), "the tree is still drawn and usable"); + + harness.app.preferences.browser_float = false; + harness.frames(4); + assert!(crate::scenario::rect_of("browser:float").is_none(), "docking again removes the card"); + assert!(handle_at("left-browser-rail").is_some(), "the docked sidebar is back"); + } + #[test] fn rollback_bar_above_last_feature_can_be_dragged_to_the_end() { let mut harness = Harness::new();