From 7bbcd7b25f505b32ec90a2b9686cde1231a889fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adem=20=C3=96zay?= Date: Tue, 22 Oct 2019 20:13:59 +0300 Subject: [PATCH] fix layout overflow issue of popover --- SpotMenu/AppDelegate/AppDelegate.swift | 23 +++++++++++++++++++---- SpotMenu/PopOver/PopOverVC.swift | 10 +++++++++- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/SpotMenu/AppDelegate/AppDelegate.swift b/SpotMenu/AppDelegate/AppDelegate.swift index 8c31806..b35fb0e 100644 --- a/SpotMenu/AppDelegate/AppDelegate.swift +++ b/SpotMenu/AppDelegate/AppDelegate.swift @@ -19,6 +19,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate { private enum Constants { static let statusItemIconLength: CGFloat = 30 static let statusItemLength: CGFloat = 250 + static let menubarHeight: CGFloat = 22 } // MARK: - Properties @@ -311,13 +312,27 @@ final class AppDelegate: NSObject, NSApplicationDelegate { } private func showPopover(_: AnyObject?) { + + let popoverWidth = PopOverViewController.Constants.width + let popoverHeight = PopOverViewController.Constants.height let rect = statusItem.button?.window?.convertToScreen((statusItem.button?.frame)!) - let menubarHeight = rect?.height ?? 22 - let height = hiddenController?.window?.frame.height ?? 300 - let xOffset = UserPreferences.fixPopoverToTheRight ? ((hiddenController?.window?.contentView?.frame.minX)! - (statusItem.button?.frame.minX)!) : ((hiddenController?.window?.contentView?.frame.midX)! - (statusItem.button?.frame.midX)!) - let x = (rect?.origin.x)! - xOffset + let menubarHeight = rect?.height ?? Constants.menubarHeight + let height = hiddenController?.window?.frame.height ?? popoverHeight + let xOffset = UserPreferences.fixPopoverToTheRight + ? ((hiddenController?.window?.contentView?.frame.minX)! - (statusItem.button?.frame.minX)!) + : ((hiddenController?.window?.contentView?.frame.midX)! - (statusItem.button?.frame.midX)!) + var x = (rect?.origin.x)! - xOffset let y = (rect?.origin.y)! // - (hiddenController?.contentViewController?.view.frame.maxY)! + + if let screen = NSScreen.main { + let width = x + popoverWidth + let maxWidth = screen.frame.size.width + if width > maxWidth { + x = maxWidth - popoverWidth + } + } + hiddenController?.window?.setFrameOrigin(NSPoint(x: x, y: y-height+menubarHeight)) hiddenController?.showWindow(self) eventMonitor?.start() diff --git a/SpotMenu/PopOver/PopOverVC.swift b/SpotMenu/PopOver/PopOverVC.swift index d1c6f72..4b6f489 100644 --- a/SpotMenu/PopOver/PopOverVC.swift +++ b/SpotMenu/PopOver/PopOverVC.swift @@ -29,13 +29,21 @@ final class PopOverViewController: NSViewController { @IBOutlet private var rightTime: NSTextField! @IBOutlet private var musicPlayerButton: NSButton! + public enum Constants { + static let width: CGFloat = 300 + static let height: CGFloat = 300 + } + // MARK: - Lifecycle methods override func viewDidLoad() { super.viewDidLoad() defaultImage = artworkImageView.image - preferredContentSize = NSSize(width: 300, height: 300) + preferredContentSize = NSSize( + width: Constants.width, + height: Constants.height + ) if #available(OSX 10.13, *) { view.layer?.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]