Add an iPhone client sharing the macOS app's SwiftUI code
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
import AppKit
|
||||
import ByreAPI
|
||||
import OpenAPIRuntime
|
||||
import SwiftUI
|
||||
@@ -206,10 +205,9 @@ struct Copyable<Content: View>: View {
|
||||
var body: some View {
|
||||
content
|
||||
.overlay(alignment: .topTrailing) {
|
||||
if hover {
|
||||
if hover || touchOnly {
|
||||
Button {
|
||||
NSPasteboard.general.clearContents()
|
||||
NSPasteboard.general.setString(text, forType: .string)
|
||||
copyToClipboard(text)
|
||||
copied = true
|
||||
Task {
|
||||
try? await Task.sleep(for: .milliseconds(1500))
|
||||
@@ -270,22 +268,41 @@ struct Spinner: View {
|
||||
}
|
||||
}
|
||||
|
||||
private struct SpinnerGlyphs: NSViewRepresentable {
|
||||
@MainActor
|
||||
private struct SpinnerGlyphs {
|
||||
let font: CTFont
|
||||
let color: Color
|
||||
|
||||
func update(_ v: SpinnerView, _ environment: EnvironmentValues) {
|
||||
v.font = font
|
||||
v.color = color.resolve(in: environment).cgColor
|
||||
v.relayout()
|
||||
}
|
||||
}
|
||||
|
||||
#if os(macOS)
|
||||
extension SpinnerGlyphs: NSViewRepresentable {
|
||||
func makeNSView(context: Context) -> SpinnerView {
|
||||
SpinnerView()
|
||||
}
|
||||
|
||||
func updateNSView(_ v: SpinnerView, context: Context) {
|
||||
v.font = font
|
||||
v.color = color.resolve(in: context.environment).cgColor
|
||||
v.needsLayout = true
|
||||
update(v, context.environment)
|
||||
}
|
||||
}
|
||||
#else
|
||||
extension SpinnerGlyphs: UIViewRepresentable {
|
||||
func makeUIView(context: Context) -> SpinnerView {
|
||||
SpinnerView()
|
||||
}
|
||||
|
||||
private final class SpinnerView: NSView {
|
||||
func updateUIView(_ v: SpinnerView, context: Context) {
|
||||
update(v, context.environment)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
private final class SpinnerView: PlatformView {
|
||||
private struct Key: Equatable {
|
||||
let size: CGSize
|
||||
let scale: CGFloat
|
||||
@@ -299,16 +316,30 @@ private final class SpinnerView: NSView {
|
||||
private let glyph = CALayer()
|
||||
private var drawn: Key?
|
||||
|
||||
override init(frame: NSRect) {
|
||||
override init(frame: CGRect) {
|
||||
super.init(frame: frame)
|
||||
#if os(macOS)
|
||||
wantsLayer = true
|
||||
layer?.addSublayer(glyph)
|
||||
#else
|
||||
isUserInteractionEnabled = false
|
||||
layer.addSublayer(glyph)
|
||||
#endif
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) is not supported")
|
||||
}
|
||||
|
||||
func relayout() {
|
||||
#if os(macOS)
|
||||
needsLayout = true
|
||||
#else
|
||||
setNeedsLayout()
|
||||
#endif
|
||||
}
|
||||
|
||||
#if os(macOS)
|
||||
override func viewDidChangeBackingProperties() {
|
||||
super.viewDidChangeBackingProperties()
|
||||
needsLayout = true
|
||||
@@ -316,10 +347,20 @@ private final class SpinnerView: NSView {
|
||||
|
||||
override func layout() {
|
||||
super.layout()
|
||||
draw(scale: convertToBacking(NSSize(width: 1, height: 1)).width)
|
||||
}
|
||||
#else
|
||||
override func layoutSubviews() {
|
||||
super.layoutSubviews()
|
||||
draw(scale: traitCollection.displayScale)
|
||||
}
|
||||
#endif
|
||||
|
||||
private func draw(scale: CGFloat) {
|
||||
guard let font, let color, bounds.width > 0, bounds.height > 0 else {
|
||||
return
|
||||
}
|
||||
let key = Key(size: bounds.size, scale: convertToBacking(NSSize(width: 1, height: 1)).width, font: font, color: color)
|
||||
let key = Key(size: bounds.size, scale: scale, font: font, color: color)
|
||||
if key == drawn {
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user