mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 03:17:43 +00:00
ui-shell: start screen takes picture shape, card width and page pictures; tile frames its picture
Part of storytold/wordcraft#7149, item 2 and item 3. StartScreen gets picture_aspect, card_width and page_pictures (white paper, no transparency grid, icon without a sunken tile); RecentList gets picture_aspect. Defaults keep today looks. A card tile now draws its frame over the picture so all four sides show, not only the top and bottom edge of the sunken well. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
243d68e12d
commit
2bd43ca27a
@@ -37,12 +37,13 @@ pub struct RecentList<'a> {
|
||||
empty: Option<Label>,
|
||||
now: Option<i64>,
|
||||
thumbnails: Option<ThumbHost<'a>>,
|
||||
picture: (u16, u16),
|
||||
}
|
||||
|
||||
impl<'a> RecentList<'a> {
|
||||
/// A list of `recent` files.
|
||||
pub fn new(recent: &'a [RecentFile]) -> Self {
|
||||
RecentList { recent, max_recent: usize::MAX, empty: None, now: None, thumbnails: None }
|
||||
RecentList { recent, max_recent: usize::MAX, empty: None, now: None, thumbnails: None, picture: (PICTURE_WIDE, PICTURE_HIGH) }
|
||||
}
|
||||
|
||||
/// At most this many recent files show.
|
||||
@@ -69,13 +70,21 @@ impl<'a> RecentList<'a> {
|
||||
self
|
||||
}
|
||||
|
||||
/// The shape of the row pictures, wide to high (default 16:9). A zero part keeps the default.
|
||||
pub fn picture_aspect(mut self, wide: u16, high: u16) -> Self {
|
||||
if wide > 0 && high > 0 {
|
||||
self.picture = (wide, high);
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
/// Shows the rows in their own section and reports what the user did.
|
||||
pub fn show(self, ui: &mut Ui) -> StartOutput {
|
||||
let RecentList { recent, max_recent, empty, now, thumbnails } = self;
|
||||
let RecentList { recent, max_recent, empty, now, thumbnails, picture } = self;
|
||||
let env = Env::of(ui);
|
||||
let words = env.words::<ShellWords>();
|
||||
let actions = RefCell::new(Vec::new());
|
||||
let page = Page { env: &env, words: &words, thumbnails, drop_hint: None, now, actions: &actions };
|
||||
let page = Page { env: &env, words: &words, thumbnails, picture, card_zoom: None, paper: None, drop_hint: None, now, actions: &actions };
|
||||
let files: Vec<RecentFile> = recent.iter().take(max_recent).cloned().collect();
|
||||
let empty = empty.unwrap_or_else(|| words.no_recent.clone());
|
||||
let shown = Section::fixed(words.recent.clone()).header_gap(Gap::Section).command_scope(StartRecentCommands::SCOPE).show(ui, |ui, cx| {
|
||||
@@ -168,8 +177,8 @@ pub(super) fn recent_body(page: &Page<'_>, ui: &mut Ui, cx: &mut BodyCx<'_>, fil
|
||||
match page.thumbnails {
|
||||
Some((thumbnails, source)) => {
|
||||
let key = ThumbKey::Recent(file.path.clone());
|
||||
row.thumb_aspect(ThumbSize::Medium, ThumbAspect::new(PICTURE_WIDE, PICTURE_HIGH), move |cx, rect| {
|
||||
paint_thumb(cx, rect, thumbnails, source, &key);
|
||||
row.thumb_aspect(ThumbSize::Medium, ThumbAspect::new(page.picture.0, page.picture.1), move |cx, rect| {
|
||||
paint_thumb(cx, rect, thumbnails, source, &key, page.paper);
|
||||
})
|
||||
}
|
||||
None => row.icon(IconSpec::svg(IconId::new("file"))),
|
||||
|
||||
@@ -24,7 +24,7 @@ use crate::runtime::commands::{self, CommandRef, FireSource};
|
||||
use crate::runtime::commands::{CommandSet as _, Commands as _};
|
||||
use crate::runtime::env::{AgeStyle, Env, Label};
|
||||
use crate::sections::Section;
|
||||
use crate::style::{Corners, SurfaceKind, SurroundLook, TypeRole};
|
||||
use crate::style::{Corners, DataColor, SurfaceKind, SurroundLook, TypeRole};
|
||||
use crate::tile_grid::{CaptionPlace, Tile, TileGrid};
|
||||
use craft_behavior::{IconId, Proposal, SelectionChange, keyed};
|
||||
use craft_theme::Variant;
|
||||
@@ -198,6 +198,16 @@ pub struct StartScreen<'a> {
|
||||
/// The text of the link that fires [`StartAction::MoreTemplates`].
|
||||
#[prop(into)]
|
||||
more_templates: Option<Label>,
|
||||
/// The width and height parts of a card and row picture (16:9 unless the pictures are another shape, as a page 3:4).
|
||||
picture: (u16, u16),
|
||||
/// The zoom of a card's picture, a factor on the card thumbnail: the cards keep that size and do not grow to fill
|
||||
/// the row, so as many as fit stand side by side (default: twice the card thumbnail, grown to fill the row).
|
||||
#[prop(quantity = "zoom factor")]
|
||||
card_zoom: Option<f32>,
|
||||
/// The pictures are opaque pages on this paper, which is document data: no transparency grid shows, and a file
|
||||
/// without a picture shows its icon on the card, not on a sunken tile (default: photos).
|
||||
#[prop]
|
||||
page_paper: Option<DataColor>,
|
||||
}
|
||||
|
||||
/// A link on the start screen's rail.
|
||||
@@ -251,9 +261,20 @@ impl<'a> StartScreen<'a> {
|
||||
layout: StartLayout::default(),
|
||||
title: None,
|
||||
more_templates: None,
|
||||
picture: (PICTURE_WIDE, PICTURE_HIGH),
|
||||
card_zoom: None,
|
||||
page_paper: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// The shape of the pictures of cards and rows, wide to high (default 16:9). A zero part keeps the default.
|
||||
pub fn picture_aspect(mut self, wide: u16, high: u16) -> Self {
|
||||
if wide > 0 && high > 0 {
|
||||
self.picture = (wide, high);
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
/// Cards, rows and templates ask `thumbnails.state(..)` for what they show; `source` gives each key's revision.
|
||||
pub fn thumbnails(mut self, thumbnails: &'a Thumbnails<ThumbKey>, source: &'a dyn ThumbSource<ThumbKey>) -> Self {
|
||||
self.thumbnails = Some((thumbnails, source));
|
||||
@@ -276,6 +297,12 @@ pub(super) struct Page<'p> {
|
||||
pub(super) env: &'p Env,
|
||||
pub(super) words: &'p ShellWords,
|
||||
pub(super) thumbnails: Option<ThumbHost<'p>>,
|
||||
/// The width and height parts of a picture.
|
||||
pub(super) picture: (u16, u16),
|
||||
/// The zoom of a card's picture, when the app sets one.
|
||||
pub(super) card_zoom: Option<f32>,
|
||||
/// The paper of page pictures, when the app's pictures are pages.
|
||||
pub(super) paper: Option<DataColor>,
|
||||
pub(super) drop_hint: Option<&'p Label>,
|
||||
pub(super) now: Option<i64>,
|
||||
pub(super) actions: &'p RefCell<Vec<StartAction>>,
|
||||
@@ -303,39 +330,54 @@ impl<'p> Page<'p> {
|
||||
tile = tile.detail(detail);
|
||||
}
|
||||
match self.thumbnails {
|
||||
Some((thumbnails, source)) => {
|
||||
tile.thumb(ThumbAspect::new(PICTURE_WIDE, PICTURE_HIGH), move |cx, rect| paint_thumb(cx, rect, thumbnails, source, &key))
|
||||
}
|
||||
Some((thumbnails, source)) => tile.thumb(ThumbAspect::new(self.picture.0, self.picture.1), {
|
||||
let paper = self.paper;
|
||||
move |cx, rect| paint_thumb(cx, rect, thumbnails, source, &key, paper)
|
||||
}),
|
||||
None => tile.icon(IconId::new("file")),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn grid<K: Clone + Eq + std::hash::Hash + Send + Sync + 'static>(&self, label: Label, deletable: bool) -> TileGrid<'static, K> {
|
||||
TileGrid::<K>::new(label)
|
||||
let grid = TileGrid::<K>::new(label)
|
||||
.select(SelectMode::Single)
|
||||
.deletable(deletable)
|
||||
.activate(ActivateOn::Click)
|
||||
.caption(CaptionPlace::Below)
|
||||
.detail_line()
|
||||
.card()
|
||||
.aspect(f32::from(PICTURE_WIDE) / f32::from(PICTURE_HIGH))
|
||||
.zoom(CARD_ZOOM)
|
||||
.fill_row()
|
||||
.sizing(Sizing::FillWidth)
|
||||
.natural_height(true)
|
||||
.aspect(f32::from(self.picture.0) / f32::from(self.picture.1));
|
||||
let grid = match self.card_zoom {
|
||||
Some(zoom) => grid.zoom(zoom),
|
||||
None => grid.zoom(CARD_ZOOM).fill_row(),
|
||||
};
|
||||
grid.sizing(Sizing::FillWidth).natural_height(true)
|
||||
}
|
||||
}
|
||||
|
||||
/// The thumbnail of `key` in `rect`, asked for at the size it paints. The store paints the placeholder while the
|
||||
/// picture is not ready and the failure mark when it failed; a key the source has no picture for (`Blank`) gets the
|
||||
/// file icon on the sunken tile.
|
||||
pub(super) fn paint_thumb(cx: &ContentPainterCx, rect: Rect, thumbnails: &Thumbnails<ThumbKey>, source: &dyn ThumbSource<ThumbKey>, key: &ThumbKey) {
|
||||
pub(super) fn paint_thumb(
|
||||
cx: &ContentPainterCx,
|
||||
rect: Rect,
|
||||
thumbnails: &Thumbnails<ThumbKey>,
|
||||
source: &dyn ThumbSource<ThumbKey>,
|
||||
key: &ThumbKey,
|
||||
paper: Option<DataColor>,
|
||||
) {
|
||||
let request = ThumbRequest::for_cell(rect.width().max(rect.height()), cx.pixels_per_point());
|
||||
let state = thumbnails.state(cx.painter.ctx(), source, key, &request, Priority::VISIBLE);
|
||||
if thumbnails.paint(cx, rect, &state, &ThumbPaint::default()) {
|
||||
let paint = match paper {
|
||||
Some(paper) => ThumbPaint::default().checker(false).paper(paper),
|
||||
None => ThumbPaint::default(),
|
||||
};
|
||||
if thumbnails.paint(cx, rect, &state, &paint) {
|
||||
return;
|
||||
}
|
||||
cx.surface(rect, SurfaceKind::Sunken, Corners::None);
|
||||
if paper.is_none() {
|
||||
cx.surface(rect, SurfaceKind::Sunken, Corners::None);
|
||||
}
|
||||
let side = cx.style_cx().tokens.size.icon.lg;
|
||||
cx.icon(Rect::from_center_size(rect.center(), Vec2::splat(side)), &IconSpec::svg(IconId::new("file")));
|
||||
}
|
||||
@@ -370,13 +412,39 @@ pub(super) fn read_events<K: Clone, E>(events: Vec<CollectionEvent<K, E>>, lead:
|
||||
impl StartScreen<'_> {
|
||||
/// Shows the page across the available room and reports what the user did.
|
||||
pub fn show(self, ui: &mut Ui) -> StartOutput {
|
||||
let StartScreen { view, sort, recent, pinned, templates, max_recent, now, thumbnails, links, drop_hint, layout, title, more_templates } =
|
||||
self;
|
||||
let StartScreen {
|
||||
view,
|
||||
sort,
|
||||
recent,
|
||||
pinned,
|
||||
templates,
|
||||
max_recent,
|
||||
now,
|
||||
thumbnails,
|
||||
links,
|
||||
drop_hint,
|
||||
layout,
|
||||
title,
|
||||
more_templates,
|
||||
picture,
|
||||
card_zoom,
|
||||
page_paper,
|
||||
} = self;
|
||||
let env = Env::of(ui);
|
||||
let collator = env.collator();
|
||||
let words = env.words::<ShellWords>();
|
||||
let actions = RefCell::new(Vec::new());
|
||||
let page = Page { env: &env, words: &words, thumbnails, drop_hint: drop_hint.as_ref(), now, actions: &actions };
|
||||
let page = Page {
|
||||
env: &env,
|
||||
words: &words,
|
||||
thumbnails,
|
||||
picture,
|
||||
card_zoom,
|
||||
paper: page_paper,
|
||||
drop_hint: drop_hint.as_ref(),
|
||||
now,
|
||||
actions: &actions,
|
||||
};
|
||||
let view = RefCell::new(view);
|
||||
let sort = RefCell::new(sort);
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//! `StartScreen`: which part of the page takes a double click, and what a recent file's menu offers.
|
||||
|
||||
use craft_ui::contract::{Bind, Role};
|
||||
use craft_ui::shell::{RecentFile, RecentList, RecentSort, RecentView, StartAction, StartLink, StartScreen};
|
||||
use craft_ui::shell::{RecentFile, RecentList, RecentSort, RecentView, StartAction, StartLayout, StartLink, StartScreen, Template};
|
||||
use craft_ui::testing::pointer::{moved, secondary};
|
||||
use craft_ui::testing::{Fixture, Target, trial};
|
||||
use egui::{Key, Modifiers};
|
||||
@@ -177,3 +177,27 @@ fn an_empty_recent_list_says_so_at_the_reading_start() {
|
||||
assert!(line.rect.left() < 60.0, "the line starts at the list's start edge: {:?}", line.rect);
|
||||
assert!(line.rect.width() < 300.0, "the line is as wide as its text: {:?}", line.rect);
|
||||
}
|
||||
|
||||
/// The template cards of a greeting page, as `(width, height)` of the card named "Memo", for the knobs `shape` sets.
|
||||
fn memo_card(shape: impl for<'a> Fn(StartScreen<'a>) -> StartScreen<'a> + 'static) -> egui::Vec2 {
|
||||
let templates = vec![Template::new("memo", "Memo", ""), Template::new("report", "Report", "")];
|
||||
let (mut view, mut sort) = (RecentView::List, RecentSort::Opened);
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let _ =
|
||||
shape(StartScreen::new(Bind::bound(&mut view), Bind::bound(&mut sort), &[]).layout(StartLayout::Greeting).templates(&templates)).show(ui);
|
||||
});
|
||||
fixture.room(egui::Vec2::new(1100.0, 700.0));
|
||||
fixture.settle();
|
||||
fixture.by_label("Memo").expect("the card shows").rect.size()
|
||||
}
|
||||
|
||||
/// An app can give its card pictures a shape and a fixed width: by default the cards are 16:9 and grow to fill the row,
|
||||
/// with `picture_aspect(3, 4)` and `card_zoom(1.0)` they are portrait and stay narrow.
|
||||
#[trial]
|
||||
fn a_start_screen_takes_the_shape_and_width_of_its_card_pictures() {
|
||||
let default = memo_card(|screen| screen);
|
||||
assert!(default.x > default.y * 1.3, "the default card is a wide 16:9 card: {default:?}");
|
||||
let page = memo_card(|screen| screen.picture_aspect(3, 4).card_zoom(1.0));
|
||||
assert!(page.y > page.x, "a 3:4 card is taller than wide: {page:?}");
|
||||
assert!(page.x < default.x * 0.7, "a card at zoom 1 does not grow to fill the row: {page:?}");
|
||||
}
|
||||
|
||||
@@ -844,12 +844,18 @@ impl Component for Tile<'_> {
|
||||
let placeholder = if cx.env.surface() == SurfaceKind::Sunken { style.placeholder_on_sunken } else { style.placeholder };
|
||||
ui.draw().fill(&PaintShape::Rect(picture, flat), placeholder);
|
||||
}
|
||||
let framed = style.sizes.card && (self.first.is_some() || self.picture.is_some());
|
||||
if let Some(paint) = self.first.take() {
|
||||
let _ = ui.child("picture", picture, RowThumb::new(paint));
|
||||
}
|
||||
if let Some(shown) = self.picture.take() {
|
||||
let _ = ui.child("picture", picture, shown);
|
||||
}
|
||||
if framed {
|
||||
// A picture covers the sunken well under it, which would leave only its top and bottom edge showing: the
|
||||
// frame goes over the picture so all four sides show.
|
||||
ui.draw().surface(picture, SurfaceKind::Frame, Corners::All);
|
||||
}
|
||||
{
|
||||
let draw = ui.draw();
|
||||
let face = self.icon.take().or_else(|| self.folder.then(|| IconSpec::svg(IconId::new("folder"))));
|
||||
|
||||
Reference in New Issue
Block a user