mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-27 16:59:56 +01:00
text: the styles panel can treat character style 0 as an editable base style
TextStylesPanel always draws character style 0 as "No character style": Style Options, Duplicate and Redefine skip it, a double click does nothing, the row is dimmed and drags of it are refused. An app whose character style 0 is a real base style cannot open its options from the panel. `base_character_style(true)` makes character style 0 behave as paragraph style 0 does: Style Options, Duplicate and Redefine take it from the footer, the panel menu, the row menu and a double click, and the row is drawn like the others. Delete still skips it. The option is off by default, so apps with a "None" row see no change.
This commit is contained in:
1 parent
ba847f6d8a
commit
75be04acdb
2 files changed
+120
-13
No files matched your search
@@ -59,7 +59,8 @@ pub enum TextStyleKind {
|
||||
}
|
||||
|
||||
/// A style in the list. Id 0 is the base style: "None" for character styles (shown dimmed, never deleted,
|
||||
/// redefined or edited) and "Basic Paragraph" for paragraph styles (editable, never deleted).
|
||||
/// redefined or edited) and "Basic Paragraph" for paragraph styles (editable, never deleted). With
|
||||
/// [`TextStylesPanel::base_character_style`], character style 0 is editable and never deleted as well.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
#[non_exhaustive]
|
||||
pub struct TextStyleRow {
|
||||
@@ -121,7 +122,8 @@ pub enum TextStyleAction {
|
||||
/// Clear Override, with a target whose style is overridden.
|
||||
ClearOverride,
|
||||
/// Redefine: the style of this id takes the target's attributes: the current style from the footer and the panel
|
||||
/// menu, the row's from its menu. Not for character style 0.
|
||||
/// menu, the row's from its menu. Not for character style 0 unless it is a base style
|
||||
/// ([`TextStylesPanel::base_character_style`]).
|
||||
Redefine(u64),
|
||||
/// New: a style from the selected type when there is a target (`from_selection`), else a default one. The app
|
||||
/// opens its Style Options for the new id.
|
||||
@@ -136,7 +138,8 @@ pub enum TextStyleAction {
|
||||
Duplicate(u64),
|
||||
/// Delete the selected row's style (never id 0).
|
||||
Delete(u64),
|
||||
/// Style Options for a style: a double click or Enter on a row, or the footer button (not for character style 0).
|
||||
/// Style Options for a style: a double click or Enter on a row, or the footer button (not for character style 0
|
||||
/// unless it is a base style, [`TextStylesPanel::base_character_style`]).
|
||||
Options(u64),
|
||||
/// Duplicate with several rows highlighted: every highlighted style, in list order.
|
||||
DuplicateHighlighted,
|
||||
@@ -182,7 +185,9 @@ impl Outcome for TextStylesPanelOutput {
|
||||
/// What the panel knows of the last pass: the rules the footer, the panel menu and the keys share.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
struct Facts {
|
||||
paragraph: bool,
|
||||
/// Whether style 0 is a base style the user edits: paragraph style 0, or character style 0 with
|
||||
/// [`TextStylesPanel::base_character_style`].
|
||||
base: bool,
|
||||
target: bool,
|
||||
overridden: bool,
|
||||
current: Option<u64>,
|
||||
@@ -195,11 +200,11 @@ struct Facts {
|
||||
impl Facts {
|
||||
/// The action of a footer button or menu command, `None` when its condition doesn't hold.
|
||||
fn action(&self, command: TextStyleCommand) -> Option<TextStyleAction> {
|
||||
let editable = self.picked.filter(|id| *id != 0 || self.paragraph);
|
||||
let editable = self.picked.filter(|id| *id != 0 || self.base);
|
||||
let several = self.selected.len() > 1;
|
||||
match command {
|
||||
TextStyleCommand::ClearOverride => (self.target && self.overridden).then_some(TextStyleAction::ClearOverride),
|
||||
TextStyleCommand::Redefine => self.current.filter(|id| self.target && (*id != 0 || self.paragraph)).map(TextStyleAction::Redefine),
|
||||
TextStyleCommand::Redefine => self.current.filter(|id| self.target && (*id != 0 || self.base)).map(TextStyleAction::Redefine),
|
||||
TextStyleCommand::New => Some(TextStyleAction::new_style(self.target, false)),
|
||||
TextStyleCommand::Duplicate if several => Some(TextStyleAction::DuplicateHighlighted),
|
||||
TextStyleCommand::Delete if several => Some(TextStyleAction::DeleteHighlighted),
|
||||
@@ -289,6 +294,10 @@ pub struct TextStylesPanel<'a> {
|
||||
/// Several rows select together: the footer's Duplicate and Delete act on all of them.
|
||||
#[prop]
|
||||
highlighting: bool,
|
||||
/// Character style 0 is a base style the user edits instead of "No character style": it takes Style Options,
|
||||
/// Duplicate and Redefine as paragraph style 0 does, is drawn like the other rows, and is still never deleted.
|
||||
#[prop]
|
||||
base_character_style: bool,
|
||||
/// The list's text with no rows, passed to the collection's `empty` prop.
|
||||
#[prop(into)]
|
||||
empty: Option<Label>,
|
||||
@@ -330,6 +339,7 @@ impl<'a> TextStylesPanel<'a> {
|
||||
target: false,
|
||||
text_selected: true,
|
||||
highlighting: false,
|
||||
base_character_style: false,
|
||||
empty: None,
|
||||
reveal: None,
|
||||
}
|
||||
@@ -344,10 +354,24 @@ impl<'a> TextStylesPanel<'a> {
|
||||
|
||||
/// Shows the panel and returns what the user asked for.
|
||||
pub fn show(self, ui: &mut Ui) -> TextStylesPanelOutput {
|
||||
let TextStylesPanel { label, kind, rows, mut selection, current, overridden, target, text_selected, highlighting, empty, reveal } = self;
|
||||
let TextStylesPanel {
|
||||
label,
|
||||
kind,
|
||||
rows,
|
||||
mut selection,
|
||||
current,
|
||||
overridden,
|
||||
target,
|
||||
text_selected,
|
||||
highlighting,
|
||||
base_character_style,
|
||||
empty,
|
||||
reveal,
|
||||
} = self;
|
||||
|
||||
let words = Env::of(ui).words::<TextStylesWords>();
|
||||
let paragraph = kind == TextStyleKind::Paragraph;
|
||||
let base = paragraph || base_character_style;
|
||||
let action = Cell::new(None::<TextStyleAction>);
|
||||
let hint = Cell::new(false);
|
||||
let tip = words.row_tip.fill(&[("key", words.alt_key.as_str())]);
|
||||
@@ -372,7 +396,7 @@ impl<'a> TextStylesPanel<'a> {
|
||||
match command {
|
||||
TextStyleCommand::New => button
|
||||
.takes_drops_disabled(true)
|
||||
.accepts_drops::<ListDrag<u64>>(move |drag| drag.list == list_id && drag.keys.iter().any(|id| *id != 0 || paragraph)),
|
||||
.accepts_drops::<ListDrag<u64>>(move |drag| drag.list == list_id && drag.keys.iter().any(|id| *id != 0 || base)),
|
||||
TextStyleCommand::Delete => button
|
||||
.takes_drops_disabled(true)
|
||||
.accepts_drops::<ListDrag<u64>>(move |drag| drag.list == list_id && drag.keys.iter().any(|id| *id != 0)),
|
||||
@@ -388,7 +412,7 @@ impl<'a> TextStylesPanel<'a> {
|
||||
Some(TextStyleCommand::Delete) if many => Some(TextStyleAction::DeleteHighlighted),
|
||||
Some(TextStyleCommand::Delete) => (drag.grabbed != 0).then_some(TextStyleAction::Delete(drag.grabbed)),
|
||||
Some(TextStyleCommand::New) if many => Some(TextStyleAction::DuplicateHighlighted),
|
||||
Some(TextStyleCommand::New) => (drag.grabbed != 0 || paragraph).then_some(TextStyleAction::Duplicate(drag.grabbed)),
|
||||
Some(TextStyleCommand::New) => (drag.grabbed != 0 || base).then_some(TextStyleAction::Duplicate(drag.grabbed)),
|
||||
_ => None,
|
||||
};
|
||||
if dropped.is_some() {
|
||||
@@ -413,7 +437,7 @@ impl<'a> TextStylesPanel<'a> {
|
||||
}
|
||||
}
|
||||
let held = selection.reborrow().read(cx).and_then(|held| held.same().cloned()).unwrap_or_default();
|
||||
let facts = Facts { paragraph, target, overridden, current, picked: held.lead.or(current), selected: held.items.clone() };
|
||||
let facts = Facts { base, target, overridden, current, picked: held.lead.or(current), selected: held.items.clone() };
|
||||
for command in [
|
||||
TextStyleCommand::ClearOverride,
|
||||
TextStyleCommand::Redefine,
|
||||
@@ -452,7 +476,7 @@ impl<'a> TextStylesPanel<'a> {
|
||||
let Some(row) = rows.iter().find(|row| row.id == *id) else { return ListRow::new(Label::Empty) };
|
||||
let is_current = target && current == Some(*id);
|
||||
let marked = is_current && overridden;
|
||||
let none = *id == 0 && !paragraph;
|
||||
let none = *id == 0 && !base;
|
||||
let name = if marked { words.overridden_name.fill(&[("name", row.name.as_str())]) } else { row.name.clone() };
|
||||
let spoken = if marked { words.overridden_spoken.fill(&[("name", row.name.as_str())]) } else { row.name.clone() };
|
||||
let description = if none { words.no_character_style.as_str().to_owned() } else { tip.clone() };
|
||||
@@ -472,7 +496,7 @@ impl<'a> TextStylesPanel<'a> {
|
||||
let plain = !mods.shift && !mods.cmd;
|
||||
for event in events {
|
||||
match event {
|
||||
CollectionEvent::Activate(id) if second && (id != 0 || paragraph) => action.set(Some(TextStyleAction::Options(id))),
|
||||
CollectionEvent::Activate(id) if second && (id != 0 || base) => action.set(Some(TextStyleAction::Options(id))),
|
||||
CollectionEvent::Activate(id) if target && (paragraph || text_selected) && plain => {
|
||||
action.set(Some(TextStyleAction::apply(id, alt)));
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
//! The styles panel's click rules: a click applies, Alt-click clears overrides, a double click opens Options, except on
|
||||
//! the character style None; at a bare caret every attempt to apply a character style warns.
|
||||
//! the character style None (unless the app makes character style 0 a base style); at a bare caret every attempt to
|
||||
//! apply a character style warns.
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
@@ -137,3 +138,85 @@ fn a_rows_menu_redefines_and_clears_the_overrides_of_that_rows_style() {
|
||||
assert_eq!(std::mem::take(&mut state.borrow_mut().1), vec![TextStyleAction::Redefine(1)], "the footer redefines the current style");
|
||||
assert_eq!(menu_ran(&mut fixture, &state, "None", "Redefine Style"), vec![], "None is never redefined");
|
||||
}
|
||||
|
||||
/// The selection the panel shows and the actions it reported.
|
||||
type Actions = Rc<RefCell<(Selection<u64>, Vec<TextStyleAction>)>>;
|
||||
|
||||
/// The tip every editable row describes itself with.
|
||||
const ROW_TIP: &str = "Click to apply, Alt-click to clear overrides, double-click for Style Options";
|
||||
|
||||
/// A Character Styles panel over a style 0 named `zero`, with style 1 current and overridden; `base` turns on
|
||||
/// [`TextStylesPanel::base_character_style`].
|
||||
fn character_panel(zero: &'static str, base: bool) -> (Fixture, Actions) {
|
||||
let state = Rc::new(RefCell::new((Selection::<u64>::default(), Vec::<TextStyleAction>::new())));
|
||||
let shared = state.clone();
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let rows = [TextStyleRow::new(0, zero), TextStyleRow::new(1, "Emphasis")];
|
||||
let mut guard = shared.borrow_mut();
|
||||
let (selection, actions) = &mut *guard;
|
||||
let out = TextStylesPanel::new("Character Styles", TextStyleKind::Character, &rows, Bind::bound(selection))
|
||||
.current(Some(1), true)
|
||||
.target(true)
|
||||
.base_character_style(base)
|
||||
.show(ui);
|
||||
actions.extend(out.action);
|
||||
});
|
||||
fixture.idle(10);
|
||||
(fixture, state)
|
||||
}
|
||||
|
||||
/// The description the row named `name` carries.
|
||||
fn description(fixture: &Fixture, name: &str) -> Option<String> {
|
||||
let row = fixture.by_label(name).expect("the row is shown");
|
||||
fixture.tree().description_of(row.node).map(str::to_owned)
|
||||
}
|
||||
|
||||
/// By default character style 0 is "No character style": its row says so, and neither its menu, a double click nor the
|
||||
/// footer opens its options, duplicates it or deletes it.
|
||||
#[trial]
|
||||
fn character_style_none_takes_no_options_duplicate_or_delete() {
|
||||
let (mut fixture, state) = character_panel("None", false);
|
||||
assert_eq!(description(&fixture, "None").as_deref(), Some("No character style"), "the row describes itself as no style");
|
||||
|
||||
let none = fixture.by_label("None").expect("the None row is shown");
|
||||
fixture.click(&none).click(&none).idle(4);
|
||||
std::mem::take(&mut state.borrow_mut().1);
|
||||
let options = fixture.by_label("Style Options…").expect("the footer's Style Options");
|
||||
fixture.click(&options).idle(4);
|
||||
assert_eq!(std::mem::take(&mut state.borrow_mut().1), vec![], "the footer opens no options for None");
|
||||
let duplicate = fixture.by_label("Duplicate style").expect("the footer's Duplicate");
|
||||
fixture.click(&duplicate).idle(4);
|
||||
assert_eq!(std::mem::take(&mut state.borrow_mut().1), vec![], "the footer does not duplicate None");
|
||||
|
||||
assert_eq!(menu_ran(&mut fixture, &state, "None", "Style Options…"), vec![], "the menu opens no options for None");
|
||||
}
|
||||
|
||||
/// With `base_character_style`, character style 0 is a style like the others: a double click, the footer and its menu
|
||||
/// open its options, duplicate it and redefine it, and its row carries the editable rows' tip; it is still never deleted.
|
||||
#[trial]
|
||||
fn a_base_character_style_takes_options_duplicate_and_redefine_but_not_delete() {
|
||||
let (mut fixture, state) = character_panel("Normal", true);
|
||||
assert_eq!(description(&fixture, "Normal").as_deref(), Some(ROW_TIP), "the row describes itself as editable");
|
||||
|
||||
let normal = fixture.by_label("Normal").expect("the Normal row is shown");
|
||||
fixture.click(&normal).click(&normal).idle(4);
|
||||
assert!(std::mem::take(&mut state.borrow_mut().1).contains(&TextStyleAction::Options(0)), "a double click opens Style Options");
|
||||
let options = fixture.by_label("Style Options…").expect("the footer's Style Options");
|
||||
fixture.click(&options).idle(4);
|
||||
assert_eq!(std::mem::take(&mut state.borrow_mut().1), vec![TextStyleAction::Options(0)], "the footer opens its options");
|
||||
let duplicate = fixture.by_label("Duplicate style").expect("the footer's Duplicate");
|
||||
fixture.click(&duplicate).idle(4);
|
||||
assert_eq!(std::mem::take(&mut state.borrow_mut().1), vec![TextStyleAction::Duplicate(0)], "the footer duplicates it");
|
||||
let delete = fixture.by_label("Delete style").expect("the footer's Delete");
|
||||
fixture.click(&delete).idle(4);
|
||||
assert_eq!(std::mem::take(&mut state.borrow_mut().1), vec![], "the footer does not delete it");
|
||||
fixture.advance(1.0).click(&normal).idle(4);
|
||||
std::mem::take(&mut state.borrow_mut().1);
|
||||
fixture.key(Key::Delete, Modifiers::NONE).idle(4);
|
||||
assert_eq!(std::mem::take(&mut state.borrow_mut().1), vec![], "the Delete key does not delete it");
|
||||
|
||||
assert_eq!(menu_ran(&mut fixture, &state, "Normal", "Style Options…"), vec![TextStyleAction::Options(0)], "the menu opens its options");
|
||||
assert_eq!(menu_ran(&mut fixture, &state, "Normal", "Duplicate Style"), vec![TextStyleAction::Duplicate(0)], "the menu duplicates it");
|
||||
assert_eq!(menu_ran(&mut fixture, &state, "Normal", "Redefine Style"), vec![TextStyleAction::Redefine(0)], "the menu redefines it");
|
||||
assert_eq!(menu_ran(&mut fixture, &state, "Normal", "Delete Style"), vec![], "the menu does not delete it");
|
||||
}
|
||||
Reference in new issue
Block a user