Skip to content

Commit

Permalink
Improve cache behavior, avoid output box get focused to prevent auto …
Browse files Browse the repository at this point in the history
…scroll.
  • Loading branch information
zufuliu committed Jan 31, 2024
1 parent a8d92d3 commit fff1a50
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 16 deletions.
19 changes: 16 additions & 3 deletions FindInFilesForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,12 @@ private async void buttonStart_Click(object sender, EventArgs e) {
process.StartInfo.FileName = exePath;
process.StartInfo.Arguments = argument;
process.OutputDataReceived += Process_OutputDataReceived;
buttonFind.Enabled = false;
process.Start();
process.BeginOutputReadLine();
var error = await process.StandardError.ReadToEndAsync();
await process.WaitForExitAsync();
buttonFind.Enabled = true;
lineParser.Flush();
richTextBox.Invalidate();
if (!string.IsNullOrEmpty(error)) {
AppendText($"{error}{Environment.NewLine}", Color.Red);
}
Expand All @@ -106,17 +105,28 @@ private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
private readonly OutputLineParser lineParser;
private static readonly string MarkerPath = "\u200C"; // zero width non-joiner
private static readonly string MarkerLine = "\u200D"; // zero width joiner
private int visibleDelta = 0;

private void ClearMatchResult() {
var page = (int)(richTextBox.Height / richTextBox.GetLineHeight());
page += page / 4;
page = Math.Max(page, 5);
visibleDelta = page * 2;
richTextBox.Clear();
lineParser.MaxCachedLine = page;
lineParser.Clear();
}

private void RenderOutput(List<OutputLine> lines) {
var visible = richTextBox.Focused;
if (!visible) {
var last = richTextBox.GetLineFromCharIndex(-1);
if (last < visibleDelta) {
visible = true;
} else {
var top = richTextBox.GetLineFromCharIndex(richTextBox.GetCharIndexFromPosition(new Point(1, 1)));
visible = last - top < visibleDelta;
}
}
richTextBox.SetRedraw(false);
foreach (var line in lines) {
switch (line.LineType) {
Expand All @@ -138,6 +148,9 @@ private void RenderOutput(List<OutputLine> lines) {
}
}
richTextBox.SetRedraw(true);
if (visible) {
richTextBox.Invalidate();
}
}

private void AddContextLine(int number, string? line) {
Expand Down
19 changes: 16 additions & 3 deletions NET48/FindInFilesForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,12 @@ private async void buttonStart_Click(object sender, EventArgs e) {
process.StartInfo.FileName = exePath;
process.StartInfo.Arguments = argument;
process.OutputDataReceived += Process_OutputDataReceived;
buttonFind.Enabled = false;
process.Start();
process.BeginOutputReadLine();
var error = await process.StandardError.ReadToEndAsync();
await Task.Run(() => process.WaitForExit());
buttonFind.Enabled = true;
lineParser.Flush();
richTextBox.Invalidate();
if (!string.IsNullOrEmpty(error)) {
AppendText($"{error}{Environment.NewLine}", Color.Red);
}
Expand All @@ -111,17 +110,28 @@ private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
private readonly OutputLineParser lineParser;
private static readonly string MarkerPath = "\u200C"; // zero width non-joiner
private static readonly string MarkerLine = "\u200D"; // zero width joiner
private int visibleDelta = 0;

private void ClearMatchResult() {
var page = (int)(richTextBox.Height / richTextBox.GetLineHeight());
page += page / 4;
page = Math.Max(page, 5);
visibleDelta = page * 2;
richTextBox.Clear();
lineParser.MaxCachedLine = page;
lineParser.Clear();
}

private void RenderOutput(List<OutputLine> lines) {
var visible = richTextBox.Focused;
if (!visible) {
var last = richTextBox.GetLineFromCharIndex(-1);
if (last < visibleDelta) {
visible = true;
} else {
var top = richTextBox.GetLineFromCharIndex(richTextBox.GetCharIndexFromPosition(new Point(1, 1)));
visible = last - top < visibleDelta;
}
}
richTextBox.SetRedraw(false);
foreach (var line in lines) {
switch (line.LineType) {
Expand All @@ -143,6 +153,9 @@ private void RenderOutput(List<OutputLine> lines) {
}
}
richTextBox.SetRedraw(true);
if (visible) {
richTextBox.Invalidate();
}
}

private void AddContextLine(string number, string line) {
Expand Down
2 changes: 1 addition & 1 deletion NET48/OutputLineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void Clear() {

public void Flush() {
if (cachedLines.Count != 0) {
owner.Invoke(callback, cachedLines);
callback(cachedLines);
cachedLines.Clear();
}
}
Expand Down
5 changes: 1 addition & 4 deletions NET48/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,12 @@ public static float GetLineHeight(this TextBoxBase textBox) {
var lineSpacing = family.GetLineSpacing(style);
var ascent = family.GetCellAscent(style);
var descent = family.GetCellDescent(style);
var height = font.Size * (lineSpacing + ascent + descent) / emHeight;
var height = font.Size * (lineSpacing + (ascent + descent) / 2) / emHeight;
return height;
}

public static void SetRedraw(this Control control, bool redraw) {
SendMessage(control.Handle, WM_SETREDRAW, new IntPtr(redraw ? 1 : 0), IntPtr.Zero);
if (redraw) {
control.Invalidate();
}
}

private const int WM_SETREDRAW = 0x000B;
Expand Down
2 changes: 1 addition & 1 deletion OutputLineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void Clear() {

public void Flush() {
if (cachedLines.Count != 0) {
owner.Invoke(callback, cachedLines);
callback(cachedLines);
cachedLines.Clear();
}
}
Expand Down
5 changes: 1 addition & 4 deletions Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,12 @@ public static float GetLineHeight(this TextBoxBase textBox) {
var lineSpacing = family.GetLineSpacing(style);
var ascent = family.GetCellAscent(style);
var descent = family.GetCellDescent(style);
var height = font.Size * (lineSpacing + ascent + descent) / emHeight;
var height = font.Size * (lineSpacing + (ascent + descent) / 2) / emHeight;
return height;
}

public static void SetRedraw(this Control control, bool redraw) {
SendMessage(control.Handle, WM_SETREDRAW, new IntPtr(redraw ? 1 : 0), IntPtr.Zero);
if (redraw) {
control.Invalidate();
}
}

private const int WM_SETREDRAW = 0x000B;
Expand Down

0 comments on commit fff1a50

Please sign in to comment.