From cde71c39ee4620731ffeca39f403185bc6e4163a Mon Sep 17 00:00:00 2001 From: Juan Manuel Pereira Date: Thu, 19 Dec 2024 10:51:26 -0300 Subject: [PATCH] Make popover resize automatically --- .../TabBarRemoteMessageView.swift | 35 +++++++++---------- .../View/TabBarRemoteMessagePresenting.swift | 7 ++-- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/DuckDuckGo/RemoteMessaging/TabBarRemoteMessageView.swift b/DuckDuckGo/RemoteMessaging/TabBarRemoteMessageView.swift index 76596d860f..4d9ed3e5fd 100644 --- a/DuckDuckGo/RemoteMessaging/TabBarRemoteMessageView.swift +++ b/DuckDuckGo/RemoteMessaging/TabBarRemoteMessageView.swift @@ -19,8 +19,8 @@ import SwiftUI struct TabBarRemoteMessageView: View { - @State private var isHovered: Bool = false - @State private var isButtonHovered: Bool = false + @State private var wasViewHovered: Bool = false + @State private var wasCloseButtonHovered: Bool = false let model: TabBarRemoteMessage @@ -45,21 +45,23 @@ struct TabBarRemoteMessageView: View { } .frame(width: 16, height: 16) .buttonStyle(PlainButtonStyle()) - .background(isButtonHovered + .background(wasCloseButtonHovered && !wasViewHovered ? Color("PrimaryButtonHover") : Color("PrimaryButtonRest")) .cornerRadius(2) .onHover { hovering in - isButtonHovered = hovering + wasCloseButtonHovered = hovering } } .padding(8) - .background(Color("PrimaryButtonRest")) + .background(wasViewHovered + ? Color("PrimaryButtonHover") + : Color("PrimaryButtonRest")) .frame(height: 24) .cornerRadius(8) .onAppear(perform: { onAppear() }) .onHover { hovering in - isHovered = hovering + wasViewHovered = hovering if hovering { onHover() @@ -71,33 +73,28 @@ struct TabBarRemoteMessageView: View { } struct TabBarRemoteMessagePopoverContent: View { - enum Constants { - static let height: CGFloat = 92 - static let width: CGFloat = 360 - } - let model: TabBarRemoteMessage var body: some View { - HStack(alignment: .center, spacing: 0) { + HStack(alignment: .center, spacing: 12) { Image(.daxResponse) .resizable() .scaledToFit() .frame(width: 72, height: 72) - .padding(.leading, 8) - .padding(.trailing, 16) - VStack(alignment: .leading, spacing: 0) { + VStack(alignment: .leading, spacing: 8) { Text(model.popupTitle) .font(.system(size: 13, weight: .bold)) - .padding(.bottom, 8) + .padding(.top, 9) Text(model.popupSubtitle) .font(.system(size: 13, weight: .medium)) + .padding(.bottom, 9) } - .padding(.trailing, 12) - .padding([.bottom, .top], 10) + .frame(width: 236) } - .frame(minWidth: Constants.width, minHeight: Constants.height) + .padding([.top, .bottom], 10) + .padding(.leading, 12) + .padding(.trailing, 24) } } diff --git a/DuckDuckGo/TabBar/View/TabBarRemoteMessagePresenting.swift b/DuckDuckGo/TabBar/View/TabBarRemoteMessagePresenting.swift index 8ced06d7ce..23c417bcc0 100644 --- a/DuckDuckGo/TabBar/View/TabBarRemoteMessagePresenting.swift +++ b/DuckDuckGo/TabBar/View/TabBarRemoteMessagePresenting.swift @@ -158,13 +158,12 @@ extension TabBarRemoteMessagePresenting { private func configurePopover(with message: TabBarRemoteMessage) { guard let popover = tabBarRemoteMessagePopover else { return } + let contentView = TabBarRemoteMessagePopoverContent(model: message) popover.animates = true popover.behavior = .semitransient - popover.contentSize = NSSize(width: TabBarRemoteMessagePopoverContent.Constants.width, - height: TabBarRemoteMessagePopoverContent.Constants.height) - + popover.contentSize = NSHostingView(rootView: contentView).fittingSize let controller = NSViewController() - controller.view = NSHostingView(rootView: TabBarRemoteMessagePopoverContent(model: message)) + controller.view = NSHostingView(rootView: contentView) popover.contentViewController = controller }