From 0024fc0eb163aeb4d8995629793fdd94d1d4a6a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Tue, 28 Oct 2025 18:06:33 +0100 Subject: [PATCH 1/2] Add `.id` method and `text_editor::focus` task --- examples/editor/src/main.rs | 10 +++++++--- widget/src/text_editor.rs | 11 ++++++++++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/examples/editor/src/main.rs b/examples/editor/src/main.rs index b854275f..f282e487 100644 --- a/examples/editor/src/main.rs +++ b/examples/editor/src/main.rs @@ -1,8 +1,8 @@ use iced::highlighter; use iced::keyboard; use iced::widget::{ - button, center_x, column, container, operation, pick_list, row, space, - text, text_editor, toggler, tooltip, + self, button, center_x, column, container, operation::focus, pick_list, + row, space, text, text_editor, toggler, tooltip, }; use iced::{Center, Element, Fill, Font, Task, Theme}; @@ -20,6 +20,7 @@ pub fn main() -> iced::Result { } struct Editor { + editor_id: widget::Id, file: Option, content: text_editor::Content, theme: highlighter::Theme, @@ -42,8 +43,10 @@ enum Message { impl Editor { fn new() -> (Self, Task) { + let id = widget::Id::unique(); ( Self { + editor_id: id.clone(), file: None, content: text_editor::Content::new(), theme: highlighter::Theme::SolarizedDark, @@ -59,7 +62,7 @@ impl Editor { )), Message::FileOpened, ), - operation::focus_next(), + focus(id), ]), ) } @@ -196,6 +199,7 @@ impl Editor { column![ controls, text_editor(&self.content) + .id(self.editor_id.clone()) .height(Fill) .on_action(Message::ActionPerformed) .wrapping(if self.word_wrap { diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs index 8ab40311..6141ee59 100644 --- a/widget/src/text_editor.rs +++ b/widget/src/text_editor.rs @@ -104,6 +104,7 @@ pub struct TextEditor< Theme: Catalog, Renderer: text::Renderer, { + id: Option, content: &'a Content, placeholder: Option>, font: Option, @@ -135,6 +136,7 @@ where /// Creates new [`TextEditor`] with the given [`Content`]. pub fn new(content: &'a Content) -> Self { Self { + id: None, content, placeholder: None, font: None, @@ -156,6 +158,12 @@ where last_status: None, } } + + /// Sets the [`Id`](widget::Id) of the [`TextEditor`]. + pub fn id(mut self, id: impl Into) -> Self { + self.id = Some(id.into()); + self + } } impl<'a, Highlighter, Message, Theme, Renderer> @@ -275,6 +283,7 @@ where ) -> highlighter::Format, ) -> TextEditor<'a, H, Message, Theme, Renderer> { TextEditor { + id: self.id, content: self.content, placeholder: self.placeholder, font: self.font, @@ -1057,7 +1066,7 @@ where ) { let state = tree.state.downcast_mut::>(); - operation.focusable(None, layout.bounds(), state); + operation.focusable(self.id.as_ref(), layout.bounds(), state); } } From d0df7c689da3e52167a50e61aa1d6859771e08ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Sat, 15 Nov 2025 22:46:05 +0100 Subject: [PATCH 2/2] Simplify `editor` example a bit --- examples/editor/src/main.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/examples/editor/src/main.rs b/examples/editor/src/main.rs index f282e487..a40f6e9c 100644 --- a/examples/editor/src/main.rs +++ b/examples/editor/src/main.rs @@ -1,8 +1,8 @@ use iced::highlighter; use iced::keyboard; use iced::widget::{ - self, button, center_x, column, container, operation::focus, pick_list, - row, space, text, text_editor, toggler, tooltip, + button, center_x, column, container, operation, pick_list, row, space, + text, text_editor, toggler, tooltip, }; use iced::{Center, Element, Fill, Font, Task, Theme}; @@ -20,7 +20,6 @@ pub fn main() -> iced::Result { } struct Editor { - editor_id: widget::Id, file: Option, content: text_editor::Content, theme: highlighter::Theme, @@ -43,10 +42,8 @@ enum Message { impl Editor { fn new() -> (Self, Task) { - let id = widget::Id::unique(); ( Self { - editor_id: id.clone(), file: None, content: text_editor::Content::new(), theme: highlighter::Theme::SolarizedDark, @@ -62,7 +59,7 @@ impl Editor { )), Message::FileOpened, ), - focus(id), + operation::focus(EDITOR), ]), ) } @@ -199,7 +196,7 @@ impl Editor { column![ controls, text_editor(&self.content) - .id(self.editor_id.clone()) + .id(EDITOR) .height(Fill) .on_action(Message::ActionPerformed) .wrapping(if self.word_wrap { @@ -335,3 +332,5 @@ fn icon<'a, Message>(codepoint: char) -> Element<'a, Message> { .shaping(text::Shaping::Basic) .into() } + +const EDITOR: &str = "editor";