TUI: use constructor instead of deprecated fields

This commit is contained in:
Reinis Mazeiks committed 2022-05-07 10:49:49 +03:00
1 parent c35dddc203
commit 9c6cd42ca2
2 files changed
+44 -33

No files matched your search

+7 -5
View File
@@ -9,7 +9,6 @@ pub mod on {
use crate::input::{ use crate::input::{
decode_mouse_button_set, encode_mouse_button_set, MouseButton, MouseButtonSet, decode_mouse_button_set, encode_mouse_button_set, MouseButton, MouseButtonSet,
}; };
use enumset::EnumSet;
use keyboard_types::Modifiers; use keyboard_types::Modifiers;
use std::collections::HashMap; use std::collections::HashMap;
@@ -570,9 +569,12 @@ pub mod on {
} }
impl MouseData { impl MouseData {
/// Construct MouseData with the specified properties
///
/// Note: the current implementation truncates coordinates. In the future, when we change the internal representation, it may also support a fractional part.
pub fn new( pub fn new(
coordinates: Coordinates, coordinates: Coordinates,
trigger_button: MouseButton, trigger_button: Option<MouseButton>,
held_buttons: MouseButtonSet, held_buttons: MouseButtonSet,
modifiers: Modifiers, modifiers: Modifiers,
) -> Self { ) -> Self {
@@ -593,7 +595,7 @@ pub mod on {
meta_key, meta_key,
shift_key, shift_key,
button: trigger_button.into_web_code(), button: trigger_button.map_or(0, |b| b.into_web_code()),
buttons: encode_mouse_button_set(held_buttons), buttons: encode_mouse_button_set(held_buttons),
client_x, client_x,
@@ -670,9 +672,9 @@ pub mod on {
/// ///
// todo the following is kind of bad; should we just return None when the trigger_button is unreliable (and frankly irrelevant)? i guess we would need the event_type here // todo the following is kind of bad; should we just return None when the trigger_button is unreliable (and frankly irrelevant)? i guess we would need the event_type here
/// This is only guaranteed to indicate which button was pressed during events caused by pressing or releasing a button. As such, it is not reliable for events such as mouseenter, mouseleave, mouseover, mouseout, or mousemove. For example, a value of MouseButton::Primary may also indicate that no button was pressed. /// This is only guaranteed to indicate which button was pressed during events caused by pressing or releasing a button. As such, it is not reliable for events such as mouseenter, mouseleave, mouseover, mouseout, or mousemove. For example, a value of MouseButton::Primary may also indicate that no button was pressed.
pub fn trigger_button(&self) -> MouseButton { pub fn trigger_button(&self) -> Option<MouseButton> {
#[allow(deprecated)] #[allow(deprecated)]
MouseButton::from_web_code(self.button) Some(MouseButton::from_web_code(self.button))
} }
} }
+37 -28
View File
@@ -4,6 +4,10 @@ use crossterm::event::{
use dioxus_core::*; use dioxus_core::*;
use fxhash::{FxHashMap, FxHashSet}; use fxhash::{FxHashMap, FxHashSet};
use dioxus_html::geometry::{ClientPoint, Coordinates, ElementPoint, PagePoint, ScreenPoint};
use dioxus_html::input::keyboard_types::Modifiers;
use dioxus_html::input::MouseButton as DioxusMouseButton;
use dioxus_html::input::MouseButtonSet as DioxusMouseButtons;
use dioxus_html::{on::*, KeyCode}; use dioxus_html::{on::*, KeyCode};
use std::{ use std::{
any::Any, any::Any,
@@ -588,40 +592,45 @@ fn get_event(evt: TermEvent) -> Option<(&'static str, EventData)> {
let ctrl = m.modifiers.contains(KeyModifiers::CONTROL); let ctrl = m.modifiers.contains(KeyModifiers::CONTROL);
let meta = false; let meta = false;
let get_mouse_data = |b| { let get_mouse_data = |crossterm_button: Option<MouseButton>| {
let buttons = match b { let button = crossterm_button.map(|b| match b {
None => 0, MouseButton::Left => DioxusMouseButton::Primary,
Some(MouseButton::Left) => 1, MouseButton::Right => DioxusMouseButton::Secondary,
Some(MouseButton::Right) => 2, MouseButton::Middle => DioxusMouseButton::Auxiliary,
Some(MouseButton::Middle) => 4, });
};
let button_state = match b { let button_set = match button {
None => 0, None => DioxusMouseButtons::empty(),
Some(MouseButton::Left) => 0, Some(button) => DioxusMouseButtons::only(button),
Some(MouseButton::Middle) => 1,
Some(MouseButton::Right) => 2,
}; };
// from https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent // from https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent
// The `offset`, `page` and `screen` coordinates are inconsistent with the MDN definition, as they are relative to the viewport (client), not the target element/page/screen, respectively. // The `offset`, `page` and `screen` coordinates are inconsistent with the MDN definition, as they are relative to the viewport (client), not the target element/page/screen, respectively.
// todo? // todo?
// But then, MDN defines them in terms of pixels, yet crossterm provides only row/column, and it might not be possible to get pixels. So we can't get 100% consistency anyway. // But then, MDN defines them in terms of pixels, yet crossterm provides only row/column, and it might not be possible to get pixels. So we can't get 100% consistency anyway.
EventData::Mouse(MouseData { let coordinates = Coordinates::new(
alt_key: alt, ScreenPoint::new(x, y),
button: button_state, ClientPoint::new(x, y),
buttons, ElementPoint::new(x, y),
client_x: x, PagePoint::new(x, y),
client_y: y, );
ctrl_key: ctrl,
meta_key: meta, let mut modifiers = Modifiers::empty();
offset_x: x, if shift {
offset_y: y, modifiers.insert(Modifiers::SHIFT);
page_x: x, }
page_y: y, if ctrl {
screen_x: x, modifiers.insert(Modifiers::CONTROL);
screen_y: y, }
shift_key: shift, if meta {
}) modifiers.insert(Modifiers::META);
}
if alt {
modifiers.insert(Modifiers::ALT);
}
EventData::Mouse(MouseData::new(coordinates, button, button_set, modifiers))
}; };
let get_wheel_data = |up| { let get_wheel_data = |up| {