mirror of
https://github.com/servo/servo.git
synced 2025-06-06 16:45:39 +00:00
Add simple fling implementation (#33219)
* Add simple fling implementation Add a simple fling implementation, which depends on a refresh tick from the embedder. Currently this refresh tick is only implemented for OpenHarmony (using the vsync signal). The fling implementation is very simple, without any fancy things like acceleration. This can be improved in the future. Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com> Signed-off-by: Jonathan Schwender <jonathan.schwender@huawei.com> * Multiply initial velocity with 2 This makes the experience much more snappy. Signed-off-by: Jonathan Schwender <jonathan.schwender@huawei.com> * address review comments Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com> * Rename constants and add todo Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com> * fmt Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com> * Add a few periods to make comments consistent Signed-off-by: Martin Robinson <mrobinson@igalia.com> --------- Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com> Signed-off-by: Jonathan Schwender <jonathan.schwender@huawei.com> Signed-off-by: Martin Robinson <mrobinson@igalia.com> Co-authored-by: Martin Robinson <mrobinson@igalia.com>
This commit is contained in:
parent
dcb9058fe3
commit
72971bd271
6 changed files with 117 additions and 24 deletions
|
@ -1896,6 +1896,16 @@ impl<Window: WindowMethods + ?Sized> IOCompositor<Window> {
|
|||
.push(ScrollZoomEvent::PinchZoom(magnification));
|
||||
}
|
||||
|
||||
/// On a Window refresh tick (e.g. vsync)
|
||||
pub fn on_vsync(&mut self) {
|
||||
if let Some(fling_action) = self.touch_handler.on_vsync() {
|
||||
self.on_scroll_window_event(
|
||||
ScrollLocation::Delta(fling_action.delta),
|
||||
fling_action.cursor,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn send_scroll_positions_to_layout_for_pipeline(&self, pipeline_id: &PipelineId) {
|
||||
let details = match self.pipeline_details.get(pipeline_id) {
|
||||
Some(details) => details,
|
||||
|
|
|
@ -3,14 +3,24 @@
|
|||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use euclid::{Point2D, Scale, Vector2D};
|
||||
use log::warn;
|
||||
use log::{debug, warn};
|
||||
use script_traits::{EventResult, TouchId};
|
||||
use style_traits::DevicePixel;
|
||||
use webrender_api::units::{DeviceIntPoint, LayoutVector2D};
|
||||
|
||||
use self::TouchState::*;
|
||||
|
||||
// TODO: All `_SCREEN_PX` units below are currently actually used as `DevicePixel`
|
||||
// without multiplying with the `hidpi_factor`. This should be fixed and the
|
||||
// constants adjusted accordingly.
|
||||
/// Minimum number of `DeviceIndependentPixel` to begin touch scrolling.
|
||||
const TOUCH_PAN_MIN_SCREEN_PX: f32 = 20.0;
|
||||
/// Factor by which the flinging velocity changes on each tick.
|
||||
const FLING_SCALING_FACTOR: f32 = 0.95;
|
||||
/// Minimum velocity required for transitioning to fling when panning ends.
|
||||
const FLING_MIN_SCREEN_PX: f32 = 3.0;
|
||||
/// Maximum velocity when flinging.
|
||||
const FLING_MAX_SCREEN_PX: f32 = 4000.0;
|
||||
|
||||
pub struct TouchHandler {
|
||||
pub state: TouchState,
|
||||
|
@ -30,9 +40,7 @@ impl TouchPoint {
|
|||
}
|
||||
|
||||
/// The states of the touch input state machine.
|
||||
///
|
||||
/// TODO: Add support for "flinging" (scrolling inertia)
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub enum TouchState {
|
||||
/// Not tracking any touch point
|
||||
Nothing,
|
||||
|
@ -45,7 +53,14 @@ pub enum TouchState {
|
|||
/// Contains the initial touch location.
|
||||
Touching,
|
||||
/// A single touch point is active and has started panning.
|
||||
Panning,
|
||||
Panning {
|
||||
velocity: Vector2D<f32, DevicePixel>,
|
||||
},
|
||||
/// No active touch points, but there is still scrolling velocity
|
||||
Flinging {
|
||||
velocity: Vector2D<f32, DevicePixel>,
|
||||
cursor: DeviceIntPoint,
|
||||
},
|
||||
/// A two-finger pinch zoom gesture is active.
|
||||
Pinching,
|
||||
/// A multi-touch gesture is in progress. Contains the number of active touch points.
|
||||
|
@ -67,6 +82,11 @@ pub enum TouchAction {
|
|||
NoAction,
|
||||
}
|
||||
|
||||
pub(crate) struct FlingAction {
|
||||
pub delta: LayoutVector2D,
|
||||
pub cursor: DeviceIntPoint,
|
||||
}
|
||||
|
||||
impl TouchHandler {
|
||||
pub fn new() -> Self {
|
||||
TouchHandler {
|
||||
|
@ -81,13 +101,36 @@ impl TouchHandler {
|
|||
|
||||
self.state = match self.state {
|
||||
Nothing => WaitingForScript,
|
||||
Touching | Panning => Pinching,
|
||||
Flinging { .. } => Touching,
|
||||
Touching | Panning { .. } => Pinching,
|
||||
WaitingForScript => WaitingForScript,
|
||||
DefaultPrevented => DefaultPrevented,
|
||||
Pinching | MultiTouch => MultiTouch,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn on_vsync(&mut self) -> Option<FlingAction> {
|
||||
let Flinging {
|
||||
velocity,
|
||||
ref cursor,
|
||||
} = &mut self.state
|
||||
else {
|
||||
return None;
|
||||
};
|
||||
if velocity.length().abs() < FLING_MIN_SCREEN_PX {
|
||||
self.state = Nothing;
|
||||
return None;
|
||||
}
|
||||
// TODO: Probably we should multiply with the current refresh rate (and divide on each frame)
|
||||
// or save a timestamp to account for a potentially changing display refresh rate.
|
||||
*velocity *= FLING_SCALING_FACTOR;
|
||||
debug_assert!(velocity.length() <= FLING_MAX_SCREEN_PX);
|
||||
Some(FlingAction {
|
||||
delta: LayoutVector2D::new(velocity.x, velocity.y),
|
||||
cursor: *cursor,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn on_touch_move(&mut self, id: TouchId, point: Point2D<f32, DevicePixel>) -> TouchAction {
|
||||
let idx = match self.active_touch_points.iter_mut().position(|t| t.id == id) {
|
||||
Some(i) => i,
|
||||
|
@ -105,16 +148,24 @@ impl TouchHandler {
|
|||
if delta.x.abs() > TOUCH_PAN_MIN_SCREEN_PX ||
|
||||
delta.y.abs() > TOUCH_PAN_MIN_SCREEN_PX
|
||||
{
|
||||
self.state = Panning;
|
||||
self.state = Panning {
|
||||
velocity: Vector2D::new(delta.x, delta.y),
|
||||
};
|
||||
TouchAction::Scroll(delta)
|
||||
} else {
|
||||
TouchAction::NoAction
|
||||
}
|
||||
},
|
||||
Panning => {
|
||||
Panning { ref mut velocity } => {
|
||||
let delta = point - old_point;
|
||||
// TODO: Probably we should track 1-3 more points and use a smarter algorithm
|
||||
*velocity += delta;
|
||||
*velocity /= 2.0;
|
||||
TouchAction::Scroll(delta)
|
||||
},
|
||||
Flinging { .. } => {
|
||||
unreachable!("Touch Move event received without preceding down.")
|
||||
},
|
||||
DefaultPrevented => TouchAction::DispatchEvent,
|
||||
Pinching => {
|
||||
let (d0, c0) = self.pinch_distance_and_center();
|
||||
|
@ -139,15 +190,14 @@ impl TouchHandler {
|
|||
action
|
||||
}
|
||||
|
||||
pub fn on_touch_up(&mut self, id: TouchId, _point: Point2D<f32, DevicePixel>) -> TouchAction {
|
||||
match self.active_touch_points.iter().position(|t| t.id == id) {
|
||||
Some(i) => {
|
||||
self.active_touch_points.swap_remove(i);
|
||||
},
|
||||
pub fn on_touch_up(&mut self, id: TouchId, point: Point2D<f32, DevicePixel>) -> TouchAction {
|
||||
let old = match self.active_touch_points.iter().position(|t| t.id == id) {
|
||||
Some(i) => Some(self.active_touch_points.swap_remove(i).point),
|
||||
None => {
|
||||
warn!("Got a touch up event for a non-active touch point");
|
||||
None
|
||||
},
|
||||
}
|
||||
};
|
||||
match self.state {
|
||||
Touching => {
|
||||
// FIXME: If the duration exceeds some threshold, send a contextmenu event instead.
|
||||
|
@ -155,14 +205,38 @@ impl TouchHandler {
|
|||
self.state = Nothing;
|
||||
TouchAction::Click
|
||||
},
|
||||
Nothing | Panning => {
|
||||
Nothing => {
|
||||
self.state = Nothing;
|
||||
TouchAction::NoAction
|
||||
},
|
||||
Panning { velocity } if velocity.length().abs() >= FLING_MIN_SCREEN_PX => {
|
||||
// TODO: point != old. Not sure which one is better to take as cursor for flinging.
|
||||
debug!(
|
||||
"Transitioning to Fling. Cursor is {point:?}. Old cursor was {old:?}. \
|
||||
Raw velocity is {velocity:?}."
|
||||
);
|
||||
debug_assert!((point.x as i64) < (i32::MAX as i64));
|
||||
debug_assert!((point.y as i64) < (i32::MAX as i64));
|
||||
let cursor = DeviceIntPoint::new(point.x as i32, point.y as i32);
|
||||
// Multiplying the initial velocity gives the fling a much more snappy feel
|
||||
// and serves well as a poor-mans acceleration algorithm.
|
||||
let velocity = (velocity * 2.0).with_max_length(FLING_MAX_SCREEN_PX);
|
||||
self.state = Flinging { velocity, cursor };
|
||||
TouchAction::NoAction
|
||||
},
|
||||
Panning { .. } => {
|
||||
self.state = Nothing;
|
||||
TouchAction::NoAction
|
||||
},
|
||||
Pinching => {
|
||||
self.state = Panning;
|
||||
self.state = Panning {
|
||||
velocity: Vector2D::new(0.0, 0.0),
|
||||
};
|
||||
TouchAction::NoAction
|
||||
},
|
||||
Flinging { .. } => {
|
||||
unreachable!("On touchup received, but already flinging.")
|
||||
},
|
||||
WaitingForScript | DefaultPrevented | MultiTouch => {
|
||||
if self.active_touch_points.is_empty() {
|
||||
self.state = Nothing;
|
||||
|
@ -184,11 +258,13 @@ impl TouchHandler {
|
|||
}
|
||||
match self.state {
|
||||
Nothing => {},
|
||||
Touching | Panning => {
|
||||
Touching | Panning { .. } | Flinging { .. } => {
|
||||
self.state = Nothing;
|
||||
},
|
||||
Pinching => {
|
||||
self.state = Panning;
|
||||
self.state = Panning {
|
||||
velocity: Vector2D::new(0.0, 0.0),
|
||||
};
|
||||
},
|
||||
WaitingForScript | DefaultPrevented | MultiTouch => {
|
||||
if self.active_touch_points.is_empty() {
|
||||
|
|
|
@ -128,6 +128,8 @@ pub enum EmbedderEvent {
|
|||
ReplaceNativeSurface(*mut c_void, DeviceIntSize),
|
||||
/// Sent when new Gamepad information is available.
|
||||
Gamepad(GamepadEvent),
|
||||
/// Vertical Synchronization tick
|
||||
Vsync,
|
||||
}
|
||||
|
||||
impl Debug for EmbedderEvent {
|
||||
|
@ -187,6 +189,7 @@ impl Debug for EmbedderEvent {
|
|||
EmbedderEvent::InvalidateNativeSurface => write!(f, "InvalidateNativeSurface"),
|
||||
EmbedderEvent::ReplaceNativeSurface(..) => write!(f, "ReplaceNativeSurface"),
|
||||
EmbedderEvent::Gamepad(..) => write!(f, "Gamepad"),
|
||||
EmbedderEvent::Vsync => write!(f, "Vsync"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -863,6 +863,9 @@ where
|
|||
warn!("Sending Gamepad event to constellation failed ({:?}).", e);
|
||||
}
|
||||
},
|
||||
EmbedderEvent::Vsync => {
|
||||
self.compositor.on_vsync();
|
||||
},
|
||||
}
|
||||
false
|
||||
}
|
||||
|
|
|
@ -233,6 +233,7 @@ mod to_servo {
|
|||
Self::InvalidateNativeSurface => target!("InvalidateNativeSurface"),
|
||||
Self::ReplaceNativeSurface(..) => target!("ReplaceNativeSurface"),
|
||||
Self::Gamepad(..) => target!("Gamepad"),
|
||||
Self::Vsync => target!("Vsync"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@ use ohos_sys::xcomponent::{
|
|||
OH_NativeXComponent_RegisterCallback, OH_NativeXComponent_TouchEvent,
|
||||
OH_NativeXComponent_TouchEventType,
|
||||
};
|
||||
use servo::compositing::windowing::EmbedderEvent;
|
||||
use servo::embedder_traits::PromptResult;
|
||||
use servo::euclid::Point2D;
|
||||
use servo::style::Zero;
|
||||
|
@ -146,12 +147,10 @@ impl ServoAction {
|
|||
panic!("Received Initialize event, even though servo is already initialized")
|
||||
},
|
||||
|
||||
Vsync => {
|
||||
servo.perform_updates().expect("Infallible");
|
||||
servo.present_if_needed();
|
||||
// Todo: perform_updates() (before or after present) if animating?
|
||||
Ok(())
|
||||
},
|
||||
Vsync => servo
|
||||
.process_event(EmbedderEvent::Vsync)
|
||||
.and_then(|()| servo.perform_updates())
|
||||
.and_then(|()| Ok(servo.present_if_needed())),
|
||||
};
|
||||
if let Err(e) = res {
|
||||
error!("Failed to do {self:?} with error {e}");
|
||||
|
@ -315,6 +314,7 @@ fn initialize_logging_once() {
|
|||
// Show GL errors by default.
|
||||
"canvas::webgl_thread",
|
||||
"compositing::compositor",
|
||||
"compositing::touch",
|
||||
"constellation::constellation",
|
||||
];
|
||||
for &module in &filters {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue