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 { private var scrollBody: some View {
MaybeLazyVStack(alignment: .leading) { MaybeLazyVStack(alignment: .leading) {
ForEach(blocks.indices) { (index) in ForEach(blocks.indices) { (index) in
RenderingBlockView(block: blocks[index], changeURL: changeURL) RenderingBlockView(document: document, block: blocks[index], changeURL: changeURL)
} }
}.padding([.leading, .trailing, .bottom]) }.padding([.leading, .trailing, .bottom])
} }

View File

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