Skip to content

Commit 65931d3

Browse files
committed
Improve completion UI appearance
Use NSVisualEffectView for the completion background to match window appearance. Increase corner radius and use continuous corner curve for a smoother look. Adjust content insets and selection background appearance.
1 parent f51a88e commit 65931d3

File tree

1 file changed

+35
-9
lines changed

1 file changed

+35
-9
lines changed

Sources/STTextViewAppKit/STCompletion/STCompletionViewController.swift

+35-9
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,23 @@ open class STCompletionViewController: NSViewController, STCompletionViewControl
2323
view = NSView()
2424
view.translatesAutoresizingMaskIntoConstraints = false
2525
view.wantsLayer = true
26-
view.layer?.cornerRadius = 5
27-
view.layer?.backgroundColor = NSColor.windowBackgroundColor.cgColor
26+
view.layer?.cornerRadius = 8
27+
view.layer?.cornerCurve = .continuous
2828

2929
NSLayoutConstraint.activate(
3030
[
3131
view.widthAnchor.constraint(greaterThanOrEqualToConstant: 320)
3232
]
3333
)
3434

35+
let backgroundEffect = NSVisualEffectView(frame: view.bounds)
36+
backgroundEffect.autoresizingMask = [.width, .height]
37+
backgroundEffect.blendingMode = .withinWindow
38+
backgroundEffect.material = .windowBackground
39+
backgroundEffect.state = .followsWindowActiveState
40+
backgroundEffect.wantsLayer = true
41+
view.addSubview(backgroundEffect)
42+
3543
tableView.style = .plain
3644
tableView.translatesAutoresizingMaskIntoConstraints = false
3745
tableView.headerView = nil
@@ -61,7 +69,7 @@ open class STCompletionViewController: NSViewController, STCompletionViewControl
6169
let scrollView = NSScrollView()
6270
scrollView.translatesAutoresizingMaskIntoConstraints = false
6371
scrollView.automaticallyAdjustsContentInsets = false
64-
scrollView.contentInsets = NSEdgeInsets(top: 4, left: 4, bottom: 4, right: 4)
72+
scrollView.contentInsets = NSEdgeInsets(top: 6, left: 6, bottom: 6, right: 6)
6573
scrollView.drawsBackground = false
6674
scrollView.backgroundColor = .clear
6775
scrollView.borderType = .noBorder
@@ -179,7 +187,7 @@ extension STCompletionViewController: NSTableViewDelegate {
179187
}
180188

181189
open func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? {
182-
STTableRowView()
190+
STTableRowView(parentCornerRadius: view.layer!.cornerRadius, inset: tableView.enclosingScrollView?.contentInsets.top ?? 0)
183191
}
184192

185193
}
@@ -192,13 +200,31 @@ extension STCompletionViewController: NSTableViewDataSource {
192200

193201
private class STTableRowView: NSTableRowView {
194202

203+
private let parentCornerRadius: CGFloat
204+
private let inset: CGFloat
205+
206+
init(parentCornerRadius: CGFloat, inset: CGFloat) {
207+
self.parentCornerRadius = parentCornerRadius * 2
208+
self.inset = inset
209+
super.init(frame: .zero)
210+
}
211+
212+
required init?(coder: NSCoder) {
213+
fatalError("init(coder:) has not been implemented")
214+
}
215+
195216
override func drawSelection(in dirtyRect: NSRect) {
196217
guard let context = NSGraphicsContext.current?.cgContext else { return }
197-
context.saveGState()
198-
let path = NSBezierPath(roundedRect: dirtyRect, xRadius: 4, yRadius: 4)
199-
context.setFillColor(NSColor.controlAccentColor.withAlphaComponent(0.7).cgColor)
200-
path.fill()
201-
context.restoreGState()
218+
if isSelected {
219+
context.saveGState()
220+
let isDark = effectiveAppearance.bestMatch(from: [.darkAqua, .aqua]) == .darkAqua
221+
222+
let radius = (parentCornerRadius - inset) / 2
223+
let path = NSBezierPath(roundedRect: bounds, xRadius: radius, yRadius: radius)
224+
context.setFillColor(NSColor.white.withAlphaComponent(isDark ? 0.2 : 1).cgColor)
225+
path.fill()
226+
context.restoreGState()
227+
}
202228
}
203229
}
204230

0 commit comments

Comments
 (0)