Skip to content

GLFW window start at correct size and draw during resize #70

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

Merged
merged 1 commit into from
Mar 24, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions rendercanvas/glfw.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def get_physical_size(window):


def enable_glfw():
glfw.init()
glfw.init() # this also resets all window hints
glfw._rc_alive = True


Expand Down Expand Up @@ -180,13 +180,15 @@ def __init__(self, *args, present_method=None, **kwargs):
# Set window hints
glfw.window_hint(glfw.CLIENT_API, glfw.NO_API)
glfw.window_hint(glfw.RESIZABLE, True)
glfw.window_hint(glfw.VISIBLE, False) # start hidden

# Create the window (the initial size may not be in logical pixels)
self._window = glfw.create_window(640, 480, "", None, None)

# Other internal variables
self._changing_pixel_ratio = False
self._is_minimized = False
self._is_in_poll_events = False

# Register callbacks. We may get notified too often, but that's
# ok, they'll result in a single draw.
Expand Down Expand Up @@ -218,6 +220,9 @@ def __init__(self, *args, present_method=None, **kwargs):
# Set size, title, etc.
self._final_canvas_init()

# Now show the window
glfw.show_window(self._window)

def _on_window_dirty(self, *args):
self.request_draw()

Expand Down Expand Up @@ -297,7 +302,11 @@ def _set_logical_size(self, new_logical_size):

def _rc_gui_poll(self):
glfw.post_empty_event() # Awake the event loop, if it's in wait-mode
glfw.poll_events()
try:
self._is_in_poll_events = True
glfw.poll_events() # Note: this blocks when the window is being resized
finally:
self._is_in_poll_events = False
self._maybe_close()

def _rc_get_present_methods(self):
Expand Down Expand Up @@ -368,6 +377,15 @@ def _on_pixelratio_change(self, *args):
def _on_size_change(self, *args):
self._determine_size()
self.request_draw()
# During a resize, the glfw.poll_events() function blocks, so
# our event-loop is on pause. However, glfw still sends resize
# events, and we can use these to draw, to get a smoother
# experience. Note that if the user holds the mouse still while
# resizing, there are no draws. Also note that any animations
# that rely on the event-loop are paused (only animations
# updated in the draw callback are alive).
if self._is_in_poll_events and not self._is_minimized:
self._draw_frame_and_present()

def _on_mouse_button(self, window, but, action, mods):
# Map button being changed, which we use to update self._pointer_buttons.
Expand Down