-
-
Notifications
You must be signed in to change notification settings - Fork 306
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
Textbox errors depending on cursor position upon submission #2037
Comments
The problem happens on |
The error might be because when we entered a value with the cursor sitting not at first or second place, and we reset the displayed string, then the only valid positions the cursor could go are the first two places. |
Just tested: One possible fix is to reset the cursor index upon submission, e.g. add If that sounds reasonable, I would upon a PR with this. |
There's already a validation feature that you can use by the way, there's a validator or validation attribute that accepts a function. |
I am aware of that feature. However, I could not figure out how to use it to reset the displayed string if the entered value is out of bounds. Or am I missing something here? The reason I want to do reset it is so that I can couple a import Makie.MakieLayout: set!
using GLMakie
GLMakie.activate!()
fig = Figure()
slider = Slider(fig[1,1], range=1:10, startvalue=1)
idxbox = Textbox(fig[2,1], stored_string="1", tellwidth=false, width=100)
prev_idx = parse(Int64, idxbox.stored_string[])
on(idxbox.stored_string) do s
global prev_idx
idx = parse(Int64, s)
if idx == prev_idx
idxbox.displayed_string[] = "$idx"
set_close_to!(slider, idx)
else
idx = max(idx,1)
idx = min(idx,10)
prev_idx = idx
set!(idxbox, "$idx")
end
end
on(slider.value) do idx
global prev_idx
if prev_idx != idx
set!(idxbox, "$idx")
end
end
display(fig) This works now with the |
It should not be able to store an invalid string at all, and reset the box after you set it, no? I've not used the textbox much after building it and it could be that what you want makes sense, though. |
Yes, reset the box if the value is out of bounds. I tried with a custom import Makie.MakieLayout: set!
using GLMakie
GLMakie.activate!()
function validate_inrange(str)
idx = tryparse(Int64, str)
if isnothing(idx)
return false
else
return 1 <= idx <= 10
end
end
fig = Figure()
idxbox = Textbox(fig[1,1], stored_string="1", tellwidth=false, width=100,
validator=validate_inrange)
prev_idx = parse(Int64, idxbox.stored_string[])
on(idxbox.stored_string) do s
println(s)
end
display(fig) |
aha I just tried it out and remembered there was also the attribute reset_on_defocus: Controls if the displayed text is reset to the stored text when defocusing the
textbox without submitting. Default: false If you set that to true, it resets when clicking somewhere else. What's still a bit buggy is that even on invalid inputs, submitting via enter makes the text gray and not red. That looks almost like it had submitted, which it didn't though so at least functionally it's correct. Just a visual inconsistency. |
Fixed that here #2041 |
Oh, I must have missed that attribute, I am sorry. Thanks for taking care of it. |
Well it's easy to miss as there are no examples anywhere :) No worries! |
MWE
The above gives you a
Textbox
which accepts only values in a range 1:10, e.g. typing into the box -1 and submitting it will reset the entered value to 1, submitting 14 will be reset to 10, values in between 1 and 10 are not altered.Unfortunately, this does not work perfectly and throws the following error:
Stacktrace
I played around with this and I think the problem has to do with where the cursor is positioned upon submitting the value. E.g. entering -1 and positioning the cursor
After some more tinkering I can say that the error is always thrown if the cursor is not positioned at the first or second place (counting from the left boundary).
The docs ofTextbox
say thatdisplayed_string
is for internal use.Is this a bug or should we simply not use
displayed_string
? If the latter is the case, how could we work around this?My bad, only
stored_string
is for internal use. Hence, I also useset!
above to alter it.But what about
displayed_string
? Are we not allowed to alter it or is this a bug?The text was updated successfully, but these errors were encountered: