ui-widgets: a link in a row's cell is as wide as its text, so a press past it selects or drags the row; Hosted::role

This commit is contained in:
bflatastic
2026-11-23 04:08:24 +07:00
parent 9b89c55e5b
commit 5c8ac4b471
4 changed files with 61 additions and 5 deletions
+8 -1
View File
@@ -1,7 +1,7 @@
//! A leaf a component hosts in a slot of its own (a row's field, a notice's drop hint), erased so the host can keep
//! leaves of different types: measured under the slot's key, then shown once at the rect the host placed it in.
use super::{Component, MeasureCx, PaintUi, Show};
use super::{Component, MeasureCx, PaintUi, Role, Show};
use craft_behavior::Proposal;
use egui::{Rect, Vec2};
@@ -15,6 +15,9 @@ pub trait Hosted<H: Component> {
/// Shows the leaf at `rect` under `key` and hands its output on.
fn show(self: Box<Self>, ui: &mut PaintUi<'_>, key: HostedKey, rect: Rect);
/// The leaf's kind of component, for a host that places some kinds apart (a link only as wide as its text).
fn role(&self) -> Role;
}
/// A hosted leaf and what takes its output; a hidden one keeps its slot's room and shows nothing.
@@ -48,4 +51,8 @@ impl<H: Component, C: Show, F: FnOnce(C::Output)> Hosted<H> for Hosting<C, F> {
(self.output)(ui.child(key, rect, self.component));
}
}
fn role(&self) -> Role {
C::ROLE
}
}
+2 -1
View File
@@ -523,7 +523,8 @@ impl<'a> ListRow<'a> {
/// A component in the cell of `column` in place of a text: a field that edits the row's value in that column (a
/// tab stop's position or alignment). It fills the cell's width, inset as a text cell is, at its own height up to
/// the row's. In the column the label takes it stands in the label's place, and the label stays the row's
/// the row's; a link is only as wide as its text, so a press past it acts on the row (selects it, starts its
/// drag) as a press on a text cell does. In the column the label takes it stands in the label's place, and the label stays the row's
/// accessible name. The row shows `field` and hands its output to `output` in the pass it is shown; its stop joins
/// the row's roving group as a trailing field's does, and it takes its own presses and the keys it declares. A
/// column that `cells(..)` does not list shows nothing.
+7 -3
View File
@@ -8,7 +8,7 @@ use super::trailing::{TrailingFace, TrailingItem, cell_key, field_key, trailing_
use super::{LabelUnderline, ListRow, RowRun};
use crate::buttons::IconButton;
use crate::choice::Checkbox;
use crate::contract::{Bind, Cx, PaintUi, Pointer as _, Props as _, Styled as _, item_part_open};
use crate::contract::{Bind, Cx, PaintUi, Pointer as _, Props as _, Role, Styled as _, item_part_open};
use crate::controls::disclosure::Disclosure;
use crate::list::toggle::{ToggleFace, ToggleState};
use crate::primitives::{Draw, IconSpec, PaintShape, RectSide, RuleKind, Span, StrokeWidth, TextRun, Wrap};
@@ -297,7 +297,8 @@ impl ListRow<'_> {
}
/// Shows each cell field in its column's cell, or in the label's room in the label's column, at its own height up to
/// the row's, centred on the row's band.
/// the row's, centred on the row's band. A link is as wide as its text, at the room's start, as a text cell's text
/// is: a press past it acts on the row.
pub(super) fn paint_cell_fields(&mut self, ui: &mut PaintUi<'_>, cx: &Cx<'_, Self>, placed: &Placed) {
let sizes = &cx.style.sizes;
let rtl = cx.env.direction().is_rtl();
@@ -305,6 +306,7 @@ impl ListRow<'_> {
let band = placed.label.y_range();
for (at, (column, field)) in self.cell_fields.iter_mut().enumerate() {
let (Some(item), Some(size)) = (field.item.take(), field.size) else { continue };
let link = item.role() == Role::LINK;
let room = if label.as_ref() == Some(column) {
placed.label
} else {
@@ -313,7 +315,9 @@ impl ListRow<'_> {
Flow::new(cell, rtl).span(sizes.cell_inset, sizes.cell_room(cell.width()))
};
let height = size.ideal.y.min(room.height());
item.show(ui, cell_key(at), Rect::from_center_size(room.center(), Vec2::new(room.width(), height)));
let width = if link { size.ideal.x.min(room.width()) } else { room.width() };
let slot = Flow::new(room, rtl).take_start(width).0;
item.show(ui, cell_key(at), Rect::from_center_size(Pos2::new(slot.center().x, room.center().y), Vec2::new(width, height)));
}
}
}
+44
View File
@@ -615,6 +615,50 @@ fn a_cell_field_stands_in_its_cell_takes_the_click_and_reports_its_edit() {
assert_eq!(*typed.borrow(), Some(12.0), "the field's edit reaches the output");
}
/// A link in the label's column is only as wide as its text, as a label is: a press on it follows the link, and a
/// press past it acts on the row (selects it, and would start its drag), as production's rows named by a link did.
#[trial]
fn a_link_in_a_cell_is_as_wide_as_its_text_and_a_press_past_it_acts_on_the_row() {
use craft_behavior::{ColumnKey, ColumnWidth};
use craft_ui::buttons::Link;
use craft_ui::contract::ColumnSpec;
use craft_ui::table::Table;
use egui::Rect;
use std::cell::Cell;
const NAME: ColumnKey = ColumnKey::new("name");
let followed = Rc::new(Cell::new(0));
let selections = Rc::new(Cell::new(0));
let name_cell = Rc::new(RefCell::new(Rect::NOTHING));
let (clicks, selected, cell) = (followed.clone(), selections.clone(), name_cell.clone());
let mut fixture = Fixture::new(move |ui| {
let keys = [0_usize];
let source = keyed(&keys, |key: &usize| *key);
let (clicks, cell) = (clicks.clone(), cell.clone());
let out =
Table::<usize>::new("Appearance", vec![ColumnSpec::new(NAME, "Name", ColumnWidth::fill(1.0))]).show_items(ui, &source, &mut |_, icx| {
*cell.borrow_mut() = icx.cell(&NAME).unwrap_or(Rect::NOTHING);
let clicks = clicks.clone();
ListRow::new("Fill").cells(icx.cells()).cell_field(NAME, Link::new("Fill:"), move |out| {
if out.clicked() {
clicks.set(clicks.get() + 1);
}
})
});
let changes = out.events.iter().filter(|event| matches!(event, CollectionEvent::Selection { .. })).count();
selected.set(selected.get() + changes);
});
fixture.room(Vec2::new(400.0, 200.0)).settle();
let link = fixture.by_label("Fill:").expect("the link shows");
let cell = *name_cell.borrow();
assert!(link.rect.width() < cell.width() / 2.0, "the link is as wide as its text: {:?} in {cell:?}", link.rect);
let past = egui::Pos2::new((link.rect.right() + cell.right()) / 2.0, link.rect.center().y);
fixture.click_at(past).settle();
assert_eq!(followed.get(), 0, "a press past the text is not the link's");
assert!(selections.get() > 0, "a press past the text acts on the row");
fixture.click(&link).settle();
assert_eq!(followed.get(), 1, "a press on the text follows the link");
}
/// A row's label band takes the toggle columns it leaves empty next to it, up to the nearest toggle it fills: a row
/// without toggles starts its label at the row's start, a row with the first toggle only starts it inside the second
/// column, and a row with both starts it past them. A row without toggles that keeps its label in the label column