#![allow(clippy::module_name_repetitions)]
#[cfg(target_os = "macos")]
#[repr(C)]
#[derive(Clone, Copy)]
struct NsPoint {
x: f64,
y: f64,
}
#[cfg(target_os = "macos")]
#[repr(C)]
#[derive(Clone, Copy)]
struct NsSize {
width: f64,
height: f64,
}
#[cfg(target_os = "macos")]
#[repr(C)]
#[derive(Clone, Copy)]
struct NsRect {
origin: NsPoint,
size: NsSize,
}
#[cfg(target_os = "macos")]
pub fn reanchor_to_superview_top(handle: raw_window_handle::RawWindowHandle) {
use objc::{msg_send, sel, sel_impl};
let view_ptr = match handle {
raw_window_handle::RawWindowHandle::AppKit(h) => h.ns_view,
_ => return,
};
if view_ptr.is_null() {
return;
}
unsafe {
let view = view_ptr.cast::<objc::runtime::Object>();
let superview: *mut objc::runtime::Object = msg_send![view, superview];
if superview.is_null() {
return;
}
let parent_frame: NsRect = msg_send![superview, frame];
let child_frame: NsRect = msg_send![view, frame];
let new_y = parent_frame.size.height - child_frame.size.height;
if (new_y - child_frame.origin.y).abs() < f64::EPSILON {
return;
}
let new_origin = NsPoint {
x: child_frame.origin.x,
y: new_y,
};
let _: () = msg_send![view, setFrameOrigin: new_origin];
}
}
#[cfg(not(target_os = "macos"))]
pub fn reanchor_to_superview_top(_handle: raw_window_handle::RawWindowHandle) {}
#[cfg(target_os = "macos")]
#[must_use]
pub fn should_skip_frame(handle: raw_window_handle::RawWindowHandle) -> bool {
use objc::{msg_send, sel, sel_impl};
let view_ptr = match handle {
raw_window_handle::RawWindowHandle::AppKit(h) => h.ns_view,
_ => return false,
};
if view_ptr.is_null() {
return true;
}
unsafe {
let view = view_ptr.cast::<objc::runtime::Object>();
let window: *mut objc::runtime::Object = msg_send![view, window];
if window.is_null() {
return true;
}
let state: u64 = msg_send![window, occlusionState];
state & (1 << 1) == 0
}
}
#[cfg(target_os = "windows")]
#[must_use]
pub fn should_skip_frame(handle: raw_window_handle::RawWindowHandle) -> bool {
unsafe extern "system" {
fn IsWindowVisible(hwnd: *mut std::ffi::c_void) -> i32;
fn IsIconic(hwnd: *mut std::ffi::c_void) -> i32;
}
let hwnd = match handle {
raw_window_handle::RawWindowHandle::Win32(h) => h.hwnd,
_ => return false,
};
if hwnd.is_null() {
return true;
}
unsafe { IsWindowVisible(hwnd) == 0 || IsIconic(hwnd) != 0 }
}
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
#[must_use]
pub fn should_skip_frame(_handle: raw_window_handle::RawWindowHandle) -> bool {
false
}
#[cfg(target_os = "macos")]
pub fn reanchor_all_children_to_top(parent: *mut std::ffi::c_void) {
use objc::runtime::Object;
use objc::{msg_send, sel, sel_impl};
if parent.is_null() {
return;
}
unsafe {
let parent_obj = parent.cast::<Object>();
let window: *mut Object = msg_send![parent_obj, window];
if window.is_null() {
return;
}
let parent_frame: NsRect = msg_send![parent_obj, frame];
let subviews: *mut Object = msg_send![parent_obj, subviews];
if subviews.is_null() {
return;
}
let count: usize = msg_send![subviews, count];
for i in 0..count {
let child: *mut Object = msg_send![subviews, objectAtIndex: i];
if child.is_null() {
continue;
}
let child_frame: NsRect = msg_send![child, frame];
let new_y = parent_frame.size.height - child_frame.size.height;
if (new_y - child_frame.origin.y).abs() < f64::EPSILON {
continue;
}
let new_origin = NsPoint {
x: child_frame.origin.x,
y: new_y,
};
let _: () = msg_send![child, setFrameOrigin: new_origin];
}
}
}
#[cfg(not(target_os = "macos"))]
pub fn reanchor_all_children_to_top(_parent: *mut std::ffi::c_void) {}