Sync source edition with VCC Plus 3.7.0 build 63
This commit is contained in:
1 parent
c652a5276d
commit
b2ecec4d87
86 files changed
+10519
-472
No files matched your search
@@ -140,12 +140,14 @@ enum ArchiveInlineContent: Sendable {
|
||||
struct ArchiveInlinePreview: View {
|
||||
let content: ArchiveInlineContent
|
||||
let title: String
|
||||
var position: LibraryReadingPosition? = nil
|
||||
var onPositionChange: (Double, Double, Double?) -> Void = { _,_,_ in }
|
||||
|
||||
var body: some View {
|
||||
switch content {
|
||||
case .image(let image): ArchiveInlineImage(image: image, title: title)
|
||||
case .text(let text): ArchiveInlineText(text: text, worksheet: false, title: title)
|
||||
case .worksheet(let text): ArchiveInlineText(text: text, worksheet: true, title: title)
|
||||
case .image(let image): ArchiveInlineImage(image: image, title: title, position: position, onPositionChange: onPositionChange)
|
||||
case .text(let text): ArchiveInlineText(text: text, worksheet: false, title: title, position: position, onPositionChange: onPositionChange)
|
||||
case .worksheet(let text): ArchiveInlineText(text: text, worksheet: true, title: title, position: position, onPositionChange: onPositionChange)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -153,8 +155,14 @@ struct ArchiveInlinePreview: View {
|
||||
private struct ArchiveInlineImage: View {
|
||||
let image: CGImage
|
||||
let title: String
|
||||
@State private var zoom = 0
|
||||
let position: LibraryReadingPosition?
|
||||
let onPositionChange: (Double, Double, Double?) -> Void
|
||||
@State private var zoom: Int
|
||||
|
||||
init(image: CGImage, title: String, position: LibraryReadingPosition?, onPositionChange: @escaping (Double, Double, Double?) -> Void) {
|
||||
self.image = image; self.title = title; self.position = position; self.onPositionChange = onPositionChange
|
||||
_zoom = State(initialValue: min(5,max(0,Int(log2(position?.zoom ?? 1)))))
|
||||
}
|
||||
var body: some View {
|
||||
VStack(spacing: 0) {
|
||||
HStack {
|
||||
@@ -166,18 +174,77 @@ private struct ArchiveInlineImage: View {
|
||||
Spacer()
|
||||
}.padding(6)
|
||||
Divider()
|
||||
GeometryReader { geometry in
|
||||
let width = CGFloat(image.width), height = CGFloat(image.height)
|
||||
let fit = min(1, min(geometry.size.width / width, geometry.size.height / height))
|
||||
let scale = fit * pow(2.0, CGFloat(zoom))
|
||||
ScrollView([.horizontal, .vertical]) {
|
||||
Image(image, scale: 1, label: Text(title))
|
||||
.resizable()
|
||||
.frame(width: width * scale, height: height * scale)
|
||||
.accessibilityIdentifier("archive-inline-image")
|
||||
.frame(minWidth: geometry.size.width, minHeight: geometry.size.height)
|
||||
}
|
||||
ArchiveImageSurface(image: image, title: title, zoom: zoom, initialPosition: position, onPositionChange: onPositionChange)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private struct ArchiveImageSurface: NSViewRepresentable {
|
||||
let image: CGImage
|
||||
let title: String
|
||||
let zoom: Int
|
||||
let initialPosition: LibraryReadingPosition?
|
||||
let onPositionChange: (Double, Double, Double?) -> Void
|
||||
func makeCoordinator() -> Coordinator { Coordinator() }
|
||||
func makeNSView(context: Context) -> NSScrollView {
|
||||
let scroll = NSScrollView()
|
||||
scroll.hasHorizontalScroller = true; scroll.hasVerticalScroller = true
|
||||
let imageView = NSImageView()
|
||||
imageView.image = NSImage(cgImage: image, size: NSSize(width: image.width, height: image.height))
|
||||
imageView.imageScaling = .scaleAxesIndependently
|
||||
imageView.setAccessibilityLabel(title)
|
||||
imageView.setAccessibilityIdentifier("archive-inline-image")
|
||||
let container = NSView()
|
||||
container.addSubview(imageView)
|
||||
scroll.documentView = container
|
||||
context.coordinator.imageView = imageView
|
||||
context.coordinator.scroll = scroll
|
||||
scroll.contentView.postsBoundsChangedNotifications = true
|
||||
context.coordinator.observer = NotificationCenter.default.addObserver(forName: NSView.boundsDidChangeNotification, object: scroll.contentView, queue: .main) { [weak coordinator = context.coordinator] _ in coordinator?.changed() }
|
||||
return scroll
|
||||
}
|
||||
func updateNSView(_ scroll: NSScrollView, context: Context) {
|
||||
let coordinator = context.coordinator
|
||||
coordinator.onPositionChange = onPositionChange; coordinator.imageSize = NSSize(width: image.width, height: image.height)
|
||||
coordinator.initialPosition = initialPosition; coordinator.zoom = zoom
|
||||
DispatchQueue.main.async { [weak coordinator] in coordinator?.layout() }
|
||||
}
|
||||
final class Coordinator {
|
||||
weak var scroll: NSScrollView?
|
||||
weak var imageView: NSImageView?
|
||||
var observer: NSObjectProtocol?
|
||||
var imageSize = NSSize.zero
|
||||
var initialPosition: LibraryReadingPosition?
|
||||
var zoom = 0
|
||||
var restored = false
|
||||
var layingOut = false
|
||||
var viewport = NSSize.zero
|
||||
var onPositionChange: (Double,Double,Double?) -> Void = { _,_,_ in }
|
||||
deinit { if let observer { NotificationCenter.default.removeObserver(observer) } }
|
||||
func layout() {
|
||||
guard let scroll, let image = scroll.documentView, imageSize.width > 0,
|
||||
scroll.contentSize.width > 0, scroll.contentSize.height > 0 else { return }
|
||||
layingOut = true
|
||||
viewport = scroll.contentSize
|
||||
let fit = min(1,min(scroll.contentSize.width/imageSize.width,scroll.contentSize.height/imageSize.height))
|
||||
let picture = NSSize(width:imageSize.width*fit*pow(2,Double(zoom)),height:imageSize.height*fit*pow(2,Double(zoom)))
|
||||
let size = NSSize(width:max(scroll.contentSize.width,picture.width),height:max(scroll.contentSize.height,picture.height))
|
||||
image.setFrameSize(size)
|
||||
imageView?.frame = NSRect(x:(size.width-picture.width)/2,y:(size.height-picture.height)/2,width:picture.width,height:picture.height)
|
||||
if !restored {
|
||||
restored = true
|
||||
let target = NSPoint(x:(initialPosition?.horizontalOffset ?? 0)*max(0,size.width-scroll.contentSize.width),y:(initialPosition?.offset ?? 0)*max(0,size.height-scroll.contentSize.height))
|
||||
scroll.contentView.scroll(to: target); scroll.reflectScrolledClipView(scroll.contentView)
|
||||
}
|
||||
layingOut = false
|
||||
changed()
|
||||
}
|
||||
func changed() {
|
||||
guard !layingOut, let scroll, let image = scroll.documentView else { return }
|
||||
if viewport != scroll.contentSize { layout(); return }
|
||||
let x = scroll.contentView.bounds.minX/max(1,image.bounds.width-scroll.contentSize.width)
|
||||
let y = scroll.contentView.bounds.minY/max(1,image.bounds.height-scroll.contentSize.height)
|
||||
onPositionChange(min(1,max(0,y)),min(1,max(0,x)),pow(2,Double(zoom)))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -186,9 +253,15 @@ private struct ArchiveInlineText: NSViewRepresentable {
|
||||
let text: String
|
||||
let worksheet: Bool
|
||||
let title: String
|
||||
let position: LibraryReadingPosition?
|
||||
let onPositionChange: (Double, Double, Double?) -> Void
|
||||
func makeCoordinator() -> Coordinator { Coordinator() }
|
||||
|
||||
func makeNSView(context: Context) -> NSScrollView {
|
||||
let scroll = NSTextView.scrollableTextView()
|
||||
scroll.contentView.postsBoundsChangedNotifications = true
|
||||
context.coordinator.scroll = scroll
|
||||
context.coordinator.observer = NotificationCenter.default.addObserver(forName: NSView.boundsDidChangeNotification, object: scroll.contentView, queue: .main) { [weak coordinator = context.coordinator] _ in coordinator?.changed() }
|
||||
guard let view = scroll.documentView as? NSTextView else { return scroll }
|
||||
view.isEditable = false
|
||||
view.isSelectable = true
|
||||
@@ -205,6 +278,7 @@ private struct ArchiveInlineText: NSViewRepresentable {
|
||||
|
||||
func updateNSView(_ scroll: NSScrollView, context: Context) {
|
||||
guard let view = scroll.documentView as? NSTextView else { return }
|
||||
context.coordinator.onPositionChange = onPositionChange
|
||||
view.setAccessibilityLabel(title)
|
||||
guard view.string != text else { return }
|
||||
view.string = text
|
||||
@@ -234,7 +308,24 @@ private struct ArchiveInlineText: NSViewRepresentable {
|
||||
view.textStorage?.addAttribute(.paragraphStyle, value: style, range: NSRange(location: 0, length: (text as NSString).length))
|
||||
}
|
||||
view.setSelectedRange(NSRange(location: 0, length: 0))
|
||||
scroll.contentView.scroll(to: .zero)
|
||||
scroll.reflectScrolledClipView(scroll.contentView)
|
||||
let initial = position
|
||||
DispatchQueue.main.async { [weak scroll] in
|
||||
guard let scroll, let view = scroll.documentView as? NSTextView else { return }
|
||||
view.layoutManager?.ensureLayout(for: view.textContainer!)
|
||||
let point = NSPoint(x:(initial?.horizontalOffset ?? 0)*max(0,view.bounds.width-scroll.contentSize.width),y:(initial?.offset ?? 0)*max(0,view.bounds.height-scroll.contentSize.height))
|
||||
scroll.contentView.scroll(to: point); scroll.reflectScrolledClipView(scroll.contentView)
|
||||
}
|
||||
}
|
||||
final class Coordinator {
|
||||
weak var scroll: NSScrollView?
|
||||
var observer: NSObjectProtocol?
|
||||
var onPositionChange: (Double,Double,Double?) -> Void = { _,_,_ in }
|
||||
deinit { if let observer { NotificationCenter.default.removeObserver(observer) } }
|
||||
func changed() {
|
||||
guard let scroll, let view = scroll.documentView else { return }
|
||||
let y = scroll.contentView.bounds.minY/max(1,view.bounds.height-scroll.contentSize.height)
|
||||
let x = scroll.contentView.bounds.minX/max(1,view.bounds.width-scroll.contentSize.width)
|
||||
onPositionChange(min(1,max(0,y)),min(1,max(0,x)),nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user