Skip to content

Commit a9275c8

Browse files
committed
Merge branch 'main' into feat/new-notifs
2 parents 1136863 + a74debc commit a9275c8

File tree

3 files changed

+136
-6
lines changed

3 files changed

+136
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
- Nothing
66

77
### Fixed:
8-
- Nothing
8+
- Fix GUI redraws not pausing when unfocused, hovered and not moving mouse (by @Willy-JL)
99

1010
### Removed:
1111
- Nothing

external/imgui_glfw.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# -*- coding: utf-8 -*-
2+
# This is a modified version of imgui.integrations.glfw, which fixes cursor position calculation and behavior when unfocused
3+
# Full credits to original authors: https://github.com/pyimgui/pyimgui/blob/master/imgui/integrations/glfw.py
4+
5+
from __future__ import absolute_import
6+
7+
import glfw
8+
import imgui
9+
10+
from imgui.integrations import compute_fb_scale
11+
from imgui.integrations.opengl import ProgrammablePipelineRenderer
12+
13+
class GlfwRenderer(ProgrammablePipelineRenderer):
14+
def __init__(self, window, attach_callbacks:bool=True):
15+
super(GlfwRenderer, self).__init__()
16+
self.window = window
17+
18+
if attach_callbacks:
19+
glfw.set_key_callback(self.window, self.keyboard_callback)
20+
glfw.set_cursor_pos_callback(self.window, self.mouse_callback)
21+
glfw.set_window_size_callback(self.window, self.resize_callback)
22+
glfw.set_char_callback(self.window, self.char_callback)
23+
glfw.set_scroll_callback(self.window, self.scroll_callback)
24+
25+
self.io.display_size = glfw.get_framebuffer_size(self.window)
26+
self.io.get_clipboard_text_fn = self._get_clipboard_text
27+
self.io.set_clipboard_text_fn = self._set_clipboard_text
28+
29+
self._map_keys()
30+
self._gui_time = None
31+
32+
def _get_clipboard_text(self):
33+
return glfw.get_clipboard_string(self.window)
34+
35+
def _set_clipboard_text(self, text):
36+
glfw.set_clipboard_string(self.window, text)
37+
38+
def _map_keys(self):
39+
key_map = self.io.key_map
40+
41+
key_map[imgui.KEY_TAB] = glfw.KEY_TAB
42+
key_map[imgui.KEY_LEFT_ARROW] = glfw.KEY_LEFT
43+
key_map[imgui.KEY_RIGHT_ARROW] = glfw.KEY_RIGHT
44+
key_map[imgui.KEY_UP_ARROW] = glfw.KEY_UP
45+
key_map[imgui.KEY_DOWN_ARROW] = glfw.KEY_DOWN
46+
key_map[imgui.KEY_PAGE_UP] = glfw.KEY_PAGE_UP
47+
key_map[imgui.KEY_PAGE_DOWN] = glfw.KEY_PAGE_DOWN
48+
key_map[imgui.KEY_HOME] = glfw.KEY_HOME
49+
key_map[imgui.KEY_END] = glfw.KEY_END
50+
key_map[imgui.KEY_INSERT] = glfw.KEY_INSERT
51+
key_map[imgui.KEY_DELETE] = glfw.KEY_DELETE
52+
key_map[imgui.KEY_BACKSPACE] = glfw.KEY_BACKSPACE
53+
key_map[imgui.KEY_SPACE] = glfw.KEY_SPACE
54+
key_map[imgui.KEY_ENTER] = glfw.KEY_ENTER
55+
key_map[imgui.KEY_ESCAPE] = glfw.KEY_ESCAPE
56+
key_map[imgui.KEY_PAD_ENTER] = glfw.KEY_KP_ENTER
57+
key_map[imgui.KEY_A] = glfw.KEY_A
58+
key_map[imgui.KEY_C] = glfw.KEY_C
59+
key_map[imgui.KEY_V] = glfw.KEY_V
60+
key_map[imgui.KEY_X] = glfw.KEY_X
61+
key_map[imgui.KEY_Y] = glfw.KEY_Y
62+
key_map[imgui.KEY_Z] = glfw.KEY_Z
63+
64+
def keyboard_callback(self, window, key, scancode, action, mods):
65+
# perf: local for faster access
66+
io = self.io
67+
68+
if action == glfw.PRESS:
69+
io.keys_down[key] = True
70+
elif action == glfw.RELEASE:
71+
io.keys_down[key] = False
72+
73+
io.key_ctrl = (
74+
io.keys_down[glfw.KEY_LEFT_CONTROL] or
75+
io.keys_down[glfw.KEY_RIGHT_CONTROL]
76+
)
77+
78+
io.key_alt = (
79+
io.keys_down[glfw.KEY_LEFT_ALT] or
80+
io.keys_down[glfw.KEY_RIGHT_ALT]
81+
)
82+
83+
io.key_shift = (
84+
io.keys_down[glfw.KEY_LEFT_SHIFT] or
85+
io.keys_down[glfw.KEY_RIGHT_SHIFT]
86+
)
87+
88+
io.key_super = (
89+
io.keys_down[glfw.KEY_LEFT_SUPER] or
90+
io.keys_down[glfw.KEY_RIGHT_SUPER]
91+
)
92+
93+
def char_callback(self, window, char):
94+
io = imgui.get_io()
95+
96+
if 0 < char < 0x10000:
97+
io.add_input_character(char)
98+
99+
def resize_callback(self, window, width, height):
100+
self.io.display_size = width, height
101+
102+
def mouse_callback(self, *args, **kwargs):
103+
pass
104+
105+
def scroll_callback(self, window, x_offset, y_offset):
106+
self.io.mouse_wheel_horizontal = x_offset
107+
self.io.mouse_wheel = y_offset
108+
109+
def process_inputs(self):
110+
io = imgui.get_io()
111+
112+
window_size = glfw.get_window_size(self.window)
113+
fb_size = glfw.get_framebuffer_size(self.window)
114+
115+
io.display_size = window_size
116+
io.display_fb_scale = compute_fb_scale(window_size, fb_size)
117+
io.delta_time = 1.0/60
118+
119+
io.mouse_pos = [round(c) for c in glfw.get_cursor_pos(self.window)]
120+
121+
io.mouse_down[0] = glfw.get_mouse_button(self.window, 0)
122+
io.mouse_down[1] = glfw.get_mouse_button(self.window, 1)
123+
io.mouse_down[2] = glfw.get_mouse_button(self.window, 2)
124+
125+
current_time = glfw.get_time()
126+
127+
if self._gui_time:
128+
self.io.delta_time = current_time - self._gui_time
129+
else:
130+
self.io.delta_time = 1. / 60.
131+
if(io.delta_time <= 0.0): io.delta_time = 1./ 1000.
132+
133+
self._gui_time = current_time

