Skip to content

Commit

Permalink
Getting TextSystem building for iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmassicotte committed May 1, 2024
1 parent 535b689 commit 59076ed
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 40 deletions.
7 changes: 5 additions & 2 deletions Edit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3901,6 +3901,9 @@
};
C9FE53B42A7677E800CACA1A /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
platformFilters = (
macos,
);
productRef = C9FE53B32A7677E800CACA1A /* ScrollViewPlus */;
};
C9FE53BB2A77353900CACA1A /* PBXTargetDependency */ = {
Expand Down Expand Up @@ -4738,7 +4741,7 @@
repositoryURL = "https://github.com/mattmassicotte/nsui.git";
requirement = {
kind = revision;
revision = b6c32973b2a9dc16c465f3e3bc44e9abb27d1683;
revision = 8792b2f789bf03be1be9ecb4df43d2bfe0e5d361;
};
};
C9BFA1AE2BC0927A00E86487 /* XCRemoteSwiftPackageReference "Glyph" */ = {
Expand Down Expand Up @@ -4786,7 +4789,7 @@
repositoryURL = "https://github.com/ChimeHQ/ThemePark";
requirement = {
kind = revision;
revision = 0843e25fdcfc9e6e197b39d668c29d8892240f1b;
revision = 11b195dd9771226a6be2b032381b1a7fa3569621;
};
};
C9FE52E62A75375300CACA1A /* XCRemoteSwiftPackageReference "ContainedDocument" */ = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/mattmassicotte/nsui.git",
"state" : {
"revision" : "b6c32973b2a9dc16c465f3e3bc44e9abb27d1683"
"revision" : "8792b2f789bf03be1be9ecb4df43d2bfe0e5d361"
}
},
{
Expand Down Expand Up @@ -228,7 +228,7 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/ChimeHQ/ThemePark",
"state" : {
"revision" : "0843e25fdcfc9e6e197b39d668c29d8892240f1b"
"revision" : "11b195dd9771226a6be2b032381b1a7fa3569621"
}
},
{
Expand Down
8 changes: 4 additions & 4 deletions Edit/Modules/TextSystem/NSTextView+Additions.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import AppKit
import NSUI

extension NSTextView {
extension NSUITextView {
func replaceTextStorage(_ newStorage: NSTextStorage) {
if let contentStorage = textContentStorage {
if let contentStorage = nsuiTextContentStorage {
contentStorage.textStorage = newStorage
return
}

if let layoutManager = layoutManager {
if let layoutManager = nsuiLayoutManager {
layoutManager.replaceTextStorage(newStorage)
return
}
Expand Down
8 changes: 5 additions & 3 deletions Edit/Modules/TextSystem/NSTextView+Mutations.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import AppKit
import NSUI

import DocumentContent

extension NSTextView {
extension NSUITextView {
func applyMutations(_ mutations: [TextStorageMutation]) {
guard let storage = nsuiTextStorage else { fatalError() }

for mutation in mutations {
for rangedString in mutation.stringMutations {
replaceCharacters(in: rangedString.range, with: rangedString.string)
storage.replaceCharacters(in: rangedString.range, with: rangedString.string)
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions Edit/Modules/TextSystem/TextLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,29 @@ import Rearrange
public struct TextLayout {
public struct LineFragment {
public let range: NSRange
public let bounds: NSRect
public let bounds: CGRect
}

public let visibleRect: () -> NSRect
public let visibleRect: () -> CGRect
public let visibleSet: () -> IndexSet
public let lineFragmentsInRect: (NSRect) -> [LineFragment]
public let lineFragmentsInRect: (CGRect) -> [LineFragment]
public let lineFragmentsInRange: (NSRange) -> [LineFragment]
}

extension TextLayout {
@MainActor
public init(textView: NSUITextView) {
self.init(container: textView.textContainer!)
self.init(container: textView.nsuiTextContainer!, textView: textView)
}

@MainActor
public init(container: NSTextContainer) {
public init(container: NSTextContainer, textView: NSUITextView) {
self.init(
visibleRect: {
container.textView!.visibleRect
textView.visibleRect
},
visibleSet: {
container.textView!.visibleCharacterIndexes
textView.visibleCharacterIndexes
},
lineFragmentsInRect: { rect in
var fragments = [LineFragment]()
Expand Down
24 changes: 12 additions & 12 deletions Edit/Modules/TextSystem/TextPresentation.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import AppKit
import Foundation

import NSUI
import Rearrange

public struct TextPresentation {
Expand All @@ -9,16 +9,16 @@ public struct TextPresentation {

extension TextPresentation {
@MainActor
public init(textView: NSTextView) {
public init(textView: NSUITextView) {
if let textLayoutManager = textView.textLayoutManager {
self.init(textLayoutManager: textLayoutManager)
self.init(textLayoutManager: textLayoutManager, textView: textView)
} else {
self.init(layoutManager: textView.layoutManager!)
self.init(layoutManager: textView.nsuiLayoutManager!)
}
}

@MainActor
public init(textLayoutManager: NSTextLayoutManager) {
public init(textLayoutManager: NSTextLayoutManager, textView: NSUITextView) {
self.init(
applyRenderingStyle: { attrs, range in
guard
Expand All @@ -28,17 +28,13 @@ extension TextPresentation {
return
}

let textView = textLayoutManager.textContainer?.textView

let selection = textView?.selectedRanges
let selection = textView.selectedRanges

textLayoutManager.setRenderingAttributes(attrs, for: textRange)

textView?.selectedRanges = [NSValue(range: range)]
textView.selectedRanges = [NSValue(range: range)]

if let selection {
textView?.selectedRanges = selection
}
textView.selectedRanges = selection
}
)
}
Expand All @@ -47,7 +43,11 @@ extension TextPresentation {
public init(layoutManager: NSLayoutManager) {
self.init(
applyRenderingStyle: { attrs, range in
#if os(macOS)
layoutManager.setTemporaryAttributes(attrs, forCharacterRange: range)
#else
print("unsupported rendering style")
#endif
}
)
}
Expand Down
20 changes: 12 additions & 8 deletions Edit/Modules/TextSystem/TextViewSystem.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import AppKit
import Foundation

import ChimeKit
import DocumentContent
import NSUI
import TextStory
import Theme

Expand All @@ -17,10 +17,10 @@ public final class TextViewSystem: NSObject {
private var contentVersion = 0
public private(set) var contentIdentity = DocumentContentIdentity()

let textView: NSTextView
let textView: NSUITextView
public private(set) var textMetrics: TextMetrics

public init(textView: NSTextView) {
public init(textView: NSUITextView) {
self.textView = textView
self.textMetrics = TextMetrics(storage: Storage.null())

Expand Down Expand Up @@ -67,27 +67,29 @@ extension TextViewSystem {
}

private func beginEditing() {
textView.textStorage?.beginEditing()
textView.nsuiTextStorage?.beginEditing()
}

private func endEditing() {
textView.textStorage?.endEditing()
textView.nsuiTextStorage?.endEditing()

contentVersion += 1

#if os(macOS)
textView.didChangeText()
#endif
}

private func length(for version: Version) -> Int? {
guard contentVersion == version else {
return nil
}

return textView.textStorage?.length
return textView.nsuiTextStorage?.length
}

private func substring(range: NSRange, version: Version) throws -> String {
guard let storage = textView.textStorage else {
guard let storage = textView.nsuiTextStorage else {
throw TextStorageError.underlyingStorageInvalid
}

Expand Down Expand Up @@ -186,7 +188,7 @@ extension TextViewSystem {
}

public func themeChanged(attributes: [NSAttributedString.Key: Any]) {
guard let storage = textView.textStorage else {
guard let storage = textView.nsuiTextStorage else {
fatalError("")
}

Expand All @@ -209,11 +211,13 @@ extension TextViewSystem {
//}

extension TextViewSystem: TSYTextStorageDelegate {
#if os(macOS)
public nonisolated func textStorage(_ textStorage: TSYTextStorage, doubleClickRangeForLocation location: UInt) -> NSRange {
textStorage.internalStorage.doubleClick(at: Int(location))
}

public nonisolated func textStorage(_ textStorage: TSYTextStorage, nextWordIndexFromLocation location: UInt, direction forward: Bool) -> UInt {
UInt(textStorage.internalStorage.nextWord(from: Int(location), forward: forward))
}
#endif
}
4 changes: 4 additions & 0 deletions Edit/Modules/Theme/ThemeStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,23 @@ public final class ThemeStore {
}

private func loadXcodeThemes() {
#if os(macOS)
for (name, theme) in XcodeTheme.all {
let identity = Theme.Identity(source: .xcode, name: name)

cacheStyler(theme, with: identity)
}
#endif
}

private func loadTextMateThemes() {
#if os(macOS)
for theme in TextMateTheme.all {
let identity = Theme.Identity(source: .textmate, name: theme.name)

cacheStyler(theme, with: identity)
}
#endif
}

private func cacheStyler(_ styler: any Styling, with identity: Theme.Identity) {
Expand Down
2 changes: 0 additions & 2 deletions Edit/Modules/UIUtility/RoundedCorners.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,3 @@ extension View {
clipShape(RoundedCornersShape(radius: radius, corners: corners) )
}
}


0 comments on commit 59076ed

Please sign in to comment.