-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dismiss Pinned Tab Previews when exiting full screen or hovering the semaphore buttons #2788
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// | ||
// Publishers.withLatestFrom.swift | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
import Combine | ||
|
||
// More Info: | ||
// - RXMarbles: https://rxmarbles.com/#withLatestFrom | ||
// - https://jasdev.me/notes/with-latest-from | ||
extension Publisher { | ||
|
||
/// Upon an emission from self, emit the latest value from the | ||
/// second publisher, if any exists. | ||
/// | ||
/// - parameter other: A second publisher source. | ||
/// | ||
/// - returns: A publisher containing the latest value from the second publisher, if any. | ||
func withLatestFrom<Other: Publisher>( | ||
_ other: Other | ||
) -> AnyPublisher<Other.Output, Other.Failure> where Failure == Other.Failure { | ||
withLatestFrom(other) { _, otherValue in otherValue } | ||
} | ||
|
||
/// Merges two publishers into a single publisher by combining each value | ||
/// from self with the latest value from the second publisher, if any. | ||
/// | ||
/// - parameter other: A second publisher source. | ||
/// - parameter resultSelector: Function to invoke for each value from the self combined | ||
/// with the latest value from the second source, if any. | ||
/// | ||
/// - returns: A publisher containing the result of combining each value of the self | ||
/// with the latest value from the second publisher, if any, using the | ||
/// specified result selector function. | ||
func withLatestFrom<Other: Publisher, Result>( | ||
_ other: Other, | ||
resultSelector: @escaping (Output, Other.Output) -> Result | ||
) -> AnyPublisher<Result, Failure> where Other.Failure == Failure { | ||
let upstream = share() | ||
|
||
return other | ||
.map { second in | ||
upstream.map { | ||
resultSelector($0, second) | ||
} | ||
} | ||
.switchToLatest() | ||
.zip(upstream) // `zip`ping and discarding `\.1` allows for upstream completions to be projected down immediately. | ||
.map(\.0) | ||
.eraseToAnyPublisher() | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,6 +65,8 @@ final class PinnedTabsViewModel: ObservableObject { | |
} | ||
} | ||
|
||
@Published var mouseMoving: Void = () | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the beginning I was passing setting the CGPoint where the mouse location was, but I wasn’t really using it so I changed to Void. If it feels weird we can always change to CGPoint as we may need it in the future we need it |
||
|
||
@Published var shouldDrawLastItemSeparator: Bool = true { | ||
didSet { | ||
updateItemsWithoutSeparator() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,9 +56,13 @@ struct PinnedTabView: View { | |
} | ||
|
||
if controlActiveState == .key { | ||
stack.onHover { [weak collectionModel, weak model] isHovered in | ||
collectionModel?.hoveredItem = isHovered ? model : nil | ||
} | ||
stack | ||
.onHover { [weak collectionModel, weak model] isHovered in | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An approach I tried was to make an operator called |
||
collectionModel?.hoveredItem = isHovered ? model : nil | ||
} | ||
.onMouseMoving { [weak collectionModel] in | ||
collectionModel?.mouseMoving = () | ||
} | ||
} else { | ||
stack | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,6 +60,8 @@ final class TabBarViewController: NSViewController { | |
private var mouseDownCancellable: AnyCancellable? | ||
private var cancellables = Set<AnyCancellable>() | ||
|
||
private let tabPreviewEventsHandler: TabPreviewEventsHandler | ||
|
||
@IBOutlet weak var shadowView: TabShadowView! | ||
|
||
@IBOutlet weak var rightSideStackView: NSStackView! | ||
|
@@ -89,10 +91,18 @@ final class TabBarViewController: NSViewController { | |
self.pinnedTabsViewModel = pinnedTabsViewModel | ||
self.pinnedTabsView = pinnedTabsView | ||
self.pinnedTabsHostingView = PinnedTabsHostingView(rootView: pinnedTabsView) | ||
tabPreviewEventsHandler = TabPreviewEventsHandler( | ||
pinnedTabHoveredIndexPublisher: pinnedTabsViewModel.$hoveredItemIndex.eraseToAnyPublisher(), | ||
pinnedTabMouseMovingPublisher: pinnedTabsViewModel.$mouseMoving.eraseToAnyPublisher() | ||
) | ||
} else { | ||
self.pinnedTabsViewModel = nil | ||
self.pinnedTabsView = nil | ||
self.pinnedTabsHostingView = nil | ||
tabPreviewEventsHandler = TabPreviewEventsHandler( | ||
pinnedTabHoveredIndexPublisher: Just(nil).eraseToAnyPublisher(), | ||
pinnedTabMouseMovingPublisher: Just(()).eraseToAnyPublisher() | ||
) | ||
} | ||
|
||
super.init(coder: coder) | ||
|
@@ -107,6 +117,7 @@ final class TabBarViewController: NSViewController { | |
setupFireButton() | ||
setupPinnedTabsView() | ||
subscribeToTabModeChanges() | ||
subscribeToTabPreviewEvents() | ||
setupAddTabButton() | ||
setupAsBurnerWindowIfNeeded() | ||
} | ||
|
@@ -245,13 +256,6 @@ final class TabBarViewController: NSViewController { | |
} | ||
.store(in: &cancellables) | ||
|
||
pinnedTabsViewModel.$hoveredItemIndex.dropFirst().removeDuplicates() | ||
.debounce(for: 0.05, scheduler: DispatchQueue.main) | ||
.sink { [weak self] index in | ||
self?.pinnedTabsViewDidUpdateHoveredItem(to: index) | ||
} | ||
.store(in: &cancellables) | ||
|
||
pinnedTabsViewModel.contextMenuActionPublisher | ||
.sink { [weak self] action in | ||
self?.handlePinnedTabContextMenuAction(action) | ||
|
@@ -278,16 +282,21 @@ final class TabBarViewController: NSViewController { | |
.store(in: &cancellables) | ||
} | ||
|
||
private func pinnedTabsViewDidUpdateHoveredItem(to index: Int?) { | ||
if let index = index { | ||
showPinnedTabPreview(at: index) | ||
} else { | ||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { | ||
if self.view.isMouseLocationInsideBounds() == false { | ||
self.hideTabPreview(allowQuickRedisplay: true) | ||
private func subscribeToTabPreviewEvents() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method receive events to show both pinned tabs and non pinned tabs. |
||
tabPreviewEventsHandler.eventPublisher | ||
.debounce(for: 0.05, scheduler: DispatchQueue.main) | ||
.sink { [weak self] event in | ||
guard let self else { return } | ||
switch event { | ||
case let .hide(allowQuickRedisplay, withDelay): | ||
self.hideTabPreview(allowQuickRedisplay: allowQuickRedisplay, withDelay: withDelay) | ||
case let .show(.pinned(index)): | ||
self.showPinnedTabPreview(at: index) | ||
case let .show(.unpinned(item)): | ||
self.showTabPreview(for: item) | ||
} | ||
} | ||
} | ||
.store(in: &cancellables) | ||
} | ||
|
||
private func deselectTabAndSelectPinnedTab(at index: Int) { | ||
|
@@ -613,8 +622,8 @@ final class TabBarViewController: NSViewController { | |
tabPreviewWindowController.show(parentWindow: window, topLeftPointInWindow: pointInWindow) | ||
} | ||
|
||
func hideTabPreview(allowQuickRedisplay: Bool = false) { | ||
tabPreviewWindowController.hide(allowQuickRedisplay: allowQuickRedisplay) | ||
func hideTabPreview(allowQuickRedisplay: Bool = false, withDelay: Bool = false) { | ||
tabPreviewWindowController.hide(allowQuickRedisplay: allowQuickRedisplay, withDelay: withDelay) | ||
} | ||
|
||
} | ||
|
@@ -1020,15 +1029,17 @@ extension TabBarViewController: NSCollectionViewDelegate { | |
extension TabBarViewController: TabBarViewItemDelegate { | ||
|
||
func tabBarViewItem(_ tabBarViewItem: TabBarViewItem, isMouseOver: Bool) { | ||
guard !isMouseOver else { return } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When the mouse exit the area we dismiss the preview |
||
|
||
if isMouseOver { | ||
// Show tab preview for visible tab bar items | ||
if collectionView.visibleRect.intersects(tabBarViewItem.view.frame) { | ||
showTabPreview(for: tabBarViewItem) | ||
} | ||
} else { | ||
tabPreviewWindowController.hide(allowQuickRedisplay: true, withDelay: true) | ||
} | ||
// Send an event to dismiss the preview when the mouse exits the area | ||
tabPreviewEventsHandler.unpinnedTabMouseExited() | ||
} | ||
|
||
func tabBarViewItemMouseIsMoving(_ tabBarViewItem: TabBarViewItem) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When the mouse moves within the tab we show the preview |
||
guard collectionView.visibleRect.intersects(tabBarViewItem.view.frame) else { return } | ||
|
||
// Send an event to show the preview when the mouse moves within the area | ||
tabPreviewEventsHandler.unpinnedTabMouseEntered(tabBarViewItem: tabBarViewItem) | ||
} | ||
|
||
func tabBarViewItemCanBeDuplicated(_ tabBarViewItem: TabBarViewItem) -> Bool { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could also import CombineExt package which has same implementation by jasdev but if we’re not using the whole set of operators, probably no need it.