ParagraphControls: alignment and direction are rows of icon toggle buttons, each a Tab stop, as the old; a justify button on point text is disabled with its reason in the tooltip

This commit is contained in:
aiongg
2026-11-12 14:32:45 +00:00
parent 21051f8d27
commit 65568b98e0
3 changed files with 38 additions and 21 deletions
+2 -2
View File
@@ -603,7 +603,7 @@ impl<'a> CharacterControls<'a> {
}
toggles.push(Toggle::new(&words.underline, "text_underline", values.underline, Box::new(CharacterChange::Underline)));
toggles.push(Toggle::new(&words.strikethrough, "text_strikethrough", values.strikethrough, Box::new(CharacterChange::Strikethrough)));
edits.extend(toggle_row(ui, toggles));
edits.extend(toggle_row(ui, None, toggles));
let names = [
&words.standard_ligatures,
@@ -623,7 +623,7 @@ impl<'a> CharacterControls<'a> {
Toggle::new(name, icon, Some(values.opentype.contains(&tag)), Box::new(move |on| CharacterChange::OpenType { tag, on }))
})
.collect();
edits.extend(toggle_row(ui, features));
edits.extend(toggle_row(ui, None, features));
if let Some(entries) = &language_entries {
edits.extend(select_row(ui, &words.language, mixed(values.language.clone()), entries, CharacterChange::Language));
+13 -13
View File
@@ -3,15 +3,15 @@
use std::ops::RangeInclusive;
use crate::ui::length::DocUnit;
use craft_behavior::{EditEvent, IconId};
use craft_ui::choice::{Checkbox, Segmented};
use craft_ui::contract::{Bind, Entry, GlyphLabel, NoValue, Outcome, Props as _, Show as _, ShowContainer as _};
use craft_behavior::EditEvent;
use craft_ui::choice::Checkbox;
use craft_ui::contract::{Bind, GlyphLabel, NoValue, Outcome, Props as _, Show as _, ShowContainer as _};
use craft_ui::layout_components::{Grid, Stack};
use craft_ui::runtime::env::{Env, Label};
use egui::{Response, Ui};
use super::TWO_COLUMNS;
use super::rows::{LengthRow, Lengths, mixed, number_change};
use super::rows::{LengthRow, Lengths, Toggle, mixed, number_change, toggle_row};
craft_ui::words! {
/// The Paragraph controls' words, English by default (PhotoCraft's tooltips).
@@ -182,7 +182,7 @@ impl<'a> ParagraphControls<'a> {
let units = env.units();
let lengths = Lengths { units: &units, unit: self.unit };
let aligns: Vec<Entry<TextAlign>> = [
let aligns: Vec<Toggle<ParagraphChange>> = [
(TextAlign::Left, "align_left", &words.left),
(TextAlign::Center, "align_center", &words.center),
(TextAlign::Right, "align_right", &words.right),
@@ -193,25 +193,25 @@ impl<'a> ParagraphControls<'a> {
]
.into_iter()
.map(|(align, icon, name)| {
let entry = Entry::new(align, name.clone()).icon(IconId::new(icon));
if point_text && align.needs_box() { entry.disabled_reason(words.justify_needs_box.clone()) } else { entry }
let toggle = Toggle::new(name, icon, Some(values.align == Some(align)), Box::new(move |_| ParagraphChange::Align(align)));
if point_text && align.needs_box() { toggle.disabled(&words.justify_needs_box) } else { toggle }
})
.collect();
let directions: Vec<Entry<ParagraphDirection>> = [
let directions: Vec<Toggle<ParagraphChange>> = [
(ParagraphDirection::Automatic, "pilcrow", &words.direction_auto),
(ParagraphDirection::LeftToRight, "pilcrow_right", &words.direction_ltr),
(ParagraphDirection::RightToLeft, "pilcrow_left", &words.direction_rtl),
]
.into_iter()
.map(|(direction, icon, name)| Entry::new(direction, name.clone()).icon(IconId::new(icon)))
.map(|(direction, icon, name)| {
Toggle::new(name, icon, Some(values.direction == Some(direction)), Box::new(move |_| ParagraphChange::Direction(direction)))
})
.collect();
let mut edits: Vec<EditEvent<ParagraphChange>> = Vec::new();
let shown = Stack::new().fill_width().label(self.label).show(ui, |ui, _| {
let out = Segmented::new(words.alignment.clone(), Bind::given(mixed(values.align)), &aligns).show(ui);
edits.extend(out.events.map(ParagraphChange::Align));
let out = Segmented::new(words.direction.clone(), Bind::given(mixed(values.direction)), &directions).show(ui);
edits.extend(out.events.map(ParagraphChange::Direction));
edits.extend(toggle_row(ui, Some(&words.alignment), aligns));
edits.extend(toggle_row(ui, Some(&words.direction), directions));
Grid::new(&TWO_COLUMNS).fill_width().show(ui, |ui, _| {
let start = LengthRow {
+23 -6
View File
@@ -6,7 +6,7 @@ use std::sync::Arc;
use crate::ui::length::{DocUnit, length_field};
use craft_behavior::{EditEvent, EditEvents, IconId, Mixed, NumberCodec};
use craft_ui::buttons::ToggleButton;
use craft_ui::contract::{Bind, Entry, FieldLayout, GlyphLabel, LabelKind, Show as _, ShowContainer as _, SideLabel};
use craft_ui::contract::{Bind, Entry, FieldLayout, GlyphLabel, LabelKind, Props as _, Show as _, ShowContainer as _, SideLabel, TooltipSpec};
use craft_ui::form::Field;
use craft_ui::layout_components::Row;
use craft_ui::pickers::Select;
@@ -163,21 +163,38 @@ pub(super) struct Toggle<C> {
pub(super) icon: &'static str,
pub(super) on: Option<bool>,
pub(super) change: Box<dyn Fn(bool) -> C>,
/// Why the button is disabled; `None` while it is enabled.
pub(super) disabled: Option<Label>,
}
impl<C> Toggle<C> {
pub(super) fn new(name: &Label, icon: &'static str, on: Option<bool>, change: Box<dyn Fn(bool) -> C>) -> Self {
Toggle { name: name.clone(), icon, on, change }
Toggle { name: name.clone(), icon, on, change, disabled: None }
}
/// The button is disabled for `reason`, which its tooltip shows.
pub(super) fn disabled(mut self, reason: &Label) -> Self {
self.disabled = Some(reason.clone());
self
}
}
/// A row of icon toggle buttons; the pressed ones report their change when clicked.
pub(super) fn toggle_row<C>(ui: &mut Ui, toggles: Vec<Toggle<C>>) -> Vec<EditEvent<C>> {
/// A row of icon toggle buttons, each its own Tab stop; a clicked one reports its change. `label` names the row as a
/// group (Alignment).
pub(super) fn toggle_row<C>(ui: &mut Ui, label: Option<&Label>, toggles: Vec<Toggle<C>>) -> Vec<EditEvent<C>> {
let mut edits = Vec::new();
Row::new().wrap(true).show(ui, |ui, _| {
let row = match label {
Some(label) => Row::new().label(label.clone()),
None => Row::new(),
};
row.wrap(true).show(ui, |ui, _| {
for toggle in &toggles {
let bind = Bind::given(mixed(toggle.on));
let out = ToggleButton::new(toggle.name.clone(), bind).icon(IconId::new(toggle.icon)).icon_only().show(ui);
let mut button = ToggleButton::new(toggle.name.clone(), bind).icon(IconId::new(toggle.icon)).icon_only();
if let Some(reason) = &toggle.disabled {
button = button.enabled(false).tooltip(TooltipSpec::new(toggle.name.clone()).disabled_text(reason.clone()));
}
let out = button.show(ui);
edits.extend(out.events.map(&toggle.change));
}
});