plugin_framework, flutter event handlers

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou
2023-04-21 21:40:34 +08:00
parent ae6d80cebe
commit 67413b7419
15 changed files with 301 additions and 192 deletions

View File

@@ -1,60 +0,0 @@
void handlePluginEvent(
Map<String, dynamic> evt,
String peer,
Function(Map<String, dynamic> e) handleMsgBox,
) {
// content
//
// {
// "t": "Option",
// "c": {
// "id": "id from RustDesk platform",
// "name": "Privacy Mode",
// "version": "v0.1.0",
// "location": "client|remote|toolbar|display",
// "key": "privacy-mode",
// "value": "1"
// }
// }
//
// {
// "t": "MsgBox",
// "c": {
// "type": "custom-nocancel",
// "title": "Privacy Mode",
// "text": "Failed unknown",
// "link": ""
// }
// }
//
if (evt['content']?['c'] == null) return;
final t = evt['content']?['t'];
if (t == 'Option') {
handleOptionEvent(evt['content']?['c'], peer);
} else if (t == 'MsgBox') {
handleMsgBox(evt['content']?['c']);
}
}
void handleOptionEvent(Map<String, dynamic> evt, String peer) {
// content
//
// {
// "id": "id from RustDesk platform",
// "name": "Privacy Mode",
// "version": "v0.1.0",
// "location": "client|remote|toolbar|display",
// "key": "privacy-mode",
// "value": "1"
// }
//
final key = evt['key'];
final value = evt['value'];
if (key == 'privacy-mode') {
if (value == '1') {
// enable privacy mode
} else {
// disable privacy mode
}
}
}

View File

@@ -8,8 +8,8 @@ import 'package:flutter_hbb/models/chat_model.dart';
import 'package:flutter_hbb/models/state_model.dart';
import 'package:flutter_hbb/consts.dart';
import 'package:flutter_hbb/utils/multi_window_manager.dart';
import 'package:flutter_hbb/desktop/plugin/widget.dart';
import 'package:flutter_hbb/desktop/plugin/common.dart';
import 'package:flutter_hbb/plugin/widget.dart';
import 'package:flutter_hbb/plugin/common.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';
import 'package:provider/provider.dart';

View File

