aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/operation.rs49
-rw-r--r--src/text.rs7
-rw-r--r--src/text/rich.rs9
3 files changed, 37 insertions, 28 deletions
diff --git a/src/operation.rs b/src/operation.rs
index 584f61f..2bfd6d8 100644
--- a/src/operation.rs
+++ b/src/operation.rs
@@ -13,12 +13,17 @@ pub struct Selection(pub String);
///
/// [`Text`]: crate::text::Text
/// [`Rich`]: crate::text::Rich
-pub struct IndependentSelection<'a>(pub &'a mut bool);
+pub struct IndependentSelection(pub std::cell::RefCell<bool>);
-impl<'a> IndependentSelection<'a> {
- /// Creates a new [`IndependentSelection`] with the given mutable reference
- pub fn new(isel: &'a mut bool) -> Self {
- Self(isel)
+impl IndependentSelection {
+ /// Creates a new [`IndependentSelection`] with the given value.
+ pub fn new(value: bool) -> Self {
+ Self(std::cell::RefCell::new(value))
+ }
+
+ /// Gets the inner bool value of this [`IndependentSelection`].
+ pub fn get(&self) -> bool {
+ *self.0.borrow()
}
}
@@ -29,8 +34,10 @@ pub fn selected() -> impl Operation<String> {
}
impl Operation<String> for CopySelection {
-
- fn traverse(&mut self, operate: &mut dyn FnMut(&mut dyn Operation<String>)) {
+ fn traverse(
+ &mut self,
+ operate: &mut dyn FnMut(&mut dyn Operation<String>),
+ ) {
operate(self);
}
@@ -47,15 +54,16 @@ pub fn selected() -> impl Operation<String> {
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
- });
+ 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
@@ -87,11 +95,10 @@ pub fn global_selection() -> impl Operation {
_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;
+ if let Some(selection) =
+ _state.downcast_mut::<IndependentSelection>()
+ {
+ let _ = selection.0.replace(false);
}
}
}
diff --git a/src/text.rs b/src/text.rs
index 76560ce..4b789c9 100644
--- a/src/text.rs
+++ b/src/text.rs
@@ -752,8 +752,11 @@ where
}
}
- // let mut isel = crate::operation::IndependentSelection::new(&mut self.independent_selection);
- operation.custom(None, layout.bounds(), &mut self.independent_selection);
+ let mut isel = crate::operation::IndependentSelection::new(
+ self.independent_selection,
+ );
+ operation.custom(None, layout.bounds(), &mut isel);
+ self.independent_selection = isel.get();
}
fn mouse_interaction(
diff --git a/src/text/rich.rs b/src/text/rich.rs
index b49adbd..eb8b6b2 100644
--- a/src/text/rich.rs
+++ b/src/text/rich.rs
@@ -954,12 +954,11 @@ where
}
}
- // let mut isel = crate::operation::IndependentSelection::new(&mut self.independent_selection);
- operation.custom(
- None,
- layout.bounds(),
- &mut self.independent_selection,
+ let mut isel = crate::operation::IndependentSelection::new(
+ self.independent_selection,
);
+ operation.custom(None, layout.bounds(), &mut isel);
+ self.independent_selection = isel.get();
}
fn mouse_interaction(