ui-canvas: TaskBar::grip(TaskBarGrip::Bar), one rounded bar lit under the pointer and while the bar moves (BodyCx::moving)

This commit is contained in:
bflatastic committed 2026-11-24 12:56:14 +07:00
1 parent 827141f831
commit 361012ff9f
5 files changed
+172 -11

No files matched your search

+1 -1
View File
@@ -27,7 +27,7 @@ pub use line_preview::{LineGuideStyle, StraightLinePreview};
pub use marquee::{Marquee, MarqueeLook, MarqueeShape, MarqueeStyle, Run};
pub use outline::{HoverMask, HoverOutline, HoverShape, OutlineStyle, SelectionOutline, SelectionOutlineLook, path_runs};
pub use pixel_grid::{GridSegment, PixelGrid, PixelGridStyle};
pub use task_bar::{TaskBar, TaskBarCx, TaskBarDismiss, TaskBarKind, TaskBarOutput, TaskBarPlace};
pub use task_bar::{TaskBar, TaskBarCx, TaskBarDismiss, TaskBarGrip, TaskBarKind, TaskBarOutput, TaskBarPlace};
// End of canvas-controls-b.
// Brush overlays.
@@ -10,18 +10,19 @@ use crate::buttons::IconButton;
use crate::canvas::canvas_space;
use crate::contract::overlay::{Anchor, Dismiss, Outside, Place, PlacementChange, PlacementKind};
use crate::contract::{
Bind, BindMut, Common, Component, Container, ContainerProps, Cx, DragBand, HeaderRegion, Inner, Mount, NoValue, Outcome, Overlay, Pad, PopupKind,
Props as _, Role, Show as _, ShowContainer as _, Sizing, Trigger,
Bind, BindMut, Common, Component, Container, ContainerProps, Cx, DragBand, HeaderRegion, Inner, MeasureCx, Mount, NoValue, Outcome, Overlay, Pad,
PaintUi, PopupKind, Props as _, Role, Show as _, ShowContainer as _, Sizing, Styled as _, Trigger,
};
use crate::display::{Divider, Icon};
use crate::menu::{Menu, MenuItem, PanelMenu, PanelRow};
use crate::primitives::IconSpec;
use crate::primitives::{IconSpec, PaintShape};
use crate::runtime::env::{Env, Label};
use crate::runtime::overlay::CloseReason;
use crate::style::{ComponentStyle, StyleCx, SurfaceKind};
use crate::style::{ComponentStyle, StyleColor, StyleCx, SurfaceKind, UiTokens};
use craft_behavior::kurbo::Point;
use craft_behavior::overlay::{Align, PlaceRequest, Side};
use craft_behavior::{EditEvent, GesturePhase, IconId, Metrics, Mixed, Sense};
use craft_theme::{Stateful, States};
use egui::{Pos2, Rect, Response, Ui, Vec2};
use std::sync::Arc;
@@ -112,6 +113,9 @@ pub struct TaskBar<'a> {
/// default.
#[prop]
compact: bool,
/// How the task bar's grip looks: the `grip` icon by default.
#[prop]
grip: TaskBarGrip,
}
/// What a bar hangs from: the screen bounds of document points, or the canvas view's top edge, aligned in reading
@@ -136,6 +140,19 @@ pub enum TaskBarKind {
Fixed,
}
/// How the grip of a [`TaskBar`] looks. Either way the grip takes no press of its own: the bar's move band under it
/// moves the bar.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum TaskBarGrip {
/// The `grip` icon.
#[default]
Icon,
/// One rounded upright bar, a value track's groove wide and the members' line less a row gap at each end tall, in
/// the control edge's line colour; muted text under the pointer and while the bar moves (VectorCraft's).
Bar,
}
/// What an outside press does to a [`TaskBar`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
@@ -257,6 +274,7 @@ impl<'a> TaskBar<'a> {
keep_clear: None,
dismiss: TaskBarDismiss::Close,
compact: false,
grip: TaskBarGrip::Icon,
}
}
@@ -283,6 +301,7 @@ impl<'a> TaskBar<'a> {
keep_clear: None,
dismiss: TaskBarDismiss::Close,
compact: false,
grip: TaskBarGrip::Icon,
}
}
@@ -311,7 +330,7 @@ impl<'a> TaskBar<'a> {
/// Shows the bar inside `area`; `body` fills its members.
pub fn show<R>(self, ui: &mut Ui, area: Rect, mut body: impl FnMut(&mut Ui, &mut TaskBarCx<'_>) -> R) -> Inner<TaskBarOutput, R> {
let TaskBar { label, hang, mut place, rows, kind, side, align, keep_clear, dismiss, compact } = self;
let TaskBar { label, hang, mut place, rows, kind, side, align, keep_clear, dismiss, compact, grip: grip_look } = self;
let side = side.unwrap_or(match kind {
TaskBarKind::Task | TaskBarKind::Fixed => Side::Below,
TaskBarKind::Selection => Side::Above,
@@ -357,14 +376,21 @@ impl<'a> TaskBar<'a> {
let mut menu = MoreMenu { words: Arc::clone(&words), offered: movable, pinned: held.pinned, rows, placed: false, pick: None };
// Only the task bar moves, so only it starts with the grip.
let grip = movable.then(|| words.task_bar_move.clone());
let mut shown = layer.show(ui, |ui, _| {
let mut shown = layer.show(ui, |ui, layer| {
let moving = layer.moving();
// The bar is the one surface: the grip is its first member, on the members' line inside its padding.
let bar = Bar::new(Label::Empty, BarKind::Tool).surface(SurfaceKind::Floating).wrap();
// A compact bar takes a popup's smaller corners and lighter shadow, as production's small frame.
let bar = if compact { bar.compact().surface(SurfaceKind::Popup) } else { bar };
bar.show(ui, |ui, _| {
if let Some(grip) = &grip {
let _ = Icon::new(IconSpec::svg(IconId::new("grip"))).label(grip.clone()).show(ui);
match (&grip, grip_look) {
(Some(grip), TaskBarGrip::Bar) => {
let _ = GripBar { common: Common::new(), moving }.label(grip.clone()).show(ui);
}
(Some(grip), _) => {
let _ = Icon::new(IconSpec::svg(IconId::new("grip"))).label(grip.clone()).show(ui);
}
(None, _) => {}
}
menu.placed = false;
let inner = body(ui, &mut TaskBarCx { menu: &mut menu });
@@ -498,6 +524,70 @@ fn settled(held: TaskBarPlace, shown: Rect, area: Rect, moved: Option<Rect>, pic
next
}
/// The style of the [`TaskBarGrip::Bar`] grip.
#[derive(Clone, Debug, PartialEq, ComponentStyle)]
#[non_exhaustive]
pub struct GripBarStyle {
#[style(metrics)]
metrics: Metrics,
/// The bar's width and height.
size: Vec2,
/// The bar's ink: the line colour at rest, muted text under the pointer.
ink: Stateful<StyleColor>,
/// The ink while the bar moves.
moving: StyleColor,
}
/// The [`TaskBarGrip::Bar`] grip: one rounded upright bar centred on the members' line. It senses only hover, so a
/// press on it reaches the layer's move band under it.
#[derive(crate::contract::Component, crate::contract::Props)]
#[component(unregistered)]
struct GripBar {
#[common]
common: Common,
/// The layer's move band is moving the bar this pass.
moving: bool,
}
impl Component for GripBar {
const ROLE: Role = Role::IMAGE;
const PARTS: bool = false;
const INPUT: craft_behavior::Input = craft_behavior::Input::Display;
const SHOWS_LABEL: bool = false;
const OVERLAYS: bool = false;
type Style = GripBarStyle;
type Output = Response;
fn style(cx: &StyleCx<'_>, _: GripBarStyleKey) -> GripBarStyle {
let colors = &cx.tokens.color;
let (rest, lit) = (cx.color(colors.line.control_edge), cx.color(colors.text.muted));
GripBarStyle {
metrics: Metrics::new(),
size: Vec2::new(cx.tokens.size.groove, cx.control().height - 2.0 * cx.tokens.space.gap.row),
ink: Stateful::new(rest).off(States::new(rest, lit, lit, rest)),
moving: lit,
}
}
fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 {
cx.style.size
}
fn sense(&self) -> Sense {
Sense::HOVER
}
fn paint(&mut self, ui: &mut PaintUi<'_>, cx: &Cx<'_, Self>) {
let ink = if self.moving { cx.style.moving } else { cx.resolve(&cx.style.ink) };
let bar = Rect::from_center_size(cx.rect.center(), cx.style.size);
ui.draw().fill(&PaintShape::Rect(bar, UiTokens::corner(cx.style.size.x / 2.0)), ink);
}
fn finish(self, cx: Cx<'_, Self>) -> Response {
cx.response.clone()
}
}
/// The style of the task bar's layer: the surface comes from the container.
#[derive(Clone, Debug, Default, PartialEq, ComponentStyle)]
#[non_exhaustive]
@@ -1,6 +1,6 @@
//! Stories of `TaskBar`.
use super::{TaskBar, TaskBarDismiss, TaskBarPlace};
use super::{TaskBar, TaskBarDismiss, TaskBarGrip, TaskBarPlace};
use crate::buttons::{Button, IconButton};
use crate::canvas::testing::canvas_story::{StoryDocument, on_canvas};
use crate::contract::{Props as _, Show as _};
@@ -198,3 +198,36 @@ fn placed_and_pinned(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
.output
})
}
/// Points the pointer at the grip.
fn hover_grip(fixture: &mut Fixture) {
if let Some(grip) = fixture.by_label("Move bar") {
fixture.frame(vec![crate::testing::pointer::moved(grip.center())]);
fixture.idle(8);
}
}
/// Drags the bar by its grip and holds it there, the button down.
fn drag_grip(fixture: &mut Fixture) {
if let Some(grip) = fixture.by_label("Move bar") {
fixture.drag_hold_at(grip.center(), egui::vec2(-40.0, 30.0));
}
}
/// The bar grip: one rounded upright bar in the line colour.
#[story(TaskBar<'static>, covers = [], drive = Drive::Never)]
fn bar_grip(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
configured(ui, data, &turned_frame(), None, |bar| bar.grip(TaskBarGrip::Bar))
}
/// The bar grip under the pointer: muted text.
#[story(TaskBar<'static>, covers = [], drive = Drive::Custom(hover_grip))]
fn bar_grip_hovered(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
configured(ui, data, &turned_frame(), None, |bar| bar.grip(TaskBarGrip::Bar))
}
/// The bar grip while the bar moves: muted text.
#[story(TaskBar<'static>, covers = [], drive = Drive::Custom(drag_grip))]
fn bar_grip_dragged(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
configured(ui, data, &turned_frame(), None, |bar| bar.grip(TaskBarGrip::Bar))
}
@@ -6,7 +6,7 @@ use craft_behavior::CanvasView;
use craft_behavior::kurbo::{Point, Vec2 as DocVec};
use craft_behavior::overlay::{Align, Side};
use craft_ui::buttons::Button;
use craft_ui::canvas_controls::{TaskBar, TaskBarDismiss, TaskBarPlace};
use craft_ui::canvas_controls::{TaskBar, TaskBarDismiss, TaskBarGrip, TaskBarPlace};
use craft_ui::contract::{Bind, Show as _};
use craft_ui::menu::PanelRow;
use craft_ui::runtime::commands::{CommandRef, EditCommands};
@@ -523,3 +523,34 @@ fn a_compact_task_bar_is_smaller_and_shows_every_member_whole() {
assert!(button.is_positive() && compact.expand(SLACK).contains_rect(button), "each member shows whole in {compact:?}: {button:?}");
}
}
/// The fill of the bar grip painted inside `grip` among `shapes`.
fn grip_ink(shapes: &[egui::Shape], grip: Rect) -> Option<egui::Color32> {
shapes.iter().find_map(|shape| match shape {
egui::Shape::Rect(rect) if grip.contains_rect(rect.rect) => Some(rect.fill),
_ => None,
})
}
/// The bar grip lights under the pointer and while the bar moves, and goes back to its rest ink after the drop.
#[trial]
fn the_bar_grip_lights_under_the_pointer_and_while_the_bar_moves() {
let scene = scene();
let mut fixture = fixture_with(&scene, |bar| bar.grip(TaskBarGrip::Bar));
fixture.settle();
let grip = fixture.by_label("Move bar").expect("the grip is named");
let rest = grip_ink(&fixture.frame_shapes(), grip.rect).expect("the grip paints its bar");
fixture.frame(vec![moved(grip.center())]).idle(8);
let hovered = grip_ink(&fixture.frame_shapes(), grip.rect);
assert!(hovered.is_some_and(|ink| ink != rest), "the grip lights under the pointer: {hovered:?} against {rest:?}");
let by = Vec2::new(-60.0, 30.0);
fixture.drag_hold_at(grip.center(), by);
let moving = grip_ink(&fixture.frame_shapes(), grip.rect.translate(by));
assert!(moving.is_some_and(|ink| ink != rest), "the grip is lit while the bar moves: {moving:?} against {rest:?}");
fixture.release_at(grip.center() + by).frame(vec![moved(grip.center() + by + Vec2::new(0.0, 200.0))]).idle(8);
let dropped = fixture.by_label("Move bar").expect("the grip is named");
assert_eq!(grip_ink(&fixture.frame_shapes(), dropped.rect), Some(rest), "the grip is back to rest once dropped and left");
}
@@ -84,6 +84,13 @@ impl<'a> BodyCx<'a> {
self.field_message.clone()
}
/// Whether a drag of the container's move band (`DragBand::Move`) moves its layer in this pass, from the pass the
/// drag starts to the one before its release: a grip in the body shows the move.
pub fn moving(&self) -> bool {
let band = crate::primitives::sensing::part_press(self.memory.ctx(), self.resolved.id, crate::contract::MOVE_BAND.into());
matches!(band.press.phase, craft_behavior::PressPhase::DragStart { .. } | craft_behavior::PressPhase::Drag { .. })
}
/// Whether the body is open.
pub fn is_open(&self) -> bool {
*self.open