window_manager icon indicating copy to clipboard operation
window_manager copied to clipboard

App crashes when calling windowManager.hide() with hotkey_manager

Open dancamdev opened this issue 9 months ago • 3 comments

Description

The app crashes when attempting to hide the window using windowManager.hide() after registering a system-wide hotkey with hotkey_manager. The crash occurs specifically when toggling the window visibility with Cmd+T.

Code Snippet

import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:window_manager/window_manager.dart';
import 'package:hotkey_manager/hotkey_manager.dart';

final showAndFocusWindowHotKey = HotKey(
  key: PhysicalKeyboardKey.keyT,
  modifiers: [HotKeyModifier.meta],
  scope: HotKeyScope.system,
);

class AppShortcuts extends StatefulWidget {
  const AppShortcuts({super.key, required this.child});
  final Widget child;

  @override
  State<AppShortcuts> createState() => _AppShortcutsState();
}

class _AppShortcutsState extends State<AppShortcuts> {
  @override
  void initState() {
    super.initState();
    hotKeyManager.register(
      showAndFocusWindowHotKey,
      keyDownHandler: (hotKey) async {
        if (!await windowManager.isVisible()) {
          print('show and focus window');
          windowManager.show();
          windowManager.focus();
        } else {
          print('hide window');
          windowManager.blur();
          await windowManager.hide(); // Crash occurs here
        }
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return widget.child;
  }
}

Steps to Reproduce

  1. Run the app
  2. Press Cmd+T to show the window
  3. Press Cmd+T again to hide the window
  4. App crashes when attempting to hide

Expected Behavior

The window should toggle visibility smoothly when pressing Cmd+T, showing when hidden and hiding when visible.

Actual Behavior

The app crashes when attempting to hide the window.

Environment

  • Flutter SDK: ^3.7.2
  • window_manager: ^0.4.3
  • hotkey_manager: ^0.2.3
  • Platform: macOS

Additional Notes

The crash seems to be related to the interaction between hotkey_manager and window_manager when attempting to hide the window. The show and focus operations work correctly, but the hide operation causes the app to crash.

dancamdev avatar Apr 05 '25 16:04 dancamdev

When I used the two libraries, window_manager and hotkey_manager together, I also encountered the same problem that the program would directly close when I called the hide method.

lingxuan0520 avatar Apr 15 '25 03:04 lingxuan0520

Refer to this, but haven't merged into the main branch yet. https://github.com/leanflutter/window_manager/pull/530/commits/af74247d3c1616968ec281435f7410bcc7f9355c

lingxuan0520 avatar Apr 15 '25 03:04 lingxuan0520

Any update to this? I just ran into the same issue using window_manager and hotkey_manager.

brettshepherd avatar Oct 08 '25 16:10 brettshepherd