From 3f8d3a5f8c0720c414745844aa6fe4485d1a2283 Mon Sep 17 00:00:00 2001 From: Chip Locke Date: Wed, 5 Dec 2018 19:04:16 -0800 Subject: [PATCH] add font resizing with ctrl+mousewheel --- src/condo/Screen.cs | 100 +++++++++++++++++++++++++++++--------------- 1 file changed, 66 insertions(+), 34 deletions(-) diff --git a/src/condo/Screen.cs b/src/condo/Screen.cs index ecb8d02..e04136e 100644 --- a/src/condo/Screen.cs +++ b/src/condo/Screen.cs @@ -45,32 +45,26 @@ public XtermPalette Palette set { this.palette = value; - for (var x = 0; x < this.buffer.Width; ++x) - { - for (var y = 0; y < this.buffer.Height; ++y) - { - this.characters[x, y].Changed = true; - } - } + this.ForceFullRedraw(); } } - private VisualCollection cells; - private DpiScale dpiInfo; - private readonly GlyphTypeface typeface; - private readonly int fontSize = 16; - private readonly double cellWidth, cellHeight; - private readonly Point baselineOrigin; - private readonly Rect cellRectangle; - private readonly double underlineY; - private readonly double underlineHeight; - private readonly GuidelineSet cellGuidelines; + private readonly VisualCollection cells; + private readonly DpiScale dpiInfo; + private GlyphTypeface typeface; + private int fontSizeEm = 16; + private double cellWidth, cellHeight; + private Point baselineOrigin; + private Rect cellRectangle; + private double underlineY; + private double underlineHeight; + private GuidelineSet cellGuidelines; private int horizontalCells, verticalCells; private DrawCharacter[,] characters; bool cursorInverted; private volatile int shouldRedraw; private int consoleBufferSize; - private SolidBrushCache brushCache = new SolidBrushCache(); + private readonly SolidBrushCache brushCache = new SolidBrushCache(); private static readonly TimeSpan BlinkFrequency = TimeSpan.FromMilliseconds(250); private readonly Stopwatch cursorBlinkWatch = new Stopwatch(); @@ -104,22 +98,8 @@ public Screen(ConsoleBuffer.Buffer buffer) { this.dpiInfo = VisualTreeHelper.GetDpi(this); this.cells = new VisualCollection(this); - if (!new Typeface("Consolas").TryGetGlyphTypeface(out this.typeface)) - { - throw new InvalidOperationException("Could not get desired font."); - } - 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.underlineY = this.baselineOrigin.Y - this.typeface.UnderlinePosition * this.fontSize; - this.underlineHeight = (this.cellHeight * this.typeface.UnderlineThickness); - this.cellRectangle = new Rect(new Size(this.cellWidth, this.cellHeight)); - this.cellGuidelines = new GuidelineSet( - new[] { this.cellRectangle.Left, this.cellRectangle.Right }, - new[] { this.cellRectangle.Top, this.underlineY, this.underlineY + this.underlineHeight, this.cellRectangle.Bottom }); - this.cellGuidelines.Freeze(); - this.Buffer = buffer; + this.cursorBlinkWatch.Start(); CompositionTarget.Rendering += this.RenderFrame; @@ -131,7 +111,17 @@ public Screen(ConsoleBuffer.Buffer buffer) { args.MouseDevice.OverrideCursor = Cursors.Arrow; }; + this.MouseWheel += (sender, args) => + { + if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) + { + var factor = args.Delta > 0 ? 2 : -2; + this.SetFontSize(this.fontSizeEm + factor); + args.Handled = true; + } + }; + this.SetFontSize(14); this.Resize(); } @@ -224,6 +214,36 @@ private void Resize() this.consoleBufferSize = this.Buffer.BufferSize; } + private void SetFontSize(int newFontSizeEm) + { + newFontSizeEm = Math.Max(8, Math.Min(72, newFontSizeEm)); + + if (this.fontSizeEm == newFontSizeEm) + { + return; + } + + this.fontSizeEm = newFontSizeEm; + + if (!new Typeface("Consolas").TryGetGlyphTypeface(out this.typeface)) + { + throw new InvalidOperationException("Could not get desired font."); + } + this.cellWidth = this.typeface.AdvanceWidths[0] * this.fontSizeEm; + this.cellHeight = this.typeface.Height * this.fontSizeEm; + this.baselineOrigin = new Point(0, this.typeface.Baseline * this.fontSizeEm); + this.underlineY = this.baselineOrigin.Y - this.typeface.UnderlinePosition * this.fontSizeEm; + this.underlineHeight = (this.cellHeight * this.typeface.UnderlineThickness); + this.cellRectangle = new Rect(new Size(this.cellWidth, this.cellHeight)); + this.cellGuidelines = new GuidelineSet( + new[] { this.cellRectangle.Left, this.cellRectangle.Right }, + new[] { this.cellRectangle.Top, this.underlineY, this.underlineY + this.underlineHeight, this.cellRectangle.Bottom }); + this.cellGuidelines.Freeze(); + + this.ForceFullRedraw(); + this.Resize(); + } + private DrawingVisual GetCell(int x, int y) { return this.cells[x + y * this.horizontalCells] as DrawingVisual; @@ -318,7 +338,7 @@ private void SetCellCharacter(int x, int y, bool invert = false) { glyphValue = 0; } - gr = new GlyphRun(this.typeface, 0, false, this.fontSize, (float)this.dpiInfo.PixelsPerDip, new[] { glyphValue }, + gr = new GlyphRun(this.typeface, 0, false, this.fontSizeEm, (float)this.dpiInfo.PixelsPerDip, new[] { glyphValue }, this.baselineOrigin, new[] { 0.0 }, new[] { new Point(0, 0) }, null, null, null, null, null); dc.DrawGlyphRun(!invert ? foregroundBrush : backgroundBrush, gr); @@ -346,6 +366,18 @@ private void Redraw() } } + private void ForceFullRedraw() + { + for (var x = 0; x < this.buffer.Width; ++x) + { + for (var y = 0; y < this.buffer.Height; ++y) + { + this.characters[x, y].Changed = true; + } + } + this.shouldRedraw = 1; + } + #region IScrollInfo public bool CanVerticallyScroll { get; set; } public bool CanHorizontallyScroll { get; set; }