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

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