Skip to content

Commit

Permalink
add font resizing with ctrl+mousewheel
Browse files Browse the repository at this point in the history
  • Loading branch information
doubleyewdee committed Dec 6, 2018
1 parent d946ae0 commit 3f8d3a5
Showing 1 changed file with 66 additions and 34 deletions.
100 changes: 66 additions & 34 deletions src/condo/Screen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand All @@ -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();
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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; }
Expand Down

0 comments on commit 3f8d3a5

Please sign in to comment.