window_manager icon indicating copy to clipboard operation
window_manager copied to clipboard

[macOS] First click does nothing after maximize/unmaximize

Open Jethro87 opened this issue 2 years ago • 5 comments

Double-clicking the top of the application to maximize or unmaximize the window results in the first click not doing anything. I have to click twice in order for any of my buttons/UI elements to register the click.

I've tried hooking into resized, resize, maximize, unmaximize events and calling show(), restore(), etc, but nothing seems to work.

Any suggestions?

Jethro87 avatar May 02 '23 16:05 Jethro87

@Jethro87 did you find solution? I'm facing same issue

13thdeus avatar Feb 06 '24 07:02 13thdeus

@13thdeus Unfortunately not

Jethro87 avatar Feb 07 '24 16:02 Jethro87

Can you provide a minimum reproducible example?

lijy91 avatar Feb 08 '24 02:02 lijy91

I've found adding window toolbar cause this issue. In my case I need it to move window control buttons (traffic lights) down a bit

Here are some details:

main.dart content
void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await windowManager.ensureInitialized();

  WindowOptions windowOptions = const WindowOptions(
    size: Size(800, 600),
    backgroundColor: Colors.transparent,
    skipTaskbar: false,
    titleBarStyle: TitleBarStyle.hidden,
  );
  windowManager.waitUntilReadyToShow(windowOptions);

  runApp(const MyApp());
}
MainFlutterWindow.swift content
import Cocoa
import FlutterMacOS

class MainFlutterWindow: NSWindow {
  override func awakeFromNib() {
    let flutterViewController = FlutterViewController()
    let windowFrame = self.frame
    self.contentViewController = flutterViewController
    self.setFrame(windowFrame, display: true)

    RegisterGeneratedPlugins(registry: flutterViewController)

    self.toolbar = NSToolbar()

    super.awakeFromNib()
  }
}

Looks like issue is not related to package since same issue can be reproduced without using package.

Probably double click on toolbar leads to some focusing related things which I can't understand. I would be grateful for any help in solving this problem

13thdeus avatar Feb 08 '24 06:02 13thdeus

Finally I've managed how to achieve effect I've wanted. I've deleted NSToolbar and used DragToMoveArea from this package to replace toolbar features (double click and drag). Then I've reimplemented this electon pull request to move traffic lights as I want.

Screenshot image
MainFlutterWindow.swift
import Cocoa
import FlutterMacOS

class MainFlutterWindow: NSWindow {
    private var inFullScreenTransition = false
    
    override func awakeFromNib() {
        let flutterViewController = FlutterViewController()
        let windowFrame = self.frame
        self.contentViewController = flutterViewController
        self.setFrame(windowFrame, display: true)
        
        RegisterGeneratedPlugins(registry: flutterViewController)

        super.awakeFromNib()
        
        // traffic lights position must be recalculated on any resize step
        NotificationCenter.default.addObserver(forName: NSWindow.didResizeNotification, object: self, queue: OperationQueue.main) { Notification in
            self.repositionTrafficLights(x: 17.5, y: 38)
        }
        
        // during exiting from full screen reposition works bad. This part fixes behavior
        NotificationCenter.default.addObserver(forName: NSWindow.willExitFullScreenNotification, object: self, queue: OperationQueue.main) { Notification in
            self.inFullScreenTransition = true
            self.repositionTrafficLights(x: 17.5, y: 38)
        }
        NotificationCenter.default.addObserver(forName: NSWindow.didExitFullScreenNotification, object: self, queue: OperationQueue.main) { Notification in
            self.inFullScreenTransition = false
            self.repositionTrafficLights(x: 17.5, y: 38)
        }
        
        
    }
    
    func repositionTrafficLights(x: CGFloat, y: CGFloat) {
        let window = self
        let close = self.standardWindowButton(NSWindow.ButtonType.closeButton)!
        let miniaturize = self.standardWindowButton(NSWindow.ButtonType.miniaturizeButton)!
        let zoom = self.standardWindowButton(NSWindow.ButtonType.zoomButton)!
        let titleBarContainerView = close.superview?.superview
    
        if (titleBarContainerView == nil) {
            return
        }
        
        if (inFullScreenTransition) {
            titleBarContainerView?.isHidden = true
            return
        }
        
        titleBarContainerView!.isHidden = false
        let buttonHeight = close.frame.size.height
        let titleBarFrameHeight = buttonHeight + y
        var titleBarRect = titleBarContainerView!.frame
        titleBarRect.size.height = titleBarFrameHeight;
        titleBarRect.origin.y = window.frame.size.height - titleBarFrameHeight;
        titleBarContainerView!.frame = titleBarRect;
        
        let windowButtons = [ close, miniaturize, zoom ];
        let space_between = miniaturize.frame.origin.x - close.frame.origin.x;
        for i in 0...2 {
            let view = windowButtons[i]
            var rect = view.frame
            rect.origin.x = x + (CGFloat(i) * space_between)
            rect.origin.y = (titleBarFrameHeight - rect.size.height) / 2
            view.setFrameOrigin(rect.origin)
        }
    }
}

Based on this code it is possible to create new cool feature for macOS (moving traffic lights is really painful)

13thdeus avatar Feb 08 '24 15:02 13thdeus