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,49 +0,0 @@
import 'dart:convert';
typedef PluginId = String;
// ui location
const String kLocationHostMainDisplayOthers =
'host|main|settings|display|others';
const String kLocationClientRemoteToolbarDisplay =
'client|remote|toolbar|display';
class MsgFromUi {
String remotePeerId;
String localPeerId;
String id;
String name;
String location;
String key;
String value;
String action;
MsgFromUi({
required this.remotePeerId,
required this.localPeerId,
required this.id,
required this.name,
required this.location,
required this.key,
required this.value,
required this.action,
});
Map<String, dynamic> toJson() {
return <String, dynamic>{
'remote_peer_id': remotePeerId,
'local_peer_id': localPeerId,
'id': id,
'name': name,
'location': location,
'key': key,
'value': value,
'action': action,
};
}
@override
String toString() {
return jsonEncode(toJson());
}
}

View File

@@ -1,166 +0,0 @@
import 'dart:collection';
const String kValueTrue = '1';
const String kValueFalse = '0';
class UiType {
String key;
String text;
String tooltip;
String action;
UiType(this.key, this.text, this.tooltip, this.action);
UiType.fromJson(Map<String, dynamic> json)
: key = json['key'] ?? '',
text = json['text'] ?? '',
tooltip = json['tooltip'] ?? '',
action = json['action'] ?? '';
static UiType? create(Map<String, dynamic> json) {
if (json['t'] == 'Button') {
return UiButton.fromJson(json['c']);
} else if (json['t'] == 'Checkbox') {
return UiCheckbox.fromJson(json['c']);
} else {
return null;
}
}
}
class UiButton extends UiType {
String icon;
UiButton(
{required String key,
required String text,
required this.icon,
required String tooltip,
required String action})
: super(key, text, tooltip, action);
UiButton.fromJson(Map<String, dynamic> json)
: icon = json['icon'] ?? '',
super.fromJson(json);
}
class UiCheckbox extends UiType {
UiCheckbox(
{required String key,
required String text,
required String tooltip,
required String action})
: super(key, text, tooltip, action);
UiCheckbox.fromJson(Map<String, dynamic> json) : super.fromJson(json);
}
class Location {
// location key:
// host|main|settings|display|others
// client|remote|toolbar|display
HashMap<String, UiType> ui;
Location(this.ui);
Location.fromJson(Map<String, dynamic> json) : ui = HashMap() {
json.forEach((key, value) {
var ui = UiType.create(value);
if (ui != null) {
this.ui[ui.key] = ui;
}
});
}
}
class ConfigItem {
String key;
String value;
String description;
String defaultValue;
ConfigItem(this.key, this.value, this.defaultValue, this.description);
ConfigItem.fromJson(Map<String, dynamic> json)
: key = json['key'] ?? '',
value = json['value'] ?? '',
description = json['description'] ?? '',
defaultValue = json['default'] ?? '';
static String get trueValue => kValueTrue;
static String get falseValue => kValueFalse;
static bool isTrue(String value) => value == kValueTrue;
static bool isFalse(String value) => value == kValueFalse;
}
class Config {
List<ConfigItem> local;
List<ConfigItem> peer;
Config(this.local, this.peer);
Config.fromJson(Map<String, dynamic> json)
: local = (json['local'] as List<dynamic>)
.map((e) => ConfigItem.fromJson(e))
.toList(),
peer = (json['peer'] as List<dynamic>)
.map((e) => ConfigItem.fromJson(e))
.toList();
}
class Desc {
String id;
String name;
String version;
String description;
String author;
String home;
String license;
String published;
String released;
String github;
Location location;
Config config;
Desc(
this.id,
this.name,
this.version,
this.description,
this.author,
this.home,
this.license,
this.published,
this.released,
this.github,
this.location,
this.config);
Desc.fromJson(Map<String, dynamic> json)
: id = json['id'] ?? '',
name = json['name'] ?? '',
version = json['version'] ?? '',
description = json['description'] ?? '',
author = json['author'] ?? '',
home = json['home'] ?? '',
license = json['license'] ?? '',
published = json['published'] ?? '',
released = json['released'] ?? '',
github = json['github'] ?? '',
location = Location(HashMap<String, UiType>.from(json['location'])),
config = Config(
(json['config'] as List<dynamic>)
.map((e) => ConfigItem.fromJson(e))
.toList(),
(json['config'] as List<dynamic>)
.map((e) => ConfigItem.fromJson(e))
.toList());
}
final mapPluginDesc = <String, Desc>{};
void updateDesc(Map<String, dynamic> desc) {
Desc d = Desc.fromJson(desc);
mapPluginDesc[d.id] = d;
}
Desc? getDesc(String id) {
return mapPluginDesc[id];
}

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

