Skip to content
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

Change Window from a borrowed handle to a weakly owned handle #174

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

prokopyl
Copy link
Member

@prokopyl prokopyl commented Mar 24, 2024

This PR implements the idea discussed with @glowcoil on Discord a few days ago, which is to make the Window handle 'static and clone-able, so that it can be easily stored in the WindowHandler.

Note this PR is based on #172 for now, which is why the new example shows up in the diff. This can be changed depending on #172's merge status. This has been properly rebased now.

This makes the following breaking API changes, for all platforms and backends:

  • Removes the lifetime of the Window type.
  • The Window type is now an owned, weak handle to the actual underlying window.
  • Makes the Window type clone-able.
  • The Window argument passed to the WindowHandler builder is now a fully owned value instead of a temporary borrow, which enables the handler to store it for its whole lifetime.
  • Removes the Window argument on WindowHandler::on_frame and WindowHandler::on_event methods.
  • All Window methods now only take &self instead of &mut self.
  • All Window methods are now internally fallible: they will panic if the actual window the handle points to has been destroyed. This can only happen if the WindowHandler misbehaves and clones the Window outside of its own lifetime.
  • The GlContext has received the same treatment, for the same reasons: it's now a weak, owned clone-able handle to the GL context of a window, and its methods will also panic if the underlying context is destroyed.

As of now, this PR is still a WIP:

  • The X11 backend is complete and functional, but it will probably need to be rebased on top of Switch from xcb crate to x11rb #173 before this PR can be merged. It's done!
  • I started working on the Win32 backend, but it's not done yet. Done!
  • I haven't touched the Cocoa backend, and it will not build at all at the moment. Done, but still needs testing.

As of now, this PR will not build on macOS or Windows until I've completed the work mentioned above.

@prokopyl prokopyl requested a review from micahrj March 24, 2024 11:54
@prokopyl
Copy link
Member Author

Win32 backend update done, tested and functional! 🙂

@prokopyl prokopyl force-pushed the remove-window-lifetime branch 3 times, most recently from bb1108a to 2ef375f Compare March 25, 2024 22:48
@prokopyl prokopyl force-pushed the remove-window-lifetime branch 4 times, most recently from 4a82bfc to 999f97d Compare March 27, 2024 07:01
@prokopyl prokopyl force-pushed the remove-window-lifetime branch from e10382d to 231bbc6 Compare March 29, 2024 23:26
micahrj pushed a commit that referenced this pull request Apr 5, 2024
This PR splits off the X11 event loop logic into a separate module. It also changes the X11 implementation of the `Window` type to take only a shared reference to the inner type (`&WindowInner` instead of `&mut WindowInner`), bringing it in line with the other backends.

This does not change any of the logic however, it only separates some of the window state from the event loop state, to make sure they don't step on each other's toes in the future (particularly around the WindowHandler).

This is part of the effort to split up #174 into smaller pieces.
prokopyl added 3 commits April 6, 2024 18:09
More post-rebase fixes, add missing flush call

Post-rebase fixes

Fix windows OpenGL context creation

Minor fixes

Squashed:

Port X11 backend to x11rb

Fix macOS build

Win32 refactor

Make the added with_scale_factor fn pub(crate) for now

Remove unneeded internal HasRawWindowHandle implementation

Simplify everything by making the public Window/GlContext types hold the Weak themselves

Remove lifetime from Window type, refactor and split X11 backend

Added functional open_parented example
@prokopyl prokopyl force-pushed the remove-window-lifetime branch from 231bbc6 to 0b58445 Compare April 6, 2024 16:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant