Sources/Orgstar/PresentationWindow.swift
103 lines · 4246 bytes
7 symbols in this file
1import AppKit
2import OrgApp
3import OrgCore
4import OrgEditorAppKit
5import OrgPresentation
6import SwiftUI
7
8/// Present (`app.present`): the open file one heading at a time in a full-screen window, as
9/// org-present and org-tree-slide show it. →, ↓, Space, Page Down and Return go on; ←, ↑,
10/// Delete and Page Up go back; Home and End go to the first and last slide; Escape or Done
11/// leaves.
12@MainActor
13enum PresentationWindow {
14 private static var window: NSWindow?
15
16 static func show(_ deck: SlideDeck, theme: ThemeSpec, options: EditingOptions) {
17 window?.close()
18 let screen = NSApp.keyWindow?.screen ?? NSScreen.main
19 let frame = screen?.frame ?? CGRect(x: 0, y: 0, width: 1280, height: 800)
20 let window = NSWindow(contentRect: frame, styleMask: [.titled, .closable, .resizable, .fullSizeContentView], backing: .buffered, defer: false)
21 window.identifier = NSUserInterfaceItemIdentifier("presentation")
22 window.title = String(localized: "Present")
23 window.titlebarAppearsTransparent = true
24 window.titleVisibility = .hidden
25 window.isReleasedWhenClosed = false
26 window.collectionBehavior = [.fullScreenPrimary]
27 window.backgroundColor = SlideText.background(theme)
28 window.contentView = NSHostingView(rootView: PresentationView(deck: deck, theme: theme, options: options) { close() })
29 window.setFrame(frame, display: true)
30 window.makeKeyAndOrderFront(nil)
31 self.window = window
32 window.toggleFullScreen(nil)
33 }
34
35 static func close() {
36 window?.close()
37 window = nil
38 }
39}
40
41private struct PresentationView: View {
42 let deck: SlideDeck
43 let theme: ThemeSpec
44 let options: EditingOptions
45 let done: () -> Void
46 @FocusState private var focused: Bool
47
48 var body: some View {
49 GeometryReader { geometry in
50 let size = SlideDeck.bodySize(for: geometry.size, themeSize: theme.fontSize)
51 let margin = geometry.size.width * 0.07
52 ZStack(alignment: .bottomTrailing) {
53 SlideTextView(text: SlideText.attributed(deck.slide, tree: deck.tree, spec: theme, size: size, width: geometry.size.width - 2 * margin,
54 directory: deck.directory, options: options, highlighter: PaneView.highlighter),
55 inset: CGSize(width: margin, height: deck.slide.title != nil ? geometry.size.height * 0.28 : geometry.size.height * 0.08))
56 HStack(spacing: 16) {
57 Text(deck.position).monospacedDigit().foregroundStyle(.secondary)
58 Button("Done", action: done)
59 }
60 .padding()
61 }
62 }
63 .background(Color(nsColor: SlideText.background(theme)))
64 .focusable()
65 .focusEffectDisabled()
66 .focused($focused)
67 .onAppear { focused = true }
68 .onKeyPress(keys: [.rightArrow, .downArrow, .space, .pageDown, .return, .leftArrow, .upArrow, .pageUp, .delete, .home, .end, .escape]) { press in
69 switch press.key {
70 case .rightArrow, .downArrow, .space, .pageDown, .return: deck.next()
71 case .leftArrow, .upArrow, .pageUp, .delete: deck.previous()
72 case .home: deck.first()
73 case .end: deck.last()
74 default: done()
75 }
76 return .handled
77 }
78 }
79}
80
81/// The slide's text, not editable, scrolling when it runs past the bottom.
82private struct SlideTextView: NSViewRepresentable {
83 let text: NSAttributedString
84 let inset: CGSize
85
86 func makeNSView(context: Context) -> NSScrollView {
87 let scroll = NSTextView.scrollableTextView()
88 scroll.drawsBackground = false
89 scroll.hasVerticalScroller = false
90 let view = scroll.documentView as! NSTextView
91 view.isEditable = false
92 view.isSelectable = false
93 view.drawsBackground = false
94 return scroll
95 }
96
97 func updateNSView(_ scroll: NSScrollView, context: Context) {
98 let view = scroll.documentView as! NSTextView
99 view.textContainerInset = inset
100 view.textStorage?.setAttributedString(text)
101 view.scroll(.zero)
102 }
103}