Skip to content

Implement find_focused() for text_input#2664

Merged
hecrj merged 3 commits intoiced-rs:masterfrom
Leonie-Theobald:findfocus_for_textinput
Nov 18, 2025
Merged

Implement find_focused() for text_input#2664
hecrj merged 3 commits intoiced-rs:masterfrom
Leonie-Theobald:findfocus_for_textinput

Conversation

@Leonie-Theobald
Copy link

This PR adds support for find_focused() for text_input.
Basically, it is a wrapper function that allows the user to indirectly make use of find_focused()within the widget module.

I also added a From<widget::Id> for Id for text_input so that the returned widget::Id can be converted to the text_input ID that the enduser can use.

This topic came up in a discussion in Discourse: here

Find an example that uses find_focused() below:

use iced::widget::{column, text_input, Column};
use iced::{Subscription, Task};

use iced::keyboard;
use keyboard::key::Named::ArrowUp;

fn main() -> iced::Result {
    iced::application("Example For find_focused()", App::update, App::view)
        .subscription(App::subscription)
        .run()
}

#[derive(Default)]
struct App {
    list: [Item; 5],
}

struct Item {
    id: text_input::Id,
    content: String,
}

impl Default for Item {
    fn default() -> Self {
        Item {
            id: text_input::Id::unique(),
            content: String::new(),
        }
    }
}

#[derive(Clone, Debug)]
enum Message {
    ContentUpdated(usize, String),
    SwapFocusedItemWithTop,
    SwapItemWithTop(text_input::Id),
}

impl App {
    fn update(&mut self, message: Message) -> Task<Message> {
        match message {
            Message::ContentUpdated(position, content) => {
                self.list[position].content = content;
            }
            Message::SwapFocusedItemWithTop => {
                return text_input::find_focused()
                    .map::<Message>(|id| Message::SwapItemWithTop(id.into()));
            }
            Message::SwapItemWithTop(id) => {
                if let Some(position) = self.list.iter().position(|item| item.id == id) {
                    self.list.swap(0, position);
                };
            }
        }

        Task::none()
    }

    fn view(&self) -> Column<Message> {
        column(self.list.iter().enumerate().map(|(position, item)| {
            text_input("", &item.content)
                .on_input(move |new_content| Message::ContentUpdated(position, new_content))
                .id(item.id.clone())
                .into()
        }))
        .spacing(10)
    }

    fn subscription(&self) -> Subscription<Message> {
        keyboard::on_key_press(|key, _modifiers| match key {
            keyboard::Key::Named(ArrowUp) => Some(Message::SwapFocusedItemWithTop),
            _ => None,
        })
    }
}

@Leonie-Theobald Leonie-Theobald force-pushed the findfocus_for_textinput branch from eb79224 to 274ac38 Compare November 5, 2024 13:54
@Leonie-Theobald Leonie-Theobald changed the title Implement find_focus() for text_input Implement find_focused() for text_input Nov 5, 2024
@hecrj hecrj force-pushed the findfocus_for_textinput branch from 274ac38 to 1ae3b5e Compare November 18, 2025 22:04
@hecrj hecrj added this to the 0.14 milestone Nov 18, 2025
@hecrj hecrj added improvement An internal improvement feature New feature or request widget change addition labels Nov 18, 2025
Copy link
Member

@hecrj hecrj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

I have created an is_focused selector and unified the selector API to simply have two operations: find and find_all.

@hecrj hecrj enabled auto-merge November 18, 2025 22:07
@hecrj hecrj merged commit 2f155f8 into iced-rs:master Nov 18, 2025
15 checks passed
@Leonie-Theobald
Copy link
Author

Didn't think that the PR would be relevant anymore^^
But nice that it made its way into the code base :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

addition change feature New feature or request improvement An internal improvement widget

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants