Improves helder
This commit is contained in:
@@ -84,8 +84,7 @@ struct NamePromptOverlay: View {
|
||||
EscChip()
|
||||
}
|
||||
OverlayCommandField(text: $name, placeholder: text.placeholder, font: TypeRole(.monoRegular, 13).nsFont,
|
||||
initialSelection: text.initialSelection, onKey: handle)
|
||||
.padding(.horizontal, 10)
|
||||
initialSelection: text.initialSelection, horizontalInset: 10, onKey: handle)
|
||||
.frame(height: 38)
|
||||
.background(Palette.editor, in: RoundedRectangle(cornerRadius: Metrics.radiusSmall))
|
||||
.focusRing(true)
|
||||
|
||||
@@ -62,6 +62,7 @@ private struct NoteEditor: NSViewRepresentable {
|
||||
textView.autoresizingMask = [.width]
|
||||
textView.textContainer?.widthTracksTextView = true
|
||||
textView.maxSize = NSSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)
|
||||
textView.fillsClipView = true
|
||||
scroll.documentView = textView
|
||||
let role = Typography.note
|
||||
let paragraph = NSMutableParagraphStyle()
|
||||
@@ -119,7 +120,7 @@ private struct NoteEditor: NSViewRepresentable {
|
||||
}
|
||||
|
||||
/// Catches ⌘P before the menu bar's ⌘P (push) and draws the placeholder.
|
||||
private final class NoteTextView: NSTextView {
|
||||
private final class NoteTextView: PointerBoundTextView {
|
||||
var onKey: (OverlayKey) -> Bool = { _ in false }
|
||||
var placeholder = ""
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -173,7 +173,6 @@ struct SearchOverlay: View {
|
||||
ForEach(group.rows) { row in
|
||||
let selected = model.column == .project && model.projectSelection == row.index
|
||||
hitLine(row.hit, selected: selected)
|
||||
.id(row.index)
|
||||
.onHover { inside in if inside { hover(.project, row: row.index) } }
|
||||
.onTapGesture { open(SearchModel.Target(path: row.path, line: row.hit.line)) }
|
||||
}
|
||||
@@ -181,7 +180,10 @@ struct SearchOverlay: View {
|
||||
}
|
||||
.padding(.bottom, 12)
|
||||
}
|
||||
.onChange(of: model.projectSelection) { _, row in proxy.scrollTo(row) }
|
||||
// Scrolls by the ForEach id: an extra `.id` on a row makes the LazyVStack keep stale rows.
|
||||
.onChange(of: model.projectSelection) { _, row in
|
||||
if model.hitRows.indices.contains(row) { proxy.scrollTo(model.hitRows[row].id) }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -238,13 +240,14 @@ struct SearchOverlay: View {
|
||||
ForEach(Array(matches.enumerated()), id: \.element.path) { index, match in
|
||||
let selected = model.column == .files && model.fileSelection == index
|
||||
fileRow(match, selected: selected)
|
||||
.id(index)
|
||||
.onHover { inside in if inside { hover(.files, row: index) } }
|
||||
.onTapGesture { open(SearchModel.Target(path: match.path, line: nil)) }
|
||||
}
|
||||
}
|
||||
}
|
||||
.onChange(of: model.fileSelection) { _, row in proxy.scrollTo(row) }
|
||||
.onChange(of: model.fileSelection) { _, row in
|
||||
if matches.indices.contains(row) { proxy.scrollTo(matches[row].path) }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user