Skip to content

Commit

Permalink
Introduce STTextLayoutRangeView and add isCompletionActive
Browse files Browse the repository at this point in the history
- Rename TextLayoutRangeView to STTextLayoutRangeView and make it open
- Add image() to get an NSImage representation of the view
- Add isCompletionActive property to STTextView to check if the completion
  window is currently presented
  • Loading branch information
krzyzanowskim committed Dec 22, 2024
1 parent b9be040 commit 39f0378
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ import CoreGraphics

/// A view with content of range.
/// Used to provide image of a text eg. for dragging
final class TextLayoutRangeView: NSView {
open class STTextLayoutRangeView: NSView {
private let textLayoutManager: NSTextLayoutManager
private let textRange: NSTextRange

override var isFlipped: Bool {
public override var isFlipped: Bool {
#if os(macOS)
true
#else
false
#endif
}

override var intrinsicContentSize: NSSize {
frame.size
open override var intrinsicContentSize: NSSize {
bounds.size
}

init(textLayoutManager: NSTextLayoutManager, textRange: NSTextRange) {
public init(textLayoutManager: NSTextLayoutManager, textRange: NSTextRange) {
self.textLayoutManager = textLayoutManager
self.textRange = textRange

Expand All @@ -42,11 +42,18 @@ final class TextLayoutRangeView: NSView {
wantsLayer = true
}

required init?(coder: NSCoder) {
public required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func draw(_ dirtyRect: NSRect) {
open func image() -> NSImage {
let imageRep = bitmapImageRepForCachingDisplay(in: bounds)!
cacheDisplay(in: bounds, to: imageRep)

return NSImage(cgImage: imageRep.cgImage!, size: bounds.size)
}

open override func draw(_ dirtyRect: NSRect) {
guard let ctx = NSGraphicsContext.current?.cgContext else { return }

var origin: CGPoint = .zero
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extension STTextView {
return
}

let rangeView = TextLayoutRangeView(textLayoutManager: textLayoutManager, textRange: textRange)
let rangeView = STTextLayoutRangeView(textLayoutManager: textLayoutManager, textRange: textRange)
let imageRep = bitmapImageRepForCachingDisplay(in: rangeView.bounds)!
rangeView.cacheDisplay(in: rangeView.bounds, to: imageRep)

Expand Down
5 changes: 5 additions & 0 deletions Sources/STTextViewAppKit/STTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,11 @@ import AVFoundation
return STCompletionWindowController(viewController)
}()

/// Completion window is presented currently
open var isCompletionActive: Bool {
completionWindowController?.isVisible == true
}

/// Search-and-replace find interface inside a view.
open private(set) var textFinder: NSTextFinder

Expand Down

0 comments on commit 39f0378

Please sign in to comment.