Mini toolbar: stack formatting controls into two rows inside floating TaskBar (#7375) (#7378)

This commit is contained in:
batistalucasv
2026-11-22 15:38:58 +00:00
committed by GitHub
parent 139f3a2c7e
commit f1decbad01
2 changed files with 96 additions and 69 deletions
+73 -69
View File
@@ -7,8 +7,9 @@ use craft::keymap::CommandRef;
use craft::pages::ui::PageBody;
use craft::ui::buttons::{IconButton, ToggleButton};
use craft::ui::canvas_controls::TaskBar;
use craft::ui::contract::{Bind, Entry, Hover, Props, Show as _};
use craft::ui::contract::{Bind, Entry, Gap, Hover, Props, Show as _, ShowContainer as _};
use craft::ui::display::Divider;
use craft::ui::layout_components::{Row, Stack};
use craft::ui::pickers::ComboBox;
use craft::ui::runtime::env::Label;
use egui::{Rect, Ui};
@@ -149,78 +150,81 @@ pub(crate) fn members(app: &mut WordApp, ui: &mut Ui, preview: bool) {
if preview && let Some(held) = app.canvas.list_preview.clone() {
app.canvas.command_preview = Some(held);
}
ui.horizontal(|ui| {
let mut family = st.get("font").and_then(Value::as_str).unwrap_or("").to_string();
let picked = crate::widgets::font_box::font_box(ui, app, tl!("Font").to_string(), &mut family);
follow_list(app, preview, &picked.preview, |f| Some(("format.font".to_string(), json!({"name": f}))));
if let Some(f) = &picked.picked {
let _ = app.run("format.font", json!({"name": f}));
}
Stack::new().gap(Gap::Tight).show(ui, |ui, _| {
Row::new().gap(Gap::Tight).show(ui, |ui, _| {
let mut family = st.get("font").and_then(Value::as_str).unwrap_or("").to_string();
let picked = crate::widgets::font_box::font_box(ui, app, tl!("Font").to_string(), &mut family);
follow_list(app, preview, &picked.preview, |f| Some(("format.font".to_string(), json!({"name": f}))));
if let Some(f) = &picked.picked {
let _ = app.run("format.font", json!({"name": f}));
}
let size = st.get("size").and_then(Value::as_f64).map(number_text).unwrap_or_default();
let sizes: Vec<Entry<String>> =
wordcraft_engine::cmd::format::SIZES.iter().map(|s| Entry::new(number_text(f64::from(*s)), number_text(f64::from(*s)))).collect();
let mut text = size.clone();
let combo = ComboBox::new(tl!("Font Size").to_string(), Bind::bound(&mut text), &sizes).show(ui);
follow_list(app, preview, &combo.preview, |v| v.trim().parse::<f64>().ok().map(|x| ("format.size".to_string(), json!({"size": x}))));
// Only a pick or Enter applies the size: typing alone is an edit of the box's draft.
let committed = combo.edit.submitted || combo.edit.events.iter().any(|e| matches!(e, EditEvent::Set { .. } | EditEvent::Commit { .. }));
if committed
&& text != size
&& let Ok(x) = text.trim().parse::<f64>()
{
let _ = app.run("format.size", json!({"size": x}));
}
let size = st.get("size").and_then(Value::as_f64).map(number_text).unwrap_or_default();
let sizes: Vec<Entry<String>> =
wordcraft_engine::cmd::format::SIZES.iter().map(|s| Entry::new(number_text(f64::from(*s)), number_text(f64::from(*s)))).collect();
let mut text = size.clone();
let combo = ComboBox::new(tl!("Font Size").to_string(), Bind::bound(&mut text), &sizes).show(ui);
follow_list(app, preview, &combo.preview, |v| v.trim().parse::<f64>().ok().map(|x| ("format.size".to_string(), json!({"size": x}))));
// Only a pick or Enter applies the size: typing alone is an edit of the box's draft.
let committed = combo.edit.submitted || combo.edit.events.iter().any(|e| matches!(e, EditEvent::Set { .. } | EditEvent::Commit { .. }));
if committed
&& text != size
&& let Ok(x) = text.trim().parse::<f64>()
{
let _ = app.run("format.size", json!({"size": x}));
}
for (id, icon) in [("format.growFont", "grow"), ("format.shrinkFont", "shrink")] {
let b = IconButton::command(crate::icons::id(icon), CommandRef::new(id)).show(ui);
preview_on_hover(app, b.hover, b.response.enabled(), id, json!({}), preview);
}
});
ui.horizontal(|ui| {
for (id, key, icon) in [
("format.bold", "bold", "bold"),
("format.italic", "italic", "italic"),
("format.underline", "underline", "underline"),
("format.strikethrough", "strike", "strike"),
] {
toggle(ui, app, id, icon, flag(key), preview);
}
let _ = Divider::vertical().show(ui);
let hl = crate::colors::highlight_split(ui, app, tl!("Text Highlight Color").to_string());
follow_list(app, preview, &hl.preview, |c| {
wordcraft_doc::props::Highlight::ALL
.iter()
.find(|h| h.rgb().as_ref() == Some(c))
.map(|h| ("format.highlight".to_string(), json!({"color": h.ooxml()})))
for (id, icon) in [("format.growFont", "grow"), ("format.shrinkFont", "shrink")] {
let b = IconButton::command(crate::icons::id(icon), CommandRef::new(id)).show(ui);
preview_on_hover(app, b.hover, b.response.enabled(), id, json!({}), preview);
}
});
// PLAN: assumed ColorPick::hover (ribbon-w11, S-L413)
let color = app.canvas.last_highlight.clone();
preview_on_hover(app, hl.hover, hl.response.enabled(), "format.highlight", json!({"color": color}), preview);
if hl.picked.is_some() {
app.canvas.focus_page(ui.ctx());
}
let fc = crate::colors::font_color_split(ui, app, tl!("Font Color").to_string(), Target::cmd("format.color").remember(Remember::FontColor));
follow_list(app, preview, &fc.preview, |c| Some(("format.color".to_string(), json!({"color": c.hex()}))));
// PLAN: assumed ColorPick::hover (ribbon-w11, S-L413)
let color = app.canvas.last_font_color.clone();
preview_on_hover(app, fc.hover, fc.response.enabled(), "format.color", json!({"color": color}), preview);
if fc.picked.is_some() {
app.canvas.focus_page(ui.ctx());
}
let _ = Divider::vertical().show(ui);
use wordcraft_doc::Align as A;
for (id, icon, a) in [
("para.alignLeft", "alignLeft", A::Left),
("para.alignCenter", "alignCenter", A::Center),
("para.alignRight", "alignRight", A::Right),
("para.justify", "justify", A::Justify),
] {
toggle(ui, app, id, icon, align == a, preview);
}
Row::new().gap(Gap::Tight).show(ui, |ui, _| {
for (id, key, icon) in [
("format.bold", "bold", "bold"),
("format.italic", "italic", "italic"),
("format.underline", "underline", "underline"),
("format.strikethrough", "strike", "strike"),
] {
toggle(ui, app, id, icon, flag(key), preview);
}
let _ = Divider::vertical().show(ui);
let hl = crate::colors::highlight_split(ui, app, tl!("Text Highlight Color").to_string());
follow_list(app, preview, &hl.preview, |c| {
wordcraft_doc::props::Highlight::ALL
.iter()
.find(|h| h.rgb().as_ref() == Some(c))
.map(|h| ("format.highlight".to_string(), json!({"color": h.ooxml()})))
});
// PLAN: assumed ColorPick::hover (ribbon-w11, S-L413)
let color = app.canvas.last_highlight.clone();
preview_on_hover(app, hl.hover, hl.response.enabled(), "format.highlight", json!({"color": color}), preview);
if hl.picked.is_some() {
app.canvas.focus_page(ui.ctx());
}
let fc =
crate::colors::font_color_split(ui, app, tl!("Font Color").to_string(), Target::cmd("format.color").remember(Remember::FontColor));
follow_list(app, preview, &fc.preview, |c| Some(("format.color".to_string(), json!({"color": c.hex()}))));
// PLAN: assumed ColorPick::hover (ribbon-w11, S-L413)
let color = app.canvas.last_font_color.clone();
preview_on_hover(app, fc.hover, fc.response.enabled(), "format.color", json!({"color": color}), preview);
if fc.picked.is_some() {
app.canvas.focus_page(ui.ctx());
}
let _ = Divider::vertical().show(ui);
use wordcraft_doc::Align as A;
for (id, icon, a) in [
("para.alignLeft", "alignLeft", A::Left),
("para.alignCenter", "alignCenter", A::Center),
("para.alignRight", "alignRight", A::Right),
("para.justify", "justify", A::Justify),
] {
toggle(ui, app, id, icon, align == a, preview);
}
});
});
}
+23
View File
@@ -214,6 +214,29 @@ fn every_control_fits_inside_the_bar() {
}
}
/// #7375: the bar's formatting controls are stacked into two rows: Row 2 controls (Bold,
/// Align Left) sit strictly below Row 1 controls (Font, Font Size) rather than side-by-side.
#[test]
fn formatting_controls_stack_into_two_rows() {
let h = bar_over("brown fox");
let font = font_box(&h);
let size = size_box(&h);
let bold = member(&h, "Bold").expect("the bar has Bold");
let align_left = member(&h, "Align Left").expect("the bar has Align Left");
// Row 1 controls sit on the top row together.
assert!((font.min.y - size.min.y).abs() < 1.0, "Font and Font Size must share Row 1: font {font:?}, size {size:?}");
// Row 2 controls sit on the second row together.
assert!((bold.min.y - align_left.min.y).abs() < 1.0, "Bold and Align Left must share Row 2: bold {bold:?}, align_left {align_left:?}");
// Row 2 controls sit strictly below Row 1 controls.
assert!(bold.min.y >= font.max.y, "Bold ({bold:?}) must sit strictly below Font ({font:?})");
assert!(bold.min.y >= size.max.y, "Bold ({bold:?}) must sit strictly below Font Size ({size:?})");
assert!(align_left.min.y >= font.max.y, "Align Left ({align_left:?}) must sit strictly below Font ({font:?})");
assert!(align_left.min.y >= size.max.y, "Align Left ({align_left:?}) must sit strictly below Font Size ({size:?})");
}
/// Hovering any member of the bar previews its command on the selection; the pick is not made.
#[test]
fn hovering_a_bar_member_previews_its_command() {