Skip to content

Commit

Permalink
Add modifiers to PointerEvent
Browse files Browse the repository at this point in the history
Closes #2686
  • Loading branch information
ogoffart committed Jul 5, 2023
1 parent 66cf903 commit d1eb659
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ All notable changes to this project are documented in this file.
### Slint Language

- Added `clear-selection()` to `TextInput`, `LineEdit`, and `TextEdit`.
- The `PointerEvent` struct now has the `modifiers: KeyboardModifiers`

### Rust

Expand Down
1 change: 1 addition & 0 deletions docs/language/src/builtins/structs.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ This structure is generated and passed to the `pointer-event` callback of the `T
- `up`: The button was released.
- `cancel`: Another element or window took hold of the grab. This applies to all pressed button and the `button` is not relevant.
- **`button`** (_enum PointerEventButton_): The button that was pressed or released. `left`, `right`, `middle`, or `none`.
- **`modifiers`** (_KeyboardModifiers_): The keyboard modifiers pressed during the event

## `StandardListViewItem`

Expand Down
16 changes: 8 additions & 8 deletions internal/compiler/builtins.slint
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,19 @@ export component Text inherits Empty {
//-default_size_binding:implicit_size
}

export struct KeyboardModifiers {
//-name:slint::private_api::KeyboardModifiers
alt: bool,
control: bool,
shift: bool,
meta: bool,
}

export struct PointerEvent {
//-name:slint::private_api::PointerEvent
button: PointerEventButton,
kind: PointerEventKind,
modifiers: KeyboardModifiers,
}

export component TouchArea {
Expand All @@ -108,14 +116,6 @@ export component TouchArea {
//-default_size_binding:expands_to_parent_geometry
}

export struct KeyboardModifiers {
//-name:slint::private_api::KeyboardModifiers
alt: bool,
control: bool,
shift: bool,
meta: bool,
}

export struct KeyEvent {
//-name:slint::private_api::KeyEvent
text: string,
Expand Down
22 changes: 13 additions & 9 deletions internal/core/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ When adding an item or a property, it needs to be kept in sync with different pl
use crate::graphics::{Brush, Color, Point};
use crate::input::{
FocusEvent, FocusEventResult, InputEventFilterResult, InputEventResult, KeyEvent,
KeyEventResult, KeyEventType, MouseEvent,
KeyEventResult, KeyEventType, KeyboardModifiers, MouseEvent,
};
use crate::item_rendering::CachedRenderingData;
pub use crate::item_tree::ItemRc;
Expand Down Expand Up @@ -549,17 +549,19 @@ impl Item for TouchArea {
Self::FIELD_OFFSETS.pressed_y.apply_pin(self).set(position.y_length());
Self::FIELD_OFFSETS.pressed.apply_pin(self).set(true);
}
Self::FIELD_OFFSETS
.pointer_event
.apply_pin(self)
.call(&(PointerEvent { button, kind: PointerEventKind::Down },));
Self::FIELD_OFFSETS.pointer_event.apply_pin(self).call(&(PointerEvent {
button,
kind: PointerEventKind::Down,
modifiers: window_adapter.window().0.modifiers.get().into(),
},));
}
MouseEvent::Exit => {
Self::FIELD_OFFSETS.pressed.apply_pin(self).set(false);
if self.grabbed.replace(false) {
Self::FIELD_OFFSETS.pointer_event.apply_pin(self).call(&(PointerEvent {
button: PointerEventButton::Other,
kind: PointerEventKind::Cancel,
modifiers: window_adapter.window().0.modifiers.get().into(),
},));
}
}
Expand All @@ -568,10 +570,11 @@ impl Item for TouchArea {
if button == PointerEventButton::Left {
Self::FIELD_OFFSETS.pressed.apply_pin(self).set(false);
}
Self::FIELD_OFFSETS
.pointer_event
.apply_pin(self)
.call(&(PointerEvent { button, kind: PointerEventKind::Up },));
Self::FIELD_OFFSETS.pointer_event.apply_pin(self).call(&(PointerEvent {
button,
kind: PointerEventKind::Up,
modifiers: window_adapter.window().0.modifiers.get().into(),
},));
}
MouseEvent::Moved { .. } => {
return if self.grabbed.get() {
Expand Down Expand Up @@ -1442,6 +1445,7 @@ i_slint_common::for_each_enums!(declare_enums);
pub struct PointerEvent {
pub button: PointerEventButton,
pub kind: PointerEventKind,
pub modifiers: KeyboardModifiers,
}

#[cfg(feature = "ffi")]
Expand Down
2 changes: 1 addition & 1 deletion internal/interpreter/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ declare_value_struct_conversion!(struct i_slint_core::input::KeyboardModifiers {
declare_value_struct_conversion!(struct i_slint_core::input::KeyEvent { text, modifiers, ..Default::default() });
declare_value_struct_conversion!(struct i_slint_core::layout::LayoutInfo { min, max, min_percent, max_percent, preferred, stretch });
declare_value_struct_conversion!(struct i_slint_core::graphics::Point { x, y, ..Default::default()});
declare_value_struct_conversion!(struct i_slint_core::items::PointerEvent { kind, button });
declare_value_struct_conversion!(struct i_slint_core::items::PointerEvent { kind, button, modifiers });

/// Implement From / TryFrom for Value that convert an `enum` to/from `Value::EnumerationValue`
///
Expand Down
22 changes: 21 additions & 1 deletion tests/cases/elements/toucharea.slint
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ export component TestCase {
} else {
pointer-event-test += "???";
}
if (e.modifiers.control) {
pointer-event-test += "(ctrl)";
}
if (e.modifiers.shift) {
pointer-event-test += "(shift)";
}
if (e.modifiers.meta) {
pointer-event-test += "(meta)";
}
if (e.modifiers.alt) {
pointer-event-test += "(alt)";
}
}
}
}
Expand Down Expand Up @@ -94,7 +106,7 @@ assert_eq(instance.get_pointer_event_test(), "downleftclickupleft");
```rust
use slint::{platform::WindowEvent, platform::PointerEventButton, LogicalPosition};
use slint::{platform::WindowEvent, platform::PointerEventButton, platform::Key, LogicalPosition};
let instance = TestCase::new().unwrap();
Expand Down Expand Up @@ -134,6 +146,14 @@ assert_eq!(instance.get_touch1(), 1);
assert_eq!(instance.get_touch2(), 1);
assert_eq!(instance.get_touch3(), 1);
instance.set_pointer_event_test("".into());
slint_testing::send_keyboard_char(&instance, Key::Control.into(), true);
instance.window().dispatch_event(WindowEvent::PointerPressed { position: LogicalPosition::new(101.0, 104.0), button: PointerEventButton::Left });
slint_testing::send_keyboard_char(&instance, Key::Control.into(), false);
slint_testing::send_keyboard_char(&instance, Key::Shift.into(), true);
instance.window().dispatch_event(WindowEvent::PointerReleased { position: LogicalPosition::new(101.0, 104.0), button: PointerEventButton::Left });
assert_eq!(instance.get_pointer_event_test().as_str(), "downleft(ctrl)clickupleft(shift)");
```
```js
Expand Down

0 comments on commit d1eb659

Please sign in to comment.