mirror of
https://github.com/venera-app/venera.git
synced 2025-09-27 07:47:24 +00:00
Improve ui api
This commit is contained in:
@@ -3,7 +3,6 @@ import 'dart:io';
|
||||
import 'dart:math' as math;
|
||||
import 'package:crypto/crypto.dart';
|
||||
import 'package:dio/io.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:html/parser.dart' as html;
|
||||
import 'package:html/dom.dart' as dom;
|
||||
@@ -20,9 +19,8 @@ import 'package:pointycastle/block/modes/cbc.dart';
|
||||
import 'package:pointycastle/block/modes/cfb.dart';
|
||||
import 'package:pointycastle/block/modes/ecb.dart';
|
||||
import 'package:pointycastle/block/modes/ofb.dart';
|
||||
import 'package:url_launcher/url_launcher_string.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
import 'package:venera/components/components.dart';
|
||||
import 'package:venera/components/js_ui.dart';
|
||||
import 'package:venera/foundation/app.dart';
|
||||
import 'package:venera/network/app_dio.dart';
|
||||
import 'package:venera/network/cookie_jar.dart';
|
||||
@@ -42,7 +40,7 @@ class JavaScriptRuntimeException implements Exception {
|
||||
}
|
||||
}
|
||||
|
||||
class JsEngine with _JSEngineApi, _JsUiApi {
|
||||
class JsEngine with _JSEngineApi, JsUiApi {
|
||||
factory JsEngine() => _cache ?? (_cache = JsEngine._create());
|
||||
|
||||
static JsEngine? _cache;
|
||||
@@ -156,7 +154,7 @@ class JsEngine with _JSEngineApi, _JsUiApi {
|
||||
case "delay":
|
||||
return Future.delayed(Duration(milliseconds: message["time"]));
|
||||
case "UI":
|
||||
handleUIMessage(Map.from(message));
|
||||
return handleUIMessage(Map.from(message));
|
||||
}
|
||||
}
|
||||
return null;
|
||||
@@ -690,48 +688,3 @@ class JSAutoFreeFunction {
|
||||
func.free();
|
||||
});
|
||||
}
|
||||
|
||||
mixin class _JsUiApi {
|
||||
void handleUIMessage(Map<String, dynamic> message) {
|
||||
switch (message['function']) {
|
||||
case 'showMessage':
|
||||
var m = message['message'];
|
||||
if (m.toString().isNotEmpty) {
|
||||
App.rootContext.showMessage(message: m.toString());
|
||||
}
|
||||
case 'showDialog':
|
||||
_showDialog(message);
|
||||
case 'launchUrl':
|
||||
var url = message['url'];
|
||||
if (url.toString().isNotEmpty) {
|
||||
launchUrlString(url.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _showDialog(Map<String, dynamic> message) {
|
||||
var title = message['title'];
|
||||
var content = message['content'];
|
||||
var actions = <String, JSAutoFreeFunction>{};
|
||||
for (var action in message['actions']) {
|
||||
// [message] will be released after the method call, causing the action to be invalid, so we need to duplicate it
|
||||
(action['callback'] as JSInvokable).dup();
|
||||
actions[action['text']] = JSAutoFreeFunction(action['callback']);
|
||||
}
|
||||
showDialog(context: App.rootContext, builder: (context) {
|
||||
return ContentDialog(
|
||||
title: title,
|
||||
content: Text(content).paddingHorizontal(16),
|
||||
actions: actions.entries.map((entry) {
|
||||
return TextButton(
|
||||
onPressed: () {
|
||||
entry.value.call([]);
|
||||
context.pop();
|
||||
},
|
||||
child: Text(entry.key),
|
||||
);
|
||||
}).toList(),
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user