#include "window_chrome.h" #include #include #include #include namespace { using Value = flutter::EncodableValue; using Map = flutter::EncodableMap; const Value* Find(const Map* map, const char* name) { if (!map) return nullptr; const auto it = map->find(Value(name)); return it == map->end() ? nullptr : &it->second; } bool Boolean(const Map* map, const char* name, bool fallback = false) { const auto value = Find(map, name); const auto boolean = value ? std::get_if(value) : nullptr; return boolean ? *boolean : fallback; } double Number(const Map* map, const char* name, double fallback = 0) { const auto value = Find(map, name); if (value) { if (const auto number = std::get_if(value)) return *number; if (const auto number = std::get_if(value)) return *number; } return fallback; } } // namespace WindowChrome::WindowChrome(HWND window, HWND view, flutter::BinaryMessenger* messenger) : window_(window), view_(view) { SetWindowSubclass(view_, ViewProc, reinterpret_cast(this), reinterpret_cast(this)); chrome_ = std::make_unique>( messenger, "cutdex_native/window", &flutter::StandardMethodCodec::GetInstance()); controls_channel_ = std::make_unique>( messenger, "cutdex_native/window_controls", &flutter::StandardMethodCodec::GetInstance()); chrome_->SetMethodCallHandler([this](const auto& call, auto result) { const auto args = call.arguments(); const auto map = args ? std::get_if(args) : nullptr; if (call.method_name() == "configureChrome") { const double height = Number(map, "headerHeight", 46); if (!map || !std::isfinite(height) || height <= 0) { result->Error("invalid_arguments", "Expected a positive header height"); return; } Configure(Boolean(map, "enabled"), height, Boolean(map, "interceptClose"), Boolean(map, "headerInteractive", true)); result->Success(Value(enabled_)); NotifyState(); } else if (call.method_name() == "setHeaderInteractive") { const auto active = args ? std::get_if(args) : nullptr; if (!active) { result->Error("invalid_arguments", "Expected a boolean"); return; } interactive_ = *active; SetMaximizeHovered(false); result->Success(); } else if (call.method_name() == "minimize") { ShowWindow(window_, SW_MINIMIZE); result->Success(); } else if (call.method_name() == "toggleMaximize") { ShowWindow(window_, IsZoomed(window_) ? SW_RESTORE : SW_MAXIMIZE); result->Success(); } else if (call.method_name() == "close") { // Defer destruction until the method reply has reached Flutter. PostMessage(window_, WM_CLOSE, 0, 0); result->Success(); } else { result->NotImplemented(); } }); controls_channel_->SetMethodCallHandler([this](const auto& call, auto result) { if (call.method_name() != "setHeaderControlRegion") { result->NotImplemented(); return; } const auto args = call.arguments(); const auto map = args ? std::get_if(args) : nullptr; const auto id_value = Find(map, "id"); const auto id = id_value ? std::get_if(id_value) : nullptr; const auto rect_value = Find(map, "rect"); if (!id || !rect_value) { result->Error("invalid_arguments", "Expected id and rect"); return; } if (std::holds_alternative(*rect_value)) { controls_.erase(*id); } else if (const auto rect = std::get_if(rect_value)) { Region region{Number(rect, "x"), Number(rect, "y"), Number(rect, "width"), Number(rect, "height"), false}; const auto role_value = Find(map, "role"); const auto role = role_value ? std::get_if(role_value) : nullptr; region.maximize = role && *role == "maximize"; if (!std::isfinite(region.x) || !std::isfinite(region.y) || !std::isfinite(region.width) || !std::isfinite(region.height) || region.width < 0 || region.height < 0) { result->Error("invalid_arguments", "Invalid control bounds"); return; } controls_[*id] = region; } else { result->Error("invalid_arguments", "Invalid rect"); return; } result->Success(); }); } WindowChrome::~WindowChrome() { chrome_->SetMethodCallHandler(nullptr); controls_channel_->SetMethodCallHandler(nullptr); if (IsWindow(view_)) RemoveWindowSubclass(view_, ViewProc, reinterpret_cast(this)); } void WindowChrome::Configure(bool enabled, double height, bool intercept_close, bool interactive) { // Plugin registration precedes the runner parenting the Flutter view. window_ = GetAncestor(view_, GA_ROOT); const bool changed = enabled_ != enabled; enabled_ = enabled; header_height_ = height; intercept_close_ = intercept_close; interactive_ = interactive; if (!changed) return; if (!enabled) { maximize_pressed_ = false; if (GetCapture() == window_) ReleaseCapture(); SetMaximizeHovered(false); } // Retain WS_THICKFRAME, WS_SYSMENU and the caption command styles so Windows // still owns resizing, Alt+Space, double-click zoom and drag-to-snap. const MARGINS margins{0, 0, enabled ? 1 : 0, 0}; DwmExtendFrameIntoClientArea(window_, &margins); SetWindowPos(window_, nullptr, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED); } LRESULT WindowChrome::HitTest(LPARAM position) const { POINT point{GET_X_LPARAM(position), GET_Y_LPARAM(position)}; RECT window_rect{}; GetWindowRect(window_, &window_rect); const UINT dpi = GetDpiForWindow(window_); const int frame = GetSystemMetricsForDpi(SM_CXSIZEFRAME, dpi) + GetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi); if (!IsZoomed(window_)) { const bool left = point.x < window_rect.left + frame; const bool right = point.x >= window_rect.right - frame; const bool top = point.y < window_rect.top + frame; const bool bottom = point.y >= window_rect.bottom - frame; if (top) return left ? HTTOPLEFT : right ? HTTOPRIGHT : HTTOP; if (bottom) return left ? HTBOTTOMLEFT : right ? HTBOTTOMRIGHT : HTBOTTOM; if (left) return HTLEFT; if (right) return HTRIGHT; } if (!interactive_) return HTCLIENT; ScreenToClient(view_, &point); const double scale = dpi / 96.0; const double x = point.x / scale; const double y = point.y / scale; if (y < 0 || y >= header_height_) return HTCLIENT; // Flutter owns button sizes and positions. Only the reported maximize region // becomes a native hit target for Windows 11 Snap Layouts. LRESULT hit = HTCAPTION; for (const auto& entry : controls_) { const auto& region = entry.second; if (x >= region.x && x < region.x + region.width && y >= region.y && y < region.y + region.height) { // The caption strip is above workspace controls, including controls // whose layout bounds extend beyond a clipped scroll viewport. if (region.maximize) return HTMAXBUTTON; hit = HTCLIENT; } } return hit; } void WindowChrome::NotifyState() { chrome_->InvokeMethod("windowState", std::make_unique(Map{ {Value("maximized"), Value(IsZoomed(window_) != FALSE)}, {Value("maximizeHovered"), Value(maximize_hovered_)}})); } void WindowChrome::SetMaximizeHovered(bool hovered) { if (maximize_hovered_ == hovered) return; maximize_hovered_ = hovered; NotifyState(); } std::optional WindowChrome::HandleMessage(UINT message, WPARAM wparam, LPARAM lparam) { if (!enabled_) return std::nullopt; switch (message) { case WM_NCCALCSIZE: { // Let Windows keep the side/bottom resize borders, replacing only the top. auto rect = wparam ? &reinterpret_cast(lparam)->rgrc[0] : reinterpret_cast(lparam); const LONG top = rect->top; DefWindowProc(window_, message, wparam, lparam); const UINT dpi = GetDpiForWindow(window_); rect->top = top + (IsZoomed(window_) ? GetSystemMetricsForDpi(SM_CYSIZEFRAME, dpi) + GetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi) : 0); return 0; } case WM_NCHITTEST: return HitTest(lparam); case WM_NCMOUSEMOVE: { SetMaximizeHovered(wparam == HTMAXBUTTON); TRACKMOUSEEVENT tracking{sizeof(TRACKMOUSEEVENT), TME_LEAVE | TME_NONCLIENT, window_, 0}; TrackMouseEvent(&tracking); // Continue through DWM/DefWindowProc so Snap Layouts can appear. break; } case WM_NCMOUSELEAVE: SetMaximizeHovered(false); break; case WM_NCLBUTTONDOWN: case WM_NCLBUTTONDBLCLK: if (wparam == HTMAXBUTTON) { maximize_pressed_ = true; SetCapture(window_); return 0; } break; case WM_LBUTTONUP: if (maximize_pressed_) { POINT point{GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam)}; ClientToScreen(window_, &point); const bool activate = HitTest(MAKELPARAM(point.x, point.y)) == HTMAXBUTTON; maximize_pressed_ = false; ReleaseCapture(); SetMaximizeHovered(false); if (activate) ShowWindow(window_, IsZoomed(window_) ? SW_RESTORE : SW_MAXIMIZE); return 0; } break; case WM_CAPTURECHANGED: case WM_CANCELMODE: maximize_pressed_ = false; break; case WM_SIZE: NotifyState(); break; case WM_CLOSE: if (intercept_close_) { chrome_->InvokeMethod("requestClose", nullptr); return 0; } break; case WM_DWMCOMPOSITIONCHANGED: { const MARGINS margins{0, 0, 1, 0}; DwmExtendFrameIntoClientArea(window_, &margins); break; } } LRESULT result = 0; if (DwmDefWindowProc(window_, message, wparam, lparam, &result)) return result; return std::nullopt; } LRESULT CALLBACK WindowChrome::ViewProc(HWND window, UINT message, WPARAM wparam, LPARAM lparam, UINT_PTR id, DWORD_PTR data) { const auto chrome = reinterpret_cast(data); if (message == WM_NCHITTEST && chrome->enabled_ && chrome->HitTest(lparam) != HTCLIENT) { // Flutter is a child HWND covering the client area. Let native caption and // resize hits pass through to its parent while keeping controls in Flutter. return HTTRANSPARENT; } if (message == WM_NCDESTROY) RemoveWindowSubclass(window, ViewProc, id); return DefSubclassProc(window, message, wparam, lparam); }