From d0a23570bf697e1d747ced85274fdeb2af24543c Mon Sep 17 00:00:00 2001 From: jlewin Date: Tue, 2 Apr 2019 14:07:16 -0700 Subject: [PATCH] Move constructors up, properties down --- .../TerminalWindow/TerminalLog.cs | 13 ++-- .../TerminalWindow/TextScrollWidget.cs | 65 ++++++++++--------- 2 files changed, 42 insertions(+), 36 deletions(-) diff --git a/MatterControlLib/PrinterControls/TerminalWindow/TerminalLog.cs b/MatterControlLib/PrinterControls/TerminalWindow/TerminalLog.cs index 6c451424d..22e4ca44e 100644 --- a/MatterControlLib/PrinterControls/TerminalWindow/TerminalLog.cs +++ b/MatterControlLib/PrinterControls/TerminalWindow/TerminalLog.cs @@ -1,5 +1,5 @@ /* -Copyright (c) 2014, Lars Brubaker +Copyright (c) 2019, Lars Brubaker, John Lewin All rights reserved. Redistribution and use in source and binary forms, with or without @@ -27,11 +27,11 @@ of the authors and should not be interpreted as representing official policies, either expressed or implied, of the FreeBSD Project. */ +using System; +using System.Collections.Generic; using MatterHackers.Agg; using MatterHackers.Localizations; using MatterHackers.MatterControl.PrinterCommunication; -using System; -using System.Collections.Generic; namespace MatterHackers.MatterControl { @@ -39,9 +39,6 @@ namespace MatterHackers.MatterControl { private static readonly bool Is32Bit = IntPtr.Size == 4; - public List<(string line, bool output)> PrinterLines = new List<(string line, bool output)>(); - - public event EventHandler<(string line, bool output)> HasChanged; private int maxLinesToBuffer = int.MaxValue - 1; public TerminalLog(PrinterConnection printerConnection) @@ -59,6 +56,10 @@ namespace MatterHackers.MatterControl } } + public event EventHandler<(string line, bool output)> HasChanged; + + public List<(string line, bool output)> PrinterLines { get; } = new List<(string line, bool output)>(); + private void OnHasChanged((string line, bool output) lineData) { HasChanged?.Invoke(this, lineData); diff --git a/MatterControlLib/PrinterControls/TerminalWindow/TextScrollWidget.cs b/MatterControlLib/PrinterControls/TerminalWindow/TextScrollWidget.cs index 119fa8b66..f5984b182 100644 --- a/MatterControlLib/PrinterControls/TerminalWindow/TextScrollWidget.cs +++ b/MatterControlLib/PrinterControls/TerminalWindow/TextScrollWidget.cs @@ -1,5 +1,5 @@ /* -Copyright (c) 2018, Lars Brubaker, John Lewin +Copyright (c) 2019, Lars Brubaker, John Lewin All rights reserved. Redistribution and use in source and binary forms, with or without @@ -39,18 +39,7 @@ namespace MatterHackers.MatterControl { public class TextScrollWidget : GuiWidget { - object locker = new object(); - - private Func<(string line, bool output), string> _lineFilterFunction; - public Func<(string line, bool output), string> LineFilterFunction - { - get => _lineFilterFunction; - set - { - _lineFilterFunction = value; - RebuildFilteredList(); - } - } + private object locker = new object(); private List<(string line, bool output)> allSourceLines; private List visibleLines; @@ -58,9 +47,20 @@ namespace MatterHackers.MatterControl private TypeFacePrinter typeFacePrinter = null; private PrinterConfig printer = null; - public Color TextColor = new Color(102, 102, 102); private int forceStartLine = -1; + private Func<(string line, bool output), string> _lineFilterFunction; + + public TextScrollWidget(PrinterConfig printer, List<(string line, bool output)> sourceLines) + { + this.printer = printer; + printer.Connection.TerminalLog.HasChanged += RecievedNewLine; + this.typeFacePrinter = new TypeFacePrinter("", new StyledTypeFace(ApplicationController.GetTypeFace(NamedTypeFace.Liberation_Mono), 12)); + this.typeFacePrinter.DrawFromHintedCache = true; + this.allSourceLines = sourceLines; + this.visibleLines = sourceLines.Select(ld => ld.line).ToList(); + } + public double Position0To1 { get @@ -71,7 +71,7 @@ namespace MatterHackers.MatterControl } else { - return ((visibleLines.Count - (double)forceStartLine) / visibleLines.Count); + return (visibleLines.Count - (double)forceStartLine) / visibleLines.Count; } } @@ -87,20 +87,23 @@ namespace MatterHackers.MatterControl { forceStartLine = -1; } + Invalidate(); } } - public int NumVisibleLines => (int)Math.Ceiling(Height / typeFacePrinter.TypeFaceStyle.EmSizeInPixels); + public int NumVisibleLines => (int)Math.Ceiling(Height / typeFacePrinter.TypeFaceStyle.EmSizeInPixels); - public TextScrollWidget(PrinterConfig printer, List<(string line, bool output)> sourceLines) + public Color TextColor { get; set; } = new Color(102, 102, 102); + + public Func<(string line, bool output), string> LineFilterFunction { - this.printer = printer; - printer.Connection.TerminalLog.HasChanged += RecievedNewLine; - this.typeFacePrinter = new TypeFacePrinter("", new StyledTypeFace(ApplicationController.GetTypeFace(NamedTypeFace.Liberation_Mono), 12)); - this.typeFacePrinter.DrawFromHintedCache = true; - this.allSourceLines = sourceLines; - this.visibleLines = sourceLines.Select(ld => ld.line).ToList(); + get => _lineFilterFunction; + set + { + _lineFilterFunction = value; + RebuildFilteredList(); + } } private void ConditionalyAddToVisible((string line, bool output) lineData) @@ -159,7 +162,7 @@ namespace MatterHackers.MatterControl public override void OnDraw(Graphics2D graphics2D) { - RectangleDouble Bounds = LocalBounds; + RectangleDouble bounds = LocalBounds; int numLinesToDraw = NumVisibleLines; @@ -183,6 +186,7 @@ namespace MatterHackers.MatterControl startLineIndex = Math.Min(forceStartLine, startLineIndex); } } + int endLineIndex = visibleLines.Count; for (int lineIndex = startLineIndex; lineIndex < endLineIndex; lineIndex++) { @@ -191,10 +195,11 @@ namespace MatterHackers.MatterControl if (visibleLines[lineIndex] != null) { typeFacePrinter.Text = visibleLines[lineIndex]; - typeFacePrinter.Origin = new Vector2(Bounds.Left + 2, y); + typeFacePrinter.Origin = new Vector2(bounds.Left + 2, y); typeFacePrinter.Render(graphics2D, TextColor); } } + y -= typeFacePrinter.TypeFaceStyle.EmSizeInPixels; if (y < -typeFacePrinter.TypeFaceStyle.EmSizeInPixels) { @@ -210,15 +215,15 @@ namespace MatterHackers.MatterControl public override void OnMouseWheel(MouseEventArgs mouseEvent) { base.OnMouseWheel(mouseEvent); - double scrollDelta = (mouseEvent.WheelDelta / ((visibleLines.Count) * 60.0)); + double scrollDelta = mouseEvent.WheelDelta / (visibleLines.Count * 60.0); - if (scrollDelta < 0)//Rounding seems to favor scrolling up, compensating scroll down to feel as smooth + if (scrollDelta < 0) // Rounding seems to favor scrolling up, compensating scroll down to feel as smooth { scrollDelta *= 2; } - else if (Position0To1 == 0)//IF we scroll up at the bottom get pop out from the "on screen" chunk + else if (Position0To1 == 0) // If we scroll up at the bottom get pop out from the "on screen" chunk { - scrollDelta = (NumVisibleLines / (double)visibleLines.Count); + scrollDelta = NumVisibleLines / (double)visibleLines.Count; } double newPos = Position0To1 + scrollDelta; @@ -247,7 +252,7 @@ namespace MatterHackers.MatterControl && !keyEvent.Shift) { double startingScrollPosition = Position0To1; - double scrollDelta = (NumVisibleLines / (double)visibleLines.Count); + double scrollDelta = NumVisibleLines / (double)visibleLines.Count; double newPos = Position0To1; switch (keyEvent.KeyCode)