Skip to content

Commit ba56482

Browse files
committed
Fix scrolling with mouse and arrows
1 parent 2ebc533 commit ba56482

File tree

4 files changed

+23
-11
lines changed

4 files changed

+23
-11
lines changed

demo/glfw_opengl3/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
BIN = demo
33

44
# Flags
5-
CFLAGS += -std=c89 -Wall -Wextra -pedantic
5+
CFLAGS += -g -std=c89 -Wall -Wextra -pedantic
66

77
SRC = main.c
88
OBJ = $(SRC:.c=.o)

nuklear.h

+11-5
Original file line numberDiff line numberDiff line change
@@ -27277,7 +27277,7 @@ nk_textedit_key(struct nk_text_edit *state, enum nk_keys key, int shift_mod,
2727727277
case NK_KEY_TEXT_WORD_LEFT:
2727827278
if (shift_mod) {
2727927279
if( !NK_TEXT_HAS_SELECTION( state ) )
27280-
nk_textedit_prep_selection_at_cursor(state);
27280+
nk_textedit_prep_selection_at_cursor(state);
2728127281
state->cursor = nk_textedit_move_to_word_previous(state);
2728227282
state->select_end = state->cursor;
2728327283
nk_textedit_clamp(state );
@@ -28298,10 +28298,12 @@ nk_do_edit(nk_flags *state, struct nk_command_buffer *out,
2829828298
} else edit->scrollbar.x = 0;
2829928299

2830028300
if (flags & NK_EDIT_MULTILINE) {
28301-
/* vertical scroll */
28301+
/* vertical scroll: like horizontal, it only adjusts if the
28302+
* cursor leaves the visible area, and then only just enough
28303+
* to keep it visible */
2830228304
if (cursor_pos.y < edit->scrollbar.y)
28303-
edit->scrollbar.y = NK_MAX(0.0f, cursor_pos.y - row_height);
28304-
if (cursor_pos.y >= edit->scrollbar.y + row_height)
28305+
edit->scrollbar.y = NK_MAX(0.0f, cursor_pos.y);
28306+
if (cursor_pos.y > edit->scrollbar.y + area.h - row_height)
2830528307
edit->scrollbar.y = edit->scrollbar.y + row_height;
2830628308
} else edit->scrollbar.y = 0;
2830728309
}
@@ -28324,9 +28326,13 @@ nk_do_edit(nk_flags *state, struct nk_command_buffer *out,
2832428326
scroll_step = scroll.h * 0.10f;
2832528327
scroll_inc = scroll.h * 0.01f;
2832628328
scroll_target = text_size.y;
28327-
edit->scrollbar.y = nk_do_scrollbarv(&ws, out, scroll, 0,
28329+
edit->scrollbar.y = nk_do_scrollbarv(&ws, out, scroll, edit->active && in,
2832828330
scroll_offset, scroll_target, scroll_step, scroll_inc,
2832928331
&style->scrollbar, in, font);
28332+
/* Eat mouse scroll if we're active */
28333+
if (edit->active && in && in->mouse.scroll_delta.y) {
28334+
in->mouse.scroll_delta.y = 0;
28335+
}
2833028336
}
2833128337
}
2833228338

src/nuklear_edit.c

+10-4
Original file line numberDiff line numberDiff line change
@@ -480,10 +480,12 @@ nk_do_edit(nk_flags *state, struct nk_command_buffer *out,
480480
} else edit->scrollbar.x = 0;
481481

482482
if (flags & NK_EDIT_MULTILINE) {
483-
/* vertical scroll */
483+
/* vertical scroll: like horizontal, it only adjusts if the
484+
* cursor leaves the visible area, and then only just enough
485+
* to keep it visible */
484486
if (cursor_pos.y < edit->scrollbar.y)
485-
edit->scrollbar.y = NK_MAX(0.0f, cursor_pos.y - row_height);
486-
if (cursor_pos.y >= edit->scrollbar.y + row_height)
487+
edit->scrollbar.y = NK_MAX(0.0f, cursor_pos.y);
488+
if (cursor_pos.y > edit->scrollbar.y + area.h - row_height)
487489
edit->scrollbar.y = edit->scrollbar.y + row_height;
488490
} else edit->scrollbar.y = 0;
489491
}
@@ -506,9 +508,13 @@ nk_do_edit(nk_flags *state, struct nk_command_buffer *out,
506508
scroll_step = scroll.h * 0.10f;
507509
scroll_inc = scroll.h * 0.01f;
508510
scroll_target = text_size.y;
509-
edit->scrollbar.y = nk_do_scrollbarv(&ws, out, scroll, 0,
511+
edit->scrollbar.y = nk_do_scrollbarv(&ws, out, scroll, edit->active && in,
510512
scroll_offset, scroll_target, scroll_step, scroll_inc,
511513
&style->scrollbar, in, font);
514+
/* Eat mouse scroll if we're active */
515+
if (edit->active && in && in->mouse.scroll_delta.y) {
516+
in->mouse.scroll_delta.y = 0;
517+
}
512518
}
513519
}
514520

src/nuklear_text_editor.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ nk_textedit_key(struct nk_text_edit *state, enum nk_keys key, int shift_mod,
499499
case NK_KEY_TEXT_WORD_LEFT:
500500
if (shift_mod) {
501501
if( !NK_TEXT_HAS_SELECTION( state ) )
502-
nk_textedit_prep_selection_at_cursor(state);
502+
nk_textedit_prep_selection_at_cursor(state);
503503
state->cursor = nk_textedit_move_to_word_previous(state);
504504
state->select_end = state->cursor;
505505
nk_textedit_clamp(state );

0 commit comments

Comments
 (0)