Merge pull request #1596 from Heap-Hop/master

Optimize flutter
This commit is contained in:
RustDesk 2022-09-22 07:39:00 +08:00 committed by GitHub
commit f5b7c34c81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 195 additions and 141 deletions

View File

@ -197,30 +197,39 @@ class MyTheme {
],
);
static changeTo(bool dark) {
if (isDarkTheme() != dark) {
Get.find<SharedPreferences>().setString("darkTheme", dark ? "Y" : "");
Get.changeThemeMode(dark ? ThemeMode.dark : ThemeMode.light);
static ThemeMode getThemeModePreference() {
return themeModeFromString(
Get.find<SharedPreferences>().getString("themeMode") ?? "");
}
static void changeDarkMode(ThemeMode mode) {
final preference = getThemeModePreference();
if (preference != mode) {
if (mode == ThemeMode.system) {
Get.find<SharedPreferences>().setString("themeMode", "");
} else {
Get.find<SharedPreferences>()
.setString("themeMode", mode.toShortString());
}
Get.changeThemeMode(mode);
if (desktopType == DesktopType.main) {
bind.mainChangeTheme(dark: dark);
bind.mainChangeTheme(dark: currentThemeMode().toShortString());
}
}
}
static bool _themeInitialed = false;
static ThemeMode initialThemeMode({bool mainPage = false}) {
bool dark;
// Brightnesss is always light on windows, Flutter 3.0.5
if (_themeInitialed || !mainPage || Platform.isWindows) {
dark = isDarkTheme();
static ThemeMode currentThemeMode() {
final preference = getThemeModePreference();
if (preference == ThemeMode.system) {
if (WidgetsBinding.instance.platformDispatcher.platformBrightness ==
Brightness.light) {
return ThemeMode.light;
} else {
return ThemeMode.dark;
}
} else {
dark = WidgetsBinding.instance.platformDispatcher.platformBrightness ==
Brightness.dark;
Get.find<SharedPreferences>().setString("darkTheme", dark ? "Y" : "");
return preference;
}
_themeInitialed = true;
return dark ? ThemeMode.dark : ThemeMode.light;
}
static ColorThemeExtension color(BuildContext context) {
@ -230,10 +239,23 @@ class MyTheme {
static TabbarTheme tabbar(BuildContext context) {
return Theme.of(context).extension<TabbarTheme>()!;
}
static ThemeMode themeModeFromString(String v) {
switch (v) {
case "light":
return ThemeMode.light;
case "dark":
return ThemeMode.dark;
default:
return ThemeMode.system;
}
}
}
bool isDarkTheme() {
return "Y" == Get.find<SharedPreferences>().getString("darkTheme");
extension ParseToString on ThemeMode {
String toShortString() {
return toString().split('.').last;
}
}
final ButtonStyle flatButtonStyle = TextButton.styleFrom(

View File

@ -303,7 +303,7 @@ class AddressBookPeerWidget extends BasePeerWidget {
static List<Peer> _loadPeers() {
debugPrint("_loadPeers : ${gFFI.abModel.peers.toString()}");
return gFFI.abModel.peers.map((e) {
return Peer.fromJson(e['id'], e);
return Peer.fromJson(e);
}).toList();
}

View File

@ -27,13 +27,11 @@ final peerCardUiType = PeerUiType.grid.obs;
class _PeerCard extends StatefulWidget {
final Peer peer;
final RxString alias;
final Function(BuildContext, String) connect;
final PopupMenuEntryBuilder popupMenuEntryBuilder;
const _PeerCard(
{required this.peer,
required this.alias,
required this.connect,
required this.popupMenuEntryBuilder,
Key? key})
@ -77,7 +75,7 @@ class _PeerCardState extends State<_PeerCard>
child: ListTile(
contentPadding: const EdgeInsets.only(left: 12),
subtitle: Text('${peer.username}@${peer.hostname}'),
title: Text(formatID(peer.id)),
title: Text(peer.alias.isEmpty ? formatID(peer.id) : peer.alias),
leading: Container(
padding: const EdgeInsets.all(6),
color: str2color('${peer.id}${peer.platform}', 0x7f),
@ -216,6 +214,7 @@ class _PeerCardState extends State<_PeerCard>
Widget _buildPeerCard(
BuildContext context, Peer peer, Rx<BoxDecoration?> deco) {
final name = '${peer.username}@${peer.hostname}';
return Card(
color: Colors.transparent,
elevation: 0,
@ -246,24 +245,18 @@ class _PeerCardState extends State<_PeerCard>
Row(
children: [
Expanded(
child: Obx(() {
final name = widget.alias.value.isEmpty
? '${peer.username}@${peer.hostname}'
: widget.alias.value;
return Tooltip(
message: name,
waitDuration:
const Duration(seconds: 1),
child: Text(
name,
style: const TextStyle(
color: Colors.white70,
fontSize: 12),
textAlign: TextAlign.center,
overflow: TextOverflow.ellipsis,
),
);
}),
child: Tooltip(
message: name,
waitDuration: const Duration(seconds: 1),
child: Text(
name,
style: const TextStyle(
color: Colors.white70,
fontSize: 12),
textAlign: TextAlign.center,
overflow: TextOverflow.ellipsis,
),
),
),
],
),
@ -287,7 +280,8 @@ class _PeerCardState extends State<_PeerCard>
backgroundColor: peer.online
? Colors.green
: Colors.yellow)),
Text(formatID(peer.id))
Text(
peer.alias.isEmpty ? formatID(peer.id) : peer.alias)
]).paddingSymmetric(vertical: 8),
_actionMore(peer),
],
@ -338,20 +332,14 @@ class _PeerCardState extends State<_PeerCard>
}
abstract class BasePeerCard extends StatelessWidget {
final RxString alias = ''.obs;
final Peer peer;
BasePeerCard({required this.peer, Key? key}) : super(key: key) {
bind
.mainGetPeerOption(id: peer.id, key: 'alias')
.then((value) => alias.value = value);
}
BasePeerCard({required this.peer, Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return _PeerCard(
peer: peer,
alias: alias,
connect: (BuildContext context, String id) => connect(context, id),
popupMenuEntryBuilder: _buildPopupMenuEntry,
);
@ -379,7 +367,7 @@ abstract class BasePeerCard extends StatelessWidget {
bool isRDP = false}) {
return MenuEntryButton<String>(
childBuilder: (TextStyle? style) => Text(
translate(title),
title,
style: style,
),
proc: () {
@ -396,8 +384,13 @@ abstract class BasePeerCard extends StatelessWidget {
}
@protected
MenuEntryBase<String> _connectAction(BuildContext context, String id) {
return _connectCommonAction(context, id, 'Connect');
MenuEntryBase<String> _connectAction(BuildContext context, Peer peer) {
return _connectCommonAction(
context,
peer.id,
peer.alias.isEmpty
? translate('Connect')
: "${translate('Connect')} ${peer.id}");
}
@protected
@ -405,7 +398,7 @@ abstract class BasePeerCard extends StatelessWidget {
return _connectCommonAction(
context,
id,
'Transfer File',
translate('Transfer File'),
isFileTransfer: true,
);
}
@ -415,7 +408,7 @@ abstract class BasePeerCard extends StatelessWidget {
return _connectCommonAction(
context,
id,
'TCP Tunneling',
translate('TCP Tunneling'),
isTcpTunneling: true,
);
}
@ -578,7 +571,7 @@ abstract class BasePeerCard extends StatelessWidget {
void _rename(String id, bool isAddressBook) async {
RxBool isInProgress = false.obs;
var name = await bind.mainGetPeerOption(id: id, key: 'alias');
var name = peer.alias;
var controller = TextEditingController(text: name);
if (isAddressBook) {
final peer = gFFI.abModel.peers.firstWhere((p) => id == p['id']);
@ -597,7 +590,12 @@ abstract class BasePeerCard extends StatelessWidget {
gFFI.abModel.setPeerOption(id, 'alias', name);
await gFFI.abModel.updateAb();
}
alias.value = await bind.mainGetPeerOption(id: peer.id, key: 'alias');
if (isAddressBook) {
gFFI.abModel.getAb();
} else {
bind.mainLoadRecentPeers();
bind.mainLoadFavPeers();
}
close();
isInProgress.value = false;
}
@ -642,7 +640,7 @@ class RecentPeerCard extends BasePeerCard {
Future<List<MenuEntryBase<String>>> _buildMenuItems(
BuildContext context) async {
final List<MenuEntryBase<String>> menuItems = [
_connectAction(context, peer.id),
_connectAction(context, peer),
_transferFileAction(context, peer.id),
_tcpTunnelingAction(context, peer.id),
];
@ -674,7 +672,7 @@ class FavoritePeerCard extends BasePeerCard {
Future<List<MenuEntryBase<String>>> _buildMenuItems(
BuildContext context) async {
final List<MenuEntryBase<String>> menuItems = [
_connectAction(context, peer.id),
_connectAction(context, peer),
_transferFileAction(context, peer.id),
_tcpTunnelingAction(context, peer.id),
];
@ -708,7 +706,7 @@ class DiscoveredPeerCard extends BasePeerCard {
Future<List<MenuEntryBase<String>>> _buildMenuItems(
BuildContext context) async {
final List<MenuEntryBase<String>> menuItems = [
_connectAction(context, peer.id),
_connectAction(context, peer),
_transferFileAction(context, peer.id),
_tcpTunnelingAction(context, peer.id),
];
@ -739,7 +737,7 @@ class AddressBookPeerCard extends BasePeerCard {
Future<List<MenuEntryBase<String>>> _buildMenuItems(
BuildContext context) async {
final List<MenuEntryBase<String>> menuItems = [
_connectAction(context, peer.id),
_connectAction(context, peer),
_transferFileAction(context, peer.id),
_tcpTunnelingAction(context, peer.id),
];

View File

@ -205,22 +205,28 @@ class _GeneralState extends State<_General> {
}
Widget theme() {
change() {
MyTheme.changeTo(!isDarkTheme());
final current = MyTheme.getThemeModePreference().toShortString();
onChanged(String value) {
MyTheme.changeDarkMode(MyTheme.themeModeFromString(value));
setState(() {});
}
return _Card(title: 'Theme', children: [
GestureDetector(
onTap: change,
child: Row(
children: [
Checkbox(value: isDarkTheme(), onChanged: (_) => change())
.marginOnly(right: 5),
Expanded(child: Text(translate('Dark Theme'))),
],
).marginOnly(left: _kCheckBoxLeftMargin),
)
_Radio<String>(context,
value: "light",
groupValue: current,
label: "Light",
onChanged: onChanged),
_Radio<String>(context,
value: "dark",
groupValue: current,
label: "Dark",
onChanged: onChanged),
_Radio<String>(context,
value: "system",
groupValue: current,
label: "Follow System",
onChanged: onChanged),
]);
}

View File

@ -247,7 +247,7 @@ class _PortForwardPageState extends State<PortForwardPage>
height: _kRowHeight,
decoration: BoxDecoration(
color: index % 2 == 0
? isDarkTheme()
? MyTheme.currentThemeMode() == ThemeMode.dark
? const Color(0xFF202020)
: const Color(0xFFF4F5F6)
: MyTheme.color(context).bg),

View File

@ -120,7 +120,7 @@ void runRemoteScreen(Map<String, dynamic> argument) async {
title: 'RustDesk - Remote Desktop',
theme: MyTheme.lightTheme,
darkTheme: MyTheme.darkTheme,
themeMode: MyTheme.initialThemeMode(),
themeMode: MyTheme.currentThemeMode(),
home: DesktopRemoteScreen(
params: argument,
),
@ -146,7 +146,7 @@ void runFileTransferScreen(Map<String, dynamic> argument) async {
title: 'RustDesk - File Transfer',
theme: MyTheme.lightTheme,
darkTheme: MyTheme.darkTheme,
themeMode: MyTheme.initialThemeMode(),
themeMode: MyTheme.currentThemeMode(),
home: DesktopFileTransferScreen(params: argument),
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
@ -171,7 +171,7 @@ void runPortForwardScreen(Map<String, dynamic> argument) async {
title: 'RustDesk - Port Forward',
theme: MyTheme.lightTheme,
darkTheme: MyTheme.darkTheme,
themeMode: MyTheme.initialThemeMode(),
themeMode: MyTheme.currentThemeMode(),
home: DesktopPortForwardScreen(params: argument),
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
@ -196,7 +196,7 @@ void runConnectionManagerScreen() async {
debugShowCheckedModeBanner: false,
theme: MyTheme.lightTheme,
darkTheme: MyTheme.darkTheme,
themeMode: MyTheme.initialThemeMode(),
themeMode: MyTheme.currentThemeMode(),
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
@ -233,12 +233,21 @@ class _AppState extends State<App> {
void initState() {
super.initState();
WidgetsBinding.instance.window.onPlatformBrightnessChanged = () {
final userPreference = MyTheme.getThemeModePreference();
if (userPreference != ThemeMode.system) return;
WidgetsBinding.instance.handlePlatformBrightnessChanged();
var system =
WidgetsBinding.instance.platformDispatcher.platformBrightness;
var current = isDarkTheme() ? Brightness.dark : Brightness.light;
if (current != system) {
MyTheme.changeTo(system == Brightness.dark);
final systemIsDark =
WidgetsBinding.instance.platformDispatcher.platformBrightness ==
Brightness.dark;
final ThemeMode to;
if (systemIsDark) {
to = ThemeMode.dark;
} else {
to = ThemeMode.light;
}
Get.changeThemeMode(to);
if (desktopType == DesktopType.main) {
bind.mainChangeTheme(dark: to.toShortString());
}
};
}
@ -263,7 +272,7 @@ class _AppState extends State<App> {
title: 'RustDesk',
theme: MyTheme.lightTheme,
darkTheme: MyTheme.darkTheme,
themeMode: MyTheme.initialThemeMode(mainPage: true),
themeMode: MyTheme.currentThemeMode(),
home: isDesktop
? const DesktopTabPage()
: !isAndroid
@ -309,7 +318,7 @@ _registerEventHandler() {
platformFFI.registerEventHandler('theme', 'theme', (evt) async {
String? dark = evt['dark'];
if (dark != null) {
MyTheme.changeTo(dark == 'true');
MyTheme.changeDarkMode(MyTheme.themeModeFromString(dark));
}
});
platformFFI.registerEventHandler('language', 'language', (_) async {

View File

@ -60,7 +60,7 @@ class _SettingsState extends State<SettingsPage> with WidgetsBindingObserver {
_enableAbr = enableAbrRes;
}
_enableAbr = isDarkTheme();
// _isDarkMode = MyTheme.currentDarkMode(); // TODO
if (update) {
setState(() {});
@ -184,7 +184,7 @@ class _SettingsState extends State<SettingsPage> with WidgetsBindingObserver {
onToggle: (v) {
setState(() {
_isDarkMode = !_isDarkMode;
MyTheme.changeTo(_isDarkMode);
// MyTheme.changeDarkMode(_isDarkMode); // TODO
});
},
)

View File

@ -22,7 +22,6 @@ import '../common.dart';
import '../common/shared_state.dart';
import '../utils/image.dart' as img;
import '../mobile/widgets/dialog.dart';
import 'peer_model.dart';
import 'platform_model.dart';
typedef HandleMsgBox = Function(Map<String, dynamic> evt, String id);
@ -1107,23 +1106,6 @@ class FFI {
id: id, msg: json.encode(modify({'x': '$x2', 'y': '$y2'})));
}
/// List the saved peers.
Future<List<Peer>> peers() async {
try {
var str = await bind.mainGetRecentPeers();
if (str == '') return [];
List<dynamic> peers = json.decode(str);
return peers
.map((s) => s as List<dynamic>)
.map((s) =>
Peer.fromJson(s[0] as String, s[1] as Map<String, dynamic>))
.toList();
} catch (e) {
debugPrint('peers(): $e');
}
return [];
}
/// Connect with the given [id]. Only transfer file if [isFileTransfer], only port forward if [isPortForward].
connect(String id,
{bool isFileTransfer = false,

View File

@ -7,13 +7,16 @@ class Peer {
final String username;
final String hostname;
final String platform;
final String alias;
final List<dynamic> tags;
bool online = false;
Peer.fromJson(this.id, Map<String, dynamic> json)
: username = json['username'] ?? '',
Peer.fromJson(Map<String, dynamic> json)
: id = json['id'] ?? '',
username = json['username'] ?? '',
hostname = json['hostname'] ?? '',
platform = json['platform'] ?? '',
alias = json['alias'] ?? '',
tags = json['tags'] ?? [];
Peer({
@ -21,6 +24,7 @@ class Peer {
required this.username,
required this.hostname,
required this.platform,
required this.alias,
required this.tags,
});
@ -30,6 +34,7 @@ class Peer {
username: '...',
hostname: '...',
platform: '...',
alias: '',
tags: []);
}
@ -109,11 +114,9 @@ class Peers extends ChangeNotifier {
try {
if (peersStr == "") return [];
List<dynamic> peers = json.decode(peersStr);
return peers
.map((s) => s as List<dynamic>)
.map((s) =>
Peer.fromJson(s[0] as String, s[1] as Map<String, dynamic>))
.toList();
return peers.map((peer) {
return Peer.fromJson(peer as Map<String, dynamic>);
}).toList();
} catch (e) {
debugPrint('peers(): $e');
}

View File

@ -379,8 +379,8 @@ pub mod connection_manager {
);
}
fn change_theme(&self, dark: bool) {
self.push_event("theme", vec![("dark", &dark.to_string())]);
fn change_theme(&self, dark: String) {
self.push_event("theme", vec![("dark", &dark)]);
}
fn change_language(&self) {

View File

@ -7,11 +7,11 @@ use std::{
use flutter_rust_bridge::{StreamSink, SyncReturn, ZeroCopyBuffer};
use serde_json::json;
use hbb_common::ResultType;
use hbb_common::{
config::{self, LocalConfig, PeerConfig, ONLINE},
fs, log,
};
use hbb_common::{message_proto::Hash, ResultType};
use crate::flutter::{self, SESSIONS};
use crate::start_server;
@ -567,9 +567,20 @@ pub fn main_forget_password(id: String) {
pub fn main_get_recent_peers() -> String {
if !config::APP_DIR.read().unwrap().is_empty() {
let peers: Vec<(String, config::PeerInfoSerde)> = PeerConfig::peers()
let peers: Vec<HashMap<&str, String>> = PeerConfig::peers()
.drain(..)
.map(|(id, _, p)| (id, p.info))
.map(|(id, _, p)| {
HashMap::<&str, String>::from_iter([
("id", id),
("username", p.info.username.clone()),
("hostname", p.info.hostname.clone()),
("platform", p.info.platform.clone()),
(
"alias",
p.options.get("alias").unwrap_or(&"".to_owned()).to_owned(),
),
])
})
.collect();
serde_json::ser::to_string(&peers).unwrap_or("".to_owned())
} else {
@ -579,9 +590,20 @@ pub fn main_get_recent_peers() -> String {
pub fn main_load_recent_peers() {
if !config::APP_DIR.read().unwrap().is_empty() {
let peers: Vec<(String, config::PeerInfoSerde)> = PeerConfig::peers()
let peers: Vec<HashMap<&str, String>> = PeerConfig::peers()
.drain(..)
.map(|(id, _, p)| (id, p.info))
.map(|(id, _, p)| {
HashMap::<&str, String>::from_iter([
("id", id),
("username", p.info.username.clone()),
("hostname", p.info.hostname.clone()),
("platform", p.info.platform.clone()),
(
"alias",
p.options.get("alias").unwrap_or(&"".to_owned()).to_owned(),
),
])
})
.collect();
if let Some(s) = flutter::GLOBAL_EVENT_STREAM
.read()
@ -603,11 +625,20 @@ pub fn main_load_recent_peers() {
pub fn main_load_fav_peers() {
if !config::APP_DIR.read().unwrap().is_empty() {
let favs = get_fav();
let peers: Vec<(String, config::PeerInfoSerde)> = PeerConfig::peers()
let peers: Vec<HashMap<&str, String>> = PeerConfig::peers()
.into_iter()
.filter_map(|(id, _, peer)| {
.filter_map(|(id, _, p)| {
if favs.contains(&id) {
Some((id, peer.info))
Some(HashMap::<&str, String>::from_iter([
("id", id),
("username", p.info.username.clone()),
("hostname", p.info.hostname.clone()),
("platform", p.info.platform.clone()),
(
"alias",
p.options.get("alias").unwrap_or(&"".to_owned()).to_owned(),
),
]))
} else {
None
}
@ -661,10 +692,10 @@ fn main_broadcast_message(data: &HashMap<&str, &str>) {
}
}
pub fn main_change_theme(dark: bool) {
pub fn main_change_theme(dark: String) {
main_broadcast_message(&HashMap::from([
("name", "theme"),
("dark", &dark.to_string()),
("dark", &dark),
]));
send_to_cm(&crate::ipc::Data::Theme(dark));
}

View File

@ -182,7 +182,7 @@ pub enum Data {
#[cfg(not(any(target_os = "android", target_os = "ios", feature = "cli")))]
Mouse(DataMouse),
Control(DataControl),
Theme(bool),
Theme(String),
Language(String),
Empty,
}

View File

@ -18,7 +18,7 @@ use hbb_common::{
tokio::{self, sync::mpsc, time},
};
use crate::common::{get_app_name};
use crate::common::get_app_name;
use crate::ipc;
use crate::ui_interface::{
check_mouse_time, closing, create_shortcut, current_is_wayland, fix_login_wayland,
@ -73,9 +73,7 @@ fn check_connect_status(
let (tx, rx) = mpsc::unbounded_channel::<ipc::Data>();
let password = Arc::new(Mutex::new(String::default()));
let cloned_password = password.clone();
std::thread::spawn(move || {
crate::ui_interface::check_connect_status_(reconnect, rx)
});
std::thread::spawn(move || crate::ui_interface::check_connect_status_(reconnect, rx));
(status, options, tx, password)
}
@ -525,9 +523,16 @@ impl UI {
fn get_lan_peers(&self) -> String {
let peers = get_lan_peers()
.into_iter()
.map(|(id, peer)| (id, peer.username, peer.hostname, peer.platform))
.map(|mut peer| {
(
peer.remove("id").unwrap_or_default(),
peer.remove("username").unwrap_or_default(),
peer.remove("hostname").unwrap_or_default(),
peer.remove("platform").unwrap_or_default(),
)
})
.collect::<Vec<(String, String, String, String)>>();
serde_json::to_string(&peers).unwrap_or_default()
serde_json::to_string(&get_lan_peers()).unwrap_or_default()
}
fn get_uuid(&self) -> String {

View File

@ -48,7 +48,7 @@ impl InvokeUiCM for SciterHandler {
self.call("newMessage", &make_args!(id, text));
}
fn change_theme(&self, _dark: bool) {
fn change_theme(&self, _dark: String) {
// TODO
}

View File

@ -61,7 +61,7 @@ pub trait InvokeUiCM: Send + Clone + 'static + Sized {
fn new_message(&self, id: i32, text: String);
fn change_theme(&self, dark: bool);
fn change_theme(&self, dark: String);
fn change_language(&self);
}

View File

@ -648,19 +648,17 @@ pub fn discover() {
}
#[inline]
pub fn get_lan_peers() -> Vec<(String, config::PeerInfoSerde)> {
pub fn get_lan_peers() -> Vec<HashMap<&'static str, String>> {
config::LanPeers::load()
.peers
.iter()
.map(|peer| {
(
peer.id.clone(),
config::PeerInfoSerde {
username: peer.username.clone(),
hostname: peer.hostname.clone(),
platform: peer.platform.clone(),
},
)
HashMap::<&str, String>::from_iter([
("id", peer.id.clone()),
("username", peer.username.clone()),
("hostname", peer.hostname.clone()),
("platform", peer.platform.clone()),
])
})
.collect()
}