52 lines
1.3 KiB
Swift
52 lines
1.3 KiB
Swift
import SwiftUI
|
|
|
|
struct TextBlockView: View {
|
|
@EnvironmentObject private var bar: BarController
|
|
@ObservedObject var block: PHBlock
|
|
|
|
@State var hovering = false
|
|
|
|
private var adjustOffset: CGAffineTransform {
|
|
guard let offset = block.text.offset else { return .init() }
|
|
return .init(translationX: 0, y: offset)
|
|
}
|
|
|
|
var body: some View {
|
|
if let label = block.label, !label.isEmpty {
|
|
ZStack(alignment: .center) {
|
|
RoundedRectangle(cornerRadius: 0)
|
|
.foregroundStyle(block.style.background?.uiColor ?? .clear)
|
|
|
|
Group {
|
|
Text(label)
|
|
.font(block.text.font)
|
|
.italic(block.text.italic)
|
|
.foregroundStyle(block.style.foreground?.uiColor ?? .clear)
|
|
.transformEffect(adjustOffset)
|
|
.border(block.debug ? .blue : .clear)
|
|
}
|
|
.padding(.trailing, block.style.padding?.trailing ?? 0)
|
|
.padding(.leading, block.style.padding?.leading ?? 0)
|
|
}
|
|
.fixedSize(horizontal: true, vertical: false)
|
|
.border(block.debug ? .red : .clear)
|
|
.onHover { hovering = $0 }
|
|
.onAppear {
|
|
NSEvent.addLocalMonitorForEvents(matching: [
|
|
.leftMouseDown,
|
|
.rightMouseDown,
|
|
.otherMouseDown,
|
|
.scrollWheel,
|
|
]) { event in
|
|
if hovering, let gestureEvent = PHGestureEvent.from(event) {
|
|
block.handleGesture(gestureEvent)
|
|
}
|
|
return event
|
|
}
|
|
}
|
|
} else {
|
|
EmptyView()
|
|
}
|
|
}
|
|
}
|