mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 02:09:00 +00:00
FontPicker: the name column is as wide as its widest row and at least the field's sample name (ColumnSpec::at_least); library list leaves always aligned; one SAMPLE_EM; star trial checks the favourite and the family
This commit is contained in:
@@ -375,6 +375,8 @@ const WARNING: PartKey = PartKey::new("missing");
|
||||
const WARNING_ICON: &str = "warning";
|
||||
/// The popup the chevron and Alt+Down toggle.
|
||||
const POPUP: &str = "list";
|
||||
/// The share of a sample band's height the sample's em takes, in the list's rows and the preview banner.
|
||||
const SAMPLE_EM: f32 = 0.7;
|
||||
/// The widest family names in common use, which the field's width is sized by.
|
||||
const NAME_SAMPLE: &str = "Source Sans Pro Semibold Condensed";
|
||||
|
||||
@@ -618,8 +620,7 @@ pub struct FontPickerStyle {
|
||||
/// The list's sample height and family row height at each preview size, none first: the thumbnail sides
|
||||
/// (`size.thumb.*`) and the height of a list row holding one.
|
||||
pub(crate) samples: [Sample; 4],
|
||||
/// The width of the list's name column: [`NAME_SAMPLE`] in a row's text, so the list holds the names the field
|
||||
/// holds.
|
||||
/// The least width of the list's name column: [`NAME_SAMPLE`] in a row's text, the names the field holds.
|
||||
pub(crate) name_column: f32,
|
||||
}
|
||||
|
||||
|
||||
@@ -14,12 +14,11 @@ use egui::{Color32, Pos2, Rect, Vec2};
|
||||
use crate::panel::faces::{self, Shown};
|
||||
use crate::panel::specimen::SampleFace;
|
||||
|
||||
use super::SAMPLE_EM;
|
||||
|
||||
#[cfg(feature = "suite")]
|
||||
pub(crate) mod stories;
|
||||
|
||||
/// The share of the sample band's height the sample's em takes, as in the list's rows.
|
||||
const SAMPLE_EM: f32 = 0.7;
|
||||
|
||||
/// The style of the banner: the well's padding, the name line and the sample band.
|
||||
#[derive(Clone, Debug, PartialEq, ComponentStyle)]
|
||||
#[non_exhaustive]
|
||||
|
||||
@@ -23,19 +23,18 @@ use craft_ui::runtime::env::Label;
|
||||
use super::banner::FontPreviewBanner;
|
||||
use super::rank::{FamilyInfos, Language, Listing, list, shown_name};
|
||||
use super::{
|
||||
FamilyRow, FontClass, FontFilters, FontFormat, FontPicker, FontPickerKind, FontPickerSettings, FontPickerWords, POPUP, Typing, infos_of, read,
|
||||
ui_language, write,
|
||||
FamilyRow, FontClass, FontFilters, FontFormat, FontPicker, FontPickerKind, FontPickerSettings, FontPickerWords, POPUP, SAMPLE_EM, Typing,
|
||||
infos_of, read, ui_language, write,
|
||||
};
|
||||
use crate::panel::faces::Owner;
|
||||
use crate::panel::specimen::{FontSample, SampleFace};
|
||||
|
||||
/// The share of a sample band's height the sample's em takes.
|
||||
const SAMPLE_EM: f32 = 0.7;
|
||||
/// The sample column is this many times as wide as the sample band is high.
|
||||
const SAMPLE_WIDTH: f32 = 4.0;
|
||||
/// The star column: a favourite's star, on every family row.
|
||||
const STAR: ColumnKey = ColumnKey::new("star");
|
||||
/// The column of the name, the disclosure, the check and, at its end, the format badge.
|
||||
/// The column of the name, the disclosure, the check and, at its end, the format badge: as wide as its widest row, and
|
||||
/// at least as wide as the names the field holds.
|
||||
const NAME: ColumnKey = ColumnKey::new("name");
|
||||
/// The column of the sample in the family's face.
|
||||
const SAMPLE: ColumnKey = ColumnKey::new("sample");
|
||||
@@ -373,7 +372,7 @@ pub(super) fn popup<'a>(picker: &mut FontPicker<'a>, ui: &mut OverlayUi<'_>, cx:
|
||||
if !plain {
|
||||
columns.push(ColumnSpec::toggle(STAR, words.favorites.clone(), ToggleColumn::default()).icon(IconId::new("star")));
|
||||
}
|
||||
columns.push(ColumnSpec::new(NAME, words.list.clone(), ColumnWidth::Fixed(cx.style.name_column)));
|
||||
columns.push(ColumnSpec::new(NAME, words.list.clone(), ColumnWidth::Content).at_least(cx.style.name_column));
|
||||
if size != ThumbSize::None {
|
||||
columns.push(ColumnSpec::new(SAMPLE, words.sample.clone(), ColumnWidth::Fixed(sample_size.side * SAMPLE_WIDTH)));
|
||||
}
|
||||
@@ -425,8 +424,9 @@ pub(super) fn popup<'a>(picker: &mut FontPicker<'a>, ui: &mut OverlayUi<'_>, cx:
|
||||
return;
|
||||
}
|
||||
let source = Slots { slots: &slots, row: sample_size.row };
|
||||
// Names line up past the disclosure column only where a family has styles to open.
|
||||
let leaves = if (0..source.len()).any(|at| source.has_children(at)) { TreeLeaves::Aligned } else { TreeLeaves::Compact };
|
||||
// The library list keeps the disclosure column on every row, so names stay put while a query
|
||||
// narrows the list; the plain list has no styles to open.
|
||||
let leaves = if plain { TreeLeaves::Compact } else { TreeLeaves::Aligned };
|
||||
let selection =
|
||||
current.filter(|_| query.trim().is_empty()).map_or_else(Selection::default, |at| Selection::single(slots[at].key()));
|
||||
let rows = List::<SlotKey>::options(name.clone())
|
||||
|
||||
@@ -476,30 +476,36 @@ fn the_missing_font_warning_icon_paints_in_status_warn_fg_and_dims_when_disabled
|
||||
);
|
||||
}
|
||||
|
||||
/// The library picker's family and settings, as the last pass left them.
|
||||
struct Library {
|
||||
family: String,
|
||||
settings: FontPickerSettings,
|
||||
}
|
||||
|
||||
/// A library list with Hack as a recent family: the shown picker and the catalog of Inter (two styles) and Hack (one).
|
||||
fn library_rig() -> Fixture {
|
||||
fn library_rig() -> (Fixture, Rc<RefCell<Library>>) {
|
||||
let catalog = catalog_of(&[INTER_REGULAR, INTER_SEMIBOLD, epaint_default_fonts::HACK_REGULAR]);
|
||||
let mut family = "Inter".to_owned();
|
||||
let mut settings = FontPickerSettings::default();
|
||||
settings.recent = vec!["Hack".to_owned()];
|
||||
let state = Rc::new(RefCell::new(Library { family: "Inter".to_owned(), settings }));
|
||||
let shown = Rc::clone(&state);
|
||||
let mut filters = FontFilters::default();
|
||||
let mut fx = Fixture::new(move |ui| {
|
||||
FontPicker::new("Font family", Bind::bound(&mut family), &catalog)
|
||||
.settings(Bind::bound(&mut settings))
|
||||
.filters(Bind::bound(&mut filters))
|
||||
.show(ui);
|
||||
let mut state = shown.borrow_mut();
|
||||
let Library { family, settings } = &mut *state;
|
||||
FontPicker::new("Font family", Bind::bound(family), &catalog).settings(Bind::bound(settings)).filters(Bind::bound(&mut filters)).show(ui);
|
||||
});
|
||||
fx.in_room(Room::Window);
|
||||
fx.settle();
|
||||
open(&mut fx);
|
||||
fx
|
||||
(fx, state)
|
||||
}
|
||||
|
||||
/// Production's and Photoshop's row: the star first, then the names in one column whether or not a family has styles
|
||||
/// to open (a recent row included), the sample after the name; and a rule, not blank room, ends the recent group.
|
||||
#[trial]
|
||||
fn family_names_line_up_after_the_star_and_before_the_sample() {
|
||||
let fx = library_rig();
|
||||
let (fx, _) = library_rig();
|
||||
let banner = fx.by_label("Font preview: Inter").expect("the banner names the current family").rect;
|
||||
let names: Vec<egui::Rect> = fx
|
||||
.texts()
|
||||
@@ -515,25 +521,33 @@ fn family_names_line_up_after_the_star_and_before_the_sample() {
|
||||
let sample =
|
||||
fx.texts().iter().all(|painted| painted.text != "Sample" || painted.rect.top() < banner.bottom() || painted.rect.left() >= inter.right());
|
||||
assert!(sample, "no sample before a name");
|
||||
// The recent group's row is the top one; the rule lies between it and the next name, lower than the room between.
|
||||
let recent = names.iter().map(|rect| rect.bottom()).fold(f32::INFINITY, f32::min);
|
||||
let rule = fx.shape_bounds().iter().any(|shape| shape.top() >= recent && shape.height() < 2.0 && shape.width() > star.width() * 4.0);
|
||||
let next = names.iter().map(|rect| rect.top()).filter(|top| *top > recent).fold(f32::INFINITY, f32::min);
|
||||
let rule = fx
|
||||
.shape_bounds()
|
||||
.iter()
|
||||
.any(|shape| shape.top() >= recent && shape.bottom() <= next && shape.height() < (next - recent) && shape.width() > star.width() * 4.0);
|
||||
assert!(rule, "a rule under the recent group");
|
||||
}
|
||||
|
||||
/// A click on a row's star stars the family without picking it or closing the list.
|
||||
#[trial]
|
||||
fn a_star_flips_the_favourite_without_picking_or_closing() {
|
||||
let mut fx = library_rig();
|
||||
let (mut fx, state) = library_rig();
|
||||
let star = fx.by_label("Favorite Hack").expect("Hack's star");
|
||||
fx.click(&star);
|
||||
fx.settle();
|
||||
assert!(fx.by_label("Favorite Inter").is_some(), "the list stays open");
|
||||
let state = state.borrow();
|
||||
assert_eq!(state.settings.favorites, ["Hack"], "Hack is starred");
|
||||
assert_eq!(state.family, "Inter", "the star picks nothing");
|
||||
}
|
||||
|
||||
/// The class column is a list of options: a class narrows the list, All Classes brings every family back.
|
||||
#[trial]
|
||||
fn a_class_narrows_the_list_and_all_classes_restores_it() {
|
||||
let mut fx = library_rig();
|
||||
let (mut fx, _) = library_rig();
|
||||
let monospace = fx.by_label("Monospace").expect("the Monospace class");
|
||||
fx.click(&monospace);
|
||||
fx.settle();
|
||||
|
||||
@@ -93,6 +93,9 @@ pub struct ColumnSpec {
|
||||
/// The most of the collection's width a `Content` column takes, as a share: a name column as wide as its widest
|
||||
/// name, at most a third of the table.
|
||||
pub max_share: Option<f32>,
|
||||
/// The least width of a `Content` column, whatever its cells hold: a list's name column at least as wide as the
|
||||
/// names the field beside it holds.
|
||||
pub min_width: f32,
|
||||
/// The header menu has a check for the column, so the user can hide it. A column that can't be hidden always shows.
|
||||
pub hideable: bool,
|
||||
/// A remove control at the header's end, which reports `ColumnEvent::Removed`.
|
||||
@@ -134,6 +137,7 @@ impl ColumnSpec {
|
||||
icon: None,
|
||||
tip: None,
|
||||
max_share: None,
|
||||
min_width: 0.0,
|
||||
hideable: false,
|
||||
remove: None,
|
||||
}
|
||||
@@ -208,6 +212,13 @@ impl ColumnSpec {
|
||||
self
|
||||
}
|
||||
|
||||
/// A `Content` column is at least `width` wide.
|
||||
#[must_use]
|
||||
pub fn at_least(mut self, width: f32) -> Self {
|
||||
self.min_width = width;
|
||||
self
|
||||
}
|
||||
|
||||
/// The header's tooltip.
|
||||
#[must_use]
|
||||
pub fn tip(mut self, tip: impl Into<Label>) -> Self {
|
||||
|
||||
@@ -157,8 +157,9 @@ pub(super) struct Header {
|
||||
/// The columns whose header cell the collection hosts a component in (`Collection::hosts_header`): they paint no
|
||||
/// label and are no sort target.
|
||||
hosted: Vec<ColumnKey>,
|
||||
/// The widest cell of each `Content` column among all rows, or its header label when wider, from the items'
|
||||
/// measure of this pass (`content_id`); zero for the other columns and while unknown.
|
||||
/// The widest cell of each `Content` column among all rows, or its header label or its least width
|
||||
/// (`ColumnSpec::min_width`) when wider, from the items' measure of this pass (`content_id`); zero for the other
|
||||
/// columns and while unknown.
|
||||
content: Vec<f32>,
|
||||
/// The column a double click on its edge fits to its widest cell in view, with its width as shown.
|
||||
pub(super) fit: Option<(ColumnKey, f32)>,
|
||||
@@ -271,8 +272,10 @@ impl Header {
|
||||
.specs
|
||||
.iter()
|
||||
.map(|spec| {
|
||||
let measured = matches!(spec.width, ColumnWidth::Content).then(|| layout_memory::measured(ctx, content_id(id, &spec.key))).flatten();
|
||||
measured.map_or(0.0, |measured| measured.took.x)
|
||||
if !matches!(spec.width, ColumnWidth::Content) {
|
||||
return 0.0;
|
||||
}
|
||||
layout_memory::measured(ctx, content_id(id, &spec.key)).map_or(0.0, |measured| measured.took.x).max(spec.min_width)
|
||||
})
|
||||
.collect();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user