Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e976b0818f | |||
| 65abdd6244 | |||
| 781fb84d7a |
@@ -2,16 +2,17 @@
|
||||
|
||||
Fully customizable Mac OS drop-down menu
|
||||
|
||||

|
||||

|
||||
|
||||
## What can be customized? Everything!
|
||||
```swift
|
||||
public protocol Configuration {
|
||||
var titlePadding: Padding.Vertical { get }
|
||||
var titleBottomSpace: CGFloat { get }
|
||||
var titleFont: NSFont? { get }
|
||||
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 }
|
||||
|
||||
@@ -57,7 +57,7 @@ class ContentViewController: NSViewController {
|
||||
NSLayoutConstraint.activate([
|
||||
label.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: configuration.contentEdgeInsets.left),
|
||||
label.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -configuration.contentEdgeInsets.right),
|
||||
label.topAnchor.constraint(equalTo: view.topAnchor, constant: configuration.titlePadding.top),
|
||||
label.topAnchor.constraint(equalTo: view.topAnchor, constant: configuration.contentEdgeInsets.top)
|
||||
])
|
||||
}
|
||||
|
||||
@@ -67,13 +67,13 @@ class ContentViewController: NSViewController {
|
||||
NSLayoutConstraint.activate([
|
||||
stackView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
||||
stackView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
||||
stackView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
|
||||
stackView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -configuration.contentEdgeInsets.bottom)
|
||||
])
|
||||
|
||||
if let titleLabel = titleLabel {
|
||||
stackView.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: configuration.titlePadding.bottom).isActive = true
|
||||
stackView.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: configuration.titleBottomSpace).isActive = true
|
||||
} else {
|
||||
stackView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
|
||||
stackView.topAnchor.constraint(equalTo: view.topAnchor, constant: configuration.contentEdgeInsets.top).isActive = true
|
||||
}
|
||||
|
||||
menuItems.enumerated().forEach { index, item in
|
||||
|
||||
+15
-9
@@ -43,13 +43,19 @@ 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 = 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 +90,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 +98,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)
|
||||
@@ -122,6 +128,6 @@ public final class Menu {
|
||||
extension Menu: ContentViewControllerDelegate {
|
||||
func didClickMenuElement(with index: Int) {
|
||||
selectedIndex = index
|
||||
dismiss()
|
||||
dismiss(animated: true)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ extension Padding.Vertical {
|
||||
}
|
||||
|
||||
public protocol Configuration {
|
||||
var titlePadding: Padding.Vertical { get }
|
||||
var titleBottomSpace: CGFloat { get }
|
||||
var titleFont: NSFont? { get }
|
||||
var backgroundColor: NSColor { get }
|
||||
var cornerRadius: CGFloat { get }
|
||||
@@ -78,8 +78,8 @@ public protocol Configuration {
|
||||
open class MenuConfiguration: Configuration {
|
||||
public init() {}
|
||||
|
||||
open var titlePadding: Padding.Vertical {
|
||||
return .init(top: .grid1, bottom: .grid1)
|
||||
open var titleBottomSpace: CGFloat {
|
||||
return .grid1
|
||||
}
|
||||
|
||||
open var titleFont: NSFont? {
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
import Cocoa
|
||||
|
||||
final class Window: NSPanel {
|
||||
final class Window: NSWindow {
|
||||
private var childContentView: NSView?
|
||||
private var backgroundView: RoundedRectangleView?
|
||||
private let configuration: Configuration
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 315 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 326 KiB |
Reference in New Issue
Block a user