-
-
Notifications
You must be signed in to change notification settings - Fork 329
fix(editor): dismiss autocomplete popup on inert clicks and constrain it to the editor #1833
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
...Editor/Sources/CodeEditSourceEditor/CodeSuggestion/Window/SuggestionWindowPlacement.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| // | ||
| // SuggestionWindowPlacement.swift | ||
| // CodeEditSourceEditor | ||
| // | ||
|
|
||
| import AppKit | ||
|
|
||
| /// The fixed anchor for a completion session: where the caret is, the editor font, | ||
| /// and the editor's screen frame. Retained by ``SuggestionController`` so placement can be | ||
| /// re-derived on every resize instead of patched from the panel's previous frame. | ||
| internal struct SuggestionPlacementAnchor { | ||
| let cursorRect: NSRect | ||
| let font: NSFont | ||
| let editorFrame: NSRect? | ||
| } | ||
|
|
||
| /// Pure placement math for the suggestion panel. Operates on plain rects so it is unit testable | ||
| /// without an `NSWindow`, and is re-invoked on every resize so the panel is re-derived from the | ||
| /// same caret anchor rather than drifting from its last frame as its content grows. | ||
| internal enum SuggestionWindowPlacement { | ||
| internal static let edgePadding: CGFloat = 22 | ||
|
|
||
| internal struct Placement: Equatable { | ||
| let origin: NSPoint | ||
| let isAboveCursor: Bool | ||
| } | ||
|
|
||
| internal static func compute( | ||
| windowSize: NSSize, | ||
| cursorRect: NSRect, | ||
| font: NSFont, | ||
| screenFrame: NSRect, | ||
| editorFrame: NSRect? | ||
| ) -> Placement { | ||
| let x = clampedX( | ||
| windowSize: windowSize, | ||
| cursorRect: cursorRect, | ||
| font: font, | ||
| screenFrame: screenFrame, | ||
| editorFrame: editorFrame | ||
| ) | ||
| let (y, isAboveCursor) = clampedY( | ||
| windowSize: windowSize, | ||
| cursorRect: cursorRect, | ||
| screenFrame: screenFrame, | ||
| editorFrame: editorFrame | ||
| ) | ||
| return Placement(origin: NSPoint(x: x, y: y), isAboveCursor: isAboveCursor) | ||
| } | ||
|
|
||
| private static func clampedX( | ||
| windowSize: NSSize, | ||
| cursorRect: NSRect, | ||
| font: NSFont, | ||
| screenFrame: NSRect, | ||
| editorFrame: NSRect? | ||
| ) -> CGFloat { | ||
| let anchorX = cursorRect.origin.x - SuggestionController.WINDOW_PADDING | ||
| - CodeSuggestionLabelView.HORIZONTAL_PADDING - font.pointSize | ||
|
|
||
| let leftLimit = max(screenFrame.minX, editorFrame?.minX ?? screenFrame.minX) | ||
| let rightLimit = min(screenFrame.maxX, editorFrame?.maxX ?? screenFrame.maxX) | ||
|
|
||
| let minX = leftLimit + edgePadding | ||
| let maxX = rightLimit - windowSize.width - edgePadding | ||
|
|
||
| guard maxX >= minX else { return minX } | ||
| if anchorX < minX { return minX } | ||
| if anchorX > maxX { return maxX } | ||
| return anchorX | ||
| } | ||
|
|
||
| private static func clampedY( | ||
| windowSize: NSSize, | ||
| cursorRect: NSRect, | ||
| screenFrame: NSRect, | ||
| editorFrame: NSRect? | ||
| ) -> (y: CGFloat, isAboveCursor: Bool) { | ||
| let lowerLimit = max(screenFrame.minY, editorFrame?.minY ?? screenFrame.minY) | ||
| let upperLimit = min(screenFrame.maxY, editorFrame?.maxY ?? screenFrame.maxY) | ||
|
|
||
| guard cursorRect.origin.y - windowSize.height < lowerLimit else { | ||
| var topLeftY = cursorRect.origin.y | ||
| let maxTopLeftY = upperLimit - edgePadding | ||
| if topLeftY > maxTopLeftY { | ||
| topLeftY = maxTopLeftY | ||
| } | ||
| return (topLeftY - windowSize.height, false) | ||
| } | ||
|
|
||
| var bottomLeftY: CGFloat | ||
| if cursorRect.origin.y < lowerLimit { | ||
| bottomLeftY = lowerLimit + edgePadding | ||
| } else { | ||
| bottomLeftY = cursorRect.origin.y + cursorRect.height | ||
| } | ||
|
|
||
| if bottomLeftY + windowSize.height > upperLimit { | ||
| bottomLeftY = max(lowerLimit, upperLimit - windowSize.height) | ||
| } | ||
|
|
||
| return (bottomLeftY, true) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...r/Tests/CodeEditSourceEditorTests/CodeSuggestion/SuggestionControllerPlacementTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import AppKit | ||
| @testable import CodeEditSourceEditor | ||
| import XCTest | ||
|
|
||
| final class SuggestionControllerPlacementTests: XCTestCase { | ||
| @MainActor | ||
| func test_updateWindowSize_reclampsXAfterGrowingPastInitialPlacement() throws { | ||
| let controller = SuggestionController() | ||
| guard let window = controller.window, let screen = window.screen else { | ||
| throw XCTSkip("No screen available in this environment") | ||
| } | ||
|
|
||
| let editorFrame = NSRect( | ||
| x: screen.visibleFrame.minX + 50, | ||
| y: screen.visibleFrame.minY + 50, | ||
| width: 400, | ||
| height: 300 | ||
| ) | ||
| let cursorRect = NSRect(x: editorFrame.maxX - 30, y: editorFrame.minY + 150, width: 6, height: 16) | ||
|
|
||
| controller.constrainWindowToScreenEdges( | ||
| cursorRect: cursorRect, font: .systemFont(ofSize: 12), editorFrame: editorFrame | ||
| ) | ||
| controller.updateWindowSize(newSize: NSSize(width: 280, height: 200)) | ||
|
|
||
| XCTAssertLessThanOrEqual(controller.window?.frame.maxX ?? .infinity, editorFrame.maxX) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the caret is near the top of a short editor and the popup height still technically fits below it, this top-edge padding clamp can lower
topLeftYand then returntopLeftY - windowSize.heightwithout recheckinglowerLimit. For example, with an editor from y=100...250, a 140pt popup, and a caret at y=245, the unclamped popup would fit at y=105...245, but clamping the top toupperLimit - 22returns an origin of 88, so the resized popup overlaps the status/buttons below the editor—the case this change is trying to prevent during resize re-placement.Useful? React with 👍 / 👎.