[Feature-Request] Multi-Window Support
Requesting the addition of multi-window support to the window_manager Flutter package.
Feature Request:
Enable the creation and management of multiple windows within a Flutter application. Each window should have independent widgets and state, enhancing the flexibility of Flutter apps.
Use Cases:
- Simultaneous multitasking.
- Custom UI layouts.
- Improved productivity for users.
Note:
Consideration for platform-specific behavior and minimal disruption to existing projects is essential. Feedback and suggestions are welcome.
So I finally found the solution for this. When working with multiple screen, you need to register plugin for each screen Just adding below code inside FlutterWindow::OnCreate() function in windows/Runner/flutter_window.cpp works for me!
#include <desktop_multi_window/desktop_multi_window_plugin.h>
#include <window_manager/window_manager_plugin.h>
DesktopMultiWindowSetWindowCreatedCallback([](void *controller) {
// get view controller and engine of new window
auto *flutter_view_controller =
reinterpret_cast<flutter::FlutterViewController *>(controller);
auto *registry = flutter_view_controller->engine();
WindowManagerPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("WindowManagerPlugin"));
});
So I finally found the solution for this. When working with multiple screen, you need to register plugin for each screen Just adding below code inside FlutterWindow::OnCreate() function in windows/Runner/flutter_window.cpp works for me!
#include <desktop_multi_window/desktop_multi_window_plugin.h> #include <window_manager/window_manager_plugin.h> DesktopMultiWindowSetWindowCreatedCallback([](void *controller) { // get view controller and engine of new window auto *flutter_view_controller = reinterpret_cast<flutter::FlutterViewController *>(controller); auto *registry = flutter_view_controller->engine(); WindowManagerPluginRegisterWithRegistrar( registry->GetRegistrarForPlugin("WindowManagerPlugin")); });
This code, closing the window will cause the program to crash
Did you address issue? If not, you can reference from here
#include "flutter_window.h"
#include <optional>
#include <desktop_multi_window/desktop_multi_window_plugin.h>
#include <media_kit_libs_windows_video/media_kit_libs_windows_video_plugin_c_api.h>
#include <media_kit_video/media_kit_video_plugin_c_api.h>
#include <screen_brightness_windows/screen_brightness_windows_plugin.h>
#include <screen_retriever/screen_retriever_plugin.h>
#include <window_manager/window_manager_plugin.h>
#include "flutter/generated_plugin_registrant.h"
FlutterWindow::FlutterWindow(const flutter::DartProject &project)
: project_(project) {}
FlutterWindow::~FlutterWindow() {}
bool FlutterWindow::OnCreate() {
if (!Win32Window::OnCreate()) {
return false;
}
RECT frame = GetClientArea();
// The size here must match the window dimensions to avoid unnecessary surface
// creation / destruction in the startup path.
flutter_controller_ = std::make_unique<flutter::FlutterViewController>(
frame.right - frame.left, frame.bottom - frame.top, project_);
// Ensure that basic setup of the controller was successful.
if (!flutter_controller_->engine() || !flutter_controller_->view()) {
return false;
}
RegisterPlugins(flutter_controller_->engine());
DesktopMultiWindowSetWindowCreatedCallback([](void *controller) {
// get view controller and engine of new window
auto *flutter_view_controller =
reinterpret_cast<flutter::FlutterViewController *>(controller);
auto *registry = flutter_view_controller->engine();
MediaKitLibsWindowsVideoPluginCApiRegisterWithRegistrar(
registry->GetRegistrarForPlugin("MediaKitLibsWindowsVideoPluginCApi"));
MediaKitVideoPluginCApiRegisterWithRegistrar(
registry->GetRegistrarForPlugin("MediaKitVideoPluginCApi"));
ScreenBrightnessWindowsPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("ScreenBrightnessWindowsPlugin"));
ScreenRetrieverPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("ScreenRetrieverPlugin"));
WindowManagerPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("WindowManagerPlugin"));
});
SetChildContent(flutter_controller_->view()->GetNativeWindow());
// flutter_controller_->engine()->SetNextFrameCallback([&]() {
// ""; // this->Show();
// });
// Flutter can complete the first frame before the "show window" callback is
// registered. The following call ensures a frame is pending to ensure the
// window is shown. It is a no-op if the first frame hasn't completed yet.
// flutter_controller_->ForceRedraw();
return true;
}
void FlutterWindow::OnDestroy() {
if (flutter_controller_) {
flutter_controller_ = nullptr;
}
Win32Window::OnDestroy();
}
LRESULT
FlutterWindow::MessageHandler(HWND hwnd, UINT const message,
WPARAM const wparam,
LPARAM const lparam) noexcept {
// Give Flutter, including plugins, an opportunity to handle window messages.
if (flutter_controller_) {
std::optional<LRESULT> result =
flutter_controller_->HandleTopLevelWindowProc(hwnd, message, wparam,
lparam);
if (result) {
return *result;
}
}
switch (message) {
case WM_FONTCHANGE:
flutter_controller_->engine()->ReloadSystemFonts();
break;
}
return Win32Window::MessageHandler(hwnd, message, wparam, lparam);
}
Need this too.
desktop_multi_window has numerous issues.
- Only "main" window has a window title bar. Secondary windows do not have it.
- File I/O API doesn't work from secondary windows (path_provider, file_picker show channel error)
upd.: desktop_multi_window was updated to 0.3.0 just today, and they solved both of these problems
Now desktop_multi_window is used solely for multiple windows creation, and window_manager for handling position, title, centering and so on.