0

I have implemented my resizing behavior for an NSWindow. The problem is I don't know how to disable the default resizing behavior. For example, a resizing cursor would been shown when the cursor was moved to borders of the NSWindow, which is not expected for me.

As resizing was implemented by myself, the NSWindow has to be resizable. The NSWindow has no caption and border frame. If I override the NSWndow.canBecomeKeyWindow and return NO, then no default resizing cursor would be shown as I expected. However, the NSWindow used here must support being key window.

Actually, I have no idea about how to deal with this problem. For a window in Windows OS, I can simply respond to WM_HITTEST and return HT_CLIENT to prevent system default behavior. However, for macOS, there is no corresponding way to do so.

4
  • 2
    Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented Jul 1 at 16:25
  • How? What functionality do you want to add or remove? Commented Jul 2 at 7:06
  • I had implemented the resizing by myself. For example, supposing I have a NSWindow,its position is (0,0, 100,100), and given it the left resizeing hot range is (0,0, 10,100). If I drag the window in the range, I will do resize. that runs good. However, If I click at position such as (1,1), macos do the resizing itself, which prevented my work. What I exptected is to disable the system resizing behavior. Thanks for your attention! Commented Jul 3 at 8:52
  • Why can't you switch . resizable off? Commented Jul 3 at 9:53

1 Answer 1

0

You may be making the wrong assumption about resizable. The description of the styleMask option resizable says:

The window can be resized by the user.

So, it refers to User-resizability. If you don't set resizable, the user does not get a resize session when clicking the corners of the window, but you can still resize programmatically, with e.g. setContentSize(_:).
With this knowledge, you should be able to use your own resize implementation.

Here's a demo, in Swift:

import SwiftUI

@main
class AppDelegate: NSObject, NSApplicationDelegate {
    var window: NSWindow!
    var timer: Timer?

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        window = NSWindow(
            contentRect: NSRect(x: 0, y: 0, width: 800, height: 200),
            styleMask: [.titled, .closable],
            backing: .buffered,
            defer: false
        )
        
        window.title = "Custom Resize"
        window.center()
        window.makeKeyAndOrderFront(nil)
        
        let resizable = window.isResizable
        print("Window is \(resizable ? "resizable" : "not resizable")")
        
        timer = Timer.scheduledTimer(withTimeInterval: 2, repeats: false) { _ in
            self.window.setContentSize(NSSize(width: 200, height: 800))
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.