1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use super::Ui;
use crate::Id;
use std::hash::Hash;
/// A group of [`SelectableLabel`](super::SelectableLabel)s that select as one document.
///
/// A drag that starts in one label and ends in another selects the end of the first, every
/// label between them, and the start of the last, in the order the labels were built.
/// Ctrl/Cmd+A selects the whole group and Ctrl/Cmd+C copies it with the labels joined by a
/// line break (or [`SelectionScope::separator`]). A label outside any scope is a group of one.
///
/// Only labels that were built take part: a virtualized list builds just its visible rows, so
/// rows that were never drawn are not selected and not copied.
///
/// ```
/// # use zaxis::*;
/// # let mut ctx = Context::new();
/// # ctx.run(|c| { Root::new().show(c, |ui| {
/// ui.selection_scope(|ui| {
/// ui.selectable_label("First paragraph");
/// ui.selectable_label("Second paragraph");
/// });
/// # }); });
/// ```
pub struct SelectionScope {
id: Option<Id>,
separator: String,
copy_urls: bool,
}
impl SelectionScope {
pub fn new() -> Self {
Self {
id: None,
separator: "\n".into(),
copy_urls: false,
}
}
pub fn id_source(mut self, source: impl Hash) -> Self {
self.id = Some(Id::new(source));
self
}
/// Text put between the copied labels; a line break by default.
pub fn separator(mut self, separator: impl Into<String>) -> Self {
self.separator = separator.into();
self
}
/// Copy ` (url)` after each link of a copied selection.
pub fn copy_url_in_selection(mut self, copy: bool) -> Self {
self.copy_urls = copy;
self
}
pub fn show<R>(self, ui: &mut Ui<'_>, build: impl FnOnce(&mut Ui<'_>) -> R) -> R {
let id = match self.id {
Some(source) => ui.scope.with(("selection-scope", source)),
None => ui.next_id("selection-scope"),
};
let scope = ui.context.selection.scopes.entry(id).or_default();
scope.separator = self.separator;
scope.copy_urls = self.copy_urls;
ui.context.selection.stack.push(id);
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| build(ui)));
ui.context.selection.stack.pop();
ui.context.selection_commit(id);
match result {
Ok(value) => value,
Err(error) => std::panic::resume_unwind(error),
}
}
}
impl Default for SelectionScope {
fn default() -> Self {
Self::new()
}
}
impl Ui<'_> {
/// Group selectable labels into one selection; see [`SelectionScope`].
pub fn selection_scope<R>(&mut self, build: impl FnOnce(&mut Ui<'_>) -> R) -> R {
SelectionScope::new().show(self, build)
}
}