Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions swiftui/PyMOLViewer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@
FCE3C0AD3EC5319F8F2E144E /* DesignEditInferenceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D6A959CC1590FC60333582B /* DesignEditInferenceTests.swift */; };
FE667ADF3DAD102C12DC4DA2 /* HoverReadoutTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BF1264ED4865B293F313315 /* HoverReadoutTests.swift */; };
FF325D5404E69D05274EEB81 /* SelectionInputField.swift in Sources */ = {isa = PBXBuildFile; fileRef = D55EF2F596852510DDACA576 /* SelectionInputField.swift */; };
FFF826842E1FE0AF110953EB /* TimelineLaneLayoutTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7BBDEC9D3E4B71F8249F6263 /* TimelineLaneLayoutTests.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -475,6 +476,7 @@
786939619F7871E32E2BB123 /* MCPDesktopInstaller.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MCPDesktopInstaller.swift; sourceTree = "<group>"; };
79E014A631DA05BA1F8C7E8A /* LightsEditing.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LightsEditing.swift; sourceTree = "<group>"; };
7AD9869000B548D9F7CDF9DE /* MLXRuntimeTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MLXRuntimeTests.swift; sourceTree = "<group>"; };
7BBDEC9D3E4B71F8249F6263 /* TimelineLaneLayoutTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimelineLaneLayoutTests.swift; sourceTree = "<group>"; };
7D10B5CB9ECF81346BD9CAE8 /* whatsnew-190-raymolrc.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "whatsnew-190-raymolrc.png"; sourceTree = "<group>"; };
7D3E805AC2D63B01AB7EE29C /* Theme.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Theme.swift; sourceTree = "<group>"; };
7DEAB18A89F026148528C71E /* PendingJobTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PendingJobTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -887,6 +889,7 @@
55F4F52D951CF1F96606F41C /* SmokeTests.swift */,
82497826B2E619BC2235983B /* SurfacePickTests.swift */,
B945DAC97919F57996278E1F /* TempChannelTests.swift */,
7BBDEC9D3E4B71F8249F6263 /* TimelineLaneLayoutTests.swift */,
0C4E3216DAFC42C515FD0C3E /* WeightsFetchStateTests.swift */,
);
path = PyMOLViewerTests;
Expand Down Expand Up @@ -1764,6 +1767,7 @@
5CAB662574D238626A8F4D68 /* SmokeTests.swift in Sources */,
3FD53A37F2DD94FC1C411A4C /* SurfacePickTests.swift in Sources */,
6C43C62D56480ABE7BA72AFD /* TempChannelTests.swift in Sources */,
FFF826842E1FE0AF110953EB /* TimelineLaneLayoutTests.swift in Sources */,
047FD10B437F95938AB4B3B0 /* WeightsFetchStateTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
48 changes: 43 additions & 5 deletions swiftui/PyMOLViewer/Panels/TimelinePanel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ struct TimelinePanel: View {
// (issue #131). Set from GeometryReader preferences below.
@State private var paletteContentW: CGFloat = 0
@State private var paletteViewportW: CGFloat = 0
// The lane's viewport width, so the track row knows whether the lane
// overflows (and so shows a legacy scroll bar) — see laneScrollerH.
@State private var laneViewportW: CGFloat = 0
#if os(macOS)
// AppKit draws legacy (always-visible) scroll bars: "Show scroll bars:
// Always", or "Automatically" with a mouse attached (CI runners and VMs too).
@State private var legacyScrollers = NSScroller.preferredScrollerStyle == .legacy
#endif

#if os(iOS)
@Environment(\.horizontalSizeClass) private var hSize
Expand All @@ -92,6 +100,21 @@ struct TimelinePanel: View {
private let laneH: CGFloat = 56
private let rulerH: CGFloat = 28
private var labelW: CGFloat { isCompact ? 34 : 90 } // wide enough for "Scenes" without wrapping
// Room for the lane's horizontal scroll bar below the ruler + items. A legacy
// scroller sits UNDER the content (17 pt), and SwiftUI grows the backing
// NSScrollView past the lane's .frame(height:), centred: the ruler slid
// 8.5 pt up into the header and the bar hung over the scenes strip. Overlay
// scrollers float over the content, and AppKit hides the bar while the lane
// fits, so neither needs room.
private var laneScrollerH: CGFloat {
#if os(macOS)
guard legacyScrollers, laneViewportW > 0,
contentWidth(laneViewportW, pps: ppsFor(laneViewportW)) > laneViewportW else { return 0 }
return NSScroller.scrollerWidth(for: .regular, scrollerStyle: .legacy)
#else
return 0
#endif
}
private let laneSpace = "timelineLane" // coord space for drag/tap -> frame
private let minZoom: CGFloat = 0.05
private let maxZoom: CGFloat = 8.0
Expand Down Expand Up @@ -340,7 +363,9 @@ struct TimelinePanel: View {
// MARK: - The unified lane (fixed-scale, scrollable)

private var tracksSection: some View {
HStack(spacing: 0) {
// Top-aligned: the gutter stays level with the ruler + items, and any
// legacy scroll bar band (laneScrollerH) is left below it.
HStack(alignment: .top, spacing: 0) {
VStack(spacing: 0) {
Color.clear.frame(height: rulerH)
HStack(spacing: 6) {
Expand Down Expand Up @@ -385,8 +410,10 @@ struct TimelinePanel: View {
// model band carries its own current-model marker)
// Bound the scroll view's own height — a horizontal ScrollView is
// otherwise vertically greedy and would absorb the panel's slack,
// opening gaps above the ruler / below the lane.
.frame(height: rulerH + laneH)
// opening gaps above the ruler / below the lane. It includes a
// legacy scroll bar, which SwiftUI puts below the content.
.frame(height: rulerH + laneH + laneScrollerH)
.preference(key: LaneViewportWKey.self, value: w)
// Symmetric edge-fade when the lane is longer than the viewport,
// so it's obvious the track scrolls horizontally (issue #131) —
// trailing at rest, leading too once scrolled off the start.
Expand All @@ -399,12 +426,19 @@ struct TimelinePanel: View {
}
}
}
.frame(height: rulerH + laneH)
.frame(height: rulerH + laneH + laneScrollerH)
.onPreferenceChange(LaneViewportWKey.self) { laneViewportW = $0 }
}
// Bound the whole track row — the gutter VStack + divider are otherwise
// vertically greedy and, in a taller pane, would center the ruler/lane
// inside an over-tall region (empty bands above/below).
.frame(height: rulerH + laneH)
.frame(height: rulerH + laneH + laneScrollerH)
#if os(macOS)
.onReceive(NotificationCenter.default.publisher(
for: NSScroller.preferredScrollerStyleDidChangeNotification)) { _ in
legacyScrollers = NSScroller.preferredScrollerStyle == .legacy
}
#endif
}

// Tick ruler across the whole (scrollable) content width. Tap to seek.
Expand Down Expand Up @@ -1045,6 +1079,10 @@ private struct PaletteViewportWKey: PreferenceKey {
static var defaultValue: CGFloat = 0
static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) { value = max(value, nextValue()) }
}
private struct LaneViewportWKey: PreferenceKey {
static var defaultValue: CGFloat = 0
static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) { value = max(value, nextValue()) }
}

// Small downward-pointing triangle for the playhead head.
private struct Triangle: Shape {
Expand Down
198 changes: 198 additions & 0 deletions swiftui/PyMOLViewerTests/TimelineLaneLayoutTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
import XCTest
import SwiftUI
@testable import RayMol

/// The Timeline's track row (the ruler over the item lane, beside the "Track"
/// gutter) under both AppKit scroller styles. Legacy scroll bars ("Show scroll
/// bars: Always", or "Automatically" with a mouse attached, as on GitHub's
/// macOS runners and in VMs) put a 17 pt scroller below the lane's content once
/// the lane overflows. The row must grow by that much, so the ruler stays level
/// with the gutter and under the header. Overlay scroll bars leave the row as
/// it was.
///
/// A run gets its host machine's scroller style. To force one, copy the
/// `UnitTests_macOS_*.xctestrun` that `xcodebuild build-for-testing` writes to
/// `<derivedData>/Build/Products/`, keep the copy in that directory, add
/// `CommandLineArguments` `["-AppleShowScrollBars", "Always"]` (legacy) or
/// `"WhenScrolling"` (overlay) to the test target in it, and run
/// `xcodebuild test-without-building -xctestrun <copy>`. Setting the default
/// from inside a running test is too late: the host app already has the
/// system's style.
final class TimelineLaneLayoutTests: XCTestCase {
private var engine: PyMOLEngine { PyMOLEngine.shared }
private var savedItems: [PyMOLEngine.TimelineItem] = []
private var savedUntracked = false
private var window: NSWindow?
private let scale: CGFloat = 2 // pixels per point of the drawn panel

// TimelinePanel's geometry: the 44 pt header and its 1 pt rule, then the
// track row, the 28 pt ruler over the 56 pt item lane.
private let rowTop: CGFloat = 45
private let rulerH: CGFloat = 28
private let laneH: CGFloat = 56

override func setUpWithError() throws {
let deadline = Date().addingTimeInterval(20)
while !engine.isReady && Date() < deadline {
RunLoop.current.run(until: Date().addingTimeInterval(0.1))
}
guard engine.isReady else {
XCTFail("PyMOLEngine.shared was not ready")
throw NSError(domain: "PyMOLEngine", code: 1)
}
savedItems = engine.timelineItems
savedUntracked = engine.untrackedMovie
}

override func tearDown() {
window?.orderOut(nil)
window?.contentView = nil
window = nil
engine.clearMovieItems()
engine.timelineItems = savedItems
engine.untrackedMovie = savedUntracked
super.tearDown()
}

private func settle(_ seconds: TimeInterval) {
RunLoop.current.run(until: Date().addingTimeInterval(seconds))
}

private func scrollViews(_ view: NSView) -> [NSScrollView] {
var found: [NSScrollView] = []
if let s = view as? NSScrollView { found.append(s) }
for sub in view.subviews { found += scrollViews(sub) }
return found
}

/// Two camera keyframes `seconds` apart. The lane holds about 10 s at zoom 1.
private func setMovie(seconds: Double) {
engine.timelineItems = [
PyMOLEngine.TimelineItem(kind: .camera),
PyMOLEngine.TimelineItem(kind: .camera, transition: .init(seconds: seconds)),
]
}

private struct Row {
var lane: CGRect // the lane's NSScrollView, host points (y down)
var clip: CGRect // its clip view: the ruler + item lane
var scroller: CGRect? // the horizontal scroll bar, when AppKit shows it
var legacy: Bool
var bar: CGFloat // the room a legacy scroll bar needs below the lane
var panelH: CGFloat // the whole panel's fitting height
var gutterIconMidY: CGFloat // the gutter's film icon, drawn
}

private func measure(_ host: NSHostingView<some View>, _ label: String) throws -> Row {
host.layoutSubtreeIfNeeded()
settle(0.8)
host.layoutSubtreeIfNeeded()
let lanes = scrollViews(host).filter {
abs(($0.documentView?.frame.height ?? 0) - (rulerH + laneH)) < 0.5
}
XCTAssertEqual(lanes.count, 1, "\(label): one lane scroll view")
let lane = try XCTUnwrap(lanes.first, "\(label): no lane scroll view")
let shown = lane.horizontalScroller.flatMap { $0.isHidden ? nil : $0 }
let legacy = lane.scrollerStyle == .legacy
let row = Row(
lane: lane.convert(lane.bounds, to: host),
clip: lane.contentView.convert(lane.contentView.bounds, to: host),
scroller: shown.map { $0.convert($0.bounds, to: host) },
legacy: legacy,
bar: legacy && shown != nil
? NSScroller.scrollerWidth(for: shown!.controlSize, scrollerStyle: .legacy) : 0,
panelH: host.fittingSize.height,
gutterIconMidY: try gutterIconMidY(host))
print("TimelineLaneLayout \(label): style=\(legacy ? "legacy" : "overlay") lane=\(row.lane)"
+ " clip=\(row.clip) scroller=\(row.scroller.map { "\($0)" } ?? "hidden")"
+ " panelH=\(row.panelH) gutterIconMidY=\(row.gutterIconMidY)")
return row
}

/// The vertical middle, in host points, of the gutter's film icon: the
/// pixels near the accent colour in the gutter's leading 26 pt, below the
/// header.
private func gutterIconMidY(_ host: NSView) throws -> CGFloat {
let w = Int(host.bounds.width * scale), h = Int(host.bounds.height * scale)
var pixels = [UInt8](repeating: 0, count: w * h * 4)
let layer = try XCTUnwrap(host.layer)
try pixels.withUnsafeMutableBytes { buf in
let ctx = try XCTUnwrap(CGContext(
data: buf.baseAddress, width: w, height: h, bitsPerComponent: 8, bytesPerRow: w * 4,
space: CGColorSpace(name: CGColorSpace.sRGB)!,
bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue))
ctx.translateBy(x: 0, y: CGFloat(h))
ctx.scaleBy(x: scale, y: -scale)
layer.render(in: ctx)
}
let accent = try XCTUnwrap(NSColor(TimelineTheme.accent).usingColorSpace(.sRGB))
var rows: [Int] = []
for y in Int(rowTop * scale)..<min(Int((rowTop + rulerH + laneH + 20) * scale), h) {
for x in 0..<Int(26 * scale) {
let i = (y * w + x) * 4
let a = max(Double(pixels[i + 3]) / 255, 1e-6)
let dr = Double(pixels[i]) / 255 / a - Double(accent.redComponent)
let dg = Double(pixels[i + 1]) / 255 / a - Double(accent.greenComponent)
let db = Double(pixels[i + 2]) / 255 / a - Double(accent.blueComponent)
if (dr * dr + dg * dg + db * db).squareRoot() < 0.15 { rows.append(y); break }
}
}
let top = try XCTUnwrap(rows.min(), "no film icon drawn in the gutter")
return CGFloat(top + rows.max()! + 1) / 2 / scale
}

func testTrackRowMakesRoomForALegacyScrollBar() throws {
let forced = UserDefaults.standard.volatileDomain(
forName: UserDefaults.argumentDomain)["AppleShowScrollBars"] as? String
let host = NSHostingView(rootView: TimelinePanel()
.environmentObject(engine)
.environmentObject(engine.playback))
let window = NSWindow(contentRect: NSRect(x: -20000, y: -20000, width: 900, height: 600),
styleMask: [.borderless], backing: .buffered, defer: false)
window.isReleasedWhenClosed = false
window.contentView = host
self.window = window
XCTAssertTrue(host.isFlipped)

setMovie(seconds: 2)
let fits = try measure(host, "fits")
setMovie(seconds: 40)
let overflows = try measure(host, "overflows")

// The forced runs really exercise the style they ask for.
switch forced {
case "Always": XCTAssertTrue(overflows.legacy, "-AppleShowScrollBars Always: legacy scroll bars")
case "WhenScrolling": XCTAssertFalse(overflows.legacy, "-AppleShowScrollBars WhenScrolling: overlay")
default: break
}
// A lane that fits shows no scroll bar, in either style.
XCTAssertNil(fits.scroller, "a 2 s movie fits the lane")
XCTAssertEqual(fits.bar, 0)
XCTAssertNotNil(overflows.scroller, "a 40 s movie overflows the lane")
if overflows.legacy {
XCTAssertEqual(overflows.bar, 17, accuracy: 0.5)
} else {
XCTAssertEqual(overflows.bar, 0)
}

for (label, row) in [("fits", fits), ("overflows", overflows)] {
// The ruler starts right under the header's rule, as the gutter does.
XCTAssertEqual(row.lane.minY, rowTop, accuracy: 0.5, "\(label): lane top")
XCTAssertEqual(row.clip.minY, rowTop, accuracy: 0.5, "\(label): ruler top")
XCTAssertEqual(row.clip.height, rulerH + laneH, accuracy: 0.5, "\(label): ruler + items")
// The scroll bar, if any, sits below the items, inside the row.
XCTAssertEqual(row.lane.height, rulerH + laneH + row.bar, accuracy: 0.5, "\(label): row height")
if row.legacy, let s = row.scroller {
XCTAssertEqual(s.minY, rowTop + rulerH + laneH, accuracy: 0.5, "\(label): scroll bar top")
XCTAssertEqual(s.maxY, row.lane.maxY, accuracy: 0.5, "\(label): scroll bar bottom")
}
// The gutter's track label stays level with the item lane.
XCTAssertEqual(row.gutterIconMidY, rowTop + rulerH + laneH / 2, accuracy: 1.5,
"\(label): gutter icon level with the items")
}
// The row, and so the panel, grows by just the legacy scroll bar: the
// bar takes room of its own instead of covering the scenes strip.
XCTAssertEqual(overflows.panelH - fits.panelH, overflows.bar, accuracy: 0.5,
"the panel grows by the scroll bar's room")
}
}
Loading