diff --git a/BrowserCore/BrowserHelper.swift b/BrowserCore/BrowserHelper.swift new file mode 100644 index 0000000..27df633 --- /dev/null +++ b/BrowserCore/BrowserHelper.swift @@ -0,0 +1,24 @@ +// +// BrowserHelper.swift +// BrowserCore +// +// Created by Shadowfacts on 9/30/21. +// + +import Foundation + +public struct BrowserHelper { + private init() {} + + public static func urlForDisplay(_ url: URL) -> String { + var str = url.host! + if let port = url.port, + url.scheme != "gemini" || port != 1965 { + str += ":\(port)" + } + if url.path != "/" { + str += url.path + } + return str + } +} diff --git a/BrowserCore/NavigationManager.swift b/BrowserCore/NavigationManager.swift index 93ab83a..412a5cc 100644 --- a/BrowserCore/NavigationManager.swift +++ b/BrowserCore/NavigationManager.swift @@ -30,7 +30,6 @@ public class NavigationManager: NSObject, ObservableObject, Codable { return components.string! } -// public var titleForCurrentURL: String? private var currentHistoryEntry: HistoryEntry public init(url: URL) { diff --git a/Gemini-iOS/BrowserWebViewController.swift b/Gemini-iOS/BrowserWebViewController.swift index 944ede0..a4c2415 100644 --- a/Gemini-iOS/BrowserWebViewController.swift +++ b/Gemini-iOS/BrowserWebViewController.swift @@ -48,6 +48,10 @@ class BrowserWebViewController: UIViewController { userActivity = NSUserActivity(geminiURL: url) userActivity!.isEligibleForPrediction = true + userActivity!.title = BrowserHelper.urlForDisplay(url) + // set the persistent identifier to the url, so that we don't get duplicate shortcuts for the same url + // (at least, i think that's how it works) + userActivity!.persistentIdentifier = url.absoluteString } required init?(coder: NSCoder) { @@ -240,6 +244,11 @@ class BrowserWebViewController: UIViewController { if navigator.currentURL == doc.url { navigator.setTitleForCurrentURL(doc.title) } + if let title = doc.title { + DispatchQueue.main.async { + self.userActivity!.title = title + } + } let html = BrowserWebViewController.preamble + renderer.renderDocumentToHTML(doc) + BrowserWebViewController.postamble DispatchQueue.main.async { diff --git a/Gemini-iOS/ToolbarView.swift b/Gemini-iOS/ToolbarView.swift index 4030942..235f12f 100644 --- a/Gemini-iOS/ToolbarView.swift +++ b/Gemini-iOS/ToolbarView.swift @@ -136,16 +136,6 @@ class ToolbarView: UIView { border.backgroundColor = UIColor(white: traitCollection.userInterfaceStyle == .dark ? 0.25 : 0.75, alpha: 1) } - private func urlForDisplay(_ url: URL) -> String { - var str = url.host! - if let port = url.port, - url.scheme != "gemini" || port != 1965 { - str += ":\(port)" - } - str += url.path - return str - } - private func updateNavigationButtons() { backButton.isEnabled = navigator.backStack.count > 0 forwardsButton.isEnabled = navigator.forwardStack.count > 0 @@ -155,11 +145,11 @@ class ToolbarView: UIView { let backCount = min(5, navigator.backStack.count) - index if #available(iOS 15.0, *), let title = entry.title { - return UIAction(title: title, subtitle: urlForDisplay(entry.url)) { [unowned self] (_) in + return UIAction(title: title, subtitle: BrowserHelper.urlForDisplay(entry.url)) { [unowned self] (_) in self.navigator.back(count: backCount) } } else { - return UIAction(title: urlForDisplay(entry.url)) { [unowned self] (_) in + return UIAction(title: BrowserHelper.urlForDisplay(entry.url)) { [unowned self] (_) in self.navigator.back(count: backCount) } } @@ -170,11 +160,11 @@ class ToolbarView: UIView { let forwardCount = index + 1 if #available(iOS 15.0, *), let title = entry.title { - return UIAction(title: title, subtitle: urlForDisplay(entry.url)) { [unowned self] (_) in + return UIAction(title: title, subtitle: BrowserHelper.urlForDisplay(entry.url)) { [unowned self] (_) in self.navigator.forward(count: forwardCount) } } else { - return UIAction(title: urlForDisplay(entry.url)) { [unowned self] (_) in + return UIAction(title: BrowserHelper.urlForDisplay(entry.url)) { [unowned self] (_) in self.navigator.forward(count: forwardCount) } } @@ -203,7 +193,7 @@ extension ToolbarView: UIContextMenuInteractionDelegate { if interaction.view == backButton { return UIContextMenuConfiguration(identifier: nil, previewProvider: { nil }) { (_) -> UIMenu? in let children = self.navigator.backStack.suffix(5).enumerated().map { (index, entry) in - UIAction(title: self.urlForDisplay(entry.url)) { (_) in + UIAction(title: BrowserHelper.urlForDisplay(entry.url)) { (_) in self.navigator.back(count: min(5, self.navigator.backStack.count) - index) } } @@ -213,7 +203,7 @@ extension ToolbarView: UIContextMenuInteractionDelegate { return UIContextMenuConfiguration(identifier: nil, previewProvider: { nil }) { (_) -> UIMenu? in let children = self.navigator.forwardStack.prefix(5).enumerated().map { (index, entry) -> UIAction in let forwardCount = index + 1 - return UIAction(title: self.urlForDisplay(entry.url)) { (_) in + return UIAction(title: BrowserHelper.urlForDisplay(entry.url)) { (_) in self.navigator.forward(count: forwardCount) } }