window_manager_plus 1.0.3 copy "window_manager_plus: ^1.0.3" to clipboard
window_manager_plus: ^1.0.3 copied to clipboard

This plugin allows Flutter desktop apps to create and manage multiple windows, such as resizing and repositioning, and communicate between them.

window_manager_plus #

pub version All Contributors

This plugin allows Flutter desktop apps to create and manage multiple windows, such as resizing and repositioning, and communicate between them.

This is a fork and a re-work of the original window_manager plugin. With inspiration from the desktop_multi_window plugin, this new implementation allows the creation and management of multiple windows.

Linux is not currently supported.


Platform Support #

Linux macOS Windows
✅️

Quick Start #

Setup to support multiple windows #

macOS

Change the file macos/Runner/MainFlutterWindow.swift as follows:

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()
    }
}

Change the file macos/Runner/AppDelegate.swift as follows:

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
  }
}

Without changing the return logic, the application will close when the main flutter window is closed.

Windows

Change the file windows/runner/main.cpp as follows:

#include <flutter/dart_project.h>
#include <flutter/flutter_view_controller.h>
#include <windows.h>

#include <iostream>

#include "flutter_window.h"
#include "utils.h"

+ #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<std::string> 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(true);
+  window.SetQuitOnClose(false);
+
+  WindowManagerPlusPluginSetWindowCreatedCallback(
+      [](std::vector<std::string> command_line_arguments) {
+        flutter::DartProject project(L"data");
+
+        project.set_dart_entrypoint_arguments(
+            std::move(command_line_arguments));
+
+        auto window = std::make_shared<FlutterWindow>(project);
+        Win32Window::Point origin(10, 10);
+        Win32Window::Size size(1280, 720);
+        if (!window->CreateAndShow(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;
}

window->SetQuitOnClose(false); is necessary to prevent the application from closing when the window is closed.

If you want to close the App only when the main window is closed, you can set window->SetQuitOnClose(true); in the main window. The others called inside the WindowManagerPlusPluginSetWindowCreatedCallback should be set to false.

Usage #

import 'package:flutter/material.dart';
import 'package:window_manager_plus/window_manager_plus.dart';

// Must add args argument.
void main(List<String> args) async {
  WidgetsFlutterBinding.ensureInitialized();
  // Must add this line.
  await WindowManagerPlus.ensureInitialized(args.isEmpty ? 0 : int.tryParse(args[0]) ?? 0);

  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());
}

Please see the example app of this plugin for a full example.

Listening events

The WindowListener mixin class is used to listen to window events. If this is used as a Global Listener using the WindowManagerPlus.addGlobalListener static method, the windowId parameter will be the ID of the window that emitted the event, otherwise, it will be always null.

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<HomePage> 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
  }
}

Quit on close

If you need to use the hide method, you need to disable QuitOnClose.

macOS

Change the file macos/Runner/AppDelegate.swift as follows:

import Cocoa
import FlutterMacOS

@NSApplicationMain
class AppDelegate: FlutterAppDelegate {
  override func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
-    return true
+    return false
  }
}
Windows

Change the file windows/runner/main.cpp as follows:

#include <flutter/dart_project.h>
#include <flutter/flutter_view_controller.h>
#include <windows.h>

#include <iostream>

#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<std::string> 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(true);
+  window.SetQuitOnClose(false);

  ::MSG msg;
  while (::GetMessage(&msg, nullptr, 0, 0)) {
    ::TranslateMessage(&msg);
    ::DispatchMessage(&msg);
  }

  ::CoUninitialize();
  return EXIT_SUCCESS;
}

Confirm before closing

import 'package:flutter/cupertino.dart';
import 'package:window_manager/window_manager.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> 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();
                },
              ),
            ],
          );
        },
      );
    }
  }
}

Hidden at launch

Linux

Change the file linux/my_application.cc as follows:


...

// Implements GApplication::activate.
static void my_application_activate(GApplication* application) {
  
  ...

  gtk_window_set_default_size(window, 1280, 720);
-  gtk_widget_show(GTK_WIDGET(window));
+  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));
}

...

macOS

Change the file macos/Runner/MainFlutterWindow.swift as follows:

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()
+    }
}

Windows

Change the file windows/runner/win32_window.cpp as follows:

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);

Since flutter 3.7 new windows project Change the file windows/runner/flutter_window.cpp as follows:

bool FlutterWindow::OnCreate() {
  ...
  flutter_controller_->engine()->SetNextFrameCallback([&]() {
-   this->Show();
+   //delete this->Show()
  });

Make sure to call setState once on the onWindowFocus event.

import 'package:flutter/cupertino.dart';
import 'package:window_manager/window_manager.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> 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
  }
}

Articles #

API #

WindowManager #

Methods

addListener(WindowListener listener) → void

Add a listener to the window.

blur() → Future<void>

Removes focus from the window.

center({bool animate = false}) → Future<void>

Moves window to the center of the screen.

