Skip to content

Commit

Permalink
More improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
jotaemepereira committed Dec 15, 2024
1 parent 9406d58 commit 371be17
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import SwiftUI

struct TabBarRemoteMessageView: View {
@State private var presentPopup: Bool = false
@State private var hoverTimer: Timer?

let model: TabBarRemoteMessage
let onClose: () -> Void
let onTap: (URL) -> Void
let onHover: () -> Void
let onHoverEnd: () -> Void

var body: some View {
HStack {
Expand All @@ -36,56 +36,46 @@ struct TabBarRemoteMessageView: View {
enabled: true,
onClose: { onClose() },
onHoverStart: {
startHoverTimer()
onHover()
},
onHoverEnd: {
cancelHoverTimer()
onHoverEnd()
})
)
.frame(width: 147)
.popover(isPresented: $presentPopup, arrowEdge: .bottom) {
PopoverContent(model: model)
}
}
}

private func startHoverTimer() {
hoverTimer?.invalidate()
hoverTimer = Timer.scheduledTimer(withTimeInterval: 2.0, repeats: false) { _ in
presentPopup = true
}
}
}

private func cancelHoverTimer() {
hoverTimer?.invalidate()
presentPopup = false
struct TabBarRemoteMessagePopoverContent: View {
enum Constants {
static let height: CGFloat = 92
static let width: CGFloat = 360
}
}

struct PopoverContent: View {
let model: TabBarRemoteMessage

var body: some View {
HStack(alignment: .center) {
HStack(alignment: .center, spacing: 0) {
Image(.daxResponse)
.resizable()
.scaledToFit()
.frame(width: 72, height: 72)
.padding(.leading, 12)
.padding(.leading, 8)
.padding(.trailing, 16)

VStack(alignment: .leading) {
VStack(alignment: .leading, spacing: 0) {
Text(model.popupTitle)
.font(.system(size: 13, weight: .bold))
.padding(.bottom, 8)

Text(model.popupSubtitle)
.font(.system(size: 13, weight: .regular))
.font(.system(size: 13, weight: .medium))
}
.frame(width: 360, height: 92)
.padding(.trailing, 24)
.padding(.leading, 4)
.padding(.trailing, 12)
.padding([.bottom, .top], 10)
}
.frame(width: Constants.width, height: Constants.height)
}
}

Expand Down
54 changes: 44 additions & 10 deletions DuckDuckGo/TabBar/View/TabBarViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ final class TabBarViewController: NSViewController {
private let pinnedTabsView: PinnedTabsView?
private let pinnedTabsHostingView: PinnedTabsHostingView?
private let tabBarRemoteMessageViewModel: TabBarRemoteMessageViewModel
private let feedbackPopoverViewController: PopoverMessageViewController
private var tabBarRemoteMessagePopover: NSPopover?
private var tabBarRemoteMessagePopoverHoverTimer: Timer?
private var feedbackBarButtonHostingController: NSHostingController<TabBarRemoteMessageView>?

private var selectionIndexCancellable: AnyCancellable?
Expand Down Expand Up @@ -115,15 +116,6 @@ final class TabBarViewController: NSViewController {
self.pinnedTabsHostingView = nil
}

feedbackPopoverViewController = PopoverMessageViewController(
title: "Tell Us What You Think",
message: "Take our short survey and help us build the best browser.",
image: .daxResponse,
shouldShowCloseButton: false,
presentMultiline: true,
autoDismissDuration: nil
)

super.init(coder: coder)
}

Expand Down Expand Up @@ -243,7 +235,11 @@ final class TabBarViewController: NSViewController {
self.removeFeedbackButton()
},
onHover: {
self.startTabBarRemotMessageTimer(message: tabBarRemotMessage)
self.tabBarRemoteMessageViewModel.onUserHovered()
},
onHoverEnd: {
self.dismissTabBarRemoteMessagePopover()
}
)
feedbackBarButtonHostingController = NSHostingController(rootView: feedbackButtonView)
Expand All @@ -260,6 +256,44 @@ final class TabBarViewController: NSViewController {
])
}

private func startTabBarRemotMessageTimer(message: TabBarRemoteMessage) {
tabBarRemoteMessagePopoverHoverTimer?.invalidate()
tabBarRemoteMessagePopoverHoverTimer = Timer.scheduledTimer(withTimeInterval: 2.0, repeats: false) { _ in
self.showTabBarRemotePopup(message)
}
}

private func dismissTabBarRemoteMessagePopover() {
tabBarRemoteMessagePopoverHoverTimer?.invalidate()
tabBarRemoteMessagePopover?.close()
}

private func showTabBarRemotePopup(_ message: TabBarRemoteMessage) {
if let popover = tabBarRemoteMessagePopover {
guard let tabBarButtonRemoteMessageView = feedbackBarButtonHostingController?.view else {
return
}

popover.show(positionedBelow: tabBarButtonRemoteMessageView.bounds, in: tabBarButtonRemoteMessageView)
} else {
tabBarRemoteMessagePopover = NSPopover()
tabBarRemoteMessagePopover?.animates = true
tabBarRemoteMessagePopover?.behavior = .semitransient
tabBarRemoteMessagePopover?.contentSize = NSSize(width: TabBarRemoteMessagePopoverContent.Constants.width,
height: TabBarRemoteMessagePopoverContent.Constants.height)

let controller = NSViewController()
controller.view = NSHostingView(rootView: TabBarRemoteMessagePopoverContent(model: message))
tabBarRemoteMessagePopover?.contentViewController = controller

guard let tabBarButtonRemoteMessageView = feedbackBarButtonHostingController?.view else {
return
}

tabBarRemoteMessagePopover?.show(positionedBelow: tabBarButtonRemoteMessageView.bounds, in: tabBarButtonRemoteMessageView)
}
}

private func removeFeedbackButton() {
guard let hostingController = feedbackBarButtonHostingController else { return }

Expand Down

0 comments on commit 371be17

Please sign in to comment.