Skip to content

Commit 31ce991

Browse files
committed
fix: Fixed the dropdown showing up in the top left corner instead of the mouse position
- Started using EditorDrawHelper.GetScreenWidth() that is more reliable for the multi-monitor setup. - Started checking if the button is too short for Unity to display the drop-down. - Replaced OR with AND to prevent frequent window resizing when _preventExpandingHeight is true.
1 parent 1eda4b9 commit 31ce991

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

Editor/TypeDropdown/DropdownWindow.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,18 @@ private void OnCreate(SelectionTree selectionTree, float windowHeight, Vector2 w
4545

4646
// If the window width is smaller than the distance from cursor to the right border of the window, the
4747
// window will not appear because the cursor is outside of the window and OnGUI will never be called.
48-
float distanceToRightBorder = Screen.currentResolution.width - windowPosition.x + 8f;
48+
float screenWidth = EditorDrawHelper.GetScreenWidth();
49+
float distanceToRightBorder = screenWidth - windowPosition.x + 8f;
4950
var windowSize = new Vector2(Mathf.Max(distanceToRightBorder, _optimalWidth), windowHeight);
5051

52+
// If the button is more than twice shorter than the dropdown menu, Unity thinks it does not fit to screen
53+
// and moves the dropdown to the top left corner of the screen.
54+
if (_optimalWidth > distanceToRightBorder * 2f)
55+
{
56+
distanceToRightBorder = _optimalWidth / 2f + 1f;
57+
windowPosition.x = screenWidth - distanceToRightBorder;
58+
}
59+
5160
// ShowAsDropDown usually shows the window under a button, but since we don't need to align the window to
5261
// any button, we set buttonRect.height to 0f.
5362
var buttonRect = new Rect(windowPosition, new Vector2(distanceToRightBorder, 0f));
@@ -92,7 +101,7 @@ private void AdjustSizeIfNeeded()
92101
if (_optimalWidth.DoesNotEqualApproximately(position.width))
93102
this.Resize(_optimalWidth);
94103

95-
if (_preventExpandingHeight || _contentHeight.DoesNotEqualApproximately(position.height))
104+
if (_preventExpandingHeight && _contentHeight.DoesNotEqualApproximately(position.height))
96105
this.Resize(height: Math.Min(_contentHeight, DropdownStyle.MaxWindowHeight));
97106
}
98107

0 commit comments

Comments
 (0)