Nuklear icon indicating copy to clipboard operation
Nuklear copied to clipboard

Restore cursor position in nk_edit_string ?

Open alexandervanrenen opened this issue 5 years ago • 2 comments

Hi folks,

First time posting here, so first things first: thanks for this great project. I have been using it for a while and <3 it. But I ran into a small problem:

I am using a text edit filed (nk_edit_string) and am programmatically re-focusing it (nk_edit_focus). Works well, however the position of the cursor gets reset.

It would be great to have a flag for the nk_edit_string to make it remember its cursor position (I think there is none as of now). Sadly, my familiarity with nuklear's internals is very limited. I solved this by adding functions to get and set the cursor position (https://github.com/alexandervanrenen/Nuklear/commit/c74da8ad5847f90824d310ef87fb82aa55cf45ef).

My question would be if this feature is of general interest: Is there a good way to already accomplish this? Would it be possible to add such a flag or use functions to set the cursor position?

Thanks, Alex

alexandervanrenen avatar Feb 27 '20 18:02 alexandervanrenen

I'll take the liberty to respond first :wink:.

There are two ways how I'd approach this question.

  1. user experience (and actually programmer experience as well) definitely needs to remember the cursor position - thus this should be the default (which it isn't currently)

  2. the solution of yours nicely shows the advantage of immediate mode way of things - it's easy to track the cursor position on your own (though a bit verbose) and thus one would say Nuklear doesn't need such things

My personal opinion is, that in this case we should somehow provide better defaults as described in (1). On the other hand, the helpers from @alexandervanrenen's solution are not comfortable enough IMHO as they don't store the information anywhere per widget instance.

My suggestion would be to either somehow abuse ctx in nuklear.h itself or if we wanted to stay conservative, then I'd say we should provide this functionality in each backend through an additional struct pointed to by void pointer in each widget instance (this second idea is more work, but wouldn't cause nuklear.h to grow).

Don't know what would be better - anyone wants to comment on this?

dumblob avatar Feb 28 '20 09:02 dumblob

Noticing this too when interacting non-programmatically if the text editor unfocused position is below the scrollbar.

Don't know what would be better - anyone wants to comment on this?

Just an opinion, but what about specifying callbacks for focus/blur stored on nk_text_edit and invoked by nk_do_edit? This could work well with widget userdata or possibly context userdata. It would also be cool to adjust the scrollbar position and do other things as well without having to provide more configuration upfront or having to reinvent things.

skinnyjames avatar Nov 06 '23 05:11 skinnyjames