| @@ -29,6 +29,8 @@ struct ReaderScreen: View { |
| 29 | 29 | @State private var fold = FoldState() |
| 30 | 30 | @State private var folded: [Range<Int>] = [] |
| 31 | 31 | @State private var loadedPath: String? |
| 32 | /// The file's display: indentation, leading stars, inline images, truncation. |
| 33 | @State private var startup: StartupDisplay? |
| 32 | 34 | @State private var showingOutline = false |
| 33 | 35 | @State private var message: String? |
| 34 | 36 | @Environment(\.openURL) private var openURL |
| @@ -41,8 +43,9 @@ struct ReaderScreen: View { |
| 41 | 43 | // Read again when settings or the Dynamic Type size change. |
| 42 | 44 | let _ = model.settingsVersion |
| 43 | 45 | let theme = MobileTheme(model.theme) |
| 46 | let truncated = startup?.truncated ?? false |
| 44 | 47 | ScrollViewReader { proxy in |
| 45 | | ScrollView { |
| 48 | ScrollView(truncated ? [.vertical, .horizontal] : .vertical) { |
| 46 | 49 | LazyVStack(alignment: .leading, spacing: 8) { |
| 47 | 50 | if let tree = session.document?.tree { |
| 48 | 51 | let preambleEnd = outline.first?.start ?? (tree.text as NSString).length |
| @@ -52,14 +55,14 @@ struct ReaderScreen: View { |
| 52 | 55 | heading(tree, entry, theme).id(entry.start) |
| 53 | 56 | if !fold.folds.contains(entry.start), !fold.bodyFolds.contains(entry.start) { |
| 54 | 57 | let body = bodyRange(index, tree) |
| 55 | | if !body.isEmpty { textView(tree, body, theme).padding(.leading, indent(entry)) } |
| 58 | if !body.isEmpty { textView(tree, body, theme).padding(.leading, bodyIndent(entry, theme)) } |
| 56 | 59 | } |
| 57 | 60 | } |
| 58 | 61 | } |
| 59 | 62 | } |
| 60 | 63 | } |
| 61 | 64 | .padding() |
| 62 | | .frame(maxWidth: .infinity, alignment: .leading) |
| 65 | .frame(maxWidth: truncated ? nil : .infinity, alignment: .leading) |
| 63 | 66 | } |
| 64 | 67 | .background(theme.swiftColor("background") ?? Color(uiColor: .systemBackground)) |
| 65 | 68 | .onChange(of: session.revealRequest?.id) { |
| @@ -89,12 +92,31 @@ struct ReaderScreen: View { |
| 89 | 92 | .onAppear(perform: open) |
| 90 | 93 | .onChange(of: session.path) { loadFolds() } |
| 91 | 94 | .onChange(of: session.editVersion) { loadFolds() } |
| 95 | .onChange(of: model.settingsVersion) { loadFolds() } |
| 92 | 96 | } |
| 93 | 97 | |
| 94 | 98 | // MARK: - Content |
| 95 | 99 | |
| 96 | 100 | private func indent(_ entry: OutlineEntry) -> CGFloat { CGFloat(entry.level - 1) * 14 } |
| 97 | 101 | |
| 102 | private var indented: Bool { startup?.indented ?? true } |
| 103 | |
| 104 | /// The text as the editor's display settings show it. |
| 105 | private var display: ReadingText.Display { |
| 106 | ReadingText.Display(showMarkup: model.showMarkup, options: session.options, highlighter: MobileEditor.highlighter) |
| 107 | } |
| 108 | |
| 109 | /// org-indent: a heading's text and its body start past its stars, at level + 1 columns, |
| 110 | /// the fold mark standing where the last star and its space would. Without it nothing is |
| 111 | /// indented and the stars show. |
| 112 | private func headingIndent(_ entry: OutlineEntry, _ theme: MobileTheme) -> CGFloat { |
| 113 | indented ? CGFloat(entry.level - 1) * theme.column : 0 |
| 114 | } |
| 115 | |
| 116 | private func bodyIndent(_ entry: OutlineEntry, _ theme: MobileTheme) -> CGFloat { |
| 117 | indented ? CGFloat(entry.level + 1) * theme.column : 0 |
| 118 | } |
| 119 | |
| 98 | 120 | /// Hidden under a folded ancestor. |
| 99 | 121 | private func isShown(_ index: Int) -> Bool { |
| 100 | 122 | var parent = outline[index].parent |
| @@ -116,20 +138,19 @@ struct ReaderScreen: View { |
| 116 | 138 | |
| 117 | 139 | private func heading(_ tree: OrgTree, _ entry: OutlineEntry, _ theme: MobileTheme) -> some View { |
| 118 | 140 | let ns = tree.text as NSString |
| 119 | | var line = ns.lineRange(for: NSRange(location: entry.start, length: 0)) |
| 120 | | let title = min(entry.start + entry.level + 1, NSMaxRange(line)) |
| 121 | | line.length = NSMaxRange(line) - title |
| 122 | | let text = attributed(ReadingText.segments(tree, in: title..<(title + line.length)), heading: entry.level, theme) |
| 123 | | let hasContent = entry.end > NSMaxRange(ns.lineRange(for: NSRange(location: entry.start, length: 0))) |
| 141 | let line = ns.lineRange(for: NSRange(location: entry.start, length: 0)) |
| 142 | let start = indented ? min(entry.start + entry.level + 1, NSMaxRange(line)) : entry.start |
| 143 | let text = attributed(ReadingText.segments(tree, in: start..<NSMaxRange(line), display: display), heading: entry.level, theme) |
| 144 | let hasContent = entry.end > NSMaxRange(line) |
| 124 | 145 | let foldedMark = fold.folds.contains(entry.start) && hasContent |
| 125 | | return HStack(alignment: .firstTextBaseline, spacing: 6) { |
| 146 | return HStack(alignment: .firstTextBaseline, spacing: 0) { |
| 126 | 147 | Image(systemName: foldedMark ? "chevron.right" : "chevron.down") |
| 127 | 148 | .font(.caption.weight(.semibold)).foregroundStyle(theme.swiftColor("markup") ?? Color(uiColor: .tertiaryLabel)) |
| 128 | 149 | .opacity(hasContent ? 1 : 0) |
| 129 | | Text(text.trimmingTrailingNewlines()).lineSpacing(CGFloat(max(0, theme.spec.lineSpacing))) |
| 130 | | .frame(maxWidth: .infinity, alignment: .leading) |
| 150 | .frame(width: 2 * theme.column, alignment: .leading) |
| 151 | lineText(text.trimmingTrailingNewlines(), theme) |
| 131 | 152 | } |
| 132 | | .padding(.leading, indent(entry)) |
| 153 | .padding(.leading, headingIndent(entry, theme)) |
| 133 | 154 | .padding(.top, entry.level == 1 ? 6 : 0) |
| 134 | 155 | .contentShape(Rectangle()) |
| 135 | 156 | .onTapGesture { fold = Presentation.cycle(entry.start, in: tree, fold) } |
| @@ -138,10 +159,52 @@ struct ReaderScreen: View { |
| 138 | 159 | } |
| 139 | 160 | |
| 140 | 161 | private func textView(_ tree: OrgTree, _ range: Range<Int>, _ theme: MobileTheme) -> some View { |
| 141 | | Text(attributed(ReadingText.segments(tree, in: range, folded: folded), heading: nil, theme).trimmingTrailingNewlines()) |
| 162 | VStack(alignment: .leading, spacing: 4) { |
| 163 | ForEach(parts(tree, range), id: \.range.lowerBound) { part in |
| 164 | if let image = part.image { |
| 165 | Image(uiImage: image.picture).resizable().scaledToFit() |
| 166 | .frame(maxWidth: image.width.map { CGFloat($0) } ?? image.picture.size.width, alignment: .leading) |
| 167 | .accessibilityLabel((image.path as NSString).lastPathComponent) |
| 168 | } else { |
| 169 | lineText(attributed(ReadingText.segments(tree, in: part.range, folded: folded, display: display), heading: nil, theme).trimmingTrailingNewlines(), theme) |
| 170 | .textSelection(.enabled) |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /// Wrapped to the view's width, or on one line each, scrolling sideways, while truncated. |
| 177 | private func lineText(_ text: AttributedString, _ theme: MobileTheme) -> some View { |
| 178 | Text(text) |
| 142 | 179 | .lineSpacing(CGFloat(max(0, theme.spec.lineSpacing))) |
| 143 | | .textSelection(.enabled) |
| 144 | | .frame(maxWidth: .infinity, alignment: .leading) |
| 180 | .fixedSize(horizontal: startup?.truncated ?? false, vertical: false) |
| 181 | .frame(maxWidth: startup?.truncated == true ? nil : .infinity, alignment: .leading) |
| 182 | } |
| 183 | |
| 184 | private struct Part { |
| 185 | let range: Range<Int> |
| 186 | var image: (picture: UIImage, path: String, width: Int?)? |
| 187 | } |
| 188 | |
| 189 | /// Text, and with inline images on, lines that are only an image link shown as the image. |
| 190 | private func parts(_ tree: OrgTree, _ range: Range<Int>) -> [Part] { |
| 191 | guard startup?.inlineImages == true, let path = session.path else { return [Part(range: range)] } |
| 192 | let base = URL(fileURLWithPath: path).deletingLastPathComponent() |
| 193 | let ns = tree.text as NSString |
| 194 | var parts: [Part] = [] |
| 195 | var textStart = range.lowerBound |
| 196 | var location = range.lowerBound |
| 197 | while location < range.upperBound { |
| 198 | let line = ns.lineRange(for: NSRange(location: location, length: 0)) |
| 199 | location = NSMaxRange(line) |
| 200 | guard folded.allSatisfy({ !$0.contains(line.location) }), let image = InlineImages.image(atLine: line.location, in: ns), |
| 201 | let picture = MobileEditor.loadImage(image.path, relativeTo: base) else { continue } |
| 202 | if textStart < line.location { parts.append(Part(range: textStart..<line.location)) } |
| 203 | parts.append(Part(range: line.location..<min(NSMaxRange(line), range.upperBound), image: (picture, image.path, image.width))) |
| 204 | textStart = min(NSMaxRange(line), range.upperBound) |
| 205 | } |
| 206 | if textStart < range.upperBound { parts.append(Part(range: textStart..<range.upperBound)) } |
| 207 | return parts |
| 145 | 208 | } |
| 146 | 209 | |
| 147 | 210 | /// Segments as styled text in the theme's font and colors, as the editor styles them; |
| @@ -168,7 +231,11 @@ struct ReaderScreen: View { |
| 168 | 231 | case .verbatim, .code, .inlineSource: |
| 169 | 232 | if let background = theme.color("inline-background") { part.backgroundColor = Color(uiColor: background) } |
| 170 | 233 | fg(role == .verbatim ? "verbatim" : "code") |
| 171 | | case .markup, .linkMarkup, .leadingStars: fg("markup") |
| 234 | case .markup, .linkMarkup: fg("markup") |
| 235 | case .leadingStars: |
| 236 | // As the editor hides them; shown in the heading's color unless hidden. |
| 237 | if !Presentation.skipsLeadingStars(role, indented: indented, hideStars: startup?.hideStars ?? false) { color = .clear } |
| 238 | case .syntax(let category): if let key = category.themeKey { fg(key) } |
| 172 | 239 | case .timestamp: fg("timestamp") |
| 173 | 240 | case .comment: fg("comment") |
| 174 | 241 | case .keyword: fg("keyword") |
| @@ -179,13 +246,18 @@ struct ReaderScreen: View { |
| 179 | 246 | case .link: |
| 180 | 247 | part.underlineStyle = .single |
| 181 | 248 | fg("link") |
| 182 | | case .heading, .block, .syntax: break |
| 249 | case .heading, .block: break |
| 183 | 250 | } |
| 184 | 251 | } |
| 185 | 252 | if segment.link != nil { |
| 186 | 253 | // Found again by position when followed. |
| 187 | 254 | part.link = URL(string: "orgstar-link:\(segment.offset)") |
| 188 | 255 | } |
| 256 | if let superscript = segment.superscript { |
| 257 | // Raised or lowered as the editor draws `org-pretty-entities` scripts. |
| 258 | part.baselineOffset = (superscript ? 0.3 : -0.3) * font.pointSize |
| 259 | font = font.withSize(font.pointSize * 0.7) |
| 260 | } |
| 189 | 261 | part.font = Font(font as CTFont) |
| 190 | 262 | part.foregroundColor = Color(uiColor: color) |
| 191 | 263 | result += part |
| @@ -216,15 +288,16 @@ struct ReaderScreen: View { |
| 216 | 288 | loadFolds() |
| 217 | 289 | } |
| 218 | 290 | |
| 219 | | /// The file's startup visibility, kept while the same file is open. |
| 291 | /// The file's startup display, as the editor opens it; its heading folds are kept while |
| 292 | /// the same file is open. |
| 220 | 293 | private func loadFolds() { |
| 221 | 294 | guard let tree = session.document?.tree else { return } |
| 222 | | folded = Presentation.startupWrappers( |
| 223 | | tree, hideDrawers: session.options.hideDrawerStartup, hideBlocks: session.options.hideBlockStartup |
| 224 | | ).map(\.hidden) |
| 295 | let display = Presentation.startupDisplay(tree, options: session.options) |
| 296 | startup = display |
| 297 | folded = display.wrappers.map(\.hidden) |
| 225 | 298 | guard loadedPath != session.path else { return } |
| 226 | 299 | loadedPath = session.path |
| 227 | | fold = Presentation.applyVisibilityProperties(Presentation.startupFoldState(tree) ?? FoldState(), in: tree) |
| 300 | fold = display.folds |
| 228 | 301 | } |
| 229 | 302 | |
| 230 | 303 | /// Unfolds the way to the heading containing `offset` and scrolls to it. |