Skip to main content

egui_pop_core/
band.rs

1//! A header band: the handle a movable surface (a movable dialog, a
2//! palette) is dragged by. It sits at the top of a frame's content, its
3//! fill reaching the frame's edges, with a separator below it and the
4//! move cursor over it.
5//!
6//! [`begin`] registers the band's drag area first (so the buttons drawn
7//! in its row, registered after it, stay on top of it), then lays out its
8//! row; [`paint`] has the kit's `header-band` paint it once the frame's
9//! width is known, at the end of the frame's content, under the row: in
10//! the `hover` state under the pointer, `active` while dragged. The band
11//! is not focusable: the keyboard moves a surface through a command of
12//! its own.
13//!
14//! ```
15//! use egui_pop_core::band::{self, BandStyle};
16//!
17//! let ctx = egui::Context::default();
18//! let mut out = ctx.run_ui(Default::default(), |ui| {
19//!     let style = BandStyle {
20//!         padding: 12.0,
21//!         gap: 8.0,
22//!     };
23//!     egui::Frame::NONE.inner_margin(style.padding).show(ui, |ui| {
24//!         // The row is told the band's state, for its title's colour.
25//!         let (_, band) = band::begin(ui, &style, |ui, _state| ui.label("Levels"));
26//!         ui.label("content");
27//!         band::paint(ui, &band, &style);
28//!         assert!(!band.dragging);
29//!     });
30//! });
31//! out.textures_delta.clear();
32//! ```
33
34use egui::{Id, Rect, Response, Role, Sense, Shape, Ui, Vec2};
35
36use crate::icon::Icon;
37use crate::kit::{Kit, KitCx, Part, State};
38use crate::state::Slot;
39use crate::theme::Tokens;
40
41/// Where a band sits in its frame.
42#[derive(Clone, Copy, Debug, PartialEq)]
43pub struct BandStyle {
44    /// The frame's inner margin: how far the band reaches out of the
45    /// content to the frame's edges.
46    pub padding: f32,
47    /// The room below its row, inside the band (and as much again below
48    /// its line): the kit's `header-band.padding-y`, or a dialog's
49    /// `dialog-header.padding-y`.
50    pub gap: f32,
51}
52
53/// A band as laid out this pass, and what the pointer did to it.
54#[derive(Clone, Debug, Default)]
55pub struct Band {
56    id: Option<Id>,
57    shape: Option<egui::layers::ShapeIdx>,
58    top: f32,
59    bottom: f32,
60    /// Being dragged.
61    pub dragging: bool,
62    /// Double-clicked.
63    pub double_clicked: bool,
64    /// The drag's move this pass.
65    pub delta: Vec2,
66    /// Under the pointer (not under one of its buttons).
67    pub hovered: bool,
68    /// The band's own response (not its buttons'): a context menu opens
69    /// on it.
70    pub response: Option<egui::Response>,
71}
72
73impl Band {
74    /// Its state for the kit: `HOVER` under the pointer, `ACTIVE` while
75    /// dragged.
76    pub fn state(&self) -> State {
77        State::REST
78            .with(State::HOVER, self.hovered)
79            .with(State::ACTIVE, self.dragging)
80    }
81}
82
83/// Starts a band at the top of a frame's content and lays out its row
84/// with `add` (a title, buttons), told the band's state. Its size is the
85/// last pass's (a guess on the first one).
86/// A band's size on the last pass, per band.
87const SIZE: Slot<Vec2> = Slot::new("core.band.size");
88
89pub fn begin<R>(
90    ui: &mut Ui,
91    style: &BandStyle,
92    add: impl FnOnce(&mut Ui, State) -> R,
93) -> (R, Band) {
94    let id = ui.scope_id().with("egui_pop.band");
95    let pad = style.padding;
96    let top_left = ui.max_rect().min - egui::vec2(pad, pad);
97    let size = SIZE.keyed(id).get(ui.ctx()).unwrap_or(egui::vec2(
98        ui.available_width() + 2.0 * pad,
99        pad + ui.spacing().interact_size.y + style.gap,
100    ));
101    let response = ui.interact(
102        Rect::from_min_size(top_left, size),
103        id,
104        Sense::CLICK | Sense::DRAG,
105    );
106    let shape = ui.painter().add(Shape::Noop);
107    let mut band = Band {
108        id: Some(id),
109        shape: Some(shape),
110        top: top_left.y,
111        bottom: 0.0,
112        dragging: response.dragged(),
113        double_clicked: response.double_clicked(),
114        delta: response.drag_delta(),
115        hovered: response.hovered(),
116        response: None,
117    };
118    let inner = add(ui, band.state());
119    band.bottom = ui.min_rect().bottom() + style.gap;
120    ui.add_space(2.0 * style.gap);
121    crate::pointer::drag_cursor(&response, crate::pointer::DragKind::Move);
122    band.response = Some(response);
123    (inner, band)
124}
125
126/// Has the kit's `header-band` paint the band across the frame's width
127/// (known once the content is laid out), under its row; call it last in
128/// the frame's content.
129pub fn paint(ui: &Ui, band: &Band, style: &BandStyle) {
130    let (Some(id), Some(shape)) = (band.id, band.shape) else {
131        return;
132    };
133    let pad = style.padding;
134    let content = ui.min_rect();
135    let rect = Rect::from_min_max(
136        egui::pos2(content.left() - pad, band.top),
137        egui::pos2(content.right() + pad, band.bottom),
138    );
139    let part = Part {
140        rect,
141        room: rect,
142        state: band.state(),
143        label: None,
144    };
145    Kit::of(ui.ctx())
146        .header_band
147        .paint_under(ui, shape, &part, None);
148    SIZE.keyed(id).set(ui.ctx(), rect.size());
149}
150
151/// A square icon button of a band, `side` points wide, named `name`
152/// (for assistive technology and its tooltip), drawn by the kit's
153/// `band-icon-button`: `selected` when `on` (a pinned palette), the icon
154/// in its state's `text`.
155pub fn button(ui: &mut Ui, side: f32, icon: Icon, name: &str, on: bool) -> Response {
156    let (rect, response) = ui.allocate_exact_size(egui::vec2(side, side), Sense::click());
157    if ui.is_rect_visible(rect) {
158        let kit = Kit::of(ui.ctx());
159        let part = Part {
160            rect,
161            room: rect,
162            state: State::from_response(&response).with(State::SELECTED, on),
163            label: None,
164        };
165        let cx = KitCx {
166            painter: ui.painter(),
167            ppp: ui.ctx().pixels_per_point(),
168        };
169        let button = &kit.band_icon_button;
170        button.paint(&cx, &part);
171        let size = Tokens::of(ui.ctx()).size.icon_inline;
172        icon.paint(
173            ui.painter(),
174            Rect::from_center_size(rect.center(), egui::vec2(size, size)),
175            button.style(part.state).text,
176        );
177    }
178    crate::a11y::name(&response, Role::Button, name);
179    response.on_hover_text(name)
180}