From 622cad7feb00a6dde315446861d8c44cb1e1a292 Mon Sep 17 00:00:00 2001 From: Ivan Sapozhnik Date: Thu, 14 May 2020 19:17:48 +0200 Subject: [PATCH] Appearance --- Sources/Menu/Menu.swift | 23 +++++++++++++++++++++-- Sources/Menu/MenuConfiguration.swift | 12 ++++++++++++ Sources/Menu/MenuElement.swift | 10 ++++++---- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/Sources/Menu/Menu.swift b/Sources/Menu/Menu.swift index c22f059..9e42614 100644 --- a/Sources/Menu/Menu.swift +++ b/Sources/Menu/Menu.swift @@ -65,6 +65,14 @@ public final class Menu { item.action?() } + public func selectItem(withTitle title: String) { + guard let item = items.first(where: { item -> Bool in + item.title == title + }) else { return } + selectedId = item.id + item.action?() + } + // MARK: - Adding and Removing Menu Items public func insertItem(_ item: MenuItem, at index: Int) { items.insert(item, at: index) @@ -218,12 +226,23 @@ public final class Menu { guard let parentWindow = view.window, let topMostSuperView = parentWindow.contentView else { return } let locationInWindow = view.convert(topMostSuperView.frame.origin, to: nil) - let rectInWindow = NSRect(origin: locationInWindow, size: CGSize(width: NSWidth(view.frame), height: NSHeight(window.frame))) + let rectInWindow = NSRect(origin: locationInWindow, size: CGSize(width: NSWidth(view.frame), height: NSHeight(view.frame))) let rectInScreen = parentWindow.convertToScreen(rectInWindow) let origin = rectInScreen.origin var additionalYOffset = configuration.appearsBelowSender ? NSHeight(view.frame) : 0 additionalYOffset += abs(configuration.presentingOffset) - let newFrame = NSRect(x: origin.x, y: origin.y - NSHeight(window.frame) - additionalYOffset, width: NSWidth(view.frame), height: NSHeight(window.frame)) + + let newFrame: NSRect + switch configuration.appearancePosition { + case .rightBottom: + newFrame = NSRect(x: origin.x, y: origin.y - NSHeight(window.frame) - additionalYOffset, width: NSWidth(view.frame), height: NSHeight(window.frame)) + case .rightTop: + newFrame = NSRect(x: origin.x, y: origin.y + additionalYOffset, width: NSWidth(view.frame), height: NSHeight(window.frame)) + case .leftTop: + newFrame = NSRect(x: origin.x - NSWidth(window.frame) + NSWidth(view.frame), y: origin.y - additionalYOffset, width: NSWidth(view.frame), height: NSHeight(window.frame)) + case .leftBottom: + newFrame = NSRect(x: origin.x - NSWidth(window.frame) + NSWidth(view.frame), y: origin.y - NSHeight(window.frame) - additionalYOffset, width: NSWidth(view.frame), height: NSHeight(window.frame)) + } window.setFrame(newFrame, display: true, animate: false) } diff --git a/Sources/Menu/MenuConfiguration.swift b/Sources/Menu/MenuConfiguration.swift index be1116c..8054cdc 100644 --- a/Sources/Menu/MenuConfiguration.swift +++ b/Sources/Menu/MenuConfiguration.swift @@ -12,6 +12,13 @@ public enum Alignment { case right } +public enum Position { + case leftBottom + case leftTop + case rightTop + case rightBottom +} + public enum Padding { public struct Horizontal { let left: CGFloat @@ -54,6 +61,7 @@ public protocol Configuration { var cornerRadius: CGFloat { get } var hasShadow: Bool { get } var appearsBelowSender: Bool { get } + var appearancePosition: Position { get } var presentingOffset: CGFloat { get } var animationDuration: TimeInterval { get } var contentEdgeInsets: NSEdgeInsets { get } @@ -114,6 +122,10 @@ open class MenuConfiguration: Configuration { return true } + open var appearancePosition: Position { + return .rightBottom + } + open var presentingOffset: CGFloat { return 0.0 } diff --git a/Sources/Menu/MenuElement.swift b/Sources/Menu/MenuElement.swift index a9fbff0..d46eaf8 100644 --- a/Sources/Menu/MenuElement.swift +++ b/Sources/Menu/MenuElement.swift @@ -208,10 +208,12 @@ class MenuElement: NSView { control.isEnabled = isEnabled control.hover = { [weak self] isHover in guard let self = self else { return } - label.textColor = isHover ? self.configuration.menuItemHoverTextColor : isSelected ? self.configuration.menuItemHoverImageTintColor : self.configuration.menuItemTextColor - if #available(OSX 10.14, *) { - leftImageView?.contentTintColor = isHover ? self.configuration.menuItemHoverImageTintColor : isSelected ? self.configuration.menuItemHoverImageTintColor : self.configuration.menuItemImageTintColor - rightImageView?.contentTintColor = isHover ? self.configuration.menuItemHoverImageTintColor : isSelected ? self.configuration.menuItemHoverImageTintColor : self.configuration.menuItemImageTintColor + if self.configuration.rememberSelection { + label.textColor = isHover ? self.configuration.menuItemHoverTextColor : isSelected ? self.configuration.menuItemHoverImageTintColor : self.configuration.menuItemTextColor + if #available(OSX 10.14, *) { + leftImageView?.contentTintColor = isHover ? self.configuration.menuItemHoverImageTintColor : isSelected ? self.configuration.menuItemHoverImageTintColor : self.configuration.menuItemImageTintColor + rightImageView?.contentTintColor = isHover ? self.configuration.menuItemHoverImageTintColor : isSelected ? self.configuration.menuItemHoverImageTintColor : self.configuration.menuItemImageTintColor + } } }