flutter_desktop: set event func to async

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou 2022-09-11 19:52:38 -07:00
parent aa6e747e8a
commit efe6d080f3
7 changed files with 37 additions and 45 deletions

View File

@ -594,8 +594,6 @@ class ImagePaint extends StatelessWidget {
} else { } else {
final key = cacheLinux.key(scale); final key = cacheLinux.key(scale);
cursor.addKeyLinux(key); cursor.addKeyLinux(key);
debugPrint(
'REMOVE ME ================================= linux curor key: $key');
return FlutterCustomMemoryImageCursor( return FlutterCustomMemoryImageCursor(
pixbuf: cacheLinux.data, pixbuf: cacheLinux.data,
key: key, key: key,

View File

@ -273,13 +273,13 @@ _keepScaleBuilder() {
_registerEventHandler() { _registerEventHandler() {
if (desktopType != DesktopType.main) { if (desktopType != DesktopType.main) {
platformFFI.registerEventHandler('theme', 'theme', (evt) { platformFFI.registerEventHandler('theme', 'theme', (evt) async {
String? dark = evt['dark']; String? dark = evt['dark'];
if (dark != null) { if (dark != null) {
MyTheme.changeTo(dark == 'true'); MyTheme.changeTo(dark == 'true');
} }
}); });
platformFFI.registerEventHandler('language', 'language', (_) { platformFFI.registerEventHandler('language', 'language', (_) async {
Get.forceAppUpdate(); Get.forceAppUpdate();
}); });
} }

View File

@ -1085,9 +1085,8 @@ void showOptions(String id, OverlayDialogManager dialogManager) async {
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
children: displays + children: displays +
<Widget>[ <Widget>[
getRadio('Original', 'original', viewStyle, setViewStyle), getRadio('Scale Original', 'original', viewStyle, setViewStyle),
getRadio('Shrink', 'shrink', viewStyle, setViewStyle), getRadio('Scale adaptive', 'adaptive', viewStyle, setViewStyle),
getRadio('Stretch', 'stretch', viewStyle, setViewStyle),
Divider(color: MyTheme.border), Divider(color: MyTheme.border),
getRadio('Good image quality', 'best', quality, setQuality), getRadio('Good image quality', 'best', quality, setQuality),
getRadio('Balanced', 'balanced', quality, setQuality), getRadio('Balanced', 'balanced', quality, setQuality),

View File

@ -358,11 +358,11 @@ class ImageModel with ChangeNotifier {
}); });
} }
void update(ui.Image? image, double tabBarHeight) { void update(ui.Image? image, double tabBarHeight) async {
if (_image == null && image != null) { if (_image == null && image != null) {
if (isWebDesktop || isDesktop) { if (isWebDesktop || isDesktop) {
parent.target?.canvasModel.updateViewStyle(); await parent.target?.canvasModel.updateViewStyle();
parent.target?.canvasModel.updateScrollStyle(); await parent.target?.canvasModel.updateScrollStyle();
} else { } else {
final size = MediaQueryData.fromWindow(ui.window).size; final size = MediaQueryData.fromWindow(ui.window).size;
final canvasWidth = size.width; final canvasWidth = size.width;
@ -372,14 +372,12 @@ class ImageModel with ChangeNotifier {
parent.target?.canvasModel.scale = min(xscale, yscale); parent.target?.canvasModel.scale = min(xscale, yscale);
} }
if (parent.target != null) { if (parent.target != null) {
initializeCursorAndCanvas(parent.target!); await initializeCursorAndCanvas(parent.target!);
}
if (parent.target?.ffiModel.isPeerAndroid ?? false) {
bind.sessionPeerOption(id: _id, name: 'view-style', value: 'adaptive');
parent.target?.canvasModel.updateViewStyle();
} }
Future.delayed(Duration(milliseconds: 1), () {
if (parent.target?.ffiModel.isPeerAndroid ?? false) {
bind.sessionPeerOption(id: _id, name: 'view-style', value: 'shrink');
parent.target?.canvasModel.updateViewStyle();
}
});
} }
_image = image; _image = image;
if (image != null) notifyListeners(); if (image != null) notifyListeners();
@ -445,7 +443,7 @@ class CanvasModel with ChangeNotifier {
double get scrollX => _scrollX; double get scrollX => _scrollX;
double get scrollY => _scrollY; double get scrollY => _scrollY;
void updateViewStyle() async { updateViewStyle() async {
final style = await bind.sessionGetOption(id: id, arg: 'view-style'); final style = await bind.sessionGetOption(id: id, arg: 'view-style');
if (style == null) { if (style == null) {
return; return;
@ -457,7 +455,6 @@ class CanvasModel with ChangeNotifier {
final s2 = size.height / getDisplayHeight(); final s2 = size.height / getDisplayHeight();
_scale = s1 < s2 ? s1 : s2; _scale = s1 < s2 ? s1 : s2;
} }
_x = (size.width - getDisplayWidth() * _scale) / 2; _x = (size.width - getDisplayWidth() * _scale) / 2;
_y = (size.height - getDisplayHeight() * _scale) / 2; _y = (size.height - getDisplayHeight() * _scale) / 2;
notifyListeners(); notifyListeners();
@ -475,7 +472,7 @@ class CanvasModel with ChangeNotifier {
notifyListeners(); notifyListeners();
} }
void update(double x, double y, double scale) { update(double x, double y, double scale) {
_x = x; _x = x;
_y = y; _y = y;
_scale = scale; _scale = scale;
@ -508,19 +505,9 @@ class CanvasModel with ChangeNotifier {
var dxOffset = 0; var dxOffset = 0;
var dyOffset = 0; var dyOffset = 0;
if (dw > size.width) { if (dw > size.width) {
final X_debugNanOrInfinite = x - dw * (x / size.width) - _x;
if (X_debugNanOrInfinite.isInfinite || X_debugNanOrInfinite.isNaN) {
debugPrint(
'REMOVE ME ============================ X_debugNanOrInfinite $x,$dw,$_scale,${size.width},$_x');
}
dxOffset = (x - dw * (x / size.width) - _x).toInt(); dxOffset = (x - dw * (x / size.width) - _x).toInt();
} }
if (dh > size.height) { if (dh > size.height) {
final Y_debugNanOrInfinite = y - dh * (y / size.height) - _y;
if (Y_debugNanOrInfinite.isInfinite || Y_debugNanOrInfinite.isNaN) {
debugPrint(
'REMOVE ME ============================ Y_debugNanOrInfinite $y,$dh,$_scale,${size.height},$_y');
}
dyOffset = (y - dh * (y / size.height) - _y).toInt(); dyOffset = (y - dh * (y / size.height) - _y).toInt();
} }
_x += dxOffset; _x += dxOffset;
@ -789,7 +776,6 @@ class CursorModel with ChangeNotifier {
List<dynamic> colors = json.decode(evt['colors']); List<dynamic> colors = json.decode(evt['colors']);
final rgba = Uint8List.fromList(colors.map((s) => s as int).toList()); final rgba = Uint8List.fromList(colors.map((s) => s as int).toList());
var pid = parent.target?.id; var pid = parent.target?.id;
final image = await img.decodeImageFromPixels( final image = await img.decodeImageFromPixels(
rgba, width, height, ui.PixelFormat.rgba8888); rgba, width, height, ui.PixelFormat.rgba8888);
if (parent.target?.id != pid) return; if (parent.target?.id != pid) return;
@ -1128,9 +1114,9 @@ class FFI {
if (message is Event) { if (message is Event) {
try { try {
Map<String, dynamic> event = json.decode(message.field0); Map<String, dynamic> event = json.decode(message.field0);
cb(event); await cb(event);
} catch (e) { } catch (e) {
print('json.decode fail(): $e'); debugPrint('json.decode fail(): $e');
} }
} else if (message is Rgba) { } else if (message is Rgba) {
imageModel.onRgba(message.field0, tabBarHeight); imageModel.onRgba(message.field0, tabBarHeight);
@ -1292,6 +1278,15 @@ class Display {
double y = 0; double y = 0;
int width = 0; int width = 0;
int height = 0; int height = 0;
Display() {
width = (isDesktop || isWebDesktop)
? kDesktopDefaultDisplayWidth
: kMobileDefaultDisplayWidth;
height = (isDesktop || isWebDesktop)
? kDesktopDefaultDisplayHeight
: kMobileDefaultDisplayHeight;
}
} }
class PeerInfo { class PeerInfo {
@ -1304,8 +1299,8 @@ class PeerInfo {
List<Display> displays = []; List<Display> displays = [];
} }
Future<void> savePreference(String id, double xCursor, double yCursor, savePreference(String id, double xCursor, double yCursor, double xCanvas,
double xCanvas, double yCanvas, double scale, int currentDisplay) async { double yCanvas, double scale, int currentDisplay) async {
SharedPreferences prefs = await SharedPreferences.getInstance(); SharedPreferences prefs = await SharedPreferences.getInstance();
final p = <String, dynamic>{}; final p = <String, dynamic>{};
p['xCursor'] = xCursor; p['xCursor'] = xCursor;
@ -1326,12 +1321,12 @@ Future<Map<String, dynamic>?> getPreference(String id) async {
return m; return m;
} }
void removePreference(String id) async { removePreference(String id) async {
SharedPreferences prefs = await SharedPreferences.getInstance(); SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.remove('peer$id'); prefs.remove('peer$id');
} }
void initializeCursorAndCanvas(FFI ffi) async { initializeCursorAndCanvas(FFI ffi) async {
var p = await getPreference(ffi.id); var p = await getPreference(ffi.id);
int currentDisplay = 0; int currentDisplay = 0;
if (p != null) { if (p != null) {

View File

@ -22,7 +22,7 @@ class RgbaFrame extends Struct {
typedef F2 = Pointer<Utf8> Function(Pointer<Utf8>, Pointer<Utf8>); typedef F2 = Pointer<Utf8> Function(Pointer<Utf8>, Pointer<Utf8>);
typedef F3 = void Function(Pointer<Utf8>, Pointer<Utf8>); typedef F3 = void Function(Pointer<Utf8>, Pointer<Utf8>);
typedef HandleEvent = void Function(Map<String, dynamic> evt); typedef HandleEvent = Future<void> Function(Map<String, dynamic> evt);
/// FFI wrapper around the native Rust core. /// FFI wrapper around the native Rust core.
/// Hides the platform differences. /// Hides the platform differences.
@ -156,14 +156,14 @@ class PlatformFFI {
version = await getVersion(); version = await getVersion();
} }
bool _tryHandle(Map<String, dynamic> evt) { Future<bool> _tryHandle(Map<String, dynamic> evt) async {
final name = evt['name']; final name = evt['name'];
if (name != null) { if (name != null) {
final handlers = _eventHandlers[name]; final handlers = _eventHandlers[name];
if (handlers != null) { if (handlers != null) {
if (handlers.isNotEmpty) { if (handlers.isNotEmpty) {
for (var handler in handlers.values) { for (var handler in handlers.values) {
handler(evt); await handler(evt);
} }
return true; return true;
} }
@ -180,7 +180,7 @@ class PlatformFFI {
try { try {
Map<String, dynamic> event = json.decode(message); Map<String, dynamic> event = json.decode(message);
// _tryHandle here may be more flexible than _eventCallback // _tryHandle here may be more flexible than _eventCallback
if (!_tryHandle(event)) { if (!await _tryHandle(event)) {
if (_eventCallback != null) { if (_eventCallback != null) {
await _eventCallback!(event); await _eventCallback!(event);
} }

View File

@ -40,10 +40,10 @@ class Peers extends ChangeNotifier {
static const _cbQueryOnlines = 'callback_query_onlines'; static const _cbQueryOnlines = 'callback_query_onlines';
Peers({required this.name, required this.peers, required this.loadEvent}) { Peers({required this.name, required this.peers, required this.loadEvent}) {
platformFFI.registerEventHandler(_cbQueryOnlines, name, (evt) { platformFFI.registerEventHandler(_cbQueryOnlines, name, (evt) async {
_updateOnlineState(evt); _updateOnlineState(evt);
}); });
platformFFI.registerEventHandler(loadEvent, name, (evt) { platformFFI.registerEventHandler(loadEvent, name, (evt) async {
_updatePeers(evt); _updatePeers(evt);
}); });
} }

View File

@ -28,10 +28,10 @@ Future<ui.Image> decodeImageFromPixels(
pixelFormat: format, pixelFormat: format,
); );
if (!allowUpscaling) { if (!allowUpscaling) {
if (targetWidth != null && targetWidth! > descriptor.width) { if (targetWidth != null && targetWidth > descriptor.width) {
targetWidth = descriptor.width; targetWidth = descriptor.width;
} }
if (targetHeight != null && targetHeight! > descriptor.height) { if (targetHeight != null && targetHeight > descriptor.height) {
targetHeight = descriptor.height; targetHeight = descriptor.height;
} }
} }