From 190dc6006c8d8f7f2ea1d0b14d35d733c1e761ed Mon Sep 17 00:00:00 2001 From: open-trade Date: Tue, 17 Nov 2020 11:12:55 +0800 Subject: [PATCH] refactor --- flutter_hbb/lib/common.dart | 44 ++++++++++++++++---------------- flutter_hbb/lib/home_page.dart | 6 ++--- flutter_hbb/lib/remote_page.dart | 32 ++++++++++++++++++++--- 3 files changed, 52 insertions(+), 30 deletions(-) diff --git a/flutter_hbb/lib/common.dart b/flutter_hbb/lib/common.dart index 3f413119f..9e612168c 100644 --- a/flutter_hbb/lib/common.dart +++ b/flutter_hbb/lib/common.dart @@ -1,5 +1,4 @@ import 'package:flutter/material.dart'; -import 'package:provider/provider.dart'; import 'package:ffi/ffi.dart'; import 'package:path_provider/path_provider.dart'; import 'dart:io'; @@ -32,19 +31,26 @@ typedef F3 = void Function(Pointer, Pointer); // https://juejin.im/post/6844903864852807694 class FfiModel with ChangeNotifier { - F1 _freeCString; - F2 _getByName; - F3 _setByName; - FfiModel() { - initialzeFFI(); + init(); } - String getId() { + Future init() async { + await FFI.init(); + notifyListeners(); + } +} + +class FFI { + static F1 _freeCString; + static F2 _getByName; + static F3 _setByName; + + static String getId() { return getByName("remote_id"); } - List peers() { + static List peers() { try { List peers = json.decode(getByName("peers")); return peers @@ -58,15 +64,11 @@ class FfiModel with ChangeNotifier { return []; } - void addRemote() { - notifyListeners(); - } - - void connect(String id) { + static void connect(String id) { setByName("connect", id); } - Map popEvent() { + static Map popEvent() { var s = getByName("event"); if (s == "") return null; try { @@ -78,7 +80,7 @@ class FfiModel with ChangeNotifier { return null; } - void login(String password, bool remember) { + static void login(String password, bool remember) { setByName( "login", json.encode({ @@ -87,15 +89,15 @@ class FfiModel with ChangeNotifier { })); } - void close() { + static void close() { setByName("close", ""); } - void setByName(String name, String value) { + static void setByName(String name, String value) { _setByName(Utf8.toUtf8(name), Utf8.toUtf8(value)); } - String getByName(String name, {String arg = ""}) { + static String getByName(String name, {String arg = ""}) { var p = _getByName(Utf8.toUtf8(name), Utf8.toUtf8(arg)); assert(p != null); var res = Utf8.fromUtf8(p); @@ -104,7 +106,7 @@ class FfiModel with ChangeNotifier { return res; } - Future initialzeFFI() async { + static Future init() async { final dylib = Platform.isAndroid ? DynamicLibrary.open('librustdesk.so') : DynamicLibrary.process(); @@ -116,7 +118,6 @@ class FfiModel with ChangeNotifier { .lookupFunction), F1>('rust_cstr_free'); final dir = (await getApplicationDocumentsDirectory()).path; setByName("init", dir); - notifyListeners(); } } @@ -148,8 +149,7 @@ void showSuccess(String text) { // https://material.io/develop/flutter/components/dialogs void enterPasswordDialog(String id, BuildContext context) { - var ffi = Provider.of(context); - var remember = ffi.getByName("remember", arg: id) == "true"; + var remember = FFI.getByName("remember", arg: id) == "true"; var dialog = AlertDialog( title: Text('Please enter your password'), contentPadding: EdgeInsets.zero, diff --git a/flutter_hbb/lib/home_page.dart b/flutter_hbb/lib/home_page.dart index 8488e1476..1a4a9e36d 100644 --- a/flutter_hbb/lib/home_page.dart +++ b/flutter_hbb/lib/home_page.dart @@ -14,13 +14,11 @@ class HomePage extends StatefulWidget { class _HomePageState extends State { final _idController = TextEditingController(); - FfiModel _ffi; @override Widget build(BuildContext context) { - _ffi = Provider.of(context); - _idController.text = _ffi.getId(); - + Provider.of(context); + _idController.text = FFI.getId(); // This method is rerun every time setState is called return Scaffold( appBar: AppBar( diff --git a/flutter_hbb/lib/remote_page.dart b/flutter_hbb/lib/remote_page.dart index 3748fddac..d110cd21f 100644 --- a/flutter_hbb/lib/remote_page.dart +++ b/flutter_hbb/lib/remote_page.dart @@ -1,7 +1,7 @@ import 'package:flutter/material.dart'; -import 'package:provider/provider.dart'; import 'common.dart'; import 'package:flutter/services.dart'; +import 'dart:ui' as ui; class RemotePage extends StatefulWidget { RemotePage({Key key, this.id}) : super(key: key); @@ -13,13 +13,37 @@ class RemotePage extends StatefulWidget { } class _RemotePageState extends State { - FfiModel _ffi; + @override + void initState() { + super.initState(); + FFI.connect(widget.id); + } @override Widget build(BuildContext context) { - _ffi = Provider.of(context); - _ffi.connect(widget.id); // https://stackoverflow.com/questions/46640116/make-flutter-application-fullscreen SystemChrome.setEnabledSystemUIOverlays([]); + return CustomPaint( + painter: new ImageEditor(image: null), + ); + } +} + +class ImageEditor extends CustomPainter { + ImageEditor({ + this.image, + }); + + ui.Image image; + + @override + void paint(Canvas canvas, Size size) { + if (image = null) return; + canvas.drawImage(image, new Offset(0.0, 0.0), new Paint()); + } + + @override + bool shouldRepaint(CustomPainter oldDelegate) { + return false; } }