From 600f604611c47bab8c48cfed8cd37cdfcd68304e Mon Sep 17 00:00:00 2001 From: rustdesk Date: Mon, 2 Aug 2021 20:54:56 +0800 Subject: [PATCH] refactor --- flutter_hbb/lib/common.dart | 98 +++++++------------------------- flutter_hbb/lib/home_page.dart | 8 +-- flutter_hbb/lib/model.dart | 26 ++++++++- flutter_hbb/lib/remote_page.dart | 39 ++++++------- 4 files changed, 71 insertions(+), 100 deletions(-) diff --git a/flutter_hbb/lib/common.dart b/flutter_hbb/lib/common.dart index 282af15b2..a40356de2 100644 --- a/flutter_hbb/lib/common.dart +++ b/flutter_hbb/lib/common.dart @@ -2,74 +2,11 @@ import 'package:flutter/material.dart'; import 'dart:async'; import 'package:flutter_easyloading/flutter_easyloading.dart'; import 'package:tuple/tuple.dart'; -import 'dart:io'; -final bool isZh = Platform.localeName.startsWith('zh'); +typedef F = String Function(String); -final langs = >{ - 'zh': { - 'Remote ID': '远程ID', - 'ID/Relay Server': 'ID/中继服务器', - 'About': '关于', - 'Mute': '静音', - 'ID Server': 'ID服务器', - 'Relay Server': '中继服务器', - 'Invalid IP': '无效IP', - 'Invalid format': '无效格式', - 'Cancel': '取消', - 'Close': '关闭', - 'Retry': '再试', - 'OK': '确认', - 'Password Required': '需要密码', - 'Enter your password': '输入你的密码', - 'Please enter your password': '请输入密码', - 'Remember password': '记住密码', - 'Wrong Password': '密码错误', - 'Do you want to enter again?': '还想输入一次吗?', - 'Connection Error': '连接错误', - 'Error': '错误', - 'Reset by the peer': '连接被对方关闭', - 'Connecting...': '正在连接...', - 'Connection in progress. Please wait.': '连接进行中,请稍等。', - 'Please try 1 minute later': '一分钟后再试', - 'Login Error': '登录错误', - 'Successful': '成功', - 'Custom Image Quality': '设置画面质量', - 'Privacy mode': '隐私模式', - 'Remove': '删除', - 'Adjust Window': '调节窗口', - 'Good image quality': '好画质', - 'Balanced': '一般画质', - 'Optimize reaction time': '优化反应时间', - 'Custom': '自定义画质', - 'Show remote cursor': '显示远程光标', - 'Disable clipboard': '禁止剪贴板', - 'Lock after session end': '断开后锁定远程电脑', - 'Insert': '插入', - 'Insert Lock': '锁定远程电脑', - 'Refresh': '刷新画面', - 'ID does not exist': 'ID不存在', - 'Failed to connect to rendezvous server': '连接服务器失败', - 'Remote desktop is offline': '远程电脑不在线', - 'Key mismatch': 'Key不匹配', - 'Timeout': '连接超时', - 'Failed to connect to relay server': '无法连接到中继服务器', - 'Failed to connect via rendezvous server': '无法通过服务器建立连接', - 'Failed to make direct connection to remote desktop': '无法建立直接连接', - 'OS Password': '操作系统密码', - 'Password': '密码', - 'Paste': '粘贴', - 'Logging in...': '正在登录...', - 'Are you sure to close the connection?': '是否确认关闭连接?', - 'Waiting for image...': '等待画面传输...', - }, - 'en': {} -}; - -String translate(name) { - final tmp = isZh ? langs['zh'] : langs['en']; - final v = tmp[name]; - return v != null ? v : name; +class Translator { + static F call; } class MyTheme { @@ -83,6 +20,15 @@ class MyTheme { static const Color border = Color(0xFFCCCCCC); } +final ButtonStyle flatButtonStyle = TextButton.styleFrom( + primary: MyTheme.accent, + minimumSize: Size(88, 36), + padding: EdgeInsets.symmetric(horizontal: 16.0), + shape: const RoundedRectangleBorder( + borderRadius: BorderRadius.all(Radius.circular(2.0)), + ), +); + void showLoading(String text, BuildContext context) { if (_hasDialog && context != null) { Navigator.pop(context); @@ -141,11 +87,11 @@ void msgbox(String type, String title, String text, BuildContext context, .shrinkWrap, //limits the touch area to the button area minWidth: 0, //wraps child's width height: 0, - child: FlatButton( - focusColor: MyTheme.accent, + child: TextButton( + style: flatButtonStyle, onPressed: onPressed, - child: - Text(translate(text), style: TextStyle(color: MyTheme.accent)))); + child: Text(Translator.call(text), + style: TextStyle(color: MyTheme.accent)))); dismissLoading(); if (_hasDialog) { @@ -153,7 +99,7 @@ void msgbox(String type, String title, String text, BuildContext context, } final buttons = [ Expanded(child: Container()), - wrap(translate('OK'), () { + wrap(Translator.call('OK'), () { dismissLoading(); Navigator.pop(context); }) @@ -164,7 +110,7 @@ void msgbox(String type, String title, String text, BuildContext context, if (hasCancel) { buttons.insert( 1, - wrap(translate('Cancel'), () { + wrap(Translator.call('Cancel'), () { dismissLoading(); })); } @@ -172,9 +118,9 @@ void msgbox(String type, String title, String text, BuildContext context, Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ - Text(translate(title), style: TextStyle(fontSize: 21)), + Text(Translator.call(title), style: TextStyle(fontSize: 21)), SizedBox(height: 20), - Text(translate(text), style: TextStyle(fontSize: 15)), + Text(Translator.call(text), style: TextStyle(fontSize: 15)), SizedBox(height: 20), Row( children: buttons, @@ -203,8 +149,8 @@ class _PasswordWidgetState extends State { obscureText: !_passwordVisible, //This will obscure text dynamically keyboardType: TextInputType.visiblePassword, decoration: InputDecoration( - labelText: translate('Password'), - hintText: translate('Enter your password'), + labelText: Translator.call('Password'), + hintText: Translator.call('Enter your password'), // Here is key idea suffixIcon: IconButton( icon: Icon( diff --git a/flutter_hbb/lib/home_page.dart b/flutter_hbb/lib/home_page.dart index 08b4ab532..9fa515fda 100644 --- a/flutter_hbb/lib/home_page.dart +++ b/flutter_hbb/lib/home_page.dart @@ -300,15 +300,15 @@ void showServer(BuildContext context) { ), ])), [ - FlatButton( - textColor: MyTheme.accent, + TextButton( + style: flatButtonStyle, onPressed: () { Navigator.pop(context); }, child: Text(translate('Cancel')), ), - FlatButton( - textColor: MyTheme.accent, + TextButton( + style: flatButtonStyle, onPressed: () { if (formKey.currentState.validate()) { formKey.currentState.save(); diff --git a/flutter_hbb/lib/model.dart b/flutter_hbb/lib/model.dart index 38a577f4f..d22e110e8 100644 --- a/flutter_hbb/lib/model.dart +++ b/flutter_hbb/lib/model.dart @@ -44,6 +44,7 @@ class FfiModel with ChangeNotifier { get pi => _pi; FfiModel() { + Translator.call = translate; clear(); () async { await FFI.init(); @@ -193,7 +194,7 @@ class FfiModel with ChangeNotifier { initializeCursorAndCanvas(); } if (displays.length > 0) { - showLoading(translate('Waiting for image...'), context); + showLoading(translate('Connected, waiting for image...'), context); _waitForImage = true; } notifyListeners(); @@ -728,3 +729,26 @@ void initializeCursorAndCanvas() async { FFI.ffiModel.display.x, FFI.ffiModel.display.y, xCursor, yCursor); FFI.canvasModel.update(xCanvas, yCanvas, scale); } + +final bool isCn = Platform.localeName.endsWith('CN'); + +final langs = >{ + 'cn': { + 'Remote ID': '远程ID', + 'Paste': '粘贴', + 'Are you sure to close the connection?': '是否确认关闭连接?', + }, + 'en': {} +}; + +String translate(name) { + final tmp = isCn ? langs['cn'] : langs['en']; + final v = tmp[name]; + if (v == null) { + var a = 'translate'; + var b = '{"locale": "${Platform.localeName}", "text": "${name}"}'; + return FFI.getByName(a, b); + } else { + return v; + } +} diff --git a/flutter_hbb/lib/remote_page.dart b/flutter_hbb/lib/remote_page.dart index 4cf055088..d5299d71c 100644 --- a/flutter_hbb/lib/remote_page.dart +++ b/flutter_hbb/lib/remote_page.dart @@ -360,12 +360,13 @@ class _RemotePageState extends State { .shrinkWrap, //limits the touch area to the button area minWidth: 0, //wraps child's width height: 0, - child: FlatButton( - splashColor: MyTheme.accent, - shape: RoundedRectangleBorder( - borderRadius: BorderRadius.circular(5.0), + child: TextButton( + style: TextButton.styleFrom( + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(5.0), + ), + primary: active == true ? MyTheme.accent80 : null, ), - color: active == true ? MyTheme.accent80 : null, child: icon != null ? Icon(icon, size: 17, color: Colors.white) : Text(translate(text), @@ -596,16 +597,16 @@ void enterPasswordDialog(String id, BuildContext context) { ), ]), [ - FlatButton( - textColor: MyTheme.accent, + TextButton( + style: flatButtonStyle, onPressed: () { Navigator.pop(context); Navigator.pop(context); }, child: Text(translate('Cancel')), ), - FlatButton( - textColor: MyTheme.accent, + TextButton( + style: flatButtonStyle, onPressed: () { var text = controller.text.trim(); if (text == '') return; @@ -624,16 +625,16 @@ void wrongPasswordDialog(String id, BuildContext context) { context, (_) => Tuple3(Text(translate('Wrong Password')), Text(translate('Do you want to enter again?')), [ - FlatButton( - textColor: MyTheme.accent, + TextButton( + style: flatButtonStyle, onPressed: () { Navigator.pop(context); Navigator.pop(context); }, child: Text(translate('Cancel')), ), - FlatButton( - textColor: MyTheme.accent, + TextButton( + style: flatButtonStyle, onPressed: () { enterPasswordDialog(id, context); }, @@ -781,8 +782,8 @@ void showActions(BuildContext context) { child: Row( children: ([ Text(translate('OS Password')), - FlatButton( - textColor: MyTheme.accent, + TextButton( + style: flatButtonStyle, onPressed: () { showSetOSPassword(context); Navigator.pop(context); @@ -850,15 +851,15 @@ void showSetOSPassword(BuildContext context) async { PasswordWidget(controller: controller), ]), [ - FlatButton( - textColor: MyTheme.accent, + TextButton( + style: flatButtonStyle, onPressed: () { Navigator.pop(context); }, child: Text(translate('Cancel')), ), - FlatButton( - textColor: MyTheme.accent, + TextButton( + style: flatButtonStyle, onPressed: () { var text = controller.text.trim(); savePassword(FFI.id, text);