Skip to content

Commit

Permalink
improve memory usage, make debugging a little easier/friendlier
Browse files Browse the repository at this point in the history
  • Loading branch information
doubleyewdee committed Nov 25, 2018
1 parent 09dfd05 commit 5c2f707
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 16 deletions.
16 changes: 11 additions & 5 deletions src/condo/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,27 @@ public partial class MainWindow : Window
public MainWindow()
{
this.InitializeComponent();

this.Loaded += this.OnLoaded;
#if DEBUG
System.Diagnostics.Debugger.Launch();
#endif
}

private void OnLoaded(object sender, RoutedEventArgs e)
{
this.console = TerminalManager.Instance.GetOrCreate(0, "wsl.exe");
this.keyHandler = new KeyHandler(this.console);

#if DEBUG
// There is currently a ... behavior ... in VS where it hijacks console output from spawned child
// processes with no recourse to turn this off, so we don't want to bother with the console output
// above as we'll never get any (sucks). To work around this use ctrl+f5 to launch, in debug builds
// the debugger will attach above.
if (!System.Diagnostics.Debugger.IsAttached)
#endif
this.screen = new Screen(this.console.Buffer);
this.scrollViewer.Content = this.screen;
this.scrollViewer.CanContentScroll = true;

#if DEBUG
System.Diagnostics.Debugger.Launch();
#endif

this.console.Buffer.PropertyChanged += (_, args) =>
{
Expand Down
45 changes: 34 additions & 11 deletions src/condo/Screen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,22 @@ namespace condo

public sealed class Screen : FrameworkElement, IRenderTarget, IScrollInfo
{
public ConsoleBuffer.Buffer Buffer { get; private set; }
private ConsoleBuffer.Buffer buffer;
public ConsoleBuffer.Buffer Buffer
{
private get { return this.buffer; }
set
{
if (this.buffer != null)
{
this.Buffer.PropertyChanged -= this.OnBufferPropertyChanged;
}

this.buffer = value ?? throw new ArgumentNullException(nameof(value));
this.Resize();
this.Buffer.PropertyChanged += this.OnBufferPropertyChanged;
}
}

private VisualCollection cells;
private DpiScale dpiInfo;
Expand All @@ -36,7 +51,16 @@ public sealed class Screen : FrameworkElement, IRenderTarget, IScrollInfo
/// <summary>
/// Empty ctor for designer purposes at present. Probably don't use.
/// </summary>
public Screen() : this(new ConsoleBuffer.Buffer(80, 25)) { }
public Screen() : this(new ConsoleBuffer.Buffer(80, 25))
{
#if DEBUG
for (var i = 0; i < 100; ++i)
{
var line = System.Text.Encoding.UTF8.GetBytes($"line {i}\r\n");
this.Buffer.Append(line, line.Length);
}
#endif
}

public Screen(ConsoleBuffer.Buffer buffer)
{
Expand All @@ -46,21 +70,15 @@ public Screen(ConsoleBuffer.Buffer buffer)
{
throw new InvalidOperationException("Could not get desired font.");
}

this.Buffer = buffer;
this.horizontalCells = this.Buffer.Width;
this.verticalCells = this.Buffer.Height;
this.characters = new Character[this.Buffer.Width, this.Buffer.Height];

this.cellWidth = this.typeface.AdvanceWidths[0] * this.fontSize;
this.cellHeight = this.typeface.Height * this.fontSize;
this.baselineOrigin = new Point(0, this.typeface.Baseline * this.fontSize);
this.cellRectangle = new Rect(new Size(this.cellWidth, this.cellHeight));

this.Buffer = buffer;
this.redrawWatch.Start();
this.cursorBlinkWatch.Start();

this.Buffer.PropertyChanged += this.OnConsolePropertyChanged;
CompositionTarget.Rendering += this.RenderFrame;
this.MouseEnter += (sender, args) =>
{
Expand All @@ -74,7 +92,7 @@ public Screen(ConsoleBuffer.Buffer buffer)
this.Resize();
}

private void OnConsolePropertyChanged(object sender, PropertyChangedEventArgs args)
private void OnBufferPropertyChanged(object sender, PropertyChangedEventArgs args)
{
if (args.PropertyName == string.Empty)
{
Expand Down Expand Up @@ -126,7 +144,7 @@ private void RenderFrame(object sender, EventArgs e)

public void Close()
{
this.Buffer.PropertyChanged -= this.OnConsolePropertyChanged;
this.Buffer.PropertyChanged -= this.OnBufferPropertyChanged;
}

protected override int VisualChildrenCount => this.cells.Count;
Expand All @@ -144,6 +162,11 @@ protected override Size MeasureOverride(Size availableSize)
private void Resize()
{
this.cells.Clear();

this.horizontalCells = this.Buffer.Width;
this.verticalCells = this.Buffer.Height;
this.characters = new Character[this.Buffer.Width, this.Buffer.Height];

for (var y = 0; y < this.verticalCells; ++y)
{
for (var x = 0; x < this.horizontalCells; ++x)
Expand Down

0 comments on commit 5c2f707

Please sign in to comment.