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 5 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ rls/
*.js
#*#
.DS_Store
.cargo/config.toml
jpedrick marked this conversation as resolved.
Show resolved Hide resolved
38 changes: 37 additions & 1 deletion examples/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use softbuffer::{Context, Surface};

use winit::application::ApplicationHandler;
use winit::dpi::{LogicalSize, PhysicalPosition, PhysicalSize};
use winit::event::{DeviceEvent, DeviceId, Ime, WindowEvent};
use winit::event::{DeviceEvent, DeviceId, Ime, TouchPhase, WindowEvent};
use winit::event::{MouseButton, MouseScrollDelta};
use winit::event_loop::{ActiveEventLoop, EventLoop};
use winit::keyboard::{Key, ModifiersState};
Expand Down 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,35 @@ impl ApplicationHandler<UserEvent> for Application {
println!("Rotated clockwise {delta:.5} (now: {rotated:.5})");
}
}
WindowEvent::PanGesture { delta, phase, .. } => {
// "delta" is the total change since "TouchPhase::Started"
// For ios this is the same for rotation and pinch
match phase {
TouchPhase::Started => {
window.last_panned_delta.x = delta.x;
window.last_panned_delta.y = delta.y;
println!("Start pan: (now: {:?})", window.panned);
}
TouchPhase::Moved => {
let dx = delta.x - window.last_panned_delta.x;
let dy = delta.y - window.last_panned_delta.y;
window.panned.x += delta.x;
window.panned.y += delta.y;
println!("Panned ({dx:.5}, {dy:.5})) (now: {:?})", window.panned);
}
TouchPhase::Ended => {
println!(
"Pan ended total movement: {delta:?} (now: {:?})",
window.panned
);
}
TouchPhase::Cancelled => {
window.panned.x -= delta.x;
window.panned.y -= delta.y;
println!("Pan cancelled total: {delta:?} (now: {:?})", window.panned);
}
}
}
WindowEvent::DoubleTapGesture { .. } => {
println!("Smart zoom");
}
Expand Down Expand Up @@ -489,6 +519,10 @@ struct WindowState {
zoom: f64,
/// The amount of rotation of the window.
rotated: f32,
/// The amount of pan of the window.
panned: PhysicalPosition<f32>,
/// The last panned delta we received.
last_panned_delta: PhysicalPosition<f32>,

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

Expand Down
1 change: 1 addition & 0 deletions src/changelog/unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@
- 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: For IOS/UIKit Support UIGestureRecognizerDelegate for simultaneous gesture input
jpedrick marked this conversation as resolved.
Show resolved Hide resolved
19 changes: 19 additions & 0 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,22 @@ pub enum WindowEvent {
///
/// This value may be NaN.
delta: f64,
velocity: f32,
jpedrick marked this conversation as resolved.
Show resolved Hide resolved
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,
///
/// This value may be NaN.
delta: PhysicalPosition<f32>,
velocity: PhysicalPosition<f32>,
phase: TouchPhase,
jpedrick marked this conversation as resolved.
Show resolved Hide resolved
},

Expand Down Expand Up @@ -352,6 +368,7 @@ pub enum WindowEvent {
RotationGesture {
device_id: DeviceId,
delta: f32,
velocity: f32,
jpedrick marked this conversation as resolved.
Show resolved Hide resolved
phase: TouchPhase,
},

Expand Down Expand Up @@ -1089,12 +1106,14 @@ mod tests {
with_window_event(PinchGesture {
device_id: did,
delta: 0.0,
velocity: 0.0,
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: 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
57 changes: 55 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 set_delegate(&self, delegate: &ProtocolObject<dyn UIGestureRecognizerDelegate>);

jpedrick marked this conversation as resolved.
Show resolved Hide resolved
#[method_id(delegate)]
pub fn get_delegate(&self) -> Id<ProtocolObject<dyn UIGestureRecognizerDelegate>>;
}
);

Expand Down Expand Up @@ -119,3 +131,44 @@ 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 translation_in_view(&self, view: &UIView) -> CGPoint;

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

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

unsafe impl Encode for UIPanGestureRecognizer {
const ENCODING: Encoding = Encoding::Object;
}

jpedrick marked this conversation as resolved.
Show resolved Hide resolved
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