diff --git a/src/app/cosmic.rs b/src/app/cosmic.rs index d1fffa33..ce8a4154 100644 --- a/src/app/cosmic.rs +++ b/src/app/cosmic.rs @@ -743,6 +743,7 @@ where #[cfg(feature = "multi-window")] pub fn view(&self, id: window::Id) -> Element<'_, crate::Action> { + crate::widget::text_context_menu::set_current_window_id(id); #[cfg(all(wayland_platform, target_os = "linux"))] if let Some((_, _, _, Some(v))) = self.surface_views.get(&id) { return v(&self.app); @@ -756,8 +757,6 @@ where return self.app.view_window(id).map(crate::Action::App); } - crate::widget::text_context_menu::set_current_window_id(id); - let view = if self.app.core().window.use_template { self.app.view_main() } else { diff --git a/src/widget/selectable_text.rs b/src/widget/selectable_text.rs index 4b47cfd4..1b65d774 100644 --- a/src/widget/selectable_text.rs +++ b/src/widget/selectable_text.rs @@ -1,6 +1,7 @@ use crate::Renderer; use crate::widget::menu::MenuBarState; +use iced::window; use iced_core::event::Event; use iced_core::text::LineHeight; use iced_core::widget::Widget; @@ -30,6 +31,7 @@ pub struct SelectableText<'a> { inner: InnerText<'a>, selectable: bool, context_menu: bool, + window_id: window::Id, } impl<'a> SelectableText<'a> { @@ -41,6 +43,7 @@ impl<'a> SelectableText<'a> { inner: InnerText::new(content), selectable: true, context_menu: true, + window_id: crate::widget::text_context_menu::current_window_id(), } } @@ -183,6 +186,7 @@ impl<'a> From> for SelectableText<'a> { inner, selectable: true, context_menu: true, + window_id: crate::widget::text_context_menu::current_window_id(), } } } @@ -356,6 +360,7 @@ impl<'a, Message: Clone + 'static> Widget for S renderer, viewport, cursor, + self.window_id, ); self.inner @@ -380,7 +385,11 @@ impl<'a, Message: Clone + 'static> Widget for S 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); + crate::widget::text_context_menu::dismiss_popup_on_event( + &menu_bar_state, + event, + self.window_id, + ); } } else { w_mut::(&mut self.inner).update( @@ -477,6 +486,7 @@ impl<'a, Message: Clone + 'static> From> for crate::Element<' inner, selectable, context_menu, + window_id, } = 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 @@ -491,6 +501,7 @@ impl<'a, Message: Clone + 'static> From> for crate::Element<' inner, selectable, context_menu, + window_id, }) } } diff --git a/src/widget/text_context_menu.rs b/src/widget/text_context_menu.rs index 11df26d7..c156d869 100644 --- a/src/widget/text_context_menu.rs +++ b/src/widget/text_context_menu.rs @@ -16,6 +16,8 @@ pub use iced_core::widget::text::HasSelectableText; #[cfg(wayland_platform)] +use iced_core::window; +#[cfg(wayland_platform)] use iced_runtime::platform_specific::wayland::CornerRadius; #[cfg(wayland_platform)] @@ -486,10 +488,10 @@ pub(crate) fn create_text_context_popup( renderer: &crate::Renderer, viewport: &Rectangle, cursor: mouse::Cursor, + window_id: window::Id, ) { use iced_runtime::platform_specific::wayland::popup::{SctkPopupSettings, SctkPositioner}; - let window_id = current_window_id(); if window_id == iced_core::window::Id::NONE { return; } @@ -625,7 +627,11 @@ pub(crate) fn create_text_context_popup( /// Dismisses this widget's open context-menu popup on an outside click, /// touch, or Escape. #[cfg(wayland_platform)] -pub(crate) fn dismiss_popup_on_event(menu_bar_state: &MenuBarState, event: &event::Event) { +pub(crate) fn dismiss_popup_on_event( + menu_bar_state: &MenuBarState, + event: &event::Event, + window_id: window::Id, +) { let is_dismiss = matches!( event, event::Event::Mouse(mouse::Event::ButtonPressed( @@ -639,7 +645,6 @@ pub(crate) fn dismiss_popup_on_event(menu_bar_state: &MenuBarState, event: &even return; } - let window_id = current_window_id(); let popup_id = menu_bar_state .inner .with_data(|state| state.popup_id.get(&window_id).copied()); diff --git a/src/widget/text_editor.rs b/src/widget/text_editor.rs index 9c617363..f25cb277 100644 --- a/src/widget/text_editor.rs +++ b/src/widget/text_editor.rs @@ -15,7 +15,7 @@ use iced_core::text::highlighter; use iced_core::widget::Widget; use iced_core::widget::tree::{self, Tree}; use iced_core::{ - Clipboard, Layout, Length, Rectangle, Shell, Size, Vector, mouse, overlay, renderer, + Clipboard, Layout, Length, Rectangle, Shell, Size, Vector, mouse, overlay, renderer, window, }; type InnerEditor<'a, Message> = @@ -24,6 +24,7 @@ type InnerEditor<'a, Message> = pub struct TextEditor<'a, Message> { inner: InnerEditor<'a, Message>, has_context_menu: bool, + window_id: window::Id, } struct EditorWrapperState { @@ -37,6 +38,7 @@ impl<'a, Message: Clone + 'static> TextEditor<'a, Message> { Self { inner: iced_widget::text_editor(content), has_context_menu: true, + window_id: crate::widget::text_context_menu::current_window_id(), } } @@ -300,6 +302,7 @@ impl<'a, Message: Clone + 'static> Widget Widget(); let menu_bar_state = wrapper_state.menu_bar_state.clone(); - crate::widget::text_context_menu::dismiss_popup_on_event(&menu_bar_state, event); + crate::widget::text_context_menu::dismiss_popup_on_event( + &menu_bar_state, + event, + self.window_id, + ); } } else { ew_mut::(&mut self.inner).update( diff --git a/src/widget/text_input/input.rs b/src/widget/text_input/input.rs index d970e435..37db1756 100644 --- a/src/widget/text_input/input.rs +++ b/src/widget/text_input/input.rs @@ -218,6 +218,7 @@ pub struct TextInput<'a, Message> { /// The text input tracks and manages the input value in its state. manage_value: bool, drag_threshold: f32, + window_id: window::Id, } impl<'a, Message> TextInput<'a, Message> @@ -268,6 +269,7 @@ where always_active: false, manage_value: false, drag_threshold: 20.0, + window_id: crate::widget::text_context_menu::current_window_id(), } } @@ -922,7 +924,11 @@ where #[cfg(all(wayland_platform, feature = "winit"))] if self.uses_popup_context_menu() { let menu_bar_state = tree.state.downcast_ref::().menu_bar_state.clone(); - crate::widget::text_context_menu::dismiss_popup_on_event(&menu_bar_state, event); + crate::widget::text_context_menu::dismiss_popup_on_event( + &menu_bar_state, + event, + self.window_id, + ); } let text_layout = self.text_layout(layout); @@ -1062,6 +1068,7 @@ where renderer, viewport, cursor_position, + self.window_id, ); let state = tree.state.downcast_mut::();