From 29057c80241e1607cac8426aa9d4edf75192bc71 Mon Sep 17 00:00:00 2001 From: James Magahern Date: Wed, 31 Jul 2024 17:53:23 -0700 Subject: [PATCH] History menu improvements --- App/AppDelegate.swift | 4 +++- App/Backend/History/BrowserHistory.swift | 2 ++ App/Utilities/StringUtilities.swift | 26 ++++++++++++++++++++++++ SBrowser.xcodeproj/project.pbxproj | 4 ++++ 4 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 App/Utilities/StringUtilities.swift diff --git a/App/AppDelegate.swift b/App/AppDelegate.swift index 1dbbfce..a6215d2 100644 --- a/App/AppDelegate.swift +++ b/App/AppDelegate.swift @@ -34,7 +34,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate { private func historyMenu() -> UIMenu { let historyItems = BrowserHistory.shared.allHistory(limit: 50).map { item in - let title = (item.title.count > 0) ? item.title : item.url.absoluteString + let title = ((item.title.count > 0) ? item.title : item.url.absoluteString) + .middleTruncatedString(maximumLength: 85) + return UIAction(title: title) { action in UIApplication.shared.sendAction(#selector(ShortcutResponder.handleOpenURL), to: nil, from: action, for: OpenURLEvent(url: item.url)) } diff --git a/App/Backend/History/BrowserHistory.swift b/App/Backend/History/BrowserHistory.swift index 08da701..22f93b9 100644 --- a/App/Backend/History/BrowserHistory.swift +++ b/App/Backend/History/BrowserHistory.swift @@ -84,6 +84,8 @@ class BrowserHistory let nserror = error as NSError fatalError("Failed saving persistent entity to store: \(nserror), \(nserror.userInfo)") } + + UIMenuSystem.main.setNeedsRebuild() } public func viewModel(forFilterString filterString: String? = nil, limit: Int = 500) -> ViewModel { diff --git a/App/Utilities/StringUtilities.swift b/App/Utilities/StringUtilities.swift new file mode 100644 index 0000000..746b4f6 --- /dev/null +++ b/App/Utilities/StringUtilities.swift @@ -0,0 +1,26 @@ +// +// StringUtilities.swift +// App +// +// Created by James Magahern on 7/31/24. +// + +import Foundation + +extension String { + internal func middleTruncatedString(maximumLength: Int) -> String { + if maximumLength > self.count { + return self + } + + // Compute replacement range + let pivot = self.count / 2 + let truncationLength = self.count - maximumLength + + let pivotIndex = self.index(startIndex, offsetBy: pivot) + let startIndex = self.index(pivotIndex, offsetBy: -truncationLength / 2) + let endIndex = self.index(pivotIndex, offsetBy: truncationLength / 2) + + return replacingCharacters(in: startIndex..