Skip to content
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

feat(Studio): choose line number padding based on visible rows #83

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions Studio/CelesteStudio/Editing/Editor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ void UpdateRoomLabelIndices(int[] rowsToIgnore) {
private Point scrollablePosition;
private Size scrollableSize;

private int previousMaxDigits = -1;

private readonly PixelLayout pixelLayout = new();
private readonly PopupMenu autoCompleteMenu = new();
private readonly PopupMenu contextActionsMenu = new();
Expand Down Expand Up @@ -309,6 +311,8 @@ public PopupMenu? ActivePopupMenu {
private static readonly Regex TimestampRegex = new(@"^\s*#+\s*(\d+:)?\d{1,2}:\d{2}\.\d{3}\(\d+\)", RegexOptions.Compiled);
public static readonly Regex RoomLabelRegex = new(@"^#lvl_([^\(\)]*)(?:\s\((\d+)\))?$", RegexOptions.Compiled);

private const int offscreenLinePadding = 3;

public Editor(Document document, Scrollable scrollable) {
this.document = document;
this.scrollable = scrollable;
Expand Down Expand Up @@ -337,6 +341,16 @@ public Editor(Document document, Scrollable scrollable) {
// Need to redraw the line numbers when scrolling horizontally
scrollable.Scroll += (_, _) => {
scrollablePosition = scrollable.ScrollPosition;

int bottomVisualRow = (int)((scrollablePosition.Y + scrollableSize.Height) / Font.LineHeight()) + offscreenLinePadding;
int bottomRow = Math.Min(Document.Lines.Count - 1, GetActualRow(bottomVisualRow));

int maxDigits = bottomRow.Digits();
if (previousMaxDigits != maxDigits) {
Recalc();
}
previousMaxDigits = maxDigits;

Invalidate();
};
scrollable.SizeChanged += (_, _) => {
Expand Down Expand Up @@ -709,16 +723,19 @@ private void Recalc() {
// Clear invalid foldings
Document.RemoveAnchorsIf(anchor => anchor.UserData is CollapseAnchorData && foldings.All(fold => fold.MinRow != anchor.Row));

int bottomVisualRow = (int)((scrollablePosition.Y + scrollableSize.Height) / Font.LineHeight()) + offscreenLinePadding;
int bottomRow = Math.Min(Document.Lines.Count - 1, GetActualRow(bottomVisualRow));

// Calculate line numbers width
const float foldButtonPadding = 5.0f;
bool hasFoldings = Settings.Instance.ShowFoldIndicators && foldings.Count != 0;
// Only when the alignment is to the left, the folding indicator can fit into the existing space
float foldingWidth = !hasFoldings ? 0.0f : Settings.Instance.LineNumberAlignment switch {
LineNumberAlignment.Left => Font.CharWidth() * (foldings[^1].MinRow.Digits() + 1) + foldButtonPadding,
LineNumberAlignment.Right => Font.CharWidth() * (Document.Lines.Count.Digits() + 1) + foldButtonPadding,
LineNumberAlignment.Right => Font.CharWidth() * (bottomRow.Digits() + 1) + foldButtonPadding,
_ => throw new UnreachableException(),
};
textOffsetX = Math.Max(foldingWidth, Font.CharWidth() * Document.Lines.Count.Digits()) + LineNumberPadding * 3.0f;
textOffsetX = Math.Max(foldingWidth, Font.CharWidth() * bottomRow.Digits()) + LineNumberPadding * 3.0f;

const float paddingRight = 50.0f;
const float paddingBottom = 100.0f;
Expand Down Expand Up @@ -3639,8 +3656,6 @@ public override void Draw(SKSurface surface) {
// To be reused below. Kinda annoying how C# handles out parameter conflicts
WrapEntry wrap;

const int offscreenLinePadding = 3;

int topVisualRow = (int)(scrollablePosition.Y / Font.LineHeight()) - offscreenLinePadding;
int bottomVisualRow = (int)((scrollablePosition.Y + scrollableSize.Height) / Font.LineHeight()) + offscreenLinePadding;
int topRow = Math.Max(0, GetActualRow(topVisualRow));
Expand Down Expand Up @@ -3885,7 +3900,7 @@ public override void Draw(SKSurface surface) {
if (Settings.Instance.LineNumberAlignment == LineNumberAlignment.Left) {
canvas.DrawText(numberString, scrollablePosition.X + LineNumberPadding, yPos + Font.Offset(), Font, fillPaint);
} else if (Settings.Instance.LineNumberAlignment == LineNumberAlignment.Right) {
float ident = Font.CharWidth() * (Document.Lines.Count.Digits() - (row + 1).Digits());
float ident = Font.CharWidth() * (bottomRow - (row + 1).Digits());
canvas.DrawText(numberString, scrollablePosition.X + LineNumberPadding + ident, yPos + Font.Offset(), Font, fillPaint);
}

Expand Down
Loading