66//
77
88import AppKit
9+ import SwiftUI
910import Combine
1011
1112class SuggestionViewController : NSViewController {
1213 var tableView : NSTableView !
1314 var scrollView : NSScrollView !
14- var tintView : NSView !
1515 var noItemsLabel : NSTextField !
1616
1717 var itemObserver : AnyCancellable ?
18+
1819 weak var model : SuggestionViewModel ? {
1920 didSet {
2021 itemObserver? . cancel ( )
@@ -28,13 +29,7 @@ class SuggestionViewController: NSViewController {
2829 super. loadView ( )
2930 view. wantsLayer = true
3031 view. layer? . cornerRadius = 8.5
31- view. layer? . backgroundColor = NSColor . windowBackgroundColor. cgColor
32-
33- tintView = NSView ( )
34- tintView. translatesAutoresizingMaskIntoConstraints = false
35- tintView. wantsLayer = true
36- tintView. layer? . cornerRadius = 8.5
37- view. addSubview ( tintView)
32+ view. layer? . backgroundColor = . clear
3833
3934 tableView = NSTableView ( )
4035 configureTableView ( )
@@ -46,24 +41,19 @@ class SuggestionViewController: NSViewController {
4641 noItemsLabel. alignment = . center
4742 noItemsLabel. translatesAutoresizingMaskIntoConstraints = false
4843 noItemsLabel. isHidden = false
49- // TODO: GET FONT SIZE FROM THEME
50- noItemsLabel. font = . monospacedSystemFont( ofSize: 12 , weight: . regular)
5144
52- tintView . addSubview ( noItemsLabel)
53- tintView . addSubview ( scrollView)
45+ view . addSubview ( noItemsLabel)
46+ view . addSubview ( scrollView)
5447
5548 NSLayoutConstraint . activate ( [
56- tintView. topAnchor. constraint ( equalTo: view. topAnchor) ,
57- tintView. bottomAnchor. constraint ( equalTo: view. bottomAnchor) ,
58- tintView. leadingAnchor. constraint ( equalTo: view. leadingAnchor) ,
59- tintView. trailingAnchor. constraint ( equalTo: view. trailingAnchor) ,
60-
61- noItemsLabel. centerXAnchor. constraint ( equalTo: tintView. centerXAnchor) ,
62- noItemsLabel. centerYAnchor. constraint ( equalTo: tintView. centerYAnchor) ,
63- scrollView. topAnchor. constraint ( equalTo: tintView. topAnchor) ,
64- scrollView. leadingAnchor. constraint ( equalTo: tintView. leadingAnchor) ,
65- scrollView. trailingAnchor. constraint ( equalTo: tintView. trailingAnchor) ,
66- scrollView. bottomAnchor. constraint ( equalTo: tintView. bottomAnchor)
49+ noItemsLabel. centerXAnchor. constraint ( equalTo: view. centerXAnchor) ,
50+ noItemsLabel. topAnchor. constraint ( equalTo: view. topAnchor, constant: 10 ) ,
51+ noItemsLabel. bottomAnchor. constraint ( equalTo: view. bottomAnchor, constant: - 10 ) ,
52+
53+ scrollView. topAnchor. constraint ( equalTo: view. topAnchor) ,
54+ scrollView. leadingAnchor. constraint ( equalTo: view. leadingAnchor) ,
55+ scrollView. trailingAnchor. constraint ( equalTo: view. trailingAnchor) ,
56+ scrollView. bottomAnchor. constraint ( equalTo: view. bottomAnchor)
6757 ] )
6858 }
6959
@@ -77,6 +67,7 @@ class SuggestionViewController: NSViewController {
7767 }
7868
7969 func styleView( using controller: TextViewController ) {
70+ noItemsLabel. font = controller. font
8071 switch controller. systemAppearance {
8172 case . aqua:
8273 let color = controller. theme. background
@@ -87,15 +78,42 @@ class SuggestionViewController: NSViewController {
8778 blue: color. blueComponent * 0.95 ,
8879 alpha: 1.0
8980 )
90- tintView . layer? . backgroundColor = newColor. cgColor
81+ view . layer? . backgroundColor = newColor. cgColor
9182 } else {
92- tintView . layer? . backgroundColor = . clear
83+ view . layer? . backgroundColor = . clear
9384 }
9485 case . darkAqua:
95- tintView . layer? . backgroundColor = controller. theme. background. cgColor
86+ view . layer? . backgroundColor = controller. theme. background. cgColor
9687 default :
9788 return
9889 }
90+
91+ guard model? . items. isEmpty == false else {
92+ let size = NSSize ( width: 256 , height: noItemsLabel. fittingSize. height + 20 )
93+ preferredContentSize = size
94+ view. window? . setContentSize ( size)
95+ view. window? . contentMinSize = size
96+ view. window? . contentMaxSize = size
97+ return
98+ }
99+ guard let rowView = tableView. view ( atColumn: 0 , row: 0 , makeIfNecessary: true ) else {
100+ return
101+ }
102+ let rowHeight = rowView. fittingSize. height
103+
104+ let numberOfVisibleRows = min ( CGFloat ( model? . items. count ?? 0 ) , SuggestionController . MAX_VISIBLE_ROWS)
105+ let newHeight = rowHeight * numberOfVisibleRows + SuggestionController. WINDOW_PADDING * 2
106+
107+ let maxLength = min ( ( model? . items. max ( by: { $0. label. count < $1. label. count } ) ? . label. count ?? 16 ) + 4 , 48 )
108+ let newWidth = CGFloat ( maxLength) * controller. font. charWidth
109+
110+ view. constraints. filter ( { $0. firstAnchor == view. heightAnchor } ) . forEach { $0. isActive = false }
111+ view. heightAnchor. constraint ( equalToConstant: newHeight) . isActive = true
112+
113+ preferredContentSize = NSSize ( width: newWidth, height: newHeight)
114+ view. window? . setContentSize ( NSSize ( width: newWidth, height: newHeight) )
115+ view. window? . contentMinSize = NSSize ( width: newWidth, height: newHeight)
116+ view. window? . contentMaxSize = NSSize ( width: . infinity, height: newHeight)
99117 }
100118
101119 func configureTableView( ) {
@@ -107,9 +125,7 @@ class SuggestionViewController: NSViewController {
107125 tableView. allowsEmptySelection = false
108126 tableView. selectionHighlightStyle = . regular
109127 tableView. style = . plain
110- tableView. usesAutomaticRowHeights = false
111- tableView. rowSizeStyle = . custom
112- tableView. rowHeight = 21
128+ tableView. usesAutomaticRowHeights = true
113129 tableView. gridStyleMask = [ ]
114130 tableView. target = self
115131 tableView. action = #selector( tableViewClicked ( _: ) )
@@ -157,7 +173,7 @@ class SuggestionViewController: NSViewController {
157173 clipView. scroll ( to: NSPoint ( x: 0 , y: - SuggestionController. WINDOW_PADDING) )
158174
159175 // Select the first item
160- if ! ( model? . items. isEmpty ?? true ) {
176+ if model? . items. isEmpty == false {
161177 tableView. selectRowIndexes ( IndexSet ( integer: 0 ) , byExtendingSelection: false )
162178 }
163179 }
@@ -179,8 +195,19 @@ extension SuggestionViewController: NSTableViewDataSource, NSTableViewDelegate {
179195 }
180196
181197 public func tableView( _ tableView: NSTableView , viewFor tableColumn: NSTableColumn ? , row: Int ) -> NSView ? {
182- guard row >= 0 , row < model? . items. count ?? 0 else { return nil }
183- return model? . items [ row] . view
198+ guard let model = model,
199+ row >= 0 , row < model. items. count,
200+ let textView = model. activeTextView else {
201+ return nil
202+ }
203+ return NSHostingView (
204+ rootView: CodeSuggestionLabelView (
205+ suggestion: model. items [ row] ,
206+ labelColor: textView. theme. text. color,
207+ secondaryLabelColor: textView. theme. comments. color,
208+ font: textView. font
209+ )
210+ )
184211 }
185212
186213 public func tableView( _ tableView: NSTableView , rowViewForRow row: Int ) -> NSTableRowView ? {
0 commit comments