mirror of
https://github.com/rustdesk/rustdesk.git
synced 2025-12-12 19:17:58 +00:00
plugin_framework, flutter event handlers
Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
43
flutter/lib/plugin/common.dart
Normal file
43
flutter/lib/plugin/common.dart
Normal file
@@ -0,0 +1,43 @@
|
||||
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 id;
|
||||
String name;
|
||||
String location;
|
||||
String key;
|
||||
String value;
|
||||
String action;
|
||||
|
||||
MsgFromUi({
|
||||
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>{
|
||||
'id': id,
|
||||
'name': name,
|
||||
'location': location,
|
||||
'key': key,
|
||||
'value': value,
|
||||
'action': action,
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return jsonEncode(toJson());
|
||||
}
|
||||
}
|
||||
166
flutter/lib/plugin/desc.dart
Normal file
166
flutter/lib/plugin/desc.dart
Normal file
@@ -0,0 +1,166 @@
|
||||
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];
|
||||
}
|
||||
11
flutter/lib/plugin/event.dart
Normal file
11
flutter/lib/plugin/event.dart
Normal 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']);
|
||||
}
|
||||
}
|
||||
76
flutter/lib/plugin/model.dart
Normal file
76
flutter/lib/plugin/model.dart
Normal file
@@ -0,0 +1,76 @@
|
||||
import 'package:flutter/material.dart';
|
||||
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;
|
||||
}
|
||||
|
||||
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]!;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
189
flutter/lib/plugin/widget.dart
Normal file
189
flutter/lib/plugin/widget.dart
Normal file
@@ -0,0 +1,189 @@
|
||||
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';
|
||||
|
||||
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;
|
||||
final KvModel kvModel;
|
||||
|
||||
PluginItem({
|
||||
Key? key,
|
||||
required this.pluginId,
|
||||
required this.peerId,
|
||||
required this.ffi,
|
||||
required this.location,
|
||||
required this.pluginModel,
|
||||
}) : kvModel = addKvModel(location, pluginId, peerId),
|
||||
super(key: key);
|
||||
|
||||
bool get isEmpty => pluginModel.isEmpty;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
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(),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildItem(UiType ui) {
|
||||
late Widget child;
|
||||
switch (ui.runtimeType) {
|
||||
case UiButton:
|
||||
child = _buildMenuButton(ui as UiButton);
|
||||
break;
|
||||
case UiCheckbox:
|
||||
child = _buildCheckboxMenuButton(ui as UiCheckbox);
|
||||
break;
|
||||
default:
|
||||
child = Container();
|
||||
}
|
||||
// to-do: add plugin icon and tooltip
|
||||
return child;
|
||||
}
|
||||
|
||||
Uint8List _makeEvent(
|
||||
String key, {
|
||||
bool? v,
|
||||
}) {
|
||||
final event = MsgFromUi(
|
||||
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: () => 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 ?
|
||||
child: Text(ui.text),
|
||||
ffi: ffi,
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildCheckboxMenuButton(UiCheckbox ui) {
|
||||
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();
|
||||
}
|
||||
return CkbMenuButton(
|
||||
value: ConfigItem.isTrue(v),
|
||||
onChanged: (v) {
|
||||
if (v != null) {
|
||||
bind.pluginEvent(
|
||||
id: pluginId,
|
||||
peer: peerId,
|
||||
event: _makeEvent(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;
|
||||
}
|
||||
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']);
|
||||
}
|
||||
Reference in New Issue
Block a user