Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

UI: the Contextual Task Bar moves by its grip and stays where it is dropped, with Pin Bar Position and Reset Bar Position in its menu

aiongg7d064f268e68
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index 9758c7d5e..7e3870f98 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -81,6 +81,7 @@ pub struct SavedWorkspace {
pub name: String,
pub control_bar: bool,
pub task_bar: bool,
+ pub task_bar_pin: Option<[f32; 2]>,
pub tools_double_column: bool,
pub dock_tab: String,
pub dock_expanded: bool,
@@ -189,6 +190,12 @@ pub struct UiState {
pub snap_zone: f64,
/// Window > Contextual Task Bar.
pub task_bar: bool,
+ /// Where the Contextual Task Bar is pinned: its top-left, in points from the canvas's top-left
+ /// (`None`: it follows the selection).
+ pub task_bar_pin: Option<[f32; 2]>,
+ /// Where the Contextual Task Bar was last shown, like [`Self::task_bar_pin`].
+ #[serde(skip)]
+ pub task_bar_at: Option<[f32; 2]>,
/// Help › About DesignCraft is open.
pub about: bool,
/// The About window's tab: 0 About, 1 Contributors, 2 Models (`about::ABOUT_TABS`).
@@ -264,6 +271,8 @@ impl Default for UiState {
smart_spacing: true,
snap_zone: 4.0,
task_bar: true,
+ task_bar_pin: None,
+ task_bar_at: None,
about: false,
about_tab: 0,
pending_urls: Vec::new(),
diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs
index abff58c62..adabafeeb 100644
--- a/crates/ui-egui/src/menus.rs
+++ b/crates/ui-egui/src/menus.rs
@@ -129,6 +129,13 @@ pub const UI_COMMANDS: &[(&str, &str, Option<&str>, &str)] = &[
("window.split", "Split Window", None, "{on?: bool} — two views of the document side by side, each with its own zoom and scroll"),
("window.newWindow", "New Window", None, "{on?: bool} — another view of the active document in its own window"),
("window.taskBar", "Contextual Task Bar", None, "{}"),
+ (
+ "window.taskBarPin",
+ "Pin Bar Position",
+ None,
+ "{on?: bool, at?: [x, y] (points from the canvas's top-left)} — the Contextual Task Bar stays where it is (or at `at`) instead of following the selection; with neither, toggles",
+ ),
+ ("window.taskBarReset", "Reset Bar Position", None, "{} — the Contextual Task Bar follows the selection again, under it"),
("help.discord", "Join the ArtCraft Discord…", None, "{} — opens https://discord.gg/artcraft in the browser"),
("help.appPage", "DesignCraft Website…", None, "{} — opens https://getartcraft.com/apps/designcraft"),
("help.github", "DesignCraft on GitHub…", None, "{} — opens https://github.com/storytold/designcraft"),
@@ -1284,6 +1291,26 @@ pub fn run_ui(app: &mut DesignApp, id: &str, p: &Value) -> Option<Result<Value,
Ok(json!(app.second_window))
}
"window.taskBar" => flag(&mut app.ui.task_bar),
+ "window.taskBarPin" => {
+ let at = match p.get("at") {
+ None | Some(Value::Null) => None,
+ Some(v) => match v.as_array().map(|a| a.iter().map(Value::as_f64).collect::<Vec<_>>()).as_deref() {
+ Some([Some(x), Some(y)]) if x.is_finite() && y.is_finite() => Some([x.clamp(-1e6, 1e6) as f32, y.clamp(-1e6, 1e6) as f32]),
+ _ => return Some(Err("window.taskBarPin: `at` is [x, y] in points".into())),
+ },
+ };
+ let on = p.get("on").and_then(Value::as_bool).unwrap_or(at.is_some() || app.ui.task_bar_pin.is_none());
+ app.ui.task_bar_pin = match (on, at.or(app.ui.task_bar_pin).or(app.ui.task_bar_at)) {
+ (false, _) => None,
+ (true, Some(at)) => Some(at),
+ (true, None) => return Some(Err("window.taskBarPin: the Contextual Task Bar isn't showing; give `at`".into())),
+ };
+ Ok(json!(app.ui.task_bar_pin))
+ }
+ "window.taskBarReset" => {
+ app.ui.task_bar_pin = None;
+ Ok(Value::Null)
+ }
"help.about" => {
if let Some(tab) = p.get("tab").and_then(Value::as_str) {
let Some(i) = crate::about::ABOUT_TABS.iter().position(|t| t.eq_ignore_ascii_case(tab)) else {
@@ -1318,6 +1345,7 @@ pub fn run_ui(app: &mut DesignApp, id: &str, p: &Value) -> Option<Result<Value,
name: name.to_string(),
control_bar: u.control_bar,
task_bar: u.task_bar,
+ task_bar_pin: u.task_bar_pin,
tools_double_column: u.tools_double_column,
dock_tab: u.dock_tab.clone(),
dock_expanded: u.dock_expanded,
@@ -1350,6 +1378,7 @@ pub fn run_ui(app: &mut DesignApp, id: &str, p: &Value) -> Option<Result<Value,
if let Some(w) = app.ui.custom_workspaces.iter().find(|w| w.name == name).cloned() {
app.ui.control_bar = w.control_bar;
app.ui.task_bar = w.task_bar;
+ app.ui.task_bar_pin = w.task_bar_pin;
app.ui.tools_double_column = w.tools_double_column;
app.ui.dock_tab = w.dock_tab;
app.ui.dock_expanded = w.dock_expanded;
@@ -1691,6 +1720,7 @@ pub fn checked(app: &DesignApp, id: &str, params: &Value) -> Option<bool> {
"app.language" => app.ui.language == params.get("lang").and_then(Value::as_str).unwrap_or(""),
"app.flattener" => app.ui.flattener == params.get("preset").and_then(Value::as_str).unwrap_or(""),
"window.taskBar" => app.ui.task_bar,
+ "window.taskBarPin" => app.ui.task_bar_pin.is_some(),
"window.toolsDoubleColumn" => app.ui.tools_double_column,
"view.togglePreview" => app.ui.screen_mode == crate::ScreenMode::Preview,
"window.brightness" => params.get("brightness").and_then(Value::as_str) == Some(app.ui.brightness.id()),
diff --git a/crates/ui-egui/src/taskbar.rs b/crates/ui-egui/src/taskbar.rs
index 4174a27e1..98815423f 100644
--- a/crates/ui-egui/src/taskbar.rs
+++ b/crates/ui-egui/src/taskbar.rs
@@ -10,10 +10,16 @@ use crate::{DesignApp, icons, panels};
pub const HEIGHT: f32 = 36.0;
-/// Show the bar under `sel` (screen rect of the selection), kept inside `canvas`.
+/// The grip that moves the bar.
+const GRIP: &str = "task_bar_grip";
+
+/// Show the bar under `sel` (screen rect of the selection), or where it is pinned, kept inside
+/// `canvas`.
pub fn show(app: &mut DesignApp, ctx: &egui::Context, sel: Rect, canvas: Rect) {
+ let grip_id = egui::Id::new(GRIP);
// (Two separate context calls: `is_pointer_over_egui` inside an `input` closure deadlocks.)
- if !app.ui.task_bar || (ctx.input(|i| i.pointer.any_down()) && !ctx.is_pointer_over_egui()) {
+ let dragging = ctx.dragged_id() == Some(grip_id);
+ if !app.ui.task_bar || (ctx.input(|i| i.pointer.any_down()) && !ctx.is_pointer_over_egui() && !dragging) {
return;
}
let Some(st) = app.session.active() else { return };
@@ -25,16 +31,23 @@ pub fn show(app: &mut DesignApp, ctx: &egui::Context, sel: Rect, canvas: Rect) {
let id = egui::Id::new("task_bar");
// Width from the previous frame (content-sized).
let w = ctx.memory(|m| m.area_rect(id).map(|r| r.width())).unwrap_or(420.0);
- let mut x = sel.center().x - w / 2.0;
- x = x.clamp(canvas.min.x + 8.0, (canvas.max.x - w - 8.0).max(canvas.min.x + 8.0));
- let mut y = sel.max.y + 14.0;
- if y + HEIGHT > canvas.max.y - 8.0 {
- y = (sel.min.y - HEIGHT - 14.0).max(canvas.min.y + 8.0);
- }
- if y + HEIGHT > canvas.max.y - 4.0 {
- y = canvas.max.y - HEIGHT - 8.0;
- }
- egui::Area::new(id).order(egui::Order::Middle).fixed_pos(pos2(x, y)).show(ctx, |ui| {
+ let at = match app.ui.task_bar_pin {
+ Some([x, y]) => inside(canvas.min + vec2(x, y), w, canvas),
+ None => {
+ let mut x = sel.center().x - w / 2.0;
+ x = x.clamp(canvas.min.x + 8.0, (canvas.max.x - w - 8.0).max(canvas.min.x + 8.0));
+ let mut y = sel.max.y + 14.0;
+ if y + HEIGHT > canvas.max.y - 8.0 {
+ y = (sel.min.y - HEIGHT - 14.0).max(canvas.min.y + 8.0);
+ }
+ if y + HEIGHT > canvas.max.y - 4.0 {
+ y = canvas.max.y - HEIGHT - 8.0;
+ }
+ pos2(x, y)
+ }
+ };
+ app.ui.task_bar_at = Some([at.x - canvas.min.x, at.y - canvas.min.y]);
+ egui::Area::new(id).order(egui::Order::Middle).fixed_pos(at).show(ctx, |ui| {
egui::Frame::NONE
.fill(t.panel)
.stroke(Stroke::new(1.0, t.border))
@@ -45,8 +58,17 @@ pub fn show(app: &mut DesignApp, ctx: &egui::Context, sel: Rect, canvas: Rect) {
ui.set_height(HEIGHT);
ui.horizontal_centered(|ui| {
ui.spacing_mut().item_spacing.x = 6.0;
+ // Dragging the grip moves the bar, and pins it where it is dropped.
let (g, _) = ui.allocate_exact_size(vec2(12.0, 20.0), Sense::hover());
- icons::paint(ui.painter(), g, "grip", t.icon);
+ let grip = ui.interact(g, grip_id, Sense::drag());
+ icons::paint(ui.painter(), g, "grip", if grip.hovered() || grip.dragged() { t.text_strong } else { t.icon });
+ if grip.dragged() {
+ ui.ctx().set_cursor_icon(egui::CursorIcon::Grabbing);
+ let to = inside(at + grip.drag_delta(), w, canvas) - canvas.min;
+ let _ = app.run("window.taskBarPin", json!({"at": [to.x, to.y]}));
+ } else if grip.hovered() {
+ ui.ctx().set_cursor_icon(egui::CursorIcon::Grab);
+ }
let text_frame = panels::sel_info(app).is_some_and(|i| i.is_text && i.count == 1);
if has_text || text_frame {
text_controls(app, ui, has_text);
@@ -59,6 +81,13 @@ pub fn show(app: &mut DesignApp, ctx: &egui::Context, sel: Rect, canvas: Rect) {
});
}
+/// `at` moved as little as needed for a bar `w` wide to sit inside `canvas` (8 pt in).
+fn inside(at: egui::Pos2, w: f32, canvas: Rect) -> egui::Pos2 {
+ let x = at.x.min(canvas.max.x - w - 8.0).max(canvas.min.x + 8.0);
+ let y = at.y.min(canvas.max.y - HEIGHT - 8.0).max(canvas.min.y + 8.0);
+ pos2(x, y)
+}
+
fn split_button(ui: &mut Ui, icon: &str, label: &str) -> (egui::Response, egui::Response) {
let t = Tokens::get(ui.ctx());
let g = crate::rtl::plain(ui.ctx(), label, egui::FontId::proportional(11.0), t.text);
@@ -246,8 +275,14 @@ fn more(app: &mut DesignApp, ui: &mut Ui) {
app.ui.task_bar = false;
ui.close();
}
- if ui.button(crate::rtl::widget(ui, crate::i18n::tr(&app.ui.language, "Properties Panel"))).clicked() {
- let _ = app.run("window.panel", json!({"panel": "properties"}));
+ let pinned = app.ui.task_bar_pin.is_some();
+ let pin = crate::i18n::tr(&app.ui.language, "Pin Bar Position");
+ if ui.button(crate::rtl::widget(ui, if pinned { format!("✓ {pin}") } else { format!(" {pin}") })).clicked() {
+ let _ = app.run("window.taskBarPin", json!({"on": !pinned}));
+ ui.close();
+ }
+ if ui.add_enabled(pinned, egui::Button::new(crate::rtl::widget(ui, crate::i18n::tr(&app.ui.language, "Reset Bar Position")))).clicked() {
+ let _ = app.run("window.taskBarReset", json!({}));
ui.close();
}
});
@@ -258,3 +293,111 @@ fn sep(ui: &mut Ui) {
let (r, _) = ui.allocate_exact_size(vec2(1.0, 24.0), Sense::hover());
ui.painter().line_segment([r.center_top(), r.center_bottom()], Stroke::new(1.0, t.border));
}
+
+#[cfg(test)]
+mod tests {
+ use egui::{pos2, vec2};
+ use egui_kittest::Harness;
+ use egui_kittest::kittest::Queryable;
+ use serde_json::json;
+
+ use crate::test_window::{self, Window, click_at};
+
+ /// A selected frame, with the task bar under it.
+ fn window() -> (Harness<'static, Window>, u64) {
+ let app = crate::DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
+ let mut h = test_window::open(app, vec2(1440.0, 900.0));
+ let app = &mut h.state_mut().app;
+ let id = app.run("frame.create", json!({"rect": [72, 72, 300, 200], "content": "text", "text": "Hello"})).unwrap()["id"].as_u64().unwrap();
+ app.run("selection.set", json!({"ids": [id]})).unwrap();
+ h.run_steps(4);
+ (h, id)
+ }
+
+ fn bar(h: &Harness<'static, Window>) -> egui::Rect {
+ h.ctx.memory(|m| m.area_rect(egui::Id::new("task_bar"))).unwrap()
+ }
+
+ fn grip(h: &Harness<'static, Window>) -> egui::Pos2 {
+ let b = bar(h);
+ pos2(b.min.x + 10.0, b.center().y)
+ }
+
+ fn drag(h: &mut Harness<'static, Window>, from: egui::Pos2, by: egui::Vec2) {
+ h.hover_at(from);
+ h.drag_at(from);
+ h.run_steps(2);
+ for k in 1..=8 {
+ h.hover_at(from + by * (k as f32 / 8.0));
+ h.run_steps(1);
+ }
+ h.drop_at(from + by);
+ h.run_steps(4);
+ }
+
+ fn more(h: &mut Harness<'static, Window>) {
+ let b = bar(h);
+ click_at(h, pos2(b.max.x - 19.0, b.center().y));
+ }
+
+ #[test]
+ fn pinning_is_a_command() {
+ let mut app = crate::DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
+ let run = |app: &mut crate::DesignApp, id: &str, p| crate::menus::run_ui(app, id, &p).unwrap();
+ assert!(run(&mut app, "window.taskBarPin", json!({})).is_err(), "nowhere to pin a bar that isn't showing");
+ assert!(run(&mut app, "window.taskBarPin", json!({"at": [1, "x"]})).is_err());
+ run(&mut app, "window.taskBarPin", json!({"at": [40, 60]})).unwrap();
+ assert_eq!(app.ui.task_bar_pin, Some([40.0, 60.0]));
+ assert_eq!(crate::menus::checked(&app, "window.taskBarPin", &json!({})), Some(true));
+ run(&mut app, "window.taskBarPin", json!({})).unwrap();
+ assert_eq!(app.ui.task_bar_pin, None, "toggled off");
+ app.ui.task_bar_at = Some([10.0, 20.0]);
+ run(&mut app, "window.taskBarPin", json!({"on": true})).unwrap();
+ assert_eq!(app.ui.task_bar_pin, Some([10.0, 20.0]), "pinned where it shows");
+ run(&mut app, "window.taskBarReset", json!({})).unwrap();
+ assert_eq!(app.ui.task_bar_pin, None);
+ // A workspace keeps the bar's place.
+ app.ui.task_bar_pin = Some([5.0, 6.0]);
+ run(&mut app, "window.newWorkspace", json!({"name": "Mine"})).unwrap();
+ app.ui.task_bar_pin = None;
+ run(&mut app, "window.workspace", json!({"name": "Mine"})).unwrap();
+ assert_eq!(app.ui.task_bar_pin, Some([5.0, 6.0]));
+ }
+
+ #[test]
+ fn the_grip_moves_the_bar_and_it_stays_there() {
+ let (mut h, id) = window();
+ let before = bar(&h);
+ let g = grip(&h);
+ drag(&mut h, g, vec2(200.0, 150.0));
+ let after = bar(&h);
+ assert!((after.min - before.min - vec2(200.0, 150.0)).length() < 2.0, "dragged from {before:?} to {after:?}");
+ assert!(h.state().app.ui.task_bar_pin.is_some(), "pinned where it was dropped");
+ // A moved selection leaves it where it is.
+ h.state_mut().app.run("transform.set", json!({"ids": [id], "y": 300})).unwrap();
+ h.run_steps(4);
+ assert_eq!(bar(&h).min, after.min);
+ // Dragged past the canvas, it stays inside.
+ let canvas = h.state().app.canvas_rect.unwrap();
+ let g = grip(&h);
+ drag(&mut h, g, vec2(5000.0, 5000.0));
+ assert!(canvas.contains_rect(bar(&h)), "{:?} outside the canvas {canvas:?}", bar(&h));
+ // Reset Bar Position: back under the selection.
+ more(&mut h);
+ let at = h.get_by_label("Reset Bar Position").rect().center();
+ click_at(&mut h, at);
+ assert_eq!(h.state().app.ui.task_bar_pin, None);
+ // Pin Bar Position keeps it where it is.
+ more(&mut h);
+ let at = h.get_by_label_contains("Pin Bar Position").rect().center();
+ click_at(&mut h, at);
+ let at = bar(&h).min - canvas.min;
+ let [x, y] = h.state().app.ui.task_bar_pin.unwrap();
+ assert!((vec2(x, y) - at).length() < 1.0, "pinned at {x}, {y}; showing at {at:?}");
+ // Hide Bar.
+ more(&mut h);
+ let at = h.get_by_label("Hide Bar").rect().center();
+ click_at(&mut h, at);
+ assert!(!h.state().app.ui.task_bar);
+ }
+}