From a84b9bd2c8d7d93ad199b799c660e63dfa7670e8 Mon Sep 17 00:00:00 2001 From: fufesou <13586388+fufesou@users.noreply.github.com> Date: Wed, 5 Jun 2024 14:52:56 +0800 Subject: [PATCH] fix: setMovable only on macos (#8261) * fix: setMovable only on macos Signed-off-by: fufesou * Refact and comments Signed-off-by: fufesou * comments Signed-off-by: fufesou * Refact comments Signed-off-by: fufesou --------- Signed-off-by: fufesou --- flutter/lib/common.dart | 27 ++++++++++++++++++- .../lib/desktop/widgets/tabbar_widget.dart | 2 ++ flutter/lib/main.dart | 8 ++++-- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/flutter/lib/common.dart b/flutter/lib/common.dart index 4fbc37b12..02f95c994 100644 --- a/flutter/lib/common.dart +++ b/flutter/lib/common.dart @@ -1008,7 +1008,8 @@ class CustomAlertDialog extends StatelessWidget { return KeyEventResult.handled; // avoid TextField exception on escape } else if (!tabTapped && onSubmit != null && - (key.logicalKey == LogicalKeyboardKey.enter || key.logicalKey == LogicalKeyboardKey.numpadEnter)) { + (key.logicalKey == LogicalKeyboardKey.enter || + key.logicalKey == LogicalKeyboardKey.numpadEnter)) { if (key is RawKeyDownEvent) onSubmit?.call(); return KeyEventResult.handled; } else if (key.logicalKey == LogicalKeyboardKey.tab) { @@ -3372,3 +3373,27 @@ get defaultOptionNo => isCustomClient ? 'N' : ''; get defaultOptionWhitelist => isCustomClient ? ',' : ''; get defaultOptionAccessMode => isCustomClient ? 'custom' : ''; get defaultOptionApproveMode => isCustomClient ? 'password-click' : ''; + +// `setMovable()` is only supported on macOS. +// +// On macOS, the window can be dragged by the tab bar by default. +// We need to disable the movable feature to prevent the window from being dragged by the tabs in the tab bar. +// +// When we drag the blank tab bar (not the tab), the window will be dragged normally by adding the `onPanStart` handle. +// +// See the following code for more details: +// https://github.com/rustdesk/rustdesk/blob/ce1dac3b8613596b4d8ae981275f9335489eb935/flutter/lib/desktop/widgets/tabbar_widget.dart#L385 +// https://github.com/rustdesk/rustdesk/blob/ce1dac3b8613596b4d8ae981275f9335489eb935/flutter/lib/desktop/widgets/tabbar_widget.dart#L399 +// +// @platforms macos +disableWindowMovable(int? windowId) { + if (!isMacOS) { + return; + } + + if (windowId == null) { + windowManager.setMovable(false); + } else { + WindowController.fromWindowId(windowId).setMovable(false); + } +} diff --git a/flutter/lib/desktop/widgets/tabbar_widget.dart b/flutter/lib/desktop/widgets/tabbar_widget.dart index c7c83251e..69ba2ecf1 100644 --- a/flutter/lib/desktop/widgets/tabbar_widget.dart +++ b/flutter/lib/desktop/widgets/tabbar_widget.dart @@ -397,6 +397,8 @@ class DesktopTab extends StatelessWidget { : null, onPanStart: (_) => startDragging(isMainWindow), onPanCancel: () { + // We want to disable dragging of the tab area in the tab bar. + // Disable dragging is needed because macOS handles dragging by default. if (isMacOS) { setMovable(isMainWindow, false); } diff --git a/flutter/lib/main.dart b/flutter/lib/main.dart index 172ff0fe5..7e89d5153 100644 --- a/flutter/lib/main.dart +++ b/flutter/lib/main.dart @@ -95,7 +95,9 @@ Future main(List args) async { desktopType = DesktopType.main; await windowManager.ensureInitialized(); windowManager.setPreventClose(true); - windowManager.setMovable(false); + if (isMacOS) { + disableWindowMovable(kWindowId); + } runMainApp(true); } } @@ -168,7 +170,9 @@ void runMultiWindow( final title = getWindowName(); // set prevent close to true, we handle close event manually WindowController.fromWindowId(kWindowId!).setPreventClose(true); - WindowController.fromWindowId(kWindowId!).setMovable(false); + if (isMacOS) { + disableWindowMovable(kWindowId); + } late Widget widget; switch (appType) { case kAppTypeDesktopRemote: