Improves helder
This commit is contained in:
@@ -39,17 +39,20 @@ enum OverlayKey: Equatable {
|
||||
}
|
||||
|
||||
/// A borderless single-line field that hands navigation keys to the overlay before the text system sees them.
|
||||
/// It fills the frame it is given and centres the text in it, so the whole box takes the I-beam and the click.
|
||||
struct OverlayCommandField: NSViewRepresentable {
|
||||
@Binding var text: String
|
||||
var placeholder: String
|
||||
var font: NSFont
|
||||
/// nil selects the whole text on focus, so typing replaces a seeded query.
|
||||
var initialSelection: NSRange?
|
||||
/// Space between the box's edges and the text.
|
||||
var horizontalInset: CGFloat = 0
|
||||
var onKey: (OverlayKey) -> Bool
|
||||
|
||||
func makeCoordinator() -> Coordinator { Coordinator(self) }
|
||||
|
||||
func makeNSView(context: Context) -> NSTextField {
|
||||
func makeNSView(context: Context) -> CommandFieldBox {
|
||||
let field = FocusingTextField()
|
||||
field.isBordered = false
|
||||
field.drawsBackground = false
|
||||
@@ -65,12 +68,19 @@ struct OverlayCommandField: NSViewRepresentable {
|
||||
field.cell?.isScrollable = true
|
||||
field.cell?.wraps = false
|
||||
field.initialSelection = initialSelection
|
||||
return field
|
||||
return CommandFieldBox(field: field, inset: horizontalInset)
|
||||
}
|
||||
|
||||
func updateNSView(_ field: NSTextField, context: Context) {
|
||||
func updateNSView(_ box: CommandFieldBox, context: Context) {
|
||||
context.coordinator.parent = self
|
||||
if field.stringValue != text { field.stringValue = text }
|
||||
if box.field.stringValue != text { box.field.stringValue = text }
|
||||
}
|
||||
|
||||
func sizeThatFits(_ proposal: ProposedViewSize, nsView box: CommandFieldBox, context: Context) -> CGSize? {
|
||||
let fieldHeight = box.field.intrinsicContentSize.height
|
||||
let width = proposal.width.flatMap { $0.isFinite ? $0 : nil } ?? max(box.field.intrinsicContentSize.width, 0) + 2 * box.inset
|
||||
let height = proposal.height.flatMap { $0.isFinite ? max($0, fieldHeight) : nil } ?? fieldHeight
|
||||
return CGSize(width: width, height: height)
|
||||
}
|
||||
|
||||
final class Coordinator: NSObject, NSTextFieldDelegate {
|
||||
@@ -92,6 +102,38 @@ struct OverlayCommandField: NSViewRepresentable {
|
||||
}
|
||||
}
|
||||
|
||||
/// The box around an `OverlayCommandField`: an I-beam over all of it, and a click anywhere focuses the field.
|
||||
final class CommandFieldBox: NSView {
|
||||
let field: NSTextField
|
||||
let inset: CGFloat
|
||||
|
||||
init(field: NSTextField, inset: CGFloat) {
|
||||
self.field = field
|
||||
self.inset = inset
|
||||
super.init(frame: .zero)
|
||||
addSubview(field)
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("CommandFieldBox is built in code")
|
||||
}
|
||||
|
||||
override func layout() {
|
||||
super.layout()
|
||||
let height = min(field.intrinsicContentSize.height, bounds.height)
|
||||
field.frame = NSRect(x: inset, y: ((bounds.height - height) / 2).rounded(), width: max(0, bounds.width - 2 * inset), height: height)
|
||||
}
|
||||
|
||||
override func resetCursorRects() {
|
||||
addCursorRect(bounds, cursor: .iBeam)
|
||||
}
|
||||
|
||||
override func mouseDown(with event: NSEvent) {
|
||||
guard field.currentEditor() == nil else { return }
|
||||
window?.makeFirstResponder(field)
|
||||
}
|
||||
}
|
||||
|
||||
/// An invisible first responder that forwards the overlay keys and swallows the ones it handles.
|
||||
struct OverlayKeyCatcher: NSViewRepresentable {
|
||||
var onKey: (OverlayKey) -> Bool
|
||||
@@ -138,6 +180,11 @@ private final class FocusingTextField: NSTextField {
|
||||
var initialSelection: NSRange?
|
||||
private var focused = false
|
||||
|
||||
override class var cellClass: AnyClass? {
|
||||
get { PointerBoundFieldCell.self }
|
||||
set {}
|
||||
}
|
||||
|
||||
override func viewDidMoveToWindow() {
|
||||
super.viewDidMoveToWindow()
|
||||
guard !focused, window != nil else { return }
|
||||
@@ -152,3 +199,16 @@ private final class FocusingTextField: NSTextField {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Edits through a PointerBoundTextView instead of the window's shared field editor, which sets the I-beam on every mouse move.
|
||||
private final class PointerBoundFieldCell: NSTextFieldCell {
|
||||
private lazy var editor: PointerBoundTextView = {
|
||||
let editor = PointerBoundTextView()
|
||||
editor.isFieldEditor = true
|
||||
return editor
|
||||
}()
|
||||
|
||||
override func fieldEditor(for controlView: NSView) -> NSTextView? {
|
||||
editor
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user