2022-05-30 13:25:06 +08:00
import 'dart:async' ;
2022-08-17 21:28:36 +08:00
import 'dart:convert' ;
2022-11-09 15:14:11 +08:00
import 'dart:math' ;
2022-05-30 13:25:06 +08:00
2022-08-17 21:28:36 +08:00
import 'package:back_button_interceptor/back_button_interceptor.dart' ;
2022-08-09 19:32:19 +08:00
import 'package:desktop_multi_window/desktop_multi_window.dart' ;
2022-11-30 13:56:02 +08:00
import 'package:flutter/foundation.dart' ;
2022-04-14 15:37:47 +08:00
import 'package:flutter/gestures.dart' ;
2020-11-15 20:04:05 +08:00
import 'package:flutter/material.dart' ;
2022-09-03 18:19:50 +08:00
import 'package:flutter/services.dart' ;
2023-08-08 17:22:25 +08:00
import 'package:flutter_hbb/common/formatter/id_formatter.dart' ;
2022-10-26 14:39:13 +08:00
import 'package:flutter_hbb/desktop/widgets/refresh_wrapper.dart' ;
2022-08-16 21:27:21 +08:00
import 'package:flutter_hbb/desktop/widgets/tabbar_widget.dart' ;
2022-10-11 19:52:03 +08:00
import 'package:flutter_hbb/main.dart' ;
2022-08-27 00:45:09 +08:00
import 'package:flutter_hbb/models/peer_model.dart' ;
2025-10-12 14:59:42 +08:00
import 'package:flutter_hbb/models/peer_tab_model.dart' ;
2023-08-11 15:53:47 +08:00
import 'package:flutter_hbb/models/state_model.dart' ;
2022-09-16 12:14:03 +08:00
import 'package:flutter_hbb/utils/multi_window_manager.dart' ;
2023-02-03 17:08:40 +08:00
import 'package:flutter_hbb/utils/platform_channel.dart' ;
import 'package:flutter_svg/flutter_svg.dart' ;
2022-08-20 19:57:16 +08:00
import 'package:get/get.dart' ;
2025-10-11 03:11:56 -05:00
import 'package:get/get_rx/src/rx_workers/utils/debouncer.dart' ;
2024-06-01 20:23:58 +08:00
import 'package:provider/provider.dart' ;
2022-10-18 10:29:33 +08:00
import 'package:uni_links/uni_links.dart' ;
2022-10-14 11:19:49 +08:00
import 'package:url_launcher/url_launcher.dart' ;
2023-06-06 07:39:44 +08:00
import 'package:uuid/uuid.dart' ;
2026-01-05 22:16:35 +08:00
import 'package:wakelock_plus/wakelock_plus.dart' ;
2023-02-03 17:08:40 +08:00
import 'package:window_manager/window_manager.dart' ;
import 'package:window_size/window_size.dart' as window_size ;
2022-04-19 13:07:45 +08:00
2023-02-03 17:08:40 +08:00
import '../consts.dart' ;
2022-09-13 09:03:34 +08:00
import 'common/widgets/overlay.dart' ;
2022-09-22 16:45:14 +08:00
import 'mobile/pages/file_manager_page.dart' ;
import 'mobile/pages/remote_page.dart' ;
2025-03-10 21:06:53 +08:00
import 'mobile/pages/view_camera_page.dart' ;
2025-07-01 13:12:55 +08:00
import 'mobile/pages/terminal_page.dart' ;
2024-03-28 11:38:11 +08:00
import 'desktop/pages/remote_page.dart' as desktop_remote ;
2024-10-12 09:03:13 +08:00
import 'desktop/pages/file_manager_page.dart' as desktop_file_manager ;
2025-03-10 21:06:53 +08:00
import 'desktop/pages/view_camera_page.dart' as desktop_view_camera ;
2024-03-28 11:38:11 +08:00
import 'package:flutter_hbb/desktop/widgets/remote_toolbar.dart' ;
2022-04-19 13:07:45 +08:00
import 'models/model.dart' ;
2022-08-03 22:03:31 +08:00
import 'models/platform_model.dart' ;
2021-04-25 00:19:35 +08:00
2024-03-22 13:16:37 +08:00
import 'package:flutter_hbb/native/win32.dart'
if ( dart . library . html ) 'package:flutter_hbb/web/win32.dart' ;
import 'package:flutter_hbb/native/common.dart'
if ( dart . library . html ) 'package:flutter_hbb/web/common.dart' ;
2025-11-13 23:35:40 +08:00
import 'package:flutter_hbb/utils/http_service.dart' as http ;
2024-03-22 13:16:37 +08:00
2022-03-07 22:54:34 +08:00
final globalKey = GlobalKey < NavigatorState > ();
2022-04-12 22:38:39 +08:00
final navigationBarKey = GlobalKey ();
2022-03-07 22:54:34 +08:00
2024-03-22 13:16:37 +08:00
final isAndroid = isAndroid_ ;
final isIOS = isIOS_ ;
final isWindows = isWindows_ ;
final isMacOS = isMacOS_ ;
final isLinux = isLinux_ ;
final isDesktop = isDesktop_ ;
final isWeb = isWeb_ ;
2024-03-28 11:38:11 +08:00
final isWebDesktop = isWebDesktop_ ;
2024-08-26 12:13:11 +08:00
final isWebOnWindows = isWebOnWindows_ ;
final isWebOnLinux = isWebOnLinux_ ;
final isWebOnMacOs = isWebOnMacOS_ ;
2023-04-12 09:41:13 +08:00
var isMobile = isAndroid || isIOS ;
2024-03-22 13:16:37 +08:00
var version = '' ;
2022-03-24 17:58:33 +08:00
int androidVersion = 0 ;
2023-02-23 16:49:31 +01:00
2024-05-10 16:40:29 +08:00
// Only used on Linux.
// `windowManager.setResizable(false)` will reset the window size to the default size on Linux.
// https://stackoverflow.com/questions/8193613/gtk-window-resize-disable-without-going-back-to-default
// So we need to use this flag to enable/disable resizable.
bool _linuxWindowResizable = true ;
2024-11-11 00:35:41 +08:00
// Only used on Windows(window manager).
bool _ignoreDevicePixelRatio = true ;
2022-12-25 20:21:13 +03:00
/// only available for Windows target
2022-11-29 23:03:16 +08:00
int windowsBuildNumber = 0 ;
2022-09-15 16:36:52 +08:00
DesktopType ? desktopType ;
2022-03-17 21:03:52 +08:00
2025-10-08 20:23:55 -04:00
// Tolerance used for floating-point position comparisons to avoid precision errors.
const double _kPositionEpsilon = 1e-6 ;
2023-10-17 00:30:34 +08:00
bool get isMainDesktopWindow =>
desktopType == DesktopType . main || desktopType == DesktopType . cm ;
2023-02-10 21:18:55 +08:00
/// Check if the app is running with single view mode.
bool isSingleViewApp () {
return desktopType == DesktopType . cm ;
}
2022-10-20 23:05:34 +09:00
/// * debug or test only, DO NOT enable in release build
bool isTest = false ;
2021-08-02 20:54:56 +08:00
typedef F = String Function ( String );
2022-02-10 02:07:53 +08:00
typedef FMethod = String Function ( String , dynamic );
2021-04-25 00:19:35 +08:00
2022-09-10 19:50:48 -07:00
typedef StreamEventHandler = Future < void > Function ( Map < String , dynamic > );
2023-06-06 07:39:44 +08:00
typedef SessionID = UuidValue ;
2022-10-20 10:31:31 +09:00
final iconHardDrive = MemoryImage ( Uint8List . fromList ( base64Decode (
'iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAmVBMVEUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAjHWqVAAAAMnRSTlMAv0BmzLJNXlhiUu2fxXDgu7WuSUUe29LJvpqUjX53VTstD7ilNujCqTEk5IYH+vEoFjKvAagAAAPpSURBVHja7d0JbhpBEIXhB3jYzb5vBgzYgO04df/DJXGUKMwU9ECmZ6pQfSfw028LCXW3YYwxxhhjjDHGGGOM0eZ9VV1MckdKWLM1bRQ/35GW/WxHHu1me6ShuyHvNl34VhlTKsYVeDWj1EzgUZ1S1DrAk/UDparZgxd9Sl0BHnxSBhpI3jfKQG2FpLUpE69I2ILikv1nsvygjBwPSNKYMlNHggqUoSKS80AZCnwHqQ1zCRvW+CRegwRFeFAMKKrtM8gTPJlzSfwFgT9dJom3IDN4VGaSeAryAK8m0SSeghTg1ZYiql6CjBDhO8mzlyAVhKhIwgXxrh5NojGIhyRckEdwpCdhgpSQgiWTRGMQNonGIGySp0SDvMDBX5KWxiB8Eo1BgE00SYJBykhNnkmSWJAcLpGaJNMgfJKyxiDAK4WNEwryhMtkJsk8CJtEYxA+icYgQIfCcgkEqcJNXhIRQdgkGoPwSTQG+e8khdu/7JOVREwQIKCwF41B2CQljUH4JLcH6SI+OUlEBQHa0SQag/BJNAbhkjxqDMIn0RgEeI4muSlID9eSkERgEKAVTaIxCJ9EYxA2ydVB8hCASVLRGAQYR5NoDMIn0RgEyFHYSGMQPonGII4kziCNvBgNJonEk4u3GAk8Sprk6eYaqbMDY0oKvUm5jfC/viGiSypV7+M3i2iDsAGpNEDYjlTa3W8RdR/r544g50ilnA0RxoZIE2NIXqQbhkAkGyKNDZHGhkhjQ6SxIdLYEGlsiDQ2JGTVeD0264U9zipPh7XOooffpA6pfNCXjxl4/c3pUzlChwzor53zwYYVfpI5pOV6LWFF/2jiJ5FDSs5jdY/0rwUAkUMeXWdBqnSqD0DikBqdqCHsjTvELm9In0IOri/0pwAEDtlSyNaRjAIAAoesKWTtuusxByBwCJp0oomwBXcYUuCQgE50ENajE4OvZAKHLB1/68Br5NqiyCGYOY8YRd77kTkEb64n7lZN+mOIX4QOwb5FX0ZVx3uOxwW+SB0CbBubemWP8/rlaaeRX+M3uUOuZENsiA25zIbYkPsZElBIHwL13U/PTjJ/cyOOEoVM3I+hziDQlELm7pPxw3eI8/7gPh1fpLA6xGnEeDDgO0UcIAzzM35HxLPIq5SXe9BLzOsj9eUaQqyXzxS1QFSfWM2cCANiHcAISJ0AnCKpUwTuIkkA3EeSInAXSQKcs1V18e24wlllUmQp9v9zXKeHi+akRAMOPVKhAqdPBZeUmnnEsO6QcJ0+4qmOSbBxFfGVRiTUqITrdKcCbyYO3/K4wX4+aQ+FfNjXhu3JfAVjjDHGGGOMMcYYY4xIPwCgfqT6TbhCLAAAAABJRU5ErkJggg==' )));
2022-08-17 21:28:36 +08:00
2022-09-05 16:01:53 +08:00
enum DesktopType {
main ,
remote ,
fileTransfer ,
2025-03-10 21:06:53 +08:00
viewCamera ,
2025-07-01 13:12:55 +08:00
terminal ,
2022-09-05 16:01:53 +08:00
cm ,
portForward ,
}
2025-10-08 20:23:55 -04:00
bool isDoubleEqual ( double a , double b ) {
return ( a - b ). abs () < _kPositionEpsilon ;
}
2022-08-20 19:57:16 +08:00
class IconFont {
2022-08-24 11:01:58 +08:00
static const _family1 = 'Tabbar' ;
static const _family2 = 'PeerSearchbar' ;
2025-02-15 12:13:11 +08:00
static const _family3 = 'AddressBook' ;
static const _family4 = 'DeviceGroup' ;
2025-03-11 16:29:02 +08:00
static const _family5 = 'More' ;
2022-08-20 19:57:16 +08:00
IconFont . _ ();
2022-08-24 11:01:58 +08:00
static const IconData max = IconData ( 0xe606 , fontFamily: _family1 );
static const IconData restore = IconData ( 0xe607 , fontFamily: _family1 );
static const IconData close = IconData ( 0xe668 , fontFamily: _family1 );
static const IconData min = IconData ( 0xe609 , fontFamily: _family1 );
static const IconData add = IconData ( 0xe664 , fontFamily: _family1 );
static const IconData menu = IconData ( 0xe628 , fontFamily: _family1 );
static const IconData search = IconData ( 0xe6a4 , fontFamily: _family2 );
2022-11-03 21:58:25 +08:00
static const IconData roundClose = IconData ( 0xe6ed , fontFamily: _family2 );
2025-02-15 12:13:11 +08:00
static const IconData addressBook = IconData ( 0xe602 , fontFamily: _family3 );
static const IconData deviceGroupOutline =
IconData ( 0xe623 , fontFamily: _family4 );
static const IconData deviceGroupFill =
IconData ( 0xe748 , fontFamily: _family4 );
2025-03-11 16:29:02 +08:00
static const IconData more = IconData ( 0xe609 , fontFamily: _family5 );
2022-08-20 19:57:16 +08:00
}
class ColorThemeExtension extends ThemeExtension < ColorThemeExtension > {
const ColorThemeExtension ({
required this . border ,
2023-03-01 18:00:56 +01:00
required this . border2 ,
2023-12-04 22:34:54 -05:00
required this . border3 ,
2022-12-11 21:40:35 +08:00
required this . highlight ,
2023-03-11 19:53:19 +08:00
required this . drag_indicator ,
2023-03-15 16:57:24 +08:00
required this . shadow ,
2023-09-11 11:18:26 +08:00
required this . errorBannerBg ,
2023-09-26 09:26:53 +08:00
required this . me ,
2023-12-03 09:56:47 -05:00
required this . toastBg ,
required this . toastText ,
2023-12-04 22:34:54 -05:00
required this . divider ,
2022-08-20 19:57:16 +08:00
});
final Color ? border ;
2023-03-01 18:00:56 +01:00
final Color ? border2 ;
2023-12-04 22:34:54 -05:00
final Color ? border3 ;
2022-12-11 21:40:35 +08:00
final Color ? highlight ;
2023-03-11 19:53:19 +08:00
final Color ? drag_indicator ;
2023-03-15 16:57:24 +08:00
final Color ? shadow ;
2023-09-11 11:18:26 +08:00
final Color ? errorBannerBg ;
2023-09-26 09:26:53 +08:00
final Color ? me ;
2023-12-03 09:56:47 -05:00
final Color ? toastBg ;
final Color ? toastText ;
2023-12-04 22:34:54 -05:00
final Color ? divider ;
2022-08-20 19:57:16 +08:00
2023-03-11 19:53:19 +08:00
static final light = ColorThemeExtension (
2022-08-20 19:57:16 +08:00
border: Color ( 0xFFCCCCCC ),
2023-03-01 18:00:56 +01:00
border2: Color ( 0xFFBBBBBB ),
2023-12-04 22:34:54 -05:00
border3: Colors . black26 ,
2022-12-11 21:40:35 +08:00
highlight: Color ( 0xFFE5E5E5 ),
2023-03-11 19:53:19 +08:00
drag_indicator: Colors . grey [ 800 ],
2023-03-15 16:57:24 +08:00
shadow: Colors . black ,
2023-09-11 11:18:26 +08:00
errorBannerBg: Color ( 0xFFFDEEEB ),
2023-09-26 09:26:53 +08:00
me: Colors . green ,
2023-12-03 09:56:47 -05:00
toastBg: Colors . black . withOpacity ( 0.6 ),
toastText: Colors . white ,
2023-12-04 22:34:54 -05:00
divider: Colors . black38 ,
2022-08-20 19:57:16 +08:00
);
2023-03-11 19:53:19 +08:00
static final dark = ColorThemeExtension (
2022-08-20 19:57:16 +08:00
border: Color ( 0xFF555555 ),
2023-03-01 18:00:56 +01:00
border2: Color ( 0xFFE5E5E5 ),
2023-12-04 22:34:54 -05:00
border3: Colors . white24 ,
2022-12-11 21:40:35 +08:00
highlight: Color ( 0xFF3F3F3F ),
2023-03-11 19:53:19 +08:00
drag_indicator: Colors . grey ,
2023-03-15 16:57:24 +08:00
shadow: Colors . grey ,
2023-09-11 11:18:26 +08:00
errorBannerBg: Color ( 0xFF470F2D ),
2023-09-26 09:26:53 +08:00
me: Colors . greenAccent ,
2023-12-03 09:56:47 -05:00
toastBg: Colors . white . withOpacity ( 0.6 ),
toastText: Colors . black ,
2023-12-04 22:34:54 -05:00
divider: Colors . white38 ,
2022-08-20 19:57:16 +08:00
);
@ override
2023-03-15 16:57:24 +08:00
ThemeExtension < ColorThemeExtension > copyWith ({
Color ? border ,
Color ? border2 ,
2023-12-04 22:34:54 -05:00
Color ? border3 ,
2023-03-15 16:57:24 +08:00
Color ? highlight ,
Color ? drag_indicator ,
Color ? shadow ,
2023-09-11 11:18:26 +08:00
Color ? errorBannerBg ,
2023-09-26 09:26:53 +08:00
Color ? me ,
2023-12-03 09:56:47 -05:00
Color ? toastBg ,
Color ? toastText ,
2023-12-04 22:34:54 -05:00
Color ? divider ,
2023-03-15 16:57:24 +08:00
}) {
2022-08-20 19:57:16 +08:00
return ColorThemeExtension (
2023-03-15 16:57:24 +08:00
border: border ?? this . border ,
border2: border2 ?? this . border2 ,
2023-12-04 22:34:54 -05:00
border3: border3 ?? this . border3 ,
2023-03-15 16:57:24 +08:00
highlight: highlight ?? this . highlight ,
drag_indicator: drag_indicator ?? this . drag_indicator ,
shadow: shadow ?? this . shadow ,
2023-09-11 11:18:26 +08:00
errorBannerBg: errorBannerBg ?? this . errorBannerBg ,
2023-09-26 09:26:53 +08:00
me: me ?? this . me ,
2023-12-03 09:56:47 -05:00
toastBg: toastBg ?? this . toastBg ,
toastText: toastText ?? this . toastText ,
2023-12-04 22:34:54 -05:00
divider: divider ?? this . divider ,
2023-03-15 16:57:24 +08:00
);
2022-08-20 19:57:16 +08:00
}
@ override
ThemeExtension < ColorThemeExtension > lerp (
ThemeExtension < ColorThemeExtension >? other , double t ) {
if ( other is ! ColorThemeExtension ) {
return this ;
}
return ColorThemeExtension (
border: Color . lerp ( border , other . border , t ),
2023-03-01 18:00:56 +01:00
border2: Color . lerp ( border2 , other . border2 , t ),
2023-12-04 22:34:54 -05:00
border3: Color . lerp ( border3 , other . border3 , t ),
2022-12-11 21:40:35 +08:00
highlight: Color . lerp ( highlight , other . highlight , t ),
2023-03-11 19:53:19 +08:00
drag_indicator: Color . lerp ( drag_indicator , other . drag_indicator , t ),
2023-03-15 16:57:24 +08:00
shadow: Color . lerp ( shadow , other . shadow , t ),
2023-09-11 11:18:26 +08:00
errorBannerBg: Color . lerp ( shadow , other . errorBannerBg , t ),
2023-09-26 09:26:53 +08:00
me: Color . lerp ( shadow , other . me , t ),
2023-12-03 09:56:47 -05:00
toastBg: Color . lerp ( shadow , other . toastBg , t ),
toastText: Color . lerp ( shadow , other . toastText , t ),
2023-12-04 22:34:54 -05:00
divider: Color . lerp ( shadow , other . divider , t ),
2022-08-20 19:57:16 +08:00
);
}
}
2020-11-16 01:13:26 +08:00
class MyTheme {
2020-11-20 16:37:48 +08:00
MyTheme . _ ();
2022-02-02 17:25:56 +08:00
2023-03-04 14:28:43 +08:00
static const Color grayBg = Color ( 0xFFEFEFF2 );
2020-11-16 22:00:09 +08:00
static const Color accent = Color ( 0xFF0071FF );
2020-11-19 17:22:42 +08:00
static const Color accent50 = Color ( 0x770071FF );
2020-11-27 02:14:27 +08:00
static const Color accent80 = Color ( 0xAA0071FF );
2020-11-19 18:41:37 +08:00
static const Color canvasColor = Color ( 0xFF212121 );
2020-11-20 13:06:52 +08:00
static const Color border = Color ( 0xFFCCCCCC );
2022-01-31 16:22:05 +08:00
static const Color idColor = Color ( 0xFF00B6F0 );
2023-02-22 22:13:21 +01:00
static const Color darkGray = Color . fromARGB ( 255 , 148 , 148 , 148 );
2022-08-17 21:28:36 +08:00
static const Color cmIdColor = Color ( 0xFF21790B );
2022-05-30 13:25:06 +08:00
static const Color dark = Colors . black87 ;
2022-08-22 17:58:48 +08:00
static const Color button = Color ( 0xFF2C8CFF );
static const Color hoverBorder = Color ( 0xFF999999 );
2022-07-29 16:47:24 +08:00
2023-03-30 00:03:15 +02:00
// ListTile
static const ListTileThemeData listTileTheme = ListTileThemeData (
shape: RoundedRectangleBorder (
borderRadius: BorderRadius . all (
Radius . circular ( 5 ),
),
),
);
2023-06-06 14:53:05 +02:00
static SwitchThemeData switchTheme () {
2024-03-28 11:38:11 +08:00
return SwitchThemeData (
splashRadius: ( isDesktop || isWebDesktop ) ? 0 : kRadialReactionRadius );
2023-06-06 14:53:05 +02:00
}
static RadioThemeData radioTheme () {
2024-03-28 11:38:11 +08:00
return RadioThemeData (
splashRadius: ( isDesktop || isWebDesktop ) ? 0 : kRadialReactionRadius );
2023-06-06 14:53:05 +02:00
}
2023-05-20 15:12:52 +02:00
2023-03-30 00:03:15 +02:00
// Checkbox
static const CheckboxThemeData checkboxTheme = CheckboxThemeData (
splashRadius: 0 ,
shape: RoundedRectangleBorder (
borderRadius: BorderRadius . all (
Radius . circular ( 5 ),
),
),
);
2023-03-29 16:14:55 +02:00
// TextButton
// Value is used to calculate "dialog.actionsPadding"
static const double mobileTextButtonPaddingLR = 20 ;
// TextButton on mobile needs a fixed padding, otherwise small buttons
// like "OK" has a larger left/right padding.
2023-03-30 00:03:15 +02:00
static TextButtonThemeData mobileTextButtonTheme = TextButtonThemeData (
style: TextButton . styleFrom (
padding: EdgeInsets . symmetric ( horizontal: mobileTextButtonPaddingLR ),
shape: RoundedRectangleBorder (
borderRadius: BorderRadius . circular ( 8.0 ),
),
),
);
2023-03-29 16:14:55 +02:00
2023-08-10 18:32:02 +05:30
//tooltip
static TooltipThemeData tooltipTheme () {
return TooltipThemeData (
2023-08-11 12:01:57 +08:00
waitDuration: Duration ( seconds: 1 , milliseconds: 500 ),
2023-08-10 18:32:02 +05:30
);
}
2023-03-29 16:14:55 +02:00
// Dialogs
static const double dialogPadding = 24 ;
2023-03-30 00:03:15 +02:00
// padding bottom depends on content (some dialogs has no content)
2023-03-29 16:14:55 +02:00
static EdgeInsets dialogTitlePadding ({ bool content = true }) {
final double p = dialogPadding ;
return EdgeInsets . fromLTRB ( p , p , p , content ? 0 : p );
}
2023-03-30 00:03:15 +02:00
// padding bottom depends on actions (mobile has dialogs without actions)
2023-03-29 16:14:55 +02:00
static EdgeInsets dialogContentPadding ({ bool actions = true }) {
final double p = dialogPadding ;
2024-03-28 11:38:11 +08:00
return ( isDesktop || isWebDesktop )
2023-03-29 16:14:55 +02:00
? EdgeInsets . fromLTRB ( p , p , p , actions ? ( p - 4 ) : p )
: EdgeInsets . fromLTRB ( p , p , p , actions ? ( p / 2 ) : p );
}
static EdgeInsets dialogActionsPadding () {
final double p = dialogPadding ;
2024-03-28 11:38:11 +08:00
return ( isDesktop || isWebDesktop )
2023-03-29 16:14:55 +02:00
? EdgeInsets . fromLTRB ( p , 0 , p , ( p - 4 ))
: EdgeInsets . fromLTRB ( p , 0 , ( p - mobileTextButtonPaddingLR ), ( p / 2 ));
}
2024-03-28 11:38:11 +08:00
static EdgeInsets dialogButtonPadding = ( isDesktop || isWebDesktop )
2023-03-29 16:14:55 +02:00
? EdgeInsets . only ( left: dialogPadding )
: EdgeInsets . only ( left: dialogPadding / 3 );
2023-09-13 21:02:21 +08:00
static ScrollbarThemeData scrollbarTheme = ScrollbarThemeData (
2023-09-26 19:58:51 +08:00
thickness: MaterialStateProperty . all ( 6 ),
thumbColor: MaterialStateProperty . resolveWith < Color ?> (( states ) {
if ( states . contains ( MaterialState . dragged )) {
return Colors . grey [ 900 ];
} else if ( states . contains ( MaterialState . hovered )) {
return Colors . grey [ 700 ];
} else {
return Colors . grey [ 500 ];
}
}),
crossAxisMargin: 4 ,
2023-09-13 21:02:21 +08:00
);
static ScrollbarThemeData scrollbarThemeDark = scrollbarTheme . copyWith (
2023-09-26 19:58:51 +08:00
thumbColor: MaterialStateProperty . resolveWith < Color ?> (( states ) {
if ( states . contains ( MaterialState . dragged )) {
return Colors . grey [ 100 ];
} else if ( states . contains ( MaterialState . hovered )) {
return Colors . grey [ 300 ];
} else {
return Colors . grey [ 500 ];
}
}),
2023-09-13 21:02:21 +08:00
);
2022-07-29 16:47:24 +08:00
static ThemeData lightTheme = ThemeData (
2024-02-11 00:15:11 +08:00
// https://stackoverflow.com/questions/77537315/after-upgrading-to-flutter-3-16-the-app-bar-background-color-button-size-and
useMaterial3: false ,
2022-07-29 16:47:24 +08:00
brightness: Brightness . light ,
2023-02-22 22:13:21 +01:00
hoverColor: Color . fromARGB ( 255 , 224 , 224 , 224 ),
2023-03-04 14:28:43 +08:00
scaffoldBackgroundColor: Colors . white ,
dialogBackgroundColor: Colors . white ,
2024-09-01 00:30:07 +08:00
appBarTheme: AppBarTheme (
shadowColor: Colors . transparent ,
),
2023-02-27 09:44:52 +01:00
dialogTheme: DialogTheme (
2023-02-28 14:08:55 +01:00
elevation: 15 ,
2023-02-27 09:44:52 +01:00
shape: RoundedRectangleBorder (
borderRadius: BorderRadius . circular ( 18.0 ),
2023-02-28 14:08:55 +01:00
side: BorderSide (
width: 1 ,
2023-03-04 14:28:43 +08:00
color: grayBg ,
2023-02-28 14:08:55 +01:00
),
2023-02-27 09:44:52 +01:00
),
),
2023-09-13 21:02:21 +08:00
scrollbarTheme: scrollbarTheme ,
2023-03-17 12:44:07 +01:00
inputDecorationTheme: isDesktop
? InputDecorationTheme (
fillColor: grayBg ,
filled: true ,
isDense: true ,
2023-03-17 16:42:49 +01:00
border: OutlineInputBorder (
borderRadius: BorderRadius . circular ( 8 ),
2023-03-17 12:44:07 +01:00
),
)
: null ,
2022-09-23 16:31:50 +08:00
textTheme: const TextTheme (
2022-09-29 13:07:20 +08:00
titleLarge: TextStyle ( fontSize: 19 , color: Colors . black87 ),
titleSmall: TextStyle ( fontSize: 14 , color: Colors . black87 ),
bodySmall: TextStyle ( fontSize: 12 , color: Colors . black87 , height: 1.25 ),
bodyMedium:
TextStyle ( fontSize: 14 , color: Colors . black87 , height: 1.25 ),
labelLarge: TextStyle ( fontSize: 16.0 , color: MyTheme . accent80 )),
2023-03-04 14:28:43 +08:00
cardColor: grayBg ,
2022-09-23 16:31:50 +08:00
hintColor: Color ( 0xFFAAAAAA ),
2022-07-29 16:47:24 +08:00
visualDensity: VisualDensity . adaptivePlatformDensity ,
2022-09-04 11:03:16 +08:00
tabBarTheme: const TabBarTheme (
2022-08-20 19:57:16 +08:00
labelColor: Colors . black87 ,
),
2023-08-10 18:32:02 +05:30
tooltipTheme: tooltipTheme (),
2024-03-28 11:38:11 +08:00
splashColor: ( isDesktop || isWebDesktop ) ? Colors . transparent : null ,
highlightColor: ( isDesktop || isWebDesktop ) ? Colors . transparent : null ,
splashFactory: ( isDesktop || isWebDesktop ) ? NoSplash . splashFactory : null ,
textButtonTheme: ( isDesktop || isWebDesktop )
2022-09-23 12:20:40 +08:00
? TextButtonThemeData (
2023-03-01 18:00:56 +01:00
style: TextButton . styleFrom (
2023-02-27 20:56:45 +01:00
splashFactory: NoSplash . splashFactory ,
2023-03-01 18:00:56 +01:00
shape: RoundedRectangleBorder (
borderRadius: BorderRadius . circular ( 18.0 ),
2023-02-27 20:56:45 +01:00
),
),
2022-09-23 12:20:40 +08:00
)
2023-03-30 00:03:15 +02:00
: mobileTextButtonTheme ,
2023-02-27 20:56:45 +01:00
elevatedButtonTheme: ElevatedButtonThemeData (
2023-03-01 18:00:56 +01:00
style: ElevatedButton . styleFrom (
backgroundColor: MyTheme . accent ,
shape: RoundedRectangleBorder (
borderRadius: BorderRadius . circular ( 8.0 ),
2023-03-01 14:50:50 +01:00
),
),
),
outlinedButtonTheme: OutlinedButtonThemeData (
2023-03-01 18:00:56 +01:00
style: OutlinedButton . styleFrom (
2023-03-04 14:28:43 +08:00
backgroundColor: grayBg ,
2023-03-01 18:00:56 +01:00
foregroundColor: Colors . black87 ,
shape: RoundedRectangleBorder (
borderRadius: BorderRadius . circular ( 8.0 ),
2023-02-27 20:56:45 +01:00
),
),
),
2023-06-06 14:53:05 +02:00
switchTheme: switchTheme (),
radioTheme: radioTheme (),
2023-03-30 00:03:15 +02:00
checkboxTheme: checkboxTheme ,
listTileTheme: listTileTheme ,
2023-03-11 19:29:26 +08:00
menuBarTheme: MenuBarThemeData (
style:
MenuStyle ( backgroundColor: MaterialStatePropertyAll ( Colors . white ))),
2023-03-10 14:12:28 +01:00
colorScheme: ColorScheme . light (
primary: Colors . blue , secondary: accent , background: grayBg ),
2023-12-04 22:34:54 -05:00
popupMenuTheme: PopupMenuThemeData (
color: Colors . white ,
shape: RoundedRectangleBorder (
side: BorderSide (
2024-03-28 11:38:11 +08:00
color: ( isDesktop || isWebDesktop )
? Color ( 0xFFECECEC )
: Colors . transparent ),
2023-12-04 22:34:54 -05:00
borderRadius: BorderRadius . all ( Radius . circular ( 8.0 )),
)),
2022-08-20 19:57:16 +08:00
). copyWith (
extensions: < ThemeExtension < dynamic >> [
ColorThemeExtension . light ,
2022-09-04 11:03:16 +08:00
TabbarTheme . light ,
2022-08-20 19:57:16 +08:00
],
2022-07-29 16:47:24 +08:00
);
static ThemeData darkTheme = ThemeData (
2024-02-11 00:15:11 +08:00
useMaterial3: false ,
2022-08-20 19:57:16 +08:00
brightness: Brightness . dark ,
2023-02-22 22:13:21 +01:00
hoverColor: Color . fromARGB ( 255 , 45 , 46 , 53 ),
scaffoldBackgroundColor: Color ( 0xFF18191E ),
2023-02-27 09:44:52 +01:00
dialogBackgroundColor: Color ( 0xFF18191E ),
2024-09-01 00:30:07 +08:00
appBarTheme: AppBarTheme (
shadowColor: Colors . transparent ,
),
2023-02-27 09:44:52 +01:00
dialogTheme: DialogTheme (
2023-02-28 14:08:55 +01:00
elevation: 15 ,
2023-02-27 09:44:52 +01:00
shape: RoundedRectangleBorder (
borderRadius: BorderRadius . circular ( 18.0 ),
2023-02-28 14:08:55 +01:00
side: BorderSide (
width: 1 ,
color: Color ( 0xFF24252B ),
),
2023-02-27 09:44:52 +01:00
),
),
2023-09-13 21:02:21 +08:00
scrollbarTheme: scrollbarThemeDark ,
2024-03-28 11:38:11 +08:00
inputDecorationTheme: ( isDesktop || isWebDesktop )
2023-03-17 12:44:07 +01:00
? InputDecorationTheme (
fillColor: Color ( 0xFF24252B ),
filled: true ,
isDense: true ,
2023-03-17 16:42:49 +01:00
border: OutlineInputBorder (
borderRadius: BorderRadius . circular ( 8 ),
2023-03-17 12:44:07 +01:00
),
)
: null ,
2022-09-23 16:31:50 +08:00
textTheme: const TextTheme (
2023-05-20 15:12:52 +02:00
titleLarge: TextStyle ( fontSize: 19 ),
titleSmall: TextStyle ( fontSize: 14 ),
bodySmall: TextStyle ( fontSize: 12 , height: 1.25 ),
bodyMedium: TextStyle ( fontSize: 14 , height: 1.25 ),
labelLarge: TextStyle (
fontSize: 16.0 ,
fontWeight: FontWeight . bold ,
color: accent80 ,
),
),
2023-02-22 22:13:21 +01:00
cardColor: Color ( 0xFF24252B ),
2022-08-20 19:57:16 +08:00
visualDensity: VisualDensity . adaptivePlatformDensity ,
2022-09-04 11:03:16 +08:00
tabBarTheme: const TabBarTheme (
2022-08-20 19:57:16 +08:00
labelColor: Colors . white70 ,
),
2023-08-10 18:32:02 +05:30
tooltipTheme: tooltipTheme (),
2024-03-28 11:38:11 +08:00
splashColor: ( isDesktop || isWebDesktop ) ? Colors . transparent : null ,
highlightColor: ( isDesktop || isWebDesktop ) ? Colors . transparent : null ,
splashFactory: ( isDesktop || isWebDesktop ) ? NoSplash . splashFactory : null ,
textButtonTheme: ( isDesktop || isWebDesktop )
2022-09-23 12:20:40 +08:00
? TextButtonThemeData (
2023-03-01 18:00:56 +01:00
style: TextButton . styleFrom (
2023-02-27 20:56:45 +01:00
splashFactory: NoSplash . splashFactory ,
2023-03-01 18:00:56 +01:00
disabledForegroundColor: Colors . white70 ,
foregroundColor: Colors . white70 ,
shape: RoundedRectangleBorder (
borderRadius: BorderRadius . circular ( 18.0 ),
2023-02-27 20:56:45 +01:00
),
),
2022-09-23 12:20:40 +08:00
)
2023-03-30 00:03:15 +02:00
: mobileTextButtonTheme ,
2023-02-27 20:56:45 +01:00
elevatedButtonTheme: ElevatedButtonThemeData (
2023-03-01 18:00:56 +01:00
style: ElevatedButton . styleFrom (
backgroundColor: MyTheme . accent ,
2023-03-10 14:12:28 +01:00
foregroundColor: Colors . white ,
2023-03-01 18:00:56 +01:00
disabledForegroundColor: Colors . white70 ,
disabledBackgroundColor: Colors . white10 ,
shape: RoundedRectangleBorder (
borderRadius: BorderRadius . circular ( 8.0 ),
2023-03-01 14:50:50 +01:00
),
),
),
outlinedButtonTheme: OutlinedButtonThemeData (
2023-03-01 18:00:56 +01:00
style: OutlinedButton . styleFrom (
backgroundColor: Color ( 0xFF24252B ),
side: BorderSide ( color: Colors . white12 , width: 0.5 ),
disabledForegroundColor: Colors . white70 ,
foregroundColor: Colors . white70 ,
shape: RoundedRectangleBorder (
borderRadius: BorderRadius . circular ( 8.0 ),
2023-02-27 20:56:45 +01:00
),
),
),
2023-06-06 14:53:05 +02:00
switchTheme: switchTheme (),
radioTheme: radioTheme (),
2023-03-30 00:03:15 +02:00
checkboxTheme: checkboxTheme ,
listTileTheme: listTileTheme ,
2023-03-11 19:29:26 +08:00
menuBarTheme: MenuBarThemeData (
style: MenuStyle (
backgroundColor: MaterialStatePropertyAll ( Color ( 0xFF121212 )))),
2023-03-06 10:55:34 +08:00
colorScheme: ColorScheme . dark (
primary: Colors . blue ,
2023-03-10 14:12:28 +01:00
secondary: accent ,
2023-02-26 11:49:22 +01:00
background: Color ( 0xFF24252B ),
),
2023-12-04 22:34:54 -05:00
popupMenuTheme: PopupMenuThemeData (
shape: RoundedRectangleBorder (
side: BorderSide ( color: Colors . white24 ),
borderRadius: BorderRadius . all ( Radius . circular ( 8.0 )),
)),
2022-08-20 19:57:16 +08:00
). copyWith (
extensions: < ThemeExtension < dynamic >> [
ColorThemeExtension . dark ,
2022-09-04 11:03:16 +08:00
TabbarTheme . dark ,
2022-08-20 19:57:16 +08:00
],
);
2022-09-21 23:32:59 +08:00
static ThemeMode getThemeModePreference () {
2022-11-10 21:25:12 +08:00
return themeModeFromString ( bind . mainGetLocalOption ( key: kCommConfKeyTheme ));
2022-09-21 23:32:59 +08:00
}
2024-10-14 17:46:38 +08:00
static Future < void > changeDarkMode ( ThemeMode mode ) async {
2022-12-03 16:32:22 +08:00
Get . changeThemeMode ( mode );
2024-09-01 00:30:07 +08:00
if ( desktopType == DesktopType . main || isAndroid || isIOS || isWeb ) {
2022-09-21 23:32:59 +08:00
if ( mode == ThemeMode . system ) {
2024-05-17 14:19:11 +08:00
await bind . mainSetLocalOption (
key: kCommConfKeyTheme , value: defaultOptionTheme );
2022-09-21 23:32:59 +08:00
} else {
2023-02-02 14:03:50 +08:00
await bind . mainSetLocalOption (
2022-11-10 21:25:12 +08:00
key: kCommConfKeyTheme , value: mode . toShortString ());
2022-09-21 23:32:59 +08:00
}
2024-09-01 00:30:07 +08:00
if ( ! isWeb ) await bind . mainChangeTheme ( dark: mode . toShortString ());
2023-02-02 14:03:50 +08:00
// Synchronize the window theme of the system.
updateSystemWindowTheme ();
2023-02-02 13:57:20 +08:00
}
2022-09-06 22:34:01 +08:00
}
2022-09-21 23:32:59 +08:00
static ThemeMode currentThemeMode () {
final preference = getThemeModePreference ();
if ( preference == ThemeMode . system ) {
if ( WidgetsBinding . instance . platformDispatcher . platformBrightness ==
Brightness . light ) {
return ThemeMode . light ;
} else {
return ThemeMode . dark ;
}
2022-09-06 22:34:01 +08:00
} else {
2022-09-21 23:32:59 +08:00
return preference ;
2022-09-06 22:34:01 +08:00
}
2022-09-07 18:57:49 +08:00
}
2022-08-20 19:57:16 +08:00
static ColorThemeExtension color ( BuildContext context ) {
return Theme . of ( context ). extension < ColorThemeExtension > () ! ;
}
2022-09-04 11:03:16 +08:00
static TabbarTheme tabbar ( BuildContext context ) {
return Theme . of ( context ). extension < TabbarTheme > () ! ;
}
2022-09-21 23:32:59 +08:00
static ThemeMode themeModeFromString ( String v ) {
switch ( v ) {
case "light" :
return ThemeMode . light ;
case "dark" :
return ThemeMode . dark ;
default :
return ThemeMode . system ;
}
}
2022-07-29 16:47:24 +08:00
}
2022-09-21 23:32:59 +08:00
extension ParseToString on ThemeMode {
String toShortString () {
return toString (). split ( '.' ). last ;
}
2020-11-16 01:13:26 +08:00
}
2021-08-02 20:54:56 +08:00
final ButtonStyle flatButtonStyle = TextButton . styleFrom (
2022-05-18 15:47:07 +08:00
minimumSize: Size ( 0 , 36 ),
padding: EdgeInsets . symmetric ( horizontal: 16.0 , vertical: 10.0 ),
2021-08-02 20:54:56 +08:00
shape: const RoundedRectangleBorder (
borderRadius: BorderRadius . all ( Radius . circular ( 2.0 )),
),
);
2022-09-19 15:46:09 +08:00
List < Locale > supportedLocales = const [
2023-03-09 06:30:39 +08:00
Locale ( 'en' , 'US' ),
2022-09-19 15:46:09 +08:00
Locale ( 'zh' , 'CN' ),
Locale ( 'zh' , 'TW' ),
2022-09-19 16:06:03 +08:00
Locale ( 'zh' , 'SG' ),
2022-09-19 15:46:09 +08:00
Locale ( 'fr' ),
Locale ( 'de' ),
Locale ( 'it' ),
Locale ( 'ja' ),
Locale ( 'cs' ),
Locale ( 'pl' ),
Locale ( 'ko' ),
Locale ( 'hu' ),
Locale ( 'pt' ),
Locale ( 'ru' ),
Locale ( 'sk' ),
Locale ( 'id' ),
Locale ( 'da' ),
Locale ( 'eo' ),
Locale ( 'tr' ),
Locale ( 'kz' ),
2023-03-09 06:30:39 +08:00
Locale ( 'es' ),
2024-08-02 15:12:48 +08:00
Locale ( 'nl' ),
Locale ( 'nb' ),
Locale ( 'et' ),
Locale ( 'eu' ),
Locale ( 'bg' ),
Locale ( 'be' ),
Locale ( 'vn' ),
Locale ( 'uk' ),
Locale ( 'fa' ),
Locale ( 'ca' ),
Locale ( 'el' ),
Locale ( 'sv' ),
Locale ( 'sq' ),
Locale ( 'sr' ),
Locale ( 'th' ),
Locale ( 'sl' ),
Locale ( 'ro' ),
Locale ( 'lt' ),
Locale ( 'lv' ),
Locale ( 'ar' ),
Locale ( 'he' ),
Locale ( 'hr' ),
2022-09-19 15:46:09 +08:00
];
2022-08-18 00:34:04 +08:00
String formatDurationToTime ( Duration duration ) {
var totalTime = duration . inSeconds ;
final secs = totalTime % 60 ;
totalTime = ( totalTime - secs ) ~/ 60 ;
final mins = totalTime % 60 ;
totalTime = ( totalTime - mins ) ~/ 60 ;
return " ${ totalTime . toString (). padLeft ( 2 , "0" ) } : ${ mins . toString (). padLeft ( 2 , "0" ) } : ${ secs . toString (). padLeft ( 2 , "0" ) } " ;
}
2022-08-16 21:27:21 +08:00
closeConnection ({ String ? id }) {
2022-08-12 18:42:02 +08:00
if ( isAndroid || isIOS ) {
2024-07-25 00:43:14 +08:00
() async {
await SystemChrome . setEnabledSystemUIMode ( SystemUiMode . manual ,
overlays: SystemUiOverlay . values );
gFFI . chatModel . hideChatOverlay ();
Navigator . popUntil ( globalKey . currentContext ! , ModalRoute . withName ( "/" ));
2024-10-01 15:25:59 +08:00
stateGlobal . isInMainPage = true ;
2024-07-25 00:43:14 +08:00
}();
2022-08-12 18:42:02 +08:00
} else {
2024-03-24 11:23:06 +08:00
if ( isWeb ) {
Navigator . popUntil ( globalKey . currentContext ! , ModalRoute . withName ( "/" ));
2024-10-01 15:25:59 +08:00
stateGlobal . isInMainPage = true ;
2024-03-24 11:23:06 +08:00
} else {
final controller = Get . find < DesktopTabController > ();
2026-05-07 13:27:13 +08:00
if ( controller . tabType == DesktopTabType . terminal &&
controller . onCloseWindow != null ) {
// Terminal windows are scoped to one peer. The optional id passed to
// closeConnection() is that peer id, not a terminal tab key
// (${peerId}_${terminalId}). Closing from terminal dialogs should close
// the peer's whole terminal window, including all terminal tabs.
unawaited ( controller . onCloseWindow ! (). catchError (( e , _ ) {
debugPrint ( '[closeConnection] Failed to close terminal window: $ e ' );
}));
return ;
}
2024-03-24 11:23:06 +08:00
controller . closeBy ( id );
}
2022-08-12 18:42:02 +08:00
}
2022-03-13 23:07:52 +08:00
}
2023-10-12 23:11:11 +08:00
Future < void > windowOnTop ( int ? id ) async {
2023-02-16 10:58:27 +08:00
if ( ! isDesktop ) {
return ;
}
2023-08-11 15:53:47 +08:00
print ( "Bring window ' $ id ' on top" );
2022-08-09 19:32:19 +08:00
if ( id == null ) {
// main window
2023-08-11 19:03:09 +08:00
if ( stateGlobal . isMinimized ) {
2023-08-11 15:53:47 +08:00
await windowManager . restore ();
}
await windowManager . show ();
await windowManager . focus ();
await rustDeskWinManager . registerActiveWindow ( kWindowMainId );
2022-08-09 19:32:19 +08:00
} else {
2022-08-09 21:12:55 +08:00
WindowController . fromWindowId ( id )
.. focus ()
.. show ();
2022-11-05 23:41:22 +08:00
rustDeskWinManager . call ( WindowType . Main , kWindowEventShow , { "id" : id });
2022-08-09 19:32:19 +08:00
}
2022-08-09 09:01:06 +08:00
}
2022-03-13 00:32:44 +08:00
typedef DialogBuilder = CustomAlertDialog Function (
2023-05-08 12:34:19 +08:00
StateSetter setState , void Function ([ dynamic ]) close , BuildContext context );
2022-03-13 00:32:44 +08:00
2022-08-12 18:42:02 +08:00
class Dialog < T > {
OverlayEntry ? entry ;
2022-08-15 19:31:58 +08:00
Completer < T ?> completer = Completer < T ?> ();
2022-02-28 16:11:21 +08:00
2022-08-12 18:42:02 +08:00
Dialog ();
void complete ( T ? res ) {
try {
if ( ! completer . isCompleted ) {
completer . complete ( res );
}
} catch ( e ) {
debugPrint ( "Dialog complete catch error: $ e " );
2022-08-15 19:31:58 +08:00
} finally {
entry ? . remove ();
2022-08-12 18:42:02 +08:00
}
}
}
2023-02-08 22:01:15 +09:00
class OverlayKeyState {
final _overlayKey = GlobalKey < OverlayState > ();
/// use global overlay by default
OverlayState ? get state =>
_overlayKey . currentState ?? globalKey . currentState ? . overlay ;
GlobalKey < OverlayState >? get key => _overlayKey ;
}
2022-08-12 18:42:02 +08:00
class OverlayDialogManager {
2022-09-08 22:18:02 +08:00
final Map < String , Dialog > _dialogs = {};
2023-02-08 22:01:15 +09:00
var _overlayKeyState = OverlayKeyState ();
2022-08-12 18:42:02 +08:00
int _tagCount = 0 ;
2022-09-08 22:18:02 +08:00
OverlayEntry ? _mobileActionsOverlayEntry ;
2024-06-23 11:06:47 +08:00
RxBool mobileActionsOverlayVisible = true . obs ;
setMobileActionsOverlayVisible ( bool v , { store = true }) {
if ( store ) {
bind . setLocalFlutterOption ( k: kOptionShowMobileAction , v: v ? 'Y' : 'N' );
}
// No need to read the value from local storage after setting it.
// It better to toggle the value directly.
mobileActionsOverlayVisible . value = v ;
}
loadMobileActionsOverlayVisible () {
mobileActionsOverlayVisible . value =
bind . getLocalFlutterOption ( k: kOptionShowMobileAction ) != 'N' ;
}
2022-09-08 22:18:02 +08:00
2023-02-08 22:01:15 +09:00
void setOverlayState ( OverlayKeyState overlayKeyState ) {
_overlayKeyState = overlayKeyState ;
2022-08-12 18:42:02 +08:00
}
2023-06-08 16:42:57 +08:00
void dismissAll () {
2022-08-12 18:42:02 +08:00
_dialogs . forEach (( key , value ) {
value . complete ( null );
BackButtonInterceptor . removeByName ( key );
});
_dialogs . clear ();
}
void dismissByTag ( String tag ) {
_dialogs [ tag ] ? . complete ( null );
_dialogs . remove ( tag );
BackButtonInterceptor . removeByName ( tag );
}
Future < T ?> show < T > ( DialogBuilder builder ,
2022-04-20 23:43:19 +08:00
{ bool clickMaskDismiss = false ,
2022-08-03 22:03:31 +08:00
bool backDismiss = false ,
String ? tag ,
2022-08-12 18:42:02 +08:00
bool useAnimation = true ,
bool forceGlobal = false }) {
final overlayState =
2023-02-08 22:01:15 +09:00
forceGlobal ? globalKey . currentState ? . overlay : _overlayKeyState . state ;
2022-08-12 18:42:02 +08:00
if ( overlayState == null ) {
return Future . error (
"[OverlayDialogManager] Failed to show dialog, _overlayState is null, call [setOverlayState] first" );
2022-04-19 13:07:45 +08:00
}
2022-08-12 18:42:02 +08:00
2022-10-09 19:57:38 +09:00
final String dialogTag ;
2022-08-12 18:42:02 +08:00
if ( tag != null ) {
2022-10-09 19:57:38 +09:00
dialogTag = tag ;
2022-08-12 18:42:02 +08:00
} else {
2022-10-09 19:57:38 +09:00
dialogTag = _tagCount . toString ();
2022-08-12 18:42:02 +08:00
_tagCount ++ ;
}
final dialog = Dialog < T > ();
2022-10-09 19:57:38 +09:00
_dialogs [ dialogTag ] = dialog ;
2022-08-12 18:42:02 +08:00
2022-10-09 19:57:38 +09:00
close ([ res ]) {
_dialogs . remove ( dialogTag );
2025-02-20 18:31:12 +08:00
try {
dialog . complete ( res );
} catch ( e ) {
debugPrint ( "Dialog complete catch error: $ e " );
}
2022-10-09 19:57:38 +09:00
BackButtonInterceptor . removeByName ( dialogTag );
}
2023-02-28 14:08:55 +01:00
dialog . entry = OverlayEntry ( builder: ( context ) {
2022-08-15 14:39:31 +08:00
bool innerClicked = false ;
return Listener (
onPointerUp: ( _ ) {
if ( ! innerClicked && clickMaskDismiss ) {
close ();
}
innerClicked = false ;
},
child: Container (
2023-02-28 14:08:55 +01:00
color: Theme . of ( context ). brightness == Brightness . light
? Colors . black12
: Colors . black45 ,
2022-08-15 14:39:31 +08:00
child: StatefulBuilder ( builder: ( context , setState ) {
return Listener (
onPointerUp: ( _ ) => innerClicked = true ,
2023-05-08 12:34:19 +08:00
child: builder ( setState , close , overlayState . context ),
2022-08-15 14:39:31 +08:00
);
})));
2022-08-12 18:42:02 +08:00
});
overlayState . insert ( dialog . entry ! );
BackButtonInterceptor . add (( stopDefaultButtonEvent , routeInfo ) {
if ( backDismiss ) {
close ();
}
return true ;
2022-10-09 19:57:38 +09:00
}, name: dialogTag );
2022-08-12 18:42:02 +08:00
return dialog . completer . future ;
}
2022-10-09 19:57:38 +09:00
String showLoading ( String text ,
2022-08-15 14:39:31 +08:00
{ bool clickMaskDismiss = false ,
bool showCancel = true ,
2023-08-05 21:40:15 +08:00
VoidCallback ? onCancel ,
String ? tag }) {
if ( tag == null ) {
tag = _tagCount . toString ();
_tagCount ++ ;
}
2023-05-08 12:34:19 +08:00
show (( setState , close , context ) {
2022-09-03 18:19:50 +08:00
cancel () {
dismissAll ();
if ( onCancel != null ) {
onCancel ();
}
}
return CustomAlertDialog (
2022-08-12 18:42:02 +08:00
content: Container (
2022-09-03 18:19:50 +08:00
constraints: const BoxConstraints ( maxWidth: 240 ),
2022-08-12 18:42:02 +08:00
child: Column (
mainAxisSize: MainAxisSize . min ,
crossAxisAlignment: CrossAxisAlignment . start ,
children: [
2022-09-03 18:19:50 +08:00
const SizedBox ( height: 30 ),
const Center ( child: CircularProgressIndicator ()),
const SizedBox ( height: 20 ),
2022-08-12 18:42:02 +08:00
Center (
child: Text ( translate ( text ),
2022-09-03 18:19:50 +08:00
style: const TextStyle ( fontSize: 15 ))),
const SizedBox ( height: 20 ),
2022-08-15 14:39:31 +08:00
Offstage (
offstage: ! showCancel ,
child: Center (
2024-03-28 11:38:11 +08:00
child: ( isDesktop || isWebDesktop )
2023-02-15 16:41:34 +01:00
? dialogButton ( 'Cancel' , onPressed: cancel )
: TextButton (
style: flatButtonStyle ,
onPressed: cancel ,
child: Text ( translate ( 'Cancel' ),
style: const TextStyle (
color: MyTheme . accent )))))
2022-09-03 18:19:50 +08:00
])),
onCancel: showCancel ? cancel : null ,
);
2022-10-09 19:57:38 +09:00
}, tag: tag );
return tag ;
2022-08-12 18:42:02 +08:00
}
2022-09-08 22:18:02 +08:00
void resetMobileActionsOverlay ({ FFI ? ffi }) {
if ( _mobileActionsOverlayEntry == null ) return ;
hideMobileActionsOverlay ();
showMobileActionsOverlay ( ffi: ffi );
}
void showMobileActionsOverlay ({ FFI ? ffi }) {
if ( _mobileActionsOverlayEntry != null ) return ;
2023-02-08 22:01:15 +09:00
final overlayState = _overlayKeyState . state ;
if ( overlayState == null ) return ;
2022-09-08 22:18:02 +08:00
2024-06-01 20:23:58 +08:00
final overlay = makeMobileActionsOverlayEntry (
() => hideMobileActionsOverlay (),
ffi: ffi ,
);
2023-02-08 22:01:15 +09:00
overlayState . insert ( overlay );
2022-09-08 22:18:02 +08:00
_mobileActionsOverlayEntry = overlay ;
2024-06-23 11:06:47 +08:00
setMobileActionsOverlayVisible ( true );
2022-09-08 22:18:02 +08:00
}
2024-06-23 11:06:47 +08:00
void hideMobileActionsOverlay ({ store = true }) {
2022-09-08 22:18:02 +08:00
if ( _mobileActionsOverlayEntry != null ) {
_mobileActionsOverlayEntry ! . remove ();
_mobileActionsOverlayEntry = null ;
2024-06-23 11:06:47 +08:00
setMobileActionsOverlayVisible ( false , store: store );
2022-09-08 22:18:02 +08:00
return ;
}
}
void toggleMobileActionsOverlay ({ FFI ? ffi }) {
if ( _mobileActionsOverlayEntry == null ) {
showMobileActionsOverlay ( ffi: ffi );
} else {
hideMobileActionsOverlay ();
}
}
2023-01-12 21:03:05 +08:00
bool existing ( String tag ) {
return _dialogs . keys . contains ( tag );
}
2022-08-15 14:39:31 +08:00
}
2022-08-12 18:42:02 +08:00
2024-06-01 20:23:58 +08:00
makeMobileActionsOverlayEntry ( VoidCallback ? onHide , { FFI ? ffi }) {
makeMobileActions ( BuildContext context , double s ) {
2024-06-02 10:35:54 +08:00
final scale = s < 0.85 ? 0.85 : s ;
2024-06-01 20:23:58 +08:00
final session = ffi ?? gFFI ;
const double overlayW = 200 ;
const double overlayH = 45 ;
2024-06-23 11:06:47 +08:00
computeOverlayPosition () {
final screenW = MediaQuery . of ( context ). size . width ;
final screenH = MediaQuery . of ( context ). size . height ;
final left = ( screenW - overlayW * scale ) / 2 ;
final top = screenH - ( overlayH + 80 ) * scale ;
return Offset ( left , top );
}
if ( draggablePositions . mobileActions . isInvalid ()) {
draggablePositions . mobileActions . update ( computeOverlayPosition ());
} else {
draggablePositions . mobileActions . tryAdjust ( overlayW , overlayH , scale );
}
2024-06-01 20:23:58 +08:00
return DraggableMobileActions (
scale: scale ,
2024-06-23 11:06:47 +08:00
position: draggablePositions . mobileActions ,
2024-06-01 20:23:58 +08:00
width: overlayW ,
height: overlayH ,
2024-06-27 13:28:05 +08:00
onBackPressed: session . inputModel . onMobileBack ,
onHomePressed: session . inputModel . onMobileHome ,
onRecentPressed: session . inputModel . onMobileApps ,
2024-06-01 20:23:58 +08:00
onHidePressed: onHide ,
);
}
return OverlayEntry ( builder: ( context ) {
if ( isDesktop ) {
final c = Provider . of < CanvasModel > ( context );
return makeMobileActions ( context , c . scale * 2.0 );
} else {
return makeMobileActions ( globalKey . currentContext ! , 1.0 );
}
});
}
2026-01-09 10:03:14 +08:00
void showToast ( String text ,
{ Duration timeout = const Duration ( seconds: 3 ),
Alignment alignment = const Alignment ( 0.0 , 0.8 )}) {
2022-08-15 14:39:31 +08:00
final overlayState = globalKey . currentState ? . overlay ;
if ( overlayState == null ) return ;
2023-12-03 09:56:47 -05:00
final entry = OverlayEntry ( builder: ( context ) {
2022-08-15 14:39:31 +08:00
return IgnorePointer (
child: Align (
2026-01-09 10:03:14 +08:00
alignment: alignment ,
2022-08-15 14:39:31 +08:00
child: Container (
decoration: BoxDecoration (
2023-12-03 09:56:47 -05:00
color: MyTheme . color ( context ). toastBg ,
2022-09-03 18:19:50 +08:00
borderRadius: const BorderRadius . all (
2022-08-15 14:39:31 +08:00
Radius . circular ( 20 ),
),
),
2022-09-03 18:19:50 +08:00
padding: const EdgeInsets . symmetric ( horizontal: 20 , vertical: 5 ),
2022-08-15 14:39:31 +08:00
child: Text (
text ,
2023-04-19 14:39:22 +08:00
textAlign: TextAlign . center ,
2023-12-03 09:56:47 -05:00
style: TextStyle (
2022-08-15 14:39:31 +08:00
decoration: TextDecoration . none ,
fontWeight: FontWeight . w300 ,
fontSize: 18 ,
2023-12-03 09:56:47 -05:00
color: MyTheme . color ( context ). toastText ),
2022-08-15 14:39:31 +08:00
),
)));
});
overlayState . insert ( entry );
Future . delayed ( timeout , () {
entry . remove ();
});
2020-11-16 21:21:27 +08:00
}
2020-11-16 22:00:09 +08:00
2023-03-29 16:14:55 +02:00
// TODO
// - Remove argument "contentPadding", no need for it, all should look the same.
// - Remove "required" for argument "content". See simple confirm dialog "delete peer", only title and actions are used. No need to "content: SizedBox.shrink()".
// - Make dead code alive, transform arguments "onSubmit" and "onCancel" into correspondenting buttons "ConfirmOkButton", "CancelButton".
2022-03-13 00:32:44 +08:00
class CustomAlertDialog extends StatelessWidget {
2022-09-03 18:19:50 +08:00
const CustomAlertDialog (
{ Key ? key ,
this . title ,
2023-07-01 12:54:19 +08:00
this . titlePadding ,
2022-09-03 18:19:50 +08:00
required this . content ,
this . actions ,
this . contentPadding ,
2022-09-29 21:09:40 +08:00
this . contentBoxConstraints = const BoxConstraints ( maxWidth: 500 ),
2022-09-03 18:19:50 +08:00
this . onSubmit ,
this . onCancel })
: super ( key: key );
2020-11-18 12:49:43 +08:00
2022-08-12 18:42:02 +08:00
final Widget ? title ;
2023-07-01 12:54:19 +08:00
final EdgeInsetsGeometry ? titlePadding ;
2022-03-13 00:32:44 +08:00
final Widget content ;
2022-08-12 18:42:02 +08:00
final List < Widget >? actions ;
2022-03-13 00:32:44 +08:00
final double ? contentPadding ;
2022-09-29 21:09:40 +08:00
final BoxConstraints contentBoxConstraints ;
2022-09-03 18:19:50 +08:00
final Function () ? onSubmit ;
final Function () ? onCancel ;
2022-03-13 00:32:44 +08:00
@ override
Widget build ( BuildContext context ) {
2023-01-28 17:10:40 +08:00
// request focus
2023-01-16 20:58:42 +08:00
FocusScopeNode scopeNode = FocusScopeNode ();
2023-01-28 17:10:40 +08:00
Future . delayed ( Duration . zero , () {
if ( ! scopeNode . hasFocus ) scopeNode . requestFocus ();
});
2023-02-16 15:16:54 +08:00
bool tabTapped = false ;
2023-06-07 22:46:20 +08:00
if ( isAndroid ) gFFI . invokeMethod ( "enable_soft_keyboard" , true );
2023-03-29 16:14:55 +02:00
2023-01-16 20:58:42 +08:00
return FocusScope (
node: scopeNode ,
2022-09-03 18:19:50 +08:00
autofocus: true ,
onKey: ( node , key ) {
if ( key . logicalKey == LogicalKeyboardKey . escape ) {
if ( key is RawKeyDownEvent ) {
onCancel ? . call ();
}
return KeyEventResult . handled ; // avoid TextField exception on escape
2023-02-16 15:16:54 +08:00
} else if ( ! tabTapped &&
onSubmit != null &&
2024-06-05 14:52:56 +08:00
( key . logicalKey == LogicalKeyboardKey . enter ||
key . logicalKey == LogicalKeyboardKey . numpadEnter )) {
2022-09-03 18:19:50 +08:00
if ( key is RawKeyDownEvent ) onSubmit ? . call ();
return KeyEventResult . handled ;
2023-01-16 20:58:42 +08:00
} else if ( key . logicalKey == LogicalKeyboardKey . tab ) {
if ( key is RawKeyDownEvent ) {
scopeNode . nextFocus ();
2023-02-16 15:16:54 +08:00
tabTapped = true ;
2023-01-16 20:58:42 +08:00
}
return KeyEventResult . handled ;
2022-09-03 18:19:50 +08:00
}
return KeyEventResult . ignored ;
},
child: AlertDialog (
2023-03-29 16:14:55 +02:00
scrollable: true ,
title: title ,
content: ConstrainedBox (
constraints: contentBoxConstraints ,
child: content ,
),
actions: actions ,
2023-07-01 12:54:19 +08:00
titlePadding: titlePadding ?? MyTheme . dialogTitlePadding (),
2023-03-29 16:14:55 +02:00
contentPadding:
MyTheme . dialogContentPadding ( actions: actions is List ),
actionsPadding: MyTheme . dialogActionsPadding (),
buttonPadding: MyTheme . dialogButtonPadding ),
2022-04-21 10:02:47 +08:00
);
2022-03-13 00:32:44 +08:00
}
2020-11-16 22:00:09 +08:00
}
2020-11-16 22:12:32 +08:00
2024-07-30 14:41:36 +08:00
Widget createDialogContent ( String text ) {
final RegExp linkRegExp = RegExp ( r'(https?://[^\s]+)' );
2026-02-01 12:18:07 +08:00
bool hasLink = linkRegExp . hasMatch ( text );
// Early return: no link, use default theme color
if ( ! hasLink ) {
return SelectableText ( text , style: const TextStyle ( fontSize: 15 ));
}
2024-07-30 14:41:36 +08:00
final List < TextSpan > spans = [];
int start = 0 ;
linkRegExp . allMatches ( text ). forEach (( match ) {
if ( match . start > start ) {
spans . add ( TextSpan ( text: text . substring ( start , match . start )));
}
spans . add ( TextSpan (
text: match . group ( 0 ) ?? '' ,
2026-02-01 12:18:07 +08:00
style: const TextStyle (
2024-07-30 14:41:36 +08:00
color: Colors . blue ,
decoration: TextDecoration . underline ,
),
recognizer: TapGestureRecognizer ()
.. onTap = () {
String linkText = match . group ( 0 ) ?? '' ;
linkText = linkText . replaceAll ( RegExp ( r'[.,;!?]+$' ), '' );
launchUrl ( Uri . parse ( linkText ));
},
));
start = match . end ;
});
if ( start < text . length ) {
spans . add ( TextSpan ( text: text . substring ( start )));
}
return SelectableText . rich (
TextSpan (
2026-02-01 12:18:07 +08:00
style: const TextStyle ( fontSize: 15 ),
2024-07-30 14:41:36 +08:00
children: spans ,
),
);
}
2023-06-06 07:39:44 +08:00
void msgBox ( SessionID sessionId , String type , String title , String text ,
String link , OverlayDialogManager dialogManager ,
2025-05-04 07:32:47 +08:00
{ bool ? hasCancel ,
ReconnectHandle ? reconnect ,
int ? reconnectTimeout ,
VoidCallback ? onSubmit ,
int ? submitTimeout }) {
2022-08-31 18:41:55 +08:00
dialogManager . dismissAll ();
2026-07-06 17:05:11 +08:00
if ( type . contains ( 'insecure-connection' )) {
Future < void > closeSession () async {
await bind . sessionSetCommon (
sessionId: sessionId ,
key: 'continue-insecure-connection' ,
value: 'N' ,
);
dialogManager . dismissAll ();
closeConnection ();
}
void continueSession () {
unawaited (
bind . sessionSetCommon (
sessionId: sessionId ,
key: 'continue-insecure-connection' ,
value: 'Y' ,
),
);
dialogManager . dismissAll ();
}
dialogManager . show (
( setState , close , context ) => CustomAlertDialog (
title: null ,
content: SelectionArea ( child: msgboxContent ( type , title , text )),
actions: [
dialogButton (
'Continue' ,
onPressed: continueSession ,
isOutline: true ,
),
dialogButton ( 'Disconnect' , onPressed: closeSession ),
],
onSubmit: closeSession ,
onCancel: closeSession ,
),
tag: ' $ sessionId - $ type - $ title - $ text - $ link ' ,
);
return ;
}
2022-08-31 18:41:55 +08:00
List < Widget > buttons = [];
2022-09-03 18:19:50 +08:00
bool hasOk = false ;
submit () {
dialogManager . dismissAll ();
2025-05-04 07:32:47 +08:00
if ( onSubmit != null ) {
onSubmit . call ();
} else {
// https://github.com/rustdesk/rustdesk/blob/5e9a31340b899822090a3731769ae79c6bf5f3e5/src/ui/common.tis#L263
if ( ! type . contains ( "custom" ) && desktopType != DesktopType . portForward ) {
closeConnection ();
}
2022-09-03 18:19:50 +08:00
}
}
cancel () {
dialogManager . dismissAll ();
}
2022-10-14 11:19:49 +08:00
jumplink () {
if ( link . startsWith ( 'http' )) {
launchUrl ( Uri . parse ( link ));
}
}
2022-08-31 18:41:55 +08:00
if ( type != "connecting" && type != "success" && ! type . contains ( "nook" )) {
2022-09-03 18:19:50 +08:00
hasOk = true ;
2025-05-04 07:32:47 +08:00
late final Widget btn ;
if ( submitTimeout != null ) {
btn = _CountDownButton (
text: 'OK' ,
second: submitTimeout ,
onPressed: submit ,
submitOnTimeout: true ,
);
} else {
btn = dialogButton ( 'OK' , onPressed: submit );
}
buttons . insert ( 0 , btn );
2022-08-31 18:41:55 +08:00
}
hasCancel ??= ! type . contains ( "error" ) &&
! type . contains ( "nocancel" ) &&
type != "restarting" ;
if ( hasCancel ) {
2023-01-15 19:46:16 +08:00
buttons . insert (
0 , dialogButton ( 'Cancel' , onPressed: cancel , isOutline: true ));
2022-08-31 18:41:55 +08:00
}
if ( type . contains ( "hasclose" )) {
buttons . insert (
0 ,
2023-01-15 19:46:16 +08:00
dialogButton ( 'Close' , onPressed: () {
2022-08-31 18:41:55 +08:00
dialogManager . dismissAll ();
}));
}
2024-10-26 22:05:54 +08:00
if ( reconnect != null &&
title == "Connection Error" &&
reconnectTimeout != null ) {
2023-09-30 14:21:11 +08:00
// `enabled` is used to disable the dialog button once the button is clicked.
2023-09-29 21:42:49 +08:00
final enabled = true . obs ;
2025-05-04 07:32:47 +08:00
final button = Obx (() => _CountDownButton (
text: 'Reconnect' ,
2024-10-26 22:05:54 +08:00
second: reconnectTimeout ,
onPressed: enabled . isTrue
? () {
// Disable the button
enabled . value = false ;
reconnect ( dialogManager , sessionId , false );
}
: null ,
));
2023-09-29 21:42:49 +08:00
buttons . insert ( 0 , button );
2023-02-16 14:54:13 +08:00
}
2022-10-14 11:19:49 +08:00
if ( link . isNotEmpty ) {
2023-01-15 19:46:16 +08:00
buttons . insert ( 0 , dialogButton ( 'JumpLink' , onPressed: jumplink ));
2022-10-14 11:19:49 +08:00
}
2022-11-15 16:49:55 +08:00
dialogManager . show (
2023-05-08 12:34:19 +08:00
( setState , close , context ) => CustomAlertDialog (
2023-01-30 17:56:35 +08:00
title: null ,
2023-02-05 16:56:13 +08:00
content: SelectionArea ( child: msgboxContent ( type , title , text )),
2022-11-15 16:49:55 +08:00
actions: buttons ,
onSubmit: hasOk ? submit : null ,
onCancel: hasCancel == true ? cancel : null ,
),
2023-06-06 07:39:44 +08:00
tag: ' $ sessionId - $ type - $ title - $ text - $ link ' ,
2022-11-15 16:49:55 +08:00
);
2022-08-31 18:41:55 +08:00
}
2023-01-30 17:56:35 +08:00
Color ? _msgboxColor ( String type ) {
if ( type == "input-password" || type == "custom-os-password" ) {
return Color ( 0xFFAD448E );
}
if ( type . contains ( "success" )) {
return Color ( 0xFF32bea6 );
}
if ( type . contains ( "error" ) || type == "re-input-password" ) {
return Color ( 0xFFE04F5F );
}
return Color ( 0xFF2C8CFF );
2022-08-31 18:41:55 +08:00
}
2020-11-29 01:36:10 +08:00
2023-01-30 17:56:35 +08:00
Widget msgboxIcon ( String type ) {
IconData ? iconData ;
if ( type . contains ( "error" ) || type == "re-input-password" ) {
iconData = Icons . cancel ;
}
if ( type . contains ( "success" )) {
iconData = Icons . check_circle ;
}
if ( type == "wait-uac" || type == "wait-remote-accept-nook" ) {
iconData = Icons . hourglass_top ;
}
if ( type == 'on-uac' || type == 'on-foreground-elevated' ) {
iconData = Icons . admin_panel_settings ;
}
2023-10-08 21:44:54 +08:00
if ( type . contains ( 'info' )) {
2023-01-30 17:56:35 +08:00
iconData = Icons . info ;
}
if ( iconData != null ) {
return Icon ( iconData , size: 50 , color: _msgboxColor ( type ))
. marginOnly ( right: 16 );
}
return Offstage ();
}
// title should be null
Widget msgboxContent ( String type , String title , String text ) {
2023-06-29 13:47:55 +08:00
String translateText ( String text ) {
2023-06-29 22:48:51 +08:00
if ( text . indexOf ( 'Failed' ) == 0 && text . indexOf ( ': ' ) > 0 ) {
2023-06-29 22:25:01 +08:00
List < String > words = text . split ( ': ' );
for ( var i = 0 ; i < words . length ; ++ i ) {
words [ i ] = translate ( words [ i ]);
}
text = words . join ( ': ' );
2023-06-29 13:47:55 +08:00
} else {
2023-06-29 22:44:15 +08:00
List < String > words = text . split ( ' ' );
if ( words . length > 1 && words [ 0 ]. endsWith ( '_tip' )) {
words [ 0 ] = translate ( words [ 0 ]);
final rest = text . substring ( words [ 0 ]. length + 1 );
text = ' ${ words [ 0 ] } ${ translate ( rest ) } ' ;
} else {
text = translate ( text );
}
2023-06-29 13:47:55 +08:00
}
2023-06-29 22:25:01 +08:00
return text ;
2023-06-29 13:47:55 +08:00
}
2023-01-30 17:56:35 +08:00
return Row (
children: [
msgboxIcon ( type ),
Expanded (
child: Column (
crossAxisAlignment: CrossAxisAlignment . start ,
children: [
Text (
translate ( title ),
style: TextStyle ( fontSize: 21 ),
). marginOnly ( bottom: 10 ),
2024-07-30 14:41:36 +08:00
createDialogContent ( translateText ( text )),
2023-01-30 17:56:35 +08:00
],
),
),
],
2023-02-05 16:56:13 +08:00
). marginOnly ( bottom: 12 );
2023-01-30 17:56:35 +08:00
}
2022-08-31 23:02:02 -07:00
2022-08-31 18:41:55 +08:00
void msgBoxCommon ( OverlayDialogManager dialogManager , String title ,
2022-09-03 18:19:50 +08:00
Widget content , List < Widget > buttons ,
{ bool hasCancel = true }) {
2023-05-08 12:34:19 +08:00
dialogManager . show (( setState , close , context ) => CustomAlertDialog (
2023-01-30 17:56:35 +08:00
title: Text (
translate ( title ),
style: TextStyle ( fontSize: 21 ),
),
2022-09-03 18:19:50 +08:00
content: content ,
actions: buttons ,
onCancel: hasCancel ? close : null ,
));
2020-11-21 14:40:28 +08:00
}
2020-11-25 18:33:09 +08:00
Color str2color ( String str , [ alpha = 0xFF ]) {
var hash = 160 << 16 + 114 << 8 + 91 ;
for ( var i = 0 ; i < str . length ; i += 1 ) {
hash = str . codeUnitAt ( i ) + (( hash << 5 ) - hash );
}
2021-08-14 14:14:01 +08:00
hash = hash % 16777216 ;
return Color (( hash & 0xFF7FFF ) | ( alpha << 24 ));
2020-11-25 18:33:09 +08:00
}
2022-02-02 17:25:56 +08:00
2023-08-22 19:07:01 +08:00
Color str2color2 ( String str , { List < int > existing = const []}) {
2023-08-15 09:52:02 +08:00
Map < String , Color > colorMap = {
"red" : Colors . red ,
"green" : Colors . green ,
"blue" : Colors . blue ,
"orange" : Colors . orange ,
"purple" : Colors . purple ,
"grey" : Colors . grey ,
"cyan" : Colors . cyan ,
"lime" : Colors . lime ,
"teal" : Colors . teal ,
"pink" : Colors . pink [ 200 ] ! ,
"indigo" : Colors . indigo ,
"brown" : Colors . brown ,
};
final color = colorMap [ str . toLowerCase ()];
if ( color != null ) {
2023-08-22 19:07:01 +08:00
return color . withAlpha ( 0xFF );
2023-08-15 09:52:02 +08:00
}
if ( str . toLowerCase () == 'yellow' ) {
2023-08-22 19:07:01 +08:00
return Colors . yellow . withAlpha ( 0xFF );
2023-08-15 09:52:02 +08:00
}
2023-08-13 18:13:06 +08:00
var hash = 0 ;
for ( var i = 0 ; i < str . length ; i ++ ) {
hash += str . codeUnitAt ( i );
}
2023-08-15 09:52:02 +08:00
List < Color > colorList = colorMap . values . toList ();
2023-08-13 18:13:06 +08:00
hash = hash % colorList . length ;
2023-08-22 19:07:01 +08:00
var result = colorList [ hash ]. withAlpha ( 0xFF );
if ( existing . contains ( result . value )) {
Color ? notUsed =
colorList . firstWhereOrNull (( e ) => ! existing . contains ( e . value ));
if ( notUsed != null ) {
result = notUsed ;
}
}
return result ;
2023-08-13 18:13:06 +08:00
}
2022-03-17 21:03:52 +08:00
const K = 1024 ;
const M = K * K ;
const G = M * K ;
String readableFileSize ( double size ) {
if ( size < K ) {
2022-08-31 18:41:55 +08:00
return " ${ size . toStringAsFixed ( 2 ) } B" ;
2022-03-17 21:03:52 +08:00
} else if ( size < M ) {
2022-08-31 18:41:55 +08:00
return " ${ ( size / K ). toStringAsFixed ( 2 ) } KB" ;
2022-03-17 21:03:52 +08:00
} else if ( size < G ) {
2022-08-31 18:41:55 +08:00
return " ${ ( size / M ). toStringAsFixed ( 2 ) } MB" ;
2022-03-17 21:03:52 +08:00
} else {
2022-08-31 18:41:55 +08:00
return " ${ ( size / G ). toStringAsFixed ( 2 ) } GB" ;
2022-03-17 21:03:52 +08:00
}
}
2022-04-14 15:37:47 +08:00
/// Flutter can't not catch PointerMoveEvent when size is 1
/// This will happen in Android AccessibilityService Input
/// android can't init dispatching size yet ,see: https://stackoverflow.com/questions/59960451/android-accessibility-dispatchgesture-is-it-possible-to-specify-pressure-for-a
/// use this temporary solution until flutter or android fixes the bug
class AccessibilityListener extends StatelessWidget {
final Widget ? child ;
static final offset = 100 ;
AccessibilityListener ({ this . child });
@ override
Widget build ( BuildContext context ) {
return Listener (
onPointerDown: ( evt ) {
2022-05-26 18:25:16 +08:00
if ( evt . size == 1 ) {
GestureBinding . instance . handlePointerEvent ( PointerAddedEvent (
2022-04-14 15:37:47 +08:00
pointer: evt . pointer + offset , position: evt . position ));
2022-05-26 18:25:16 +08:00
GestureBinding . instance . handlePointerEvent ( PointerDownEvent (
2022-04-14 15:37:47 +08:00
pointer: evt . pointer + offset ,
size: 0.1 ,
position: evt . position ));
}
},
onPointerUp: ( evt ) {
2022-06-19 17:15:37 +01:00
if ( evt . size == 1 ) {
2022-05-26 18:25:16 +08:00
GestureBinding . instance . handlePointerEvent ( PointerUpEvent (
2022-04-14 15:37:47 +08:00
pointer: evt . pointer + offset ,
size: 0.1 ,
position: evt . position ));
2022-05-26 18:25:16 +08:00
GestureBinding . instance . handlePointerEvent ( PointerRemovedEvent (
2022-04-15 17:45:48 +08:00
pointer: evt . pointer + offset , position: evt . position ));
2022-04-14 15:37:47 +08:00
}
},
onPointerMove: ( evt ) {
2022-06-19 17:15:37 +01:00
if ( evt . size == 1 ) {
2022-05-26 18:25:16 +08:00
GestureBinding . instance . handlePointerEvent ( PointerMoveEvent (
2022-04-14 15:37:47 +08:00
pointer: evt . pointer + offset ,
size: 0.1 ,
delta: evt . delta ,
position: evt . position ));
}
},
child: child );
}
}
2022-04-20 22:37:47 +08:00
2023-03-01 18:00:56 +01:00
class AndroidPermissionManager {
2022-04-20 22:37:47 +08:00
static Completer < bool >? _completer ;
static Timer ? _timer ;
static var _current = "" ;
static Future < bool > check ( String type ) {
2024-03-28 11:38:11 +08:00
if ( isDesktop || isWeb ) {
2022-08-18 00:34:04 +08:00
return Future . value ( true );
}
2022-06-13 21:07:26 +08:00
return gFFI . invokeMethod ( "check_permission" , type );
2022-04-20 22:37:47 +08:00
}
2023-03-01 18:00:56 +01:00
// startActivity goto Android Setting's page to request permission manually by user
static void startAction ( String action ) {
gFFI . invokeMethod ( AndroidChannel . kStartAction , action );
}
/// We use XXPermissions to request permissions,
/// for supported types, see https://github.com/getActivity/XXPermissions/blob/e46caea32a64ad7819df62d448fb1c825481cd28/library/src/main/java/com/hjq/permissions/Permission.java
2022-04-20 22:37:47 +08:00
static Future < bool > request ( String type ) {
2024-03-28 11:38:11 +08:00
if ( isDesktop || isWeb ) {
2022-08-18 00:34:04 +08:00
return Future . value ( true );
}
2022-04-20 22:37:47 +08:00
2022-08-01 14:33:08 +08:00
gFFI . invokeMethod ( "request_permission" , type );
2023-03-01 18:00:56 +01:00
// clear last task
if ( _completer ? . isCompleted == false ) {
_completer ? . complete ( false );
2022-07-14 17:44:37 +08:00
}
2023-03-01 18:00:56 +01:00
_timer ? . cancel ();
2022-04-20 22:37:47 +08:00
_current = type ;
_completer = Completer < bool > ();
2023-03-01 18:00:56 +01:00
_timer = Timer ( Duration ( seconds: 120 ), () {
2022-04-20 22:37:47 +08:00
if ( _completer == null ) return ;
if ( ! _completer ! . isCompleted ) {
_completer ! . complete ( false );
}
_completer = null ;
_current = "" ;
});
return _completer ! . future ;
}
static complete ( String type , bool res ) {
if ( type != _current ) {
res = false ;
}
_timer ? . cancel ();
_completer ? . complete ( res );
_current = "" ;
}
}
2022-06-13 21:07:26 +08:00
2022-07-29 18:34:25 +08:00
RadioListTile < T > getRadio < T > (
2023-04-12 09:41:13 +08:00
Widget title , T toValue , T curValue , ValueChanged < T ?>? onChange ,
2024-07-26 10:31:01 +08:00
{ bool ? dense }) {
2022-07-29 18:34:25 +08:00
return RadioListTile < T > (
2023-03-30 02:33:04 +02:00
visualDensity: VisualDensity . compact ,
2022-07-29 18:34:25 +08:00
controlAffinity: ListTileControlAffinity . trailing ,
2023-04-12 09:41:13 +08:00
title: title ,
2022-07-29 18:34:25 +08:00
value: toValue ,
groupValue: curValue ,
onChanged: onChange ,
2023-06-11 16:32:22 +08:00
dense: dense ,
2022-07-29 18:34:25 +08:00
);
}
2022-08-01 10:44:05 +08:00
2022-06-13 21:07:26 +08:00
/// find ffi, tag is Remote ID
/// for session specific usage
FFI ffi ( String ? tag ) {
return Get . find < FFI > ( tag: tag );
}
/// Global FFI object
late FFI _globalFFI ;
FFI get gFFI => _globalFFI ;
Future < void > initGlobalFFI () async {
2022-08-04 17:24:02 +08:00
debugPrint ( "_globalFFI init" );
2023-08-03 23:14:40 +08:00
_globalFFI = FFI ( null );
2022-08-04 17:24:02 +08:00
debugPrint ( "_globalFFI init end" );
2022-06-13 21:07:26 +08:00
// after `put`, can also be globally found by Get.find<FFI>();
2024-06-30 21:24:18 +08:00
Get . put < FFI > ( _globalFFI , permanent: true );
2022-08-03 22:03:31 +08:00
}
2022-08-08 17:53:51 +08:00
String translate ( String name ) {
if ( name . startsWith ( 'Failed to' ) && name . contains ( ': ' )) {
return name . split ( ': ' ). map (( x ) => translate ( x )). join ( ': ' );
}
return platformFFI . translate ( name , localeName );
}
2022-08-13 12:43:35 +08:00
2024-06-10 00:11:59 +08:00
// This function must be kept the same as the one in rust and sciter code.
// rust: libs/hbb_common/src/config.rs -> option2bool()
// sciter: Does not have the function, but it should be kept the same.
2022-08-15 11:08:42 +08:00
bool option2bool ( String option , String value ) {
2022-08-13 12:43:35 +08:00
bool res ;
2022-08-15 11:08:42 +08:00
if ( option . startsWith ( "enable-" )) {
2022-08-13 12:43:35 +08:00
res = value != "N" ;
2022-08-15 11:08:42 +08:00
} else if ( option . startsWith ( "allow-" ) ||
2024-06-10 19:53:02 +08:00
option == kOptionStopService ||
2024-05-18 23:13:54 +08:00
option == kOptionDirectServer ||
2023-08-08 12:12:35 +08:00
option == kOptionForceAlwaysRelay ) {
2022-08-13 12:43:35 +08:00
res = value == "Y" ;
} else {
2026-01-21 16:25:57 +08:00
// "" is true
2022-08-13 12:43:35 +08:00
res = value != "N" ;
}
return res ;
}
2022-08-15 11:08:42 +08:00
String bool2option ( String option , bool b ) {
2022-08-13 12:43:35 +08:00
String res ;
2025-07-01 13:12:55 +08:00
if ( option . startsWith ( 'enable-' ) &&
option != kOptionEnableUdpPunch &&
2026-09-06 00:21:28 +08:00
option != kOptionEnableIpv6Punch &&
option != kOptionEnableWebrtc ) {
2024-05-17 14:19:11 +08:00
res = b ? defaultOptionYes : 'N' ;
2022-08-15 11:08:42 +08:00
} else if ( option . startsWith ( 'allow-' ) ||
2024-06-10 19:53:02 +08:00
option == kOptionStopService ||
2024-05-18 23:13:54 +08:00
option == kOptionDirectServer ||
2023-08-08 12:12:35 +08:00
option == kOptionForceAlwaysRelay ) {
2024-05-17 14:19:11 +08:00
res = b ? 'Y' : defaultOptionNo ;
2022-08-13 12:43:35 +08:00
} else {
2022-08-15 11:08:42 +08:00
res = b ? 'Y' : 'N' ;
2022-08-13 12:43:35 +08:00
}
return res ;
}
2022-08-27 00:45:09 +08:00
2023-07-23 17:07:30 +08:00
mainSetBoolOption ( String key , bool value ) async {
String v = bool2option ( key , value );
await bind . mainSetOption ( key: key , value: v );
}
Future < bool > mainGetBoolOption ( String key ) async {
return option2bool ( key , await bind . mainGetOption ( key: key ));
}
bool mainGetBoolOptionSync ( String key ) {
return option2bool ( key , bind . mainGetOptionSync ( key: key ));
}
2023-07-30 12:16:00 +08:00
mainSetLocalBoolOption ( String key , bool value ) async {
String v = bool2option ( key , value );
await bind . mainSetLocalOption ( key: key , value: v );
}
bool mainGetLocalBoolOptionSync ( String key ) {
return option2bool ( key , bind . mainGetLocalOption ( key: key ));
}
2023-08-08 12:12:35 +08:00
bool mainGetPeerBoolOptionSync ( String id , String key ) {
return option2bool ( key , bind . mainGetPeerOptionSync ( id: id , key: key ));
}
2024-06-10 19:53:02 +08:00
// Don't use `option2bool()` and `bool2option()` to convert the session option.
// Use `sessionGetToggleOption()` and `sessionToggleOption()` instead.
// Because all session options use `Y` and `<Empty>` as values.
2023-08-08 12:12:35 +08:00
2025-10-12 14:59:42 +08:00
Future < bool > matchPeer (
String searchText , Peer peer , PeerTabIndex peerTabIndex ) async {
2022-08-27 00:45:09 +08:00
if ( searchText . isEmpty ) {
return true ;
}
if ( peer . id . toLowerCase (). contains ( searchText )) {
return true ;
}
if ( peer . hostname . toLowerCase (). contains ( searchText ) ||
peer . username . toLowerCase (). contains ( searchText )) {
return true ;
}
2025-10-12 14:59:42 +08:00
if ( peer . alias . toLowerCase (). contains ( searchText )) {
return true ;
2022-08-27 00:45:09 +08:00
}
2025-10-12 14:59:42 +08:00
if ( peerTabShowNote ( peerTabIndex ) &&
peer . note . toLowerCase (). contains ( searchText )) {
return true ;
}
return false ;
2022-08-27 00:45:09 +08:00
}
2022-09-13 21:36:38 +08:00
/// Get the image for the current [platform].
Widget getPlatformImage ( String platform , { double size = 50 }) {
2023-06-23 17:05:48 +08:00
if ( platform . isEmpty ) {
return Container ( width: size , height: size );
}
2023-01-10 17:13:40 +08:00
if ( platform == kPeerPlatformMacOS ) {
2022-09-13 21:36:38 +08:00
platform = 'mac' ;
2023-01-10 17:13:40 +08:00
} else if ( platform != kPeerPlatformLinux &&
platform != kPeerPlatformAndroid ) {
2022-09-13 21:36:38 +08:00
platform = 'win' ;
2023-01-11 18:41:45 +08:00
} else {
2023-01-12 21:03:05 +08:00
platform = platform . toLowerCase ();
2022-09-13 21:36:38 +08:00
}
2022-09-27 18:34:05 +08:00
return SvgPicture . asset ( 'assets/ $ platform .svg' , height: size , width: size );
2022-09-13 21:36:38 +08:00
}
2022-09-16 12:14:03 +08:00
class LastWindowPosition {
double ? width ;
double ? height ;
double ? offsetWidth ;
double ? offsetHeight ;
2022-10-14 19:48:41 +09:00
bool ? isMaximized ;
2023-09-07 13:39:20 +08:00
bool ? isFullscreen ;
2022-09-16 12:14:03 +08:00
2022-10-14 19:48:41 +09:00
LastWindowPosition ( this . width , this . height , this . offsetWidth ,
2023-09-07 13:39:20 +08:00
this . offsetHeight , this . isMaximized , this . isFullscreen );
2022-09-16 12:14:03 +08:00
2025-10-11 03:11:56 -05:00
bool equals ( LastWindowPosition other ) {
2025-11-13 23:35:40 +08:00
return (( width == other . width ) &&
( height == other . height ) &&
( offsetWidth == other . offsetWidth ) &&
( offsetHeight == other . offsetHeight ) &&
( isMaximized == other . isMaximized ) &&
( isFullscreen == other . isFullscreen ));
2025-10-11 03:11:56 -05:00
}
2022-09-16 12:14:03 +08:00
Map < String , dynamic > toJson () {
return < String , dynamic > {
"width" : width ,
"height" : height ,
"offsetWidth" : offsetWidth ,
2022-10-14 19:48:41 +09:00
"offsetHeight" : offsetHeight ,
"isMaximized" : isMaximized ,
2023-09-07 13:39:20 +08:00
"isFullscreen" : isFullscreen ,
2022-09-16 12:14:03 +08:00
};
}
@ override
String toString () {
return jsonEncode ( toJson ());
}
static LastWindowPosition ? loadFromString ( String content ) {
if ( content . isEmpty ) {
return null ;
}
try {
final m = jsonDecode ( content );
2022-10-14 19:48:41 +09:00
return LastWindowPosition ( m [ "width" ], m [ "height" ], m [ "offsetWidth" ],
2023-09-07 13:39:20 +08:00
m [ "offsetHeight" ], m [ "isMaximized" ], m [ "isFullscreen" ]);
2022-09-16 12:14:03 +08:00
} catch ( e ) {
2023-07-26 21:29:35 +08:00
debugPrintStack (
label:
'Failed to load LastWindowPosition " $ content " ${ e . toString () } ' );
2022-09-16 12:14:03 +08:00
return null ;
}
}
}
2024-02-27 17:02:10 +08:00
String get windowFramePrefix =>
2024-03-14 11:36:14 +08:00
kWindowPrefix +
( bind . isIncomingOnly ()
? "incoming_"
: ( bind . isOutgoingOnly () ? "outgoing_" : "" ));
2024-02-27 17:02:10 +08:00
2025-10-11 03:11:56 -05:00
typedef WindowKey = ({ WindowType type , int ? windowId });
LastWindowPosition ? _lastWindowPosition = null ;
final Debouncer _saveWindowDebounce = Debouncer ( delay: Duration ( seconds: 1 ));
2022-09-16 12:14:03 +08:00
/// Save window position and size on exit
/// Note that windowId must be provided if it's subwindow
2025-10-24 17:20:56 +08:00
Future < void > saveWindowPosition ( WindowType type ,
{ int ? windowId , bool ? flush }) async {
2022-09-16 12:14:03 +08:00
if ( type != WindowType . Main && windowId == null ) {
debugPrint (
"Error: windowId cannot be null when saving positions for sub window" );
}
2023-07-26 07:52:40 +08:00
2025-10-24 17:20:56 +08:00
Offset ? position ;
Size ? sz ;
2023-07-26 07:52:40 +08:00
late bool isMaximized ;
2024-05-23 22:11:40 +08:00
bool isFullscreen = stateGlobal . fullscreen . isTrue ;
2025-10-24 17:20:56 +08:00
2024-05-23 09:51:19 +08:00
setPreFrame () {
final pos = bind . getLocalFlutterOption ( k: windowFramePrefix + type . name );
var lpos = LastWindowPosition . loadFromString ( pos );
2025-10-24 17:20:56 +08:00
if ( lpos != null ) {
if ( lpos . offsetWidth != null && lpos . offsetHeight != null ) {
position = Offset ( lpos . offsetWidth ! , lpos . offsetHeight ! );
}
if ( lpos . width != null && lpos . height != null ) {
sz = Size ( lpos . width ! , lpos . height ! );
}
}
2023-08-11 12:01:57 +08:00
}
2022-09-16 12:14:03 +08:00
switch ( type ) {
case WindowType . Main:
2024-03-15 08:42:41 +08:00
// Checking `bind.isIncomingOnly()` is a simple workaround for MacOS.
// `await windowManager.isMaximized()` will always return true
// if is not resizable. The reason is unknown.
//
2024-05-10 16:40:29 +08:00
// `setResizable(!bind.isIncomingOnly());` in main.dart
2024-03-15 08:42:41 +08:00
isMaximized =
bind . isIncomingOnly () ? false : await windowManager . isMaximized ();
2024-05-23 09:51:19 +08:00
if ( isFullscreen || isMaximized ) {
setPreFrame ();
} else {
2024-11-11 00:35:41 +08:00
position = await windowManager . getPosition (
ignoreDevicePixelRatio: _ignoreDevicePixelRatio );
sz = await windowManager . getSize (
ignoreDevicePixelRatio: _ignoreDevicePixelRatio );
2024-05-23 09:51:19 +08:00
}
2022-09-16 12:14:03 +08:00
break ;
default :
2022-10-14 19:48:41 +09:00
final wc = WindowController . fromWindowId ( windowId ! );
2023-08-11 12:01:57 +08:00
isMaximized = await wc . isMaximized ();
2024-05-23 09:51:19 +08:00
if ( isFullscreen || isMaximized ) {
setPreFrame ();
} else {
final Rect frame ;
try {
frame = await wc . getFrame ();
} catch ( e ) {
debugPrint (
"Failed to get frame of window $ windowId , it may be hidden" );
return ;
}
position = frame . topLeft ;
sz = frame . size ;
2023-06-21 00:30:34 +08:00
}
2022-09-16 12:14:03 +08:00
break ;
}
2025-10-24 17:20:56 +08:00
if ( isWindows && position != null ) {
2023-07-27 20:45:29 +08:00
const kMinOffset = - 10000 ;
2023-07-27 22:19:38 +08:00
const kMaxOffset = 10000 ;
2025-10-24 17:20:56 +08:00
if ( position ! . dx < kMinOffset ||
position ! . dy < kMinOffset ||
position ! . dx > kMaxOffset ||
position ! . dy > kMaxOffset ) {
2023-07-27 20:45:29 +08:00
debugPrint ( "Invalid position: $ position , ignore saving position" );
return ;
}
}
2023-07-26 07:52:40 +08:00
2025-10-24 17:20:56 +08:00
final pos = LastWindowPosition ( sz ? . width , sz ? . height , position ? . dx ,
position ? . dy , isMaximized , isFullscreen );
2023-08-02 22:02:18 +08:00
2025-10-11 03:11:56 -05:00
final WindowKey key = ( type: type , windowId: windowId );
2023-08-02 22:02:18 +08:00
2025-11-13 23:35:40 +08:00
final bool haveNewWindowPosition =
( _lastWindowPosition == null ) || ! pos . equals ( _lastWindowPosition ! );
2025-10-11 03:11:56 -05:00
final bool isPreviousNewWindowPositionPending = _saveWindowDebounce . isRunning ;
if ( haveNewWindowPosition || isPreviousNewWindowPositionPending ) {
_lastWindowPosition = pos ;
if ( flush ?? false ) {
// If a previous update is pending, replace it.
_saveWindowDebounce . cancel ();
await _saveWindowPositionActual ( key );
} else if ( haveNewWindowPosition ) {
_saveWindowDebounce . call (() => _saveWindowPositionActual ( key ));
}
}
}
Future < void > _saveWindowPositionActual ( WindowKey key ) async {
LastWindowPosition ? pos = _lastWindowPosition ;
if ( pos != null ) {
debugPrint (
"Saving frame: ${ key . windowId } : ${ pos . width } / ${ pos . height } , offset: ${ pos . offsetWidth } / ${ pos . offsetHeight } , isMaximized: ${ pos . isMaximized } , isFullscreen: ${ pos . isFullscreen } " );
await bind . setLocalFlutterOption (
k: windowFramePrefix + key . type . name , v: pos . toString ());
2025-11-13 23:35:40 +08:00
if (( key . type == WindowType . RemoteDesktop ||
key . type == WindowType . ViewCamera ) &&
2025-10-11 03:11:56 -05:00
key . windowId != null ) {
2025-11-13 23:35:40 +08:00
await _saveSessionWindowPosition ( key . type , key . windowId ! ,
pos . isMaximized ?? false , pos . isFullscreen ?? false , pos );
2025-10-11 03:11:56 -05:00
}
2023-08-02 22:02:18 +08:00
}
}
2023-08-11 12:01:57 +08:00
Future _saveSessionWindowPosition ( WindowType windowType , int windowId ,
2023-09-07 13:39:20 +08:00
bool isMaximized , bool isFullscreen , LastWindowPosition pos ) async {
2023-08-02 22:02:18 +08:00
final remoteList = await DesktopMultiWindow . invokeMethod (
windowId , kWindowEventGetRemoteList , null );
2023-08-11 12:01:57 +08:00
getPeerPos ( String peerId ) {
2024-05-23 09:51:19 +08:00
if ( isMaximized || isFullscreen ) {
2023-08-11 12:01:57 +08:00
final peerPos = bind . mainGetPeerFlutterOptionSync (
2024-02-27 17:02:10 +08:00
id: peerId , k: windowFramePrefix + windowType . name );
2023-08-11 12:01:57 +08:00
var lpos = LastWindowPosition . loadFromString ( peerPos );
return LastWindowPosition (
lpos ? . width ?? pos . offsetWidth ,
lpos ? . height ?? pos . offsetHeight ,
lpos ? . offsetWidth ?? pos . offsetWidth ,
lpos ? . offsetHeight ?? pos . offsetHeight ,
2023-09-07 13:39:20 +08:00
isMaximized ,
isFullscreen )
2023-08-11 12:01:57 +08:00
. toString ();
} else {
return pos . toString ();
}
}
2023-08-02 22:02:18 +08:00
if ( remoteList != null ) {
for ( final peerId in remoteList . split ( ',' )) {
2023-08-10 22:27:35 +08:00
bind . mainSetPeerFlutterOptionSync (
2023-08-11 12:01:57 +08:00
id: peerId ,
2024-02-27 17:02:10 +08:00
k: windowFramePrefix + windowType . name ,
2023-08-11 12:01:57 +08:00
v: getPeerPos ( peerId ));
2023-08-02 22:02:18 +08:00
}
}
2022-09-16 12:14:03 +08:00
}
2022-10-14 19:48:41 +09:00
Future < Size > _adjustRestoreMainWindowSize ( double ? width , double ? height ) async {
2023-08-03 18:15:50 +08:00
const double minWidth = 1 ;
const double minHeight = 1 ;
const double maxWidth = 6480 ;
const double maxHeight = 6480 ;
2022-10-09 19:27:30 +08:00
2022-10-10 09:56:27 +08:00
final defaultWidth =
(( isDesktop || isWebDesktop ) ? 1280 : kMobileDefaultDisplayWidth )
. toDouble ();
final defaultHeight =
(( isDesktop || isWebDesktop ) ? 720 : kMobileDefaultDisplayHeight )
. toDouble ();
2022-10-09 19:27:30 +08:00
double restoreWidth = width ?? defaultWidth ;
double restoreHeight = height ?? defaultHeight ;
if ( restoreWidth < minWidth ) {
2023-07-26 21:29:35 +08:00
restoreWidth = defaultWidth ;
2022-10-09 19:27:30 +08:00
}
if ( restoreHeight < minHeight ) {
2023-07-26 21:29:35 +08:00
restoreHeight = defaultHeight ;
2022-10-09 19:27:30 +08:00
}
if ( restoreWidth > maxWidth ) {
2023-07-26 21:29:35 +08:00
restoreWidth = defaultWidth ;
2022-10-09 19:27:30 +08:00
}
if ( restoreHeight > maxHeight ) {
2023-07-26 21:29:35 +08:00
restoreHeight = defaultHeight ;
2022-10-09 19:27:30 +08:00
}
2022-10-14 19:48:41 +09:00
return Size ( restoreWidth , restoreHeight );
2022-10-09 19:27:30 +08:00
}
2025-10-08 20:23:55 -04:00
// Consider using Rect.contains() instead,
// though the implementation is not exactly the same.
2024-05-18 08:23:09 +08:00
bool isPointInRect ( Offset point , Rect rect ) {
return point . dx >= rect . left &&
point . dx <= rect . right &&
point . dy >= rect . top &&
point . dy <= rect . bottom ;
}
2022-10-14 19:48:41 +09:00
/// return null means center
2024-11-11 00:35:41 +08:00
Future < Offset ?> _adjustRestoreMainWindowOffset (
2023-07-26 21:29:35 +08:00
double ? left ,
double ? top ,
double ? width ,
double ? height ,
) async {
if ( left == null || top == null || width == null || height == null ) {
return null ;
}
2022-10-09 19:27:30 +08:00
2023-07-26 21:29:35 +08:00
if ( isDesktop || isWebDesktop ) {
2026-01-07 11:20:26 +05:30
final screens = await window_size . getScreenList ();
if ( screens . isNotEmpty ) {
final windowRect = Rect . fromLTWH ( left , top , width , height );
bool isVisible = false ;
for ( final screen in screens ) {
final intersection = windowRect . intersect ( screen . visibleFrame );
if ( intersection . width >= 10.0 && intersection . height >= 10.0 ) {
isVisible = true ;
break ;
}
}
if ( ! isVisible ) {
return null ;
}
return Offset ( left , top );
2022-10-09 19:27:30 +08:00
}
}
2026-01-07 11:20:26 +05:30
double frameLeft = 0.0 ;
double frameTop = 0.0 ;
double frameRight = (( isDesktop || isWebDesktop )
? kDesktopMaxDisplaySize
: kMobileMaxDisplaySize )
. toDouble ();
double frameBottom = (( isDesktop || isWebDesktop )
? kDesktopMaxDisplaySize
: kMobileMaxDisplaySize )
. toDouble ();
2023-07-27 09:11:43 +08:00
final minWidth = 10.0 ;
2026-01-07 11:20:26 +05:30
if (( left + minWidth ) > frameRight ||
( top + minWidth ) > frameBottom ||
2023-07-27 09:13:39 +08:00
( left + width - minWidth ) < frameLeft ||
2026-01-07 11:20:26 +05:30
top < frameTop ) {
2023-07-26 21:29:35 +08:00
return null ;
} else {
2024-11-11 00:35:41 +08:00
return Offset ( left , top );
2023-07-26 21:29:35 +08:00
}
2022-10-09 19:27:30 +08:00
}
2022-10-14 19:48:41 +09:00
/// Restore window position and size on start
2022-09-16 12:14:03 +08:00
/// Note that windowId must be provided if it's subwindow
2023-10-19 07:50:59 +08:00
//
// display is used to set the offset of the window in individual display mode.
2023-08-02 23:10:31 +08:00
Future < bool > restoreWindowPosition ( WindowType type ,
2023-10-19 07:50:59 +08:00
{ int ? windowId , String ? peerId , int ? display }) async {
2023-06-27 13:52:29 +08:00
if ( bind
. mainGetEnv ( key: "DISABLE_RUSTDESK_RESTORE_WINDOW_POSITION" )
. isNotEmpty ) {
2023-06-27 13:26:55 +08:00
return false ;
}
2022-09-16 12:14:03 +08:00
if ( type != WindowType . Main && windowId == null ) {
debugPrint (
"Error: windowId cannot be null when saving positions for sub window" );
2023-08-02 22:02:18 +08:00
return false ;
2022-09-16 12:14:03 +08:00
}
2023-08-02 22:02:18 +08:00
2023-08-03 18:03:56 +08:00
bool isRemotePeerPos = false ;
2023-08-02 22:02:18 +08:00
String ? pos ;
2023-08-09 18:37:09 +08:00
// No need to check mainGetLocalBoolOptionSync(kOptionOpenNewConnInTabs)
2023-08-11 12:01:57 +08:00
// Though "open in tabs" is true and the new window restore peer position, it's ok.
2025-03-10 21:06:53 +08:00
if (( type == WindowType . RemoteDesktop || type == WindowType . ViewCamera ) &&
windowId != null &&
peerId != null ) {
2024-05-23 09:51:19 +08:00
final peerPos = bind . mainGetPeerFlutterOptionSync (
id: peerId , k: windowFramePrefix + type . name );
if ( peerPos . isNotEmpty ) {
pos = peerPos ;
2023-08-08 18:14:01 +08:00
}
2023-08-03 18:03:56 +08:00
isRemotePeerPos = pos != null ;
2023-08-02 22:02:18 +08:00
}
2024-02-27 17:02:10 +08:00
pos ??= bind . getLocalFlutterOption ( k: windowFramePrefix + type . name );
2023-08-02 23:10:31 +08:00
2022-10-14 19:48:41 +09:00
var lpos = LastWindowPosition . loadFromString ( pos );
if ( lpos == null ) {
2025-09-24 01:47:29 -05:00
debugPrint ( "No window position saved, trying to center the window." );
switch ( type ) {
case WindowType . Main:
// Center the main window only if no position is saved (on first run).
if ( isWindows || isLinux ) {
await windowManager . center ();
}
// For MacOS, the window is already centered by default.
// See https://github.com/rustdesk/rustdesk/blob/9b9276e7524523d7f667fefcd0694d981443df0e/flutter/macos/Runner/Base.lproj/MainMenu.xib#L333
// If `<windowPositionMask>` in `<window>` is not set, the window will be centered.
break ;
default :
// No need to change the position of a sub window if no position is saved,
// since the default position is already centered.
// https://github.com/rustdesk/rustdesk/blob/317639169359936f7f9f85ef445ec9774218772d/flutter/lib/utils/multi_window_manager.dart#L163
break ;
}
return true ;
2022-10-14 19:48:41 +09:00
}
2025-03-10 21:06:53 +08:00
if ( type == WindowType . RemoteDesktop || type == WindowType . ViewCamera ) {
2023-10-19 07:50:59 +08:00
if ( ! isRemotePeerPos && windowId != null ) {
if ( lpos . offsetWidth != null ) {
2023-10-19 12:21:35 +08:00
lpos . offsetWidth = lpos . offsetWidth ! + windowId * kNewWindowOffset ;
2023-10-19 07:50:59 +08:00
}
if ( lpos . offsetHeight != null ) {
2023-10-19 12:21:35 +08:00
lpos . offsetHeight = lpos . offsetHeight ! + windowId * kNewWindowOffset ;
2023-10-19 07:50:59 +08:00
}
2023-08-03 18:03:56 +08:00
}
2023-10-19 07:50:59 +08:00
if ( display != null ) {
if ( lpos . offsetWidth != null ) {
2023-10-19 12:21:35 +08:00
lpos . offsetWidth = lpos . offsetWidth ! + display * kNewWindowOffset ;
2023-10-19 07:50:59 +08:00
}
if ( lpos . offsetHeight != null ) {
2023-10-19 12:21:35 +08:00
lpos . offsetHeight = lpos . offsetHeight ! + display * kNewWindowOffset ;
2023-10-19 07:50:59 +08:00
}
2023-08-03 18:03:56 +08:00
}
}
2022-10-14 19:48:41 +09:00
2023-08-11 12:01:57 +08:00
final size = await _adjustRestoreMainWindowSize ( lpos . width , lpos . height );
2024-11-11 00:35:41 +08:00
final offsetLeftTop = await _adjustRestoreMainWindowOffset (
2023-08-11 12:01:57 +08:00
lpos . offsetWidth ,
lpos . offsetHeight ,
size . width ,
size . height ,
);
debugPrint (
2024-11-11 00:35:41 +08:00
"restore lpos: ${ size . width } / ${ size . height } , offset: ${ offsetLeftTop ? . dx } / ${ offsetLeftTop ? . dy } , isMaximized: ${ lpos . isMaximized } , isFullscreen: ${ lpos . isFullscreen } " );
2023-08-11 12:01:57 +08:00
2022-09-16 12:14:03 +08:00
switch ( type ) {
case WindowType . Main:
2023-08-11 12:01:57 +08:00
restorePos () async {
2024-11-11 00:35:41 +08:00
if ( offsetLeftTop == null ) {
2022-10-14 19:48:41 +09:00
await windowManager . center ();
} else {
2024-11-11 00:35:41 +08:00
await windowManager . setPosition ( offsetLeftTop ,
ignoreDevicePixelRatio: _ignoreDevicePixelRatio );
2022-10-14 19:48:41 +09:00
}
2022-09-16 12:14:03 +08:00
}
2023-08-11 12:01:57 +08:00
if ( lpos . isMaximized == true ) {
await restorePos ();
2024-03-14 11:36:14 +08:00
if ( ! ( bind . isIncomingOnly () || bind . isOutgoingOnly ())) {
2024-02-27 17:02:10 +08:00
await windowManager . maximize ();
}
2023-08-11 12:01:57 +08:00
} else {
2024-11-11 00:35:41 +08:00
final storeSize = ! bind . isIncomingOnly () || bind . isOutgoingOnly ();
if ( isWindows ) {
if ( storeSize ) {
// We need to set the window size first to avoid the incorrect size in some special cases.
// E.g. There are two monitors, the left one is 100% DPI and the right one is 175% DPI.
// The window belongs to the left monitor, but if it is moved a little to the right, it will belong to the right monitor.
// After restoring, the size will be incorrect.
// See known issue in https://github.com/rustdesk/rustdesk/pull/9840
await windowManager . setSize ( size ,
ignoreDevicePixelRatio: _ignoreDevicePixelRatio );
}
await restorePos ();
if ( storeSize ) {
await windowManager . setSize ( size ,
ignoreDevicePixelRatio: _ignoreDevicePixelRatio );
}
} else {
if ( storeSize ) {
await windowManager . setSize ( size ,
ignoreDevicePixelRatio: _ignoreDevicePixelRatio );
}
await restorePos ();
2024-02-27 17:02:10 +08:00
}
2023-08-11 12:01:57 +08:00
}
2022-09-16 12:14:03 +08:00
return true ;
default :
2022-10-14 19:48:41 +09:00
final wc = WindowController . fromWindowId ( windowId ! );
2023-08-11 12:01:57 +08:00
restoreFrame () async {
2024-11-11 00:35:41 +08:00
if ( offsetLeftTop == null ) {
2022-10-14 19:48:41 +09:00
await wc . center ();
} else {
2024-11-11 00:35:41 +08:00
final frame = Rect . fromLTWH (
offsetLeftTop . dx , offsetLeftTop . dy , size . width , size . height );
2022-10-14 19:48:41 +09:00
await wc . setFrame ( frame );
}
}
2023-09-07 13:39:20 +08:00
if ( lpos . isFullscreen == true ) {
2024-05-23 09:51:19 +08:00
if ( ! isMacOS ) {
await restoreFrame ();
}
2023-09-07 13:39:20 +08:00
// An duration is needed to avoid the window being restored after fullscreen.
Future . delayed ( Duration ( milliseconds: 300 ), () async {
2024-05-23 09:51:19 +08:00
if ( kWindowId == windowId ) {
stateGlobal . setFullscreen ( true );
} else {
// If is not current window, we need to send a fullscreen message to `windowId`
DesktopMultiWindow . invokeMethod (
windowId , kWindowEventSetFullscreen , 'true' );
}
2023-09-07 13:39:20 +08:00
});
} else if ( lpos . isMaximized == true ) {
2023-08-11 12:01:57 +08:00
await restoreFrame ();
2023-09-03 22:27:42 +08:00
// An duration is needed to avoid the window being restored after maximized.
Future . delayed ( Duration ( milliseconds: 300 ), () async {
await wc . maximize ();
});
2023-08-11 12:01:57 +08:00
} else {
await restoreFrame ();
}
2022-09-16 12:14:03 +08:00
break ;
}
return false ;
}
2022-09-19 20:26:39 +08:00
2024-10-19 15:32:17 +08:00
var webInitialLink = "" ;
2022-10-18 10:29:33 +08:00
/// Initialize uni links for macos/windows
///
/// [Availability]
/// initUniLinks should only be used on macos/windows.
/// we use dbus for linux currently.
2023-02-03 18:52:22 +08:00
Future < bool > initUniLinks () async {
2024-03-23 10:08:55 +08:00
if ( isLinux ) {
2023-02-03 18:52:22 +08:00
return false ;
2022-10-18 10:29:33 +08:00
}
// check cold boot
try {
final initialLink = await getInitialLink ();
2024-03-27 15:51:30 +08:00
print ( "initialLink: $ initialLink " );
if ( initialLink == null || initialLink . isEmpty ) {
2023-02-03 18:52:22 +08:00
return false ;
2022-10-19 09:54:04 +08:00
}
2024-10-19 15:32:17 +08:00
if ( isWeb ) {
webInitialLink = initialLink ;
return false ;
} else {
return handleUriLink ( uriString: initialLink );
}
2022-10-19 09:54:04 +08:00
} catch ( err ) {
2022-12-09 10:49:47 +08:00
debugPrintStack ( label: " $ err " );
2023-02-03 18:52:22 +08:00
return false ;
2022-10-18 10:29:33 +08:00
}
}
2023-02-07 01:31:11 +08:00
/// Listen for uni links.
///
2023-02-07 01:35:38 +08:00
/// * handleByFlutter: Should uni links be handled by Flutter.
2023-02-07 01:31:11 +08:00
///
/// Returns a [StreamSubscription] which can listen the uni links.
StreamSubscription ? listenUniLinks ({ handleByFlutter = true }) {
2024-10-19 15:32:17 +08:00
if ( isLinux || isWeb ) {
2022-10-31 16:35:14 +08:00
return null ;
2022-10-18 10:29:33 +08:00
}
2022-10-31 16:35:14 +08:00
final sub = uriLinkStream . listen (( Uri ? uri ) {
2023-08-11 15:01:42 +08:00
debugPrint ( "A uri was received: $ uri . handleByFlutter $ handleByFlutter " );
2022-10-31 16:35:14 +08:00
if ( uri != null ) {
2023-11-03 01:46:51 +05:30
if ( handleByFlutter ) {
handleUriLink ( uri: uri );
} else {
bind . sendUrlScheme ( url: uri . toString ());
2023-11-02 02:13:33 +05:30
}
2023-11-03 01:51:40 +05:30
} else {
2022-10-31 16:35:14 +08:00
print ( "uni listen error: uri is empty." );
}
}, onError: ( err ) {
print ( "uni links error: $ err " );
});
return sub ;
2022-10-18 10:29:33 +08:00
}
2023-07-07 12:22:39 +08:00
enum UriLinkType {
remoteDesktop ,
fileTransfer ,
2025-03-10 21:06:53 +08:00
viewCamera ,
2023-07-07 12:22:39 +08:00
portForward ,
rdp ,
2025-07-01 13:12:55 +08:00
terminal ,
2023-07-07 12:22:39 +08:00
}
2025-07-15 16:32:14 +08:00
setEnvTerminalAdmin () {
bind . mainSetEnv ( key: 'IS_TERMINAL_ADMIN' , value: 'Y' );
}
2023-07-07 12:22:39 +08:00
// uri link handler
bool handleUriLink ({ List < String >? cmdArgs , Uri ? uri , String ? uriString }) {
List < String >? args ;
2023-10-12 23:11:11 +08:00
if ( cmdArgs != null && cmdArgs . isNotEmpty ) {
2023-07-07 12:22:39 +08:00
args = cmdArgs ;
2023-07-07 18:35:15 +08:00
// rustdesk <uri link>
2024-02-25 13:29:06 +08:00
if ( args [ 0 ]. startsWith ( bind . mainUriPrefixSync ())) {
2023-07-07 12:22:39 +08:00
final uri = Uri . tryParse ( args [ 0 ]);
if ( uri != null ) {
args = urlLinkToCmdArgs ( uri );
}
}
} else if ( uri != null ) {
args = urlLinkToCmdArgs ( uri );
} else if ( uriString != null ) {
final uri = Uri . tryParse ( uriString );
if ( uri != null ) {
args = urlLinkToCmdArgs ( uri );
2023-02-04 01:22:40 +08:00
}
}
2023-08-11 15:01:42 +08:00
if ( args == null ) {
return false ;
}
if ( args . isEmpty ) {
windowOnTop ( null );
return true ;
}
2023-07-07 12:22:39 +08:00
UriLinkType ? type ;
String ? id ;
String ? password ;
String ? switchUuid ;
bool ? forceRelay ;
for ( int i = 0 ; i < args . length ; i ++ ) {
switch ( args [ i ]) {
case '--connect' :
case '--play' :
type = UriLinkType . remoteDesktop ;
id = args [ i + 1 ];
i ++ ;
break ;
case '--file-transfer' :
type = UriLinkType . fileTransfer ;
id = args [ i + 1 ];
i ++ ;
break ;
2025-03-10 21:06:53 +08:00
case '--view-camera' :
type = UriLinkType . viewCamera ;
id = args [ i + 1 ];
i ++ ;
break ;
2023-07-07 12:22:39 +08:00
case '--port-forward' :
type = UriLinkType . portForward ;
id = args [ i + 1 ];
i ++ ;
break ;
case '--rdp' :
type = UriLinkType . rdp ;
id = args [ i + 1 ];
i ++ ;
break ;
2025-07-01 13:12:55 +08:00
case '--terminal' :
type = UriLinkType . terminal ;
id = args [ i + 1 ];
i ++ ;
break ;
2025-07-15 16:32:14 +08:00
case '--terminal-admin' :
setEnvTerminalAdmin ();
type = UriLinkType . terminal ;
id = args [ i + 1 ];
i ++ ;
break ;
2023-07-07 12:22:39 +08:00
case '--password' :
password = args [ i + 1 ];
i ++ ;
break ;
case '--switch_uuid' :
switchUuid = args [ i + 1 ];
i ++ ;
break ;
case '--relay' :
forceRelay = true ;
break ;
default :
break ;
2022-10-12 21:57:19 +08:00
}
}
2023-07-07 12:22:39 +08:00
if ( type != null && id != null ) {
switch ( type ) {
case UriLinkType . remoteDesktop:
Future . delayed ( Duration . zero , () {
rustDeskWinManager . newRemoteDesktop ( id ! ,
password: password ,
2023-08-01 22:19:38 +08:00
switchUuid: switchUuid ,
2023-07-07 12:22:39 +08:00
forceRelay: forceRelay );
});
break ;
case UriLinkType . fileTransfer:
Future . delayed ( Duration . zero , () {
rustDeskWinManager . newFileTransfer ( id ! ,
password: password , forceRelay: forceRelay );
});
break ;
2025-03-10 21:06:53 +08:00
case UriLinkType . viewCamera:
Future . delayed ( Duration . zero , () {
rustDeskWinManager . newViewCamera ( id ! ,
password: password , forceRelay: forceRelay );
});
break ;
2023-07-07 12:22:39 +08:00
case UriLinkType . portForward:
Future . delayed ( Duration . zero , () {
rustDeskWinManager . newPortForward ( id ! , false ,
password: password , forceRelay: forceRelay );
});
break ;
case UriLinkType . rdp:
Future . delayed ( Duration . zero , () {
rustDeskWinManager . newPortForward ( id ! , true ,
password: password , forceRelay: forceRelay );
});
break ;
2025-07-01 13:12:55 +08:00
case UriLinkType . terminal:
Future . delayed ( Duration . zero , () {
rustDeskWinManager . newTerminal ( id ! ,
password: password , forceRelay: forceRelay );
});
break ;
2023-07-07 12:22:39 +08:00
}
return true ;
}
2022-11-26 11:40:13 +08:00
return false ;
2022-10-12 21:57:19 +08:00
}
2023-07-07 12:22:39 +08:00
List < String >? urlLinkToCmdArgs ( Uri uri ) {
String ? command ;
String ? id ;
2025-03-10 21:06:53 +08:00
final options = [
"connect" ,
"play" ,
"file-transfer" ,
"view-camera" ,
"port-forward" ,
2025-07-01 13:12:55 +08:00
"rdp" ,
2025-07-15 16:32:14 +08:00
"terminal" ,
"terminal-admin" ,
2025-03-10 21:06:53 +08:00
];
2023-08-11 15:01:42 +08:00
if ( uri . authority . isEmpty &&
uri . path . split ( '' ). every (( char ) => char == '/' )) {
return [];
} else if ( uri . authority == "connection" && uri . path . startsWith ( "/new/" )) {
2023-07-07 12:22:39 +08:00
// For compatibility
command = '--connect' ;
id = uri . path . substring ( "/new/" . length );
2023-12-03 20:31:48 +08:00
} else if ( uri . authority == "config" ) {
2024-03-23 10:08:55 +08:00
if ( isAndroid || isIOS ) {
2026-04-16 23:21:14 +08:00
final allowDeepLinkServerSettings =
bind . mainGetBuildinOption ( key: kOptionAllowDeepLinkServerSettings ) ==
'Y' ;
if ( ! allowDeepLinkServerSettings ) {
debugPrint (
"Ignore rustdesk://config because $ kOptionAllowDeepLinkServerSettings is not enabled." );
// Keep the user-facing error generic; detailed rejection reason is in debug logs.
// Delay toast to avoid missing overlay during cold-start deeplink handling.
Timer ( Duration ( seconds: 1 ), () {
showToast ( translate ( 'Failed' ));
});
return null ;
}
2023-12-03 22:33:52 +08:00
final config = uri . path . substring ( "/" . length );
// add a timer to make showToast work
Timer ( Duration ( seconds: 1 ), () {
importConfig ( null , null , config );
});
}
2023-12-03 20:31:48 +08:00
return null ;
2023-12-03 20:51:53 +08:00
} else if ( uri . authority == "password" ) {
2024-03-23 10:08:55 +08:00
if ( isAndroid || isIOS ) {
2026-04-16 23:21:14 +08:00
final allowDeepLinkPassword =
bind . mainGetBuildinOption ( key: kOptionAllowDeepLinkPassword ) == 'Y' ;
if ( ! allowDeepLinkPassword ) {
debugPrint (
"Ignore rustdesk://password because $ kOptionAllowDeepLinkPassword is not enabled." );
// Keep the user-facing error generic; detailed rejection reason is in debug logs.
// Delay toast to avoid missing overlay during cold-start deeplink handling.
Timer ( Duration ( seconds: 1 ), () {
showToast ( translate ( 'Failed' ));
});
return null ;
}
2023-12-03 20:51:53 +08:00
final password = uri . path . substring ( "/" . length );
if ( password . isNotEmpty ) {
Timer ( Duration ( seconds: 1 ), () async {
2026-03-26 14:49:54 +08:00
final ok =
await bind . mainSetPermanentPasswordWithResult ( password: password );
showToast ( translate ( ok ? 'Successful' : 'Failed' ));
2023-12-03 20:51:53 +08:00
});
}
}
2023-11-03 18:31:30 +05:30
} else if ( options . contains ( uri . authority )) {
2023-07-07 12:22:39 +08:00
command = '-- ${ uri . authority } ' ;
if ( uri . path . length > 1 ) {
id = uri . path . substring ( 1 );
}
2023-12-03 22:18:11 +08:00
} else if ( uri . authority . length > 2 &&
( uri . path . length <= 1 ||
( uri . path == '/r' || uri . path . startsWith ( '/r@' )))) {
2023-07-07 18:35:15 +08:00
// rustdesk://<connect-id>
2023-12-03 22:18:11 +08:00
// rustdesk://<connect-id>/r
// rustdesk://<connect-id>/r@<server>
2023-07-07 18:35:15 +08:00
command = '--connect' ;
id = uri . authority ;
2023-12-03 22:18:11 +08:00
if ( uri . path . length > 1 ) {
id = id + uri . path ;
}
}
2023-12-04 22:34:54 -05:00
2024-11-20 19:44:24 +08:00
var queryParameters =
uri . queryParameters . map (( k , v ) => MapEntry ( k . toLowerCase (), v ));
var key = queryParameters [ "key" ];
2023-12-03 22:18:11 +08:00
if ( id != null ) {
if ( key != null ) {
id = " $ id ?key= $ key " ;
}
2022-10-11 19:52:03 +08:00
}
2023-07-07 12:22:39 +08:00
2025-07-01 13:12:55 +08:00
if ( isMobile && id != null ) {
final forceRelay = queryParameters [ "relay" ] != null ;
final password = queryParameters [ "password" ];
// Determine connection type based on command
if ( command == '--file-transfer' ) {
2025-06-10 22:01:15 +08:00
connect ( Get . context ! , id ,
2025-07-01 13:12:55 +08:00
isFileTransfer: true , forceRelay: forceRelay , password: password );
} else if ( command == '--view-camera' ) {
connect ( Get . context ! , id ,
isViewCamera: true , forceRelay: forceRelay , password: password );
} else if ( command == '--terminal' ) {
connect ( Get . context ! , id ,
isTerminal: true , forceRelay: forceRelay , password: password );
2025-07-15 16:32:14 +08:00
} else if ( command == 'terminal-admin' ) {
setEnvTerminalAdmin ();
connect ( Get . context ! , id ,
isTerminal: true , forceRelay: forceRelay , password: password );
2025-07-01 13:12:55 +08:00
} else {
// Default to remote desktop for '--connect', '--play', or direct connection
connect ( Get . context ! , id , forceRelay: forceRelay , password: password );
2023-11-03 01:46:51 +05:30
}
2025-07-01 13:12:55 +08:00
return null ;
2023-11-03 01:46:51 +05:30
}
2023-07-07 12:22:39 +08:00
List < String > args = List . empty ( growable: true );
if ( command != null && id != null ) {
args . add ( command );
args . add ( id );
2024-11-20 19:44:24 +08:00
var param = queryParameters ;
2023-07-07 12:22:39 +08:00
String ? password = param [ "password" ];
if ( password != null ) args . addAll ([ '--password' , password ]);
String ? switch_uuid = param [ "switch_uuid" ];
if ( switch_uuid != null ) args . addAll ([ '--switch_uuid' , switch_uuid ]);
if ( param [ "relay" ] != null ) args . add ( "--relay" );
return args ;
}
return null ;
2022-10-11 19:52:03 +08:00
}
2024-03-20 15:05:54 +08:00
connectMainDesktop ( String id ,
{ required bool isFileTransfer ,
2025-03-10 21:06:53 +08:00
required bool isViewCamera ,
2025-07-01 13:12:55 +08:00
required bool isTerminal ,
2024-03-20 15:05:54 +08:00
required bool isTcpTunneling ,
required bool isRDP ,
bool ? forceRelay ,
String ? password ,
2024-10-24 17:20:48 +08:00
String ? connToken ,
2024-03-20 15:05:54 +08:00
bool ? isSharedPassword }) async {
2022-12-01 13:52:12 +08:00
if ( isFileTransfer ) {
2024-03-20 15:05:54 +08:00
await rustDeskWinManager . newFileTransfer ( id ,
password: password ,
isSharedPassword: isSharedPassword ,
2024-10-24 17:20:48 +08:00
connToken: connToken ,
2024-03-20 15:05:54 +08:00
forceRelay: forceRelay );
2025-03-10 21:06:53 +08:00
} else if ( isViewCamera ) {
await rustDeskWinManager . newViewCamera ( id ,
password: password ,
isSharedPassword: isSharedPassword ,
connToken: connToken ,
forceRelay: forceRelay );
2022-12-01 13:52:12 +08:00
} else if ( isTcpTunneling || isRDP ) {
2024-03-20 15:05:54 +08:00
await rustDeskWinManager . newPortForward ( id , isRDP ,
password: password ,
isSharedPassword: isSharedPassword ,
2024-10-24 17:20:48 +08:00
connToken: connToken ,
2024-03-20 15:05:54 +08:00
forceRelay: forceRelay );
2025-07-01 13:12:55 +08:00
} else if ( isTerminal ) {
await rustDeskWinManager . newTerminal ( id ,
password: password ,
isSharedPassword: isSharedPassword ,
connToken: connToken ,
forceRelay: forceRelay );
2022-12-01 13:52:12 +08:00
} else {
2024-03-20 15:05:54 +08:00
await rustDeskWinManager . newRemoteDesktop ( id ,
password: password ,
isSharedPassword: isSharedPassword ,
forceRelay: forceRelay );
2022-12-01 13:52:12 +08:00
}
}
2022-09-19 20:26:39 +08:00
/// Connect to a peer with [id].
/// If [isFileTransfer], starts a session only for file transfer.
2025-03-10 21:06:53 +08:00
/// If [isViewCamera], starts a session only for view camera.
2022-09-19 20:26:39 +08:00
/// If [isTcpTunneling], starts a session only for tcp tunneling.
/// If [isRDP], starts a session only for rdp.
2024-03-20 15:05:54 +08:00
connect ( BuildContext context , String id ,
{ bool isFileTransfer = false ,
2025-03-10 21:06:53 +08:00
bool isViewCamera = false ,
2025-07-01 13:12:55 +08:00
bool isTerminal = false ,
2024-03-20 15:05:54 +08:00
bool isTcpTunneling = false ,
bool isRDP = false ,
bool forceRelay = false ,
String ? password ,
2024-10-24 17:20:48 +08:00
String ? connToken ,
2024-03-20 15:05:54 +08:00
bool ? isSharedPassword }) async {
2022-09-19 20:26:39 +08:00
if ( id == '' ) return ;
2023-08-08 17:22:25 +08:00
if ( ! isDesktop || desktopType == DesktopType . main ) {
try {
if ( Get . isRegistered < IDTextEditingController > ()) {
final idController = Get . find < IDTextEditingController > ();
idController . text = formatID ( id );
}
2023-11-14 12:11:38 +08:00
if ( Get . isRegistered < TextEditingController > ()) {
2023-10-25 02:36:23 +05:30
final fieldTextEditingController = Get . find < TextEditingController > ();
fieldTextEditingController . text = formatID ( id );
}
2023-08-08 17:22:25 +08:00
} catch ( _ ) {}
}
2022-09-19 20:26:39 +08:00
id = id . replaceAll ( ' ' , '' );
2023-02-26 11:23:43 +08:00
final oldId = id ;
id = await bind . mainHandleRelayId ( id: id );
2025-07-01 13:12:55 +08:00
forceRelay = id != oldId || forceRelay ;
2022-09-19 20:26:39 +08:00
assert ( ! ( isFileTransfer && isTcpTunneling && isRDP ),
"more than one connect type" );
2022-09-22 16:45:14 +08:00
if ( isDesktop ) {
2022-12-01 13:52:12 +08:00
if ( desktopType == DesktopType . main ) {
2023-08-02 23:10:31 +08:00
await connectMainDesktop (
id ,
isFileTransfer: isFileTransfer ,
2025-03-10 21:06:53 +08:00
isViewCamera: isViewCamera ,
2025-07-01 13:12:55 +08:00
isTerminal: isTerminal ,
2023-08-02 23:10:31 +08:00
isTcpTunneling: isTcpTunneling ,
isRDP: isRDP ,
2024-03-20 15:05:54 +08:00
password: password ,
isSharedPassword: isSharedPassword ,
2025-07-01 13:12:55 +08:00
forceRelay: forceRelay ,
2023-08-02 23:10:31 +08:00
);
2022-09-22 16:45:14 +08:00
} else {
2022-12-01 13:52:12 +08:00
await rustDeskWinManager . call ( WindowType . Main , kWindowConnect , {
'id' : id ,
'isFileTransfer' : isFileTransfer ,
2025-03-10 21:06:53 +08:00
'isViewCamera' : isViewCamera ,
2025-07-01 13:12:55 +08:00
'isTerminal' : isTerminal ,
2022-12-01 13:52:12 +08:00
'isTcpTunneling' : isTcpTunneling ,
'isRDP' : isRDP ,
2024-03-20 15:05:54 +08:00
'password' : password ,
'isSharedPassword' : isSharedPassword ,
2023-08-02 23:10:31 +08:00
'forceRelay' : forceRelay ,
2024-10-24 17:20:48 +08:00
'connToken' : connToken ,
2022-12-01 13:52:12 +08:00
});
2022-09-22 16:45:14 +08:00
}
2022-09-19 20:26:39 +08:00
} else {
2022-09-22 16:45:14 +08:00
if ( isFileTransfer ) {
2024-10-12 09:03:13 +08:00
if ( isWeb ) {
Navigator . push (
context ,
MaterialPageRoute (
builder: ( BuildContext context ) =>
desktop_file_manager . FileManagerPage (
id: id ,
password: password ,
isSharedPassword: isSharedPassword ),
),
);
} else {
Navigator . push (
context ,
MaterialPageRoute (
builder: ( BuildContext context ) => FileManagerPage (
2025-07-01 13:12:55 +08:00
id: id ,
password: password ,
isSharedPassword: isSharedPassword ,
forceRelay: forceRelay ),
2024-10-12 09:03:13 +08:00
),
);
}
2025-03-10 21:06:53 +08:00
} else if ( isViewCamera ) {
if ( isWeb ) {
Navigator . push (
context ,
MaterialPageRoute (
builder: ( BuildContext context ) =>
desktop_view_camera . ViewCameraPage (
key: ValueKey ( id ),
id: id ,
toolbarState: ToolbarState (),
password: password ,
isSharedPassword: isSharedPassword ,
),
),
);
} else {
Navigator . push (
context ,
MaterialPageRoute (
builder: ( BuildContext context ) => ViewCameraPage (
2025-07-01 13:12:55 +08:00
id: id ,
password: password ,
isSharedPassword: isSharedPassword ,
forceRelay: forceRelay ),
2025-03-10 21:06:53 +08:00
),
);
}
2025-07-01 13:12:55 +08:00
} else if ( isTerminal ) {
Navigator . push (
context ,
MaterialPageRoute (
builder: ( BuildContext context ) => TerminalPage (
id: id ,
password: password ,
isSharedPassword: isSharedPassword ,
forceRelay: forceRelay ,
),
),
);
2022-09-22 16:45:14 +08:00
} else {
2024-10-11 09:52:09 +08:00
if ( isWeb ) {
2024-03-28 11:38:11 +08:00
Navigator . push (
context ,
MaterialPageRoute (
builder: ( BuildContext context ) => desktop_remote . RemotePage (
key: ValueKey ( id ),
id: id ,
toolbarState: ToolbarState (),
password: password ,
isSharedPassword: isSharedPassword ,
),
),
);
} else {
Navigator . push (
context ,
MaterialPageRoute (
builder: ( BuildContext context ) => RemotePage (
2025-07-01 13:12:55 +08:00
id: id ,
password: password ,
isSharedPassword: isSharedPassword ,
forceRelay: forceRelay ),
2024-03-28 11:38:11 +08:00
),
);
}
2022-09-22 16:45:14 +08:00
}
2024-10-01 15:25:59 +08:00
stateGlobal . isInMainPage = false ;
2022-09-19 20:26:39 +08:00
}
2022-09-22 16:45:14 +08:00
FocusScopeNode currentFocus = FocusScope . of ( context );
2022-09-19 20:26:39 +08:00
if ( ! currentFocus . hasPrimaryFocus ) {
currentFocus . unfocus ();
}
}
2022-09-27 17:52:36 +08:00
2023-01-06 19:26:19 +09:00
Map < String , String > getHttpHeaders () {
2022-09-27 17:52:36 +08:00
return {
2022-11-10 21:25:12 +08:00
'Authorization' : 'Bearer ${ bind . mainGetLocalOption ( key: 'access_token' ) } '
2022-09-27 17:52:36 +08:00
};
}
2022-10-03 22:03:49 -07:00
2022-12-25 20:21:13 +03:00
// Simple wrapper of built-in types for reference use.
2022-10-03 22:03:49 -07:00
class SimpleWrapper < T > {
2022-11-03 21:58:25 +08:00
T value ;
SimpleWrapper ( this . value );
2022-10-03 22:03:49 -07:00
}
2022-10-26 14:39:13 +08:00
2026-01-05 22:16:35 +08:00
/// Wakelock manager with reference counting for desktop.
/// Ensures wakelock is only disabled when all sessions are closed/minimized.
///
/// Note: Each isolate has its own WakelockPlus instance with independent assertion.
/// As long as one isolate has wakelock enabled, the screen stays awake.
/// This manager handles multiple tabs within the same isolate.
class WakelockManager {
static final Set < UniqueKey > _enabledKeys = {};
2026-01-21 16:25:57 +08:00
// Don't use WakelockPlus.enabled, it causes error on Android:
// Unhandled Exception: FormatException: Message corrupted
//
// On Linux, multiple enable() calls create only one inhibit, but each disable()
// only releases if _cookie != null. So we need our own _enabled state to avoid
// calling disable() when not enabled.
// See: https://github.com/fluttercommunity/wakelock_plus/blob/0c74e5bbc6aefac57b6c96bb7ef987705ed559ec/wakelock_plus/lib/src/wakelock_plus_linux_plugin.dart#L48
static bool _enabled = false ;
2026-01-05 22:16:35 +08:00
2026-01-21 16:25:57 +08:00
static void enable ( UniqueKey key , { bool isServer = false }) {
// Check if we should keep awake during outgoing sessions
if ( ! isServer ) {
final keepAwake =
mainGetLocalBoolOptionSync ( kOptionKeepAwakeDuringOutgoingSessions );
if ( ! keepAwake ) {
return ; // Don't enable wakelock if user disabled keep awake
}
}
if ( isDesktop ) {
_enabledKeys . add ( key );
}
if ( ! _enabled ) {
_enabled = true ;
WakelockPlus . enable ();
}
2026-01-05 22:16:35 +08:00
}
static void disable ( UniqueKey key ) {
2026-01-21 16:25:57 +08:00
if ( isDesktop ) {
_enabledKeys . remove ( key );
if ( _enabledKeys . isNotEmpty ) {
return ;
2026-01-05 22:16:35 +08:00
}
}
2026-01-21 16:25:57 +08:00
if ( _enabled ) {
WakelockPlus . disable ();
_enabled = false ;
}
2026-01-05 22:16:35 +08:00
}
}
2022-10-26 14:39:13 +08:00
/// call this to reload current window.
///
/// [Note]
/// Must have [RefreshWrapper] on the top of widget tree.
void reloadCurrentWindow () {
if ( Get . context != null ) {
// reload self window
RefreshWrapper . of ( Get . context ! ) ? . rebuild ();
} else {
debugPrint (
"reload current window failed, global BuildContext does not exist" );
}
}
/// call this to reload all windows, including main + all sub windows.
Future < void > reloadAllWindows () async {
reloadCurrentWindow ();
try {
final ids = await DesktopMultiWindow . getAllSubWindowIds ();
for ( final id in ids ) {
DesktopMultiWindow . invokeMethod ( id , kWindowActionRebuild );
}
} on AssertionError {
// ignore
}
}
2022-11-05 23:41:22 +08:00
/// Indicate the flutter app is running in portable mode.
///
/// [Note]
2022-12-25 20:21:13 +03:00
/// Portable build is only available on Windows.
2022-11-05 23:41:22 +08:00
bool isRunningInPortableMode () {
2024-03-23 10:08:55 +08:00
if ( ! isWindows ) {
2022-11-05 23:41:22 +08:00
return false ;
}
return bool . hasEnvironment ( kEnvPortableExecutable );
}
/// Window status callback
2023-01-06 20:40:29 -08:00
Future < void > onActiveWindowChanged () async {
2022-11-05 23:41:22 +08:00
print (
"[MultiWindowHandler] active window changed: ${ rustDeskWinManager . getActiveWindows () } " );
if ( rustDeskWinManager . getActiveWindows (). isEmpty ) {
// close all sub windows
try {
2024-03-23 10:08:55 +08:00
if ( isLinux ) {
2023-07-26 07:52:40 +08:00
await Future . wait ([
saveWindowPosition ( WindowType . Main ),
rustDeskWinManager . closeAllSubWindows ()
]);
} else {
await rustDeskWinManager . closeAllSubWindows ();
}
2022-11-05 23:41:22 +08:00
} catch ( err ) {
2022-12-09 10:49:47 +08:00
debugPrintStack ( label: " $ err " );
2022-11-05 23:41:22 +08:00
} finally {
2023-02-03 17:08:40 +08:00
debugPrint ( "Start closing RustDesk..." );
2022-11-05 23:41:22 +08:00
await windowManager . setPreventClose ( false );
await windowManager . close ();
2024-03-23 10:08:55 +08:00
if ( isMacOS ) {
2024-06-02 10:35:54 +08:00
// If we call without delay, `flutter/macos/Runner/MainFlutterWindow.swift` can handle the "terminate" event.
// But the app will not close.
//
// No idea why we need to delay here, `terminate()` itself is also an async function.
2024-11-11 13:06:23 +08:00
//
// A quick workaround, use `Timer.periodic` to avoid the app not closing.
// Because `await windowManager.close()` and `RdPlatformChannel.instance.terminate()`
// may not work since `Flutter 3.24.4`, see the following logs.
// A delay will allow the app to close.
//
//```
// embedder.cc (2725): 'FlutterPlatformMessageCreateResponseHandle' returned 'kInvalidArguments'. Engine handle was invalid.
// 2024-11-11 11:41:11.546 RustDesk[90272:2567686] Failed to create a FlutterPlatformMessageResponseHandle (2)
// embedder.cc (2672): 'FlutterEngineSendPlatformMessage' returned 'kInvalidArguments'. Invalid engine handle.
// 2024-11-11 11:41:11.565 RustDesk[90272:2567686] Failed to send message to Flutter engine on channel 'flutter/lifecycle' (2).
// ```
2024-11-20 19:44:24 +08:00
periodic_immediate (
Duration ( milliseconds: 30 ), RdPlatformChannel . instance . terminate );
2023-02-03 17:08:40 +08:00
}
2022-11-05 23:41:22 +08:00
}
}
}
2022-11-29 22:36:35 +08:00
Timer periodic_immediate ( Duration duration , Future < void > Function () callback ) {
Future . delayed ( Duration . zero , callback );
return Timer . periodic ( duration , ( timer ) async {
await callback ();
});
}
2022-11-30 13:56:02 +08:00
2022-11-29 23:03:16 +08:00
/// return a human readable windows version
WindowsTarget getWindowsTarget ( int buildNumber ) {
2024-03-23 10:08:55 +08:00
if ( ! isWindows ) {
2022-11-29 23:03:16 +08:00
return WindowsTarget . naw ;
2022-11-30 13:56:02 +08:00
}
2022-11-29 23:03:16 +08:00
if ( buildNumber >= 22000 ) {
return WindowsTarget . w11 ;
} else if ( buildNumber >= 10240 ) {
return WindowsTarget . w10 ;
} else if ( buildNumber >= 9600 ) {
return WindowsTarget . w8_1 ;
} else if ( buildNumber >= 9200 ) {
return WindowsTarget . w8 ;
} else if ( buildNumber >= 7601 ) {
return WindowsTarget . w7 ;
} else if ( buildNumber >= 6002 ) {
return WindowsTarget . vista ;
} else {
// minimum support
return WindowsTarget . xp ;
}
}
2022-11-30 13:56:02 +08:00
/// Get windows target build number.
///
/// [Note]
/// Please use this function wrapped with `Platform.isWindows`.
int getWindowsTargetBuildNumber () {
2024-03-22 13:16:37 +08:00
return getWindowsTargetBuildNumber_ ();
2022-11-30 13:56:02 +08:00
}
/// Indicating we need to use compatible ui mode.
///
/// [Conditions]
/// - Windows 7, window will overflow when we use frameless ui.
bool get kUseCompatibleUiMode =>
2024-03-23 10:08:55 +08:00
isWindows &&
2022-11-30 13:56:02 +08:00
const [ WindowsTarget . w7 ]. contains ( windowsBuildNumber . windowsVersion );
2022-12-21 15:14:43 +09:00
2025-02-10 13:50:28 +08:00
bool get isWin10 => windowsBuildNumber . windowsVersion == WindowsTarget . w10 ;
2022-12-21 15:14:43 +09:00
class ServerConfig {
late String idServer ;
late String relayServer ;
late String apiServer ;
late String key ;
ServerConfig (
{ String ? idServer , String ? relayServer , String ? apiServer , String ? key }) {
this . idServer = idServer ? . trim () ?? '' ;
this . relayServer = relayServer ? . trim () ?? '' ;
this . apiServer = apiServer ? . trim () ?? '' ;
this . key = key ? . trim () ?? '' ;
}
2022-12-21 16:24:01 +09:00
/// decode from shared string (from user shared or rustdesk-server generated)
/// also see [encode]
/// throw when decoding failure
2022-12-21 15:14:43 +09:00
ServerConfig . decode ( String msg ) {
2023-03-19 16:33:33 +08:00
var json = {};
try {
2023-03-19 16:41:51 +08:00
// back compatible
2023-03-19 16:33:33 +08:00
json = jsonDecode ( msg );
} catch ( err ) {
final input = msg . split ( '' ). reversed . join ( '' );
final bytes = base64Decode ( base64 . normalize ( input ));
2025-08-20 14:55:52 +08:00
json = jsonDecode ( utf8 . decode ( bytes , allowMalformed: true ));
2023-03-19 16:33:33 +08:00
}
2022-12-21 15:14:43 +09:00
idServer = json [ 'host' ] ?? '' ;
relayServer = json [ 'relay' ] ?? '' ;
apiServer = json [ 'api' ] ?? '' ;
key = json [ 'key' ] ?? '' ;
}
2022-12-21 16:24:01 +09:00
/// encode to shared string
/// also see [ServerConfig.decode]
2022-12-21 15:14:43 +09:00
String encode () {
Map < String , String > config = {};
config [ 'host' ] = idServer . trim ();
config [ 'relay' ] = relayServer . trim ();
config [ 'api' ] = apiServer . trim ();
config [ 'key' ] = key . trim ();
2024-11-12 20:15:34 +08:00
return base64UrlEncode ( Uint8List . fromList ( jsonEncode ( config ). codeUnits ))
2022-12-21 15:14:43 +09:00
. split ( '' )
. reversed
. join ();
}
2022-12-21 16:24:01 +09:00
/// from local options
ServerConfig . fromOptions ( Map < String , dynamic > options )
: idServer = options [ 'custom-rendezvous-server' ] ?? "" ,
relayServer = options [ 'relay-server' ] ?? "" ,
apiServer = options [ 'api-server' ] ?? "" ,
key = options [ 'key' ] ?? "" ;
2022-12-21 15:14:43 +09:00
}
2023-01-15 19:46:16 +08:00
Widget dialogButton ( String text ,
{ required VoidCallback ? onPressed ,
bool isOutline = false ,
2023-03-01 14:50:50 +01:00
Widget ? icon ,
2023-01-30 17:56:35 +08:00
TextStyle ? style ,
ButtonStyle ? buttonStyle }) {
2024-03-28 11:38:11 +08:00
if ( isDesktop || isWebDesktop ) {
2023-01-15 19:46:16 +08:00
if ( isOutline ) {
2023-03-01 14:50:50 +01:00
return icon == null
? OutlinedButton (
onPressed: onPressed ,
child: Text ( translate ( text ), style: style ),
)
: OutlinedButton . icon (
icon: icon ,
onPressed: onPressed ,
label: Text ( translate ( text ), style: style ),
);
2023-01-15 19:46:16 +08:00
} else {
2023-03-01 14:50:50 +01:00
return icon == null
? ElevatedButton (
style: ElevatedButton . styleFrom ( elevation: 0 ). merge ( buttonStyle ),
onPressed: onPressed ,
child: Text ( translate ( text ), style: style ),
)
: ElevatedButton . icon (
icon: icon ,
style: ElevatedButton . styleFrom ( elevation: 0 ). merge ( buttonStyle ),
onPressed: onPressed ,
label: Text ( translate ( text ), style: style ),
);
2023-01-15 19:46:16 +08:00
}
} else {
return TextButton (
2023-03-01 14:50:50 +01:00
onPressed: onPressed ,
child: Text (
translate ( text ),
style: style ,
),
);
2023-01-15 19:46:16 +08:00
}
}
2023-01-17 13:28:33 +08:00
2023-10-08 21:44:54 +08:00
int versionCmp ( String v1 , String v2 ) {
2023-01-19 21:21:28 +08:00
return bind . versionToNumber ( v: v1 ) - bind . versionToNumber ( v: v2 );
2023-01-17 13:28:33 +08:00
}
2023-01-23 22:07:50 +08:00
String getWindowName ({ WindowType ? overrideType }) {
2024-02-25 13:29:06 +08:00
final name = bind . mainGetAppNameSync ();
2023-01-23 22:07:50 +08:00
switch ( overrideType ?? kWindowType ) {
case WindowType . Main:
2024-02-25 13:29:06 +08:00
return name ;
2023-01-23 22:07:50 +08:00
case WindowType . FileTransfer:
2024-02-25 13:29:06 +08:00
return "File Transfer - $ name " ;
2025-03-10 21:06:53 +08:00
case WindowType . ViewCamera:
return "View Camera - $ name " ;
2023-01-23 22:07:50 +08:00
case WindowType . PortForward:
2024-02-25 13:29:06 +08:00
return "Port Forward - $ name " ;
2023-01-23 22:07:50 +08:00
case WindowType . RemoteDesktop:
2024-02-25 13:29:06 +08:00
return "Remote Desktop - $ name " ;
2023-01-23 22:07:50 +08:00
default :
break ;
}
2024-02-25 13:29:06 +08:00
return name ;
2023-01-23 22:07:50 +08:00
}
String getWindowNameWithId ( String id , { WindowType ? overrideType }) {
2023-09-26 15:11:31 +08:00
return " ${ DesktopTab . tablabelGetter ( id ). value } - ${ getWindowName ( overrideType: overrideType ) } " ;
2023-01-23 22:07:50 +08:00
}
2023-02-02 13:57:20 +08:00
2023-02-02 14:03:50 +08:00
Future < void > updateSystemWindowTheme () async {
// Set system window theme for macOS.
2023-02-02 13:57:20 +08:00
final userPreference = MyTheme . getThemeModePreference ();
if ( userPreference != ThemeMode . system ) {
2024-03-23 10:08:55 +08:00
if ( isMacOS ) {
2023-02-02 14:03:50 +08:00
await RdPlatformChannel . instance . changeSystemWindowTheme (
2023-02-02 13:57:20 +08:00
userPreference == ThemeMode . light
? SystemWindowTheme . light
: SystemWindowTheme . dark );
}
}
2023-02-08 22:01:15 +09:00
}
2023-02-13 16:18:46 +08:00
2023-01-31 13:32:10 +08:00
/// macOS only
///
/// Note: not found a general solution for rust based AVFoundation bingding.
/// [AVFoundation] crate has compile error.
2025-10-30 06:54:11 -05:00
const kMacOSPermChannel = MethodChannel ( "org.rustdesk.rustdesk/host" );
2023-01-31 13:32:10 +08:00
enum PermissionAuthorizeType {
undetermined ,
authorized ,
denied , // and restricted
}
Future < PermissionAuthorizeType > osxCanRecordAudio () async {
int res = await kMacOSPermChannel . invokeMethod ( "canRecordAudio" );
print ( res );
if ( res > 0 ) {
return PermissionAuthorizeType . authorized ;
} else if ( res == 0 ) {
return PermissionAuthorizeType . undetermined ;
} else {
return PermissionAuthorizeType . denied ;
}
}
Future < bool > osxRequestAudio () async {
return await kMacOSPermChannel . invokeMethod ( "requestRecordAudio" );
}
2023-02-13 16:18:46 +08:00
2023-02-23 14:30:29 +08:00
Widget futureBuilder (
{ required Future ? future , required Widget Function ( dynamic data ) hasData }) {
return FutureBuilder (
future: future ,
builder: ( BuildContext context , AsyncSnapshot snapshot ) {
if ( snapshot . hasData ) {
return hasData ( snapshot . data ! );
} else {
if ( snapshot . hasError ) {
debugPrint ( snapshot . error . toString ());
}
return Container ();
}
});
}
2023-04-19 14:39:22 +08:00
void onCopyFingerprint ( String value ) {
if ( value . isNotEmpty ) {
Clipboard . setData ( ClipboardData ( text: value ));
showToast ( ' $ value \n ${ translate ( "Copied" ) } ' );
} else {
showToast ( translate ( "no fingerprints" ));
}
}
2023-06-06 21:51:40 +08:00
2026-07-27 23:24:32 +08:00
void onCopyId ( String value ) {
if ( value . isNotEmpty ) {
Clipboard . setData ( ClipboardData ( text: value ));
showToast ( ' $ value \n ${ translate ( "Copied" ) } ' );
} else {
showToast ( translate ( "Invalid ID" ));
}
}
2023-08-13 17:50:19 +08:00
Future < bool > callMainCheckSuperUserPermission () async {
bool checked = await bind . mainCheckSuperUserPermission ();
2024-03-23 10:08:55 +08:00
if ( isMacOS ) {
2023-08-13 17:50:19 +08:00
await windowManager . show ();
}
return checked ;
}
2023-06-06 21:51:40 +08:00
Future < void > start_service ( bool is_start ) async {
bool checked = ! bind . mainIsInstalled () ||
2024-03-23 10:08:55 +08:00
! isMacOS ||
2023-08-13 17:50:19 +08:00
await callMainCheckSuperUserPermission ();
2023-06-06 21:51:40 +08:00
if ( checked ) {
2024-06-10 19:53:02 +08:00
mainSetBoolOption ( kOptionStopService , ! is_start );
2023-06-06 21:51:40 +08:00
}
}
2023-07-05 17:35:37 +08:00
2024-06-18 14:39:56 +08:00
Future < bool > canBeBlocked () async {
2026-02-24 21:12:06 +08:00
if ( isWeb ) {
// Web can only act as a controller, never as a controlled side,
// so it should never be blocked by a remote session.
return false ;
}
2026-01-09 00:21:28 +08:00
// First check control permission
final controlPermission = await bind . mainGetCommon (
key: "is-remote-modify-enabled-by-control-permissions" );
if ( controlPermission == "true" ) {
return false ;
} else if ( controlPermission == "false" ) {
return true ;
}
// Check local settings
var accessMode = await bind . mainGetOption ( key: kOptionAccessMode );
var isCustomAccessMode = accessMode != 'full' && accessMode != 'view' ;
2024-06-18 14:39:56 +08:00
var option = option2bool ( kOptionAllowRemoteConfigModification ,
await bind . mainGetOption ( key: kOptionAllowRemoteConfigModification ));
2026-01-09 00:21:28 +08:00
return accessMode == 'view' || ( isCustomAccessMode && ! option );
2024-06-18 14:39:56 +08:00
}
2025-05-15 09:06:37 +08:00
// to-do: web not implemented
2024-06-18 14:39:56 +08:00
Future < void > shouldBeBlocked ( RxBool block , WhetherUseRemoteBlock ? use ) async {
if ( use != null && ! await use ()) {
block . value = false ;
return ;
}
var time0 = DateTime . now (). millisecondsSinceEpoch ;
await bind . mainCheckMouseTime ();
Timer ( const Duration ( milliseconds: 120 ), () async {
var d = time0 - await bind . mainGetMouseTime ();
if ( d < 120 ) {
block . value = true ;
2024-06-18 16:30:56 +08:00
} else {
block . value = false ;
2024-06-18 14:39:56 +08:00
}
});
}
2024-02-12 21:39:19 +08:00
typedef WhetherUseRemoteBlock = Future < bool > Function ();
2024-07-08 20:08:05 +08:00
Widget buildRemoteBlock (
{ required Widget child ,
required RxBool block ,
required bool mask ,
WhetherUseRemoteBlock ? use }) {
2023-07-05 17:35:37 +08:00
return Obx (() => MouseRegion (
onEnter: ( _ ) async {
2024-06-18 14:39:56 +08:00
await shouldBeBlocked ( block , use );
2023-07-05 17:35:37 +08:00
},
onExit: ( event ) => block . value = false ,
child: Stack ( children: [
2024-07-08 20:08:05 +08:00
// scope block tab
2024-12-08 18:26:55 +08:00
preventMouseKeyBuilder ( child: child , block: block . value ),
2024-07-08 20:08:05 +08:00
// mask block click, cm not block click and still use check_click_time to avoid block local click
if ( mask )
Offstage (
offstage: ! block . value ,
child: Container (
color: Colors . black . withOpacity ( 0.5 ),
)),
2023-07-05 17:35:37 +08:00
]),
));
}
2023-07-06 09:40:03 +08:00
2024-12-08 18:26:55 +08:00
Widget preventMouseKeyBuilder ({ required Widget child , required bool block }) {
return ExcludeFocus (
excluding: block , child: AbsorbPointer ( child: child , absorbing: block ));
}
2023-07-10 09:40:41 +08:00
Widget unreadMessageCountBuilder ( RxInt ? count ,
2023-07-10 21:03:35 +08:00
{ double ? size , double ? fontSize }) {
2023-07-06 09:40:03 +08:00
return Obx (() => Offstage (
offstage: ! (( count ? . value ?? 0 ) > 0 ),
child: Container (
2023-07-10 09:40:41 +08:00
width: size ?? 16 ,
height: size ?? 16 ,
2023-07-06 09:40:03 +08:00
decoration: BoxDecoration (
color: Colors . red ,
shape: BoxShape . circle ,
),
child: Center (
child: Text ( " ${ count ? . value ?? 0 } " ,
2023-07-10 09:40:41 +08:00
maxLines: 1 ,
style: TextStyle ( color: Colors . white , fontSize: fontSize ?? 10 )),
2023-07-06 09:40:03 +08:00
),
2023-07-10 21:03:35 +08:00
)));
}
2023-07-11 10:05:31 +08:00
Widget unreadTopRightBuilder ( RxInt ? count , { Widget ? icon }) {
2023-07-10 21:03:35 +08:00
return Stack (
children: [
icon ?? Icon ( Icons . chat ),
Positioned (
top: 0 ,
right: 0 ,
child: unreadMessageCountBuilder ( count , size: 12 , fontSize: 8 ))
],
);
2023-07-06 09:40:03 +08:00
}
2023-07-20 08:05:38 +08:00
String toCapitalized ( String s ) {
if ( s . isEmpty ) {
return s ;
}
return s . substring ( 0 , 1 ). toUpperCase () + s . substring ( 1 );
}
2023-09-14 10:17:03 +08:00
Widget buildErrorBanner ( BuildContext context ,
{ required RxBool loading ,
required RxString err ,
required Function ? retry ,
required Function close }) {
return Obx (() => Offstage (
offstage: ! ( ! loading . value && err . value . isNotEmpty ),
child: Center (
child: Container (
color: MyTheme . color ( context ). errorBannerBg ,
child: Row (
mainAxisAlignment: MainAxisAlignment . center ,
crossAxisAlignment: CrossAxisAlignment . center ,
children: [
FittedBox (
child: Icon (
Icons . info ,
color: Color . fromARGB ( 255 , 249 , 81 , 81 ),
),
). marginAll ( 4 ),
Flexible (
child: Align (
alignment: Alignment . centerLeft ,
child: Tooltip (
message: translate ( err . value ),
2024-07-28 11:13:19 +08:00
child: SelectableText (
2023-09-14 10:17:03 +08:00
translate ( err . value ),
),
)). marginSymmetric ( vertical: 2 ),
),
if ( retry != null )
InkWell (
onTap: () {
retry . call ();
},
child: Text (
translate ( "Retry" ),
style: TextStyle ( color: MyTheme . accent ),
)). marginSymmetric ( horizontal: 5 ),
FittedBox (
child: InkWell (
onTap: () {
close . call ();
},
child: Icon ( Icons . close ). marginSymmetric ( horizontal: 5 ),
),
). marginAll ( 4 )
],
),
)). marginOnly ( bottom: 14 ),
));
}
2023-09-26 15:11:31 +08:00
String getDesktopTabLabel ( String peerId , String alias ) {
String label = alias . isEmpty ? peerId : alias ;
try {
String peer = bind . mainGetPeerSync ( id: peerId );
Map < String , dynamic > config = jsonDecode ( peer );
if ( config [ 'info' ][ 'hostname' ] is String ) {
String hostname = config [ 'info' ][ 'hostname' ];
if ( hostname . isNotEmpty &&
! label . toLowerCase (). contains ( hostname . toLowerCase ())) {
label += "@ $ hostname " ;
}
}
} catch ( e ) {
debugPrint ( "Failed to get hostname: $ e " );
}
return label ;
}
2023-10-08 21:44:54 +08:00
sessionRefreshVideo ( SessionID sessionId , PeerInfo pi ) async {
if ( pi . currentDisplay == kAllDisplayValue ) {
for ( int i = 0 ; i < pi . displays . length ; i ++ ) {
await bind . sessionRefresh ( sessionId: sessionId , display: i );
}
} else {
await bind . sessionRefresh ( sessionId: sessionId , display: pi . currentDisplay );
}
}
2023-10-17 00:30:34 +08:00
Future < List < Rect >> getScreenListWayland () async {
final screenRectList = < Rect > [];
if ( isMainDesktopWindow ) {
for ( var screen in await window_size . getScreenList ()) {
final scale = kIgnoreDpi ? 1.0 : screen . scaleFactor ;
double l = screen . frame . left ;
double t = screen . frame . top ;
double r = screen . frame . right ;
double b = screen . frame . bottom ;
final rect = Rect . fromLTRB ( l / scale , t / scale , r / scale , b / scale );
screenRectList . add ( rect );
}
} else {
final screenList = await rustDeskWinManager . call (
WindowType . Main , kWindowGetScreenList , '' );
try {
for ( var screen in jsonDecode ( screenList . result ) as List < dynamic > ) {
final scale = kIgnoreDpi ? 1.0 : screen [ 'scaleFactor' ];
double l = screen [ 'frame' ][ 'l' ];
double t = screen [ 'frame' ][ 't' ];
double r = screen [ 'frame' ][ 'r' ];
double b = screen [ 'frame' ][ 'b' ];
final rect = Rect . fromLTRB ( l / scale , t / scale , r / scale , b / scale );
screenRectList . add ( rect );
}
} catch ( e ) {
debugPrint ( 'Failed to parse screenList: $ e ' );
}
}
return screenRectList ;
}
Future < List < Rect >> getScreenListNotWayland () async {
final screenRectList = < Rect > [];
final displays = bind . mainGetDisplays ();
if ( displays . isEmpty ) {
return screenRectList ;
}
try {
for ( var display in jsonDecode ( displays ) as List < dynamic > ) {
// to-do: scale factor ?
// final scale = kIgnoreDpi ? 1.0 : screen.scaleFactor;
double l = display [ 'x' ]. toDouble ();
double t = display [ 'y' ]. toDouble ();
double r = ( display [ 'x' ] + display [ 'w' ]). toDouble ();
double b = ( display [ 'y' ] + display [ 'h' ]). toDouble ();
screenRectList . add ( Rect . fromLTRB ( l , t , r , b ));
}
} catch ( e ) {
debugPrint ( 'Failed to parse displays: $ e ' );
}
return screenRectList ;
}
Future < List < Rect >> getScreenRectList () async {
return bind . mainCurrentIsWayland ()
? await getScreenListWayland ()
: await getScreenListNotWayland ();
}
2023-12-03 09:56:47 -05:00
openMonitorInTheSameTab ( int i , FFI ffi , PeerInfo pi ,
2026-07-05 09:32:47 -05:00
{ bool updateCursorPos = true , bool recordSelection = true }) {
if ( recordSelection ) {
ffi . ffiModel . lastUserDisplay = i ;
ffi . ffiModel . cancelPendingRestoreTimer ();
ffi . ffiModel . pendingMonitorRestore = null ;
}
2023-10-17 00:30:34 +08:00
final displays = i == kAllDisplayValue
? List . generate ( pi . displays . length , ( index ) => index )
: [ i ];
2024-07-24 17:35:06 +08:00
// Try clear image model before switching from all displays
// 1. The remote side has multiple displays.
// 2. Do not use texture render.
// 3. Connect to Display 1.
// 4. Switch to multi-displays `kAllDisplayValue`
// 5. Switch to Display 2.
// Then the remote page will display last picture of Display 1 at the beginning.
if ( pi . forceTextureRender && i != kAllDisplayValue ) {
ffi . imageModel . clearImage ();
}
2023-10-17 00:30:34 +08:00
bind . sessionSwitchDisplay (
2023-11-30 16:09:37 +08:00
isDesktop: isDesktop ,
sessionId: ffi . sessionId ,
value: Int32List . fromList ( displays ),
);
2023-12-03 09:56:47 -05:00
ffi . ffiModel . switchToNewDisplay ( i , ffi . sessionId , ffi . id ,
updateCursorPos: updateCursorPos );
2023-10-17 00:30:34 +08:00
}
// Open new tab or window to show this monitor.
// For now just open new window.
//
// screenRect is used to move the new window to the specified screen and set fullscreen.
openMonitorInNewTabOrWindow ( int i , String peerId , PeerInfo pi ,
{ Rect ? screenRect }) {
final args = {
'window_id' : stateGlobal . windowId ,
'peer_id' : peerId ,
'display' : i ,
'display_count' : pi . displays . length ,
2025-03-10 21:06:53 +08:00
'window_type' : ( kWindowType ?? WindowType . RemoteDesktop ). index ,
2023-10-17 00:30:34 +08:00
};
if ( screenRect != null ) {
args [ 'screen_rect' ] = {
'l' : screenRect . left ,
't' : screenRect . top ,
'r' : screenRect . right ,
'b' : screenRect . bottom ,
};
}
DesktopMultiWindow . invokeMethod (
kMainWindowId , kWindowEventOpenMonitorSession , jsonEncode ( args ));
}
2024-07-25 21:52:57 +08:00
setNewConnectWindowFrame ( int windowId , String peerId , int preSessionCount ,
2025-03-10 21:06:53 +08:00
WindowType windowType , int ? display , Rect ? screenRect ) async {
2024-05-23 09:51:19 +08:00
if ( screenRect == null ) {
2024-07-25 21:52:57 +08:00
// Do not restore window position to new connection if there's a pre-session.
// https://github.com/rustdesk/rustdesk/discussions/8825
if ( preSessionCount == 0 ) {
2025-03-10 21:06:53 +08:00
await restoreWindowPosition ( windowType ,
2024-07-25 21:52:57 +08:00
windowId: windowId , display: display , peerId: peerId );
}
2024-05-23 09:51:19 +08:00
} else {
2024-05-23 22:11:40 +08:00
await tryMoveToScreenAndSetFullscreen ( screenRect );
2024-05-23 09:51:19 +08:00
}
}
2023-10-17 00:30:34 +08:00
tryMoveToScreenAndSetFullscreen ( Rect ? screenRect ) async {
if ( screenRect == null ) {
return ;
}
2023-10-17 07:43:12 +08:00
final wc = WindowController . fromWindowId ( stateGlobal . windowId );
final curFrame = await wc . getFrame ();
2023-10-17 00:30:34 +08:00
final frame =
Rect . fromLTWH ( screenRect . left + 30 , screenRect . top + 30 , 600 , 400 );
2023-10-17 14:29:14 +08:00
if ( stateGlobal . fullscreen . isTrue &&
2023-10-17 07:43:12 +08:00
curFrame . left <= frame . left &&
curFrame . top <= frame . top &&
curFrame . width >= frame . width &&
curFrame . height >= frame . height ) {
return ;
}
await wc . setFrame ( frame );
2023-10-17 00:30:34 +08:00
// An duration is needed to avoid the window being restored after fullscreen.
Future . delayed ( Duration ( milliseconds: 300 ), () async {
stateGlobal . setFullscreen ( true );
});
}
2023-10-17 09:36:08 +08:00
parseParamScreenRect ( Map < String , dynamic > params ) {
Rect ? screenRect ;
if ( params [ 'screen_rect' ] != null ) {
double l = params [ 'screen_rect' ][ 'l' ];
double t = params [ 'screen_rect' ][ 't' ];
double r = params [ 'screen_rect' ][ 'r' ];
double b = params [ 'screen_rect' ][ 'b' ];
screenRect = Rect . fromLTRB ( l , t , r , b );
}
return screenRect ;
}
2023-10-21 15:25:01 +08:00
2023-11-28 23:57:48 +08:00
get isInputSourceFlutter => stateGlobal . getInputSource () == "Input source 2" ;
2025-05-04 07:32:47 +08:00
class _CountDownButton extends StatefulWidget {
_CountDownButton ({
2023-10-21 15:25:01 +08:00
Key ? key ,
2025-05-04 07:32:47 +08:00
required this . text ,
2023-10-21 15:25:01 +08:00
required this . second ,
required this . onPressed ,
2025-05-04 07:32:47 +08:00
this . submitOnTimeout = false ,
2023-10-21 15:25:01 +08:00
}) : super ( key: key );
2025-05-04 07:32:47 +08:00
final String text ;
2023-10-21 15:25:01 +08:00
final VoidCallback ? onPressed ;
final int second ;
2025-05-04 07:32:47 +08:00
final bool submitOnTimeout ;
2023-10-21 15:25:01 +08:00
@ override
2025-05-04 07:32:47 +08:00
State < _CountDownButton > createState () => _CountDownButtonState ();
2023-10-21 15:25:01 +08:00
}
2025-05-04 07:32:47 +08:00
class _CountDownButtonState extends State < _CountDownButton > {
2023-10-21 15:25:01 +08:00
late int _countdownSeconds = widget . second ;
Timer ? _timer ;
@ override
void initState () {
super . initState ();
_startCountdownTimer ();
}
@ override
void dispose () {
_timer ? . cancel ();
super . dispose ();
}
void _startCountdownTimer () {
_timer = Timer . periodic ( Duration ( seconds: 1 ), ( timer ) {
if ( _countdownSeconds <= 0 ) {
timer . cancel ();
2025-05-04 07:32:47 +08:00
if ( widget . submitOnTimeout ) {
widget . onPressed ? . call ();
}
2023-10-21 15:25:01 +08:00
} else {
setState (() {
_countdownSeconds -- ;
});
}
});
}
@ override
Widget build ( BuildContext context ) {
return dialogButton (
2025-05-04 07:32:47 +08:00
' ${ translate ( widget . text ) } ( ${ _countdownSeconds } s)' ,
2023-10-21 15:25:01 +08:00
onPressed: widget . onPressed ,
isOutline: true ,
);
}
}
2023-12-03 20:31:48 +08:00
2023-12-03 09:56:47 -05:00
importConfig ( List < TextEditingController >? controllers , List < RxString >? errMsgs ,
String ? text ) {
2024-09-13 09:21:50 +08:00
text = text ? . trim ();
2023-12-03 09:56:47 -05:00
if ( text != null && text . isNotEmpty ) {
try {
final sc = ServerConfig . decode ( text );
2024-10-18 08:45:16 +08:00
if ( isWeb || isIOS ) {
sc . relayServer = '' ;
}
2023-12-03 09:56:47 -05:00
if ( sc . idServer . isNotEmpty ) {
Future < bool > success = setServerConfig ( controllers , errMsgs , sc );
success . then (( value ) {
if ( value ) {
showToast ( translate ( 'Import server configuration successfully' ));
2023-12-03 20:31:48 +08:00
} else {
showToast ( translate ( 'Invalid server configuration' ));
}
2023-12-03 09:56:47 -05:00
});
2023-12-03 20:31:48 +08:00
} else {
2023-12-03 09:56:47 -05:00
showToast ( translate ( 'Invalid server configuration' ));
2023-12-03 20:31:48 +08:00
}
2023-12-03 09:56:47 -05:00
return sc ;
} catch ( e ) {
showToast ( translate ( 'Invalid server configuration' ));
}
} else {
showToast ( translate ( 'Clipboard is empty' ));
}
2023-12-03 20:31:48 +08:00
}
Future < bool > setServerConfig (
List < TextEditingController >? controllers ,
List < RxString >? errMsgs ,
ServerConfig config ,
) async {
2024-07-22 17:11:32 +08:00
String removeEndSlash ( String input ) {
if ( input . endsWith ( '/' )) {
return input . substring ( 0 , input . length - 1 );
}
return input ;
}
config . idServer = removeEndSlash ( config . idServer . trim ());
config . relayServer = removeEndSlash ( config . relayServer . trim ());
config . apiServer = removeEndSlash ( config . apiServer . trim ());
2023-12-03 20:31:48 +08:00
config . key = config . key . trim ();
if ( controllers != null ) {
controllers [ 0 ]. text = config . idServer ;
controllers [ 1 ]. text = config . relayServer ;
controllers [ 2 ]. text = config . apiServer ;
controllers [ 3 ]. text = config . key ;
2023-12-03 09:56:47 -05:00
}
2023-12-03 20:31:48 +08:00
// id
if ( config . idServer . isNotEmpty && errMsgs != null ) {
2024-04-28 14:22:21 +08:00
errMsgs [ 0 ]. value = translate ( await bind . mainTestIfValidServer (
server: config . idServer , testWithProxy: true ));
2023-12-03 20:31:48 +08:00
if ( errMsgs [ 0 ]. isNotEmpty ) {
return false ;
}
}
// relay
if ( config . relayServer . isNotEmpty && errMsgs != null ) {
2024-04-28 14:22:21 +08:00
errMsgs [ 1 ]. value = translate ( await bind . mainTestIfValidServer (
server: config . relayServer , testWithProxy: true ));
2023-12-03 20:31:48 +08:00
if ( errMsgs [ 1 ]. isNotEmpty ) {
return false ;
}
}
// api
if ( config . apiServer . isNotEmpty && errMsgs != null ) {
if ( ! config . apiServer . startsWith ( 'http://' ) &&
! config . apiServer . startsWith ( 'https://' )) {
errMsgs [ 2 ]. value =
' ${ translate ( "API Server" ) } : ${ translate ( "invalid_http" ) } ' ;
return false ;
}
}
final oldApiServer = await bind . mainGetApiServer ();
// should set one by one
await bind . mainSetOption (
key: 'custom-rendezvous-server' , value: config . idServer );
await bind . mainSetOption ( key: 'relay-server' , value: config . relayServer );
await bind . mainSetOption ( key: 'api-server' , value: config . apiServer );
await bind . mainSetOption ( key: 'key' , value: config . key );
final newApiServer = await bind . mainGetApiServer ();
if ( oldApiServer . isNotEmpty &&
oldApiServer != newApiServer &&
gFFI . userModel . isLogin ) {
gFFI . userModel . logOut ( apiServer: oldApiServer );
}
return true ;
2023-12-03 09:56:47 -05:00
}
2024-02-12 21:39:19 +08:00
ColorFilter ? svgColor ( Color ? color ) {
if ( color == null ) {
return null ;
} else {
return ColorFilter . mode ( color , BlendMode . srcIn );
}
}
2024-02-18 22:08:25 +08:00
// ignore: must_be_immutable
class ComboBox extends StatelessWidget {
late final List < String > keys ;
late final List < String > values ;
late final String initialKey ;
late final Function ( String key ) onChanged ;
late final bool enabled ;
late String current ;
ComboBox ({
Key ? key ,
required this . keys ,
required this . values ,
required this . initialKey ,
required this . onChanged ,
this . enabled = true ,
}) : super ( key: key );
@ override
Widget build ( BuildContext context ) {
var index = keys . indexOf ( initialKey );
if ( index < 0 ) {
index = 0 ;
}
var ref = values [ index ]. obs ;
current = keys [ index ];
return Container (
decoration: BoxDecoration (
border: Border . all (
color: enabled
? MyTheme . color ( context ). border2 ?? MyTheme . border
: MyTheme . border ,
),
borderRadius:
BorderRadius . circular ( 8 ), //border raiuds of dropdown button
),
height: 42 , // should be the height of a TextField
child: Obx (() => DropdownButton < String > (
isExpanded: true ,
value: ref . value ,
elevation: 16 ,
underline: Container (),
style: TextStyle (
color: enabled
? Theme . of ( context ). textTheme . titleMedium ? . color
: disabledTextColor ( context , enabled )),
icon: const Icon (
Icons . expand_more_sharp ,
size: 20 ,
). marginOnly ( right: 15 ),
onChanged: enabled
? ( String ? newValue ) {
if ( newValue != null && newValue != ref . value ) {
ref . value = newValue ;
current = newValue ;
onChanged ( keys [ values . indexOf ( newValue )]);
}
}
: null ,
items: values . map < DropdownMenuItem < String >> (( String value ) {
return DropdownMenuItem < String > (
value: value ,
child: Text (
value ,
style: const TextStyle ( fontSize: 15 ),
overflow: TextOverflow . ellipsis ,
). marginOnly ( left: 15 ),
);
}). toList (),
)),
). marginOnly ( bottom: 5 );
}
}
Color ? disabledTextColor ( BuildContext context , bool enabled ) {
return enabled
? null
: Theme . of ( context ). textTheme . titleLarge ? . color ? . withOpacity ( 0.6 );
}
2024-02-26 17:49:12 +08:00
2024-04-17 12:48:27 +08:00
Widget loadPowered ( BuildContext context ) {
2025-06-10 22:01:15 +08:00
if ( bind . mainGetBuildinOption ( key: "hide-powered-by-me" ) == 'Y' ) {
return SizedBox . shrink ();
}
2024-04-17 12:48:27 +08:00
return MouseRegion (
cursor: SystemMouseCursors . click ,
child: GestureDetector (
onTap: () {
launchUrl ( Uri . parse ( 'https://rustdesk.com' ));
},
child: Opacity (
opacity: 0.5 ,
child: Text (
translate ( "powered_by_me" ),
overflow: TextOverflow . clip ,
style: Theme . of ( context )
. textTheme
. bodySmall
? . copyWith ( fontSize: 9 , decoration: TextDecoration . underline ),
)),
),
). marginOnly ( top: 6 );
}
2026-06-14 13:47:39 +08:00
const _kDefaultLogoAsset = 'assets/logo.png' ;
const _kLightLogoAsset = 'assets/logo_light.png' ;
const _kDarkLogoAsset = 'assets/logo_dark.png' ;
List < String > _logoAssetCandidatesForBrightness ( Brightness brightness ) {
return brightness == Brightness . dark
? [ _kDarkLogoAsset , _kDefaultLogoAsset ]
: [ _kLightLogoAsset , _kDefaultLogoAsset ];
}
Future < String ?> _resolveLogoAsset ( Brightness brightness ) async {
for ( final asset in _logoAssetCandidatesForBrightness ( brightness )) {
try {
await rootBundle . load ( asset );
return asset ;
} on FlutterError {
continue ;
}
}
return null ;
}
class _Logo extends StatefulWidget {
const _Logo ();
@ override
State < _Logo > createState () => _LogoState ();
}
class _LogoState extends State < _Logo > {
final Map < Brightness , Future < String ?>> _logoFutures = {};
Future < String ?> _logoFutureFor ( Brightness brightness ) {
return _logoFutures . putIfAbsent (
brightness ,
() => _resolveLogoAsset ( brightness ),
);
}
@ override
Widget build ( BuildContext context ) {
return FutureBuilder < String ?> (
future: _logoFutureFor ( Theme . of ( context ). brightness ),
builder: ( BuildContext context , AsyncSnapshot < String ?> snapshot ) {
final asset = snapshot . data ;
if ( asset != null ) {
2024-03-16 21:55:03 +08:00
final image = Image . asset (
2026-06-14 13:47:39 +08:00
asset ,
2024-03-16 21:55:03 +08:00
fit: BoxFit . contain ,
errorBuilder: ( ctx , error , stackTrace ) {
return Container ();
},
);
return Container (
constraints: BoxConstraints ( maxWidth: 300 , maxHeight: 60 ),
child: image ,
). marginOnly ( left: 12 , right: 12 , top: 12 );
}
return const Offstage ();
2026-06-14 13:47:39 +08:00
},
);
}
2024-03-14 11:36:14 +08:00
}
2026-06-14 13:47:39 +08:00
// max 300 x 60
Widget loadLogo () => const _Logo ();
2024-03-14 11:36:14 +08:00
Widget loadIcon ( double size ) {
return Image . asset ( 'assets/icon.png' ,
2024-02-26 17:49:12 +08:00
width: size ,
height: size ,
errorBuilder: ( ctx , error , stackTrace ) => SvgPicture . asset (
2024-03-14 11:36:14 +08:00
'assets/icon.svg' ,
2024-02-26 17:49:12 +08:00
width: size ,
height: size ,
));
}
2024-02-27 17:02:10 +08:00
2024-03-14 11:36:14 +08:00
var imcomingOnlyHomeSize = Size ( 280 , 300 );
Size getIncomingOnlyHomeSize () {
2024-03-23 10:08:55 +08:00
final magicWidth = isWindows ? 11.0 : 2.0 ;
2024-03-16 15:37:23 +08:00
final magicHeight = 10.0 ;
2024-03-14 11:36:14 +08:00
return imcomingOnlyHomeSize +
2024-02-27 17:02:10 +08:00
Offset ( magicWidth , kDesktopRemoteTabBarHeight + magicHeight );
}
2024-03-14 11:36:14 +08:00
Size getIncomingOnlySettingsSize () {
2024-02-27 17:02:10 +08:00
return Size ( 768 , 600 );
}
2024-03-16 17:33:58 +08:00
bool isInHomePage () {
final controller = Get . find < DesktopTabController > ();
return controller . state . value . selected == 0 ;
}
2024-04-17 22:01:09 +08:00
2024-07-25 00:13:22 +08:00
Widget _buildPresetPasswordWarning () {
if ( bind . mainGetBuildinOption ( key: kOptionRemovePresetPasswordWarning ) !=
'N' ) {
return SizedBox . shrink ();
}
return Container (
color: Colors . yellow ,
child: Column (
children: [
Align (
child: Text (
translate ( "Security Alert" ),
style: TextStyle (
color: Colors . red ,
fontSize:
18 , // https://github.com/rustdesk/rustdesk-server-pro/issues/261
fontWeight: FontWeight . bold ,
),
)). paddingOnly ( bottom: 8 ),
Text (
translate ( "preset_password_warning" ),
style: TextStyle ( color: Colors . red ),
)
],
). paddingAll ( 8 ),
); // Show a warning message if the Future completed with true
}
Widget buildPresetPasswordWarningMobile () {
if ( bind . isPresetPasswordMobileOnly ()) {
return _buildPresetPasswordWarning ();
} else {
return SizedBox . shrink ();
}
}
2024-04-17 22:01:09 +08:00
Widget buildPresetPasswordWarning () {
return FutureBuilder < bool > (
future: bind . isPresetPassword (),
builder: ( BuildContext context , AsyncSnapshot < bool > snapshot ) {
if ( snapshot . connectionState == ConnectionState . waiting ) {
return CircularProgressIndicator (); // Show a loading spinner while waiting for the Future to complete
} else if ( snapshot . hasError ) {
return Text (
'Error: ${ snapshot . error } ' ); // Show an error message if the Future completed with an error
} else if ( snapshot . hasData && snapshot . data == true ) {
2024-07-25 00:13:22 +08:00
return _buildPresetPasswordWarning ();
2024-04-17 22:01:09 +08:00
} else {
return SizedBox
. shrink (); // Show nothing if the Future completed with false or null
}
},
);
}
2024-05-08 09:59:05 +08:00
// https://github.com/leanflutter/window_manager/blob/87dd7a50b4cb47a375b9fc697f05e56eea0a2ab3/lib/src/widgets/virtual_window_frame.dart#L44
Widget buildVirtualWindowFrame ( BuildContext context , Widget child ) {
boxShadow () => isMainDesktopWindow
? < BoxShadow > [
if ( stateGlobal . fullscreen . isFalse || stateGlobal . isMaximized . isFalse )
BoxShadow (
color: Colors . black . withOpacity ( 0.1 ),
offset: Offset (
0.0 ,
stateGlobal . isFocused . isTrue
? kFrameBoxShadowOffsetFocused
: kFrameBoxShadowOffsetUnfocused ),
blurRadius: kFrameBoxShadowBlurRadius ,
),
]
: null ;
return Obx (
() => Container (
decoration: BoxDecoration (
2024-05-10 16:40:29 +08:00
color: isMainDesktopWindow
? Colors . transparent
: Theme . of ( context ). colorScheme . background ,
2024-05-08 09:59:05 +08:00
border: Border . all (
color: Theme . of ( context ). dividerColor ,
width: stateGlobal . windowBorderWidth . value ,
),
borderRadius: BorderRadius . circular (
( stateGlobal . fullscreen . isTrue || stateGlobal . isMaximized . isTrue )
? 0
: kFrameBorderRadius ,
),
boxShadow: boxShadow (),
),
child: ClipRRect (
borderRadius: BorderRadius . circular (
( stateGlobal . fullscreen . isTrue || stateGlobal . isMaximized . isTrue )
? 0
: kFrameClipRRectBorderRadius ,
),
child: child ,
),
),
);
}
2024-05-09 22:51:53 +08:00
2024-08-15 23:58:19 +08:00
get windowResizeEdgeSize =>
isLinux && ! _linuxWindowResizable ? 0.0 : kWindowResizeEdgeSize ;
2024-05-10 16:40:29 +08:00
// `windowManager.setResizable(false)` will reset the window size to the default size on Linux and then set unresizable.
// See _linuxWindowResizable for more details.
// So we use `setResizable()` instead of `windowManager.setResizable()`.
//
// We can only call `windowManager.setResizable(false)` if we need the default size on Linux.
setResizable ( bool resizable ) {
if ( isLinux ) {
_linuxWindowResizable = resizable ;
stateGlobal . refreshResizeEdgeSize ();
} else {
windowManager . setResizable ( resizable );
}
}
2024-05-17 14:19:11 +08:00
isOptionFixed ( String key ) => bind . mainIsOptionFixed ( key: key );
2026-01-07 13:51:02 +08:00
bool isChangePermanentPasswordDisabled () =>
bind . mainGetBuildinOption ( key: kOptionDisableChangePermanentPassword ) ==
'Y' ;
bool isChangeIdDisabled () =>
bind . mainGetBuildinOption ( key: kOptionDisableChangeId ) == 'Y' ;
bool isUnlockPinDisabled () =>
bind . mainGetBuildinOption ( key: kOptionDisableUnlockPin ) == 'Y' ;
2024-07-25 10:45:51 +08:00
bool ? _isCustomClient ;
bool get isCustomClient {
_isCustomClient ??= bind . isCustomClient ();
return _isCustomClient ! ;
}
2024-05-17 14:19:11 +08:00
get defaultOptionLang => isCustomClient ? 'default' : '' ;
get defaultOptionTheme => isCustomClient ? 'system' : '' ;
get defaultOptionYes => isCustomClient ? 'Y' : '' ;
get defaultOptionNo => isCustomClient ? 'N' : '' ;
get defaultOptionWhitelist => isCustomClient ? ',' : '' ;
get defaultOptionAccessMode => isCustomClient ? 'custom' : '' ;
get defaultOptionApproveMode => isCustomClient ? 'password-click' : '' ;
2024-06-05 14:52:56 +08:00
2024-07-22 17:00:29 +08:00
bool whitelistNotEmpty () {
// https://rustdesk.com/docs/en/self-host/client-configuration/advanced-settings/#whitelist
final v = bind . mainGetOptionSync ( key: kOptionWhitelist );
return v != '' && v != ',' ;
}
2026-07-27 23:24:32 +08:00
bool idWhitelistNotEmpty () {
final v = bind . mainGetOptionSync ( key: kOptionIdWhitelist );
return v != '' && v != ',' ;
}
2024-06-05 14:52:56 +08:00
// `setMovable()` is only supported on macOS.
//
// On macOS, the window can be dragged by the tab bar by default.
// We need to disable the movable feature to prevent the window from being dragged by the tabs in the tab bar.
//
// When we drag the blank tab bar (not the tab), the window will be dragged normally by adding the `onPanStart` handle.
//
// See the following code for more details:
// https://github.com/rustdesk/rustdesk/blob/ce1dac3b8613596b4d8ae981275f9335489eb935/flutter/lib/desktop/widgets/tabbar_widget.dart#L385
// https://github.com/rustdesk/rustdesk/blob/ce1dac3b8613596b4d8ae981275f9335489eb935/flutter/lib/desktop/widgets/tabbar_widget.dart#L399
//
// @platforms macos
disableWindowMovable ( int ? windowId ) {
if ( ! isMacOS ) {
return ;
}
if ( windowId == null ) {
windowManager . setMovable ( false );
} else {
WindowController . fromWindowId ( windowId ). setMovable ( false );
}
}
2024-07-28 10:15:09 +08:00
Widget netWorkErrorWidget () {
return Center (
child: Column (
mainAxisAlignment: MainAxisAlignment . center ,
crossAxisAlignment: CrossAxisAlignment . center ,
children: [
2026-08-04 10:19:53 +08:00
if ( ! gFFI . userModel . networkErrorFromServer . value )
Text ( translate ( "network_error_tip" )),
2024-07-28 10:15:09 +08:00
ElevatedButton (
onPressed: gFFI . userModel . refreshCurrentUser ,
child: Text ( translate ( "Retry" )))
. marginSymmetric ( vertical: 16 ),
2024-07-28 11:13:19 +08:00
SelectableText ( gFFI . userModel . networkError . value ,
2024-07-28 10:15:09 +08:00
style: TextStyle ( fontSize: 11 , color: Colors . red )),
],
));
}
2024-08-15 23:58:19 +08:00
List < ResizeEdge >? get windowManagerEnableResizeEdges => isWindows
? [
ResizeEdge . topLeft ,
ResizeEdge . top ,
ResizeEdge . topRight ,
]
: null ;
List < SubWindowResizeEdge >? get subWindowManagerEnableResizeEdges => isWindows
? [
SubWindowResizeEdge . topLeft ,
SubWindowResizeEdge . top ,
SubWindowResizeEdge . topRight ,
]
: null ;
2024-10-13 01:07:47 +08:00
void earlyAssert () {
assert ( ' \ 1' == '1' );
}
2024-12-04 17:10:32 +08:00
void checkUpdate () {
2025-01-20 17:33:41 +08:00
if ( ! isWeb ) {
2026-02-28 18:17:26 +08:00
if ( ! bind . isCustomClient ()) {
2024-12-04 17:10:32 +08:00
platformFFI . registerEventHandler (
kCheckSoftwareUpdateFinish , kCheckSoftwareUpdateFinish ,
( Map < String , dynamic > evt ) async {
if ( evt [ 'url' ] is String ) {
stateGlobal . updateUrl . value = evt [ 'url' ];
}
});
Timer ( const Duration ( seconds: 1 ), () async {
bind . mainGetSoftwareUpdateUrl ();
});
}
}
}
2024-12-20 09:24:08 +08:00
// https://github.com/flutter/flutter/issues/153560#issuecomment-2497160535
// For TextField, TextFormField
extension WorkaroundFreezeLinuxMint on Widget {
Widget workaroundFreezeLinuxMint () {
// No need to check if is Linux Mint, because this workaround is harmless on other platforms.
if ( isLinux ) {
return ExcludeSemantics ( child: this );
} else {
return this ;
}
}
}
2025-02-10 13:50:28 +08:00
// Don't use `extension` here, the border looks weird if using `extension` in my test.
Widget workaroundWindowBorder ( BuildContext context , Widget child ) {
if ( ! isWin10 ) {
return child ;
}
final isLight = Theme . of ( context ). brightness == Brightness . light ;
final borderColor = isLight ? Colors . black87 : Colors . grey ;
final width = isLight ? 0.5 : 0.1 ;
getBorderWidget ( Widget child ) {
return Obx (() =>
( stateGlobal . isMaximized . isTrue || stateGlobal . fullscreen . isTrue )
? Offstage ()
: child );
}
final List < Widget > borders = [
getBorderWidget ( Container (
color: borderColor ,
height: width + 0.1 ,
))
];
if ( kWindowType == WindowType . Main && ! isLight ) {
borders . addAll ([
getBorderWidget ( Align (
alignment: Alignment . topLeft ,
child: Container (
color: borderColor ,
width: width ,
),
)),
getBorderWidget ( Align (
alignment: Alignment . topRight ,
child: Container (
color: borderColor ,
width: width ,
),
)),
getBorderWidget ( Align (
alignment: Alignment . bottomCenter ,
child: Container (
color: borderColor ,
height: width ,
),
)),
]);
}
return Stack (
children: [
child ,
... borders ,
],
);
}
2025-02-21 10:41:57 +08:00
2025-02-21 11:00:19 +08:00
void updateTextAndPreserveSelection (
TextEditingController controller , String text ) {
2025-02-21 10:41:57 +08:00
// Only care about select all for now.
2025-02-21 11:00:19 +08:00
final isSelected = controller . selection . isValid &&
controller . selection . end > controller . selection . start ;
2025-02-21 10:41:57 +08:00
// Set text will make the selection invalid.
controller . text = text ;
if ( isSelected ) {
controller . selection = TextSelection (
baseOffset: 0 , extentOffset: controller . value . text . length );
}
}
2025-03-27 15:34:27 +08:00
List < String > getPrinterNames () {
final printerNamesJson = bind . mainGetPrinterNames ();
if ( printerNamesJson . isEmpty ) {
return [];
}
try {
final List < dynamic > printerNamesList = jsonDecode ( printerNamesJson );
final appPrinterName = ' $ appName Printer' ;
return printerNamesList
. map (( e ) => e . toString ())
. where (( name ) => name != appPrinterName )
. toList ();
} catch ( e ) {
debugPrint ( 'failed to parse printer names, err: $ e ' );
return [];
}
}
String _appName = '' ;
String get appName {
if ( _appName . isEmpty ) {
_appName = bind . mainGetAppNameSync ();
}
return _appName ;
}
2025-08-11 16:13:31 +08:00
String getConnectionText ( bool secure , bool direct , String streamType ) {
String connectionText ;
if ( secure && direct ) {
connectionText = translate ( "Direct and encrypted connection" );
} else if ( secure && ! direct ) {
connectionText = translate ( "Relayed and encrypted connection" );
} else if ( ! secure && direct ) {
connectionText = translate ( "Direct and unencrypted connection" );
} else {
connectionText = translate ( "Relayed and unencrypted connection" );
}
if ( streamType == 'Relay' ) {
streamType = 'TCP' ;
}
if ( streamType . isEmpty ) {
return connectionText ;
} else {
return ' $ connectionText ( $ streamType )' ;
}
}
2025-08-20 14:55:52 +08:00
String decode_http_response ( http . Response resp ) {
try {
// https://github.com/rustdesk/rustdesk-server-pro/discussions/758
return utf8 . decode ( resp . bodyBytes , allowMalformed: true );
} catch ( e ) {
debugPrint ( 'Failed to decode response as UTF-8: $ e ' );
// Fallback to bodyString which handles encoding automatically
return resp . body ;
}
}
2025-10-12 14:59:42 +08:00
bool peerTabShowNote ( PeerTabIndex peerTabIndex ) {
return peerTabIndex == PeerTabIndex . ab || peerTabIndex == PeerTabIndex . group ;
}
2026-01-09 10:03:14 +08:00
// TODO: We should support individual bits combinations in the future.
// But for now, just keep it simple, because the old code only supports single button.
// No users have requested multi-button support yet.
String mouseButtonsToPeer ( int buttons ) {
switch ( buttons ) {
case kPrimaryMouseButton:
return 'left' ;
case kSecondaryMouseButton:
return 'right' ;
case kMiddleMouseButton:
return 'wheel' ;
case kBackMouseButton:
return 'back' ;
case kForwardMouseButton:
return 'forward' ;
default :
return '' ;
}
}
2026-03-04 21:43:19 +08:00
/// Build an avatar widget from an avatar URL or data URI string.
/// Returns [fallback] if avatar is empty or cannot be decoded.
/// [borderRadius] defaults to [size]/2 (circle).
Widget ? buildAvatarWidget ({
required String avatar ,
required double size ,
double ? borderRadius ,
Widget ? fallback ,
}) {
final trimmed = avatar . trim ();
if ( trimmed . isEmpty ) return fallback ;
ImageProvider ? imageProvider ;
if ( trimmed . startsWith ( 'data:image/' )) {
final comma = trimmed . indexOf ( ',' );
if ( comma > 0 ) {
try {
imageProvider = MemoryImage ( base64Decode ( trimmed . substring ( comma + 1 )));
} catch ( _ ) {}
}
} else if ( trimmed . startsWith ( 'http://' ) || trimmed . startsWith ( 'https://' )) {
imageProvider = NetworkImage ( trimmed );
}
if ( imageProvider == null ) return fallback ;
final radius = borderRadius ?? size / 2 ;
return ClipRRect (
borderRadius: BorderRadius . circular ( radius ),
child: Image (
image: imageProvider ,
width: size ,
height: size ,
fit: BoxFit . cover ,
2026-05-07 13:27:13 +08:00
errorBuilder: ( _ , __ , ___ ) => fallback ?? SizedBox . shrink (),
2026-03-04 21:43:19 +08:00
),
);
}