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

Add UIGestureRecognizerDelegate and PanGestureRecogniser plus default… #3597

Merged
merged 18 commits into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ run-wasm = ["run", "--release", "--package", "run-wasm", "--"]
[build]
rustflags = ["--cfg=unreleased_changelogs"]
rustdocflags = ["--cfg=unreleased_changelogs"]

jpedrick marked this conversation as resolved.
Show resolved Hide resolved
9 changes: 9 additions & 0 deletions examples/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ impl Application {
window.recognize_doubletap_gesture(true);
window.recognize_pinch_gesture(true);
window.recognize_rotation_gesture(true);
window.recognize_pan_gesture(true);
}

let window_state = WindowState::new(self, window)?;
Expand Down Expand Up @@ -414,6 +415,11 @@ impl ApplicationHandler<UserEvent> for Application {
println!("Rotated clockwise {delta:.5} (now: {rotated:.5})");
}
}
WindowEvent::PanGesture { delta, phase, .. } => {
window.panned.x += delta.x;
window.panned.y += delta.y;
println!("Panned ({delta:?})) (now: {:?}), {phase:?}", window.panned);
}
WindowEvent::DoubleTapGesture { .. } => {
println!("Smart zoom");
}
Expand Down Expand Up @@ -489,6 +495,8 @@ struct WindowState {
zoom: f64,
/// The amount of rotation of the window.
rotated: f32,
/// The amount of pan of the window.
panned: PhysicalPosition<f32>,

#[cfg(macos_platform)]
option_as_alt: OptionAsAlt,
Expand Down Expand Up @@ -532,6 +540,7 @@ impl WindowState {
modifiers: Default::default(),
occluded: Default::default(),
rotated: Default::default(),
panned: Default::default(),
zoom: Default::default(),
};

Expand Down
4 changes: 4 additions & 0 deletions src/changelog/unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@
- Add `Window::default_attributes` to get default `WindowAttributes`.
- `log` has been replaced with `tracing`. The old behavior can be emulated by setting the `log` feature on the `tracing` crate.
- On Windows, confine cursor to center of window when grabbed and hidden.
- On iOS: Support UIGestureRecognizerDelegate for simultaneous gesture input
- On iOS: Support UIPanGestureRecognizer
- On iOS: Modifies UIRotationGestureRecognizer handing code to work identically to macOS rotation(sends change in delta instead of velocity)
- On iOS: Modifies UIPinchGestureRecognizer handing code to work identically to macOS magnify(sends change in delta instead of velocity)
33 changes: 33 additions & 0 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,25 @@ pub enum WindowEvent {
///
/// This value may be NaN.
delta: f64,
/// Only available on **iOS**.
/// https://developer.apple.com/documentation/uikit/uipinchgesturerecognizer/1622233-velocity?language=objc
velocity: Option<f32>,
phase: TouchPhase,
},

/// Two-finger pan gesture
///
/// ## Platform-specific
///
/// - Only available on **iOS**.
/// - On iOS, not recognized by default. It must be enabled when needed.
PanGesture {
device_id: DeviceId,
/// Change in pixels of pan gesture from last update.
delta: PhysicalPosition<f32>,
/// Velocity of pan (delta/time?). Only available on **iOS**
/// https://developer.apple.com/documentation/uikit/uipangesturerecognizer/1621209-velocityinview?language=objc
velocity: PhysicalPosition<f32>,
jpedrick marked this conversation as resolved.
Show resolved Hide resolved
phase: TouchPhase,
},

Expand Down Expand Up @@ -351,7 +370,12 @@ pub enum WindowEvent {
/// - On iOS, not recognized by default. It must be enabled when needed.
RotationGesture {
device_id: DeviceId,
/// change in rotation in degrees
delta: f32,
/// Rotation velocity in degrees. Only available on **iOS**.
/// Units differ from UIKit as velocity is converted from radians to degrees.
/// https://developer.apple.com/documentation/uikit/uirotationgesturerecognizer/1624335-velocity?language=objc
velocity: Option<f32>,
phase: TouchPhase,
},

Expand Down Expand Up @@ -1029,6 +1053,7 @@ impl PartialEq for InnerSizeWriter {

#[cfg(test)]
mod tests {
use crate::dpi::PhysicalPosition;
use crate::event;
use std::collections::{BTreeSet, HashSet};

Expand Down Expand Up @@ -1089,12 +1114,20 @@ mod tests {
with_window_event(PinchGesture {
device_id: did,
delta: 0.0,
velocity: None,
phase: event::TouchPhase::Started,
});
with_window_event(DoubleTapGesture { device_id: did });
jpedrick marked this conversation as resolved.
Show resolved Hide resolved
with_window_event(RotationGesture {
device_id: did,
delta: 0.0,
velocity: None,
phase: event::TouchPhase::Started,
});
with_window_event(PanGesture {
device_id: did,
delta: PhysicalPosition::<f32>::new(0.0, 0.0),
velocity: PhysicalPosition::<f32>::new(0.0, 0.0),
phase: event::TouchPhase::Started,
});
with_window_event(TouchpadPressure {
Expand Down
11 changes: 11 additions & 0 deletions src/platform/ios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ pub trait WindowExtIOS {
/// The default is to not recognize gestures.
fn recognize_pinch_gesture(&self, should_recognize: bool);

/// Sets whether the [`Window`] should recognize pan gestures.
///
/// The default is to not recognize gestures.
fn recognize_pan_gesture(&self, should_recognize: bool);

/// Sets whether the [`Window`] should recognize double tap gestures.
///
/// The default is to not recognize gestures.
Expand Down Expand Up @@ -212,6 +217,12 @@ impl WindowExtIOS for Window {
.maybe_queue_on_main(move |w| w.recognize_pinch_gesture(should_recognize));
}

#[inline]
fn recognize_pan_gesture(&self, should_recognize: bool) {
self.window
.maybe_queue_on_main(move |w| w.recognize_pan_gesture(should_recognize));
}

#[inline]
fn recognize_doubletap_gesture(&self, should_recognize: bool) {
self.window
Expand Down
53 changes: 51 additions & 2 deletions src/platform_impl/ios/uikit/gesture_recognizer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use icrate::Foundation::{CGFloat, NSInteger, NSObject, NSUInteger};
use super::UIView;
use icrate::Foundation::{CGFloat, CGPoint, NSInteger, NSObject, NSUInteger};
use objc2::{
encode::{Encode, Encoding},
extern_class, extern_methods, mutability, ClassType,
extern_class, extern_methods, extern_protocol, mutability,
rc::Id,
runtime::{NSObjectProtocol, ProtocolObject},
ClassType, ProtocolType,
};

// https://developer.apple.com/documentation/uikit/uigesturerecognizer
Expand All @@ -19,6 +23,14 @@ extern_methods!(
unsafe impl UIGestureRecognizer {
#[method(state)]
pub fn state(&self) -> UIGestureRecognizerState;

/// (delegate)[https://developer.apple.com/documentation/uikit/uigesturerecognizer/1624207-delegate?language=objc]
/// @property(nullable, nonatomic, weak) id<UIGestureRecognizerDelegate> delegate;
#[method(setDelegate:)]
pub fn setDelegate(&self, delegate: &ProtocolObject<dyn UIGestureRecognizerDelegate>);

#[method_id(delegate)]
pub fn delegate(&self) -> Id<ProtocolObject<dyn UIGestureRecognizerDelegate>>;
}
);

Expand Down Expand Up @@ -119,3 +131,40 @@ extern_methods!(
unsafe impl Encode for UITapGestureRecognizer {
const ENCODING: Encoding = Encoding::Object;
}

// https://developer.apple.com/documentation/uikit/uipangesturerecognizer
extern_class!(
#[derive(Debug, PartialEq, Eq, Hash)]
pub(crate) struct UIPanGestureRecognizer;

unsafe impl ClassType for UIPanGestureRecognizer {
type Super = UIGestureRecognizer;
type Mutability = mutability::InteriorMutable;
}
);

extern_methods!(
unsafe impl UIPanGestureRecognizer {
#[method(translationInView:)]
pub fn translationInView(&self, view: &UIView) -> CGPoint;

#[method(setTranslation:inView:)]
pub fn setTranslationInView(&self, translation: CGPoint, view: &UIView);

#[method(velocityInView:)]
pub fn velocityInView(&self, view: &UIView) -> CGPoint;
}
);

extern_protocol!(
/// (@protocol UIGestureRecognizerDelegate)[https://developer.apple.com/documentation/uikit/uigesturerecognizerdelegate?language=objc]
pub(crate) unsafe trait UIGestureRecognizerDelegate: NSObjectProtocol {}

unsafe impl ProtocolType for dyn UIGestureRecognizerDelegate {
const NAME: &'static str = "UIGestureRecognizerDelegate";
}
);

unsafe impl Encode for dyn UIGestureRecognizerDelegate {
const ENCODING: Encoding = Encoding::Object;
}
5 changes: 3 additions & 2 deletions src/platform_impl/ios/uikit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ pub(crate) use self::device::{UIDevice, UIUserInterfaceIdiom};
pub(crate) use self::event::UIEvent;
pub(crate) use self::geometry::UIRectEdge;
pub(crate) use self::gesture_recognizer::{
UIGestureRecognizer, UIGestureRecognizerState, UIPinchGestureRecognizer,
UIRotationGestureRecognizer, UITapGestureRecognizer,
UIGestureRecognizer, UIGestureRecognizerDelegate, UIGestureRecognizerState,
UIPanGestureRecognizer, UIPinchGestureRecognizer, UIRotationGestureRecognizer,
UITapGestureRecognizer,
};
pub(crate) use self::responder::UIResponder;
pub(crate) use self::screen::{UIScreen, UIScreenOverscanCompensation};
Expand Down