mirror of
https://github.com/storytold/lightcraft.git
synced 2026-11-28 09:19:25 +01:00
Merge pull request #3953 from bd78/status-box-move
LR-BEHAV-BG-TASKS: make the activity stack window movable
This commit is contained in:
2 files changed
+219
-29
No files matched your search
@@ -20,8 +20,13 @@ pub const MAX_ROWS: usize = 3;
|
||||
/// The stack's width (points).
|
||||
pub const WIDTH: f32 = 280.0;
|
||||
|
||||
/// Inner margin around the activity stack frame (points).
|
||||
const MARGIN: i8 = 8;
|
||||
/// Size of the ✕ cancel cross (points).
|
||||
const CROSS: f32 = 16.0;
|
||||
/// Height of the grip strip at the top of the stack (points): a drag there always moves the stack, also when the
|
||||
/// task list overflows and a drag over its rows scrolls it (touch screens).
|
||||
const HANDLE_H: f32 = 10.0;
|
||||
|
||||
/// "3 of 25", "12 of 340 MB", "40 %"; empty while the amount of work isn't known.
|
||||
pub fn count_text(t: &TaskInfo) -> String {
|
||||
@@ -35,6 +40,7 @@ pub fn count_text(t: &TaskInfo) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
/// Format bytes as a decimal megabyte string ("1.2", "42").
|
||||
fn megabytes(bytes: u64) -> String {
|
||||
let mb = bytes as f64 / 1_048_576.0;
|
||||
if mb < 10.0 { format!("{mb:.1}") } else { format!("{:.0}", mb.floor()) }
|
||||
@@ -70,38 +76,72 @@ pub fn show(app: &mut LightcraftApp, ctx: &egui::Context) {
|
||||
let t = Tokens::get(ctx);
|
||||
let shown = if app.activity_expanded { tasks.len() } else { tasks.len().min(MAX_ROWS) };
|
||||
let hidden = tasks.len().saturating_sub(shown);
|
||||
let max_height = (ctx.content_rect().height() - t.top_bar_h - 60.0).max(80.0);
|
||||
let mut cancel = None;
|
||||
let mut toggle = false;
|
||||
let frame = egui::Frame::NONE.fill(t.chrome).stroke(Stroke::new(1.0, t.field_border)).corner_radius(6).inner_margin(Margin::same(MARGIN));
|
||||
egui::Area::new(egui::Id::unique("activity-stack")).order(egui::Order::Middle).fixed_pos(pos2(8.0, t.top_bar_h + 8.0)).show(ctx, |ui| {
|
||||
// An area lays out in the size it had last frame, and the scroll area would take that as all the room there
|
||||
// is: the stack would never grow when rows are added. Give it the whole height below the top bar instead
|
||||
// (the scroll area keeps to `max_height`).
|
||||
ui.set_max_height((ctx.content_rect().height() - t.top_bar_h - 16.0).max(80.0));
|
||||
frame.show(ui, |ui| {
|
||||
let inner = WIDTH - 2.0 * (f32::from(MARGIN) + 1.0);
|
||||
ui.set_width(inner);
|
||||
ui.spacing_mut().item_spacing.y = 3.0;
|
||||
egui::ScrollArea::vertical().max_height(max_height).auto_shrink([false, true]).show(ui, |ui| {
|
||||
for (i, task) in tasks.iter().take(shown).enumerate() {
|
||||
if i > 0 {
|
||||
ui.add_space(6.0);
|
||||
}
|
||||
if row(ui, &t, task, inner) {
|
||||
cancel = Some(task.id);
|
||||
}
|
||||
let area_resp = egui::Area::new(egui::Id::unique("activity-stack"))
|
||||
.order(egui::Order::Middle)
|
||||
.default_pos(pos2(8.0, t.top_bar_h + 8.0))
|
||||
.movable(true)
|
||||
.constrain(true)
|
||||
.show(ctx, |ui| {
|
||||
// An area lays out in the size it had last frame, and the scroll area would take that as all the room there
|
||||
// is: the stack would never grow when rows are added. Give it the whole height below its position instead
|
||||
// (the scroll area keeps to `max_scroll_h`).
|
||||
let has_footer = hidden > 0 || (app.activity_expanded && tasks.len() > MAX_ROWS);
|
||||
// the footer (or the frame's margins alone) and the grip strip with its spacing
|
||||
let footer_reserve = if has_footer { 44.0 } else { 20.0 } + HANDLE_H + 3.0;
|
||||
let space_below = (ctx.content_rect().bottom() - ui.cursor().top() - 16.0).max(0.0);
|
||||
// Cap at the true viewport capacity so the stack never claims more height
|
||||
// than the viewport can provide, even when it is shorter than `footer_reserve`.
|
||||
let max_stack_h = (ctx.content_rect().height() - 16.0).max(0.0);
|
||||
let available_h = space_below.clamp(max_stack_h.min(footer_reserve), max_stack_h);
|
||||
ui.set_max_height(available_h);
|
||||
let frame_resp = frame.show(ui, |ui| {
|
||||
let inner = WIDTH - 2.0 * (f32::from(MARGIN) + 1.0);
|
||||
ui.set_width(inner);
|
||||
ui.spacing_mut().item_spacing.y = 3.0;
|
||||
let mut interactive_hovered = false;
|
||||
// the grip: not a widget, so a drag on it is the area's (moves the stack), never the list's
|
||||
let (grip, _) = ui.allocate_exact_size(vec2(inner, HANDLE_H), Sense::hover());
|
||||
register(ui, "activity:handle", grip);
|
||||
for dy in [-1.5, 1.5] {
|
||||
let y = grip.center().y + dy;
|
||||
ui.painter().line_segment([pos2(grip.center().x - 12.0, y), pos2(grip.center().x + 12.0, y)], Stroke::new(1.0, t.text_dim));
|
||||
}
|
||||
let max_scroll_h = (available_h - footer_reserve).max(0.0);
|
||||
egui::ScrollArea::vertical().max_height(max_scroll_h).min_scrolled_height(0.0).auto_shrink([false, true]).show(ui, |ui| {
|
||||
for (i, task) in tasks.iter().take(shown).enumerate() {
|
||||
if i > 0 {
|
||||
ui.add_space(6.0);
|
||||
}
|
||||
let (clicked, hovered) = row(ui, &t, task, inner);
|
||||
if clicked {
|
||||
cancel = Some(task.id);
|
||||
}
|
||||
interactive_hovered |= hovered;
|
||||
}
|
||||
});
|
||||
if has_footer {
|
||||
ui.add_space(4.0);
|
||||
let text = if hidden > 0 { tr_format!("+{n} more", n = hidden) } else { tr("Show less").to_string() };
|
||||
let r = ui.add(egui::Label::new(RichText::new(text).font(t.font(11.0)).color(t.accent)).sense(Sense::click()));
|
||||
register(ui, "activity:more", r.rect);
|
||||
toggle = r.clicked();
|
||||
interactive_hovered |= r.hovered();
|
||||
}
|
||||
interactive_hovered
|
||||
});
|
||||
if hidden > 0 || (app.activity_expanded && tasks.len() > MAX_ROWS) {
|
||||
ui.add_space(4.0);
|
||||
let text = if hidden > 0 { tr_format!("+{n} more", n = hidden) } else { tr("Show less").to_string() };
|
||||
let r = ui.add(egui::Label::new(RichText::new(text).font(t.font(11.0)).color(t.accent)).sense(Sense::click()));
|
||||
register(ui, "activity:more", r.rect);
|
||||
toggle = r.clicked();
|
||||
}
|
||||
frame_resp.inner
|
||||
});
|
||||
});
|
||||
let interactive_hovered = area_resp.inner;
|
||||
if area_resp.response.dragged() {
|
||||
ctx.set_cursor_icon(egui::CursorIcon::Grabbing);
|
||||
} else if interactive_hovered {
|
||||
ctx.set_cursor_icon(egui::CursorIcon::PointingHand);
|
||||
} else if area_resp.response.hovered() {
|
||||
ctx.set_cursor_icon(egui::CursorIcon::Grab);
|
||||
}
|
||||
if let Some(id) = cancel {
|
||||
self::cancel(app, id);
|
||||
}
|
||||
@@ -116,9 +156,10 @@ pub(crate) fn cancel(app: &mut LightcraftApp, id: u64) {
|
||||
let _ = app.session.execute("activity.cancel", &json!({"id": id}));
|
||||
}
|
||||
|
||||
/// One task: name and ✕, the bar, the detail line. True when ✕ was clicked.
|
||||
fn row(ui: &mut egui::Ui, t: &Tokens, task: &TaskInfo, width: f32) -> bool {
|
||||
/// One task: name and ✕, the bar, the detail line. Returns `(cancel_clicked, cross_hovered)`.
|
||||
fn row(ui: &mut egui::Ui, t: &Tokens, task: &TaskInfo, width: f32) -> (bool, bool) {
|
||||
let mut clicked = false;
|
||||
let mut cross_hovered = false;
|
||||
let r = ui.vertical(|ui| {
|
||||
ui.set_width(width);
|
||||
ui.horizontal(|ui| {
|
||||
@@ -134,6 +175,7 @@ fn row(ui: &mut egui::Ui, t: &Tokens, task: &TaskInfo, width: f32) -> bool {
|
||||
crate::widgets::announce(&resp, crate::i18n::tr("Cancel"));
|
||||
paint(ui.painter(), rect.shrink(2.0), Icon::Close, if resp.hovered() { t.text } else { t.icon });
|
||||
register(ui, format!("activity:cancel:{}", task.id), rect);
|
||||
cross_hovered = resp.hovered();
|
||||
clicked = resp.on_hover_text(tr("Cancel")).clicked();
|
||||
}
|
||||
});
|
||||
@@ -144,7 +186,7 @@ fn row(ui: &mut egui::Ui, t: &Tokens, task: &TaskInfo, width: f32) -> bool {
|
||||
}
|
||||
});
|
||||
register(ui, format!("activity:row:{}", task.id), r.response.rect);
|
||||
clicked
|
||||
(clicked, cross_hovered)
|
||||
}
|
||||
|
||||
/// The progress bar, named after its task for screen readers; while the amount of work isn't
|
||||
|
||||
@@ -12,6 +12,7 @@ use crate::{LightcraftApp, Services};
|
||||
// generous: merges and renders run on worker threads that a busy machine (a full workspace test run) slows a lot
|
||||
const T: Duration = Duration::from_secs(60);
|
||||
|
||||
/// Build a headless instance with the procedural demo library loaded.
|
||||
fn demo() -> Headless {
|
||||
let app = LightcraftApp::new(lightcraft_engine::Session::with_demo(), Services { png: None, ..Default::default() });
|
||||
let mut h = Headless::new(app, [1200.0, 800.0], 1.0);
|
||||
@@ -21,14 +22,17 @@ fn demo() -> Headless {
|
||||
h
|
||||
}
|
||||
|
||||
/// Check whether a widget with the given identifier is registered.
|
||||
fn has(h: &Headless, id: &str) -> bool {
|
||||
h.app.widgets.iter().any(|(w, _)| w == id)
|
||||
}
|
||||
|
||||
/// Count the number of activity row widgets registered.
|
||||
fn rows(h: &Headless) -> usize {
|
||||
h.app.widgets.iter().filter(|(w, _)| w.starts_with("activity:row:")).count()
|
||||
}
|
||||
|
||||
/// Return the bounding rectangle of the registered widget with `id`.
|
||||
fn rect(h: &Headless, id: &str) -> egui::Rect {
|
||||
h.app.widgets.iter().find(|(w, _)| w == id).map(|(_, r)| *r).unwrap_or_else(|| panic!("no widget {id}"))
|
||||
}
|
||||
@@ -576,6 +580,150 @@ fn rows_added_later_grow_the_stack() {
|
||||
}
|
||||
}
|
||||
|
||||
/// Verify that dragging the activity stack moves the panel across frames and respects viewport constraints.
|
||||
#[test]
|
||||
fn activity_stack_area_is_movable() {
|
||||
let mut h = demo();
|
||||
let g = h.app.session.activity.start("export", "Exporting", Cancel::Yes);
|
||||
wait_visible(&mut h);
|
||||
let id = egui::Id::unique("activity-stack");
|
||||
let initial_rect = h.view.ctx.memory(|m| m.area_rect(id)).expect("the stack is drawn");
|
||||
assert!(initial_rect.min.x >= 7.0 && initial_rect.min.x <= 9.0, "default pos at x=8: {initial_rect:?}");
|
||||
|
||||
// Hover cancel cross -> PointingHand
|
||||
let cancel_id = format!("activity:cancel:{}", g.id());
|
||||
let r = h.request("ui.hoverWidget", json!({"id": cancel_id}), T);
|
||||
assert_eq!(r["ok"], true, "{r:?}");
|
||||
h.step();
|
||||
assert_eq!(h.last_cursor, egui::CursorIcon::PointingHand, "cancel cross shows pointing hand");
|
||||
|
||||
// Hover frame padding -> Grab
|
||||
let r = h.request("ui.move", json!({"x": initial_rect.min.x + 4.0, "y": initial_rect.min.y + 4.0}), T);
|
||||
assert_eq!(r["ok"], true, "{r:?}");
|
||||
h.step();
|
||||
assert_eq!(h.last_cursor, egui::CursorIcon::Grab, "frame padding hover shows grab cursor");
|
||||
|
||||
// Drag the activity stack from non-interactive frame padding, verifying Grabbing cursor mid-drag
|
||||
let start_pos = initial_rect.min + egui::vec2(4.0, 4.0);
|
||||
let target_pos = start_pos + egui::vec2(100.0, 60.0);
|
||||
let modifiers = egui::Modifiers::NONE;
|
||||
h.app.synthetic.push(egui::Event::PointerMoved(start_pos));
|
||||
h.app.synthetic.push(egui::Event::PointerButton { pos: start_pos, button: egui::PointerButton::Primary, pressed: true, modifiers });
|
||||
h.step();
|
||||
for i in 1..=4 {
|
||||
h.app.synthetic.push(egui::Event::PointerMoved(start_pos + (target_pos - start_pos) * (i as f32 / 4.0)));
|
||||
h.step();
|
||||
}
|
||||
assert_eq!(h.last_cursor, egui::CursorIcon::Grabbing, "cursor is Grabbing mid-drag");
|
||||
h.app.synthetic.push(egui::Event::PointerButton { pos: target_pos, button: egui::PointerButton::Primary, pressed: false, modifiers });
|
||||
h.step();
|
||||
h.step();
|
||||
let updated_rect = h.view.ctx.memory(|m| m.area_rect(id)).expect("the stack is drawn");
|
||||
assert!((updated_rect.min.x - (initial_rect.min.x + 100.0)).abs() < 5.0, "stack moved in x: {updated_rect:?}");
|
||||
assert!((updated_rect.min.y - (initial_rect.min.y + 60.0)).abs() < 5.0, "stack moved in y: {updated_rect:?}");
|
||||
assert!(!g.is_cancelled(), "dragging the stack does not cancel the task");
|
||||
|
||||
// Dragging beyond the right and bottom viewport edges moves toward edges and is constrained on screen
|
||||
let content = h.view.ctx.content_rect();
|
||||
let before_rb = updated_rect;
|
||||
let r = h.request(
|
||||
"ui.drag",
|
||||
json!({"x": updated_rect.min.x + 4.0, "y": updated_rect.min.y + 4.0, "toX": content.max.x + 500.0, "toY": content.max.y + 500.0, "steps": 6}),
|
||||
T,
|
||||
);
|
||||
assert_eq!(r["ok"], true, "{r:?}");
|
||||
h.step();
|
||||
let constrained_rb = h.view.ctx.memory(|m| m.area_rect(id)).expect("the stack is drawn");
|
||||
assert!(constrained_rb.min.x > before_rb.min.x, "stack moved right toward edge: {constrained_rb:?} vs {before_rb:?}");
|
||||
assert!(constrained_rb.min.y > before_rb.min.y, "stack moved down toward edge: {constrained_rb:?} vs {before_rb:?}");
|
||||
assert!(constrained_rb.max.x <= content.max.x + 1.0, "constrained in right: {constrained_rb:?} vs {content:?}");
|
||||
assert!(constrained_rb.max.y <= content.max.y + 1.0, "constrained in bottom: {constrained_rb:?} vs {content:?}");
|
||||
|
||||
// Dragging past the left and top edges moves toward top-left and is constrained
|
||||
let before_lt = constrained_rb;
|
||||
let r = h.request(
|
||||
"ui.drag",
|
||||
json!({"x": constrained_rb.min.x + 4.0, "y": constrained_rb.min.y + 4.0, "toX": content.min.x - 500.0, "toY": content.min.y - 500.0, "steps": 6}),
|
||||
T,
|
||||
);
|
||||
assert_eq!(r["ok"], true, "{r:?}");
|
||||
h.step();
|
||||
let constrained_lt = h.view.ctx.memory(|m| m.area_rect(id)).expect("the stack is drawn");
|
||||
assert!(constrained_lt.min.x < before_lt.min.x, "stack moved left toward edge: {constrained_lt:?} vs {before_lt:?}");
|
||||
assert!(constrained_lt.min.y < before_lt.min.y, "stack moved up toward edge: {constrained_lt:?} vs {before_lt:?}");
|
||||
assert!(constrained_lt.min.x >= content.min.x - 1.0, "constrained in left: {constrained_lt:?} vs {content:?}");
|
||||
assert!(constrained_lt.min.y >= content.min.y - 1.0, "constrained in top: {constrained_lt:?} vs {content:?}");
|
||||
}
|
||||
|
||||
/// Verify that dragging the stack near the bottom in a short viewport keeps controls reachable and within bounds.
|
||||
#[test]
|
||||
fn activity_stack_short_viewport_near_bottom_keeps_controls_reachable() {
|
||||
// 300 pt tall viewport exercises the height-budget path.
|
||||
// Do NOT use h.settle(T): demo renders asynchronously and blocks 60 s.
|
||||
// Two steps suffice for a pure-UI / height-constraint test.
|
||||
let app = LightcraftApp::new(lightcraft_engine::Session::with_demo(), Services { png: None, ..Default::default() });
|
||||
let mut h = Headless::new(app, [600.0, 300.0], 1.0);
|
||||
h.step();
|
||||
h.step();
|
||||
let guards: Vec<_> = (0..5).map(|i| h.app.session.activity.start("export", &format!("Task {i}"), Cancel::Yes)).collect();
|
||||
wait_visible(&mut h);
|
||||
|
||||
let content = h.view.ctx.content_rect();
|
||||
let id = egui::Id::unique("activity-stack");
|
||||
|
||||
// At the default position the cancel and "+more" controls are within the viewport.
|
||||
let cancel_id = format!("activity:cancel:{}", guards[0].id());
|
||||
assert!(has(&h, &cancel_id), "cancel button is present at default position");
|
||||
assert!(content.contains_rect(rect(&h, &cancel_id)), "cancel button within viewport at default position");
|
||||
assert!(has(&h, "activity:more"), "+more button is present at default position");
|
||||
assert!(content.contains_rect(rect(&h, "activity:more")), "+more button within viewport at default position");
|
||||
|
||||
// Cancel task 0 while controls are reachable at the default position.
|
||||
click(&mut h, &cancel_id);
|
||||
assert!(guards[0].is_cancelled(), "cancel button was clickable and worked");
|
||||
|
||||
// Drag the stack to 40 pt above the bottom edge. When only 40 pt remain the
|
||||
// height-budget path clamps available_h to footer_reserve, so rows disappear —
|
||||
// that is expected. What must hold is that the stack does not exceed the
|
||||
// viewport: its max.y must stay at or below content.bottom().
|
||||
let initial_rect = h.view.ctx.memory(|m| m.area_rect(id)).expect("the stack is drawn");
|
||||
let r = h.request(
|
||||
"ui.drag",
|
||||
json!({"x": initial_rect.min.x + 4.0, "y": initial_rect.min.y + 4.0, "toX": initial_rect.min.x + 4.0, "toY": content.bottom() - 40.0, "steps": 6}),
|
||||
T,
|
||||
);
|
||||
assert_eq!(r["ok"], true, "{r:?}");
|
||||
h.step();
|
||||
let dragged_rect = h.view.ctx.memory(|m| m.area_rect(id)).expect("the stack is drawn");
|
||||
assert!(dragged_rect.max.y <= content.bottom() + 1.0, "stack stays within viewport after near-bottom drag: {dragged_rect:?} vs {content:?}");
|
||||
}
|
||||
|
||||
/// The grip at the top of the stack sits outside the task list: dragging it moves the stack even when the list
|
||||
/// overflows (a drag over the rows would scroll the list instead).
|
||||
#[test]
|
||||
fn the_grip_moves_the_stack_when_the_list_overflows() {
|
||||
let app = LightcraftApp::new(lightcraft_engine::Session::with_demo(), Services { png: None, ..Default::default() });
|
||||
let mut h = Headless::new(app, [600.0, 300.0], 1.0);
|
||||
h.step();
|
||||
h.step();
|
||||
let _guards: Vec<_> = (0..8).map(|i| h.app.session.activity.start("export", &format!("Task {i}"), Cancel::Yes)).collect();
|
||||
wait_visible(&mut h);
|
||||
click(&mut h, "activity:more");
|
||||
assert!(h.app.activity_expanded, "the stack shows every task");
|
||||
let grip = rect(&h, "activity:handle");
|
||||
let first_row = h.app.widgets.iter().filter(|(w, _)| w.starts_with("activity:row:")).map(|(_, r)| r.min.y).fold(f32::MAX, f32::min);
|
||||
assert!(grip.max.y <= first_row, "the grip is above the list: {grip:?} vs first row at {first_row}");
|
||||
let id = egui::Id::unique("activity-stack");
|
||||
let before = h.view.ctx.memory(|m| m.area_rect(id)).expect("the stack is drawn");
|
||||
let from = grip.center();
|
||||
let r = h.request("ui.drag", json!({"x": from.x, "y": from.y, "toX": from.x + 120.0, "toY": from.y, "steps": 6}), T);
|
||||
assert_eq!(r["ok"], true, "{r:?}");
|
||||
h.step();
|
||||
h.step();
|
||||
let after = h.view.ctx.memory(|m| m.area_rect(id)).expect("the stack is drawn");
|
||||
assert!((after.min.x - (before.min.x + 120.0)).abs() < 5.0, "the grip moved the stack: {before:?} -> {after:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn immediate_library_commands_and_auto_sync_show_completion() {
|
||||
let mut h = demo();
|
||||
|
||||
Reference in new issue
Block a user