Skip to content

Commit 5a21f0e

Browse files
committed
Add .id method and text_editor::focus task
1 parent 55da2a5 commit 5a21f0e

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

examples/editor/src/main.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use iced::highlighter;
22
use iced::keyboard;
33
use iced::widget::{
4-
button, center_x, column, container, operation, pick_list, row, space,
5-
text, text_editor, toggler, tooltip,
4+
self, button, center_x, column, container, operation::focus, pick_list,
5+
row, space, text, text_editor, toggler, tooltip,
66
};
77
use iced::{Center, Element, Fill, Font, Task, Theme};
88

@@ -20,6 +20,7 @@ pub fn main() -> iced::Result {
2020
}
2121

2222
struct Editor {
23+
editor_id: widget::Id,
2324
file: Option<PathBuf>,
2425
content: text_editor::Content,
2526
theme: highlighter::Theme,
@@ -42,8 +43,10 @@ enum Message {
4243

4344
impl Editor {
4445
fn new() -> (Self, Task<Message>) {
46+
let id = widget::Id::unique();
4547
(
4648
Self {
49+
editor_id: id.clone(),
4750
file: None,
4851
content: text_editor::Content::new(),
4952
theme: highlighter::Theme::SolarizedDark,
@@ -59,7 +62,7 @@ impl Editor {
5962
)),
6063
Message::FileOpened,
6164
),
62-
operation::focus_next(),
65+
focus(id),
6366
]),
6467
)
6568
}
@@ -196,6 +199,7 @@ impl Editor {
196199
column![
197200
controls,
198201
text_editor(&self.content)
202+
.id(self.editor_id.clone())
199203
.height(Fill)
200204
.on_action(Message::ActionPerformed)
201205
.wrapping(if self.word_wrap {

widget/src/text_editor.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ pub struct TextEditor<
104104
Theme: Catalog,
105105
Renderer: text::Renderer,
106106
{
107+
id: Option<widget::Id>,
107108
content: &'a Content<Renderer>,
108109
placeholder: Option<text::Fragment<'a>>,
109110
font: Option<Renderer::Font>,
@@ -135,6 +136,7 @@ where
135136
/// Creates new [`TextEditor`] with the given [`Content`].
136137
pub fn new(content: &'a Content<Renderer>) -> Self {
137138
Self {
139+
id: None,
138140
content,
139141
placeholder: None,
140142
font: None,
@@ -156,6 +158,12 @@ where
156158
last_status: None,
157159
}
158160
}
161+
162+
/// Sets the [`Id`](widget::Id) of the [`TextEditor`].
163+
pub fn id(mut self, id: impl Into<widget::Id>) -> Self {
164+
self.id = Some(id.into());
165+
self
166+
}
159167
}
160168

161169
impl<'a, Highlighter, Message, Theme, Renderer>
@@ -275,6 +283,7 @@ where
275283
) -> highlighter::Format<Renderer::Font>,
276284
) -> TextEditor<'a, H, Message, Theme, Renderer> {
277285
TextEditor {
286+
id: self.id,
278287
content: self.content,
279288
placeholder: self.placeholder,
280289
font: self.font,
@@ -1057,7 +1066,7 @@ where
10571066
) {
10581067
let state = tree.state.downcast_mut::<State<Highlighter>>();
10591068

1060-
operation.focusable(None, layout.bounds(), state);
1069+
operation.focusable(self.id.as_ref(), layout.bounds(), state);
10611070
}
10621071
}
10631072

0 commit comments

Comments
 (0)