Skip to content

Commit

Permalink
fix(Studio): Positioning issues with the popup-menu
Browse files Browse the repository at this point in the history
  • Loading branch information
psyGamer committed Sep 22, 2024
1 parent 2894775 commit f27c2af
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
18 changes: 12 additions & 6 deletions Studio/CelesteStudio/Editing/Editor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -616,24 +616,26 @@ private void Recalc() {

// Update popup-menu position
if (ActivePopupMenu != null) {
const float menuXOffset = 8.0f;
const float menuYOffset = 7.0f;
const int menuXOffset = 8;
const int menuYOffset = 7;
const int menuLPadding = 7;
const int menuRPadding = 20;

float availableWidth = scrollablePosition.X + scrollableSize.Width - Font.CharWidth();
float carX = Font.CharWidth() * Document.Caret.Col;
float carY = Font.LineHeight() * (actualToVisualRows[Document.Caret.Row] + 1);

int menuX = (int)(carX + scrollablePosition.X + textOffsetX + menuXOffset);
int menuYBelow = (int)(carY + menuYOffset);
int menuYAbove = (int)Math.Max(carY - Font.LineHeight() - menuYOffset - ActivePopupMenu.ContentHeight, scrollablePosition.Y + menuYOffset);
int menuMaxW = (int)(availableWidth - menuX);
int menuMaxRight = scrollablePosition.X + scrollableSize.Width - menuRPadding;
int menuMaxW = menuMaxRight - menuX;
int menuMaxHBelow = (int)(scrollablePosition.Y + scrollableSize.Height - Font.LineHeight() - menuYBelow);
int menuMaxHAbove = (int)(scrollablePosition.Y + carY - Font.LineHeight() - menuYOffset - menuYAbove);

// Try moving the menu to the left when there isn't enough space, before having to shrink it
if (menuMaxW < ActivePopupMenu.ContentWidth) {
menuX = (int)Math.Max(availableWidth - ActivePopupMenu.ContentWidth, scrollablePosition.X + textOffsetX);
menuMaxW = (int)(availableWidth - menuX);
menuX = (int)Math.Max(menuMaxRight - ActivePopupMenu.ContentWidth, scrollablePosition.X + textOffsetX + menuLPadding);
menuMaxW = menuMaxRight - menuX;
}

// Chose above / below caret depending on which provides more height. Default to below
Expand All @@ -649,6 +651,7 @@ private void Recalc() {
// Set height first, to account for a potential scroll bar
ActivePopupMenu.ContentHeight = Math.Min(ActivePopupMenu.ContentHeight, menuMaxH);
ActivePopupMenu.ContentWidth = Math.Min(ActivePopupMenu.ContentWidth, menuMaxW);
ActivePopupMenu.Recalc();
pixelLayout.Move(ActivePopupMenu, menuX, menuY);
}

Expand Down Expand Up @@ -1338,6 +1341,7 @@ private void UpdateAutoComplete(bool open = true) {
await Application.Instance.InvokeAsync(() => {
autoCompleteMenu.Entries.Clear();
autoCompleteMenu.Entries.Add(loadingEntry);
autoCompleteMenu.Recalc();
Recalc();
}).ConfigureAwait(false);

Expand Down Expand Up @@ -1426,6 +1430,7 @@ await Application.Instance.InvokeAsync(() => {
if (!done) {
autoCompleteMenu.Entries.Add(loadingEntry);
}

autoCompleteMenu.Recalc();
Recalc();
}).ConfigureAwait(false);
Expand All @@ -1442,6 +1447,7 @@ await Application.Instance.InvokeAsync(() => {
} else {
autoCompleteMenu.Entries.Clear();
autoCompleteMenu.Recalc();
Recalc();
}

if (GetSelectedQuickEdit() is { } quickEdit && commandLine.Value.Arguments[^1] == quickEdit.DefaultText) {
Expand Down
9 changes: 4 additions & 5 deletions Studio/CelesteStudio/Editing/PopupMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ public ContentDrawable(PopupMenu menu) {
}

protected override void OnPaint(PaintEventArgs e) {
e.Graphics.FillPath(
Settings.Instance.Theme.PopupMenuBg,
GraphicsPath.GetRoundRect(new RectangleF(menu.ScrollPosition.X, menu.ScrollPosition.Y, menu.Width, menu.Height), Settings.Instance.Theme.PopupMenuBorderRounding));
e.Graphics.SetClip(GraphicsPath.GetRoundRect(new RectangleF(menu.ScrollPosition.X, menu.ScrollPosition.Y, menu.Width, menu.Height), Settings.Instance.Theme.PopupMenuBorderRounding));
e.Graphics.FillRectangle(Settings.Instance.Theme.PopupMenuBg, menu.ScrollPosition.X, menu.ScrollPosition.Y, menu.Width, menu.Height);

if (menu.shownEntries.Length == 0) {
return;
Expand Down Expand Up @@ -194,7 +193,7 @@ public int SelectedEntry {
}

public int ContentWidth {
set => Width = Math.Max(0, value + ScrollBarWidth);
set => Width = Math.Max(0, value);
get {
if (shownEntries.Length == 0) {
return 0;
Expand All @@ -204,7 +203,7 @@ public int ContentWidth {
int maxDisplayLen = shownEntries.Select(entry => entry.DisplayText.Length).Aggregate(Math.Max);
int maxExtraLen = shownEntries.Select(entry => entry.ExtraText.Length).Aggregate(Math.Max);

return (int)(font.CharWidth() * (maxDisplayLen + DisplayExtraPadding + maxExtraLen) + Settings.Instance.Theme.PopupMenuEntryHorizontalPadding * 2.0f + Settings.Instance.Theme.PopupMenuBorderPadding * 2);
return (int)(font.CharWidth() * (maxDisplayLen + DisplayExtraPadding + maxExtraLen) + Settings.Instance.Theme.PopupMenuEntryHorizontalPadding * 2.0f + Settings.Instance.Theme.PopupMenuBorderPadding * 2 + ScrollBarWidth);
}
}
public int ContentHeight {
Expand Down

0 comments on commit f27c2af

Please sign in to comment.