Renderer: Add link icons

This commit is contained in:
Shadowfacts 2020-09-29 15:30:10 -04:00
parent 1449dc215b
commit 71b6352395
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
3 changed files with 48 additions and 14 deletions

View File

@ -35,7 +35,7 @@ public struct DocumentView: View {
private var scrollBody: some View {
MaybeLazyVStack(alignment: .leading) {
ForEach(blocks.indices) { (index) in
RenderingBlockView(block: blocks[index], changeURL: changeURL)
RenderingBlockView(document: document, block: blocks[index], changeURL: changeURL)
}
}.padding([.leading, .trailing, .bottom])
}

View File

@ -9,11 +9,13 @@ import SwiftUI
import GeminiFormat
struct RenderingBlockView: View {
let document: Document
let block: RenderingBlock
let changeURL: ((URL) -> Void)?
@State var hovering = false
init(block: RenderingBlock, changeURL: ((URL) -> Void)? = nil) {
init(document: Document, block: RenderingBlock, changeURL: ((URL) -> Void)? = nil) {
self.document = document
self.block = block
self.changeURL = changeURL
}
@ -51,25 +53,34 @@ struct RenderingBlockView: View {
let buttonStyle = PlainButtonStyle()
#endif
let imageName: String
if url.scheme != "gemini" {
imageName = "arrow.up.left.square"
} else if url.host == document.url.host {
imageName = "arrow.right"
} else {
imageName = "link"
}
let button: some View = Button {
self.changeURL?(url)
} label: {
Text(verbatim: text)
.font(.documentBody)
.foregroundColor(hovering ? .blue : Color.blue.opacity(0.8))
.underline()
.frame(maxWidth: .infinity, alignment: .leading)
HStack(alignment: .firstTextBaseline, spacing: 4) {
Image(systemName: imageName)
Text(verbatim: text)
.font(.documentBody)
.foregroundColor(hovering ? .blue : Color.blue.opacity(0.8))
.underline()
.frame(maxWidth: .infinity, alignment: .leading)
}
}
.buttonStyle(buttonStyle)
.onHover { hovering in
self.hovering = hovering
}
if #available(macOS 10.16, iOS 14.0, *) {
return AnyView(button.help(url.absoluteString))
} else {
return AnyView(button)
}
return button.maybeHelp(url.absoluteString)
}
private func preformatted(_ text: String) -> some View {
@ -109,10 +120,12 @@ struct RenderingBlockView: View {
}
struct RenderingBlockView_Previews: PreviewProvider {
static let doc = Document(url: URL(string: "gemini://localhost/test.gmi")!, lines: [.text("Some Text"), .quote("A Quote")])
static var previews: some View {
Group {
RenderingBlockView(block: .text("Some Text"))
RenderingBlockView(block: .quote("A Quote"))
RenderingBlockView(document: doc, block: .text("Some Text"))
RenderingBlockView(document: doc, block: .quote("A Quote"))
}
}
}

View File

@ -0,0 +1,21 @@
//
// View+Extensions.swift
// GeminiRenderer
//
// Created by Shadowfacts on 9/29/20.
//
import SwiftUI
extension View {
@ViewBuilder
func maybeHelp(_ help: String) -> some View {
if #available(iOS 14.0, macOS 11.0, *) {
self.help(help)
} else {
self
}
}
}