6ef13b2cc8a6
buttons.rs
use super::groups::command_menu;
use super::tooltip::{keytip_badge, shortcut_text, show_hint, tooltip, tooltip_lines};
use super::{Color32, CornerRadius, Rect, Response, Sense, Stroke, Tokens, Ui, Value, WordApp, icons, pos2, regular, vec2};
/// Height of the ribbon content area (without group labels).
pub const CONTENT_H: f32 = 66.0;
/// Height of the group title row under the content.
pub const LABEL_H: f32 = 18.0;
/// Large buttons: centre of the 32 px icon, top of the first label line, and the pitch and
/// height of a label line (`regular(11.5)`; a test checks the font's real line height).
pub const BIG_ICON_Y: f32 = 18.0;
pub const BIG_LABEL_Y: f32 = 36.0;
pub const LABEL_PITCH: f32 = 13.0;
pub const LABEL_LINE_H: f32 = 14.0;
/// Whether a command exists and is enabled.
pub fn enabled(app: &WordApp, id: &str) -> bool {
// No document open (#1129): only what opens one, Help and the settings.
if !app.has_document() && !crate::works_without_document(id) {
return false;
}
match app.session.registry.get(id) {
Some(_) => app.session.why_disabled(id).is_none(),
// Zotero: one command at a time, and only in the desktop app.
None if id.starts_with("ui.zotero.") => app.zotero.busy.is_none() && !cfg!(target_arch = "wasm32"),
None if id == "ui.linkTextBox" => enabled(app, "shape.link"),
None if id == "ui.crop" => crate::crop::can_toggle(app),
None => id.starts_with("ui."),
}
}
pub(crate) fn bg(ui: &Ui, r: Rect, resp: &Response, checked: bool, t: &Tokens) {
bg_if(ui, r, resp, checked, t, true);
}
/// Paints a button background with an optional hover/press response. Disabled ribbon commands
/// pass `false` so their visual state cannot imply that they are interactive.
pub(crate) fn bg_if(ui: &Ui, r: Rect, resp: &Response, checked: bool, t: &Tokens, on: bool) {
let hover = ui.ctx().animate_bool_responsive(resp.id.with("hover"), resp.hovered() && on);
let fill = if resp.is_pointer_button_down_on() && on {
t.pressed
} else if checked {
t.checked
} else if hover > 0.0 {
Color32::TRANSPARENT.lerp_to_gamma(t.hover, hover)
} else {
return;
};
ui.painter().rect_filled(r, CornerRadius::same(4), fill);
if checked {
ui.painter().rect_stroke(r, CornerRadius::same(4), Stroke::new(1.0, t.accent.linear_multiply(0.5)), egui::StrokeKind::Inside);
}
}
/// Runs a ribbon button's command, then hands keyboard focus back to the document so typing
/// continues without another click, as in Word (#686). Interface commands (`ui.*`) open dialogs,
/// panes and tabs that take focus themselves, so they keep it.
pub(crate) fn run_from_ribbon(app: &mut WordApp, id: &str, params: Value) {
let _ = app.run(id, params);
if !id.starts_with("ui.") {
app.canvas.want_focus = true;
}
}
/// A large ribbon button: 32 px icon over a (possibly two-line) label.
pub fn big(ui: &mut Ui, app: &mut WordApp, icon: &str, label: &str, id: &str, params: Value, menu: bool) -> Response {
big_button(ui, app, icon, label, id, params, BigState { menu, checked: false })
}
/// A large ribbon button for a mode that stays on (`checked` draws it pressed), run without params.
pub fn big_toggle(ui: &mut Ui, app: &mut WordApp, icon: &str, label: &str, id: &str, checked: bool) -> Response {
big_button(ui, app, icon, label, id, Value::Object(Default::default()), BigState { menu: false, checked })
}
/// Size of the drop-down marker drawn after a label or beside a split button (#659: one marker,
/// the `dropdown` icon, everywhere; no text glyph).
pub const DROPDOWN: f32 = 10.0;
/// One centred label line (top at `y`); `marker` draws the drop-down icon after the text, in the
/// text's colour, and the pair is centred together.
pub fn label_line(ui: &Ui, cx: f32, y: f32, text: &str, font: egui::FontId, color: Color32, marker: bool) {
let galley = ui.painter().layout_no_wrap(text.to_string(), font, color);
let extra = if marker { DROPDOWN + 3.0 } else { 0.0 };
let size = galley.size();
let x = cx - (size.x + extra) / 2.0;
ui.painter().galley(pos2(x, y), galley, color);
if marker {
let c = pos2(x + size.x + 3.0 + DROPDOWN / 2.0, y + size.y / 2.0);
icons::paint(ui.painter(), Rect::from_center_size(c, vec2(DROPDOWN, DROPDOWN)), "dropdown", color, color);
}
}
/// How a large ribbon button behaves: `menu` adds a drop-down marker, `checked` draws it toggled.
#[derive(Clone, Copy)]
struct BigState {
menu: bool,
checked: bool,
}
fn big_button(ui: &mut Ui, app: &mut WordApp, icon: &str, label: &str, id: &str, params: Value, state: BigState) -> Response {
let BigState { menu, checked } = state;
let label = tl!(label);
let t = Tokens::get(ui.ctx());
let galley_w =
label.split('\n').map(|l| ui.ctx().fonts_mut(|f| f.layout_no_wrap(l.to_string(), regular(11.5), t.text).size().x)).fold(0.0, f32::max);
let w = (galley_w + 12.0).max(44.0);
let (r, resp) = ui.allocate_exact_size(vec2(w, CONTENT_H), Sense::click());
keytip_badge(ui, r, id, &label.replace('\n', " "));
let on = enabled(app, id);
bg_if(ui, r, &resp, checked, &t, on);
let ic = Rect::from_center_size(pos2(r.center().x, r.min.y + BIG_ICON_Y), vec2(32.0, 32.0));
let (c, a) = if on { (t.icon, t.accent) } else { (t.text_disabled, t.text_disabled) };
icons::paint(ui.painter(), ic, icon, c, a);
let mut y = r.min.y + BIG_LABEL_Y;
let lines: Vec<&str> = label.split('\n').collect();
for (i, l) in lines.iter().enumerate() {
label_line(ui, r.center().x, y, l, regular(11.5), if on { t.text } else { t.text_disabled }, menu && i + 1 == lines.len());
y += LABEL_PITCH;
}
let resp = tooltip(app, resp, &label.replace('\n', " "), id);
command_menu(&resp, app, id);
if resp.clicked() && on && !menu {
run_from_ribbon(app, id, params);
}
resp
}
/// A small button: 16 px icon with an optional label; `checked` draws the toggled state.
pub struct ButtonAction<'a> {
pub id: &'a str,
pub params: Value,
pub checked: bool,
}
impl<'a> ButtonAction<'a> {
pub fn new(id: &'a str, params: Value, checked: bool) -> Self {
Self { id, params, checked }
}
}
pub fn small(ui: &mut Ui, app: &mut WordApp, icon: &str, label: Option<&str>, tip: &str, action: ButtonAction<'_>) -> Response {
let ButtonAction { id, params, checked } = action;
let (label, tip) = (label.map(|l| tl!(l)), tl!(tip));
// A compact group (#662) shows only the icon; the tooltip keeps the name.
let label = label.filter(|_| !crate::ribbon_fit::compact(ui.ctx()));
let t = Tokens::get(ui.ctx());
let text_w = label.map(|l| ui.ctx().fonts_mut(|f| f.layout_no_wrap(l.to_string(), regular(11.5), t.text).size().x) + 6.0).unwrap_or(0.0);
let (r, resp) = ui.allocate_exact_size(vec2(24.0 + text_w, 22.0), Sense::click());
keytip_badge(ui, r, id, tip);
let on = enabled(app, id);
bg_if(ui, r, &resp, checked, &t, on);
let rtl = crate::rtl::flows(ui);
let ic = Rect::from_center_size(pos2(crate::rtl::lead_x(rtl, r, 12.0), r.center().y), vec2(17.0, 17.0));
let (c, a) = if on { (t.icon, t.accent) } else { (t.text_disabled, t.text_disabled) };
icons::paint(ui.painter(), ic, icon, c, a);
if let Some(l) = label {
let at = pos2(crate::rtl::lead_x(rtl, r, 24.0), r.center().y);
ui.painter().text(at, crate::rtl::lead_center(rtl), l, regular(11.5), if on { t.text } else { t.text_disabled });
}
let resp = tooltip(app, resp, tip, id);
command_menu(&resp, app, id);
if resp.clicked() && on {
run_from_ribbon(app, id, params);
}
resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, on, tip));
resp
}
/// A split button: the left part runs `id`, the arrow opens a menu filled by `menu`.
pub fn split(
ui: &mut Ui,
app: &mut WordApp,
icon: &str,
tip: &str,
action: ButtonAction<'_>,
swatch: Option<Color32>,
menu: impl FnOnce(&mut Ui, &mut WordApp),
) -> Response {
split_button(ui, app, icon, tip, action, SplitLook { swatch, labeled: false }, menu)
}
/// A [`split`] button with its label beside the icon and the arrow on the same row, also in a
/// vertical ribbon stack (Table Design › Shading, #1726). A compact group shows only the icon.
pub fn split_labeled(
ui: &mut Ui,
app: &mut WordApp,
icon: &str,
label: &str,
action: ButtonAction<'_>,
swatch: Option<Color32>,
menu: impl FnOnce(&mut Ui, &mut WordApp),
) -> Response {
ui.horizontal(|ui| {
ui.spacing_mut().item_spacing.x = 0.0;
split_button(ui, app, icon, label, action, SplitLook { swatch, labeled: true }, menu)
})
.inner
}
/// A split button's look beyond its icon: the colour bar under it, and its name beside it.
struct SplitLook {
swatch: Option<Color32>,
labeled: bool,
}
fn split_button(
ui: &mut Ui,
app: &mut WordApp,
icon: &str,
tip: &str,
action: ButtonAction<'_>,
look: SplitLook,
menu: impl FnOnce(&mut Ui, &mut WordApp),
) -> Response {
let ButtonAction { id, params, checked } = action;
let tip = tl!(tip);
let t = Tokens::get(ui.ctx());
let on = enabled(app, id);
// A compact group (#662) shows only the icon, as `small` does; the tooltip keeps the name.
let label = (look.labeled && !crate::ribbon_fit::compact(ui.ctx()))
.then(|| ui.painter().layout_no_wrap(tip.to_string(), regular(11.5), if on { t.text } else { t.text_disabled }));
let text_w = label.as_ref().map_or(0.0, |g| g.size().x + 6.0);
// What a compact group gives back, so the ribbon can plan it before drawing it compact.
crate::ribbon_fit::add_saving(ui.ctx(), text_w);
let (r, resp) = ui.allocate_exact_size(vec2(24.0 + text_w, 22.0), Sense::click());
keytip_badge(ui, r, id, tip);
let (ar, aresp) = ui.allocate_exact_size(vec2(11.0, 22.0), Sense::click());
bg_if(ui, r, &resp, checked, &t, on);
bg_if(ui, ar, &aresp, false, &t, on);
// The icon (and its colour bar) leads; a label follows it, mirrored in a right-to-left interface.
let rtl = crate::rtl::flows(ui);
let ic = Rect::from_center_size(pos2(crate::rtl::lead_x(rtl, r, 12.0), r.center().y), vec2(17.0, 17.0));
let (c, a) = if on { (t.icon, t.accent) } else { (t.text_disabled, t.text_disabled) };
icons::paint(ui.painter(), ic, icon, c, a);
if let Some(sw) = look.swatch {
let sw = if on { sw } else { t.text_disabled };
let left = crate::rtl::lead_left(rtl, r, 4.0, 16.0);
let bar = Rect::from_min_max(pos2(left, r.max.y - 5.0), pos2(left + 16.0, r.max.y - 2.0));
ui.painter().rect_filled(bar, 0.0, sw);
ui.painter().rect_stroke(bar, 0.0, Stroke::new(0.5, t.border), egui::StrokeKind::Inside);
}
if let Some(g) = label {
let y = r.center().y - g.size().y / 2.0;
ui.painter().galley(pos2(crate::rtl::lead_left(rtl, r, 24.0, g.size().x), y), g, t.text);
}
icons::paint(ui.painter(), Rect::from_center_size(ar.center(), vec2(DROPDOWN, DROPDOWN)), "dropdown", c, a);
let resp = tooltip(app, resp, tip, id);
command_menu(&resp, app, id);
if resp.clicked() && on {
run_from_ribbon(app, id, params);
}
let (arrow_title, _) = tooltip_lines(&crate::i18n::fmt(tl!("{name} options"), &[("name", tip)]), "", "", None);
show_hint(&aresp, &arrow_title, &[]);
resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, on, tip));
// The arrow opens the choices beside the button: it says whose, so it is not just "button"
// (#3172).
aresp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, on, &arrow_title));
if !on {
return resp;
}
crate::rtl::menu(&aresp).show(|ui| {
ui.set_min_width(180.0);
menu(ui, app);
});
resp
}
/// Whether a menu button whose menu is about command `id` is on ("" = always).
pub fn menu_enabled(app: &WordApp, id: &str) -> bool {
id.is_empty() || enabled(app, id)
}
/// A button that only opens a menu (icon + label + drop-down marker).
pub struct MenuButtonOptions<'a> {
pub icon: &'a str,
pub label: Option<&'a str>,
pub tip: &'a str,
pub big_btn: bool,
pub id: &'a str,
}
impl<'a> MenuButtonOptions<'a> {
pub fn new(icon: &'a str, label: Option<&'a str>, tip: &'a str, big_btn: bool, id: &'a str) -> Self {
Self { icon, label, tip, big_btn, id }
}
}
pub fn menu_button(ui: &mut Ui, app: &mut WordApp, options: MenuButtonOptions<'_>, menu: impl FnOnce(&mut Ui, &mut WordApp)) {
let MenuButtonOptions { icon, label, tip, big_btn, id } = options;
// The keytips table holds the tips in English, so the badge looks this one up, not the
// translated tip, which no other language's "Paste" equals.
let keytip_name = tip;
let (label, tip) = (label.map(|l| tl!(l)), tl!(tip));
let label = label.filter(|_| big_btn || !crate::ribbon_fit::compact(ui.ctx()));
let t = Tokens::get(ui.ctx());
// `id` names the command the menu is about (Header for the Header menu…); while it is
// disabled the button greys out and doesn't open, like the other ribbon buttons. "" = always on.
let on = menu_enabled(app, id);
let (c, a, text) = if on { (t.icon, t.accent, t.text) } else { (t.text_disabled, t.text_disabled, t.text_disabled) };
let resp = if big_btn {
let galley_w = label
.unwrap_or("")
.split('\n')
.map(|l| ui.ctx().fonts_mut(|f| f.layout_no_wrap(l.to_string(), regular(11.5), t.text).size().x))
.fold(0.0, f32::max);
let w = (galley_w + 22.0).max(44.0);
let (r, resp) = ui.allocate_exact_size(vec2(w, CONTENT_H), Sense::click());
keytip_badge(ui, r, "menu", keytip_name);
bg_if(ui, r, &resp, false, &t, on);
icons::paint(ui.painter(), Rect::from_center_size(pos2(r.center().x, r.min.y + BIG_ICON_Y), vec2(32.0, 32.0)), icon, c, a);
let lines: Vec<&str> = label.unwrap_or("").split('\n').collect();
let mut y = r.min.y + BIG_LABEL_Y;
for (i, l) in lines.iter().enumerate() {
label_line(ui, r.center().x, y, l, regular(11.5), text, i + 1 == lines.len());
y += LABEL_PITCH;
}
resp
} else {
let text_w = label.map(|l| ui.ctx().fonts_mut(|f| f.layout_no_wrap(l.to_string(), regular(11.5), t.text).size().x) + 6.0).unwrap_or(0.0);
let (r, resp) = ui.allocate_exact_size(vec2(24.0 + text_w + 10.0, 22.0), Sense::click());
keytip_badge(ui, r, "menu", keytip_name);
bg_if(ui, r, &resp, false, &t, on);
let rtl = crate::rtl::flows(ui);
let lead = |inset: f32| crate::rtl::lead_x(rtl, r, inset);
icons::paint(ui.painter(), Rect::from_center_size(pos2(lead(12.0), r.center().y), vec2(17.0, 17.0)), icon, c, a);
if let Some(l) = label {
ui.painter().text(pos2(lead(24.0), r.center().y), crate::rtl::lead_center(rtl), l, regular(11.5), text);
}
let arrow = pos2(crate::rtl::trail_x(rtl, r, 6.0), r.center().y);
icons::paint(ui.painter(), Rect::from_center_size(arrow, vec2(DROPDOWN, DROPDOWN)), "dropdown", c, c);
resp
};
let reason = if on { None } else { Some(app.session.why_disabled(id).unwrap_or("")) };
let (title, notes) = tooltip_lines(tip, &shortcut_text(app, id), "", reason);
show_hint(&resp, &title, ¬es);
resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, on, tip));
if !on {
return;
}
crate::rtl::menu(&resp).show(|ui| {
ui.set_min_width(200.0);
menu(ui, app);
});
}
No file is open. Choose one in the tree, or press Ctrl+O (⌘O on a Mac) to find one by name.
Syntax highlighting could not load. The unmodified read-only text remains available above.