iOS: Add back/forward history context menus

This commit is contained in:
Shadowfacts 2020-09-29 23:36:24 -04:00
parent 01a3eaf17f
commit b000f1c2b3
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 46 additions and 0 deletions

View File

@ -71,10 +71,28 @@ public class NavigationManager: NSObject, ObservableObject {
currentURL = backStack.removeLast() currentURL = backStack.removeLast()
} }
public func back(count: Int) {
guard count <= backStack.count else { return }
var removed = backStack.suffix(count)
backStack.removeLast(count)
forwardStack.insert(currentURL, at: 0)
currentURL = removed.removeFirst()
forwardStack.insert(contentsOf: removed, at: 0)
}
@objc public func forward() { @objc public func forward() {
guard !forwardStack.isEmpty else { return } guard !forwardStack.isEmpty else { return }
backStack.append(currentURL) backStack.append(currentURL)
currentURL = forwardStack.removeFirst() currentURL = forwardStack.removeFirst()
} }
public func forward(count: Int) {
guard count <= forwardStack.count else { return }
var removed = forwardStack.prefix(count)
forwardStack.removeFirst(count)
backStack.append(currentURL)
currentURL = removed.removeLast()
backStack.append(contentsOf: removed)
}
} }

View File

@ -30,6 +30,15 @@ struct ToolBar: View {
Image(systemName: "arrow.left") Image(systemName: "arrow.left")
.font(.system(size: 24)) .font(.system(size: 24))
} }
.contextMenu {
ForEach(Array(navigator.backStack.suffix(5).enumerated()), id: \.1) { (index, url) in
Button {
navigator.back(count: min(5, navigator.backStack.count) - index)
} label: {
Text(verbatim: urlForDisplay(url))
}
}
}
.disabled(navigator.backStack.isEmpty) .disabled(navigator.backStack.isEmpty)
Spacer() Spacer()
@ -38,6 +47,15 @@ struct ToolBar: View {
Image(systemName: "arrow.right") Image(systemName: "arrow.right")
.font(.system(size: 24)) .font(.system(size: 24))
} }
.contextMenu {
ForEach(navigator.forwardStack.prefix(5).enumerated().reversed(), id: \.1) { (index, url) in
Button {
navigator.forward(count: index + 1)
} label: {
Text(verbatim: urlForDisplay(url))
}
}
}
.disabled(navigator.forwardStack.isEmpty) .disabled(navigator.forwardStack.isEmpty)
Spacer() Spacer()
@ -74,6 +92,16 @@ struct ToolBar: View {
PreferencesView(presented: $showPreferencesSheet) PreferencesView(presented: $showPreferencesSheet)
}) })
} }
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
}
} }
struct ToolBar_Previews: PreviewProvider { struct ToolBar_Previews: PreviewProvider {