From 5f7aa1f2626458744a4508759c226723be695589 Mon Sep 17 00:00:00 2001 From: Abhishek Date: Mon, 28 Jun 2021 19:03:46 -0500 Subject: [PATCH] Update ParentPanelController+ModernSlider.swift --- .../ParentPanelController+ModernSlider.swift | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/Clocker/Panel/ParentPanelController+ModernSlider.swift b/Clocker/Panel/ParentPanelController+ModernSlider.swift index e00d65e..5609c85 100644 --- a/Clocker/Panel/ParentPanelController+ModernSlider.swift +++ b/Clocker/Panel/ParentPanelController+ModernSlider.swift @@ -16,8 +16,29 @@ extension ParentPanelController: NSCollectionViewDataSource { } extension ParentPanelController { + @IBAction func goForward(_: NSButton) { + navigateModernSliderToSpecificIndex(1) + } + + @IBAction func goBackward(_: NSButton) { + navigateModernSliderToSpecificIndex(-1) + } + + private func navigateModernSliderToSpecificIndex(_ index: Int) { + let contentView = modernSlider.superview as! NSClipView + let changedOrigin = contentView.documentVisibleRect.origin + let newPoint = NSPoint(x: changedOrigin.x + contentView.frame.width / 2, y: changedOrigin.y) + if let indexPath = modernSlider.indexPathForItem(at: newPoint) { + let previousIndexPath = IndexPath(item: indexPath.item + index, section: indexPath.section) + modernSlider.scrollToItems(at: Set([previousIndexPath]), scrollPosition: .centeredHorizontally) + } + } + @objc func collectionViewDidScroll(_ notification: NSNotification) { - let contentView = notification.object as! NSClipView + guard let contentView = notification.object as? NSClipView else { + return + } + let changedOrigin = contentView.documentVisibleRect.origin let newPoint = NSPoint(x: changedOrigin.x + contentView.frame.width / 2, y: changedOrigin.y) let indexPath = modernSlider.indexPathForItem(at: newPoint)