Merge debug/animation-timings; support ability for drawing sublayers as one and control of layer redraw policy

This commit is contained in:
Ryan McGrath 2023-08-01 00:18:55 -07:00
parent d417289f92
commit 186a9a6c3f
No known key found for this signature in database
GPG key ID: DA6CBD9233593DEA
2 changed files with 60 additions and 2 deletions

View file

@ -7,7 +7,7 @@
use cacao::color::Color;
use cacao::layout::{Layout, LayoutConstraint, LayoutConstraintAnimatorProxy};
use cacao::view::{View, ViewAnimatorProxy};
use cacao::view::{View, ViewAnimatorProxy, LayerContentsRedrawPolicy};
use cacao::appkit::menu::Menu;
use cacao::appkit::window::{Window, WindowConfig, WindowDelegate};
@ -104,8 +104,15 @@ impl WindowDelegate for AppWindow {
window.set_title("Animation Example (Use W/A/S/D to change state!)");
window.set_minimum_content_size(300., 300.);
self.blue.set_contents_redraw_policy(LayerContentsRedrawPolicy::OnSetNeedsDisplay);
self.red.set_contents_redraw_policy(LayerContentsRedrawPolicy::OnSetNeedsDisplay);
self.green.set_contents_redraw_policy(LayerContentsRedrawPolicy::OnSetNeedsDisplay);
self.content.set_contents_redraw_policy(LayerContentsRedrawPolicy::OnSetNeedsDisplay);
window.set_content_view(&self.content);
self.content.set_can_draw_subviews_into_layer(true);
let blue_frame = apply_styles(&self.blue, &self.content, Color::SystemBlue, 0);
let red_frame = apply_styles(&self.red, &self.content, Color::SystemRed, 1);
let green_frame = apply_styles(&self.green, &self.content, Color::SystemGreen, 2);

View file

@ -46,7 +46,7 @@ use objc::runtime::{Class, Object};
use objc::{msg_send, sel, sel_impl};
use crate::color::Color;
use crate::foundation::{id, nil, NSArray, NSString, NO, YES};
use crate::foundation::{id, nil, NSArray, NSInteger, NSString, NO, YES};
use crate::layer::Layer;
use crate::layout::Layout;
use crate::objc_access::ObjcAccess;
@ -318,6 +318,32 @@ impl<T> View<T> {
let _: () = msg_send![&*obj, setBackgroundColor: color];
});
}
/// A setter for `[NSView layerContentsRedrawPolicy]`.
///
/// For more information, consult:
///
/// [https://developer.apple.com/documentation/appkit/nsview/1483514-layercontentsredrawpolicy?language=objc](https://developer.apple.com/documentation/appkit/nsview/1483514-layercontentsredrawpolicy?language=objc)
#[cfg(feature = "appkit")]
pub fn set_contents_redraw_policy(&self, policy: LayerContentsRedrawPolicy) {
self.objc.with_mut(|obj| unsafe {
let policy = policy.to_nsinteger();
let _: () = msg_send![obj, setLayerContentsRedrawPolicy:policy];
});
}
/// Mark all child layers as being able to be drawn into a single CALayer. This can be useful
/// for moments when you need to lower your total layer count, which can impair composition
/// time.
#[cfg(feature = "appkit")]
pub fn set_can_draw_subviews_into_layer(&self, can: bool) {
self.objc.with_mut(|obj| unsafe {
let _: () = msg_send![&*obj, setCanDrawSubviewsIntoLayer:match can {
true => YES,
false => NO
}];
});
}
}
impl<T> ObjcAccess for View<T> {
@ -347,3 +373,28 @@ impl<T> Drop for View<T> {
}
}
}
/// Variants describing what an underlying NSView layer redraw policy should be.
#[cfg(feature = "appkit")]
#[derive(Debug)]
pub enum LayerContentsRedrawPolicy {
Never,
OnSetNeedsDisplay,
DuringViewResize,
BeforeViewResize,
Crossfade
}
#[cfg(feature = "appkit")]
impl LayerContentsRedrawPolicy {
/// Mapping required for ObjC setters.
pub fn to_nsinteger(&self) -> NSInteger {
match self {
Self::Never => 0,
Self::OnSetNeedsDisplay => 1,
Self::DuringViewResize => 2,
Self::BeforeViewResize => 3,
Self::Crossfade => 4
}
}
}