From 6c895ad4fee762317adf667b3d5a33d4c012ff0e Mon Sep 17 00:00:00 2001 From: Kristen McWilliam <9575627+Merrit@users.noreply.github.com> Date: Sat, 11 Feb 2023 01:16:38 -0500 Subject: [PATCH] Refactor: app window logic (#1842) * feat: add TargetPlatform `isDesktop` helper Safe and convenient way to check if the current platform is a desktop platform. * refactor: move window logic to separate class This is much cleaner and better encapsulated. Sets up an area for further window management in the future. Does not try to initialize on mobile or web, which would crash. * fix: isDesktop doc comment * fix: set window title Currently only displays "app_flowy" as the window title. This commit sets the window title to "AppFlowy" as part of the init process. --- .../app_flowy/lib/core/helpers/helpers.dart | 1 + .../lib/core/helpers/target_platform.dart | 11 ++++++ frontend/app_flowy/lib/main.dart | 15 +------- frontend/app_flowy/lib/window/app_window.dart | 37 +++++++++++++++++++ frontend/app_flowy/lib/window/window.dart | 1 + 5 files changed, 52 insertions(+), 13 deletions(-) create mode 100644 frontend/app_flowy/lib/core/helpers/helpers.dart create mode 100644 frontend/app_flowy/lib/core/helpers/target_platform.dart create mode 100644 frontend/app_flowy/lib/window/app_window.dart create mode 100644 frontend/app_flowy/lib/window/window.dart diff --git a/frontend/app_flowy/lib/core/helpers/helpers.dart b/frontend/app_flowy/lib/core/helpers/helpers.dart new file mode 100644 index 0000000000..e325b1a7bb --- /dev/null +++ b/frontend/app_flowy/lib/core/helpers/helpers.dart @@ -0,0 +1 @@ +export 'target_platform.dart'; diff --git a/frontend/app_flowy/lib/core/helpers/target_platform.dart b/frontend/app_flowy/lib/core/helpers/target_platform.dart new file mode 100644 index 0000000000..5b5419dfd9 --- /dev/null +++ b/frontend/app_flowy/lib/core/helpers/target_platform.dart @@ -0,0 +1,11 @@ +import 'package:flutter/foundation.dart' show TargetPlatform; + +extension TargetPlatformHelper on TargetPlatform { + /// Convenience function to check if the app is running on a desktop computer. + /// + /// Easily check if on desktop by checking `defaultTargetPlatform.isDesktop`. + bool get isDesktop => + this == TargetPlatform.linux || + this == TargetPlatform.macOS || + this == TargetPlatform.windows; +} diff --git a/frontend/app_flowy/lib/main.dart b/frontend/app_flowy/lib/main.dart index bfdc7a5fc1..57db6b7c44 100644 --- a/frontend/app_flowy/lib/main.dart +++ b/frontend/app_flowy/lib/main.dart @@ -1,11 +1,11 @@ import 'package:easy_localization/easy_localization.dart'; import 'package:flutter/material.dart'; import 'package:hotkey_manager/hotkey_manager.dart'; -import 'package:window_manager/window_manager.dart'; import 'startup/launch_configuration.dart'; import 'startup/startup.dart'; import 'user/presentation/splash_screen.dart'; +import 'window/window.dart'; class FlowyApp implements EntryPoint { @override @@ -21,19 +21,8 @@ Future main() async { await EasyLocalization.ensureInitialized(); await hotKeyManager.unregisterAll(); - await windowManager.ensureInitialized(); - await setWindowOptions(); + await AppWindow.initialize(); await FlowyRunner.run(FlowyApp()); } - -Future setWindowOptions() async { - WindowOptions windowOptions = const WindowOptions( - minimumSize: Size(600, 400), - ); - return windowManager.waitUntilReadyToShow(windowOptions, () async { - await windowManager.show(); - await windowManager.focus(); - }); -} diff --git a/frontend/app_flowy/lib/window/app_window.dart b/frontend/app_flowy/lib/window/app_window.dart new file mode 100644 index 0000000000..5d45af0d27 --- /dev/null +++ b/frontend/app_flowy/lib/window/app_window.dart @@ -0,0 +1,37 @@ +import 'dart:ui'; + +import 'package:app_flowy/core/helpers/helpers.dart'; +import 'package:flutter/foundation.dart'; +import 'package:window_manager/window_manager.dart'; + +/// Represents the main window of the app. +class AppWindow { + /// The singleton instance of the window. + static late AppWindow instance; + + AppWindow._() { + instance = this; + } + + /// Initializes the window. + static Future initialize() async { + // Don't initialize on mobile or web. + if (!defaultTargetPlatform.isDesktop) { + return null; + } + + await windowManager.ensureInitialized(); + + WindowOptions windowOptions = const WindowOptions( + minimumSize: Size(600, 400), + title: 'AppFlowy', + ); + + await windowManager.waitUntilReadyToShow(windowOptions, () async { + await windowManager.show(); + await windowManager.focus(); + }); + + return AppWindow._(); + } +} diff --git a/frontend/app_flowy/lib/window/window.dart b/frontend/app_flowy/lib/window/window.dart new file mode 100644 index 0000000000..d0b0bc01c2 --- /dev/null +++ b/frontend/app_flowy/lib/window/window.dart @@ -0,0 +1 @@ +export 'app_window.dart';