Skip to content
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

Fixes the multiline text heights #1427

Merged
merged 7 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,11 @@ public final class NetworkProtectionPopover: NSPopover {
statusReporter: statusReporter,
menuItems: menuItems)

let controller: NSViewController
Copy link
Contributor Author

@diegoreymendez diegoreymendez Aug 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was no need to separate the declaration of controller in this line and then assign it in line 76, so I unified both.


let view = NetworkProtectionStatusView(model: model).environment(\.dismiss, { [weak self] in
self?.close()
}).fixedSize()
.padding(.vertical, 10)
Copy link
Contributor Author

@diegoreymendez diegoreymendez Aug 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was removed thanks to the multiline-text height fixes, which were causing the popover height to be off.

controller = NSHostingController(rootView: view)

let controller = NSHostingController(rootView: view)
contentViewController = controller
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ public struct NetworkProtectionStatusView: View {
HStack(spacing: 0) {
Text(message)
.makeSelectable()
.multilineText()
.foregroundColor(defaultTextColor)

Spacer()
Expand All @@ -219,8 +220,9 @@ public struct NetworkProtectionStatusView: View {
.padding([.top], 8)

Text(UserText.networkProtectionStatusViewFeatureDesc)
.applyDescriptionAttributes(colorScheme: colorScheme)
.multilineText()
.multilineTextAlignment(.center)
.applyDescriptionAttributes(colorScheme: colorScheme)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure it was necessary to move this below, but I wanted the first thing to happen to be the fixing of the height.

.fixedSize(horizontal: false, vertical: true)
.padding(EdgeInsets(top: 8, leading: 16, bottom: 16, trailing: 16))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// MultilineTextHeightFixer.swift
//
// Copyright © 2023 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 Combine
import Foundation
import SwiftUI

/// Fixes the height for multiline text fields, which seem to suffer from a layout issue where
/// their height isn't properly honored.
///
private struct MultilineTextHeightFixer: ViewModifier {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We implemented this with @brindy 's help. We discussed it's probably reusable but that it may be best to leave it here for now.

@State var textHeight: CGFloat?

func body(content: Content) -> some View {
content
.background(
GeometryReader { geometry in
Color.clear // This is just to have something to attach .onReceive to.
.onReceive(Just(geometry.size)) { _ in
textHeight = geometry.size.height
}
})
.frame(height: textHeight)
}
}

extension View {

/// Meant to be used for multiline-text. This is currently only applying a modifier
///
@ViewBuilder
func multilineText() -> some View {
self.fixedSize(horizontal: false, vertical: true)
.modifier(MultilineTextHeightFixer())
}
}
Loading