1
0
Fork 0

Implement window resizing for macOS

Now this works on all baseview platforms.
This commit is contained in:
Robbert van der Helm 2022-12-01 20:12:50 +01:00
parent 80fb0a00b8
commit e0c79c58fb
2 changed files with 26 additions and 5 deletions

View file

@ -5,10 +5,10 @@ use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc; use std::sync::Arc;
use cocoa::appkit::{ use cocoa::appkit::{
NSApp, NSApplication, NSApplicationActivationPolicyRegular, NSBackingStoreBuffered, NSWindow, NSApp, NSApplication, NSApplicationActivationPolicyRegular, NSBackingStoreBuffered, NSView,
NSWindowStyleMask, NSWindow, NSWindowStyleMask,
}; };
use cocoa::base::{id, nil, NO}; use cocoa::base::{id, nil, NO, YES};
use cocoa::foundation::{NSAutoreleasePool, NSPoint, NSRect, NSSize, NSString}; use cocoa::foundation::{NSAutoreleasePool, NSPoint, NSRect, NSSize, NSString};
use core_foundation::runloop::{ use core_foundation::runloop::{
CFRunLoop, CFRunLoopTimer, CFRunLoopTimerContext, __CFRunLoopTimer, kCFRunLoopDefaultMode, CFRunLoop, CFRunLoopTimer, CFRunLoopTimerContext, __CFRunLoopTimer, kCFRunLoopDefaultMode,
@ -20,7 +20,7 @@ use objc::{msg_send, runtime::Object, sel, sel_impl};
use raw_window_handle::{AppKitHandle, HasRawWindowHandle, RawWindowHandle}; use raw_window_handle::{AppKitHandle, HasRawWindowHandle, RawWindowHandle};
use crate::{ use crate::{
Event, EventStatus, WindowEvent, WindowHandler, WindowInfo, WindowOpenOptions, Event, EventStatus, Size, WindowEvent, WindowHandler, WindowInfo, WindowOpenOptions,
WindowScalePolicy, WindowScalePolicy,
}; };
@ -319,6 +319,28 @@ impl Window {
self.close_requested = true; self.close_requested = true;
} }
pub fn resize(&mut self, size: Size) {
let size = NSSize::new(size.width, size.height);
unsafe { NSView::setFrameSize(self.ns_view, size) };
unsafe {
let _: () = msg_send![self.ns_view, setNeedsDisplay: YES];
}
// When using OpenGL the `NSOpenGLView` needs to be resized separately? Why? Because macOS.
#[cfg(feature = "opengl")]
if let Some(gl_context) = &self.gl_context {
gl_context.resize(size);
}
// If this is a standalone window then we'll also need to resize the window itself
if let Some(ns_window) = self.ns_window {
let mut frame = unsafe { NSWindow::frame(ns_window) };
frame.size = size;
unsafe { NSWindow::setFrame_display_(ns_window, frame, YES) }
}
}
#[cfg(feature = "opengl")] #[cfg(feature = "opengl")]
pub fn gl_context(&self) -> Option<&GlContext> { pub fn gl_context(&self) -> Option<&GlContext> {
self.gl_context.as_ref() self.gl_context.as_ref()

View file

@ -101,7 +101,6 @@ impl<'a> Window<'a> {
/// Resize the window to the given size. The size is always in logical pixels. DPI scaling will /// Resize the window to the given size. The size is always in logical pixels. DPI scaling will
/// automatically be accounted for. /// automatically be accounted for.
#[cfg(any(target_os = "linux", target_os = "windows"))]
pub fn resize(&mut self, size: Size) { pub fn resize(&mut self, size: Size) {
self.window.resize(size); self.window.resize(size);
} }