flutter_desktop: fix ffi model provider

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou 2022-08-06 18:48:07 +08:00
parent 0e012894b5
commit 917830fb69
2 changed files with 98 additions and 94 deletions

View File

@ -58,6 +58,8 @@ class _ConnectionPageState extends State<ConnectionPage> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
Provider.of<FfiModel>(context);
if (_idController.text.isEmpty) _idController.text = gFFI.getId(); if (_idController.text.isEmpty) _idController.text = gFFI.getId();
return Container( return Container(
decoration: BoxDecoration(color: isDarkTheme() ? null : MyTheme.grayBg), decoration: BoxDecoration(color: isDarkTheme() ? null : MyTheme.grayBg),

View File

@ -236,68 +236,66 @@ class _RemotePageState extends State<RemotePage>
_ffi.inputKey(label, down: down, press: press ?? false); _ffi.inputKey(label, down: down, press: press ?? false);
} }
Widget buildBody(FfiModel ffiModel) {
final hasDisplays = ffiModel.pi.displays.length > 0;
final hideKeyboard = isKeyboardShown() && _showEdit;
final showActionButton = !_showBar || hideKeyboard;
final keyboard = ffiModel.permissions['keyboard'] != false;
return Scaffold(
// resizeToAvoidBottomInset: true,
floatingActionButton: !showActionButton
? null
: FloatingActionButton(
mini: !hideKeyboard,
child:
Icon(hideKeyboard ? Icons.expand_more : Icons.expand_less),
backgroundColor: MyTheme.accent,
onPressed: () {
setState(() {
if (hideKeyboard) {
_showEdit = false;
_ffi.invokeMethod("enable_soft_keyboard", false);
_mobileFocusNode.unfocus();
_physicalFocusNode.requestFocus();
} else {
_showBar = !_showBar;
}
});
}),
bottomNavigationBar: _showBar && hasDisplays ? getBottomAppBar() : null,
body: Overlay(
initialEntries: [
OverlayEntry(builder: (context) {
return Container(
color: Colors.black,
child: getBodyForDesktopWithListener(keyboard));
})
],
));
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
super.build(context); super.build(context);
Provider.of<CanvasModel>(context, listen: false).tabBarHeight = Provider.of<CanvasModel>(context, listen: false).tabBarHeight =
super.widget.tabBarHeight; super.widget.tabBarHeight;
final hasDisplays = _ffi.ffiModel.pi.displays.length > 0;
final hideKeyboard = isKeyboardShown() && _showEdit;
final showActionButton = !_showBar || hideKeyboard;
final keyboard = _ffi.ffiModel.permissions['keyboard'] != false;
return WillPopScope( return WillPopScope(
onWillPop: () async { onWillPop: () async {
clientClose(); clientClose();
return false; return false;
}, },
child: MultiProvider( child: MultiProvider(
providers: [ providers: [
ChangeNotifierProvider.value(value: _ffi.ffiModel), ChangeNotifierProvider.value(value: _ffi.ffiModel),
ChangeNotifierProvider.value(value: _ffi.imageModel), ChangeNotifierProvider.value(value: _ffi.imageModel),
ChangeNotifierProvider.value(value: _ffi.cursorModel), ChangeNotifierProvider.value(value: _ffi.cursorModel),
ChangeNotifierProvider.value(value: _ffi.canvasModel), ChangeNotifierProvider.value(value: _ffi.canvasModel),
], ],
child: getRawPointerAndKeyBody( child: getRawPointerAndKeyBody(Consumer<FfiModel>(
keyboard, builder: (context, ffiModel, _child) => buildBody(ffiModel)))));
Scaffold(
// resizeToAvoidBottomInset: true,
floatingActionButton: !showActionButton
? null
: FloatingActionButton(
mini: !hideKeyboard,
child: Icon(hideKeyboard
? Icons.expand_more
: Icons.expand_less),
backgroundColor: MyTheme.accent,
onPressed: () {
setState(() {
if (hideKeyboard) {
_showEdit = false;
_ffi.invokeMethod(
"enable_soft_keyboard", false);
_mobileFocusNode.unfocus();
_physicalFocusNode.requestFocus();
} else {
_showBar = !_showBar;
}
});
}),
bottomNavigationBar: _showBar && hasDisplays
? getBottomAppBar(keyboard)
: null,
body: Overlay(
initialEntries: [
OverlayEntry(builder: (context) {
return Container(
color: Colors.black,
child: getBodyForDesktopWithListener(keyboard));
})
],
))),
));
} }
Widget getRawPointerAndKeyBody(bool keyboard, Widget child) { Widget getRawPointerAndKeyBody(Widget child) {
return Listener( return Listener(
onPointerHover: (e) { onPointerHover: (e) {
if (e.kind != ui.PointerDeviceKind.mouse) return; if (e.kind != ui.PointerDeviceKind.mouse) return;
@ -352,55 +350,58 @@ class _RemotePageState extends State<RemotePage>
'{"id": "${widget.id}", "type": "wheel", "x": "$dx", "y": "$dy"}'); '{"id": "${widget.id}", "type": "wheel", "x": "$dx", "y": "$dy"}');
} }
}, },
child: MouseRegion( child: Consumer<FfiModel>(
cursor: keyboard ? SystemMouseCursors.none : MouseCursor.defer, builder: (context, FfiModel, _child) => MouseRegion(
child: FocusScope( cursor: FfiModel.permissions['keyboard'] != false
autofocus: true, ? SystemMouseCursors.none
child: Focus( : MouseCursor.defer,
child: FocusScope(
autofocus: true, autofocus: true,
canRequestFocus: true, child: Focus(
focusNode: _physicalFocusNode, autofocus: true,
onKey: (data, e) { canRequestFocus: true,
final key = e.logicalKey; focusNode: _physicalFocusNode,
if (e is RawKeyDownEvent) { onKey: (data, e) {
if (e.repeat) { final key = e.logicalKey;
sendRawKey(e, press: true); if (e is RawKeyDownEvent) {
} else { if (e.repeat) {
if (e.isAltPressed && !_ffi.alt) { sendRawKey(e, press: true);
_ffi.alt = true; } else {
} else if (e.isControlPressed && !_ffi.ctrl) { if (e.isAltPressed && !_ffi.alt) {
_ffi.ctrl = true; _ffi.alt = true;
} else if (e.isShiftPressed && !_ffi.shift) { } else if (e.isControlPressed && !_ffi.ctrl) {
_ffi.shift = true; _ffi.ctrl = true;
} else if (e.isMetaPressed && !_ffi.command) { } else if (e.isShiftPressed && !_ffi.shift) {
_ffi.command = true; _ffi.shift = true;
} else if (e.isMetaPressed && !_ffi.command) {
_ffi.command = true;
}
sendRawKey(e, down: true);
}
} }
sendRawKey(e, down: true); // [!_showEdit] workaround for soft-keyboard's control_key like Backspace / Enter
} if (!_showEdit && e is RawKeyUpEvent) {
} if (key == LogicalKeyboardKey.altLeft ||
// [!_showEdit] workaround for soft-keyboard's control_key like Backspace / Enter key == LogicalKeyboardKey.altRight) {
if (!_showEdit && e is RawKeyUpEvent) { _ffi.alt = false;
if (key == LogicalKeyboardKey.altLeft || } else if (key == LogicalKeyboardKey.controlLeft ||
key == LogicalKeyboardKey.altRight) { key == LogicalKeyboardKey.controlRight) {
_ffi.alt = false; _ffi.ctrl = false;
} else if (key == LogicalKeyboardKey.controlLeft || } else if (key == LogicalKeyboardKey.shiftRight ||
key == LogicalKeyboardKey.controlRight) { key == LogicalKeyboardKey.shiftLeft) {
_ffi.ctrl = false; _ffi.shift = false;
} else if (key == LogicalKeyboardKey.shiftRight || } else if (key == LogicalKeyboardKey.metaLeft ||
key == LogicalKeyboardKey.shiftLeft) { key == LogicalKeyboardKey.metaRight) {
_ffi.shift = false; _ffi.command = false;
} else if (key == LogicalKeyboardKey.metaLeft || }
key == LogicalKeyboardKey.metaRight) { sendRawKey(e);
_ffi.command = false; }
} return KeyEventResult.handled;
sendRawKey(e); },
} child: child)))));
return KeyEventResult.handled;
},
child: child))));
} }
Widget getBottomAppBar(bool keyboard) { Widget? getBottomAppBar() {
return BottomAppBar( return BottomAppBar(
elevation: 10, elevation: 10,
color: MyTheme.accent, color: MyTheme.accent,
@ -515,6 +516,7 @@ class _RemotePageState extends State<RemotePage>
)); ));
} }
paints.add(getHelpTools()); paints.add(getHelpTools());
return MouseRegion( return MouseRegion(
onEnter: (evt) { onEnter: (evt) {
bind.hostStopSystemKeyPropagate(stopped: false); bind.hostStopSystemKeyPropagate(stopped: false);