Skip to content

[HELP] Number only input #2331

Description

@Leone25

Hello,

I'm trying to create a TOTP aka numbers only input.
From what I can tell there's no dedicated widget to do it, so this is what I came up with something like this:

use gtk4 as gtk;
use gtk::prelude::*;
use gtk::{glib, Application, ApplicationWindow, Entry, InputPurpose};

fn main() -> glib::ExitCode {
    let application = Application::builder()
        .application_id("com.example.test")
        .build();

    application.connect_activate(|app| {
        let window = ApplicationWindow::builder()
            .application(app)
            .title("Test")
            .build();

        let totp_input = Entry::builder()
            .placeholder_text("TOTP")
            .input_purpose(InputPurpose::Digits)
            .build();

        totp_input.connect_insert_text(|editable, text, position| {
            let filtered: String = text.chars().filter(|c| c.is_ascii_digit()).collect();

            if filtered != text {
                editable.stop_signal_emission_by_name("insert-text");
                editable.insert_text(&filtered, position);
            }
        });

        window.set_child(Some(&totp_input));

        window.present();
    });

    application.run()
}

If you run that and try to enter both words and numbers you'll notice that the filtering does not work. From what I can the the insert-text signal is never called.

Am I doing something wrong? Is there a better approach?

Thank you in advance

Rico

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions