|
| 1 | +// Copyright © SixtyFPS GmbH <[email protected]> |
| 2 | +// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0 |
| 3 | + |
| 4 | +use std::cell::{OnceCell, RefCell}; |
| 5 | + |
| 6 | +use block2::RcBlock; |
| 7 | +use objc2::{define_class, msg_send, rc::Retained, DefinedClass, MainThreadMarker, MainThreadOnly}; |
| 8 | +use objc2_foundation::{NSDefaultRunLoopMode, NSObject, NSObjectProtocol, NSRect, NSRunLoop}; |
| 9 | +use objc2_quartz_core::{CADisplayLink, CATransaction}; |
| 10 | +use objc2_ui_kit::{UIView, UIViewAnimating as _, UIViewAnimationCurve, UIViewPropertyAnimator}; |
| 11 | + |
| 12 | +struct DisplayLinkTargetIvars { |
| 13 | + view: Retained<UIView>, |
| 14 | + callback: Box<dyn Fn(NSRect)>, |
| 15 | + animator: RefCell<Option<Retained<UIViewPropertyAnimator>>>, |
| 16 | + display_link: OnceCell<Retained<CADisplayLink>>, |
| 17 | +} |
| 18 | + |
| 19 | +define_class!( |
| 20 | + #[unsafe(super = NSObject)] |
| 21 | + #[thread_kind = MainThreadOnly] |
| 22 | + #[ivars = DisplayLinkTargetIvars] |
| 23 | + struct DisplayLinkTarget; |
| 24 | + |
| 25 | + unsafe impl NSObjectProtocol for DisplayLinkTarget {} |
| 26 | + |
| 27 | + impl DisplayLinkTarget { |
| 28 | + #[unsafe(method(tick:))] |
| 29 | + fn tick(&self, display_link: &CADisplayLink) { |
| 30 | + let this = self.ivars(); |
| 31 | + if let Some(layer) = unsafe { this.view.layer().presentationLayer() } { |
| 32 | + (this.callback)(layer.frame()); |
| 33 | + } |
| 34 | + let mut animator_ref = this.animator.borrow_mut(); |
| 35 | + if let Some(false) = animator_ref.as_ref().map(|animator| animator.isRunning()) { |
| 36 | + display_link.setPaused(true); |
| 37 | + *animator_ref = None; |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | +); |
| 42 | + |
| 43 | +impl DisplayLinkTarget { |
| 44 | + fn new( |
| 45 | + mtm: MainThreadMarker, |
| 46 | + view: Retained<UIView>, |
| 47 | + callback: impl Fn(NSRect) + 'static, |
| 48 | + ) -> Retained<Self> { |
| 49 | + let this = Self::alloc(mtm).set_ivars(DisplayLinkTargetIvars { |
| 50 | + view, |
| 51 | + callback: Box::new(callback), |
| 52 | + animator: Default::default(), |
| 53 | + display_link: OnceCell::new(), |
| 54 | + }); |
| 55 | + unsafe { msg_send![super(this), init] } |
| 56 | + } |
| 57 | + |
| 58 | + fn set_display_link(&self, display_link: Retained<CADisplayLink>) { |
| 59 | + self.ivars().display_link.set(display_link).unwrap(); |
| 60 | + } |
| 61 | + |
| 62 | + fn stop(&self) { |
| 63 | + let ivars = self.ivars(); |
| 64 | + ivars.display_link.get().unwrap().setPaused(true); |
| 65 | + if let Some(animator) = ivars.animator.borrow_mut().take() { |
| 66 | + animator.stopAnimation(true); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + fn start(&self, animator: Retained<UIViewPropertyAnimator>) { |
| 71 | + let ivars = self.ivars(); |
| 72 | + animator.startAnimation(); |
| 73 | + if let Some(old_animator) = ivars.animator.borrow_mut().replace(animator) { |
| 74 | + old_animator.stopAnimation(true); |
| 75 | + } |
| 76 | + ivars.display_link.get().unwrap().setPaused(false); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/// A helper to sample keyboard animation curves. |
| 81 | +/// Since the iOS keyboard animation is not directly accessible, we create a hidden UIView |
| 82 | +/// and animate its frame using the same parameters as the keyboard animation. |
| 83 | +/// The animation curve used by iOS is private and not documented, but using UIViewPropertyAnimator |
| 84 | +/// with the same duration and curve produces identical results. |
| 85 | +pub(crate) struct KeyboardCurveSampler { |
| 86 | + view: Retained<UIView>, |
| 87 | + target: Retained<DisplayLinkTarget>, |
| 88 | + mtm: MainThreadMarker, |
| 89 | +} |
| 90 | + |
| 91 | +impl KeyboardCurveSampler { |
| 92 | + pub(crate) fn new(content_view: &UIView, sampler: impl Fn(NSRect) + 'static) -> Self { |
| 93 | + let mtm = MainThreadMarker::new().expect("Must be created on main thread"); |
| 94 | + let view = UIView::new(mtm); |
| 95 | + content_view.addSubview(&view); |
| 96 | + |
| 97 | + let target = DisplayLinkTarget::new(mtm, view.clone(), sampler); |
| 98 | + let display_link = |
| 99 | + unsafe { CADisplayLink::displayLinkWithTarget_selector(&target, objc2::sel!(tick:)) }; |
| 100 | + |
| 101 | + unsafe { |
| 102 | + display_link.addToRunLoop_forMode(&NSRunLoop::currentRunLoop(), NSDefaultRunLoopMode); |
| 103 | + } |
| 104 | + |
| 105 | + display_link.setPaused(true); |
| 106 | + target.set_display_link(display_link); |
| 107 | + |
| 108 | + Self { view, target, mtm } |
| 109 | + } |
| 110 | + |
| 111 | + pub(crate) fn start( |
| 112 | + &self, |
| 113 | + duration: f64, |
| 114 | + curve: UIViewAnimationCurve, |
| 115 | + begin: NSRect, |
| 116 | + end: NSRect, |
| 117 | + ) { |
| 118 | + CATransaction::begin(); |
| 119 | + CATransaction::setDisableActions(true); |
| 120 | + self.target.stop(); |
| 121 | + self.view.setFrame(begin); |
| 122 | + CATransaction::commit(); |
| 123 | + |
| 124 | + let view = self.view.clone(); |
| 125 | + let animations = RcBlock::new(move || { |
| 126 | + view.setFrame(end); |
| 127 | + }); |
| 128 | + |
| 129 | + let animator = UIViewPropertyAnimator::initWithDuration_curve_animations( |
| 130 | + UIViewPropertyAnimator::alloc(self.mtm), |
| 131 | + duration, // duration is already in seconds |
| 132 | + curve, |
| 133 | + Some(&animations), |
| 134 | + ); |
| 135 | + |
| 136 | + self.target.start(animator); |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +impl Drop for DisplayLinkTargetIvars { |
| 141 | + fn drop(&mut self) { |
| 142 | + if let Some(display_link) = self.display_link.get() { |
| 143 | + display_link.invalidate(); |
| 144 | + } |
| 145 | + if let Some(animator) = self.animator.borrow_mut().take() { |
| 146 | + animator.stopAnimation(true); |
| 147 | + } |
| 148 | + self.view.removeFromSuperview(); |
| 149 | + } |
| 150 | +} |
0 commit comments