@@ -9,7 +9,7 @@ import AppKit
99
1010extension SuggestionController {
1111 /// Will constrain the window's frame to be within the visible screen
12- public func constrainWindowToScreenEdges( cursorRect: NSRect , horizontalOffset : CGFloat ) {
12+ public func constrainWindowToScreenEdges( cursorRect: NSRect ) {
1313 guard let window = self . window,
1414 let screenFrame = window. screen? . visibleFrame else {
1515 return
@@ -18,7 +18,7 @@ extension SuggestionController {
1818 let windowSize = window. frame. size
1919 let padding : CGFloat = 22
2020 var newWindowOrigin = NSPoint (
21- x: cursorRect. origin. x - Self. WINDOW_PADDING - horizontalOffset ,
21+ x: cursorRect. origin. x - Self. WINDOW_PADDING,
2222 y: cursorRect. origin. y
2323 )
2424
@@ -64,17 +64,11 @@ extension SuggestionController {
6464 static func makeWindow( ) -> NSWindow {
6565 let window = NSWindow (
6666 contentRect: NSRect ( origin: . zero, size: self . DEFAULT_SIZE) ,
67- styleMask: [ . resizable, . fullSizeContentView, . nonactivatingPanel] ,
67+ styleMask: [ . resizable, . fullSizeContentView, . nonactivatingPanel, . utilityWindow ] ,
6868 backing: . buffered,
6969 defer: false
7070 )
7171
72- configureWindow ( window)
73- configureWindowContent ( window)
74- return window
75- }
76-
77- static func configureWindow( _ window: NSWindow ) {
7872 window. titleVisibility = . hidden
7973 window. titlebarAppearsTransparent = true
8074 window. isExcludedFromWindowsMenu = true
@@ -86,87 +80,8 @@ extension SuggestionController {
8680 window. hidesOnDeactivate = true
8781 window. backgroundColor = . clear
8882 window. minSize = Self . DEFAULT_SIZE
89- }
90-
91- static func configureWindowContent( _ window: NSWindow ) {
92- guard let contentView = window. contentView else { return }
93-
94- contentView. wantsLayer = true
95- // TODO: GET COLOR FROM THEME
96- contentView. layer? . backgroundColor = CGColor (
97- srgbRed: 31.0 / 255.0 ,
98- green: 31.0 / 255.0 ,
99- blue: 36.0 / 255.0 ,
100- alpha: 1.0
101- )
102- contentView. layer? . cornerRadius = 8.5
103- contentView. layer? . borderWidth = 1
104- contentView. layer? . borderColor = NSColor . gray. withAlphaComponent ( 0.45 ) . cgColor
105-
106- let innerShadow = NSShadow ( )
107- innerShadow. shadowColor = NSColor . black. withAlphaComponent ( 0.1 )
108- innerShadow. shadowOffset = NSSize ( width: 0 , height: - 1 )
109- innerShadow. shadowBlurRadius = 2
110- contentView. shadow = innerShadow
111- }
112-
113- func configureTableView( ) {
114- tableView. delegate = self
115- tableView. dataSource = self
116- tableView. headerView = nil
117- tableView. backgroundColor = . clear
118- tableView. intercellSpacing = . zero
119- tableView. allowsEmptySelection = false
120- tableView. selectionHighlightStyle = . regular
121- tableView. style = . plain
122- tableView. usesAutomaticRowHeights = false
123- tableView. rowSizeStyle = . custom
124- tableView. rowHeight = 21
125- tableView. gridStyleMask = [ ]
126- tableView. target = self
127- tableView. action = #selector( tableViewClicked ( _: ) )
128- let column = NSTableColumn ( identifier: NSUserInterfaceItemIdentifier ( " ItemsCell " ) )
129- tableView. addTableColumn ( column)
130- }
131-
132- @objc private func tableViewClicked( _ sender: Any ? ) {
133- if NSApp . currentEvent? . clickCount == 2 {
134- let row = tableView. selectedRow
135- guard row >= 0 , row < items. count else {
136- return
137- }
138- let selectedItem = items [ row]
139- delegate? . applyCompletionItem ( item: selectedItem)
140- self . close ( )
141- }
142- }
14383
144- func configureScrollView( ) {
145- scrollView. documentView = tableView
146- scrollView. hasVerticalScroller = true
147- scrollView. verticalScroller = NoSlotScroller ( )
148- scrollView. scrollerStyle = . overlay
149- scrollView. autohidesScrollers = true
150- scrollView. drawsBackground = false
151- scrollView. automaticallyAdjustsContentInsets = false
152- scrollView. translatesAutoresizingMaskIntoConstraints = false
153- scrollView. verticalScrollElasticity = . allowed
154- scrollView. contentInsets = NSEdgeInsets (
155- top: Self . WINDOW_PADDING,
156- left: 0 ,
157- bottom: Self . WINDOW_PADDING,
158- right: 0
159- )
160-
161- guard let contentView = window? . contentView else { return }
162- contentView. addSubview ( scrollView)
163-
164- NSLayoutConstraint . activate ( [
165- scrollView. topAnchor. constraint ( equalTo: contentView. topAnchor) ,
166- scrollView. leadingAnchor. constraint ( equalTo: contentView. leadingAnchor) ,
167- scrollView. trailingAnchor. constraint ( equalTo: contentView. trailingAnchor) ,
168- scrollView. bottomAnchor. constraint ( equalTo: contentView. bottomAnchor)
169- ] )
84+ return window
17085 }
17186
17287 /// Updates the item box window's height based on the number of items.
@@ -176,12 +91,9 @@ extension SuggestionController {
17691 return
17792 }
17893
179- noItemsLabel. isHidden = !items. isEmpty
180- scrollView. isHidden = items. isEmpty
181-
18294 // Update window dimensions
183- let numberOfVisibleRows = min ( CGFloat ( items. count) , Self . MAX_VISIBLE_ROWS)
184- let newHeight = items. count == 0 ?
95+ let numberOfVisibleRows = min ( CGFloat ( model . items. count) , Self . MAX_VISIBLE_ROWS)
96+ let newHeight = model . items. count == 0 ?
18597 Self . rowsToWindowHeight ( for: 1 ) : // Height for 1 row when empty
18698 Self . rowsToWindowHeight ( for: numberOfVisibleRows)
18799
@@ -206,15 +118,6 @@ extension SuggestionController {
206118 window. minSize = NSSize ( width: Self . DEFAULT_SIZE. width, height: newHeight)
207119 }
208120
209- func configureNoItemsLabel( ) {
210- window? . contentView? . addSubview ( noItemsLabel)
211-
212- NSLayoutConstraint . activate ( [
213- noItemsLabel. centerXAnchor. constraint ( equalTo: window!. contentView!. centerXAnchor) ,
214- noItemsLabel. centerYAnchor. constraint ( equalTo: window!. contentView!. centerYAnchor)
215- ] )
216- }
217-
218121 /// Calculate the window height for a given number of rows.
219122 static func rowsToWindowHeight( for numberOfRows: CGFloat ) -> CGFloat {
220123 let wholeRows = floor ( numberOfRows)
@@ -229,57 +132,3 @@ extension SuggestionController {
229132 return baseHeight + partialHeight + padding
230133 }
231134}
232-
233- extension SuggestionController : NSTableViewDataSource , NSTableViewDelegate {
234- public func numberOfRows( in tableView: NSTableView ) -> Int {
235- return items. count
236- }
237-
238- public func tableView( _ tableView: NSTableView , viewFor tableColumn: NSTableColumn ? , row: Int ) -> NSView ? {
239- guard row >= 0 , row < items. count else { return nil }
240- return items [ row] . view
241- }
242-
243- public func tableView( _ tableView: NSTableView , rowViewForRow row: Int ) -> NSTableRowView ? {
244- CodeSuggestionRowView ( )
245- }
246-
247- public func tableView( _ tableView: NSTableView , shouldSelectRow row: Int ) -> Bool {
248- // Only allow selection through keyboard navigation or single clicks
249- let event = NSApp . currentEvent
250- if event? . type == . leftMouseDragged {
251- return false
252- }
253- return true
254- }
255- }
256-
257- /// Used to draw a custom selection highlight for the table row
258- private class CodeSuggestionRowView : NSTableRowView {
259- override func drawSelection( in dirtyRect: NSRect ) {
260- guard isSelected else { return }
261- guard let context = NSGraphicsContext . current? . cgContext else { return }
262-
263- context. saveGState ( )
264- defer { context. restoreGState ( ) }
265-
266- // Create a rect that's inset from the edges and has proper padding
267- // TODO: We create a new selectionRect instead of using dirtyRect
268- // because there is a visual bug when holding down the arrow keys
269- // to select the first or last item, which draws a clipped
270- // rectangular highlight shape instead of the whole rectangle.
271- // Replace this when it gets fixed.
272- let selectionRect = NSRect (
273- x: SuggestionController . WINDOW_PADDING,
274- y: 0 ,
275- width: bounds. width - ( SuggestionController . WINDOW_PADDING * 2 ) ,
276- height: bounds. height
277- )
278- let cornerRadius : CGFloat = 5
279- let path = NSBezierPath ( roundedRect: selectionRect, xRadius: cornerRadius, yRadius: cornerRadius)
280- let selectionColor = NSColor . gray. withAlphaComponent ( 0.19 )
281-
282- context. setFillColor ( selectionColor. cgColor)
283- path. fill ( )
284- }
285- }
0 commit comments