From eee22c13aa83accab95ff0edf2de2b6103ace072 Mon Sep 17 00:00:00 2001 From: brave-builds Date: Fri, 25 Oct 2024 22:47:21 +0000 Subject: [PATCH] Uplift of #26226 (squashed) to release --- .../BrowserViewController/BVC+Menu.swift | 6 ++- .../Menu/Bookmarks/BookmarkDetailsView.swift | 1 + .../Menu/History/HistoryView.swift | 9 +++- .../Frontend/Shields/ShieldsPanelView.swift | 14 ++++--- .../BraveShared/URLElidedTextView.swift | 41 +++++++++++++++++++ 5 files changed, 61 insertions(+), 10 deletions(-) create mode 100644 ios/brave-ios/Sources/BraveShared/URLElidedTextView.swift diff --git a/ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+Menu.swift b/ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+Menu.swift index bef118ce0fcb..f34c8414d1db 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+Menu.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+Menu.swift @@ -4,6 +4,7 @@ // file, You can obtain one at https://mozilla.org/MPL/2.0/. import BraveCore +import BraveShared import BraveUI import BraveVPN import BraveWallet @@ -412,8 +413,9 @@ extension BrowserViewController { .lineLimit(1) .foregroundColor(Color(.braveLabel)) } - Text( - verbatim: URLFormatter.formatURLOrigin( + + URLElidedText( + text: URLFormatter.formatURLOrigin( forDisplayOmitSchemePathAndTrivialSubdomains: url.absoluteString ) ) diff --git a/ios/brave-ios/Sources/Brave/Frontend/Browser/Toolbars/BottomToolbar/Menu/Bookmarks/BookmarkDetailsView.swift b/ios/brave-ios/Sources/Brave/Frontend/Browser/Toolbars/BottomToolbar/Menu/Bookmarks/BookmarkDetailsView.swift index 1cf579f5ef86..4a20205afe4d 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/Browser/Toolbars/BottomToolbar/Menu/Bookmarks/BookmarkDetailsView.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/Browser/Toolbars/BottomToolbar/Menu/Bookmarks/BookmarkDetailsView.swift @@ -21,6 +21,7 @@ private class URLTextField: UITextField { defaultTextAttributes[.paragraphStyle, default: NSParagraphStyle()] as! NSParagraphStyle let style = oldStyle.mutableCopy() as! NSMutableParagraphStyle style.lineBreakMode = .byTruncatingHead + style.baseWritingDirection = .leftToRight defaultTextAttributes[.paragraphStyle] = style self.addTarget(self, action: #selector(didBeginEditing), for: .editingDidBegin) diff --git a/ios/brave-ios/Sources/Brave/Frontend/Browser/Toolbars/BottomToolbar/Menu/History/HistoryView.swift b/ios/brave-ios/Sources/Brave/Frontend/Browser/Toolbars/BottomToolbar/Menu/History/HistoryView.swift index 0a4b1e909a9d..9b002b718173 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/Browser/Toolbars/BottomToolbar/Menu/History/HistoryView.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/Browser/Toolbars/BottomToolbar/Menu/History/HistoryView.swift @@ -4,10 +4,12 @@ // file, You can obtain one at https://mozilla.org/MPL/2.0/. import BraveCore +import BraveShared import BraveStrings import DesignSystem import Favicon import Preferences +import Shared import SwiftUI struct HistoryItemView: View { @@ -37,15 +39,18 @@ struct HistoryItemView: View { .foregroundStyle(Color(braveSystemName: .textPrimary)) } - Text( - URLFormatter.formatURLOrigin( + URLElidedText( + text: URLFormatter.formatURLOrigin( forDisplayOmitSchemePathAndTrivialSubdomains: url.absoluteString ) ) + .truncationMode(.tail) .font(.footnote) .frame(maxWidth: .infinity, alignment: .leading) .fixedSize(horizontal: false, vertical: true) .foregroundStyle(Color(braveSystemName: .textSecondary)) + .environment(\.layoutDirection, .leftToRight) + .flipsForRightToLeftLayoutDirection(false) } } } diff --git a/ios/brave-ios/Sources/Brave/Frontend/Shields/ShieldsPanelView.swift b/ios/brave-ios/Sources/Brave/Frontend/Shields/ShieldsPanelView.swift index 0cdf3ca85553..83faf1088c20 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/Shields/ShieldsPanelView.swift +++ b/ios/brave-ios/Sources/Brave/Frontend/Shields/ShieldsPanelView.swift @@ -4,6 +4,7 @@ // file, You can obtain one at https://mozilla.org/MPL/2.0/. import BraveCore +import BraveShared import BraveShields import BraveUI import Data @@ -35,9 +36,8 @@ struct ShieldsPanelView: View { self.url = url self.viewModel = ShieldsSettingsViewModel(tab: tab, domain: domain) self.actionCallback = callback - self.displayHost = URLFormatter.formatURLOrigin( - forDisplayOmitSchemePathAndTrivialSubdomains: url.absoluteString - ) + self.displayHost = + "\u{200E}\(URLFormatter.formatURLOrigin(forDisplayOmitSchemePathAndTrivialSubdomains: url.absoluteString))" } private var shieldsEnabledAccessibiltyLabel: String { @@ -82,7 +82,7 @@ struct ShieldsPanelView: View { url: url.absoluteString, isPrivateBrowsing: viewModel.isPrivateBrowsing ) - Text(displayHost) + URLElidedText(text: displayHost) .font(.title2) .foregroundStyle(Color(.bravePrimary)) } @@ -187,7 +187,9 @@ struct ShieldsPanelView: View { } @ViewBuilder private var shieldSettingsSectionView: some View { - ShieldSettingSectionHeader(title: displayHost) + ShieldSettingSectionHeader( + title: displayHost + ) ShieldSettingRow { HStack { Text(Strings.Shields.trackersAndAdsBlocking) @@ -309,7 +311,7 @@ private struct ShieldSettingSectionHeader: View { var body: some View { VStack(alignment: .leading, spacing: 8) { - Text(title) + URLElidedText(text: title) .font(.footnote) .foregroundStyle(Color(.secondaryBraveLabel)) .textCase(.uppercase) diff --git a/ios/brave-ios/Sources/BraveShared/URLElidedTextView.swift b/ios/brave-ios/Sources/BraveShared/URLElidedTextView.swift new file mode 100644 index 000000000000..e84f543012cb --- /dev/null +++ b/ios/brave-ios/Sources/BraveShared/URLElidedTextView.swift @@ -0,0 +1,41 @@ +// Copyright 2024 The Brave Authors. All rights reserved. +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at https://mozilla.org/MPL/2.0/. + +import BraveCore +import SwiftUI +import UIKit + +public struct URLElidedText: View { + public var text: String + + @Environment(\.font) + private var font: Font? + + @Environment(\.truncationMode) + private var truncationMode: Text.TruncationMode + + private var paragraphStyle: NSParagraphStyle { + let paragraphStyle = NSMutableParagraphStyle() + paragraphStyle.lineBreakMode = + truncationMode == .head + ? .byTruncatingHead : truncationMode == .middle ? .byTruncatingMiddle : .byTruncatingTail + paragraphStyle.baseWritingDirection = .leftToRight + return paragraphStyle + } + + public init(text: String) { + self.text = text + } + + public var body: some View { + // LRM character prevents Text elements from rendering RTL special characters + Text( + AttributedString( + "\u{200E}\(text)", + attributes: .init([.font: font ?? .body, .paragraphStyle: paragraphStyle]) + ) + ) + } +}