mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 04:27:49 +00:00
IconicColumn: initial for a panel without an icon, names in a wide column
This commit is contained in:
@@ -6,7 +6,7 @@ use super::rule::RuleKind;
|
||||
use super::surface::Surface;
|
||||
use super::text::{TextEnv, TextLayout, TextRun};
|
||||
use crate::fonts::stack_facts;
|
||||
use crate::style::{Corners, DataColor, Ink, StyleColor, SurfaceKind, SurfaceStyle, UiTokens, ink_color, rule_look, surface_style};
|
||||
use crate::style::{Corners, DataColor, Ink, StyleColor, SurfaceKind, SurfaceStyle, TypeRole, UiTokens, ink_color, rule_look, surface_style};
|
||||
use craft_i18n::Direction;
|
||||
use egui::epaint::{CircleShape, RectShape};
|
||||
use egui::{Color32, CornerRadius, Painter, Pos2, Rect, Shape, Stroke, StrokeKind, TextureId, Vec2};
|
||||
@@ -281,9 +281,25 @@ impl<'a> Draw<'a> {
|
||||
self.paint(&shape, None, Some(Stroke::new(self.width(StrokeWidth::Thin), edge)));
|
||||
return;
|
||||
}
|
||||
if let super::IconSource::Initial(letter) = icon.source {
|
||||
self.initial(rect, letter, icon.color.unwrap_or_else(|| StyleColor(ink_color(self.env.tokens, icon.ink))));
|
||||
return;
|
||||
}
|
||||
super::icon::paint(self.painter, self.env.tokens, self.env.direction, rect, icon);
|
||||
}
|
||||
|
||||
/// `letter` in caption text centred in an outlined box that fills the square of `rect`'s shorter side.
|
||||
fn initial(&self, rect: Rect, letter: char, color: StyleColor) {
|
||||
let side = rect.width().min(rect.height());
|
||||
let square = Rect::from_center_size(rect.center(), Vec2::splat(side));
|
||||
let radius = UiTokens::corner(self.env.tokens.shape.radius.sm);
|
||||
let width = self.width(StrokeWidth::Thin);
|
||||
self.paint(&PaintShape::Rect(square.shrink(width / 2.0), radius), None, Some(Stroke::new(width, color.egui())));
|
||||
let run = TextRun::new(letter.to_string()).role(TypeRole::Caption).color(color);
|
||||
let layout = self.layout(&run, f32::INFINITY);
|
||||
self.galley(square.center() - layout.size() / 2.0, &layout);
|
||||
}
|
||||
|
||||
/// Paints the reset mark of `look` centred on `center`: the dot while the value `differs`, else the ring.
|
||||
pub fn reset_mark(&self, look: &crate::style::ResetMarkLook, center: Pos2, differs: bool) {
|
||||
self.painter.extend(super::ResetMark::shapes(look, center, differs));
|
||||
|
||||
@@ -48,6 +48,9 @@ pub enum IconSource<'a> {
|
||||
/// A filled circle as wide as the icon's square, in the spec's colour or ink (`IconSpec::data_color` for a colour
|
||||
/// from data).
|
||||
Disc,
|
||||
/// A letter in an outlined box, in the spec's colour or ink: the icon of something that has none, from the first
|
||||
/// letter of its name. Painted by [`Draw::icon`](super::Draw::icon) as text, in the theme's fonts.
|
||||
Initial(char),
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for IconSource<'_> {
|
||||
@@ -57,6 +60,7 @@ impl std::fmt::Debug for IconSource<'_> {
|
||||
IconSource::Glyph(_) => f.write_str("Glyph(..)"),
|
||||
IconSource::Swatch(color) => f.debug_tuple("Swatch").field(color).finish(),
|
||||
IconSource::Disc => f.write_str("Disc"),
|
||||
IconSource::Initial(letter) => f.debug_tuple("Initial").field(letter).finish(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -69,6 +73,7 @@ impl PartialEq for IconSource<'_> {
|
||||
(IconSource::Glyph(a), IconSource::Glyph(b)) => std::ptr::addr_eq(*a, *b),
|
||||
(IconSource::Swatch(a), IconSource::Swatch(b)) => a == b,
|
||||
(IconSource::Disc, IconSource::Disc) => true,
|
||||
(IconSource::Initial(a), IconSource::Initial(b)) => a == b,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
@@ -105,6 +110,13 @@ impl<'a> IconSpec<'a> {
|
||||
IconSpec { source: IconSource::Swatch(color), ink: Ink::OnPanel, color: None, mirror: Mirror::Never, set: IconSet::default() }
|
||||
}
|
||||
|
||||
/// The first letter of `name` in capitals, in an outlined box, in the panel ink: the icon of something that has
|
||||
/// none. A blank name shows an empty box.
|
||||
pub fn initial(name: &str) -> Self {
|
||||
let letter = name.trim_start().chars().next().map_or(' ', |first| first.to_uppercase().next().unwrap_or(first));
|
||||
IconSpec { source: IconSource::Initial(letter), ink: Ink::OnPanel, color: None, mirror: Mirror::Never, set: IconSet::default() }
|
||||
}
|
||||
|
||||
/// A filled circle in the panel ink, or in the colour [`color`](Self::color) or [`data_color`](Self::data_color)
|
||||
/// sets (a status dot, a person's dot).
|
||||
pub fn disc() -> Self {
|
||||
@@ -167,8 +179,8 @@ pub(crate) fn paint(painter: &Painter, tokens: &UiTokens, direction: Direction,
|
||||
let side = rect.width().min(rect.height());
|
||||
let square = Rect::from_center_size(rect.center(), vec2(side, side));
|
||||
match &spec.source {
|
||||
// `Draw::icon` paints swatches with the data-colour fill.
|
||||
IconSource::Swatch(_) => {}
|
||||
// `Draw::icon` paints swatches with the data-colour fill and initials as text.
|
||||
IconSource::Swatch(_) | IconSource::Initial(_) => {}
|
||||
IconSource::Disc => {
|
||||
painter.circle_filled(square.center(), side / 2.0, color);
|
||||
}
|
||||
|
||||
@@ -12,18 +12,22 @@ use crate::contract::{
|
||||
};
|
||||
use crate::menu::GroupItem;
|
||||
use crate::primitives::IconSpec;
|
||||
use crate::primitives::TextRun;
|
||||
use crate::runtime::env::Label;
|
||||
use crate::runtime::focus::KeyUse;
|
||||
use crate::style::{ComponentStyle, Corners, StyleCx, SurfaceKind};
|
||||
use crate::style::{ComponentStyle, Corners, StyleColor, StyleCx, SurfaceKind};
|
||||
use craft_behavior::dock::{Column, DockEdge, Group};
|
||||
use craft_behavior::{Axis, CursorIcon, DockSide, IconId, Metrics, PartKey, PressPhase, Sense, Value};
|
||||
use craft_behavior::{Axis, CursorIcon, DockSide, Metrics, PartKey, PressPhase, Sense, Value};
|
||||
use egui::{Id, Pos2, Rect, Response, Ui, Vec2};
|
||||
use std::hash::Hash;
|
||||
|
||||
#[cfg(feature = "testing")]
|
||||
mod stories;
|
||||
#[cfg(feature = "testing")]
|
||||
mod trials;
|
||||
|
||||
const CELL: PartKey = PartKey::new("cell");
|
||||
const NAME: PartKey = PartKey::new("name");
|
||||
const GRIP: PartKey = PartKey::new("grip");
|
||||
const EDGE: PartKey = PartKey::new("edge");
|
||||
|
||||
@@ -39,13 +43,24 @@ pub struct IconicSizes {
|
||||
pub grip: f32,
|
||||
/// The room between two icon groups.
|
||||
pub separator: f32,
|
||||
/// The column width from which each cell shows its panel's name beside its icon.
|
||||
pub labels: f32,
|
||||
}
|
||||
|
||||
/// The column width, in rail widths, from which the cells show their panels' names: 96 points at the Compact size.
|
||||
const LABELS_IN_RAIL_WIDTHS: f32 = 2.4;
|
||||
|
||||
impl IconicSizes {
|
||||
/// The sizes of the style context `cx`.
|
||||
pub fn style(cx: &StyleCx<'_>) -> Self {
|
||||
let size = &cx.tokens.size;
|
||||
IconicSizes { column: DockSizes::style(cx), cell: size.rail.button, grip: size.handle.sm, separator: cx.tokens.space.gap.tight }
|
||||
IconicSizes {
|
||||
column: DockSizes::style(cx),
|
||||
cell: size.rail.button,
|
||||
grip: size.handle.sm,
|
||||
separator: cx.tokens.space.gap.tight,
|
||||
labels: size.rail.width * LABELS_IN_RAIL_WIDTHS,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,9 +101,17 @@ pub struct IconicColumnStyle {
|
||||
#[style(metrics)]
|
||||
metrics: Metrics,
|
||||
sizes: IconicSizes,
|
||||
/// The ink of a panel's name in a wide column.
|
||||
name: StyleColor,
|
||||
/// The ink of the name of the panel whose flyout is open.
|
||||
name_selected: StyleColor,
|
||||
/// The room between an icon and its name.
|
||||
name_gap: f32,
|
||||
}
|
||||
|
||||
/// A column collapsed to icons: the column bar over icon groups, one cell per panel. A click or Enter on an icon
|
||||
/// A column collapsed to icons: the column bar over icon groups, one cell per panel. A panel without an icon shows the
|
||||
/// first letter of its name in a box. A column widened past `IconicSizes::labels` shows each panel's name beside its
|
||||
/// icon, cut with an ellipsis to the cell; a click or drag on the name acts as one on the icon. A click or Enter on an icon
|
||||
/// opens the flyout of its panel beside the column, switches it to that panel or closes it; one flyout is open at a
|
||||
/// time. A drag of an icon starts a move of its panel, a drag of the band on top of a group a move of the group, a
|
||||
/// drag of the bar beside its button a move of the column; each starts through the drag session, and a press outside
|
||||
@@ -134,8 +157,11 @@ pub struct IconicColumn<'a, P: Value + Hash + 'static> {
|
||||
/// Where the parts of an iconic column sit.
|
||||
struct Plan {
|
||||
bar: Rect,
|
||||
/// `(group, panel, rect)` of each icon cell, top to bottom.
|
||||
/// `(group, panel, rect)` of each icon cell, top to bottom: the whole row, or in a wide column the icon's square at
|
||||
/// the reading start.
|
||||
cells: Vec<(usize, usize, Rect)>,
|
||||
/// The rest of each row in a wide column, where the panel's name shows; empty in a narrow column.
|
||||
names: Vec<Rect>,
|
||||
/// `(group, rect)` of each group's drag band.
|
||||
grips: Vec<(usize, Rect)>,
|
||||
/// `(group, rect)` of the cells of each group.
|
||||
@@ -217,23 +243,31 @@ impl<'a, P: Value + Hash + 'static> IconicColumn<'a, P> {
|
||||
self.column.groups.get(group).and_then(|group| group.panels.get(panel))
|
||||
}
|
||||
|
||||
fn plan(&self, rect: Rect, sizes: &IconicSizes) -> Plan {
|
||||
fn plan(&self, rect: Rect, sizes: &IconicSizes, rtl: bool) -> Plan {
|
||||
let bar = Rect::from_min_max(rect.min, Pos2::new(rect.right(), (rect.top() + sizes.column.bar).min(rect.bottom())));
|
||||
let body = Rect::from_min_max(Pos2::new(rect.left(), bar.bottom()), rect.max);
|
||||
let (mut y, mut cells, mut grips, mut groups) = (body.top(), Vec::new(), Vec::new(), Vec::new());
|
||||
let wide = body.width() >= sizes.labels;
|
||||
let (mut y, mut cells, mut names, mut grips, mut groups) = (body.top(), Vec::new(), Vec::new(), Vec::new(), Vec::new());
|
||||
for (at, group) in self.column.groups.iter().enumerate() {
|
||||
if at > 0 {
|
||||
y += sizes.separator;
|
||||
}
|
||||
let top = y;
|
||||
for panel in 0..group.panels.len() {
|
||||
cells.push((at, panel, Rect::from_min_size(Pos2::new(body.left(), y), Vec2::new(body.width(), sizes.cell))));
|
||||
let row = Rect::from_min_size(Pos2::new(body.left(), y), Vec2::new(body.width(), sizes.cell));
|
||||
if wide {
|
||||
let (icon, name) = split_row(row, sizes.cell, rtl);
|
||||
cells.push((at, panel, icon));
|
||||
names.push(name);
|
||||
} else {
|
||||
cells.push((at, panel, row));
|
||||
}
|
||||
y += sizes.cell;
|
||||
}
|
||||
grips.push((at, Rect::from_min_max(Pos2::new(body.left(), top), Pos2::new(body.right(), (top + sizes.grip).min(y)))));
|
||||
groups.push((at, Rect::from_min_max(Pos2::new(body.left(), top), Pos2::new(body.right(), y))));
|
||||
}
|
||||
Plan { bar, cells, grips, groups, body }
|
||||
Plan { bar, cells, names, grips, groups, body }
|
||||
}
|
||||
|
||||
/// The height of the flyout of `group`: the group's own height, else what its panels ask for, else the least.
|
||||
@@ -271,7 +305,14 @@ impl<P: Value + Hash + 'static> Component for IconicColumn<'_, P> {
|
||||
}
|
||||
|
||||
fn style(cx: &StyleCx<'_>, _: IconicColumnStyleKey<P>) -> IconicColumnStyle {
|
||||
IconicColumnStyle { metrics: Metrics::new(), sizes: IconicSizes::style(cx) }
|
||||
let colors = &cx.tokens.color;
|
||||
IconicColumnStyle {
|
||||
metrics: Metrics::new(),
|
||||
sizes: IconicSizes::style(cx),
|
||||
name: cx.color(colors.text.normal),
|
||||
name_selected: cx.color(colors.accent.text),
|
||||
name_gap: cx.tokens.space.gap.item,
|
||||
}
|
||||
}
|
||||
|
||||
fn sense(&self) -> Sense {
|
||||
@@ -286,7 +327,7 @@ impl<P: Value + Hash + 'static> Component for IconicColumn<'_, P> {
|
||||
/// top of the first icon over the band, which is declared first.
|
||||
fn parts(&mut self, rect: Rect, cx: &mut PartsCx<'_, Self>) {
|
||||
let sizes = cx.style.sizes;
|
||||
let plan = self.plan(rect, &sizes);
|
||||
let plan = self.plan(rect, &sizes, cx.flow.is_rtl());
|
||||
self.bar = plan.bar;
|
||||
let (chevron, _) = bar_slots(plan.bar, self.edge, cx.flow.is_rtl());
|
||||
let name = chevron_name(&cx.words::<DockWords>(), false);
|
||||
@@ -300,6 +341,9 @@ impl<P: Value + Hash + 'static> Component for IconicColumn<'_, P> {
|
||||
.tooltip(label)
|
||||
.toggles_at("flyout", at)
|
||||
.on_click();
|
||||
if let Some(name) = plan.names.get(at) {
|
||||
cx.part(NAME.at(at as u64), *name).sense(Sense::CLICK | Sense::DRAG).keyboard_via(CELL).toggles_at("flyout", at).on_click();
|
||||
}
|
||||
}
|
||||
for (group, grip) in &plan.grips {
|
||||
cx.part(GRIP.at(*group as u64), *grip)
|
||||
@@ -334,7 +378,8 @@ impl<P: Value + Hash + 'static> Component for IconicColumn<'_, P> {
|
||||
}
|
||||
}
|
||||
for (at, (group, panel, cell)) in plan.cells.iter().enumerate() {
|
||||
if dragging(cx.part(CELL.at(at as u64)).press.phase)
|
||||
let named = cx.part_if_declared(NAME.at(at as u64)).is_some_and(|name| dragging(name.press.phase));
|
||||
if (dragging(cx.part(CELL.at(at as u64)).press.phase) || named)
|
||||
&& let Some(panel) = self.panel(*group, *panel)
|
||||
{
|
||||
let label = Label::from((self.spec)(panel).label.as_str().to_owned());
|
||||
@@ -394,8 +439,15 @@ impl<P: Value + Hash + 'static> Component for IconicColumn<'_, P> {
|
||||
for (at, (group, panel)) in flat.iter().enumerate() {
|
||||
let Some(panel) = self.panel(*group, *panel) else { continue };
|
||||
let spec = (self.spec)(panel);
|
||||
let icon: IconSpec<'static> = spec.icon.unwrap_or_else(|| IconId::new("more").into());
|
||||
ui.face(CELL.at(at as u64), IconButton::new(icon).label(spec.label).selected(self.shown == Some(at)));
|
||||
let icon: IconSpec<'static> = spec.icon.unwrap_or_else(|| IconSpec::initial(spec.label.as_str()));
|
||||
let selected = self.shown == Some(at);
|
||||
if let Some(name) = self.laid.as_ref().and_then(|plan| plan.names.get(at)) {
|
||||
let style = &cx.style;
|
||||
let room = if cx.flow.is_rtl() { name.with_max_x(name.max.x - style.name_gap) } else { name.with_min_x(name.min.x + style.name_gap) };
|
||||
let ink = if selected { style.name_selected } else { style.name };
|
||||
let _ = ui.draw().text(room, &TextRun::new(spec.label.as_str().to_owned()).color(ink).elide());
|
||||
}
|
||||
ui.face(CELL.at(at as u64), IconButton::new(icon).label(spec.label).selected(selected));
|
||||
}
|
||||
// The edge's line lies on the column's inner edge; its handle stays inside the column.
|
||||
let (rect, edge) = (cx.rect, self.edge_divider(cx.rect, &cx.style.sizes.column));
|
||||
@@ -475,6 +527,18 @@ impl<P: Value + Hash + 'static> IconicColumn<'_, P> {
|
||||
}
|
||||
}
|
||||
|
||||
/// A row of a wide column split into the icon's square at the reading start, `side` wide, and the room for the name.
|
||||
fn split_row(row: Rect, side: f32, rtl: bool) -> (Rect, Rect) {
|
||||
let side = side.min(row.width());
|
||||
if rtl {
|
||||
let icon = Rect::from_min_max(Pos2::new(row.right() - side, row.top()), row.max);
|
||||
(icon, Rect::from_min_max(row.min, Pos2::new(icon.left(), row.bottom())))
|
||||
} else {
|
||||
let icon = Rect::from_min_max(row.min, Pos2::new(row.left() + side, row.bottom()));
|
||||
(icon, Rect::from_min_max(Pos2::new(icon.right(), row.top()), row.max))
|
||||
}
|
||||
}
|
||||
|
||||
/// The edge of the header line the chevron sits at: the bar's outer end, the window edge the dock is on.
|
||||
fn chevron_edge(edge: DockEdge) -> HeaderEdge {
|
||||
if edge == DockEdge::End { HeaderEdge::End } else { HeaderEdge::Start }
|
||||
|
||||
@@ -13,8 +13,9 @@ use craft_behavior::Mixed;
|
||||
use craft_behavior::dock::{Column, Group};
|
||||
use egui::Ui;
|
||||
|
||||
/// Panels without icons, so each cell shows the first letter of its panel's name.
|
||||
fn spec(panel: &u8) -> PanelSpec {
|
||||
PanelSpec::new(format!("Panel {panel}"))
|
||||
PanelSpec::new(["Brushes", "Swatches", "History"].get(usize::from(*panel)).copied().unwrap_or("Panel"))
|
||||
}
|
||||
|
||||
#[story(IconicColumn<'static, u8>, covers = [Default], drive = Drive::Never)]
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
//! Trials of `IconicColumn`: the letter of a panel without an icon, and the names a wide column shows.
|
||||
|
||||
use super::CELL;
|
||||
use crate::contract::{Bind, ShowContainer as _};
|
||||
use crate::docking::{GroupAt, IconicColumn, PanelSpec};
|
||||
use crate::sections::Panel;
|
||||
use crate::testing::memory::global;
|
||||
use crate::testing::{Fixture, trial};
|
||||
use craft_behavior::dock::{Column, Group};
|
||||
use egui::Vec2;
|
||||
use std::cell::Cell;
|
||||
use std::rc::Rc;
|
||||
|
||||
fn spec(panel: &u8) -> PanelSpec {
|
||||
PanelSpec::new(if *panel == 0 { "Brushes" } else { "Swatches" })
|
||||
}
|
||||
|
||||
/// A column `width` wide over two icon-less panels, and the panel whose flyout is open.
|
||||
fn column(width: f32) -> (Fixture, Rc<Cell<Option<u8>>>) {
|
||||
let open = Rc::new(Cell::new(None));
|
||||
let seen = Rc::clone(&open);
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let column = Column::new(vec![Group::new(vec![0, 1], 0)]);
|
||||
let mut now = seen.get();
|
||||
IconicColumn::new("Panels", global("iconic column", "trial"), &column, Bind::bound(&mut now), &spec).show_flyouts(ui, |_, at, ui, _| {
|
||||
let GroupAt::Show(rect) = at else { return None };
|
||||
Panel::new("Flyout").show_at(ui, rect, |_, _| ());
|
||||
None
|
||||
});
|
||||
seen.set(now);
|
||||
});
|
||||
fixture.room(Vec2::new(width, 300.0)).settle();
|
||||
(fixture, open)
|
||||
}
|
||||
|
||||
/// A panel without an icon shows the first letter of its name; a column wider than the label width shows the names
|
||||
/// too, and a click on a name opens that panel's flyout as a click on its icon does.
|
||||
#[trial]
|
||||
fn an_icon_less_panel_shows_its_initial_and_a_wide_column_shows_names_that_open_flyouts() {
|
||||
let painted = |fixture: &Fixture, text: &str| fixture.texts().iter().any(|run| run.text == text);
|
||||
let (narrow, _) = column(40.0);
|
||||
assert!(painted(&narrow, "B") && painted(&narrow, "S"), "each icon-less panel shows its initial");
|
||||
assert!(!painted(&narrow, "Brushes"), "a narrow column shows no names");
|
||||
|
||||
let (mut wide, open) = column(200.0);
|
||||
assert!(painted(&wide, "Brushes") && painted(&wide, "Swatches"), "a wide column shows the names");
|
||||
let id = wide.root_id().expect("the column is shown");
|
||||
let icon = wide.part(id, CELL.at(1)).expect("the second icon");
|
||||
assert!(icon.rect.width() < 200.0 / 2.0, "the icon keeps its square at the start: {:?}", icon.rect);
|
||||
// The name has no node of its own: the icon is its keyboard stop. Click on it, past the icon.
|
||||
wide.click_at(icon.rect.right_center() + Vec2::new(icon.rect.width(), 0.0)).idle(3);
|
||||
assert_eq!(open.get(), Some(1), "a click on the name opens its panel's flyout");
|
||||
}
|
||||
@@ -22,3 +22,8 @@ reason = "test fixtures: rooms, drag offsets and window sizes, not looks"
|
||||
path = "crates/ui-panels/src/rails/panel_rail/trials.rs"
|
||||
rules = ["literal-size"]
|
||||
reason = "trial rooms, not looks"
|
||||
|
||||
[[exempt]]
|
||||
path = "crates/ui-panels/src/docking/iconic/trials.rs"
|
||||
rules = ["literal-size"]
|
||||
reason = "trial rooms, not looks"
|
||||
|
||||
Reference in New Issue
Block a user