add ui event

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou
2023-04-20 20:57:47 +08:00
parent d9755abbc2
commit 9a08e0bed4
9 changed files with 262 additions and 63 deletions

View File

@@ -1 +1,43 @@
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,45 +1,58 @@
import 'dart:collection';
class UiButton {
String key;
String text;
String icon;
String tooltip;
String action;
UiButton(this.key, this.text, this.icon, this.tooltip, this.action);
UiButton.fromJson(Map<String, dynamic> json)
: key = json['key'] ?? '',
text = json['text'] ?? '',
icon = json['icon'] ?? '',
tooltip = json['tooltip'] ?? '',
action = json['action'] ?? '';
}
class UiCheckbox {
String key;
String text;
String tooltip;
String action;
UiCheckbox(this.key, this.text, this.tooltip, this.action);
UiCheckbox.fromJson(Map<String, dynamic> json)
: key = json['key'] ?? '',
text = json['text'] ?? '',
tooltip = json['tooltip'] ?? '',
action = json['action'] ?? '';
}
const String kValueTrue = '1';
const String kValueFalse = '0';
class UiType {
UiButton? button;
UiCheckbox? checkbox;
String key;
String text;
String tooltip;
String action;
UiType(this.key, this.text, this.tooltip, this.action);
UiType.fromJson(Map<String, dynamic> json)
: button = json['t'] == 'Button' ? UiButton.fromJson(json['c']) : null,
checkbox =
json['t'] != 'Checkbox' ? UiCheckbox.fromJson(json['c']) : null;
: key = json['key'] ?? '',
text = json['text'] ?? '',
tooltip = json['tooltip'] ?? '',
action = json['action'] ?? '';
bool get isValid => button != null || checkbox != null;
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 {
@@ -49,6 +62,14 @@ class Location {
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 {
@@ -63,6 +84,11 @@ class ConfigItem {
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 {

View File

@@ -15,6 +15,8 @@ class LocationModel with ChangeNotifier {
uiList.add(ui);
notifyListeners();
}
bool get isEmpty => uiList.isEmpty;
}
void addLocation(PluginId id, String location, UiType ui) {

View File

@@ -40,8 +40,5 @@ void handleReloading(Map<String, dynamic> evt, String peer) {
return;
}
final ui = UiType.fromJson(evt);
if (!ui.isValid) {
return;
}
addLocation(evt['id']!, evt['location']!, ui);
}

View File

@@ -1,27 +1,110 @@
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: [],
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,
);
}
}