aboutsummaryrefslogtreecommitdiff
path: root/src/operation.rs
blob: 584f61fb3e0187420003f55a44128b4254dbc260 (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//! Provides some operations to be used on the [`Text`] or [`Rich`].
//!
//! [`Text`]: crate::text::Text
//! [`Rich`]: crate::text::Rich

use crate::core::widget::operation::{self, Operation};

/// Some selected text
pub struct Selection(pub String);

/// Some mutable independent selection field of a [`Text`] or [`Rich`] that can be changed through
/// an operation.
///
/// [`Text`]: crate::text::Text
/// [`Rich`]: crate::text::Rich
pub struct IndependentSelection<'a>(pub &'a mut bool);

impl<'a> IndependentSelection<'a> {
    /// Creates a new [`IndependentSelection`] with the given mutable reference
    pub fn new(isel: &'a mut bool) -> Self {
        Self(isel)
    }
}

/// Gets all the currently selected text
pub fn selected() -> impl Operation<String> {
    struct CopySelection {
        contents: Vec<String>,
    }

    impl Operation<String> for CopySelection {

        fn traverse(&mut self, operate: &mut dyn FnMut(&mut dyn Operation<String>)) {
            operate(self);
        }

        fn custom(
            &mut self,
            _id: Option<&iced_widget::Id>,
            _bounds: iced_widget::core::Rectangle,
            _state: &mut dyn std::any::Any,
        ) {
            if let Some(selection) = _state.downcast_ref::<Selection>() {
                self.contents.push(selection.0.clone());
            }
        }

        fn finish(&self) -> operation::Outcome<String> {
            if !self.contents.is_empty() {
                let clipboard = self.contents.iter().fold(String::new(), |mut str, s| {
                    if str.is_empty() {
                        str = s.to_owned();
                    } else {
                        str.push('\n');
                        str.push_str(s);
                    }
                    str
                });
                operation::Outcome::Some(clipboard)
            } else {
                operation::Outcome::None
            }
        }
    }

    CopySelection {
        contents: Vec::new(),
    }
}

/// Sets all children instances of [`Text`] or [`Rich`] to be globally selectable instead of
/// independently selectable.
///
/// [`Text`]: crate::text::Text
/// [`Rich`]: crate::text::Rich
pub fn global_selection() -> impl Operation {
    struct SetGlobalSelection;

    impl Operation for SetGlobalSelection {
        fn traverse(&mut self, operate: &mut dyn FnMut(&mut dyn Operation)) {
            operate(self);
        }

        fn custom(
            &mut self,
            _id: Option<&iced_widget::Id>,
            _bounds: iced_widget::core::Rectangle,
            _state: &mut dyn std::any::Any,
        ) {
            // if let Some(selection) = _state.downcast_mut::<IndependentSelection<'_>>() {
            //     *selection.0 = false;
            // }
            if let Some(selection) = _state.downcast_mut::<bool>() {
                *selection = false;
            }
        }
    }

    SetGlobalSelection
}