use eframe::egui::{Key, KeyboardShortcut, Modifiers, RichText, Ui};
use egui_richedit::{Mark, Model};
use crate::i18n::t;
use crate::{Block, FlowModel, PageEditor};
pub fn marks<M: Model<Paragraph = Vec<usize>>>(
ui: &mut Ui,
editor: &PageEditor,
model: &M,
) -> Option<Mark> {
let mut pressed = None;
ui.add_enabled_ui(editor.selection().is_some(), |ui| {
ui.horizontal(|ui| pressed = mark_buttons(ui, editor, model));
});
pressed
}
#[derive(Default)]
pub struct Pressed {
pub mark: Option<Mark>,
pub block: Option<Block>,
}
pub fn text(ui: &mut Ui, editor: &PageEditor, model: &FlowModel<'_>) -> Pressed {
let mut pressed = Pressed::default();
ui.add_enabled_ui(editor.selection().is_some(), |ui| {
ui.horizontal(|ui| {
pressed.mark = mark_buttons(ui, editor, model);
ui.separator();
for (block, label, name) in [
(Block::Body, "\u{b6}", t("Body text")),
(Block::Heading(1), "H1", t("Heading 1")),
(Block::Heading(2), "H2", t("Heading 2")),
(Block::Heading(3), "H3", t("Heading 3")),
(Block::Bullets, "\u{2022}", t("Bulleted list")),
(Block::Numbers, "1.", t("Numbered list")),
] {
let lit = editor
.selection()
.is_some_and(|selection| model.is_block(selection, block));
if ui
.selectable_label(lit, label)
.on_hover_text(name)
.clicked()
{
pressed.block = Some(block);
}
}
});
});
pressed
}
fn mark_buttons<M: Model<Paragraph = Vec<usize>>>(
ui: &mut Ui,
editor: &PageEditor,
model: &M,
) -> Option<Mark> {
let mut pressed = None;
for mark in Mark::ALL {
let lit = editor.marked(model, mark) == Some(true);
let (label, name, key) = match mark {
Mark::Bold => (RichText::new(t("B")).strong(), t("Bold"), Some(Key::B)),
Mark::Italic => (RichText::new(t("I")).italics(), t("Italic"), Some(Key::I)),
Mark::Underline => (
RichText::new(t("U")).underline(),
t("Underline"),
Some(Key::U),
),
Mark::Strike => (
RichText::new(t("S")).strikethrough(),
t("Strikethrough"),
None,
),
};
let hint = match key {
Some(key) => format!(
"{name} ({})",
ui.ctx()
.format_shortcut(&KeyboardShortcut::new(Modifiers::COMMAND, key))
),
None => name.to_owned(),
};
if ui
.selectable_label(lit, label)
.on_hover_text(hint)
.clicked()
{
pressed = Some(mark);
}
}
pressed
}