Skip to content
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

NkEditString doesn't update the buffer length #31

Open
tbogdala opened this issue Nov 2, 2017 · 1 comment
Open

NkEditString doesn't update the buffer length #31

tbogdala opened this issue Nov 2, 2017 · 1 comment

Comments

@tbogdala
Copy link

tbogdala commented Nov 2, 2017

When calling nk.NkEditString (or NkEditStringZeroTerminated) the nuklear wrapper will send the underlying slice Data out to C, but it never updates the length of the buffer. So if the user edits the string in the editbox, the buffer that the Go code has will always have the same length and won't update properly.

A quick hack that I created to get around the problem is to wrap the function and return a new string from the buffer (which could be improved to only create the string when necessary, but this was just a proof of concept).

func editString(ctx *nk.Context, flags nk.Flags, bufferStr string, filter nk.PluginFilter) (string, nk.Flags) {
	const extraBuffer = 64
	len := int32(len(bufferStr))
	max := len + extraBuffer
	haxBuffer := make([]byte, 0, max)
	haxBuffer = append(haxBuffer, bufferStr...)

	retflags := nk.NkEditStringZeroTerminated(ctx, flags, haxBuffer, max, filter)
	rawData := (*C.char)(unsafe.Pointer((*reflect.SliceHeader)(unsafe.Pointer(&haxBuffer)).Data))
	return C.GoString(rawData), retflags
}
@wilyarti
Copy link

wilyarti commented Apr 17, 2018

I did a work around for this. Create a fixed buffer length and trim the null bytes.

See my code: github.com/wilyarti/hashtree-ui

You can also pass a pointer to the buffer and update it that way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants