fix cursor lost control sometime && refactor some Camel-Case flutter
Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
parent
60a30042c0
commit
98bb47a81d
@ -42,7 +42,7 @@ class InputModel {
|
|||||||
// mouse
|
// mouse
|
||||||
final isPhysicalMouse = false.obs;
|
final isPhysicalMouse = false.obs;
|
||||||
int _lastMouseDownButtons = 0;
|
int _lastMouseDownButtons = 0;
|
||||||
Offset last_mouse_pos = Offset.zero;
|
Offset lastMousePos = Offset.zero;
|
||||||
|
|
||||||
get id => parent.target?.id ?? "";
|
get id => parent.target?.id ?? "";
|
||||||
|
|
||||||
@ -308,23 +308,23 @@ class InputModel {
|
|||||||
double y = max(0.0, evt['y']);
|
double y = max(0.0, evt['y']);
|
||||||
final cursorModel = parent.target!.cursorModel;
|
final cursorModel = parent.target!.cursorModel;
|
||||||
|
|
||||||
if (cursorModel.is_peer_control_protected) {
|
if (cursorModel.isPeerControlProtected) {
|
||||||
last_mouse_pos = ui.Offset(x, y);
|
lastMousePos = ui.Offset(x, y);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!cursorModel.got_mouse_control) {
|
if (!cursorModel.gotMouseControl) {
|
||||||
bool self_get_control =
|
bool selfGetControl =
|
||||||
(x - last_mouse_pos.dx).abs() > kMouseControlDistance ||
|
(x - lastMousePos.dx).abs() > kMouseControlDistance ||
|
||||||
(y - last_mouse_pos.dy).abs() > kMouseControlDistance;
|
(y - lastMousePos.dy).abs() > kMouseControlDistance;
|
||||||
if (self_get_control) {
|
if (selfGetControl) {
|
||||||
cursorModel.got_mouse_control = true;
|
cursorModel.gotMouseControl = true;
|
||||||
} else {
|
} else {
|
||||||
last_mouse_pos = ui.Offset(x, y);
|
lastMousePos = ui.Offset(x, y);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
last_mouse_pos = ui.Offset(x, y);
|
lastMousePos = ui.Offset(x, y);
|
||||||
|
|
||||||
var type = '';
|
var type = '';
|
||||||
var isMove = false;
|
var isMove = false;
|
||||||
|
@ -753,8 +753,8 @@ class CursorModel with ChangeNotifier {
|
|||||||
double _hoty = 0;
|
double _hoty = 0;
|
||||||
double _displayOriginX = 0;
|
double _displayOriginX = 0;
|
||||||
double _displayOriginY = 0;
|
double _displayOriginY = 0;
|
||||||
bool got_mouse_control = true;
|
bool gotMouseControl = true;
|
||||||
DateTime _last_peer_mouse = DateTime.now()
|
DateTime _lastPeerMouse = DateTime.now()
|
||||||
.subtract(Duration(milliseconds: 2 * kMouseControlTimeoutMSec));
|
.subtract(Duration(milliseconds: 2 * kMouseControlTimeoutMSec));
|
||||||
String id = '';
|
String id = '';
|
||||||
WeakReference<FFI> parent;
|
WeakReference<FFI> parent;
|
||||||
@ -772,8 +772,8 @@ class CursorModel with ChangeNotifier {
|
|||||||
double get hotx => _hotx;
|
double get hotx => _hotx;
|
||||||
double get hoty => _hoty;
|
double get hoty => _hoty;
|
||||||
|
|
||||||
bool get is_peer_control_protected =>
|
bool get isPeerControlProtected =>
|
||||||
DateTime.now().difference(_last_peer_mouse).inMilliseconds <
|
DateTime.now().difference(_lastPeerMouse).inMilliseconds <
|
||||||
kMouseControlTimeoutMSec;
|
kMouseControlTimeoutMSec;
|
||||||
|
|
||||||
CursorModel(this.parent) {
|
CursorModel(this.parent) {
|
||||||
@ -1012,8 +1012,8 @@ class CursorModel with ChangeNotifier {
|
|||||||
|
|
||||||
/// Update the cursor position.
|
/// Update the cursor position.
|
||||||
updateCursorPosition(Map<String, dynamic> evt, String id) async {
|
updateCursorPosition(Map<String, dynamic> evt, String id) async {
|
||||||
got_mouse_control = false;
|
gotMouseControl = false;
|
||||||
_last_peer_mouse = DateTime.now();
|
_lastPeerMouse = DateTime.now();
|
||||||
_x = double.parse(evt['x']);
|
_x = double.parse(evt['x']);
|
||||||
_y = double.parse(evt['y']);
|
_y = double.parse(evt['y']);
|
||||||
try {
|
try {
|
||||||
|
@ -209,6 +209,7 @@ lazy_static::lazy_static! {
|
|||||||
static EXITING: AtomicBool = AtomicBool::new(false);
|
static EXITING: AtomicBool = AtomicBool::new(false);
|
||||||
|
|
||||||
const MOUSE_MOVE_PROTECTION_TIMEOUT: Duration = Duration::from_millis(1_000);
|
const MOUSE_MOVE_PROTECTION_TIMEOUT: Duration = Duration::from_millis(1_000);
|
||||||
|
// Actual diff of (x,y) is (1,1) here. But 5 may be tolerant.
|
||||||
const MOUSE_ACTIVE_DISTANCE: i32 = 5;
|
const MOUSE_ACTIVE_DISTANCE: i32 = 5;
|
||||||
|
|
||||||
// mac key input must be run in main thread, otherwise crash on >= osx 10.15
|
// mac key input must be run in main thread, otherwise crash on >= osx 10.15
|
||||||
@ -406,14 +407,25 @@ fn active_mouse_(conn: i32) -> bool {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let in_actived_dist = |a: i32, b: i32| -> bool { (a - b).abs() < MOUSE_ACTIVE_DISTANCE };
|
||||||
|
|
||||||
// check if input is in valid range
|
// check if input is in valid range
|
||||||
match crate::get_cursor_pos() {
|
match crate::get_cursor_pos() {
|
||||||
Some((x, y)) => {
|
Some((x, y)) => {
|
||||||
let can_active = (last_input.x - x).abs() < MOUSE_ACTIVE_DISTANCE
|
let mut can_active =
|
||||||
&& (last_input.y - y).abs() < MOUSE_ACTIVE_DISTANCE;
|
in_actived_dist(last_input.x, x) && in_actived_dist(last_input.y, y);
|
||||||
if !can_active {
|
if !can_active {
|
||||||
last_input.x = -MOUSE_ACTIVE_DISTANCE * 2;
|
// Try agin
|
||||||
last_input.y = -MOUSE_ACTIVE_DISTANCE * 2;
|
// No need to care about sleep here. It's not a common case.
|
||||||
|
std::thread::sleep(std::time::Duration::from_micros(10));
|
||||||
|
if let Some((x2, y2)) = crate::get_cursor_pos() {
|
||||||
|
can_active =
|
||||||
|
in_actived_dist(last_input.x, x2) && in_actived_dist(last_input.y, y2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !can_active {
|
||||||
|
last_input.x = INVALID_CURSOR_POS / 2;
|
||||||
|
last_input.y = INVALID_CURSOR_POS / 2;
|
||||||
}
|
}
|
||||||
can_active
|
can_active
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user