Add .id method and text_editor::focus task

This commit is contained in:
Sosthène Guédon 2025-10-28 18:06:33 +01:00 • committed by Héctor Ramón Jiménez
parent 41d930bd00
commit 0024fc0eb1
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
2 changed files with 17 additions and 4 deletions

View file

@ -1,8 +1,8 @@
use iced::highlighter; use iced::highlighter;
use iced::keyboard; use iced::keyboard;
use iced::widget::{ use iced::widget::{
button, center_x, column, container, operation, pick_list, row, space, self, button, center_x, column, container, operation::focus, pick_list,
text, text_editor, toggler, tooltip, row, space, text, text_editor, toggler, tooltip,
}; };
use iced::{Center, Element, Fill, Font, Task, Theme}; use iced::{Center, Element, Fill, Font, Task, Theme};
@ -20,6 +20,7 @@ pub fn main() -> iced::Result {
} }
struct Editor { struct Editor {
editor_id: widget::Id,
file: Option<PathBuf>, file: Option<PathBuf>,
content: text_editor::Content, content: text_editor::Content,
theme: highlighter::Theme, theme: highlighter::Theme,
@ -42,8 +43,10 @@ enum Message {
impl Editor { impl Editor {
fn new() -> (Self, Task<Message>) { fn new() -> (Self, Task<Message>) {
let id = widget::Id::unique();
( (
Self { Self {
editor_id: id.clone(),
file: None, file: None,
content: text_editor::Content::new(), content: text_editor::Content::new(),
theme: highlighter::Theme::SolarizedDark, theme: highlighter::Theme::SolarizedDark,
@ -59,7 +62,7 @@ impl Editor {
)), )),
Message::FileOpened, Message::FileOpened,
), ),
operation::focus_next(), focus(id),
]), ]),
) )
} }
@ -196,6 +199,7 @@ impl Editor {
column![ column![
controls, controls,
text_editor(&self.content) text_editor(&self.content)
.id(self.editor_id.clone())
.height(Fill) .height(Fill)
.on_action(Message::ActionPerformed) .on_action(Message::ActionPerformed)
.wrapping(if self.word_wrap { .wrapping(if self.word_wrap {

View file

@ -104,6 +104,7 @@ pub struct TextEditor<
Theme: Catalog, Theme: Catalog,
Renderer: text::Renderer, Renderer: text::Renderer,
{ {
id: Option<widget::Id>,
content: &'a Content<Renderer>, content: &'a Content<Renderer>,
placeholder: Option<text::Fragment<'a>>, placeholder: Option<text::Fragment<'a>>,
font: Option<Renderer::Font>, font: Option<Renderer::Font>,
@ -135,6 +136,7 @@ where
/// Creates new [`TextEditor`] with the given [`Content`]. /// Creates new [`TextEditor`] with the given [`Content`].
pub fn new(content: &'a Content<Renderer>) -> Self { pub fn new(content: &'a Content<Renderer>) -> Self {
Self { Self {
id: None,
content, content,
placeholder: None, placeholder: None,
font: None, font: None,
@ -156,6 +158,12 @@ where
last_status: None, last_status: None,
} }
} }
/// Sets the [`Id`](widget::Id) of the [`TextEditor`].
pub fn id(mut self, id: impl Into<widget::Id>) -> Self {
self.id = Some(id.into());
self
}
} }
impl<'a, Highlighter, Message, Theme, Renderer> impl<'a, Highlighter, Message, Theme, Renderer>
@ -275,6 +283,7 @@ where
) -> highlighter::Format<Renderer::Font>, ) -> highlighter::Format<Renderer::Font>,
) -> TextEditor<'a, H, Message, Theme, Renderer> { ) -> TextEditor<'a, H, Message, Theme, Renderer> {
TextEditor { TextEditor {
id: self.id,
content: self.content, content: self.content,
placeholder: self.placeholder, placeholder: self.placeholder,
font: self.font, font: self.font,
@ -1057,7 +1066,7 @@ where
) { ) {
let state = tree.state.downcast_mut::<State<Highlighter>>(); let state = tree.state.downcast_mut::<State<Highlighter>>();
operation.focusable(None, layout.bounds(), state); operation.focusable(self.id.as_ref(), layout.bounds(), state);
} }
} }