Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement find_focused() for text_input #2664

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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 changed the title Implement find_focus() for text_input Implement find_focused() for text_input Nov 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant