Druid controller - what is the correct way to "ignore" update

252 Views Asked by At

In my rust druid application I need to filter the characters when they are non numeric and do not update the widget and associated field of my state structure. This is my code for the controller

struct NumericOnly;
impl Controller<String, TextBox<String>> for NumericOnly {
    fn update(&mut self, child: &mut TextBox<String>, ctx: &mut UpdateCtx, old_data: &String, data: &String, env: &Env) {

        if data.chars().enumerate().all(|(idx, chr)| chr.is_numeric() || (chr == '-' && idx == 0)) {
            child.update(
                ctx, old_data, data, env
            );
        }
    }
}

This is built on top of an answer I got to a similar question long time ago.

Sadly now I face the issue where, while it doesn't update on non-numeric characters or '-' at any position but the first, once such "faulty" character is detected the widget seems to freeze. I assume that instead of completely ignoring the update I perhaps should've either used some dedicated functionality (which I didn't find any mention of), or update passing old_data as new data in child.update() call. Latter worked, but the unwanted characters started appearing in the text field.

0

There are 0 best solutions below