Makie.jl Textbox

Thanks for creating the issue Textbox set! fails · Issue #3069 · MakieOrg/Makie.jl · GitHub.
Note to further improve readability:
it is possible to tell github that a block of code is julia and get syntax highlighting) by typing

 ```julia

instead of bare triple quotes.


Reading the previous issue I have a feeling there is a workaround

Indeed, there is a workaround (Makie gives us a lot of control, so it is almost always the case :slight_smile:): define a custom method, e.g.

function custom_set!(tbox::Makie.Textbox, new_text)
	old_text = tbox.displayed_string[]
	old_pos = tbox.cursorindex[]
	if length(new_text) == 0
		# for now setting new_text to "" errors
		# TODO: is resetting the correct thing to do ?
		Makie.reset!(tbox)
	elseif length(new_text) == length(old_text)
		# no length change, cursor is already at the correct position
		Makie.set!(tbox, new_text)
	elseif length(new_text) > length(old_text)
		if old_pos == length(old_text)
			# Let's keep the cursor at the end.
			# Need to add the new text first,
			# to make the new cursor position valid.
			Makie.set!(tbox, new_text)
			tbox.cursorindex[] = length(new_text)
		else
			# cursor was inside the text; keep cursor position
			Makie.set!(tbox, new_text)
		end
	else
		# new text is shorter
		# place the cursor at a future valid position first (not outside new_text)
		tbox.cursorindex[] = min(old_pos, length(new_text))
		Makie.set!(tbox, new_text)
	end
end

and use that instead of Makie.set!, like custom_set!(atextbox, "a string2").