NSPopOver & NSViewController - Drag to resize
Asked Answered
V

3

7

I am making a MenuBar app for which I am using NSPopOver. The problem is that NSPopover uses NSViewController as a contentViewController, whose size gets fixed. My requirement is to make the size of NSViewController flexible i.e just like NSWindowController(set the minimum size and maximum depends upon the total screen size). In simple words how to change the size of NSViewController(NSPopOver) when user drags it. I am new to OS X programming.

Viburnum answered 4/8, 2015 at 10:45 Comment(0)
V
11

I finally got it working by using Mouse Events. Just need to monitor the

override func mouseDown(theEvent: NSEvent) {}
override func mouseDragged(theEvent: NSEvent) {}

events, and reset the content size of the popOver. Hope this would be helpful to someone one day.

EDIT

override func mouseDragged(theEvent: NSEvent) {
        var currentLocation = NSEvent.mouseLocation()
        println("Dragged at : \(currentLocation)")

        var newOrigin   = currentLocation
        let screenFrame = NSScreen.mainScreen()?.frame
        var windowFrame = self.view.window?.frame

        newOrigin.x     = screenFrame!.size.width - currentLocation.x
        newOrigin.y     = screenFrame!.size.height - currentLocation.y

        println("the New Origin Points : \(newOrigin)")

        // Don't let window get dragged up under the menu bar
        if newOrigin.x < 450 {
            newOrigin.x = 450
        }

        if newOrigin.y < 650 {
            newOrigin.y = 650
        }

        println("the New Origin Points : \(newOrigin)")

        let appDelegate : AppDelegate = NSApplication.sharedApplication().delegate as! AppDelegate
        appDelegate.popover.contentSize = NSSize(width: newOrigin.x, height: newOrigin.y)

    }

This is how i tracked the mouse event. On Mouse drag just calculated the current position and the new position(To the point where user has dragged), then checked if the point is smaller then my default size for the Popover i.e. (450, 650) in this case. Once the point has been calculated, just set the size of the popover.

This is just a proposed way. There must be something better then this, but for time being this is what I did.

Viburnum answered 7/8, 2015 at 10:10 Comment(1)
Could you be more specific? I am new to programming. I made a MenuBar app with a popover and want to let users resize the popover view by dragging the corner. This post is only a clue i got.Eighteenth
N
5

I had the same need, and thanks to the helpful comments above, I created PopoverResize. This allows a user to resize a menubar NSPopover. It includes cursors for the edges as well.

https://github.com/dboydor/PopoverResize

Nightwalker answered 29/10, 2018 at 18:53 Comment(1)
Your great job is really appreciated @David Boyd! Thanks for sharing.Genuflect
M
1

Here is an answer that handles only vertical, but not horizontal resizing of an NSPopover.

    override func mouseDragged(with theEvent: NSEvent) {
        let currentLocation = NSEvent.mouseLocation()
        let screenFrame = NSScreen.main()?.frame

        var newY = screenFrame!.size.height - currentLocation.y

        if newY < MIN_HEIGHT {
            newY = MIN_HEIGHT
        }

        if newY > MAX_HEIGHT {
            newY = MAX_HEIGHT
        }

        let appDelegate : AppDelegate = NSApplication.shared().delegate as! AppDelegate
        appDelegate.popover.contentSize = NSSize(width: FIXED_WIDTH, height: newY)

     }
Maisonette answered 31/12, 2016 at 21:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.