Sources/OrgApp/SlideDeck.swift
44 lines · 1899 bytes
1import CoreGraphics
2import Foundation
3import Observation
4import OrgCore
5import OrgPresentation
6
7/// Present: the open file's slides (`SlideShow`) and the one showing, for the full-screen
8/// presentation on the Mac and iOS and an iPad's external display.
9@MainActor @Observable
10public final class SlideDeck {
11 public let tree: OrgTree
12 /// The file's path, for images.
13 public let path: String
14 public let slides: [SlideShow.Slide]
15 public private(set) var index: Int
16
17 /// Nil when the file has nothing to show. Starts at the slide of the heading at `caret`.
18 public init?(tree: OrgTree, path: String, settings: OrgSettings = .default, caret: Int = 0) {
19 let slides = SlideShow.slides(tree, settings: settings)
20 guard !slides.isEmpty else { return nil }
21 self.tree = tree
22 self.path = path
23 self.slides = slides
24 index = slides.lastIndex { ($0.start ?? 0) <= caret } ?? 0
25 }
26
27 public var slide: SlideShow.Slide { slides[index] }
28 /// The slide after this one, for the presenter's screen.
29 public var upcoming: SlideShow.Slide? { index + 1 < slides.count ? slides[index + 1] : nil }
30 public var directory: URL { URL(fileURLWithPath: path).deletingLastPathComponent() }
31 /// "3 / 12".
32 public var position: String { "\(index + 1) / \(slides.count)" }
33
34 public func next() { index = min(index + 1, slides.count - 1) }
35 public func previous() { index = max(index - 1, 0) }
36 public func first() { index = 0 }
37 public func last() { index = slides.count - 1 }
38
39 /// The body text's size in points on a screen of `size`: about 40 columns across and 18
40 /// lines down at most, and never below 1.25 times the theme's own size.
41 public nonisolated static func bodySize(for size: CGSize, themeSize: Int) -> CGFloat {
42 max(CGFloat(themeSize) * 1.25, min(size.width / 40, size.height / 18))
43 }
44}