mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-18 02:32:33 +00:00
FindingsList: a click activates a finding, and the summary starts with a status dot in the good or bad tone, as the old list did
This commit is contained in:
@@ -5,16 +5,16 @@ use super::view_words::FindingsWords;
|
||||
use super::words::ListWords;
|
||||
use super::{ListRow, RowEvent};
|
||||
use crate::contract::{
|
||||
Bind, BindMut, BodyCx, Collection, CollectionProps, CollectionResponse, Common, Component, ContainerProps, Cx, MeasureCx, PaintUi, Props as _,
|
||||
Role, SelectMode, Show as _, Sizing, Styled as _, WordSetCx as _,
|
||||
ActivateOn, Bind, BindMut, BodyCx, Collection, CollectionProps, CollectionResponse, Common, Component, ContainerProps, Cx, MeasureCx, PaintUi,
|
||||
Props as _, Role, SelectMode, Show as _, Sizing, Styled as _, WordSetCx as _,
|
||||
};
|
||||
use crate::notices::{Notice, NoticeLayout};
|
||||
use crate::primitives::{IconSpec, TextRun};
|
||||
use crate::primitives::{IconSpec, TextRun, glyph};
|
||||
use crate::style::{ComponentStyle, IconLook, Stateful, StyleCx, SurfaceKind, TextLook, TypeRole};
|
||||
use craft_behavior::{DisplayOrder, Flow, IconId, Insets, ItemSource, Metrics, Selection, Sense, SpanLayout};
|
||||
use craft_i18n::Format;
|
||||
use craft_theme::variant::Tone;
|
||||
use egui::{Rect, Response, Ui, Vec2};
|
||||
use egui::{Response, Ui, Vec2};
|
||||
use std::borrow::Cow;
|
||||
|
||||
/// How serious a finding is. The glyph's shape differs per severity as well as its colour.
|
||||
@@ -146,7 +146,8 @@ pub struct FindingsListStyle {
|
||||
metrics: Metrics,
|
||||
}
|
||||
|
||||
/// The list of findings: one selected finding, Enter activates, and an empty list says so.
|
||||
/// The list of findings: one selected finding, a click or Enter activates it (the app goes to what it is about), and an
|
||||
/// empty list says so.
|
||||
#[derive(crate::contract::Component, crate::contract::Props)]
|
||||
#[component(collection)]
|
||||
pub struct FindingsList<'a> {
|
||||
@@ -166,7 +167,7 @@ impl<'a> FindingsList<'a> {
|
||||
FindingsList {
|
||||
common: Common::new(),
|
||||
props: ContainerProps::new().surface(SurfaceKind::Sunken).sizing(Sizing::Fill),
|
||||
items: CollectionProps::new().select(SelectMode::Single),
|
||||
items: CollectionProps::new().select(SelectMode::Single).activate(ActivateOn::Click),
|
||||
selection: None,
|
||||
}
|
||||
.label(label)
|
||||
@@ -225,15 +226,15 @@ pub struct FindingsSummaryStyle {
|
||||
metrics: Metrics,
|
||||
#[style(text)]
|
||||
text: TextLook,
|
||||
/// The icon when there are no errors.
|
||||
/// The status dot when there are no errors.
|
||||
#[style(icon)]
|
||||
good: IconLook,
|
||||
/// The icon when there are errors.
|
||||
/// The status dot when there are errors.
|
||||
#[style(icon)]
|
||||
bad: IconLook,
|
||||
}
|
||||
|
||||
/// The line over a list of findings: an icon that says whether there are errors, then the counts.
|
||||
/// The line over a list of findings: a status dot that says whether there are errors, then the counts.
|
||||
#[derive(crate::contract::Component)]
|
||||
pub struct FindingsSummary<'a> {
|
||||
#[common]
|
||||
@@ -254,10 +255,6 @@ impl<'a> FindingsSummary<'a> {
|
||||
fn has_errors(&self) -> bool {
|
||||
self.findings.iter().any(|finding| finding.severity == Severity::Error)
|
||||
}
|
||||
|
||||
fn icon(&self) -> IconId {
|
||||
if self.has_errors() { Severity::Error.icon() } else { IconId::new("success") }
|
||||
}
|
||||
}
|
||||
|
||||
impl Component for FindingsSummary<'_> {
|
||||
@@ -289,11 +286,11 @@ impl Component for FindingsSummary<'_> {
|
||||
|
||||
fn paint(&mut self, ui: &mut PaintUi<'_>, cx: &Cx<'_, Self>) {
|
||||
let rtl = cx.env.direction().is_rtl();
|
||||
let (glyph, rest) = Flow::new(cx.content_rect, rtl).take_start(cx.style.good.side);
|
||||
let (mark, rest) = Flow::new(cx.content_rect, rtl).take_start(cx.style.good.side);
|
||||
let (_, rest) = rest.take_start(cx.style.metrics.gap);
|
||||
let draw = ui.draw();
|
||||
let look = if self.has_errors() { &cx.style.bad } else { &cx.style.good };
|
||||
draw.icon(Rect::from_center_size(glyph.center(), Vec2::splat(look.side)), &IconSpec::svg(self.icon()).color(cx.resolve(&look.color)));
|
||||
draw.fill(&glyph::dot(mark.center(), look.side), cx.resolve(&look.color));
|
||||
let words = cx.words::<FindingsWords>();
|
||||
draw.text(rest.rect(), &TextRun::new(self.text(&words, cx.format())).color(cx.resolve(&cx.style.text.color)).elide());
|
||||
}
|
||||
|
||||
@@ -156,6 +156,25 @@ fn a_click_on_the_selected_event_deselects_it() {
|
||||
);
|
||||
}
|
||||
|
||||
/// A single click on a finding activates it, so the app goes to what it is about.
|
||||
#[trial]
|
||||
fn a_click_on_a_finding_activates_it() {
|
||||
use craft_ui::contract::WordSetCx as _;
|
||||
use craft_ui::list::{Finding, Findings, FindingsList, FindingsWords, Severity};
|
||||
let seen = Rc::new(RefCell::new(Vec::new()));
|
||||
let held = seen.clone();
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let findings: Vec<Finding> = NAMES.iter().enumerate().map(|(at, name)| Finding::new(at as u64, Severity::Warning, *name)).collect();
|
||||
let source = Findings::new(&findings);
|
||||
let out = FindingsList::new("Findings").show_items(ui, &source, &mut |key, icx| findings[*key as usize].row(&icx.words::<FindingsWords>()));
|
||||
held.borrow_mut().extend(out.events);
|
||||
});
|
||||
fixture.settle();
|
||||
let row = fixture.by_label("Artboard 2, Warning").expect("the second finding");
|
||||
fixture.click(&row).settle();
|
||||
assert!(seen.borrow().iter().any(|event| matches!(event, CollectionEvent::Activate(1))), "one click activates the finding");
|
||||
}
|
||||
|
||||
/// A list of the three rows built by `row`, in `room`, and what it reported.
|
||||
fn rows_rig(room: Vec2, row: fn(usize) -> ListRow<'static>) -> (Fixture, Rc<RefCell<Vec<CollectionEvent<usize, RowEvent>>>>) {
|
||||
let seen = Rc::new(RefCell::new(Vec::new()));
|
||||
|
||||
Reference in New Issue
Block a user