opt: use whole focus instead to trigger session enter or leave

This commit is contained in:
Kingtous 2022-12-21 22:39:30 +08:00
parent 380febb2af
commit 2ae38c93f0
2 changed files with 41 additions and 15 deletions

View File

@ -9,11 +9,12 @@ class RawKeyFocusScope extends StatelessWidget {
final InputModel inputModel; final InputModel inputModel;
final Widget child; final Widget child;
RawKeyFocusScope( RawKeyFocusScope({
{this.focusNode, this.focusNode,
this.onFocusChange, this.onFocusChange,
required this.inputModel, required this.inputModel,
required this.child}); required this.child,
});
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -35,11 +36,15 @@ class RawPointerMouseRegion extends StatelessWidget {
final MouseCursor? cursor; final MouseCursor? cursor;
final PointerEnterEventListener? onEnter; final PointerEnterEventListener? onEnter;
final PointerExitEventListener? onExit; final PointerExitEventListener? onExit;
final PointerDownEventListener? onPointerDown;
final PointerUpEventListener? onPointerUp;
RawPointerMouseRegion( RawPointerMouseRegion(
{this.onEnter, {this.onEnter,
this.onExit, this.onExit,
this.cursor, this.cursor,
this.onPointerDown,
this.onPointerUp,
required this.inputModel, required this.inputModel,
required this.child}); required this.child});
@ -47,8 +52,14 @@ class RawPointerMouseRegion extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Listener( return Listener(
onPointerHover: inputModel.onPointHoverImage, onPointerHover: inputModel.onPointHoverImage,
onPointerDown: inputModel.onPointDownImage, onPointerDown: (evt) {
onPointerUp: inputModel.onPointUpImage, onPointerDown?.call(evt);
inputModel.onPointDownImage(evt);
},
onPointerUp: (evt) {
onPointerUp?.call(evt);
inputModel.onPointUpImage(evt);
},
onPointerMove: inputModel.onPointMoveImage, onPointerMove: inputModel.onPointMoveImage,
onPointerSignal: inputModel.onPointerSignalImage, onPointerSignal: inputModel.onPointerSignalImage,
/* /*

View File

@ -52,6 +52,7 @@ class _RemotePageState extends State<RemotePage>
with AutomaticKeepAliveClientMixin, MultiWindowListener { with AutomaticKeepAliveClientMixin, MultiWindowListener {
Timer? _timer; Timer? _timer;
String keyboardMode = "legacy"; String keyboardMode = "legacy";
bool _isWindowBlur = false;
final _cursorOverImage = false.obs; final _cursorOverImage = false.obs;
late RxBool _showRemoteCursor; late RxBool _showRemoteCursor;
late RxBool _zoomCursor; late RxBool _zoomCursor;
@ -59,7 +60,6 @@ class _RemotePageState extends State<RemotePage>
late RxBool _keyboardEnabled; late RxBool _keyboardEnabled;
final FocusNode _rawKeyFocusNode = FocusNode(debugLabel: "rawkeyFocusNode"); final FocusNode _rawKeyFocusNode = FocusNode(debugLabel: "rawkeyFocusNode");
var _imageFocused = false;
Function(bool)? _onEnterOrLeaveImage4Menubar; Function(bool)? _onEnterOrLeaveImage4Menubar;
@ -104,7 +104,6 @@ class _RemotePageState extends State<RemotePage>
if (!Platform.isLinux) { if (!Platform.isLinux) {
Wakelock.enable(); Wakelock.enable();
} }
_rawKeyFocusNode.requestFocus();
_ffi.ffiModel.updateEventListener(widget.id); _ffi.ffiModel.updateEventListener(widget.id);
_ffi.qualityMonitorModel.checkShowQualityMonitor(widget.id); _ffi.qualityMonitorModel.checkShowQualityMonitor(widget.id);
// Session option should be set after models.dart/FFI.start // Session option should be set after models.dart/FFI.start
@ -129,11 +128,18 @@ class _RemotePageState extends State<RemotePage>
@override @override
void onWindowBlur() { void onWindowBlur() {
super.onWindowBlur(); super.onWindowBlur();
_isWindowBlur = true;
// unfocus the key focus when the whole window is lost focus, // unfocus the key focus when the whole window is lost focus,
// and let OS to handle events instead. // and let OS to handle events instead.
_rawKeyFocusNode.unfocus(); _rawKeyFocusNode.unfocus();
} }
@override
void onWindowFocus() {
super.onWindowFocus();
_isWindowBlur = false;
}
@override @override
void dispose() { void dispose() {
debugPrint("REMOTE PAGE dispose ${widget.id}"); debugPrint("REMOTE PAGE dispose ${widget.id}");
@ -166,9 +172,16 @@ class _RemotePageState extends State<RemotePage>
color: Colors.black, color: Colors.black,
child: RawKeyFocusScope( child: RawKeyFocusScope(
focusNode: _rawKeyFocusNode, focusNode: _rawKeyFocusNode,
onFocusChange: (bool v) { onFocusChange: (bool imageFocused) {
_imageFocused = v; debugPrint(
if (_imageFocused) { "onFocusChange(window active:${!_isWindowBlur}) $imageFocused");
if (_isWindowBlur) {
imageFocused = false;
Future.delayed(Duration.zero, () {
_rawKeyFocusNode.unfocus();
});
}
if (imageFocused) {
_ffi.inputModel.enterOrLeave(true); _ffi.inputModel.enterOrLeave(true);
} else { } else {
_ffi.inputModel.enterOrLeave(false); _ffi.inputModel.enterOrLeave(false);
@ -199,9 +212,6 @@ class _RemotePageState extends State<RemotePage>
} }
void enterView(PointerEnterEvent evt) { void enterView(PointerEnterEvent evt) {
if (!_imageFocused) {
_rawKeyFocusNode.requestFocus();
}
_cursorOverImage.value = true; _cursorOverImage.value = true;
_firstEnterImage.value = true; _firstEnterImage.value = true;
if (_onEnterOrLeaveImage4Menubar != null) { if (_onEnterOrLeaveImage4Menubar != null) {
@ -244,6 +254,11 @@ class _RemotePageState extends State<RemotePage>
listenerBuilder: (child) => RawPointerMouseRegion( listenerBuilder: (child) => RawPointerMouseRegion(
onEnter: enterView, onEnter: enterView,
onExit: leaveView, onExit: leaveView,
onPointerDown: (event) {
if (!_rawKeyFocusNode.hasFocus) {
_rawKeyFocusNode.requestFocus();
}
},
inputModel: _ffi.inputModel, inputModel: _ffi.inputModel,
child: child, child: child,
), ),