Skip to main content

Module formatting_toolbar

Module formatting_toolbar 

Source
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§

FormattingToolbar
A formatting toolbar builder. Convert it into an Element to render.
State
The buttons, the size menu and the link field.

Enums§

Event
Changes to the toolbar.
Output
What a press asks of the app.
Tool
A button in the strip.

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_event every control renders disabled.
unfocus
A task that takes focus from whichever widget has it, for Output::Unfocus.