plugin_framework, ui tmp

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou
2023-04-20 22:53:43 +08:00
parent 9a08e0bed4
commit 1b303b7b27
11 changed files with 147 additions and 93 deletions

View File

@@ -1,43 +0,0 @@
import 'dart:convert';
typedef PluginId = String;
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,30 +0,0 @@
import 'package:flutter/material.dart';
import './common.dart';
import './desc.dart';
// ui location
// host|main|settings|display|others
// client|remote|toolbar|display
final Map<PluginId, Map<String, LocationModel>> locationModels = {};
class LocationModel with ChangeNotifier {
final List<UiType> uiList = [];
void add(UiType ui) {
uiList.add(ui);
notifyListeners();
}
bool get isEmpty => uiList.isEmpty;
}
void addLocation(PluginId id, String location, UiType ui) {
if (!locationModels.containsKey(id)) {
locationModels[id] = {};
}
if (!locationModels[id]!.containsKey(location)) {
locationModels[id]![location] = LocationModel();
}
locationModels[id]![location]!.add(ui);
}

View File

@@ -1,44 +0,0 @@
import 'package:flutter/material.dart';
import './desc.dart';
import './model.dart';
final Map<String, PluginWidget> pluginWidgets = {};
class PluginWidget {
final String id;
final String name;
final String location;
final Widget widget;
PluginWidget({
required this.id,
required this.name,
required this.location,
required this.widget,
});
// static Widget createButton(UiButton btn) {}
// static Widget createCheckbox(UiCheckbox chk) {}
// // ui location
// // host|main|settings|display|others
// // client|remote|toolbar|display
// static Widget? create(String id, String locatin, UiType ui) {
// if (ui.button != null) {
// return createButton(ui.button!);
// } else if (ui.checkbox != null) {
// return createCheckbox(ui.checkbox!);
// } else {
// return null;
// }
// }
}
void handleReloading(Map<String, dynamic> evt, String peer) {
if (evt['id'] == null || evt['location'] == null) {
return;
}
final ui = UiType.fromJson(evt);
addLocation(evt['id']!, evt['location']!, ui);
}

View File

@@ -1,110 +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 Display extends StatelessWidget {
final PluginId pluginId;
final String peerId;
final FFI ffi;
final String location;
final LocationModel locationModel;
Display({
Key? key,
required this.pluginId,
required this.peerId,
required this.ffi,
required this.location,
required this.locationModel,
}) : super(key: key);
bool get isEmpty => locationModel.isEmpty;
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider.value(
value: locationModel,
child: Consumer<LocationModel>(builder: (context, model, child) {
return Column(
children: locationModel.uiList.map((ui) => _buildItem(ui)).toList(),
);
}),
);
}
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),
);
}();
},
// 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);
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,
);
}
}