Coder Social home page Coder Social logo

Comments (3)

matthewjrichey avatar matthewjrichey commented on June 23, 2024

As a random member of the public, I do not have push access to this public repository (which is understandable), so I could not create a pull request.

Here is a diff that captures my fix for the issue.

diff --git a/internal/backends/android-activity/androidwindowadapter.rs b/internal/backends/android-activity/androidwindowadapter.rs
index 10e98dff8..ff26cd9ea 100644
--- a/internal/backends/android-activity/androidwindowadapter.rs
+++ b/internal/backends/android-activity/androidwindowadapter.rs
@@ -3,7 +3,9 @@
 
 use super::*;
 use crate::javahelper::{print_jni_error, JavaHelper};
-use android_activity::input::{InputEvent, KeyAction, Keycode, MotionAction, MotionEvent};
+use android_activity::input::{
+    ButtonState, InputEvent, KeyAction, Keycode, MotionAction, MotionEvent,
+};
 use android_activity::{InputStatus, MainEvent, PollEvent};
 use i_slint_core::api::{LogicalPosition, PhysicalPosition, PhysicalSize, PlatformError, Window};
 use i_slint_core::items::ColorScheme;
@@ -40,6 +42,7 @@ pub struct AndroidWindowAdapter {
     pub(crate) show_cursor_handles: Cell<bool>,
 
     long_press: RefCell<Option<LongPressDetection>>,
+    last_pressed_state: Cell<ButtonState>,
 }
 
 impl WindowAdapter for AndroidWindowAdapter {
@@ -187,6 +190,7 @@ pub fn new(app: AndroidApp) -> Rc<Self> {
             offset: Default::default(),
             show_cursor_handles: Cell::new(false),
             long_press: RefCell::default(),
+            last_pressed_state: Cell::new(ButtonState(0)),
         })
     }
 
@@ -281,7 +285,7 @@ fn process_inputs(&self) -> Result<(), android_activity::error::AppError> {
                         self.window.dispatch_event(WindowEvent::PointerPressed {
                             position: position_for_event(motion_event, self.offset.get())
                                 .to_logical(self.window.scale_factor()),
-                            button: button_for_event(motion_event),
+                            button: button_for_event(motion_event, &self.last_pressed_state),
                         });
                         InputStatus::Handled
                     }
@@ -308,7 +312,7 @@ fn process_inputs(&self) -> Result<(), android_activity::error::AppError> {
                         self.window.dispatch_event(WindowEvent::PointerReleased {
                             position: position_for_event(motion_event, self.offset.get())
                                 .to_logical(self.window.scale_factor()),
-                            button: button_for_event(motion_event),
+                            button: button_for_event(motion_event, &self.last_pressed_state),
                         });
                         InputStatus::Handled
                     }
@@ -456,13 +460,51 @@ fn position_for_event(motion_event: &MotionEvent, offset: PhysicalPosition) -> P
     })
 }
 
-fn button_for_event(motion_event: &MotionEvent) -> PointerEventButton {
-    match motion_event.action_button() {
-        android_activity::input::Button::Primary => PointerEventButton::Left,
-        android_activity::input::Button::Secondary => PointerEventButton::Right,
-        android_activity::input::Button::Tertiary => PointerEventButton::Middle,
-        _ => PointerEventButton::Other,
+fn button_for_event(
+    motion_event: &MotionEvent,
+    last_pressed_cell: &Cell<ButtonState>,
+) -> PointerEventButton {
+    //
+    // The motion_event API has a method called action_button() which can be used to directly
+    // determine the button associated with the event. However, the disadvantage of using the
+    // action_button() API is that it relies on NDK 33 or higher, which implies that the output
+    // application will only run on Android 13 or higher.
+    //
+    // This functionally equivalent method of computing the action button relies on the
+    // button_state() call from the motion event, rather than action_button(). It is a bit more
+    // complex than using action_button() directly, since the previous button state must be
+    // tracked and used in the calculation for computing which button was toggled. However, this
+    // will run on Android 12 (and possibly lower).
+    //
+    // See here for further discussion:
+    //
+    // https://stackoverflow.com/questions/75718566/amotionevent-getbuttonstate-returns-0-for-every-button-during-mouse-button-relea
+    //
+    let cur_pressed_state = motion_event.button_state();
+    let last_pressed_state = last_pressed_cell.get();
+    let toggled = match motion_event.action() {
+        MotionAction::ButtonPress => {
+            last_pressed_cell.set(cur_pressed_state);
+            ButtonState((last_pressed_state.0 ^ cur_pressed_state.0) & cur_pressed_state.0)
+        }
+        MotionAction::ButtonRelease => {
+            last_pressed_cell.set(cur_pressed_state);
+            ButtonState((last_pressed_state.0 ^ cur_pressed_state.0) & last_pressed_state.0)
+        }
+        _ => ButtonState(0),
+    };
+
+    // if multiple buttons toggled, primary takes precedence, then secondary, etc.
+    if toggled.primary() {
+        return PointerEventButton::Left;
+    }
+    if toggled.secondary() {
+        return PointerEventButton::Right;
+    }
+    if toggled.teriary() {
+        return PointerEventButton::Middle;
     }
+    return PointerEventButton::Other;
 }
 
 fn map_key_event(key_event: &android_activity::input::KeyEvent) -> Option<WindowEvent> {

from slint.

hunger avatar hunger commented on June 23, 2024

I took the liberty to turn this diff into a PR here: #5047 for your review-conveninece.

The code looks sensible to me, but I have no Android setup here (nor a Android 12 device) to test this with.

@matthewjrichey: You can clone the repo, push your code into your own clone and then create a PR from that. This approach is not limited to Slint, it works for any project on github, just in case you run into the situation again.

from slint.

hunger avatar hunger commented on June 23, 2024

Thank you for your time and contribution. I think this is small enough to count as a "simple fix" and not need the full CLA dance, which my PR goes around...

from slint.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.