modules/gui.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import time
1616
import tomllib
1717

18-
from imgui.integrations.glfw import GlfwRenderer
1918
from PIL import Image
2019
from PyQt6 import (
2120
QtCore,
@@ -57,6 +56,7 @@
5756
error,
5857
filepicker,
5958
imagehelper,
59+
imgui_glfw,
6060
ratingwidget,
6161
)
6262
from modules import (
@@ -395,7 +395,7 @@ def __init__(self):
395395
glfw.terminate()
396396
sys.exit(1)
397397
glfw.make_context_current(self.window)
398-
self.impl = GlfwRenderer(self.window)
398+
self.impl = imgui_glfw.GlfwRenderer(self.window)
399399

400400
# Window position and icon
401401
if all(type(x) is int for x in pos) and len(pos) == 2 and utils.validate_geometry(*pos, *size):
@@ -871,9 +871,6 @@ def main_loop(self):
871871
cursor = imgui.get_mouse_cursor()
872872
any_hovered = imgui.is_any_item_hovered()
873873
win_hovered = glfw.get_window_attrib(self.window, glfw.HOVERED)
874-
if not self.focused and win_hovered:
875-
# GlfwRenderer (self.impl) resets cursor pos if not focused, making it unresponsive
876-
imgui.io.mouse_pos = glfw.get_cursor_pos(self.window)
877874
if not self.hidden and not self.minimized and (self.focused or globals.settings.render_when_unfocused):
878875

879876
# Scroll modifiers (must be before new_frame())

0 commit comments

Comments
 (0)