From 8a03f063c015bee0a0031f24c1082a086791e4a9 Mon Sep 17 00:00:00 2001 From: Hojjat Date: Mon, 29 Jun 2026 16:49:35 -0600 Subject: [PATCH] refactor: rename to `SelectableText` instead of `Text` This avoids API breakage --- src/applet/mod.rs | 4 +- src/widget/menu/menu_tree.rs | 2 +- src/widget/mod.rs | 4 + src/widget/selectable_text.rs | 505 ++++++++++++++++++++++++++++++++ src/widget/settings/item.rs | 8 +- src/widget/text.rs | 525 +++------------------------------- 6 files changed, 557 insertions(+), 491 deletions(-) create mode 100644 src/widget/selectable_text.rs diff --git a/src/applet/mod.rs b/src/applet/mod.rs index b9805c9b..37635bf4 100644 --- a/src/applet/mod.rs +++ b/src/applet/mod.rs @@ -290,7 +290,7 @@ impl Context { ) } - pub fn applet_tooltip<'a, Message: Clone + 'static>( + pub fn applet_tooltip<'a, Message: 'static>( &self, content: impl Into>, tooltip: impl Into>, @@ -502,7 +502,7 @@ impl Context { } } - pub fn text<'a>(&self, msg: impl Into>) -> crate::widget::Text<'a> { + pub fn text<'a>(&self, msg: impl Into>) -> crate::widget::Text<'a, crate::Theme> { let msg = msg.into(); let t = match self.size { Size::Hardcoded(_) => crate::widget::text, diff --git a/src/widget/menu/menu_tree.rs b/src/widget/menu/menu_tree.rs index cf99335c..bd4f1e6c 100644 --- a/src/widget/menu/menu_tree.rs +++ b/src/widget/menu/menu_tree.rs @@ -194,7 +194,7 @@ pub fn menu_root<'a, Message, Renderer: renderer::Renderer>( ) -> Button<'a, Message> where Element<'a, Message, crate::Theme, Renderer>: From>, - Message: std::clone::Clone + 'static, + Message: std::clone::Clone + 'a, { widget::button::custom(widget::text(label)) .padding([4, 12]) diff --git a/src/widget/mod.rs b/src/widget/mod.rs index 4c33f7e3..ed6f55d7 100644 --- a/src/widget/mod.rs +++ b/src/widget/mod.rs @@ -296,6 +296,10 @@ pub mod text; #[doc(inline)] pub use text::{Text, text}; +pub mod selectable_text; +#[doc(inline)] +pub use selectable_text::{SelectableText, selectable_text}; + pub mod text_context_menu; #[doc(inline)] pub use text_context_menu::HasSelectableText; diff --git a/src/widget/selectable_text.rs b/src/widget/selectable_text.rs new file mode 100644 index 00000000..824077c7 --- /dev/null +++ b/src/widget/selectable_text.rs @@ -0,0 +1,505 @@ +use crate::Renderer; +use crate::widget::menu::MenuBarState; + +use iced_core::event::Event; +use iced_core::text::LineHeight; +use iced_core::widget::Widget; +use iced_core::widget::text::{Alignment, Catalog, Ellipsize, Shaping, Style, Wrapping}; +use iced_core::widget::tree::{self, Tree}; +use iced_core::{ + Clipboard, Color, Layout, Length, Pixels, Rectangle, Shell, Size, Vector, alignment, mouse, + overlay, renderer, +}; +use std::borrow::Cow; + +type InnerText<'a> = iced_core::widget::text::Text<'a, crate::Theme, Renderer>; + +/// A selectable text widget with right-click context menu support. +/// +/// Wraps the plain [`Text`](crate::widget::Text) widget to add text selection +/// and a context menu (Copy / Select All), both **on by default**. Created +/// with [`selectable_text`]; the plain [`text`](crate::widget::text) +/// constructor and the typography presets return the unwrapped +/// [`Text`](crate::widget::Text), so non-selectable text keeps iced's looser +/// trait bounds. +/// +/// Use [`.selectable(false)`](Self::selectable) for plain, non-interactive +/// text, or [`.context_menu(false)`](Self::context_menu) to keep selection +/// without the menu. +pub struct SelectableText<'a> { + inner: InnerText<'a>, + selectable: bool, + context_menu: bool, +} + +impl<'a> SelectableText<'a> { + /// Creates a new [`SelectableText`] widget with the given content. + /// + /// Selection and the context menu are enabled by default. + pub fn new(content: impl iced_core::text::IntoFragment<'a>) -> Self { + Self { + inner: InnerText::new(content), + selectable: true, + context_menu: true, + } + } + + /// Controls whether the text is selectable. On by default. + /// + /// Pass `false` to render plain, non-interactive text (this also + /// suppresses the context menu, which has nothing to act on). + pub fn selectable(mut self, enabled: bool) -> Self { + self.selectable = enabled; + self + } + + /// Controls whether the right-click context menu is shown. On by default. + /// + /// Pass `false` to keep text selection but disable the context menu. + pub fn context_menu(mut self, enabled: bool) -> Self { + self.context_menu = enabled; + self + } + + /// Whether the wrapper should install its context-menu machinery: only + /// when the menu is enabled *and* the text is selectable (a menu over + /// non-selectable text would have nothing to act on). + fn shows_context_menu(&self) -> bool { + self.selectable && self.context_menu + } + + fn uses_popup_context_menu(&self) -> bool { + #[cfg(all(feature = "wayland", feature = "winit"))] + if matches!( + crate::app::cosmic::WINDOWING_SYSTEM.get(), + Some(crate::app::cosmic::WindowingSystem::Wayland) + ) { + return true; + } + false + } + + pub fn size(mut self, size: impl Into) -> Self { + self.inner = self.inner.size(size); + self + } + + pub fn line_height(mut self, line_height: impl Into) -> Self { + self.inner = self.inner.line_height(line_height); + self + } + + pub fn font(mut self, font: impl Into<::Font>) -> Self { + self.inner = self.inner.font(font); + self + } + + pub fn font_maybe( + mut self, + font: Option::Font>>, + ) -> Self { + self.inner = self.inner.font_maybe(font); + self + } + + pub fn width(mut self, width: impl Into) -> Self { + self.inner = self.inner.width(width); + self + } + + pub fn height(mut self, height: impl Into) -> Self { + self.inner = self.inner.height(height); + self + } + + pub fn center(mut self) -> Self { + self.inner = self.inner.center(); + self + } + + pub fn align_x(mut self, alignment: impl Into) -> Self { + self.inner = self.inner.align_x(alignment); + self + } + + pub fn align_y(mut self, alignment: impl Into) -> Self { + self.inner = self.inner.align_y(alignment); + self + } + + pub fn shaping(mut self, shaping: Shaping) -> Self { + self.inner = self.inner.shaping(shaping); + self + } + + pub fn wrapping(mut self, wrapping: Wrapping) -> Self { + self.inner = self.inner.wrapping(wrapping); + self + } + + pub fn ellipsize(mut self, ellipsize: Ellipsize) -> Self { + self.inner = self.inner.ellipsize(ellipsize); + self + } + + #[must_use] + pub fn style(mut self, style: impl Fn(&crate::Theme) -> Style + 'a) -> Self + where + ::Class<'a>: + From>, + { + self.inner = self.inner.style(style); + self + } + + pub fn color(mut self, color: impl Into) -> Self + where + ::Class<'a>: + From>, + { + self.inner = self.inner.color(color); + self + } + + pub fn color_maybe(mut self, color: Option>) -> Self + where + ::Class<'a>: + From>, + { + self.inner = self.inner.color_maybe(color); + self + } + + #[must_use] + pub fn class(mut self, class: impl Into<::Class<'a>>) -> Self { + self.inner = self.inner.class(class); + self + } +} + +impl<'a> From> for SelectableText<'a> { + fn from(inner: InnerText<'a>) -> Self { + Self { + inner, + selectable: true, + context_menu: true, + } + } +} + +impl<'a> From<&'a str> for SelectableText<'a> { + fn from(content: &'a str) -> Self { + Self::new(content) + } +} + +struct TextWrapperState { + menu_bar_state: MenuBarState, + pending_action: crate::widget::text_context_menu::PendingAction, +} + +fn w<'x, Message>(inner: &'x InnerText<'_>) -> &'x dyn Widget { + inner +} + +fn w_mut<'x, Message>( + inner: &'x mut InnerText<'_>, +) -> &'x mut dyn Widget { + inner +} + +impl<'a, Message: Clone + 'static> Widget for SelectableText<'a> { + fn tag(&self) -> tree::Tag { + if self.shows_context_menu() { + tree::Tag::of::() + } else { + w::(&self.inner).tag() + } + } + + fn state(&self) -> tree::State { + if self.shows_context_menu() { + tree::State::new(TextWrapperState { + menu_bar_state: MenuBarState::default(), + pending_action: crate::widget::text_context_menu::pending_action(), + }) + } else { + w::(&self.inner).state() + } + } + + fn children(&self) -> Vec { + if self.shows_context_menu() { + vec![Tree::new(w::(&self.inner))] + } else { + Vec::new() + } + } + + fn diff(&mut self, tree: &mut Tree) { + if self.shows_context_menu() { + if let Some(child) = tree.children.first_mut() { + child.diff(&mut self.inner as &mut dyn Widget); + } + } + } + + fn size(&self) -> Size { + w::(&self.inner).size() + } + + fn layout( + &mut self, + tree: &mut Tree, + renderer: &Renderer, + limits: &iced_core::layout::Limits, + ) -> iced_core::layout::Node { + let inner_tree = if self.shows_context_menu() { + &mut tree.children[0] + } else { + tree + }; + w_mut::(&mut self.inner).layout(inner_tree, renderer, limits) + } + + fn draw( + &self, + tree: &Tree, + renderer: &mut Renderer, + theme: &crate::Theme, + defaults: &renderer::Style, + layout: Layout<'_>, + cursor: mouse::Cursor, + viewport: &Rectangle, + ) { + let inner_tree = if self.shows_context_menu() { + &tree.children[0] + } else { + tree + }; + w::(&self.inner).draw( + inner_tree, renderer, theme, defaults, layout, cursor, viewport, + ); + + // draw a border when the text is focused + use iced_core::renderer::Renderer as RendererExt; + use iced_core::widget::text::State as TextState; + + let state = inner_tree + .state + .downcast_ref::::Paragraph>>(); + if state.is_keyboard_focused() { + let cosmic = theme.cosmic(); + let accent: Color = cosmic.accent.base.into(); + renderer.fill_quad( + renderer::Quad { + bounds: layout.bounds(), + border: iced_core::Border { + color: accent, + width: 1.0, + radius: cosmic.corner_radii.radius_s.into(), + }, + ..renderer::Quad::default() + }, + Color::TRANSPARENT, + ); + } + } + + fn update( + &mut self, + tree: &mut Tree, + event: &Event, + layout: Layout<'_>, + cursor: mouse::Cursor, + renderer: &Renderer, + clipboard: &mut dyn Clipboard, + shell: &mut Shell<'_, Message>, + viewport: &Rectangle, + ) { + if self.shows_context_menu() { + w_mut::(&mut self.inner).update( + &mut tree.children[0], + event, + layout, + cursor, + renderer, + clipboard, + shell, + viewport, + ); + + #[cfg(feature = "wayland")] + if self.uses_popup_context_menu() { + use iced_core::widget::text::HasSelectableText; + + if self + .inner + .context_menu_position(&tree.children[0]) + .is_some() + { + let selected_text = self.inner.selected_text(&tree.children[0]); + let has_selection = selected_text.is_some(); + let click_position = + self.inner.context_menu_position(&tree.children[0]).unwrap(); + let wrapper_state = tree.state.downcast_ref::(); + let menu_bar_state = wrapper_state.menu_bar_state.clone(); + let pending_action = wrapper_state.pending_action.clone(); + + crate::widget::text_context_menu::create_text_context_popup( + click_position, + selected_text, + false, + has_selection, + &menu_bar_state, + &pending_action, + renderer, + viewport, + cursor, + ); + + self.inner + .set_context_menu_position(&mut tree.children[0], None); + } + + { + let wrapper_state = tree.state.downcast_ref::(); + let pending_action = wrapper_state.pending_action.clone(); + if let Some(action) = + crate::widget::text_context_menu::take_pending_action(&pending_action) + { + match action { + crate::widget::text_context_menu::TextCtxAction::Copy => {} + crate::widget::text_context_menu::TextCtxAction::SelectAll => { + self.inner.select_all(&mut tree.children[0]); + } + _ => {} + } + } + } + + let wrapper_state = tree.state.downcast_ref::(); + let menu_bar_state = wrapper_state.menu_bar_state.clone(); + crate::widget::text_context_menu::dismiss_popup_on_event(&menu_bar_state, event); + } + } else { + w_mut::(&mut self.inner).update( + tree, event, layout, cursor, renderer, clipboard, shell, viewport, + ); + } + } + + fn mouse_interaction( + &self, + tree: &Tree, + layout: Layout<'_>, + cursor: mouse::Cursor, + viewport: &Rectangle, + renderer: &Renderer, + ) -> mouse::Interaction { + let inner_tree = if self.shows_context_menu() { + &tree.children[0] + } else { + tree + }; + w::(&self.inner).mouse_interaction(inner_tree, layout, cursor, viewport, renderer) + } + + fn overlay<'b>( + &'b mut self, + tree: &'b mut Tree, + _layout: Layout<'b>, + _renderer: &Renderer, + _viewport: &Rectangle, + translation: Vector, + ) -> Option> { + if self.shows_context_menu() && !self.uses_popup_context_menu() { + let menu_bar_state = tree + .state + .downcast_ref::() + .menu_bar_state + .clone(); + let inner_tree = &mut tree.children[0]; + crate::widget::text_context_menu::context_menu_overlay( + &self.inner, + inner_tree, + None, + translation, + menu_bar_state, + ) + } else { + None + } + } + + fn operate( + &mut self, + tree: &mut Tree, + layout: Layout<'_>, + renderer: &Renderer, + operation: &mut dyn iced_core::widget::Operation, + ) { + let inner_tree = if self.shows_context_menu() { + &mut tree.children[0] + } else { + tree + }; + w_mut::(&mut self.inner).operate(inner_tree, layout, renderer, operation); + } + + #[cfg(feature = "a11y")] + fn a11y_nodes( + &self, + layout: Layout<'_>, + state: &Tree, + cursor: mouse::Cursor, + ) -> iced_accessibility::A11yTree { + let inner_state = if self.shows_context_menu() { + &state.children[0] + } else { + state + }; + w::(&self.inner).a11y_nodes(layout, inner_state, cursor) + } + + fn id(&self) -> Option { + w::(&self.inner).id() + } + + fn set_id(&mut self, id: iced_core::widget::Id) { + w_mut::(&mut self.inner).set_id(id); + } +} + +impl<'a, Message: Clone + 'static> From> for crate::Element<'a, Message> { + fn from(text: SelectableText<'a>) -> Self { + let SelectableText { + inner, + selectable, + context_menu, + } = text; + // Apply selection to the inner widget here, at the last moment before + // it becomes part of the tree. iced's `Text::selectable()` is one-way + // (it can only set the flag), so deferring until conversion lets + // `selectable(false)` work without ever having set it. + let inner = if selectable { + inner.selectable() + } else { + inner + }; + Self::new(SelectableText { + inner, + selectable, + context_menu, + }) + } +} + +/// Creates a [`SelectableText`] widget: text the user can select, with a +/// right-click context menu (Copy / Select All). +/// +/// Use [`.context_menu(false)`](SelectableText::context_menu) to keep +/// selection but suppress the menu. +pub fn selectable_text<'a>(content: impl Into> + 'a) -> SelectableText<'a> { + SelectableText::new(content.into()).font(crate::font::default()) +} diff --git a/src/widget/settings/item.rs b/src/widget/settings/item.rs index af4b17ab..4eb5bb17 100644 --- a/src/widget/settings/item.rs +++ b/src/widget/settings/item.rs @@ -14,12 +14,12 @@ use taffy::AlignContent; /// A settings item aligned in a row #[must_use] #[allow(clippy::module_name_repetitions)] -pub fn item<'a, Message: Clone + 'static>( +pub fn item<'a, Message: 'static>( title: impl Into> + 'a, widget: impl Into> + 'a, ) -> Row<'a, Message, Theme> { #[inline(never)] - fn inner<'a, Message: Clone + 'static>( + fn inner<'a, Message: 'static>( title: Cow<'a, str>, widget: Element<'a, Message>, ) -> Row<'a, Message, Theme> { @@ -45,12 +45,12 @@ pub fn item_row(children: Vec>) -> Row /// A settings item aligned in a flex row #[allow(clippy::module_name_repetitions)] -pub fn flex_item<'a, Message: Clone + 'static>( +pub fn flex_item<'a, Message: 'static>( title: impl Into> + 'a, widget: impl Into> + 'a, ) -> FlexRow<'a, Message> { #[inline(never)] - fn inner<'a, Message: Clone + 'static>( + fn inner<'a, Message: 'static>( title: Cow<'a, str>, widget: Element<'a, Message>, ) -> FlexRow<'a, Message> { diff --git a/src/widget/text.rs b/src/widget/text.rs index 5c490d4d..37e85b80 100644 --- a/src/widget/text.rs +++ b/src/widget/text.rs @@ -1,460 +1,13 @@ use crate::Renderer; -use crate::widget::menu::MenuBarState; - -use iced_core::event::Event; +pub use iced::widget::Text; use iced_core::text::LineHeight; -use iced_core::widget::Widget; -use iced_core::widget::text::{Alignment, Catalog, Ellipsize, Shaping, Style, Wrapping}; -use iced_core::widget::tree::{self, Tree}; -use iced_core::{ - Clipboard, Color, Layout, Length, Pixels, Rectangle, Shell, Size, Vector, alignment, mouse, - overlay, renderer, -}; use std::borrow::Cow; -type InnerText<'a> = iced_core::widget::text::Text<'a, crate::Theme, Renderer>; - -/// A text widget with optional selection and context menu support. +/// Creates a new [`Text`] widget with the provided content. /// -/// By default, text is non-selectable. Call [`.selectable()`](Text::selectable) -/// to enable text selection and a right-click context menu with Copy and -/// Select All. -pub struct Text<'a> { - inner: InnerText<'a>, - context_menu: bool, -} - -impl<'a> Text<'a> { - /// Creates a new [`Text`] widget with the given content. - pub fn new(content: impl iced_core::text::IntoFragment<'a>) -> Self { - Self { - inner: InnerText::new(content), - context_menu: false, - } - } - - /// Enables text selection and a right-click context menu. - pub fn selectable(mut self) -> Self { - self.inner = self.inner.selectable(); - self.context_menu = true; - self - } - - /// Controls whether the right-click context menu is shown. - /// - /// Only meaningful after calling [`.selectable()`](Self::selectable). - /// Pass `false` to keep text selection but disable the context menu. - pub fn context_menu(mut self, enabled: bool) -> Self { - self.context_menu = enabled; - self - } - - fn uses_popup_context_menu(&self) -> bool { - #[cfg(all(feature = "wayland", feature = "winit"))] - if matches!( - crate::app::cosmic::WINDOWING_SYSTEM.get(), - Some(crate::app::cosmic::WindowingSystem::Wayland) - ) { - return true; - } - false - } - - pub fn size(mut self, size: impl Into) -> Self { - self.inner = self.inner.size(size); - self - } - - pub fn line_height(mut self, line_height: impl Into) -> Self { - self.inner = self.inner.line_height(line_height); - self - } - - pub fn font(mut self, font: impl Into<::Font>) -> Self { - self.inner = self.inner.font(font); - self - } - - pub fn font_maybe( - mut self, - font: Option::Font>>, - ) -> Self { - self.inner = self.inner.font_maybe(font); - self - } - - pub fn width(mut self, width: impl Into) -> Self { - self.inner = self.inner.width(width); - self - } - - pub fn height(mut self, height: impl Into) -> Self { - self.inner = self.inner.height(height); - self - } - - pub fn center(mut self) -> Self { - self.inner = self.inner.center(); - self - } - - pub fn align_x(mut self, alignment: impl Into) -> Self { - self.inner = self.inner.align_x(alignment); - self - } - - pub fn align_y(mut self, alignment: impl Into) -> Self { - self.inner = self.inner.align_y(alignment); - self - } - - pub fn shaping(mut self, shaping: Shaping) -> Self { - self.inner = self.inner.shaping(shaping); - self - } - - pub fn wrapping(mut self, wrapping: Wrapping) -> Self { - self.inner = self.inner.wrapping(wrapping); - self - } - - pub fn ellipsize(mut self, ellipsize: Ellipsize) -> Self { - self.inner = self.inner.ellipsize(ellipsize); - self - } - - #[must_use] - pub fn style(mut self, style: impl Fn(&crate::Theme) -> Style + 'a) -> Self - where - ::Class<'a>: - From>, - { - self.inner = self.inner.style(style); - self - } - - pub fn color(mut self, color: impl Into) -> Self - where - ::Class<'a>: - From>, - { - self.inner = self.inner.color(color); - self - } - - pub fn color_maybe(mut self, color: Option>) -> Self - where - ::Class<'a>: - From>, - { - self.inner = self.inner.color_maybe(color); - self - } - - #[must_use] - pub fn class(mut self, class: impl Into<::Class<'a>>) -> Self { - self.inner = self.inner.class(class); - self - } -} - -impl<'a> From> for Text<'a> { - fn from(inner: InnerText<'a>) -> Self { - Self { - inner, - context_menu: false, - } - } -} - -impl<'a> From<&'a str> for Text<'a> { - fn from(content: &'a str) -> Self { - Self::new(content) - } -} - -struct TextWrapperState { - menu_bar_state: MenuBarState, - pending_action: crate::widget::text_context_menu::PendingAction, -} - -fn w<'x, Message>(inner: &'x InnerText<'_>) -> &'x dyn Widget { - inner -} - -fn w_mut<'x, Message>( - inner: &'x mut InnerText<'_>, -) -> &'x mut dyn Widget { - inner -} - -impl<'a, Message: Clone + 'static> Widget for Text<'a> { - fn tag(&self) -> tree::Tag { - if self.context_menu { - tree::Tag::of::() - } else { - w::(&self.inner).tag() - } - } - - fn state(&self) -> tree::State { - if self.context_menu { - tree::State::new(TextWrapperState { - menu_bar_state: MenuBarState::default(), - pending_action: crate::widget::text_context_menu::pending_action(), - }) - } else { - w::(&self.inner).state() - } - } - - fn children(&self) -> Vec { - if self.context_menu { - vec![Tree::new(w::(&self.inner))] - } else { - Vec::new() - } - } - - fn diff(&mut self, tree: &mut Tree) { - if self.context_menu { - if let Some(child) = tree.children.first_mut() { - child.diff(&mut self.inner as &mut dyn Widget); - } - } - } - - fn size(&self) -> Size { - w::(&self.inner).size() - } - - fn layout( - &mut self, - tree: &mut Tree, - renderer: &Renderer, - limits: &iced_core::layout::Limits, - ) -> iced_core::layout::Node { - let inner_tree = if self.context_menu { - &mut tree.children[0] - } else { - tree - }; - w_mut::(&mut self.inner).layout(inner_tree, renderer, limits) - } - - fn draw( - &self, - tree: &Tree, - renderer: &mut Renderer, - theme: &crate::Theme, - defaults: &renderer::Style, - layout: Layout<'_>, - cursor: mouse::Cursor, - viewport: &Rectangle, - ) { - let inner_tree = if self.context_menu { - &tree.children[0] - } else { - tree - }; - w::(&self.inner).draw( - inner_tree, renderer, theme, defaults, layout, cursor, viewport, - ); - - // draw a border when the text is focused - use iced_core::renderer::Renderer as RendererExt; - use iced_core::widget::text::State as TextState; - - let state = inner_tree - .state - .downcast_ref::::Paragraph>>(); - if state.is_keyboard_focused() { - let cosmic = theme.cosmic(); - let accent: Color = cosmic.accent.base.into(); - renderer.fill_quad( - renderer::Quad { - bounds: layout.bounds(), - border: iced_core::Border { - color: accent, - width: 1.0, - radius: cosmic.corner_radii.radius_s.into(), - }, - ..renderer::Quad::default() - }, - Color::TRANSPARENT, - ); - } - } - - fn update( - &mut self, - tree: &mut Tree, - event: &Event, - layout: Layout<'_>, - cursor: mouse::Cursor, - renderer: &Renderer, - clipboard: &mut dyn Clipboard, - shell: &mut Shell<'_, Message>, - viewport: &Rectangle, - ) { - if self.context_menu { - w_mut::(&mut self.inner).update( - &mut tree.children[0], - event, - layout, - cursor, - renderer, - clipboard, - shell, - viewport, - ); - - #[cfg(feature = "wayland")] - if self.uses_popup_context_menu() { - use iced_core::widget::text::HasSelectableText; - - if self - .inner - .context_menu_position(&tree.children[0]) - .is_some() - { - let selected_text = self.inner.selected_text(&tree.children[0]); - let has_selection = selected_text.is_some(); - let click_position = - self.inner.context_menu_position(&tree.children[0]).unwrap(); - let wrapper_state = tree.state.downcast_ref::(); - let menu_bar_state = wrapper_state.menu_bar_state.clone(); - let pending_action = wrapper_state.pending_action.clone(); - - crate::widget::text_context_menu::create_text_context_popup( - click_position, - selected_text, - false, - has_selection, - &menu_bar_state, - &pending_action, - renderer, - viewport, - cursor, - ); - - self.inner - .set_context_menu_position(&mut tree.children[0], None); - } - - { - let wrapper_state = tree.state.downcast_ref::(); - let pending_action = wrapper_state.pending_action.clone(); - if let Some(action) = - crate::widget::text_context_menu::take_pending_action(&pending_action) - { - match action { - crate::widget::text_context_menu::TextCtxAction::Copy => {} - crate::widget::text_context_menu::TextCtxAction::SelectAll => { - self.inner.select_all(&mut tree.children[0]); - } - _ => {} - } - } - } - - let wrapper_state = tree.state.downcast_ref::(); - let menu_bar_state = wrapper_state.menu_bar_state.clone(); - crate::widget::text_context_menu::dismiss_popup_on_event(&menu_bar_state, event); - } - } else { - w_mut::(&mut self.inner).update( - tree, event, layout, cursor, renderer, clipboard, shell, viewport, - ); - } - } - - fn mouse_interaction( - &self, - tree: &Tree, - layout: Layout<'_>, - cursor: mouse::Cursor, - viewport: &Rectangle, - renderer: &Renderer, - ) -> mouse::Interaction { - let inner_tree = if self.context_menu { - &tree.children[0] - } else { - tree - }; - w::(&self.inner).mouse_interaction(inner_tree, layout, cursor, viewport, renderer) - } - - fn overlay<'b>( - &'b mut self, - tree: &'b mut Tree, - _layout: Layout<'b>, - _renderer: &Renderer, - _viewport: &Rectangle, - translation: Vector, - ) -> Option> { - if self.context_menu && !self.uses_popup_context_menu() { - let menu_bar_state = tree - .state - .downcast_ref::() - .menu_bar_state - .clone(); - let inner_tree = &mut tree.children[0]; - crate::widget::text_context_menu::context_menu_overlay( - &self.inner, - inner_tree, - None, - translation, - menu_bar_state, - ) - } else { - None - } - } - - fn operate( - &mut self, - tree: &mut Tree, - layout: Layout<'_>, - renderer: &Renderer, - operation: &mut dyn iced_core::widget::Operation, - ) { - let inner_tree = if self.context_menu { - &mut tree.children[0] - } else { - tree - }; - w_mut::(&mut self.inner).operate(inner_tree, layout, renderer, operation); - } - - #[cfg(feature = "a11y")] - fn a11y_nodes( - &self, - layout: Layout<'_>, - state: &Tree, - cursor: mouse::Cursor, - ) -> iced_accessibility::A11yTree { - let inner_state = if self.context_menu { - &state.children[0] - } else { - state - }; - w::(&self.inner).a11y_nodes(layout, inner_state, cursor) - } - - fn id(&self) -> Option { - w::(&self.inner).id() - } - - fn set_id(&mut self, id: iced_core::widget::Id) { - w_mut::(&mut self.inner).set_id(id); - } -} - -impl<'a, Message: Clone + 'static> From> for crate::Element<'a, Message> { - fn from(text: Text<'a>) -> Self { - Self::new(text) - } +/// [`Text`]: widget::Text +pub fn text<'a>(text: impl Into> + 'a) -> Text<'a, crate::Theme, Renderer> { + Text::new(text.into()).font(crate::font::default()) } /// Available presets for text typography @@ -471,115 +24,119 @@ pub enum Typography { Title4, } -/// Creates a new [`Text`] widget with the provided content. -pub fn text<'a>(content: impl Into> + 'a) -> Text<'a> { - Text::new(content.into()).font(crate::font::default()) -} - /// [`Text`] widget with the Title 1 typography preset. -pub fn title1<'a>(content: impl Into> + 'a) -> Text<'a> { +pub fn title1<'a>(text: impl Into> + 'a) -> Text<'a, crate::Theme, Renderer> { #[inline(never)] - fn inner(text: Cow) -> Text { + fn inner(text: Cow) -> Text { Text::new(text) .size(35.0) .line_height(LineHeight::Absolute(52.0.into())) .font(crate::font::semibold()) } - inner(content.into()) + + inner(text.into()) } /// [`Text`] widget with the Title 2 typography preset. -pub fn title2<'a>(content: impl Into> + 'a) -> Text<'a> { +pub fn title2<'a>(text: impl Into> + 'a) -> Text<'a, crate::Theme, Renderer> { #[inline(never)] - fn inner(text: Cow) -> Text { + fn inner(text: Cow) -> Text { Text::new(text) .size(29.0) .line_height(LineHeight::Absolute(43.0.into())) .font(crate::font::semibold()) } - inner(content.into()) + + inner(text.into()) } /// [`Text`] widget with the Title 3 typography preset. -pub fn title3<'a>(content: impl Into> + 'a) -> Text<'a> { +pub fn title3<'a>(text: impl Into> + 'a) -> Text<'a, crate::Theme, Renderer> { #[inline(never)] - fn inner(text: Cow) -> Text { + fn inner(text: Cow) -> Text { Text::new(text) .size(24.0) .line_height(LineHeight::Absolute(36.0.into())) .font(crate::font::bold()) } - inner(content.into()) + + inner(text.into()) } /// [`Text`] widget with the Title 4 typography preset. -pub fn title4<'a>(content: impl Into> + 'a) -> Text<'a> { +pub fn title4<'a>(text: impl Into> + 'a) -> Text<'a, crate::Theme, Renderer> { #[inline(never)] - fn inner(text: Cow) -> Text { + fn inner(text: Cow) -> Text { Text::new(text) .size(20.0) .line_height(LineHeight::Absolute(30.0.into())) .font(crate::font::bold()) } - inner(content.into()) + + inner(text.into()) } /// [`Text`] widget with the Heading typography preset. -pub fn heading<'a>(content: impl Into> + 'a) -> Text<'a> { +pub fn heading<'a>(text: impl Into> + 'a) -> Text<'a, crate::Theme, Renderer> { #[inline(never)] - fn inner(text: Cow) -> Text { + fn inner(text: Cow) -> Text { Text::new(text) .size(14.0) .line_height(LineHeight::Absolute(iced::Pixels(21.0))) .font(crate::font::bold()) } - inner(content.into()) + + inner(text.into()) } /// [`Text`] widget with the Caption Heading typography preset. -pub fn caption_heading<'a>(content: impl Into> + 'a) -> Text<'a> { +pub fn caption_heading<'a>(text: impl Into> + 'a) -> Text<'a, crate::Theme, Renderer> { #[inline(never)] - fn inner(text: Cow) -> Text { + fn inner(text: Cow) -> Text { Text::new(text) .size(12.0) .line_height(LineHeight::Absolute(iced::Pixels(17.0))) .font(crate::font::semibold()) } - inner(content.into()) + + inner(text.into()) } /// [`Text`] widget with the Body typography preset. -pub fn body<'a>(content: impl Into> + 'a) -> Text<'a> { +pub fn body<'a>(text: impl Into> + 'a) -> Text<'a, crate::Theme, Renderer> { #[inline(never)] - fn inner(text: Cow) -> Text { + fn inner(text: Cow) -> Text { Text::new(text) .size(14.0) .line_height(LineHeight::Absolute(21.0.into())) .font(crate::font::default()) } - inner(content.into()) + + inner(text.into()) } /// [`Text`] widget with the Caption typography preset. -pub fn caption<'a>(content: impl Into> + 'a) -> Text<'a> { +pub fn caption<'a>(text: impl Into> + 'a) -> Text<'a, crate::Theme, Renderer> { #[inline(never)] - fn inner(text: Cow) -> Text { + fn inner(text: Cow) -> Text { Text::new(text) .size(12.0) .line_height(LineHeight::Absolute(17.0.into())) .font(crate::font::default()) } - inner(content.into()) + + inner(text.into()) } /// [`Text`] widget with the Monotext typography preset. -pub fn monotext<'a>(content: impl Into> + 'a) -> Text<'a> { +pub fn monotext<'a>(text: impl Into> + 'a) -> Text<'a, crate::Theme, Renderer> { #[inline(never)] - fn inner(text: Cow) -> Text { + fn inner(text: Cow) -> Text { Text::new(text) .size(14.0) .line_height(LineHeight::Absolute(20.0.into())) .font(crate::font::mono()) } - inner(content.into()) + + inner(text.into()) }