david-vanderson / dvui

Other
346 stars 26 forks source link

Text entry number improvements #107

Closed VisenDev closed 1 month ago

VisenDev commented 1 month ago

When working on my struct-ui branch it seemed evident that being able to set a minimum and maximum value for a textEntryNumber would be useful

Example use cases,

Screenshots:

(Empty)

Screenshot 2024-08-18 at 3 46 18 PM

(With both valid and invalid numbers entered)

Screenshot 2024-08-18 at 3 46 35 PM

I also changed the widget to create its own buffer rather than requiring the user to provide one, as this simplifies usage.

david-vanderson commented 1 month ago

Thanks! This looks pretty good, but we need 2 more things:

  1. Some way to also return the different invalid states, so the code can message the user properly. In the example, to the right of the text entry we should be able to display "too low", "too high", or "invalid". What do you think about returning something like error{TooLow, TooHigh, Invalid}!?f32 where the return is the number if it was valid?

  2. Passing in a pointer to a number, because many times an app will need to prepopulate it with a number. I guess we add an optional pointer into the TextEntryNumberInitOptions? If it's not null, then it both prepopulates the text with that number and also on a valid change writes the new number back?

VisenDev commented 1 month ago

Thanks! This looks pretty good, but we need 2 more things:

  1. Some way to also return the different invalid states, so the code can message the user properly. In the example, to the right of the text entry we should be able to display "too low", "too high", or "invalid". What do you think about returning something like error{TooLow, TooHigh, Invalid}!?f32 where the return is the number if it was valid?
  2. Passing in a pointer to a number, because many times an app will need to prepopulate it with a number. I guess we add an optional pointer into the TextEntryNumberInitOptions? If it's not null, then it both prepopulates the text with that number and also on a valid change writes the new number back?

On it

VisenDev commented 1 month ago

@david-vanderson How do you feel about how I addressed issues 1 and 2?

Edit I seem to have introduced a bug, let me fix that

david-vanderson commented 1 month ago

That looks pretty good! I like the return strategy.

For thing 2, can you change TextEntryNumberInitOptions.initialize to be a pointer ?*T and a name like value and if the number parses valid, write the number back to that pointer?

VisenDev commented 1 month ago

Should I removeresult.Valid also returning the number?

Edit: Actually, that would mean the app has to provide a opts.value pointer to use the function, which would defeat the purpose of it being ?*T

david-vanderson commented 1 month ago

Looks great - merge it! Let me know if you don't have the permissions or would rather I do it.