RobLoach / raylib-nuklear

Nuklear immediate mode GUI for raylib
https://robloach.github.io/raylib-nuklear/
zlib License
151 stars 22 forks source link

Clipboard issues #76

Closed CG8516 closed 3 months ago

CG8516 commented 4 months ago

Hello! I've noticed a couple of issues with the clipboard functionality and would like to share my solutions.
I didn't make a pull request because I'm reasonably new to both raylib and nuklear, and there's a fair chance I'm doing something wrong or that there could be better solutions.
I'm using the changes from #61 to get ctrl+c and ctrl+v working.

  1. The clipboard callbacks are being set before the context is initialised.
    This causes them to be set to null almost immediately after they are set, breaking clipboard functionality.
    I fixed it by moving the clipboard callback assignment a few lines down to after nk_init instead of before it.

  2. nk_raylib_clipboard_copy ignores length, which is problematic when copying text from nk_edit_string and nk_edit_buffer.
    This can result in additional bytes beyond the displayed textbox contents being copied to the clipboard.
    len seems to be set to the number of text characters without including the null terminator. A single char copied from nk_edit_string and nk_edit_string_zero_terminated both have a length of 1, but the array from nk_edit_string_zero_terminated has text[len] set to 0.
    I'm not sure if it's possible to know if a string is null-terminated or not without risking reading beyond the length of the buffer (text[len] could be one byte beyond allocated memory).
    I avoid the issue by always copying the string to a len+1 array with the last byte set to 0, but maybe this can be avoided in some cases if there's a safe way to check if the string is already null-terminated.

RobLoach commented 4 months ago

Would you be able to send a Pull request with the change? Happy to have this in thanks so much.

RobLoach commented 4 months ago

Merged in that other PR too. There are some performance fixes we could apply here too. Overall looking really good. THanks.

RobLoach commented 3 months ago

I believe we can now close this one. :+1: