//! 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(pub std::cell::RefCell); 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() } } /// Gets all the currently selected text pub fn selected() -> impl Operation { struct CopySelection { contents: Vec, } impl Operation for CopySelection { 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_ref::() { self.contents.push(selection.0.clone()); } } fn finish(&self) -> operation::Outcome { 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::() { let _ = selection.0.replace(false); } } } SetGlobalSelection }