Skip to content

Commit bfcb76b

Browse files
committed
Track window focus to prevent scrolling in inactive window
1 parent 2a8bf51 commit bfcb76b

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

src/input/mouse.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ void mouse_set_left_down(int down)
7777
data.left.system_change |= down ? SYSTEM_DOWN : SYSTEM_UP;
7878
data.is_touch = 0;
7979
data.is_inside_window = 1;
80+
data.window_has_focus = 1;
8081
if (!down) {
8182
time_millis now = time_get_millis();
8283
int is_double_click = (last_click < now) && ((now - last_click) <= DOUBLE_CLICK_TIME);
@@ -90,6 +91,7 @@ void mouse_set_right_down(int down)
9091
data.right.system_change |= down ? SYSTEM_DOWN : SYSTEM_UP;
9192
data.is_touch = 0;
9293
data.is_inside_window = 1;
94+
data.window_has_focus = 1;
9395
last_click = 0;
9496
}
9597

@@ -99,6 +101,11 @@ void mouse_set_inside_window(int inside)
99101
data.is_touch = 0;
100102
}
101103

104+
void mouse_set_window_focus(int focus)
105+
{
106+
data.window_has_focus = focus;
107+
}
108+
102109
static void update_button_state(mouse_button *button)
103110
{
104111
button->went_down = (button->system_change & SYSTEM_DOWN) == SYSTEM_DOWN;
@@ -145,6 +152,7 @@ const mouse *mouse_in_dialog(const mouse *m)
145152
dialog.right = m->right;
146153
dialog.scrolled = m->scrolled;
147154
dialog.is_inside_window = m->is_inside_window;
155+
dialog.window_has_focus = m->window_has_focus;
148156
dialog.is_touch = m->is_touch;
149157

150158
dialog.x = m->x - screen_dialog_offset_x();

src/input/mouse.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ typedef struct {
3535
mouse_button left; /**< Left mouse button */
3636
mouse_button right; /**< Right mouse button */
3737
int is_inside_window; /**< Whether the mouse is in the window */
38+
int window_has_focus; /**< Whether the window has focus */
3839
int is_touch; /**< Whether the mouse is a translated touch event */
3940
} mouse;
4041

@@ -59,6 +60,8 @@ void mouse_set_scroll(scroll_state state);
5960

6061
void mouse_set_inside_window(int inside);
6162

63+
void mouse_set_window_focus(int focus);
64+
6265
/**
6366
* Changes the mouse information from touch information
6467
* @param first The first touch

src/input/scroll.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -332,15 +332,16 @@ static int set_arrow_input(key *arrow, const key *opposite_arrow, float *modifie
332332

333333
static int get_direction(const mouse *m)
334334
{
335-
int is_inside_window = m->is_inside_window;
335+
int is_inside_active_window = m->is_inside_window && m->window_has_focus;
336336
int width = screen_width();
337337
int height = screen_height();
338-
if (setting_fullscreen() && m->x < width && m->y < height) {
338+
if (setting_fullscreen() && m->window_has_focus) {
339339
// For Windows 10, in fullscreen mode, on HiDPI screens, this is needed
340-
// to get scrolling to work
341-
is_inside_window = 1;
340+
// to get scrolling to work, because the mouse leaves the window on the
341+
// rightmost and bottommost pixel, even though we have grabbed the mouse.
342+
is_inside_active_window = 1;
342343
}
343-
if (!is_inside_window && !m->is_touch) {
344+
if (!is_inside_active_window && !m->is_touch) {
344345
return DIR_8_NONE;
345346
}
346347
int top = 0;

src/platform/julius.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,12 @@ static void handle_window_event(SDL_WindowEvent *event, int *window_active)
238238
case SDL_WINDOWEVENT_LEAVE:
239239
mouse_set_inside_window(0);
240240
break;
241+
case SDL_WINDOWEVENT_FOCUS_LOST:
242+
mouse_set_window_focus(0);
243+
break;
244+
case SDL_WINDOWEVENT_FOCUS_GAINED:
245+
mouse_set_window_focus(1);
246+
break;
241247
case SDL_WINDOWEVENT_SIZE_CHANGED:
242248
SDL_Log("Window resized to %d x %d", (int) event->data1, (int) event->data2);
243249
platform_screen_resize(event->data1, event->data2);
@@ -641,6 +647,7 @@ int main(int argc, char **argv)
641647
setup(&args);
642648

643649
mouse_set_inside_window(1);
650+
mouse_set_window_focus(1);
644651
run_and_draw();
645652

646653
#ifdef __EMSCRIPTEN__

0 commit comments

Comments
 (0)