close() → Future<void>

Try to close the window.

destroy() → Future<void>

Force closing the window.

dock({required DockSide side, required int width}) → Future<void>

Docks the window. only works on Windows

focus() → Future<void>

Focuses on the window.

getBounds() → Future<Rect>

Returns Rect - The bounds of the window as Object.

getDevicePixelRatio() → double

Get the device pixel ratio.

getOpacity() → Future<double>

Returns double - between 0.0 (fully transparent) and 1.0 (fully opaque).

getPosition() → Future<Offset>

Returns Offset - Contains the window's current position.

getSize() → Future<Size>

Returns Size - Contains the window's width and height.

getTitle() → Future<String>

Returns String - The title of the native window.

getTitleBarHeight() → Future<int>

Returns int - The title bar height of the native window.

hasShadow() → Future<bool>

Returns bool - Whether the window has a shadow. On Windows, always returns true unless window is frameless.

hide() → Future<void>

Hides the window.

invokeMethodToWindow(int targetWindowId, String method, [dynamic args]) → Future

Invokes a method on the window with id targetWindowId. It could return a Future that resolves to the return value of the invoked method, otherwise null. Use [WindowListener.onEventFromWindow](https://pub.##### dev/documentation/window_manager_plus/latest/WindowListener/onEventFromWindow.html) to listen for the event.

isAlwaysOnBottom() → Future<bool>

Returns bool - Whether the window is always below other windows.

isAlwaysOnTop() → Future<bool>

Returns bool - Whether the window is always on top of other windows.

isClosable() → Future<bool>

Returns bool - Whether the window can be manually closed by user.

isDockable() → Future<bool>

Returns bool - Whether the window is dockable or not.

isDocked() → Future<DockSide?>

Returns bool - Whether the window is docked.

isFocused() → Future<bool>

Returns bool - Whether window is focused.

isFullScreen() → Future<bool>

Returns bool - Whether the window is in fullscreen mode.

isMaximizable() → Future<bool>

Returns bool - Whether the window can be manually maximized by the user.

isMaximized() → Future<bool>

Returns bool - Whether the window is maximized.

isMinimizable() → Future<bool>

Returns bool - Whether the window can be manually minimized by the user.

isMinimized() → Future<bool>

Returns bool - Whether the window is minimized.

isMovable() → Future<bool>

Returns bool - Whether the window can be moved by user.

isPreventClose() → Future<bool>

Check if is intercepting the native close signal.

isResizable() → Future<bool>

Returns bool - Whether the window can be manually resized by the user.

isSkipTaskbar() → Future<bool>

Returns bool - Whether skipping taskbar is enabled.

isVisible() → Future<bool>

Returns bool - Whether the window is visible to the user.

isVisibleOnAllWorkspaces() → Future<bool>

Returns bool - Whether the window is visible on all workspaces.

maximize({bool vertically = false}) → Future<void>

Maximizes the window. vertically simulates aero snap, only works on Windows

minimize() → Future<void>

Minimizes the window. On some platforms the minimized window will be shown in the Dock.

popUpWindowMenu() → Future<void>
removeListener(WindowListener listener) → void

Remove a listener from the window.

restore() → Future<void>

Restores the window from minimized state to its previous state.

setAlignment(Alignment alignment, {bool animate = false}) → Future<void>

Move the window to a position aligned with the screen.

setAlwaysOnBottom(bool isAlwaysOnBottom) → Future<void>

Sets whether the window should show always below other windows.

setAlwaysOnTop(bool isAlwaysOnTop) → Future<void>

Sets whether the window should show always on top of other windows.

setAsFrameless() → Future<void>

You can call this to remove the window frame (title bar, outline border, etc), which is basically everything except the Flutter view, also can call setTitleBarStyle(TitleBarStyle.normal) or setTitleBarStyle(TitleBarStyle.hidden) to restore it.

setAspectRatio(double aspectRatio) → Future<void>

This will make a window maintain an aspect ratio.

setBackgroundColor(Color backgroundColor) → Future<void>

Sets the background color of the window.

setBadgeLabel([String? label]) → Future<void>

Set/unset label on taskbar(dock) app icon

setBounds(Rect? bounds, {Offset? position, Size? size, bool animate = false}) → Future<void>

Resizes and moves the window to the supplied bounds.

setBrightness(Brightness brightness) → Future<void>

Sets the brightness of the window.

setClosable(bool isClosable) → Future<void>

Sets whether the window can be manually closed by user.

setFullScreen(bool isFullScreen) → Future<void>

Sets whether the window should be in fullscreen mode.

setHasShadow(bool hasShadow) → Future<void>

Sets whether the window should have a shadow. On Windows, doesn't do anything unless window is frameless.

setIcon(String iconPath) → Future<void>

Sets window/taskbar icon.

setIgnoreMouseEvents(bool ignore, {bool forward = false}) → Future<void>

Makes the window ignore all mouse events.

setMaximizable(bool isMaximizable) → Future<void>

Sets whether the window can be manually maximized by the user.

setMaximumSize(Size size) → Future<void>

Sets the maximum size of window to width and height.

setMinimizable(bool isMinimizable) → Future<void>

Sets whether the window can be manually minimized by user.

setMinimumSize(Size size) → Future<void>

Sets the minimum size of window to width and height.

setMovable(bool isMovable) → Future<void>

Sets whether the window can be moved by user.

setOpacity(double opacity) → Future<void>

Sets the opacity of the window.

setPosition(Offset position, {bool animate = false}) → Future<void>

Moves window to position.

setPreventClose(bool isPreventClose) → Future<void>

Set if intercept the native close signal. May useful when combine with the onclose event listener. This will also prevent the manually triggered close event.

setProgressBar(double progress) → Future<void>

Sets progress value in progress bar. Valid range is 0, 1.0.

setResizable(bool isResizable) → Future<void>

Sets whether the window can be manually resized by the user.

setSize(Size size, {bool animate = false}) → Future<void>

Resizes the window to width and height.

setSkipTaskbar(bool isSkipTaskbar) → Future<void>

Makes the window not show in the taskbar / dock.

setTitle(String title) → Future<void>

Changes the title of native window to title.

setTitleBarStyle(TitleBarStyle titleBarStyle, {bool windowButtonVisibility = true}) → Future<void>

Changes the title bar style of native window.

setVisibleOnAllWorkspaces(bool visible, {bool? visibleOnFullScreen}) → Future<void>

Sets whether the window should be visible on all workspaces.

show({bool inactive = false}) → Future<void>

Shows and gives focus to the window.

startDragging() → Future<void>

Starts a window drag based on the specified mouse-down event. On Windows, this is disabled during full screen mode.

startResizing(ResizeEdge resizeEdge) → Future<void>

Starts a window resize based on the specified mouse-down & mouse-move event. On Windows, this is disabled during full screen mode.

toString() → String

A string representation of this object.

undock() → Future<bool>

Undocks the window. only works on Windows

unmaximize() → Future<void>

Unmaximizes the window.

waitUntilReadyToShow([WindowOptions? options, VoidCallback? callback]) → Future<void>

Wait until ready to show.

Static Methods

addGlobalListener(WindowListener listener) → void

Add a global listener to the window.

createWindow([List<String>? args]) → Future<WindowManagerPlus?>

Create a new window.

ensureInitialized(int windowId) → Future<void>

Ensure the window manager for this windowId is initialized. Must be called before accessing the WindowManagerPlus.current.

fromWindowId(int windowId) → [WindowManagerPlus](https://pub.##### dev/documentation/window_manager_plus/latest/WindowManagerPlus-class.html)

Get the window manager from the window id.

getAllWindowManagerIds() → Future<List<int>>

Get all window manager ids.

removeGlobalListener(WindowListener listener) → void

Remove a global listener from the window.

WindowListener #

Methods

onEventFromWindow(String eventName, int fromWindowId, dynamic arguments) → Future

Event from other windows.

onWindowBlur([int? windowId]) → void

Emitted when the window loses focus.

onWindowClose([int? windowId]) → void

Emitted when the window is going to be closed.

onWindowDocked([int? windowId]) → void

Emitted when the window entered a docked state.

onWindowEnterFullScreen([int? windowId]) → void

Emitted when the window enters a full-screen state.

onWindowEvent(String eventName, [int? windowId]) → void

Emitted all events.

onWindowFocus([int? windowId]) → void

Emitted when the window gains focus.

onWindowLeaveFullScreen([int? windowId]) → void

Emitted when the window leaves a full-screen state.

onWindowMaximize([int? windowId]) → void

Emitted when window is maximized.

onWindowMinimize([int? windowId]) → void

Emitted when the window is minimized.

onWindowMove([int? windowId]) → void

Emitted when the window is being moved to a new position.

onWindowMoved([int? windowId]) → void

Emitted once when the window is moved to a new position.

onWindowResize([int? windowId]) → void

Emitted after the window has been resized.

onWindowResized([int? windowId]) → void

Emitted once when the window has finished being resized.

onWindowRestore([int? windowId]) → void

Emitted when the window is restored from a minimized state.

onWindowUndocked([int? windowId]) → void

Emitted when the window leaves a docked state.

onWindowUnmaximize([int? windowId]) → void

Emitted when the window exits from a maximized state.

Contributors #

License #

MIT

17
likes
0
pub points
78%
popularity

Publisher

unverified uploader

This plugin allows Flutter desktop apps to create and manage multiple windows, such as resizing and repositioning, and communicate between them.

Repository (GitHub)
View/report issues

Topics

#window #window-resize #window-manager #multi-window #desktop

Funding

Consider supporting this project:

www.paypal.com

License

unknown (license)

Dependencies

flutter, path, screen_retriever

More

Packages that depend on window_manager_plus