Detecting Focus on TextInput to Dispatch a Message

Hello Iced community,

I’m working on an application where I have several views with TextInput widgets, and I’d like to detect when they gain focus in order to dispatch a specific message. I’ve assigned an Id to my TextInput widgets like this:

let text_input = TextInput::new("", save_name)
    .id(Id::new("save_preset_input"));

I’ve looked into the find_focused operation, but I’m unsure how to proceed with it, or even if it’s the right tool for this job. I haven’t found a built-in on_focus event for TextInput. Could anyone guide me on how to properly use commands, or any other mechanism, to detect when a TextInput is focused and dispatch a message in response?

Any advice or examples would be greatly appreciated.

Thank you!

I was trying to achieve something similar a few months ago, and the solution I found was to create a very simple custom widget that wraps the iced::widget::TextInput widget and whose constructor allows passing a callback to receive messages for changes in the TextInput’s “focus state”.

Here’s the link to the repo: GitHub - BB-301/iced-text-input-wrapper-widget: A custom Iced widget that wraps around the official text input widget to allow subscribing to changes in the text input's focus state.