Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 781fb84d7a | |||
| 994681e321 | |||
| 44c7d2b415 | |||
| 84579bccce |
+18
-10
@@ -43,13 +43,20 @@ public final class Menu {
|
||||
fadeIn(window)
|
||||
}
|
||||
|
||||
public func dismiss() {
|
||||
public func dismiss(animated: Bool) {
|
||||
let actualDismiss: (NSWindow) -> Void = { [weak self] menuWindow in
|
||||
self?.window?.parent?.removeChildWindow(menuWindow)
|
||||
self?.window?.orderOut(self)
|
||||
self?.window?.close()
|
||||
self?.window = nil
|
||||
}
|
||||
if let menuWindow = window {
|
||||
fadeOut(window: menuWindow) { [weak self] in
|
||||
self?.window?.parent?.removeChildWindow(menuWindow)
|
||||
self?.window?.orderOut(self)
|
||||
self?.window?.close()
|
||||
self?.window = nil
|
||||
if animated {
|
||||
fadeOut(window: menuWindow) {
|
||||
actualDismiss(menuWindow)
|
||||
}
|
||||
} else {
|
||||
actualDismiss(menuWindow)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,7 +91,7 @@ public final class Menu {
|
||||
|
||||
private func setupMonitors(for parentWindow: NSWindow, targetView: NSView) {
|
||||
lostFocusObserver = NotificationCenter.default.addObserver(forName: NSWindow.didResignKeyNotification, object: parentWindow, queue: nil, using: { [weak self] (_ arg1: Notification) -> Void in
|
||||
self?.dismiss()
|
||||
self?.dismiss(animated: false)
|
||||
})
|
||||
|
||||
localMonitor = EventMonitor(monitorType: .local, mask: [.leftMouseDown, .rightMouseDown, .otherMouseDown], globalHandler: nil, localHandler: { [weak self] event -> NSEvent? in
|
||||
@@ -92,7 +99,7 @@ public final class Menu {
|
||||
|
||||
if localEvent.window != self?.window {
|
||||
if localEvent.window == parentWindow {
|
||||
self?.dismiss()
|
||||
self?.dismiss(animated: true)
|
||||
// Ignore clicking on presenting view
|
||||
// let contentView = parentWindow.contentView
|
||||
// let locationTest = contentView?.convert(localEvent.locationInWindow, from: nil)
|
||||
@@ -112,8 +119,9 @@ public final class Menu {
|
||||
|
||||
let presentationFrame = presentationWindow.convertToScreen(view.frame)
|
||||
let presentationPoint = presentationFrame.origin
|
||||
let additionalYOffset = configuration.appearsBelowSender ? 0 : NSHeight(view.frame)
|
||||
|
||||
let newFrame = NSRect(x: presentationPoint.x, y: presentationPoint.y - NSHeight(window.frame), width: NSWidth(view.frame), height: NSHeight(window.frame))
|
||||
let newFrame = NSRect(x: presentationPoint.x, y: presentationPoint.y - NSHeight(window.frame) + additionalYOffset, width: NSWidth(view.frame), height: NSHeight(window.frame))
|
||||
window.setFrame(newFrame, display: true, animate: false)
|
||||
}
|
||||
}
|
||||
@@ -121,6 +129,6 @@ public final class Menu {
|
||||
extension Menu: ContentViewControllerDelegate {
|
||||
func didClickMenuElement(with index: Int) {
|
||||
selectedIndex = index
|
||||
dismiss()
|
||||
dismiss(animated: true)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,11 +16,21 @@ public enum Padding {
|
||||
public struct Horizontal {
|
||||
let left: CGFloat
|
||||
let right: CGFloat
|
||||
|
||||
public init(left: CGFloat, right: CGFloat) {
|
||||
self.left = left
|
||||
self.right = right
|
||||
}
|
||||
}
|
||||
|
||||
public struct Vertical {
|
||||
let top: CGFloat
|
||||
let bottom: CGFloat
|
||||
|
||||
public init(top: CGFloat, bottom: CGFloat) {
|
||||
self.top = top
|
||||
self.bottom = bottom
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,6 +52,7 @@ public protocol Configuration {
|
||||
var backgroundColor: NSColor { get }
|
||||
var cornerRadius: CGFloat { get }
|
||||
var hasShadow: Bool { get }
|
||||
var appearsBelowSender: Bool { get }
|
||||
var contentEdgeInsets: NSEdgeInsets { get }
|
||||
var separatorColor: NSColor { get }
|
||||
var separatorThickness: CGFloat { get }
|
||||
@@ -87,6 +98,10 @@ open class MenuConfiguration: Configuration {
|
||||
return true
|
||||
}
|
||||
|
||||
open var appearsBelowSender: Bool {
|
||||
return true
|
||||
}
|
||||
|
||||
open var contentEdgeInsets: NSEdgeInsets {
|
||||
return NSEdgeInsets(top: .grid2, left: .grid2, bottom: .grid2, right: .grid2)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user