### Initialize WindowManagerPlus in main (Dart) Source: https://github.com/pichillilorenzo/window_manager_plus/blob/main/README.md This Dart code snippet demonstrates how to initialize the window_manager_plus plugin in the application's main function. It shows how to handle command-line arguments to determine the current window's ID (0 for the main window) and pass it to `WindowManagerPlus.ensureInitialized`. It also includes an example of setting window options and showing the window. ```dart import 'package:flutter/material.dart'; import 'package:window_manager_plus/window_manager_plus.dart'; // Must add List args parameter to your main function. void main(List args) async { WidgetsFlutterBinding.ensureInitialized(); // await the initialization of the plugin. // Here is an example of how to use ensureInitialized in the main function: await WindowManagerPlus.ensureInitialized(args.isEmpty ? 0 : int.tryParse(args[0]) ?? 0); // Now you can use the plugin, such as WindowManagerPlus.current WindowOptions windowOptions = WindowOptions( size: Size(800, 600), center: true, backgroundColor: Colors.transparent, skipTaskbar: false, titleBarStyle: TitleBarStyle.hidden, ); WindowManagerPlus.current.waitUntilReadyToShow(windowOptions, () async { await WindowManagerPlus.current.show(); await WindowManagerPlus.current.focus(); }); runApp(MyApp()); } ``` -------------------------------- ### Configure macOS Hidden at Launch Swift Source: https://github.com/pichillilorenzo/window_manager_plus/blob/main/README.md Modifies the macOS runner file (MainFlutterWindow.swift) to import the window_manager plugin and override the window's order method to call hiddenWindowAtLaunch, preventing the window from appearing immediately when the application starts. ```Swift import Cocoa import FlutterMacOS import window_manager class MainFlutterWindow: NSWindow { override func awakeFromNib() { let flutterViewController = FlutterViewController.init() let windowFrame = self.frame self.contentViewController = flutterViewController self.setFrame(windowFrame, display: true) RegisterGeneratedPlugins(registry: flutterViewController) super.awakeFromNib() } override public func order(_ place: NSWindow.OrderingMode, relativeTo otherWin: Int) { super.order(place, relativeTo: otherWin) hiddenWindowAtLaunch() } } ``` -------------------------------- ### Listen to Window Events (Dart) Source: https://github.com/pichillilorenzo/window_manager_plus/blob/main/README.md Shows how to implement the WindowListener mixin in a StatefulWidget to subscribe to and handle various window lifecycle and state change events like close, focus, blur, maximize, etc. ```Dart import 'package:flutter/cupertino.dart'; import 'package:window_manager_plus/window_manager_plus.dart'; class HomePage extends StatefulWidget { @override _HomePageState createState() => _HomePageState(); } class _HomePageState extends State with WindowListener { @override void initState() { super.initState(); WindowManagerPlus.current.addListener(this); } @override void dispose() { WindowManagerPlus.current.removeListener(this); super.dispose(); } @override Widget build(BuildContext context) { // ... } @override void onWindowEvent(String eventName, [int? windowId]) { print('[WindowManager] onWindowEvent: $eventName'); } @override void onWindowClose([int? windowId]) { // do something } @override void onWindowFocus([int? windowId]) { // do something } @override void onWindowBlur([int? windowId]) { // do something } @override void onWindowMaximize([int? windowId]) { // do something } @override void onWindowUnmaximize([int? windowId]) { // do something } @override void onWindowMinimize([int? windowId]) { // do something } @override void onWindowRestore([int? windowId]) { // do something } @override void onWindowResize([int? windowId]) { // do something } @override void onWindowMove([int? windowId]) { // do something } @override void onWindowEnterFullScreen([int? windowId]) { // do something } @override void onWindowLeaveFullScreen([int? windowId]) { // do something } } ``` -------------------------------- ### Create a New Window (Dart) Source: https://github.com/pichillilorenzo/window_manager_plus/blob/main/README.md This Dart code snippet shows how to create a new window programmatically using the `WindowManagerPlus.createWindow` static method. It demonstrates passing arguments to the new window's main function and retrieving the `WindowManagerPlus` instance for the newly created window. ```dart final newWindow = await WindowManagerPlus.createWindow(['my test arg 1', 'my test arg 2']); if (newWindow != null) { print('New Created Window: $newWindow'); } ``` -------------------------------- ### Modify Windows Runner main.cpp (C++) Source: https://github.com/pichillilorenzo/window_manager_plus/blob/main/README.md This diff shows the necessary modifications to the Windows runner's main.cpp file to integrate window_manager_plus. It includes adding necessary headers, setting the main window's SetQuitOnClose to false, and registering a callback to handle the creation of new windows. ```diff #include #include #include #include "flutter_window.h" #include "utils.h" + #include + #include "window_manager_plus/window_manager_plus_plugin.h" int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev, _In_ wchar_t* command_line, _In_ int show_command) { // Attach to console when present (e.g., 'flutter run') or create a // new console when running with a debugger. if (!::AttachConsole(ATTACH_PARENT_PROCESS) && ::IsDebuggerPresent()) { CreateAndAttachConsole(); } // Initialize COM, so that it is available for use in the library and/or // plugins. ::CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED); flutter::DartProject project(L"data"); std::vector command_line_arguments = GetCommandLineArguments(); project.set_dart_entrypoint_arguments(std::move(command_line_arguments)); FlutterWindow window(project); Win32Window::Point origin(10, 10); Win32Window::Size size(1280, 720); if (!window.Create(L"window_manager_example", origin, size)) { return EXIT_FAILURE; } - window.SetQuitOnClose(true); + window.SetQuitOnClose(false); + + WindowManagerPlusPluginSetWindowCreatedCallback( + [](std::vector command_line_arguments) { + flutter::DartProject project(L"data"); + + project.set_dart_entrypoint_arguments( + std::move(command_line_arguments)); + + auto window = std::make_shared(project); + Win32Window::Point origin(10, 10); + Win32Window::Size size(1280, 720); + // Check whether window->Create or window->CreateAndShow is available. + // Take a look at the code above for the main flutter window and + // what method the variable "FlutterWindow window(project)" calls + if (!window->Create(L"window_manager_example", origin, size)) { + std::cerr << "Failed to create a new window" << std::endl; + } + window->SetQuitOnClose(false); + return std::move(window); + }); ::MSG msg; while (::GetMessage(&msg, nullptr, 0, 0)) { ::TranslateMessage(&msg); ::DispatchMessage(&msg); } ::CoUninitialize(); return EXIT_SUCCESS; } ``` -------------------------------- ### Configure Linux Window Visibility C Source: https://github.com/pichillilorenzo/window_manager_plus/blob/main/README.md Modifies the Linux runner file (my_application.cc) to change the method used for displaying the main window from gtk_widget_show to gtk_widget_realize, which can be used in conjunction with window_manager_plus to control initial window visibility. ```C // Implements GApplication::activate. static void my_application_activate(GApplication* application) { ... gtk_window_set_default_size(window, 1280, 720); gtk_widget_realize(GTK_WIDGET(window)); g_autoptr(FlDartProject) project = fl_dart_project_new(); fl_dart_project_set_dart_entrypoint_arguments(project, self->dart_entrypoint_arguments); FlView* view = fl_view_new(project); gtk_widget_show(GTK_WIDGET(view)); gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(view)); fl_register_plugins(FL_PLUGIN_REGISTRY(view)); gtk_widget_grab_focus(GTK_WIDGET(view)); } ... ``` -------------------------------- ### Invoke Method Between Windows (Dart) Source: https://github.com/pichillilorenzo/window_manager_plus/blob/main/README.md Demonstrates how to call a method on another window using its ID and how the target window receives the call via the onEventFromWindow method, requiring the WindowListener mixin. ```Dart // assuming we are in the first window, Window ID 0, and we want to communicate with the second window. final secondWindowId = 1; // ID of the second window final result = await WindowManagerPlus.current.invokeMethodToWindow(secondWindowId, 'myTestMethod', ['arg1', 'arg2']); // the result will be 'Hello from Window 1' // assuming we are in the second window, Window ID 1 class _MyWidgetState extends State with WindowListener { // ... @override void initState() { WindowManagerPlus.current.addListener(this); super.initState(); } @override void dispose() { WindowManagerPlus.current.removeListener(this); super.dispose(); } // ... @override Future onEventFromWindow(String eventName, int fromWindowId, dynamic arguments) async { print('[${WindowManagerPlus.current}] Event $eventName from Window $fromWindowId with arguments $arguments'); return 'Hello from ${WindowManagerPlus.current}'; } } ``` -------------------------------- ### Implement WindowListener in Flutter Widget Source: https://github.com/pichillilorenzo/window_manager_plus/blob/main/README.md Shows a Flutter `StatefulWidget` (`HomePage`) that mixes in `WindowListener`. It demonstrates adding the listener in `initState`, removing it in `dispose`, and calling `setState` within the `onWindowFocus` callback to trigger a UI rebuild when the window gains focus. ```dart import 'package:flutter/cupertino.dart'; import 'package:window_manager/window_manager.dart'; class HomePage extends StatefulWidget { @override _HomePageState createState() => _HomePageState(); } class _HomePageState extends State with WindowListener { @override void initState() { super.initState(); WindowManagerPlus.current.addListener(this); } @override void dispose() { WindowManagerPlus.current.removeListener(this); super.dispose(); } @override Widget build(BuildContext context) { // ... } @override void onWindowFocus() { // Make sure to call once. setState(() {}); // do something } } ``` -------------------------------- ### Registering WindowManagerPlus Plugins (macOS Swift) Source: https://github.com/pichillilorenzo/window_manager_plus/blob/main/README.md Modifies the MainFlutterWindow class in Swift to import the window_manager_plus plugin and register the generated plugins function. This step is essential for the plugin to correctly initialize and interact with the Flutter engine within the main window context. ```Swift import Cocoa import FlutterMacOS + import window_manager_plus class MainFlutterWindow: NSPanel { override func awakeFromNib() { let flutterViewController = FlutterViewController.init() let windowFrame = self.frame self.contentViewController = flutterViewController self.setFrame(windowFrame, display: true) RegisterGeneratedPlugins(registry: flutterViewController) + + WindowManagerPlusPlugin.RegisterGeneratedPlugins = RegisterGeneratedPlugins super.awakeFromNib() } override public func order(_ place: NSWindow.OrderingMode, relativeTo otherWin: Int) { super.order(place, relativeTo: otherWin) hiddenWindowAtLaunch() } } ``` -------------------------------- ### Adjust Flutter 3.7 Windows Show Behavior Source: https://github.com/pichillilorenzo/window_manager_plus/blob/main/README.md Modifies the `OnCreate` method in `flutter_window.cpp` for Flutter 3.7 projects. It comments out or removes the `this->Show()` call within the engine's `SetNextFrameCallback`, suggesting a change in how the window is initially displayed after engine initialization. ```diff bool FlutterWindow::OnCreate() { ... flutter_controller_->engine()->SetNextFrameCallback([&]() { - this->Show(); + //delete this->Show() }); } ``` -------------------------------- ### Implement Confirm Before Closing Dart Source: https://github.com/pichillilorenzo/window_manager_plus/blob/main/README.md Demonstrates how to use the WindowListener mixin and the setPreventClose method from window_manager_plus to intercept the window close event and display a confirmation dialog before allowing the window to close. ```Dart import 'package:flutter/cupertino.dart'; import 'package:window_manager/window_manager.dart'; class HomePage extends StatefulWidget { @override _HomePageState createState() => _HomePageState(); } class _HomePageState extends State with WindowListener { @override void initState() { super.initState(); WindowManagerPlus.current.addListener(this); _init(); } @override void dispose() { WindowManagerPlus.current.removeListener(this); super.dispose(); } void _init() async { // Add this line to override the default close handler await WindowManagerPlus.current.setPreventClose(true); setState(() {}); } @override Widget build(BuildContext context) { // ... } @override void onWindowClose() async { bool _isPreventClose = await WindowManagerPlus.current.isPreventClose(); if (_isPreventClose) { showDialog( context: context, builder: (_) { return AlertDialog( title: Text('Are you sure you want to close this window?'), actions: [ TextButton( child: Text('No'), onPressed: () { Navigator.of(context).pop(); }, ), TextButton( child: Text('Yes'), onPressed: () { Navigator.of(context).pop(); await WindowManagerPlus.current.destroy(); }, ), ], ); }, ); } } } ``` -------------------------------- ### Modify Windows C++ Window Creation Flags Source: https://github.com/pichillilorenzo/window_manager_plus/blob/main/README.md Changes the `CreateWindow` call in `win32_window.cpp` to remove the `WS_VISIBLE` flag, allowing the window to be shown later explicitly instead of immediately upon creation. This is a modification to the native Windows runner code. ```diff bool Win32Window::CreateAndShow(const std::wstring& title, const Point& origin, const Size& size) { ... HWND window = CreateWindow( - window_class, title.c_str(), WS_OVERLAPPEDWINDOW | WS_VISIBLE, + window_class, title.c_str(), + WS_OVERLAPPEDWINDOW, // do not add WS_VISIBLE since the window will be shown later Scale(origin.x, scale_factor), Scale(origin.y, scale_factor), Scale(size.width, scale_factor), Scale(size.height, scale_factor), nullptr, nullptr, GetModuleHandle(nullptr), this); } ``` -------------------------------- ### Disable Quit on Close (macOS Swift) Source: https://github.com/pichillilorenzo/window_manager_plus/blob/main/README.md Provides a code diff showing the necessary modification in the macOS AppDelegate.swift file to prevent the application from quitting when the last window is closed, enabling the use of the hide method. ```diff import Cocoa import FlutterMacOS @NSApplicationMain class AppDelegate: FlutterAppDelegate { override func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool { - return true + return false } } ``` -------------------------------- ### Configure Windows Quit Behavior C++ Source: https://github.com/pichillilorenzo/window_manager_plus/blob/main/README.md Modifies the main Windows runner file (main.cpp) to change the default window close behavior, preventing the application from quitting automatically when the main window is closed. This allows for custom handling of the close event. ```C++ #include #include #include #include #include "flutter_window.h" #include "utils.h" int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev, _In_ wchar_t* command_line, _In_ int show_command) { // Attach to console when present (e.g., 'flutter run') or create a // new console when running with a debugger. if (!::AttachConsole(ATTACH_PARENT_PROCESS) && ::IsDebuggerPresent()) { CreateAndAttachConsole(); } // Initialize COM, so that it is available for use in the library and/or // plugins. ::CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED); flutter::DartProject project(L"data"); std::vector command_line_arguments = GetCommandLineArguments(); project.set_dart_entrypoint_arguments(std::move(command_line_arguments)); FlutterWindow window(project); Win32Window::Point origin(10, 10); Win32Window::Size size(1280, 720); if (!window.CreateAndShow(L"window_manager_example", origin, size)) { return EXIT_FAILURE; } window.SetQuitOnClose(false); ::MSG msg; while (::GetMessage(&msg, nullptr, 0, 0)) { ::TranslateMessage(&msg); ::DispatchMessage(&msg); }m ::CoUninitialize(); return EXIT_SUCCESS; } ``` -------------------------------- ### Modifying App Termination Logic (macOS Swift) Source: https://github.com/pichillilorenzo/window_manager_plus/blob/main/README.md Adjusts the application termination behavior in the AppDelegate to prevent the app from closing prematurely when only the main window is closed. This allows the application to continue running as long as other windows created by the plugin are still open. ```Swift import Cocoa import FlutterMacOS + import window_manager_plus @main class AppDelegate: FlutterAppDelegate { override func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool { - return true + return NSApp.windows.filter({$0 is MainFlutterWindow || $0 is WindowManagerPlusFlutterWindow}).count == 1 // or return false } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.