window_manager_plus
このプラグインにより、Flutterデスクトップアプリが複数のウィンドウを作成・管理でき、リサイズや再配置が可能になり、それらの間で通信できます。
このプラグインにより、Flutterデスクトップアプリが複数のウィンドウを作成・管理でき、リサイズや再配置が可能になり、それらの間で通信できます。
{"sdk":"flutter"}^1.8.0^0.2.0^4.1.1{"sdk":"flutter"}^0.1.2以下は英語原文のスナップショットです。最新版は GitHub をご覧ください。
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.
| Linux | macOS | Windows |
|---|---|---|
| ❌ | ✅ | ✅️ |
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.
Change the file windows/runner/main.cpp as follows:
#include <flutter/dart_project.h>
#include <flutter/flutter_view_controller.h>
#include <windows.h>
#include "flutter_window.h"
#include "utils.h"
+ #include <iostream>
+ #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.Create(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);
+ // 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;
}
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.
You must call WindowManagerPlus.ensureInitialized static method and await it before using any WindowManagerPlus methods or WindowManagerPlus.current.
It is used to initialize the plugin with the current window ID.
When creating a new window, the args parameter of the main function will have the window ID as a String.
You must parse it to an integer and pass it to the WindowManagerPlus.ensureInitialized method.
If the args parameter is empty or the first argument is not an integer, then we are in the main window, which ID is exactly 0.
The other arguments will contain the arguments passed to the WindowManagerPlus.createWindow method, if any.
import 'package:flutter/material.dart';
import 'package:window_manager_plus/window_manager_plus.dart';
// Must add List<String> args parameter to your main function.
void main(List<String> 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());
}
You can create a new window by calling the WindowManagerPlus.createWindow static method.
If you want control the new window, you can use the return value of the method, which returns a WindowManagerPlus instance.
final newWindow = await WindowManagerPlus.createWindow(['my test arg 1', 'my test arg 2']);
if (newWindow != null) {
print('New Created Window: $newWindow');
}
You can communicate with another window by using the WindowManagerPlus.invokeMethodToWindow method.
The first parameter is the ID of the window you want to communicate with.
The second parameter is the method name you want to call.
The third parameter is the arguments you want to pass to the method, if any.
The other window must register and implement the WindowListener class and override the WindowListener.onEventFromWindow method to receive the event.
// 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<MyWidget> with WindowListener {
// ...
@override
void initState() {
WindowManagerPlus.current.addListener(this);
super.initState();
}
@override
void dispose() {
WindowManagerPlus.current.removeListener(this);
super.dispose();
}
// ...
@override
Future<dynamic> onEventFromWindow(String eventName, int fromWindowId, dynamic arguments) async {
print('[${WindowManagerPlus.current}] Event $eventName from Window $fromWindowId with arguments $arguments');
return 'Hello from ${WindowManagerPlus.current}';
}
}
Using WindowManagerPlus.getAllWindowManagerIds() static method you can get all the window manager ids available.
Please see the example app of this plugin for a full example.
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
}
}
If you need to use the hide method, you need to disable QuitOnClose.
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
}
}
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;
}
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();
},
),
],
);
},
);
}
}
}
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));
}
...
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()
+ }
}
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
}
}
Add a listener to the window.
Removes focus from the window.
Moves window to the center of the screen.
Try to close the window.
Force closing the window.
Docks the window. only works on Windows
Focuses on the window.
Returns Rect - The bounds of the window as Object.
Get the device pixel ratio.
Returns double - between 0.0 (fully transparent) and 1.0 (fully opaque).
Returns Offset - Contains the window's current position.
Returns Size - Contains the window's width and height.
Returns String - The title of the native window.
Returns int - The title bar height of the native window.
Returns bool - Whether the window has a shadow. On Windows, always returns true unless window is frameless.
Hides the window.
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 to listen for the event.
Returns bool - Whether the window is always below other windows.
Returns bool - Whether the window is always on top of other windows.
Returns bool - Whether the window can be manually closed by user.
Returns bool - Whether the window is dockable or not.
Returns bool - Whether the window is docked.
Returns bool - Whether window is focused.
Returns bool - Whether the window is in fullscreen mode.
Returns bool - Whether the window can be manually maximized by the user.
Returns bool - Whether the window is maximized.
Returns bool - Whether the window can be manually minimized by the user.
Returns bool - Whether the window is minimized.
Returns bool - Whether the window can be moved by user.
Check if is intercepting the native close signal.
Returns bool - Whether the window can be manually resized by the user.
Returns bool - Whether skipping taskbar is enabled.
Returns bool - Whether the window is visible to the user.
Returns bool - Whether the window is visible on all workspaces.
Maximizes the window. vertically simulates aero snap, only works on Windows
Minimizes the window. On some platforms the minimized window will be shown in the Dock.
Remove a listener from the window.
Restores the window from minimized state to its previous state.
Move the window to a position aligned with the screen.
Sets whether the window should show always below other windows.
Sets whether the window should show always on top of other windows.
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.
This will make a window maintain an aspect ratio.
Sets the background color of the window.
Set/unset label on taskbar(dock) app icon
Resizes and moves the window to the supplied bounds.
Sets the brightness of the window.
Sets whether the window can be manually closed by user.
Sets whether the window should be in fullscreen mode.
Sets whether the window should have a shadow. On Windows, doesn't do anything unless window is frameless.
Sets window/taskbar icon.
Makes the window ignore all mouse events.
Sets whether the window can be manually maximized by the user.
Sets the maximum size of window to width and height.
Sets whether the window can be manually minimized by user.
Sets the minimum size of window to width and height.
Sets whether the window can be moved by user.
Sets the opacity of the window.
Moves window to position.
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.
Sets progress value in progress bar. Valid range is 0, 1.0.
Sets whether the window can be manually resized by the user.
Resizes the window to width and height.
Makes the window not show in the taskbar / dock.
Changes the title of native window to title.
Changes the title bar style of native window.
Sets whether the window should be visible on all workspaces.
Shows and gives focus to the window.
Starts a window drag based on the specified mouse-down event. On Windows, this is disabled during full screen mode.
Starts a window resize based on the specified mouse-down & mouse-move event. On Windows, this is disabled during full screen mode.
A string representation of this object.
Undocks the window. only works on Windows
Unmaximizes the window.
Wait until ready to show.
Add a global listener to the window.
Create a new window.
Ensure the window manager for this windowId is initialized. Must be called before accessing the WindowManagerPlus.current.
Get the window manager from the window id.
Get all window manager ids.
Remove a global listener from the window.
Event from other windows.
Emitted when the window loses focus.
Emitted when the window is going to be closed.
Emitted when the window entered a docked state.
Emitted when the window enters a full-screen state.
Emitted all events.
Emitted when the window gains focus.
Emitted when the window leaves a full-screen state.
Emitted when window is maximized.
Emitted when the window is minimized.
Emitted when the window is being moved to a new position.
Emitted once when the window is moved to a new position.
Emitted after the window has been resized.
Emitted once when the window has finished being resized.
Emitted when the window is restored from a minimized state.
Emitted when the window leaves a docked state.
Emitted when the window exits from a maximized state.