Skip to main content

Module selectable_text

Module selectable_text 

Source
Available on crate feature selectable-text only.
Expand description

Rich text that can be selected with the mouse and copied.

selectable_text takes the spans iced’s rich text takes and draws its own part of a selection. A selection_area around any number of them owns the selection, so a drag can run from one paragraph into the next: drag to select, double click for a word, triple click for a block. Links still open on a plain click.

The area sends the selected text through on_select each time a selection is finished and through on_copy when the Copy shortcut is pressed; the app writes it to the clipboard. Keyboard shortcuts resolve through a Keymap of Actions; see default_keymap. Typing, Tab or another Ctrl or Cmd chord drops the selection, as the keys then belong to something else. A field given focus without a key or a press, by a task, does not: clear the selection then if the two share chords.

Put the scrollable inside the area, not around it, and a drag past its top or bottom edge scrolls it.

For a context menu, give the area an id and wrap it in a context menu whose items run selected and select_all.

use iced::widget::{column, span};
use iced::{Element, Task};
use iced_cube::data::selectable_text::{selectable_text, selection_area};

#[derive(Debug, Clone)]
enum Message {
    Copy(String),
    Open(String),
}

fn view<'a>() -> Element<'a, Message> {
    selection_area(column![
        selectable_text(vec![span("Read the "), span("guide").link("guide".to_owned())])
            .on_link_click(Message::Open),
        selectable_text::<String, _>(vec![span("A second paragraph.")]),
    ])
    .on_copy(Message::Copy)
    .into()
}

fn update(message: Message) -> Task<Message> {
    match message {
        Message::Copy(text) => iced::clipboard::write(text),
        Message::Open(_) => Task::none(),
    }
}

Modules§

operation
The widget operations behind selected and select_all, for apps that run operations themselves.

Structs§

SelectableText
Rich text whose links can be clicked and whose text can be selected inside a SelectionArea. Convert it into an Element to render.
SelectionArea
Lets the SelectableTexts inside it be selected together. Convert it into an Element to render.
Style
How selected text looks.

Enums§

Action
What a selection area’s keyboard shortcut does.

Constants§

DOUBLE_CLICK
How soon a press must follow the last to count as a double or triple click.
MAX_SCROLL_SPEED
The fastest a drag scrolls, in pixels a second.
SCROLL_GAIN
How much faster it scrolls for each pixel the pointer is past the edge.
SCROLL_SPEED
How fast a drag just past the edge of a scrollable scrolls it, in pixels a second.

Functions§

default_keymap
The default selection shortcuts. Mod is Cmd on macOS and Ctrl elsewhere.
scroll_speed
How fast a drag distance pixels past the edge of a scrollable scrolls it, in pixels a second.
select_all
Selects every text in the area with this id and produces it.
selectable_text
Selectable rich text made of spans, as iced’s rich text takes them. Text without links needs the link type named: selectable_text::<String, _>(...).
selected
Produces the text selected in the area with this id, which is empty when nothing is. Produces nothing when no such area is on screen.
selection_area
An area in which the selectable texts of content are selected together.
style
The selection colour, matching a text field’s.