Skip to main content

Module toolbar

Module toolbar 

Source
Available on crate feature toolbar only.
Expand description

A compact strip of icon buttons: actions, toggles that stay pressed, single-choice groups, separators and a spacer.

State owns the entries and whether each toggle or choice is on. The toolbar measures itself with iced’s responsive widget, and the entries that do not fit move, in order from the end, into a “More” dropdown menu at the end of the strip, where toggles become checkbox items and choices radio items. So a toolbar built for a desktop window still works on a phone.

The keyboard moves a highlight along the strip. The app routes key presses from keys::subscription through State::key_event: Ctrl+F10 focuses the toolbar, Left and Right move between the buttons (and the More button), Enter or Space presses one and Escape leaves. iced gives an app-drawn strip no focus of its own, so the toolbar tracks its focus in State and draws the highlight as a ring.

use iced::Element;
use iced_cube::lucide;
use iced_cube::toolbar::{self, Output, separator, toggle, toolbar};

#[derive(Debug, Clone)]
enum Message {
    Toolbar(toolbar::Event<&'static str>),
}

struct App {
    toolbar: toolbar::State<&'static str>,
}

impl App {
    fn new() -> Self {
        let toolbar = toolbar::State::new([
            toggle("bold", lucide!(Bold), "Bold", false).shortcut("Ctrl+B"),
            toggle("italic", lucide!(Italic), "Italic", false),
            separator(),
            toolbar::button("undo", lucide!(Undo2), "Undo"),
        ]);
        Self { toolbar }
    }

    fn update(&mut self, message: Message) {
        let Message::Toolbar(event) = message;
        if let Some(Output::Toggled(id, on)) = self.toolbar.update(event) {
            println!("{id} is now {on}");
        }
    }

    fn view(&self) -> Element<'_, Message> {
        toolbar(&self.toolbar).on_event(Message::Toolbar).into()
    }
}

Re-exports§

pub use crate::overlay::dropdown_menu::Output;

Structs§

Item
One icon button.
State
The entries, their on and off states, the overflow menu and the keyboard highlight.
Toolbar
A toolbar builder. Convert it into an Element to render.

Enums§

Action
What a toolbar keyboard shortcut does.
Entry
One slot of a toolbar.
Event
Changes to a toolbar.
Highlight
Where the keyboard highlight is.
Kind
What a button does when pressed.

Constants§

GAP
Space between neighbouring entries.
PADDING
Space between the strip’s edge and its entries.
RING_WIDTH
Width of the highlight ring around the focused button.
SEPARATOR_HEIGHT
Height of a separator’s line.
SEPARATOR_WIDTH
Width a separator takes: the line and a margin on each side.

Functions§

bar_style
The style of the strip behind the buttons.
button
An action button.
choice
A button that turns itself on and the choices next to it off.
default_keymap
The default toolbar shortcuts:
fit
How many entries fit in width before the rest move into the overflow menu. Every entry fits when they all do; otherwise room is kept for the More button and trailing separators and spacers go too.
overflow_entries
The overflow menu’s entries for everything from start on. Toggles become checkbox items and choices radio items; spacers are dropped and separators kept only between items.
ring_style
The ring drawn around the highlighted button.
separator
spacer
toggle
A button that stays pressed while on.
toolbar
Renders the entries of state that fit, and a More menu with the rest. Without on_event every button renders disabled.