import 'dart:async'; import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; enum WindowCaptionCommand { minimize, toggleMaximize, close } enum HeaderControlRole { control, maximize } /// Owns native window chrome while its Flutter owner is active. class WindowChromeSession extends ChangeNotifier { WindowChromeSession({required this._headerHeight, this._onCloseRequested}); static const _chrome = MethodChannel('cutdex_native/window'); static const _controls = MethodChannel('cutdex_native/window_controls'); static final _sessions = []; static WindowChromeSession? _owner; static bool get isWindows => !kIsWeb && defaultTargetPlatform == TargetPlatform.windows; static bool get supported => !kIsWeb && (defaultTargetPlatform == TargetPlatform.macOS || isWindows); double _headerHeight; VoidCallback? _onCloseRequested; bool _interactive = true; bool _disposed = false; int _revision = 0; bool _integrated = false; bool _maximized = false; bool _maximizeHovered = false; bool get integrated => _integrated; bool get maximized => _maximized; bool get maximizeHovered => _maximizeHovered; void attach() { if (!supported || _disposed || _sessions.contains(this)) return; _sessions.add(this); _activate(); } void _activate() { _owner = this; _chrome.setMethodCallHandler(_handleMethodCall); unawaited(_enable()); } void configure({ required double headerHeight, VoidCallback? onCloseRequested, }) { final changed = _headerHeight != headerHeight || (_onCloseRequested == null) != (onCloseRequested == null); _headerHeight = headerHeight; _onCloseRequested = onCloseRequested; if (changed && identical(_owner, this)) unawaited(_enable()); } void setInteractive(bool interactive) { if (_interactive == interactive) return; _interactive = interactive; if (isWindows && identical(_owner, this)) { unawaited(_invoke('setHeaderInteractive', interactive)); } } Future perform(WindowCaptionCommand command) async { if (!_disposed && identical(_owner, this)) { await _invoke(command.name); } } Future _handleMethodCall(MethodCall call) async { if (_disposed || !identical(_owner, this)) return false; switch (call.method) { case 'windowState': final state = call.arguments as Map; final maximized = state['maximized'] == true; final hovered = state['maximizeHovered'] == true; if (_maximized != maximized || _maximizeHovered != hovered) { _maximized = maximized; _maximizeHovered = hovered; notifyListeners(); } return true; case 'requestClose': if (_onCloseRequested == null) return false; _onCloseRequested!(); return true; default: throw MissingPluginException('Unsupported window chrome method'); } } Future _enable() async { final revision = ++_revision; final enabled = await _invoke('configureChrome', { 'enabled': true, 'headerHeight': _headerHeight, 'interceptClose': _onCloseRequested != null, if (isWindows) 'headerInteractive': _interactive, }); if (!_disposed && identical(_owner, this) && revision == _revision && _integrated != (enabled == true)) { _integrated = enabled == true; notifyListeners(); } } static Future _invoke(String method, [Object? arguments]) async { try { return await _chrome.invokeMethod(method, arguments); } on MissingPluginException { // Widget tests without native plugins keep the standard header. } on PlatformException catch (error) { debugPrint('Could not update window chrome: ${error.code}'); } return null; } static Future reportControl( String id, Rect? rect, HeaderControlRole role, ) async { try { await _controls.invokeMethod('setHeaderControlRegion', { 'id': id, 'role': role.name, 'rect': rect == null ? null : { 'x': rect.left, 'y': rect.top, 'width': rect.width, 'height': rect.height, }, }); } on MissingPluginException { // Widget tests do not register native plugins. } on PlatformException catch (error) { debugPrint('Could not update titlebar controls: ${error.code}'); } } @override void dispose() { _disposed = true; _sessions.remove(this); if (identical(_owner, this)) { _owner = null; _chrome.setMethodCallHandler(null); if (_sessions.isNotEmpty) { _sessions.last._activate(); } else { scheduleMicrotask(() { if (_owner == null) { unawaited(_invoke('configureChrome', {'enabled': false})); } }); } } super.dispose(); } }