mirror of
https://github.com/bggRGjQaUbCoE/PiliPlus.git
synced 2026-04-24 04:22:39 +08:00
140
lib/main.dart
140
lib/main.dart
@@ -2,9 +2,10 @@ import 'dart:io';
|
||||
|
||||
import 'package:PiliPlus/build_config.dart';
|
||||
import 'package:PiliPlus/common/constants.dart';
|
||||
import 'package:PiliPlus/common/widgets/back_detector.dart';
|
||||
import 'package:PiliPlus/common/widgets/custom_toast.dart';
|
||||
import 'package:PiliPlus/common/widgets/mouse_back.dart';
|
||||
import 'package:PiliPlus/common/widgets/scale_app.dart';
|
||||
import 'package:PiliPlus/common/widgets/scroll_behavior.dart';
|
||||
import 'package:PiliPlus/http/init.dart';
|
||||
import 'package:PiliPlus/models/common/theme/theme_color_type.dart';
|
||||
import 'package:PiliPlus/router/app_pages.dart';
|
||||
@@ -30,7 +31,6 @@ import 'package:PiliPlus/utils/utils.dart';
|
||||
import 'package:catcher_2/catcher_2.dart';
|
||||
import 'package:dynamic_color/dynamic_color.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/gestures.dart' show PointerDeviceKind;
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_displaymode/flutter_displaymode.dart';
|
||||
@@ -45,20 +45,7 @@ import 'package:window_manager/window_manager.dart' hide calcWindowPosition;
|
||||
|
||||
WebViewEnvironment? webViewEnvironment;
|
||||
|
||||
void main() async {
|
||||
ScaledWidgetsFlutterBinding.ensureInitialized();
|
||||
MediaKit.ensureInitialized();
|
||||
tmpDirPath = (await getTemporaryDirectory()).path;
|
||||
appSupportDirPath = (await getApplicationSupportDirectory()).path;
|
||||
try {
|
||||
await GStorage.init();
|
||||
} catch (e) {
|
||||
await Utils.copyText(e.toString());
|
||||
if (kDebugMode) debugPrint('GStorage init error: $e');
|
||||
exit(0);
|
||||
}
|
||||
ScaledWidgetsFlutterBinding.instance.setScaleFactor(Pref.uiScale);
|
||||
|
||||
Future<void> _initDownPath() async {
|
||||
if (PlatformUtils.isDesktop) {
|
||||
final customDownPath = Pref.downloadPath;
|
||||
if (customDownPath != null && customDownPath.isNotEmpty) {
|
||||
@@ -86,6 +73,29 @@ void main() async {
|
||||
} else {
|
||||
downloadPath = defDownloadPath;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _initTmpPath() async {
|
||||
tmpDirPath = (await getTemporaryDirectory()).path;
|
||||
}
|
||||
|
||||
Future<void> _initAppPath() async {
|
||||
appSupportDirPath = (await getApplicationSupportDirectory()).path;
|
||||
}
|
||||
|
||||
void main() async {
|
||||
ScaledWidgetsFlutterBinding.ensureInitialized();
|
||||
MediaKit.ensureInitialized();
|
||||
await _initAppPath();
|
||||
try {
|
||||
await GStorage.init();
|
||||
} catch (e) {
|
||||
await Utils.copyText(e.toString());
|
||||
if (kDebugMode) debugPrint('GStorage init error: $e');
|
||||
exit(0);
|
||||
}
|
||||
ScaledWidgetsFlutterBinding.instance.setScaleFactor(Pref.uiScale);
|
||||
await Future.wait([_initDownPath(), _initTmpPath()]);
|
||||
Get
|
||||
..lazyPut(AccountService.new)
|
||||
..lazyPut(DownloadService.new);
|
||||
@@ -106,9 +116,7 @@ void main() async {
|
||||
),
|
||||
setupServiceLocator(),
|
||||
]);
|
||||
}
|
||||
|
||||
if (Platform.isWindows) {
|
||||
} else if (Platform.isWindows) {
|
||||
if (await WebViewEnvironment.getAvailableVersion() != null) {
|
||||
webViewEnvironment = await WebViewEnvironment.create(
|
||||
settings: WebViewEnvironmentSettings(
|
||||
@@ -279,66 +287,49 @@ class MyApp extends StatelessWidget {
|
||||
builder: FlutterSmartDialog.init(
|
||||
toastBuilder: (msg) => CustomToast(msg: msg),
|
||||
loadingBuilder: (msg) => LoadingWidget(msg: msg),
|
||||
builder: (context, child) {
|
||||
final uiScale = Pref.uiScale;
|
||||
final mediaQuery = MediaQuery.of(context);
|
||||
final textScaler = TextScaler.linear(Pref.defaultTextScale);
|
||||
if (uiScale != 1.0) {
|
||||
child = MediaQuery(
|
||||
data: mediaQuery.copyWith(
|
||||
textScaler: textScaler,
|
||||
size: mediaQuery.size / uiScale,
|
||||
padding: mediaQuery.padding / uiScale,
|
||||
viewInsets: mediaQuery.viewInsets / uiScale,
|
||||
viewPadding: mediaQuery.viewPadding / uiScale,
|
||||
devicePixelRatio: mediaQuery.devicePixelRatio * uiScale,
|
||||
),
|
||||
child: child!,
|
||||
);
|
||||
} else {
|
||||
child = MediaQuery(
|
||||
data: mediaQuery.copyWith(textScaler: textScaler),
|
||||
child: child!,
|
||||
);
|
||||
}
|
||||
if (PlatformUtils.isDesktop) {
|
||||
return Focus(
|
||||
canRequestFocus: false,
|
||||
onKeyEvent: (_, event) {
|
||||
if (event.logicalKey == LogicalKeyboardKey.escape &&
|
||||
event is KeyDownEvent) {
|
||||
_onBack();
|
||||
return KeyEventResult.handled;
|
||||
}
|
||||
return KeyEventResult.ignored;
|
||||
},
|
||||
child: MouseBackDetector(
|
||||
onTapDown: _onBack,
|
||||
child: child,
|
||||
),
|
||||
);
|
||||
}
|
||||
return child;
|
||||
},
|
||||
builder: _builder,
|
||||
),
|
||||
navigatorObservers: [
|
||||
PageUtils.routeObserver,
|
||||
FlutterSmartDialog.observer,
|
||||
],
|
||||
scrollBehavior: const MaterialScrollBehavior().copyWith(
|
||||
scrollbars: false,
|
||||
dragDevices: {
|
||||
PointerDeviceKind.touch,
|
||||
PointerDeviceKind.stylus,
|
||||
PointerDeviceKind.invertedStylus,
|
||||
PointerDeviceKind.trackpad,
|
||||
PointerDeviceKind.unknown,
|
||||
if (PlatformUtils.isDesktop) PointerDeviceKind.mouse,
|
||||
},
|
||||
),
|
||||
scrollBehavior: PlatformUtils.isDesktop
|
||||
? const CustomScrollBehavior(desktopDragDevices)
|
||||
: null,
|
||||
);
|
||||
}
|
||||
|
||||
static Widget _builder(BuildContext context, Widget? child) {
|
||||
final uiScale = Pref.uiScale;
|
||||
final mediaQuery = MediaQuery.of(context);
|
||||
final textScaler = TextScaler.linear(Pref.defaultTextScale);
|
||||
if (uiScale != 1.0) {
|
||||
child = MediaQuery(
|
||||
data: mediaQuery.copyWith(
|
||||
textScaler: textScaler,
|
||||
size: mediaQuery.size / uiScale,
|
||||
padding: mediaQuery.padding / uiScale,
|
||||
viewInsets: mediaQuery.viewInsets / uiScale,
|
||||
viewPadding: mediaQuery.viewPadding / uiScale,
|
||||
devicePixelRatio: mediaQuery.devicePixelRatio * uiScale,
|
||||
),
|
||||
child: child!,
|
||||
);
|
||||
} else {
|
||||
child = MediaQuery(
|
||||
data: mediaQuery.copyWith(textScaler: textScaler),
|
||||
child: child!,
|
||||
);
|
||||
}
|
||||
if (PlatformUtils.isDesktop) {
|
||||
return BackDetector(
|
||||
onBack: _onBack,
|
||||
child: child,
|
||||
);
|
||||
}
|
||||
return child;
|
||||
}
|
||||
|
||||
/// from [DynamicColorBuilderState.initPlatformState]
|
||||
static Future<bool> initPlatformState() async {
|
||||
if (_light != null || _dark != null) return true;
|
||||
@@ -388,9 +379,10 @@ class MyApp extends StatelessWidget {
|
||||
class _CustomHttpOverrides extends HttpOverrides {
|
||||
@override
|
||||
HttpClient createHttpClient(SecurityContext? context) {
|
||||
final client = super.createHttpClient(context)
|
||||
// ..maxConnectionsPerHost = 32
|
||||
..idleTimeout = const Duration(seconds: 15);
|
||||
final client = super.createHttpClient(context);
|
||||
// ..maxConnectionsPerHost = 32
|
||||
/// The default value is 15 seconds.
|
||||
// ..idleTimeout = const Duration(seconds: 15);
|
||||
if (kDebugMode || Pref.badCertificateCallback) {
|
||||
client.badCertificateCallback = (cert, host, port) => true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user