Hi,
I am trying to retrieve the ID of an active text_input widget.
The text_input widget implements the Focusable trait. Therefore, I thought that I could use iced::advanced::widget::operation::focusable::find_focused()
but it doesn’t return any ID.
Then I played around a bit and tried whether I could use a focus() function first to see if this is working. If so, I would expect that calling find_focus() directly afterwards produces a positive result.
I noticed that iced::widget::text_input::focus("some-id")
focuses the text_input widget but iced::advanced::widget::operation::focusable::focus::<Message>(Id::new("some-id"))
does not.
Is the second one not working because iced::advanced uses a different ID type than iced::widget and therefore cannot find any ID matches?
Do you have any tips what I am missing here? Or any ideas/examples how I could make iced::advanced::widget::operation::focusable::focus::<T>(...)
work or how an alternative approach could look like?
Thanks a lot!
I am using iced 0.13.1
Below I pasted a short example code that I used to play around.
When the button is pressed, the console produces “Did not find a widget with ID some-id” and “Did not find a focused widget”.
And I would hope for “Focus on widget with ID some-id now” and “Found focused widget with ID some-id”.
use iced::advanced::widget::operation::Outcome;
use iced::advanced::widget::{Id, Operation};
use iced::widget::{button, column, text_input, Column};
fn main() -> iced::Result {
iced::application("Focus Test", App::update, App::view).run()
}
#[derive(Default)]
struct App {
input: String,
}
#[derive(Clone, Debug)]
enum Message {
InputUpdated(String),
FocusInput,
}
impl App {
fn update(&mut self, message: Message) {
match message {
Message::InputUpdated(input) => {
self.input = input;
}
Message::FocusInput => {
// focus text_input widget
match iced::advanced::widget::operation::focusable::focus::<Message>(Id::new(
"some-id"
))
.finish()
{
Outcome::None => println!("Did not find a widget with ID some-id"),
Outcome::Some(_) => println!("Focus on widget with ID some-id now"),
Outcome::Chain(_) => todo!(),
}
// expectation: find_focus function returns some-id as this widget just got focused
match iced::advanced::widget::operation::focusable::find_focused().finish() {
Outcome::None => println!("Did not find a focused widget"),
Outcome::Some(id) => println!("Found focused widget with ID {:?}", id),
Outcome::Chain(_) => todo!(),
}
}
}
}
fn view(&self) -> Column<Message> {
column![
text_input("", &self.input)
.id("some-id")
.on_input(Message::InputUpdated),
button("Focus On Input").on_press(Message::FocusInput),
]
}
}