veilidchat/lib/tools/window_control.dart

111 lines
3.2 KiB
Dart
Raw Normal View History

2023-12-27 22:56:24 -05:00
import 'dart:async';
2024-07-11 23:04:08 -04:00
import 'package:async_tools/async_tools.dart';
import 'package:flutter/foundation.dart';
2023-12-27 22:56:24 -05:00
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:window_manager/window_manager.dart';
2024-07-08 21:29:52 -04:00
import '../theme/views/responsive.dart';
2024-07-11 23:04:08 -04:00
import 'tools.dart';
2023-12-27 22:56:24 -05:00
export 'package:window_manager/window_manager.dart' show TitleBarStyle;
enum OrientationCapability {
normal,
portraitOnly,
landscapeOnly,
}
// Window Control
Future<void> initializeWindowControl() async {
if (isDesktop) {
await windowManager.ensureInitialized();
const windowOptions = WindowOptions(
size: Size(768, 1024),
2024-07-08 21:29:52 -04:00
minimumSize: Size(400, 500),
2023-12-27 22:56:24 -05:00
center: true,
backgroundColor: Colors.transparent,
skipTaskbar: false,
);
await windowManager.waitUntilReadyToShow(windowOptions, () async {
2024-07-11 23:04:08 -04:00
await _asyncChangeWindowSetup(
2024-01-08 21:37:08 -05:00
TitleBarStyle.hidden, OrientationCapability.normal);
2023-12-27 22:56:24 -05:00
await windowManager.show();
await windowManager.focus();
});
}
}
2024-07-11 23:04:08 -04:00
const kWindowSetup = '__windowSetup';
Future<void> _asyncChangeWindowSetup(TitleBarStyle titleBarStyle,
2023-12-27 22:56:24 -05:00
OrientationCapability orientationCapability) async {
if (isDesktop) {
await windowManager.setTitleBarStyle(titleBarStyle);
} else {
switch (orientationCapability) {
case OrientationCapability.normal:
await SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.landscapeLeft,
DeviceOrientation.landscapeRight,
]);
case OrientationCapability.portraitOnly:
await SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
]);
case OrientationCapability.landscapeOnly:
await SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeLeft,
DeviceOrientation.landscapeRight,
]);
}
}
}
2024-07-11 23:04:08 -04:00
void changeWindowSetup(
TitleBarStyle titleBarStyle, OrientationCapability orientationCapability) {
singleFuture<void>(
kWindowSetup,
() async =>
_asyncChangeWindowSetup(titleBarStyle, orientationCapability));
}
abstract class WindowSetupState<T extends StatefulWidget> extends State<T> {
WindowSetupState(
{required this.titleBarStyle, required this.orientationCapability});
@override
void initState() {
changeWindowSetup(this.titleBarStyle, this.orientationCapability);
super.initState();
}
@override
void activate() {
changeWindowSetup(this.titleBarStyle, this.orientationCapability);
super.activate();
}
@override
void deactivate() {
changeWindowSetup(TitleBarStyle.normal, OrientationCapability.normal);
super.deactivate();
}
////////////////////////////////////////////////////////////////////////////
final TitleBarStyle titleBarStyle;
final OrientationCapability orientationCapability;
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties
..add(EnumProperty<TitleBarStyle>('titleBarStyle', titleBarStyle))
..add(EnumProperty<OrientationCapability>(
'orientationCapability', orientationCapability));
}
}