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

Set up NSView the way Glutin requires it #1698

Merged
merged 3 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Fixed EGL's `Display::new()` making an `EGLDisplay::Khr` when the EGL version for the display is 1.4 or lower.
- Added `Device::drm_device_node_path()` and `Device::drm_render_device_node_path()` getters to EGL via `EGL_EXT_device_drm`.
- Added support for `DrmDisplayHandle` in EGL's `Display::with_device()` using `EGL_DRM_MASTER_FD_EXT` from `EGL_EXT_device_drm`.
- Properly set up OpenGL-specific stuff on the `NSView`, instead of relying on Winit to do it.

# Version 0.32.0

Expand Down
2 changes: 2 additions & 0 deletions glutin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,12 @@ features = [
[target.'cfg(any(target_os = "macos"))'.dependencies.objc2-app-kit]
version = "0.2.0"
features = [
"NSApplication",
"NSResponder",
"NSView",
"NSWindow",
"NSOpenGL",
"NSOpenGLView",
]

[build-dependencies]
Expand Down
21 changes: 19 additions & 2 deletions glutin/src/api/cgl/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::marker::PhantomData;
use std::num::NonZeroU32;

use objc2::rc::Id;
use objc2_app_kit::NSView;
use objc2_app_kit::{NSAppKitVersionNumber, NSAppKitVersionNumber10_12, NSView};
use objc2_foundation::{run_on_main, MainThreadBound, MainThreadMarker};
use raw_window_handle::RawWindowHandle;

Expand Down Expand Up @@ -60,12 +60,29 @@ impl Display {
// SAFETY: Validity of the view and window is ensured by caller
// This function makes sure the window is non null.
let ns_view = if let Some(ns_view) =
unsafe { Id::retain(native_window.ns_view.as_ptr().cast()) }
unsafe { Id::retain(native_window.ns_view.as_ptr().cast::<NSView>()) }
{
ns_view
} else {
return Err(ErrorKind::NotSupported("ns_view of provided native window is nil").into());
};

// The default value of `wantsBestResolutionOpenGLSurface` is `false` when
// linked with the macOS 10.14 SDK and `true` if linked with a macOS 10.15 SDK
// or newer. We always set it to `true` because we want High DPI surfaces, and
// we want to avoid this confusing default system value.
#[allow(deprecated)]
ns_view.setWantsBestResolutionOpenGLSurface(true);

// On Mojave, views apparently automatically become layer-backed shortly after
// being added to a window. Changing the layer-backedness of a view breaks the
// association between the view and its associated OpenGL context. To work
// around this, we explicitly make the view layer-backed up front so that AppKit
// doesn't do it itself and break the association with its context.
if unsafe { NSAppKitVersionNumber }.floor() > NSAppKitVersionNumber10_12 {
ns_view.setWantsLayer(true);
}

let ns_view = MainThreadBound::new(ns_view, mtm);

let surface = Surface {
Expand Down
Loading