mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 05:14:24 +00:00
Tooltips: a control that paints no label shows its own label, and the hints and names the old controls showed
A control whose own label is not painted (an icon, a glyph label, a caption-less tile, a chip, the stroke button) shows that label as its tooltip inside a field too; one named only by its field label shows none. Restores the old tooltips of the ReferencePoint points, the list disclosure, the PeopleBar avatars, the recent files, Distribute Rows Evenly, a page that failed to render, the font class mark (ListRow::trailing_named) and the Character hints (SideLabel::tooltip).
This commit is contained in:
@@ -43,8 +43,8 @@ struct Opened(Option<Option<u64>>);
|
||||
/// [`StrokeEditor`] in a popover under it.
|
||||
///
|
||||
/// - The field shows a line sample, the style's name (Solid, Dashed, Dotted, Custom; "No Color" while
|
||||
/// [`stroked`](Self::stroked) is false) and a chevron. Its accessible name is the title, "Stroke Options", with the
|
||||
/// style's name as its value.
|
||||
/// [`stroked`](Self::stroked) is false) and a chevron. Its accessible name and tooltip are the title, "Stroke
|
||||
/// Options", with the style's name as its value.
|
||||
/// - A click, Enter or Space opens the popover below the field and focus moves into it; Escape closes it and returns
|
||||
/// focus to the field; a press outside closes it and reaches nothing under it. It closes when the
|
||||
/// [`target`](Self::target) changes and when the field is disabled.
|
||||
@@ -122,6 +122,7 @@ impl<'a> StrokeButton<'a> {
|
||||
|
||||
impl Component for StrokeButton<'_> {
|
||||
const ROLE: Role = Role::BUTTON;
|
||||
const SHOWS_LABEL: bool = false;
|
||||
type Style = StrokeButtonStyle;
|
||||
type Output = EditResponse<StrokeStyle>;
|
||||
|
||||
|
||||
@@ -221,7 +221,7 @@ pub(super) fn popup<'a>(picker: &mut FontPicker<'a>, ui: &mut OverlayUi<'_>, cx:
|
||||
let star = TrailingFace::Icon(IconSpec::svg(IconId::new(icon)));
|
||||
let line = ListRow::new(row.shown.clone()).option().check(current == Some(*key)).trailing_part(STAR, star, name);
|
||||
let line = match row.class {
|
||||
Some(class) => line.trailing(class_mark(class, &words)),
|
||||
Some(class) => line.trailing_named(class_mark(class, &words), class_label(class, &words)),
|
||||
None => line,
|
||||
};
|
||||
match size {
|
||||
|
||||
@@ -7,20 +7,19 @@ mod stories;
|
||||
|
||||
use super::view::PageSource;
|
||||
use craft_behavior::IconId;
|
||||
use craft_behavior::{Input, Metrics, NavMove, Sense};
|
||||
use craft_behavior::{Input, Metrics, NavMove, PartKey, Sense};
|
||||
use craft_ui::buttons::IconButton;
|
||||
use craft_ui::contract::{
|
||||
Common, Component, Cx, Focus as _, MeasureCx, NoValue, Outcome, PaintUi, Props as _, Role, Show as _, ShowContainer as _, clicked,
|
||||
Common, Component, Cx, Focus as _, MeasureCx, NoValue, Outcome, PaintUi, PartsCx, Props as _, Role, Show as _, ShowContainer as _, clicked,
|
||||
};
|
||||
use craft_ui::dialog::Dialog;
|
||||
use craft_ui::layout_components::Row;
|
||||
use craft_ui::primitives::textures::{
|
||||
MAX_TEXTURE, Priority, TextureState, TextureStoreOptions, ThumbPaint, ThumbRequest, ThumbnailOptions, Thumbnails,
|
||||
MAX_TEXTURE, Priority, TextureState, TextureStoreOptions, TextureWords, ThumbPaint, ThumbRequest, ThumbnailOptions, Thumbnails,
|
||||
};
|
||||
use craft_ui::primitives::{PaintShape, TextRun};
|
||||
use craft_ui::runtime::env::Env;
|
||||
use craft_ui::runtime::focus::{ActivateKeys, KeepKeys, KeyUse};
|
||||
use craft_ui::runtime::memory::Remember as _;
|
||||
use craft_ui::style::{ComponentStyle, DataColor, StyleColor, StyleCx, TypeRole};
|
||||
use egui::{CornerRadius, Rect, Response, Ui, Vec2};
|
||||
use std::cell::Cell;
|
||||
@@ -152,11 +151,21 @@ struct PagePicture<'a> {
|
||||
unavailable: &'a str,
|
||||
paper: DataColor,
|
||||
step: isize,
|
||||
/// The page's picture as step 7 asked for it, which `paint` shows.
|
||||
state: Option<TextureState>,
|
||||
}
|
||||
|
||||
/// The page's picture; a page that failed to render gives the reason as its tooltip.
|
||||
const PICTURE: PartKey = PartKey::new("picture");
|
||||
|
||||
impl<'a> PagePicture<'a> {
|
||||
fn new(source: &'a dyn PageSource, page: usize, unavailable: &'a str, paper: DataColor) -> Self {
|
||||
PagePicture { common: Common::new(), source, page, unavailable, paper, step: 0 }
|
||||
PagePicture { common: Common::new(), source, page, unavailable, paper, step: 0, state: None }
|
||||
}
|
||||
|
||||
/// The page pictures of the component `id`.
|
||||
fn thumbnails(ctx: &egui::Context, id: egui::Id) -> Thumbnails<usize> {
|
||||
Thumbnails::<usize>::in_context(ctx, id.with("pictures"), ThumbnailOptions::new().max_side(MAX_TEXTURE).store(TextureStoreOptions::pages()))
|
||||
}
|
||||
|
||||
/// The part of the room the page fills, whole page and no larger than the ideal.
|
||||
@@ -204,19 +213,26 @@ impl Component for PagePicture<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
fn parts(&mut self, rect: Rect, cx: &mut PartsCx<'_, Self>) {
|
||||
let ctx = cx.ui.ctx().clone();
|
||||
let thumbs = Self::thumbnails(&ctx, cx.id);
|
||||
let shape = self.fitted(rect.size());
|
||||
let request = ThumbRequest::for_cell(shape.max_elem(), ctx.pixels_per_point());
|
||||
let state = thumbs.state(&ctx, self.source, &self.page, &request, Priority::VISIBLE);
|
||||
if let TextureState::Failed { failure, .. } = &state {
|
||||
let reason = failure.text(&cx.env.words::<TextureWords>()).to_owned();
|
||||
cx.part(PICTURE, Rect::from_center_size(rect.center(), shape)).tooltip(reason);
|
||||
}
|
||||
self.state = Some(state);
|
||||
}
|
||||
|
||||
fn paint(&mut self, ui: &mut PaintUi<'_>, cx: &Cx<'_, Self>) {
|
||||
let Some(state) = self.state.take() else { return };
|
||||
let painter = ui.content_painter(cx);
|
||||
let ctx = painter.painter.ctx().clone();
|
||||
let thumbs = Thumbnails::<usize>::in_context(
|
||||
&ctx,
|
||||
cx.key("pictures"),
|
||||
ThumbnailOptions::new().max_side(MAX_TEXTURE).store(TextureStoreOptions::pages()),
|
||||
);
|
||||
let thumbs = Self::thumbnails(&ctx, cx.id);
|
||||
let area = cx.rect;
|
||||
let shape = self.fitted(area.size());
|
||||
let rect = Rect::from_center_size(area.center(), shape);
|
||||
let request = ThumbRequest::for_cell(shape.max_elem(), painter.pixels_per_point());
|
||||
let state = thumbs.state(&ctx, self.source, &self.page, &request, Priority::VISIBLE);
|
||||
let rect = Rect::from_center_size(area.center(), self.fitted(area.size()));
|
||||
// The paper shows under a page that has no picture yet; under a picture the thumbnail paints it.
|
||||
ui.draw().data_fill(&PaintShape::Rect(rect, CornerRadius::ZERO), self.paper);
|
||||
thumbs.paint(&painter, rect, &state, &ThumbPaint::default().paper(self.paper));
|
||||
|
||||
@@ -31,6 +31,7 @@ craft_ui::words! {
|
||||
exactly: "tableProperties.exactly" = "Exactly";
|
||||
column_width: "tableProperties.columnWidth" = "Column Width";
|
||||
distribute_rows: "tableProperties.distributeRows" = "Distribute Rows Evenly";
|
||||
distribute_rows_tip: "tableProperties.distributeRowsTip" = "Equal fixed heights for the selected rows; cell text may become overset.";
|
||||
cell_text: "tableProperties.cellText" = "Cell Text";
|
||||
cell_align: "tableProperties.cellAlign" = "Vertical Alignment";
|
||||
align_top: "tableProperties.alignTop" = "Top";
|
||||
@@ -237,7 +238,8 @@ impl<'a> TableProperties<'a> {
|
||||
set(choose(ui, &words.row_height_mode, v.row_height_mode, &modes, TableChange::RowHeightMode));
|
||||
set(length(ui, &words.row_height, v.row_height, unit, TableChange::RowHeight));
|
||||
set(length(ui, &words.column_width, v.column_width, unit, TableChange::ColumnWidth));
|
||||
set(button(ui, &words.distribute_rows, true, TableChange::DistributeRows));
|
||||
let distribute = Button::new(words.distribute_rows.clone()).tooltip(words.distribute_rows_tip.clone()).show(ui);
|
||||
set(clicked(&distribute).then_some(TableChange::DistributeRows));
|
||||
GroupBox::new(words.cell_text.clone(), None).show(ui, |ui, _| {
|
||||
let aligns = [
|
||||
Entry::new(CellAlign::Top, words.align_top.clone()),
|
||||
|
||||
@@ -102,7 +102,7 @@ impl<'a> PeopleBar<'a> {
|
||||
if let Some(me) = me {
|
||||
let action = if me.spotlight { words.stop_spotlight.clone() } else { words.spotlight.clone() };
|
||||
let avatar =
|
||||
Avatar::new(me.name.clone()).fill(me.color).pressable(true).selected(me.spotlight).description(action.as_str().to_owned());
|
||||
Avatar::new(me.name.clone()).fill(me.color).pressable(true).selected(me.spotlight).tooltip(action.as_str().to_owned());
|
||||
if avatar.show(ui).clicked {
|
||||
requests.push(CollabRequest::Spotlight(!me.spotlight));
|
||||
}
|
||||
@@ -116,7 +116,7 @@ impl<'a> PeopleBar<'a> {
|
||||
} else {
|
||||
action
|
||||
};
|
||||
let avatar = Avatar::new(person.name.clone()).fill(person.color).pressable(true).selected(followed).description(name).show(ui);
|
||||
let avatar = Avatar::new(person.name.clone()).fill(person.color).pressable(true).selected(followed).tooltip(name).show(ui);
|
||||
if avatar.clicked {
|
||||
requests.push(CollabRequest::Follow(if followed { None } else { Some(person.id) }));
|
||||
}
|
||||
|
||||
@@ -55,6 +55,9 @@ craft_ui::words! {
|
||||
metrics: "character.metrics" = "Metrics";
|
||||
optical: "character.optical" = "Optical";
|
||||
tracking: "character.tracking" = "Tracking";
|
||||
leading_hint: "character.leadingHint" = "Leading (set to the font size × auto-leading when Auto)";
|
||||
kerning_hint: "character.kerningHint" = "Kerning: Metrics, Optical or a value in 1/1000 em (Alt+←/→ at the caret)";
|
||||
tracking_hint: "character.trackingHint" = "Tracking (1/1000 em)";
|
||||
vertical_scale: "character.verticalScale" = "Vertical scale";
|
||||
horizontal_scale: "character.horizontalScale" = "Horizontal scale";
|
||||
baseline_shift: "character.baselineShift" = "Baseline shift";
|
||||
@@ -477,6 +480,7 @@ impl<'a> CharacterControls<'a> {
|
||||
let size = LengthRow {
|
||||
label: words.size.clone(),
|
||||
glyph: GlyphLabel::text("tT"),
|
||||
hint: None,
|
||||
value: values.size,
|
||||
range: SIZE_RANGE,
|
||||
presets: &size_presets,
|
||||
@@ -486,6 +490,7 @@ impl<'a> CharacterControls<'a> {
|
||||
let leading = LengthRow {
|
||||
label: words.leading.clone(),
|
||||
glyph: GlyphLabel::text("A↕"),
|
||||
hint: Some(words.leading_hint.clone()),
|
||||
value: leading_value,
|
||||
range: LEADING_RANGE,
|
||||
presets: &leading_presets,
|
||||
@@ -497,11 +502,19 @@ impl<'a> CharacterControls<'a> {
|
||||
}
|
||||
});
|
||||
Grid::new(&TWO_COLUMNS).fill_width().show(ui, |ui, _| {
|
||||
let event = lengths.kerning(ui, words.kerning.clone(), GlyphLabel::icon(IconId::new("text_cursor")), kerning_shown, &kerning_presets);
|
||||
let event = lengths.kerning(
|
||||
ui,
|
||||
words.kerning.clone(),
|
||||
GlyphLabel::icon(IconId::new("text_cursor")),
|
||||
words.kerning_hint.clone(),
|
||||
kerning_shown,
|
||||
&kerning_presets,
|
||||
);
|
||||
edits.extend(event.map(|value| CharacterChange::Kerning(kerning_of(value))));
|
||||
let tracking = PlainRow {
|
||||
label: words.tracking.clone(),
|
||||
glyph: GlyphLabel::text("VA"),
|
||||
hint: Some(words.tracking_hint.clone()),
|
||||
value: plain(values.tracking),
|
||||
range: TRACKING_RANGE,
|
||||
percent: false,
|
||||
@@ -514,6 +527,7 @@ impl<'a> CharacterControls<'a> {
|
||||
let vertical = PlainRow {
|
||||
label: words.vertical_scale.clone(),
|
||||
glyph: GlyphLabel::text("↕T"),
|
||||
hint: None,
|
||||
value: plain(values.vertical_scale),
|
||||
range: SCALE_RANGE,
|
||||
percent: true,
|
||||
@@ -524,6 +538,7 @@ impl<'a> CharacterControls<'a> {
|
||||
let horizontal = PlainRow {
|
||||
label: words.horizontal_scale.clone(),
|
||||
glyph: GlyphLabel::text("↔T"),
|
||||
hint: None,
|
||||
value: plain(values.horizontal_scale),
|
||||
range: SCALE_RANGE,
|
||||
percent: true,
|
||||
@@ -536,6 +551,7 @@ impl<'a> CharacterControls<'a> {
|
||||
let shift = LengthRow {
|
||||
label: words.baseline_shift.clone(),
|
||||
glyph: GlyphLabel::text("Aª"),
|
||||
hint: None,
|
||||
value: values.baseline_shift,
|
||||
range: SHIFT_RANGE,
|
||||
presets: &[],
|
||||
|
||||
@@ -217,6 +217,7 @@ impl<'a> ParagraphControls<'a> {
|
||||
let start = LengthRow {
|
||||
label: words.start_indent.clone(),
|
||||
glyph: GlyphLabel::text("→|"),
|
||||
hint: None,
|
||||
value: values.start_indent,
|
||||
range: INDENT_RANGE,
|
||||
presets: &[],
|
||||
@@ -226,6 +227,7 @@ impl<'a> ParagraphControls<'a> {
|
||||
let end = LengthRow {
|
||||
label: words.end_indent.clone(),
|
||||
glyph: GlyphLabel::text("|←"),
|
||||
hint: None,
|
||||
value: values.end_indent,
|
||||
range: INDENT_RANGE,
|
||||
presets: &[],
|
||||
@@ -235,6 +237,7 @@ impl<'a> ParagraphControls<'a> {
|
||||
let first = LengthRow {
|
||||
label: words.first_line_indent.clone(),
|
||||
glyph: GlyphLabel::text("¶→"),
|
||||
hint: None,
|
||||
value: values.first_line_indent,
|
||||
range: INDENT_RANGE,
|
||||
presets: &[],
|
||||
@@ -246,6 +249,7 @@ impl<'a> ParagraphControls<'a> {
|
||||
let before = LengthRow {
|
||||
label: words.space_before.clone(),
|
||||
glyph: GlyphLabel::text("↑¶"),
|
||||
hint: None,
|
||||
value: values.space_before,
|
||||
range: SPACE_RANGE,
|
||||
presets: &[],
|
||||
@@ -255,6 +259,7 @@ impl<'a> ParagraphControls<'a> {
|
||||
let after = LengthRow {
|
||||
label: words.space_after.clone(),
|
||||
glyph: GlyphLabel::text("¶↓"),
|
||||
hint: None,
|
||||
value: values.space_after,
|
||||
range: SPACE_RANGE,
|
||||
presets: &[],
|
||||
|
||||
@@ -22,10 +22,11 @@ pub(super) struct Lengths<'u> {
|
||||
pub(super) unit: DocUnit,
|
||||
}
|
||||
|
||||
/// A length field of a row: its label, mark, value in points and range in points.
|
||||
/// A length field of a row: its label, mark, the mark's hint, value in points and range in points.
|
||||
pub(super) struct LengthRow<'p> {
|
||||
pub(super) label: Label,
|
||||
pub(super) glyph: GlyphLabel,
|
||||
pub(super) hint: Option<Label>,
|
||||
pub(super) value: Option<f32>,
|
||||
pub(super) range: RangeInclusive<f64>,
|
||||
pub(super) presets: &'p [Entry<NumberPick>],
|
||||
@@ -35,6 +36,7 @@ pub(super) struct LengthRow<'p> {
|
||||
pub(super) struct PlainRow<'p> {
|
||||
pub(super) label: Label,
|
||||
pub(super) glyph: GlyphLabel,
|
||||
pub(super) hint: Option<Label>,
|
||||
pub(super) value: Mixed<Option<f64>>,
|
||||
pub(super) range: RangeInclusive<f64>,
|
||||
pub(super) percent: bool,
|
||||
@@ -44,9 +46,9 @@ pub(super) struct PlainRow<'p> {
|
||||
impl Lengths<'_> {
|
||||
/// A field over a length in points, shown in the unit: its event in points and the preset row picked.
|
||||
pub(super) fn row(&self, ui: &mut Ui, spec: LengthRow<'_>) -> (EditEvents<Option<f64>>, Option<NumberPick>) {
|
||||
let LengthRow { label, glyph, value, range, presets } = spec;
|
||||
let LengthRow { label, glyph, hint, value, range, presets } = spec;
|
||||
let points = value.map_or(Mixed::Mixed, |value| Mixed::Same(Some(f64::from(value))));
|
||||
let field = Field::new(label.clone()).side(mark(glyph)).show(ui, |ui, _| {
|
||||
let field = Field::new(label.clone()).side(mark(glyph, hint)).show(ui, |ui, _| {
|
||||
let out = length_field(self.unit, label.clone(), points, range.clone()).presets(presets).scrub_by_label().show(ui);
|
||||
(out.edit.events, out.preset)
|
||||
});
|
||||
@@ -59,11 +61,12 @@ impl Lengths<'_> {
|
||||
ui: &mut Ui,
|
||||
label: Label,
|
||||
glyph: GlyphLabel,
|
||||
hint: Label,
|
||||
value: Option<NamedOr<KerningName>>,
|
||||
presets: &[Entry<NumberPick>],
|
||||
) -> EditEvents<NamedOr<KerningName>> {
|
||||
let units = self.units;
|
||||
let shown = Field::new(label.clone()).side(mark(glyph)).show(ui, |ui, _| {
|
||||
let shown = Field::new(label.clone()).side(mark(glyph, Some(hint))).show(ui, |ui, _| {
|
||||
let field = NumberField::named(label.clone(), Bind::given(mixed(value))).range(TRACKING_RANGE).presets(presets).scrub_by_label();
|
||||
field.codec(NumberCodec::new(units.reader(None), 0)).show(ui).edit.events
|
||||
});
|
||||
@@ -72,9 +75,9 @@ impl Lengths<'_> {
|
||||
|
||||
/// A field over a number that is not a length: its event and the preset row picked.
|
||||
pub(super) fn plain(&self, ui: &mut Ui, spec: PlainRow<'_>) -> (EditEvents<Option<f64>>, Option<NumberPick>) {
|
||||
let PlainRow { label, glyph, value, range, percent, presets } = spec;
|
||||
let PlainRow { label, glyph, hint, value, range, percent, presets } = spec;
|
||||
let units = self.units;
|
||||
let shown = Field::new(label.clone()).side(mark(glyph)).show(ui, |ui, _| {
|
||||
let shown = Field::new(label.clone()).side(mark(glyph, hint)).show(ui, |ui, _| {
|
||||
let field = NumberField::new(label.clone(), Bind::given(value)).range(range.clone()).presets(presets).scrub_by_label();
|
||||
let field = if percent {
|
||||
field.unit_kind(UnitKind::Percent).unit(units.symbol(UnitKind::Percent)).step(UnitKind::Percent.step())
|
||||
@@ -88,9 +91,13 @@ impl Lengths<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
/// A label shown as a mark, whose drag scrubs the field.
|
||||
pub(super) fn mark(glyph: GlyphLabel) -> SideLabel {
|
||||
SideLabel::new().kind(LabelKind::Glyph(glyph)).scrub(true)
|
||||
/// A label shown as a mark, whose drag scrubs the field; `hint`, when given, is its tooltip in place of the label.
|
||||
pub(super) fn mark(glyph: GlyphLabel, hint: Option<Label>) -> SideLabel {
|
||||
let side = SideLabel::new().kind(LabelKind::Glyph(glyph)).scrub(true);
|
||||
match hint {
|
||||
Some(hint) => side.tooltip(hint),
|
||||
None => side,
|
||||
}
|
||||
}
|
||||
|
||||
/// `value` as a field's value: a missing one is mixed.
|
||||
|
||||
@@ -118,6 +118,10 @@ impl Component for LabelLeaf {
|
||||
type Style = LabelStyle;
|
||||
type Output = LabelOutput;
|
||||
|
||||
fn shows_label(&self) -> bool {
|
||||
self.glyph.is_none()
|
||||
}
|
||||
|
||||
fn style(cx: &StyleCx<'_>, _: LabelLeafStyleKey) -> LabelStyle {
|
||||
LabelStyle {
|
||||
metrics: Metrics::new(),
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
//! What a side label is: its kind, glyph, alignment, layout, `SideLabel` and `Scrub`.
|
||||
|
||||
use crate::contract::Tooltip;
|
||||
use crate::primitives::IconSpec;
|
||||
use crate::runtime::access::scope::FieldLabelRole;
|
||||
use crate::runtime::env::{Env, Label};
|
||||
@@ -108,6 +109,8 @@ pub struct SideLabel {
|
||||
pub icon: Option<IconSpec<'static>>,
|
||||
/// A line after the text in the muted text colour. It joins the control's description, not its name.
|
||||
pub detail: Option<Label>,
|
||||
/// The label's tooltip, in place of its text: a hint a short or glyph label has no room for.
|
||||
pub tooltip: Option<Tooltip>,
|
||||
}
|
||||
|
||||
impl SideLabel {
|
||||
@@ -122,6 +125,7 @@ impl SideLabel {
|
||||
reset_dot: false,
|
||||
icon: None,
|
||||
detail: None,
|
||||
tooltip: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -180,6 +184,13 @@ impl SideLabel {
|
||||
self.detail = Some(detail.into());
|
||||
self
|
||||
}
|
||||
|
||||
/// Shows `tooltip` over the label in place of its text, and adds it to the label's description.
|
||||
#[must_use]
|
||||
pub fn tooltip(mut self, tooltip: impl Into<Tooltip>) -> Self {
|
||||
self.tooltip = Some(tooltip.into());
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// How a drag on the side label (or on the control itself) changes a control's value: the drag maths over the
|
||||
|
||||
@@ -130,6 +130,9 @@ pub(crate) fn run(ui: &mut Ui, env: &Env, input: &LabelIn<'_>) -> Placed {
|
||||
leaf.resettable = reset.is_some();
|
||||
leaf.differs = reset.is_some_and(|fact| fact.differs);
|
||||
leaf.colon = colon(env, input);
|
||||
if let Some(tooltip) = input.side.tooltip.clone() {
|
||||
leaf = leaf.tooltip(tooltip);
|
||||
}
|
||||
match &input.side.kind {
|
||||
LabelKind::Glyph(glyph) => leaf.glyph = Some(glyph.resolved(env)),
|
||||
_ => {
|
||||
|
||||
@@ -4,8 +4,9 @@
|
||||
//! delay, and at once while a neighbour's tooltip was hiding a moment ago. A press, a menu that owns keys and a
|
||||
//! running drag hide it.
|
||||
//!
|
||||
//! A component with no tooltip shows its label as the tooltip when its type declares `Component::SHOWS_LABEL` false
|
||||
//! (or an instance does, through `Component::shows_label`): the label is then its only name and no text paints it.
|
||||
//! A component with no tooltip shows its own label as the tooltip when its type declares `Component::SHOWS_LABEL`
|
||||
//! false (or an instance does, through `Component::shows_label`): no text paints that name, inside a field or not. A
|
||||
//! component with no label of its own is named by its field label, which is on screen, and shows none.
|
||||
//! A component that paints its label shows it whole as the tooltip when the paint cut it.
|
||||
|
||||
use super::cx::{Decls, PartExtras};
|
||||
@@ -68,8 +69,8 @@ fn spec_text(env: &crate::runtime::env::EnvView<'_>, spec: &TooltipSpec, enabled
|
||||
}
|
||||
|
||||
/// The text the component's own tooltip shows, or `None` when it has none: the tooltip's text, or its disabled text
|
||||
/// while disabled, with the bound command's chord; else the full label when the paint cut it, or when the component
|
||||
/// declares it paints no label (`shows_label`) and no field label names it.
|
||||
/// while disabled, with the bound command's chord; else the full label when the paint cut it, or its own label when
|
||||
/// the component declares it paints no label (`shows_label`).
|
||||
fn text_of<C: Component>(cx: &Cx<'_, C>, shows_label: bool) -> Option<String> {
|
||||
let core = &cx.core;
|
||||
let base = match core.resolved.tooltip.map(|tooltip| tooltip.spec()) {
|
||||
@@ -78,7 +79,7 @@ fn text_of<C: Component>(cx: &Cx<'_, C>, shows_label: bool) -> Option<String> {
|
||||
let paint = core.paint.borrow();
|
||||
match paint.elided.clone() {
|
||||
Some(full) => full,
|
||||
None if !shows_label && unnamed_by_field(core) => core.resolved.label.to_owned(),
|
||||
None if !shows_label && !core.resolved.label.is_empty() => core.resolved.label.to_owned(),
|
||||
None => return None,
|
||||
}
|
||||
}
|
||||
@@ -90,12 +91,6 @@ fn text_of<C: Component>(cx: &Cx<'_, C>, shows_label: bool) -> Option<String> {
|
||||
})
|
||||
}
|
||||
|
||||
/// Whether the label is non-empty and no field label shows the component's name, so the label is the only visible
|
||||
/// name a tooltip can give.
|
||||
fn unnamed_by_field(core: &super::CxCore<'_>) -> bool {
|
||||
!core.resolved.label.is_empty() && crate::runtime::access::scope::field_label(&core.ctx).is_none()
|
||||
}
|
||||
|
||||
/// A part's tooltip, for the part the pointer is over or the keyboard is on.
|
||||
struct PartTip {
|
||||
part: PartRef,
|
||||
|
||||
@@ -379,6 +379,7 @@ fn recent_section(
|
||||
let Some(file) = file_of(path) else { return Tile::new(Label::Empty) };
|
||||
page.tile(&file.name, page.age(file.opened), page.picture(ThumbKey::Recent(file.path.clone()), file.thumbnail))
|
||||
.corner_action(IconId::new("close"), StartRecentCommands::Remove)
|
||||
.tooltip(file.path.display().to_string())
|
||||
});
|
||||
let corner: Vec<PathBuf> = out
|
||||
.response
|
||||
@@ -401,7 +402,7 @@ fn recent_section(
|
||||
.natural_height(true)
|
||||
.show_items(ui, &source, &mut |path, _| {
|
||||
let Some(file) = file_of(path) else { return ListRow::new(Label::Empty) };
|
||||
let row = ListRow::new(Label::from(file.name.clone())).detail(folder_of(&file.path));
|
||||
let row = ListRow::new(Label::from(file.name.clone())).detail(folder_of(&file.path)).tooltip(file.path.display().to_string());
|
||||
let row = match page.age(file.opened) {
|
||||
Some(age) => row.trailing(age),
|
||||
None => row,
|
||||
|
||||
@@ -378,14 +378,22 @@ impl<'a> ListRow<'a> {
|
||||
/// The first item added is the outermost.
|
||||
#[must_use]
|
||||
pub fn trailing(mut self, text: impl Into<Label>) -> Self {
|
||||
self.trailing.push(Trailing { item: TrailingItem::Text(text.into()), optional: false });
|
||||
self.trailing.push(Trailing { item: TrailingItem::Text { text: text.into(), name: None }, optional: false });
|
||||
self
|
||||
}
|
||||
|
||||
/// A short mark at the trailing end, shown as [`trailing`](Self::trailing) shows a text, that stands for `name`
|
||||
/// (a font class's sign for the class): `name` is its tooltip and part of the row's description.
|
||||
#[must_use]
|
||||
pub fn trailing_named(mut self, text: impl Into<Label>, name: impl Into<Label>) -> Self {
|
||||
self.trailing.push(Trailing { item: TrailingItem::Text { text: text.into(), name: Some(name.into()) }, optional: false });
|
||||
self
|
||||
}
|
||||
|
||||
/// A text at the trailing end that drops out when the label would get narrower than its least width with it.
|
||||
#[must_use]
|
||||
pub fn optional(mut self, text: impl Into<Label>) -> Self {
|
||||
self.trailing.push(Trailing { item: TrailingItem::Text(text.into()), optional: true });
|
||||
self.trailing.push(Trailing { item: TrailingItem::Text { text: text.into(), name: None }, optional: true });
|
||||
self
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ impl ListRow<'_> {
|
||||
let Some(slot) = slot else { continue };
|
||||
let shape = Rect::from_center_size(slot.center(), side);
|
||||
match &mut trailing.item {
|
||||
TrailingItem::Text(text) => {
|
||||
TrailingItem::Text { text, .. } => {
|
||||
ui.draw().text(*slot, &trailing_run(text));
|
||||
}
|
||||
TrailingItem::Icon { icon, .. } => {
|
||||
|
||||
@@ -216,7 +216,7 @@ impl ListRow<'_> {
|
||||
/// The widths of the trailing items: as `text_width` measures a text, the icon side for the others.
|
||||
pub(super) fn item_widths(&self, sizes: &Sizes, text_width: &dyn Fn(&TextRun<'_>) -> f32) -> Vec<(f32, bool)> {
|
||||
let width = |item: &TrailingItem| match item {
|
||||
TrailingItem::Text(text) => text_width(&trailing_run(text)),
|
||||
TrailingItem::Text { text, .. } => text_width(&trailing_run(text)),
|
||||
TrailingItem::Icon { .. } | TrailingItem::Part { .. } => sizes.icon,
|
||||
TrailingItem::Hosted(_) => sizes.field,
|
||||
};
|
||||
|
||||
@@ -37,6 +37,7 @@ const PROGRESS: PartKey = PartKey::new("progress");
|
||||
const LINES: PartKey = PartKey::new("lines");
|
||||
pub(super) const TRAILING: PartKey = PartKey::new("trailing");
|
||||
const TRAILING_ICON: PartKey = PartKey::new("trailing-icon");
|
||||
const TRAILING_TEXT: PartKey = PartKey::new("trailing-text");
|
||||
const THUMB: PartKey = PartKey::new("thumb");
|
||||
|
||||
/// The name a pattern with `{row}` gives for the row labelled `row`.
|
||||
@@ -182,7 +183,9 @@ impl Component for ListRow<'_> {
|
||||
};
|
||||
}
|
||||
if let Some(disclosure) = placed.disclosure {
|
||||
cx.part(DISCLOSURE, disclosure).sense(Sense::CLICK).pointer_only("Left and Right open and close the row");
|
||||
let words = cx.words::<ListWords>();
|
||||
let tip = if self.group == Some(true) { words.collapse_tip.clone() } else { words.expand_tip.clone() };
|
||||
cx.part(DISCLOSURE, disclosure).sense(Sense::CLICK).pointer_only("Left and Right open and close the row").tooltip(tip);
|
||||
}
|
||||
if self.progress.is_some() {
|
||||
cx.part(PROGRESS, self.progress_band(placed.label, &cx.style.sizes));
|
||||
@@ -213,7 +216,10 @@ impl Component for ListRow<'_> {
|
||||
let Some(slot) = slot else { continue };
|
||||
let shape = Rect::from_center_size(slot.center(), Vec2::splat(side));
|
||||
match &trailing.item {
|
||||
TrailingItem::Text(_) | TrailingItem::Hosted(_) => {}
|
||||
TrailingItem::Text { name: None, .. } | TrailingItem::Hosted(_) => {}
|
||||
TrailingItem::Text { name: Some(name), .. } => {
|
||||
cx.part(TRAILING_TEXT.at(at as u64), *slot).tooltip(name.clone());
|
||||
}
|
||||
TrailingItem::Icon { name, .. } => {
|
||||
cx.part(TRAILING_ICON.at(at as u64), shape).tooltip(name.clone());
|
||||
}
|
||||
@@ -303,7 +309,7 @@ impl Component for ListRow<'_> {
|
||||
};
|
||||
access.name(name);
|
||||
let icons = self.trailing.iter().filter_map(|trailing| match &trailing.item {
|
||||
TrailingItem::Icon { name, .. } => Some(name.as_str().to_owned()),
|
||||
TrailingItem::Icon { name, .. } | TrailingItem::Text { name: Some(name), .. } => Some(name.as_str().to_owned()),
|
||||
_ => None,
|
||||
});
|
||||
let percent = self.progress.map(|fraction| cx.format().percent(f64::from(Progress::clamped(fraction)), 0));
|
||||
@@ -363,7 +369,7 @@ impl Component for ListRow<'_> {
|
||||
}
|
||||
for (at, trailing) in self.trailing.iter().enumerate() {
|
||||
match &trailing.item {
|
||||
TrailingItem::Text(_) | TrailingItem::Hosted(_) => {}
|
||||
TrailingItem::Text { .. } | TrailingItem::Hosted(_) => {}
|
||||
TrailingItem::Icon { name, .. } => {
|
||||
access.node(TRAILING_ICON.at(at as u64), Role::IMAGE).name = Name::Text(name.as_str().to_owned());
|
||||
}
|
||||
|
||||
@@ -44,8 +44,8 @@ pub(super) type HostedShow<'a> = Box<dyn FnOnce(&mut PaintUi<'_>, usize, Rect) +
|
||||
|
||||
/// What a trailing item is.
|
||||
pub(in crate::list) enum TrailingItem<'a> {
|
||||
/// A caption-role text as wide as it measures.
|
||||
Text(Label),
|
||||
/// A caption-role text as wide as it measures; a short mark carries the `name` it stands for, its tooltip.
|
||||
Text { text: Label, name: Option<Label> },
|
||||
/// An icon of the row's icon size, named for the screen reader and the tooltip.
|
||||
Icon { icon: IconSpec<'static>, name: Label },
|
||||
/// A pressable face of the row's icon size.
|
||||
|
||||
@@ -25,5 +25,7 @@ craft_ui::words! {
|
||||
label_gray: "list.labelGray" = "Gray";
|
||||
expand: "listRow.expand" = "Expand {row}";
|
||||
collapse: "listRow.collapse" = "Collapse {row}";
|
||||
expand_tip: "listRow.expandTip" = "Expand (Alt-click: all groups)";
|
||||
collapse_tip: "listRow.collapseTip" = "Collapse (Alt-click: all groups)";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -178,8 +178,10 @@ impl Component for ReferencePoint<'_> {
|
||||
|
||||
fn parts(&mut self, rect: Rect, cx: &mut PartsCx<'_, Self>) {
|
||||
let current = Self::position_of(cx.value());
|
||||
for at in 0..Anchor9::ALL.len() {
|
||||
let part = cx.part(Self::part(at), Self::cell_rect(rect, at)).sense(Sense::CLICK | Sense::FOCUS).focusable();
|
||||
let words = cx.words::<PickerWords>();
|
||||
let names: Vec<Label> = Anchor9::ALL.iter().map(|point| Self::name_of(*point, &words).clone()).collect();
|
||||
for (at, name) in names.into_iter().enumerate() {
|
||||
let part = cx.part(Self::part(at), Self::cell_rect(rect, at)).sense(Sense::CLICK | Sense::FOCUS).focusable().tooltip(name);
|
||||
if current == Some(at) {
|
||||
part.flags(Flags::CHECKED).selected();
|
||||
}
|
||||
|
||||
@@ -601,6 +601,10 @@ impl Component for Tile<'_> {
|
||||
type Style = TileStyle;
|
||||
type Output = TileOutput;
|
||||
|
||||
fn shows_label(&self) -> bool {
|
||||
self.caption != CaptionPlace::None
|
||||
}
|
||||
|
||||
fn style(cx: &StyleCx<'_>, key: TileStyleKey) -> TileStyle {
|
||||
let tokens = cx.tokens;
|
||||
let colors = &tokens.color;
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
mod align;
|
||||
pub mod group;
|
||||
pub mod kit;
|
||||
mod tooltip;
|
||||
|
||||
use self::kit::{Dial, Field, Seen, Wide};
|
||||
use craft_host::target::ShellInfo;
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
//! The tooltip of a control that paints no label: its own label shows there, inside a field or not; a control named
|
||||
//! only by its field label shows none.
|
||||
|
||||
use super::kit::PlainStyle;
|
||||
use craft_behavior::Sense;
|
||||
use craft_ui::contract::tooltip::DELAY;
|
||||
use craft_ui::contract::{Common, Component, Cx, GlyphLabel, LabelKind, MeasureCx, Props as _, Role, Show as _, ShowContainer as _, SideLabel};
|
||||
use craft_ui::form::Field;
|
||||
use craft_ui::style::StyleCx;
|
||||
use craft_ui::testing::pointer::moved;
|
||||
use craft_ui::testing::{Fixture, trial};
|
||||
use craft_ui::tile_grid::{CaptionPlace, Tile};
|
||||
use egui::{Pos2, Rect, Response, Ui, Vec2};
|
||||
use std::cell::Cell;
|
||||
use std::rc::Rc;
|
||||
|
||||
/// A mark that paints no text.
|
||||
#[derive(craft_ui::contract::Component)]
|
||||
struct Mark {
|
||||
#[common]
|
||||
common: Common,
|
||||
}
|
||||
|
||||
impl Component for Mark {
|
||||
const ROLE: Role = Role::IMAGE;
|
||||
const SHOWS_LABEL: bool = false;
|
||||
type Style = PlainStyle;
|
||||
type Output = Response;
|
||||
|
||||
fn style(_: &StyleCx<'_>, _: MarkStyleKey) -> PlainStyle {
|
||||
PlainStyle::default()
|
||||
}
|
||||
|
||||
fn measure(&mut self, _: &MeasureCx<'_, Self>) -> Vec2 {
|
||||
Vec2::splat(24.0)
|
||||
}
|
||||
|
||||
fn sense(&self) -> Sense {
|
||||
Sense::HOVER
|
||||
}
|
||||
|
||||
fn finish(self, cx: Cx<'_, Self>) -> Response {
|
||||
cx.response.clone()
|
||||
}
|
||||
}
|
||||
|
||||
/// The texts the pointer's rest on what `show` shows (it returns its rect) adds after the tooltip delay.
|
||||
fn texts_added_by_hover(show: impl Fn(&mut Ui) -> Rect + 'static) -> Vec<String> {
|
||||
let at = Rc::new(Cell::new(Rect::NOTHING));
|
||||
let seen = Rc::clone(&at);
|
||||
let mut fixture = Fixture::new(move |ui| seen.set(show(ui)));
|
||||
fixture.settle();
|
||||
// A cut run carries its full text too: runs are told apart by whether they are cut.
|
||||
let before: Vec<(String, bool)> = fixture.texts().iter().map(|run| (run.text.clone(), run.elided)).collect();
|
||||
fixture.frame(vec![moved(at.get().center())]);
|
||||
fixture.advance(DELAY);
|
||||
fixture.settle();
|
||||
let after = fixture.texts().iter().map(|run| (run.text.clone(), run.elided));
|
||||
after.filter(|run| !before.contains(run)).map(|(text, _)| text).collect()
|
||||
}
|
||||
|
||||
/// A mark in the field "Layer", with `own` as its own label.
|
||||
fn mark_in_field(own: Option<&'static str>) -> Vec<String> {
|
||||
texts_added_by_hover(move |ui| {
|
||||
let shown = Field::new("Layer").show(ui, |ui, _| {
|
||||
let mark = Mark { common: Common::new() };
|
||||
let mark = match own {
|
||||
Some(label) => mark.label(label),
|
||||
None => mark,
|
||||
};
|
||||
mark.show(ui).rect
|
||||
});
|
||||
shown.inner.unwrap_or(Rect::NOTHING)
|
||||
})
|
||||
}
|
||||
|
||||
/// A mark in the field "Leading", whose label shows as a glyph at the row's start, with `hint` as its tooltip.
|
||||
fn glyph_label(hint: Option<&'static str>) -> Vec<String> {
|
||||
texts_added_by_hover(move |ui| {
|
||||
let side = SideLabel::new().kind(LabelKind::Glyph(GlyphLabel::text("A")));
|
||||
let side = match hint {
|
||||
Some(hint) => side.tooltip(hint),
|
||||
None => side,
|
||||
};
|
||||
Field::new("Leading").side(side).show(ui, |ui, _| Mark { common: Common::new() }.show(ui));
|
||||
// The glyph sits at the start of the row.
|
||||
Rect::from_min_size(Pos2::ZERO, Vec2::new(8.0, 24.0))
|
||||
})
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_control_that_paints_no_label_shows_its_own_label_as_tooltip() {
|
||||
assert_eq!(mark_in_field(Some("Lock")), ["Lock"], "a mark with its own label shows it inside a field");
|
||||
assert!(mark_in_field(None).is_empty(), "a mark the field names shows no tooltip");
|
||||
|
||||
let tile = texts_added_by_hover(|ui| {
|
||||
let rect = Rect::from_min_size(Pos2::new(20.0, 20.0), Vec2::splat(96.0));
|
||||
Tile::new("Harbour at dusk").caption(CaptionPlace::None).show_at(ui, rect);
|
||||
rect
|
||||
});
|
||||
assert_eq!(tile, ["Harbour at dusk"], "a tile with no caption shows its name");
|
||||
|
||||
let long = "Harbour at dusk, seen from the lighthouse on the northern breakwater";
|
||||
let cut = texts_added_by_hover(move |ui| {
|
||||
let rect = Rect::from_min_size(Pos2::new(20.0, 20.0), Vec2::new(96.0, 128.0));
|
||||
Tile::new(long).show_at(ui, rect);
|
||||
rect
|
||||
});
|
||||
assert_eq!(cut, [long], "a tile whose caption is cut shows its whole name");
|
||||
|
||||
assert_eq!(glyph_label(None), ["Leading"], "a glyph label shows its name");
|
||||
assert_eq!(glyph_label(Some("Leading (1/1000 em)")), ["Leading (1/1000 em)"], "a glyph label's tooltip replaces its name");
|
||||
}
|
||||
Reference in New Issue
Block a user