Add title to browse user activities

This commit is contained in:
Shadowfacts 2021-09-30 11:05:35 -04:00
parent 7dd4f3aa4c
commit 144695cc96
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
4 changed files with 39 additions and 17 deletions

View File

@ -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
}
}

View File

@ -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) {

View File

@ -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 {

View File

@ -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)
}
}