@@ -16,9 +16,9 @@ import 'package:flutter_hbb/models/peer_tab_model.dart';
import 'package:flutter_hbb/models/server_model.dart';
import 'package:flutter_hbb/models/user_model.dart';
import 'package:flutter_hbb/models/state_model.dart';
import 'package:flutter_hbb/desktop/plugin/event.dart';
import 'package:flutter_hbb/desktop/plugin/desc.dart';
import 'package:flutter_hbb/desktop/plugin/widget.dart';
import 'package:flutter_hbb/plugin/event.dart';
import 'package:flutter_hbb/plugin/desc.dart';
import 'package:flutter_hbb/plugin/widget.dart';
import 'package:flutter_hbb/common/shared_state.dart';
import 'package:tuple/tuple.dart';
import 'package:image/image.dart' as img2;
@@ -209,35 +209,37 @@ class FfiModel with ChangeNotifier {
closeConnection(id: peer_id);
} else if (name == 'portable_service_running') {
parent.target?.elevationModel.onPortableServiceRunning(evt);
} else if (name == "on_url_scheme_received") {
} else if (name == 'on_url_scheme_received') {
final url = evt['url'].toString();
parseRustdeskUri(url);
} else if (name == "on_voice_call_waiting") {
} else if (name == 'on_voice_call_waiting') {
// Waiting for the response from the peer.
parent.target?.chatModel.onVoiceCallWaiting();
} else if (name == "on_voice_call_started") {
} else if (name == 'on_voice_call_started') {
// Voice call is connected.
parent.target?.chatModel.onVoiceCallStarted();
} else if (name == "on_voice_call_closed") {
} else if (name == 'on_voice_call_closed') {
// Voice call is closed with reason.
final reason = evt['reason'].toString();
parent.target?.chatModel.onVoiceCallClosed(reason);
} else if (name == "on_voice_call_incoming") {
} else if (name == 'on_voice_call_incoming') {
// Voice call is requested by the peer.
parent.target?.chatModel.onVoiceCallIncoming();
} else if (name == "update_voice_call_state") {
} else if (name == 'update_voice_call_state') {
parent.target?.serverModel.updateVoiceCallState(evt);
} else if (name == "fingerprint") {
} else if (name == 'fingerprint') {
FingerprintState.find(peerId).value = evt['fingerprint'] ?? '';
} else if (name == "plugin_desc") {
} else if (name == 'plugin_desc') {
updateDesc(evt);
} else if (name == "plugin_event") {
} else if (name == 'plugin_event') {
handlePluginEvent(
evt, peerId, (Map<String, dynamic> e) => handleMsgBox(e, peerId));
} else if (name == "plugin_reload") {
} else if (name == 'plugin_reload') {
handleReloading(evt, peerId);
} else if (name == 'plugin_option') {
handleOption(evt, peerId);
} else {
debugPrint("Unknown event name: $name");
debugPrint('Unknown event name: $name');
}
};
}
@@ -282,7 +284,7 @@ class FfiModel with ChangeNotifier {
//
}
parent.target?.recordingModel.onSwitchDisplay();
handleResolutions(peerId, evt["resolutions"]);
handleResolutions(peerId, evt['resolutions']);
notifyListeners();
}
@@ -321,7 +323,7 @@ class FfiModel with ChangeNotifier {
showWaitUacDialog(id, dialogManager, type);
} else if (type == 'elevation-error') {
showElevationError(id, type, title, text, dialogManager);
} else if (type == "relay-hint") {
} else if (type == 'relay-hint') {
showRelayHintDialog(id, type, title, text, dialogManager);
} else {
var hasRetry = evt['hasRetry'] == 'true';

View File

@@ -9,8 +9,6 @@ const String kLocationClientRemoteToolbarDisplay =
'client|remote|toolbar|display';
class MsgFromUi {
String remotePeerId;
String localPeerId;
String id;
String name;
String location;
@@ -19,8 +17,6 @@ class MsgFromUi {
String action;
MsgFromUi({
required this.remotePeerId,
required this.localPeerId,
required this.id,
required this.name,
required this.location,
@@ -31,8 +27,6 @@ class MsgFromUi {
Map<String, dynamic> toJson() {
return <String, dynamic>{
'remote_peer_id': remotePeerId,
'local_peer_id': localPeerId,
'id': id,
'name': name,
'location': location,

View File

@@ -0,0 +1,11 @@
void handlePluginEvent(
Map<String, dynamic> evt,
String peer,
Function(Map<String, dynamic> e) handleMsgBox,
) {
if (evt['content']?['c'] == null) return;
final t = evt['content']?['t'];
if (t == 'MsgBox') {
handleMsgBox(evt['content']?['c']);
}
}

View File

@@ -3,15 +3,30 @@ import './common.dart';
import './desc.dart';
final Map<String, LocationModel> locationModels = {};
final Map<String, KvModel> kvModels = {};
class KvModel with ChangeNotifier {
final Map<String, String> kv = {};
String? get(String key) => kv.remove(key);
void set(String key, String value) {
kv[key] = value;
notifyListeners();
}
}
class PluginModel with ChangeNotifier {
final List<UiType> uiList = [];
final Map<String, String> opts = {};
void add(UiType ui) {
uiList.add(ui);
notifyListeners();
}
String? getOpt(String key) => opts.remove(key);
bool get isEmpty => uiList.isEmpty;
}
@@ -42,3 +57,20 @@ LocationModel addLocation(String location) {
}
return locationModels[location]!;
}
String makeKvModelInstance(String location, PluginId id, String peer) =>
'$location|$id|$peer';
KvModel addKvModel(String location, PluginId pluginId, String peer) {
final instance = makeKvModelInstance(location, pluginId, peer);
if (kvModels[instance] == null) {
kvModels[instance] = KvModel();
}
return kvModels[instance]!;
}
void updateOption(
String location, PluginId id, String peer, String key, String value) {
final instance = makeKvModelInstance(location, id, peer);
kvModels[instance]?.set(key, value);
}

View File

@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_hbb/models/model.dart';
import 'package:provider/provider.dart';
// to-do: do not depend on desktop
import 'package:flutter_hbb/desktop/widgets/remote_toolbar.dart';
import 'package:flutter_hbb/models/platform_model.dart';
@@ -65,6 +66,7 @@ class PluginItem extends StatelessWidget {
final FFI ffi;
final String location;
final PluginModel pluginModel;
final KvModel kvModel;
PluginItem({
Key? key,
@@ -73,42 +75,49 @@ class PluginItem extends StatelessWidget {
required this.ffi,
required this.location,
required this.pluginModel,
}) : super(key: key);
}) : kvModel = addKvModel(location, pluginId, peerId),
super(key: key);
bool get isEmpty => pluginModel.isEmpty;
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider.value(
value: pluginModel,
child: Consumer<PluginModel>(builder: (context, model, child) {
return Column(
children: model.uiList.map((ui) => _buildItem(ui)).toList(),
);
}),
return MultiProvider(
providers: [
ChangeNotifierProvider.value(value: pluginModel),
ChangeNotifierProvider.value(value: kvModel),
],
child: Consumer2<PluginModel, KvModel>(
builder: (context, pluginModel, kvModel, child) {
return Column(
children: pluginModel.uiList.map((ui) => _buildItem(ui)).toList(),
);
},
),
);
}
// to-do: add plugin icon and tooltip
Widget _buildItem(UiType ui) {
late Widget child;
switch (ui.runtimeType) {
case UiButton:
return _buildMenuButton(ui as UiButton);
child = _buildMenuButton(ui as UiButton);
break;
case UiCheckbox:
return _buildCheckboxMenuButton(ui as UiCheckbox);
child = _buildCheckboxMenuButton(ui as UiCheckbox);
break;
default:
return Container();
child = Container();
}
// to-do: add plugin icon and tooltip
return child;
}
Uint8List _makeEvent(
String localPeerId,
String key, {
bool? v,
}) {
final event = MsgFromUi(
remotePeerId: peerId,
localPeerId: localPeerId,
id: pluginId,
name: getDesc(pluginId)?.name ?? '',
location: location,
@@ -122,15 +131,11 @@ class PluginItem extends StatelessWidget {
Widget _buildMenuButton(UiButton ui) {
return MenuButton(
onPressed: () {
() async {
final localPeerId = await bind.mainGetMyId();
bind.pluginEvent(
id: pluginId,
event: _makeEvent(localPeerId, ui.key),
);
}();
},
onPressed: () => bind.pluginEvent(
id: pluginId,
peer: peerId,
event: _makeEvent(ui.key),
),
trailingIcon: Icon(
IconData(int.parse(ui.icon, radix: 16), fontFamily: 'MaterialIcons')),
// to-do: RustDesk translate or plugin translate ?
@@ -140,8 +145,15 @@ class PluginItem extends StatelessWidget {
}
Widget _buildCheckboxMenuButton(UiCheckbox ui) {
final v =
bind.pluginGetSessionOption(id: pluginId, peer: peerId, key: ui.key);
var v = kvModel.get(ui.key);
if (v == null) {
if (peerId.isEmpty) {
v = bind.pluginGetLocalOption(id: pluginId, key: ui.key);
} else {
v = bind.pluginGetSessionOption(
id: pluginId, peer: peerId, key: ui.key);
}
}
if (v == null) {
// session or plugin not found
return Container();
@@ -150,16 +162,14 @@ class PluginItem extends StatelessWidget {
value: ConfigItem.isTrue(v),
onChanged: (v) {
if (v != null) {
() async {
final localPeerId = await bind.mainGetMyId();
bind.pluginEvent(
id: pluginId,
event: _makeEvent(localPeerId, ui.key, v: v),
);
}();
bind.pluginEvent(
id: pluginId,
peer: peerId,
event: _makeEvent(ui.key, v: v),
);
}
},
// to-do: rustdesk translate or plugin translate ?
// to-do: RustDesk translate or plugin translate ?
child: Text(ui.text),
ffi: ffi,
);
@@ -170,6 +180,10 @@ void handleReloading(Map<String, dynamic> evt, String peer) {
if (evt['id'] == null || evt['location'] == null) {
return;
}
final ui = UiType.fromJson(evt);
addLocationUi(evt['location']!, evt['id']!, ui);
addLocationUi(evt['location']!, evt['id']!, UiType.fromJson(evt));
}
void handleOption(Map<String, dynamic> evt, String peer) {
updateOption(
evt['location'], evt['id'], evt['peer'] ?? '', evt['key'], evt['value']);
}