iOS: tap a folded drawer to open it !209
4 files changed, +59 −3
Layout: unified · split
Sources/OrgPresentation/ReadingText.swift +8 −3
| @@ -14,6 +14,8 @@ public enum ReadingText { | |||
| 14 | public var offset: Int | 14 | public var offset: Int |
| 15 | /// A pretty sub/superscript's body: true raised, false lowered. | 15 | /// A pretty sub/superscript's body: true raised, false lowered. |
| 16 | public var superscript: Bool? | 16 | public var superscript: Bool? |
| 17 | /// A fold's `…`: where the hidden range it stands for starts. | ||
| 18 | public var unfolds: Int? | ||
| 17 | } | 19 | } |
| 18 | 20 | ||
| 19 | /// How the text shows, as the editor's display settings make it. | 21 | /// How the text shows, as the editor's display settings make it. |
| @@ -85,7 +87,7 @@ public enum ReadingText { | |||
| 85 | let points = bounds.sorted() | 87 | let points = bounds.sorted() |
| 86 | var segments: [Segment] = [] | 88 | var segments: [Segment] = [] |
| 87 | func append(_ segment: Segment) { | 89 | func append(_ segment: Segment) { |
| 88 | if var last = segments.last, last.roles == segment.roles, last.link == segment.link, last.superscript == segment.superscript, | 90 | if var last = segments.last, last.roles == segment.roles, last.link == segment.link, last.superscript == segment.superscript, last.unfolds == segment.unfolds, |
| 89 | last.offset + (last.text as NSString).length == segment.offset { | 91 | last.offset + (last.text as NSString).length == segment.offset { |
| 90 | last.text += segment.text | 92 | last.text += segment.text |
| 91 | segments[segments.count - 1] = last | 93 | segments[segments.count - 1] = last |
| @@ -110,16 +112,19 @@ public enum ReadingText { | |||
| 110 | var scriptAt = [Bool?](repeating: nil, count: intervals) | 112 | var scriptAt = [Bool?](repeating: nil, count: intervals) |
| 111 | for script in scripts { for k in covered(script.range) where scriptAt[k] == nil { scriptAt[k] = script.superscript } } | 113 | for script in scripts { for k in covered(script.range) where scriptAt[k] == nil { scriptAt[k] = script.superscript } } |
| 112 | let foldStarts = Set(folds.map(\.lowerBound)) | 114 | let foldStarts = Set(folds.map(\.lowerBound)) |
| 115 | // A fold's opening line opens it too, a wider target than its `…`. | ||
| 116 | let openings = folds.map { (line: text.lineRange(for: NSRange(location: $0.lowerBound, length: 0)).location..<$0.lowerBound, start: $0.lowerBound + 1) } | ||
| 113 | for (k, (a, b)) in zip(points, points.dropFirst()).enumerated() { | 117 | for (k, (a, b)) in zip(points, points.dropFirst()).enumerated() { |
| 114 | if foldStarts.contains(a) { | 118 | if foldStarts.contains(a) { |
| 115 | segments.append(Segment(text: "…", roles: [.metadata], link: nil, offset: a)) | 119 | segments.append(Segment(text: "…", roles: [.metadata], link: nil, offset: a, unfolds: a + 1)) |
| 116 | } | 120 | } |
| 117 | if hiddenAt[k] { continue } | 121 | if hiddenAt[k] { continue } |
| 118 | let roles = rolesAt[k] | 122 | let roles = rolesAt[k] |
| 119 | let link = roles.contains(.link) ? Links.link(at: a, text: tree.text, tree: tree)?.link : nil | 123 | let link = roles.contains(.link) ? Links.link(at: a, text: tree.text, tree: tree)?.link : nil |
| 120 | let superscript = scriptAt[k] | 124 | let superscript = scriptAt[k] |
| 121 | let shown = replaced[a] ?? text.substring(with: NSRange(location: a, length: b - a)) | 125 | let shown = replaced[a] ?? text.substring(with: NSRange(location: a, length: b - a)) |
| 122 | let segment = Segment(text: shown, roles: roles, link: link, offset: a, superscript: superscript) | 126 | let unfolds = openings.first { $0.line.contains(a) }?.start |
| 127 | let segment = Segment(text: shown, roles: roles, link: link, offset: a, superscript: superscript, unfolds: unfolds) | ||
| 123 | if replaced[a] != nil { segments.append(segment) } else { append(segment) } | 128 | if replaced[a] != nil { segments.append(segment) } else { append(segment) } |
| 124 | } | 129 | } |
| 125 | return segments | 130 | return segments |
Sources/OrgstarMobile/MobileEditor.swift +43
| @@ -16,6 +16,7 @@ final class LockedRanges: @unchecked Sendable { | |||
| 16 | var all: [Range<Int>] { lock.withLock { ranges } } | 16 | var all: [Range<Int>] { lock.withLock { ranges } } |
| 17 | func covers(_ start: Int, _ end: Int) -> Bool { lock.withLock { ranges.contains { $0.lowerBound <= start && end <= $0.upperBound } } } | 17 | func covers(_ start: Int, _ end: Int) -> Bool { lock.withLock { ranges.contains { $0.lowerBound <= start && end <= $0.upperBound } } } |
| 18 | func isFolded(_ start: Int) -> Bool { lock.withLock { starts.contains(start) } } | 18 | func isFolded(_ start: Int) -> Bool { lock.withLock { starts.contains(start) } } |
| 19 | var folded: Set<Int> { lock.withLock { starts } } | ||
| 19 | func containing(_ offset: Int) -> Range<Int>? { lock.withLock { ranges.first { $0.contains(offset) } } } | 20 | func containing(_ offset: Int) -> Range<Int>? { lock.withLock { ranges.first { $0.contains(offset) } } } |
| 20 | } | 21 | } |
| 21 | 22 | ||
| @@ -210,6 +211,9 @@ final class MobileEditor: NSObject, DocumentBuffer { | |||
| 210 | textView.textStorage.delegate = self | 211 | textView.textStorage.delegate = self |
| 211 | textView.textLayoutManager?.delegate = self | 212 | textView.textLayoutManager?.delegate = self |
| 212 | (textView.textLayoutManager?.textContentManager as? NSTextContentStorage)?.delegate = self | 213 | (textView.textLayoutManager?.textContentManager as? NSTextContentStorage)?.delegate = self |
| 214 | let foldTap = UITapGestureRecognizer(target: self, action: #selector(tappedFold)) | ||
| 215 | foldTap.delegate = self | ||
| 216 | textView.addGestureRecognizer(foldTap) | ||
| 213 | let startup = Presentation.startupDisplay(document.tree, options: options) | 217 | let startup = Presentation.startupDisplay(document.tree, options: options) |
| 214 | indented = startup.indented | 218 | indented = startup.indented |
| 215 | hideStars = startup.hideStars | 219 | hideStars = startup.hideStars |
| @@ -820,10 +824,20 @@ final class MobileEditor: NSObject, DocumentBuffer { | |||
| 820 | private func updateHidden() { | 824 | private func updateHidden() { |
| 821 | let tree = document.tree | 825 | let tree = document.tree |
| 822 | let wrappers = fold.wrapperFolds.compactMap { Wrappers.starting(at: $0, in: tree)?.hidden } | 826 | let wrappers = fold.wrapperFolds.compactMap { Wrappers.starting(at: $0, in: tree)?.hidden } |
| 827 | let before = hidden.folded | ||
| 823 | hidden.set( | 828 | hidden.set( |
| 824 | Presentation.hiddenRanges(tree, folds: fold.folds, bodyFolds: fold.bodyFolds, wrapperRanges: wrappers), | 829 | Presentation.hiddenRanges(tree, folds: fold.folds, bodyFolds: fold.bodyFolds, wrapperRanges: wrappers), |
| 825 | folded: fold.folds.union(fold.bodyFolds).union(fold.wrapperFolds) | 830 | folded: fold.folds.union(fold.bodyFolds).union(fold.wrapperFolds) |
| 826 | ) | 831 | ) |
| 832 | // Lines that gain or lose their `…` are made again: TextKit keeps a shown line's fragment. | ||
| 833 | let storage = textView.textStorage | ||
| 834 | let changed = before.symmetricDifference(hidden.folded).filter { $0 < storage.length } | ||
| 835 | if !changed.isEmpty { | ||
| 836 | let text = storage.string as NSString | ||
| 837 | storage.beginEditing() | ||
| 838 | for start in changed { storage.edited(.editedAttributes, range: text.lineRange(for: NSRange(location: start, length: 0)), changeInLength: 0) } | ||
| 839 | storage.endEditing() | ||
| 840 | } | ||
| 827 | if let manager = textView.textLayoutManager { manager.invalidateLayout(for: manager.documentRange) } | 841 | if let manager = textView.textLayoutManager { manager.invalidateLayout(for: manager.documentRange) } |
| 828 | textView.setNeedsDisplay() | 842 | textView.setNeedsDisplay() |
| 829 | } | 843 | } |
| @@ -902,6 +916,35 @@ extension MobileEditor: UITextViewDelegate { | |||
| 902 | } | 916 | } |
| 903 | } | 917 | } |
| 904 | 918 | ||
| 919 | extension MobileEditor: UIGestureRecognizerDelegate { | ||
| 920 | /// The start of the folded line whose `…` is at `point`: the space after its text. | ||
| 921 | private func foldedLine(at point: CGPoint) -> Int? { | ||
| 922 | guard let manager = textView.textLayoutManager, let content = manager.textContentManager else { return nil } | ||
| 923 | let inContainer = CGPoint(x: point.x - textView.textContainerInset.left, y: point.y - textView.textContainerInset.top) | ||
| 924 | guard let fragment = manager.textLayoutFragment(for: inContainer), let range = fragment.textElement?.elementRange else { return nil } | ||
| 925 | let start = content.offset(from: content.documentRange.location, to: range.location) | ||
| 926 | guard hidden.isFolded(start), | ||
| 927 | let line = fragment.textLineFragments.first(where: { $0.characterRange.length > 0 }) ?? fragment.textLineFragments.first | ||
| 928 | else { return nil } | ||
| 929 | return inContainer.x - fragment.layoutFragmentFrame.minX >= line.typographicBounds.maxX ? start : nil | ||
| 930 | } | ||
| 931 | |||
| 932 | /// A tap on a fold's `…` opens it, as TAB on its line does. | ||
| 933 | @objc fileprivate func tappedFold(_ tap: UITapGestureRecognizer) { | ||
| 934 | guard let start = foldedLine(at: tap.location(in: textView)), | ||
| 935 | let next = Presentation.cycle(at: start, in: document.tree, fold) else { return } | ||
| 936 | setFolds(next) | ||
| 937 | } | ||
| 938 | |||
| 939 | func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool { | ||
| 940 | foldedLine(at: gestureRecognizer.location(in: textView)) != nil | ||
| 941 | } | ||
| 942 | |||
| 943 | func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith other: UIGestureRecognizer) -> Bool { | ||
| 944 | true | ||
| 945 | } | ||
| 946 | } | ||
| 947 | |||
| 905 | extension MobileEditor: NSTextStorageDelegate { | 948 | extension MobileEditor: NSTextStorageDelegate { |
| 906 | nonisolated func textStorage( | 949 | nonisolated func textStorage( |
| 907 | _ textStorage: NSTextStorage, didProcessEditing editedMask: NSTextStorage.EditActions, range editedRange: NSRange, changeInLength delta: Int | 950 | _ textStorage: NSTextStorage, didProcessEditing editedMask: NSTextStorage.EditActions, range editedRange: NSRange, changeInLength delta: Int |
Sources/OrgstarMobile/ReaderScreen.swift +5
| @@ -273,6 +273,7 @@ struct ReaderScreen: View { | |||
| 273 | // Found again by position when followed. | 273 | // Found again by position when followed. |
| 274 | part.link = URL(string: "orgstar-link:\(segment.offset)") | 274 | part.link = URL(string: "orgstar-link:\(segment.offset)") |
| 275 | } | 275 | } |
| 276 | if let start = segment.unfolds { part.link = URL(string: "orgstar-unfold:\(start)") } | ||
| 276 | if let superscript = segment.superscript { | 277 | if let superscript = segment.superscript { |
| 277 | // Raised or lowered as the editor draws `org-pretty-entities` scripts. | 278 | // Raised or lowered as the editor draws `org-pretty-entities` scripts. |
| 278 | part.baselineOffset = (superscript ? 0.3 : -0.3) * font.pointSize | 279 | part.baselineOffset = (superscript ? 0.3 : -0.3) * font.pointSize |
| @@ -333,6 +334,10 @@ struct ReaderScreen: View { | |||
| 333 | } | 334 | } |
| 334 | 335 | ||
| 335 | private func follow(_ url: URL) -> OpenURLAction.Result { | 336 | private func follow(_ url: URL) -> OpenURLAction.Result { |
| 337 | if url.scheme == "orgstar-unfold", let start = Int(url.absoluteString.dropFirst("orgstar-unfold:".count)) { | ||
| 338 | folded.removeAll { $0.lowerBound == start } | ||
| 339 | return .handled | ||
| 340 | } | ||
| 336 | guard url.scheme == "orgstar-link", let offset = Int(url.absoluteString.dropFirst("orgstar-link:".count)), | 341 | guard url.scheme == "orgstar-link", let offset = Int(url.absoluteString.dropFirst("orgstar-link:".count)), |
| 337 | let tree = session.document?.tree, let link = Links.link(at: offset, text: tree.text, tree: tree)?.link else { | 342 | let tree = session.document?.tree, let link = Links.link(at: offset, text: tree.text, tree: tree)?.link else { |
| 338 | return .systemAction | 343 | return .systemAction |
Tests/OrgPresentationTests/ReadingTextTests.swift +3
| @@ -10,6 +10,9 @@ struct ReadingTextTests { | |||
| 10 | let folded = Presentation.startupWrappers(tree).map(\.hidden) | 10 | let folded = Presentation.startupWrappers(tree).map(\.hidden) |
| 11 | let segments = ReadingText.segments(tree, in: 0..<(text as NSString).length, folded: folded) | 11 | let segments = ReadingText.segments(tree, in: 0..<(text as NSString).length, folded: folded) |
| 12 | #expect(segments.map(\.text).joined() == "* TODO H :t:\n:PROPERTIES:…\nSee the site and *b* c.org.\n") | 12 | #expect(segments.map(\.text).joined() == "* TODO H :t:\n:PROPERTIES:…\nSee the site and *b* c.org.\n") |
| 13 | // The drawer's first line and its `…` name the hidden range they open. | ||
| 14 | #expect(segments.filter { $0.unfolds != nil }.map(\.text).joined() == ":PROPERTIES:…") | ||
| 15 | #expect(segments.first { $0.text == "…" }?.unfolds == folded.first?.lowerBound) | ||
| 13 | let site = segments.first { $0.text == "the site" } | 16 | let site = segments.first { $0.text == "the site" } |
| 14 | #expect(site?.link?.raw == "https://a.org" && site?.roles.contains(.link) == true) | 17 | #expect(site?.link?.raw == "https://a.org" && site?.roles.contains(.link) == true) |
| 15 | #expect(segments.first { $0.text == "TODO" }?.roles.contains(.todo) == true) | 18 | #expect(segments.first { $0.text == "TODO" }?.roles.contains(.todo) == true) |