@@ -1,44 +0,0 @@
import 'package:flutter/material.dart';
import './common.dart';
import './desc.dart';
final Map<String, LocationModel> locationModels = {};
class PluginModel with ChangeNotifier {
final List<UiType> uiList = [];
void add(UiType ui) {
uiList.add(ui);
notifyListeners();
}
bool get isEmpty => uiList.isEmpty;
}
class LocationModel with ChangeNotifier {
final Map<PluginId, PluginModel> pluginModels = {};
void add(PluginId id, UiType ui) {
if (pluginModels[id] != null) {
pluginModels[id]!.add(ui);
} else {
var model = PluginModel();
model.add(ui);
pluginModels[id] = model;
notifyListeners();
}
}
bool get isEmpty => pluginModels.isEmpty;
}
void addLocationUi(String location, PluginId id, UiType ui) {
locationModels[location]?.add(id, ui);
}
LocationModel addLocation(String location) {
if (locationModels[location] == null) {
locationModels[location] = LocationModel();
}
return locationModels[location]!;
}

View File

@@ -1,175 +0,0 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_hbb/models/model.dart';
import 'package:provider/provider.dart';
import 'package:flutter_hbb/desktop/widgets/remote_toolbar.dart';
import 'package:flutter_hbb/models/platform_model.dart';
import './desc.dart';
import './model.dart';
import './common.dart';
class LocationItem extends StatelessWidget {
final String peerId;
final FFI ffi;
final String location;
final LocationModel locationModel;
LocationItem({
Key? key,
required this.peerId,
required this.ffi,
required this.location,
required this.locationModel,
}) : super(key: key);
bool get isEmpty => locationModel.isEmpty;
static LocationItem createLocationItem(
String peerId, FFI ffi, String location) {
final model = addLocation(location);
return LocationItem(
peerId: peerId,
ffi: ffi,
location: location,
locationModel: model,
);
}
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider.value(
value: locationModel,
child: Consumer<LocationModel>(builder: (context, model, child) {
return Column(
children: model.pluginModels.entries
.map((entry) => _buildPluginItem(entry.key, entry.value))
.toList(),
);
}),
);
}
Widget _buildPluginItem(PluginId id, PluginModel model) => PluginItem(
pluginId: id,
peerId: peerId,
ffi: ffi,
location: location,
pluginModel: model,
);
}
class PluginItem extends StatelessWidget {
final PluginId pluginId;
final String peerId;
final FFI ffi;
final String location;
final PluginModel pluginModel;
PluginItem({
Key? key,
required this.pluginId,
required this.peerId,
required this.ffi,
required this.location,
required this.pluginModel,
}) : 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(),
);
}),
);
}
// to-do: add plugin icon and tooltip
Widget _buildItem(UiType ui) {
switch (ui.runtimeType) {
case UiButton:
return _buildMenuButton(ui as UiButton);
case UiCheckbox:
return _buildCheckboxMenuButton(ui as UiCheckbox);
default:
return Container();
}
}
Uint8List _makeEvent(
String localPeerId,
String key, {
bool? v,
}) {
final event = MsgFromUi(
remotePeerId: peerId,
localPeerId: localPeerId,
id: pluginId,
name: getDesc(pluginId)?.name ?? '',
location: location,
key: key,
value:
v != null ? (v ? ConfigItem.trueValue : ConfigItem.falseValue) : '',
action: '',
);
return Uint8List.fromList(event.toString().codeUnits);
}
Widget _buildMenuButton(UiButton ui) {
return MenuButton(
onPressed: () {
() async {
final localPeerId = await bind.mainGetMyId();
bind.pluginEvent(
id: pluginId,
event: _makeEvent(localPeerId, ui.key),
);
}();
},
trailingIcon: Icon(
IconData(int.parse(ui.icon, radix: 16), fontFamily: 'MaterialIcons')),
// to-do: RustDesk translate or plugin translate ?
child: Text(ui.text),
ffi: ffi,
);
}
Widget _buildCheckboxMenuButton(UiCheckbox ui) {
final v =
bind.pluginGetSessionOption(id: pluginId, peer: peerId, key: ui.key);
if (v == null) {
// session or plugin not found
return Container();
}
return CkbMenuButton(
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),
);
}();
}
},
// to-do: rustdesk translate or plugin translate ?
child: Text(ui.text),
ffi: ffi,
);
}
}
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);
}

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';