-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
876364c
commit 18d6ef8
Showing
20 changed files
with
1,474 additions
and
295 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import AppKit | ||
|
||
import IBeam | ||
import Glyph | ||
import Ligature | ||
|
||
extension IBeam.TextGranularity { | ||
var ligatureGranulaity: Ligature.TextGranularity { | ||
switch self { | ||
case .character: .character | ||
case .word: .word | ||
case .line: .line | ||
} | ||
} | ||
} | ||
|
||
@MainActor | ||
public struct IBeamTextViewSystem { | ||
let textView: NSTextView | ||
let tokenizer: UTF16CodePointTextViewTextTokenizer | ||
|
||
public init(textView: NSTextView) { | ||
self.textView = textView | ||
self.tokenizer = UTF16CodePointTextViewTextTokenizer(textView: textView) | ||
} | ||
|
||
private var partialSystem: MutableStringPartialSystem { | ||
MutableStringPartialSystem(textView.textStorage ?? NSTextStorage()) | ||
} | ||
} | ||
|
||
extension IBeamTextViewSystem : @preconcurrency IBeam.TextSystem { | ||
public typealias TextRange = NSRange | ||
public typealias TextPosition = Int | ||
|
||
public func location(for position: TextPosition) -> CGFloat? { | ||
tokenizer.location(for: position) | ||
} | ||
|
||
// movement calculation | ||
public func position(from position: TextPosition, moving direction: IBeam.TextDirection, by granularity: IBeam.TextGranularity) -> TextPosition? { | ||
let ligGranularity = granularity.ligatureGranulaity | ||
|
||
switch direction { | ||
case .forward: | ||
return tokenizer.position(from: position, toBoundary: ligGranularity, inDirection: .storage(.forward)) | ||
case .backward: | ||
return tokenizer.position(from: position, toBoundary: ligGranularity, inDirection: .storage(.backward)) | ||
case .left: | ||
return tokenizer.position(from: position, toBoundary: ligGranularity, inDirection: .layout(.left)) | ||
case .right: | ||
return tokenizer.position(from: position, toBoundary: ligGranularity, inDirection: .layout(.right)) | ||
case let .down(alignment): | ||
return tokenizer.position(from: position, toBoundary: ligGranularity, inDirection: .layout(.down), alignment: alignment) | ||
case let .up(alignment): | ||
return tokenizer.position(from: position, toBoundary: ligGranularity, inDirection: .layout(.up), alignment: alignment) | ||
} | ||
} | ||
|
||
public func position(from start: TextPosition, offset: Int) -> TextPosition? { | ||
partialSystem.position(from: start, offset: offset) | ||
} | ||
|
||
public func layoutDirection(at position: TextPosition) -> IBeam.TextLayoutDirection? { | ||
partialSystem.layoutDirection(at: position) | ||
} | ||
|
||
// range calculation | ||
public var beginningOfDocument: TextPosition { partialSystem.beginningOfDocument } | ||
public var endOfDocument: TextPosition { partialSystem.endOfDocument } | ||
|
||
public func compare(_ position: TextPosition, to other: TextPosition) -> ComparisonResult { | ||
partialSystem.compare(position, to: other) | ||
} | ||
|
||
public func positions(composing range: TextRange) -> (TextPosition, TextPosition) { | ||
partialSystem.positions(composing: range) | ||
} | ||
|
||
public func textRange(from start: TextPosition, to end: TextPosition) -> TextRange? { | ||
partialSystem.textRange(from: start, to: end) | ||
} | ||
|
||
// content mutation | ||
public func beginEditing() { partialSystem.beginEditing() } | ||
public func endEditing() { partialSystem.endEditing() } | ||
|
||
public func applyMutation(_ range: TextRange, string: AttributedString) -> MutationOutput<TextRange>? { | ||
partialSystem.applyMutation(range, string: string) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import AppKit | ||
|
||
public enum InputOperation { | ||
case deleteBackwards(TextGranularity) | ||
case moveLeft(TextGranularity) | ||
case moveRight(TextGranularity) | ||
case moveUp | ||
case moveDown | ||
case insertText(AttributedString) | ||
case moveToLeftEndOfLine | ||
case moveToRightEndOfLine | ||
|
||
public static func insertText(_ value: String) -> InputOperation { | ||
Self.insertText(AttributedString(value)) | ||
} | ||
|
||
public init?(selector: Selector) { | ||
switch selector { | ||
case #selector(NSResponder.moveLeft(_:)): | ||
self = .moveLeft(.character) | ||
case #selector(NSResponder.moveRight(_:)): | ||
self = .moveRight(.character) | ||
case #selector(NSResponder.moveDown(_:)): | ||
self = .moveDown | ||
case #selector(NSResponder.moveUp(_:)): | ||
self = .moveUp | ||
case #selector(NSResponder.deleteBackward(_:)): | ||
self = .deleteBackwards(.character) | ||
case #selector(NSResponder.moveToLeftEndOfLine(_:)): | ||
self = .moveToLeftEndOfLine | ||
case #selector(NSResponder.moveToRightEndOfLine(_:)): | ||
self = .moveToRightEndOfLine | ||
default: | ||
return nil | ||
} | ||
} | ||
|
||
public var affectsAlignment: Bool { | ||
switch self { | ||
case .moveUp, .moveDown: | ||
return false | ||
default: | ||
return true | ||
} | ||
} | ||
} |
Oops, something went wrong.