Available on crate feature
formatting-toolbar only.Expand description
A toolbar for the rich text editor: text size, bold, italic, underline, strikethrough, lists, link and clear formatting, each showing whether it is on at the caret.
It is its own component so the app can put it anywhere: above the editor, in a window’s toolbar row or below a compose form. The buttons are a toolbar, so whatever does not fit moves into its More menu on a narrow window, and the text size is a dropdown menu.
The app owns a State. After every change to the editor’s
Content, call State::sync so the buttons follow the caret. A
press returns an Output: apply Output::Format to the content and
give the editor focus back, and focus LINK_FIELD on
Output::EditLink, when the address field opens.
use iced::widget::{column, operation};
use iced::{Element, Task};
use iced_cube::formatting_toolbar::{self, Output, formatting_toolbar};
use iced_cube::rich_text_editor::{self, Action, Content, rich_text_editor};
const BODY: &str = "body";
#[derive(Debug, Clone)]
enum Message {
Edit(Action),
Format(formatting_toolbar::Event),
}
#[derive(Default)]
struct Compose {
body: Content,
formatting: formatting_toolbar::State,
}
impl Compose {
fn update(&mut self, message: Message) -> Task<Message> {
let output = match message {
Message::Edit(action) => {
self.body.perform(action);
self.formatting.sync(&self.body)
}
Message::Format(event) => self.formatting.update(event, &self.body),
};
match output {
Some(Output::Format(action)) => {
self.body.perform(action);
self.formatting.sync(&self.body);
operation::focus(BODY)
}
Some(Output::EditLink) => operation::focus(formatting_toolbar::LINK_FIELD),
Some(Output::Closed) => operation::focus(BODY),
Some(Output::Unfocus) => formatting_toolbar::unfocus(),
None => Task::none(),
}
}
fn view(&self) -> Element<'_, Message> {
column![
formatting_toolbar(&self.formatting).on_event(Message::Format),
rich_text_editor(&self.body).id(BODY).on_action(Message::Edit),
]
.into()
}
}Structs§
- Formatting
Toolbar - A formatting toolbar builder. Convert it into an
Elementto render. - State
- The buttons, the size menu and the link field.
Enums§
Constants§
- LINK_
FIELD - The id of the link address field, to focus it on
Output::EditLink. - SIZE_
WIDTH - Width of the text size button, wide enough for the longest size name.
Functions§
- formatting_
toolbar - Renders the size menu, the buttons that fit and, while it is open, the
link field. Without
on_eventevery control renders disabled. - unfocus
- A task that takes focus from whichever widget has it, for
Output::Unfocus.