Skip to content

Commit

Permalink
Autofill now fills in blank space and calculates new margins whe resized
Browse files Browse the repository at this point in the history
  • Loading branch information
javierdlg committed Oct 7, 2020
1 parent dee6aa4 commit 40d6124
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 11 deletions.
24 changes: 16 additions & 8 deletions src/cascadia/WpfTerminalControl/TerminalContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,16 @@ internal string GetSelectedText()
var dpiScale = VisualTreeHelper.GetDpi(this);

NativeMethods.COORD dimensions;
NativeMethods.TerminalTriggerResize(this.terminal, renderSize.Width * dpiScale.DpiScaleX, renderSize.Height * dpiScale.DpiScaleY, out dimensions);
NativeMethods.TerminalTriggerResize(
this.terminal,
Convert.ToInt16(renderSize.Width * dpiScale.DpiScaleX),
Convert.ToInt16(renderSize.Height * dpiScale.DpiScaleY),
out dimensions);

this.Rows = dimensions.Y;
this.Columns = dimensions.X;

this.connection?.Resize((uint)dimensions.Y, (uint)dimensions.X);
this.Connection?.Resize((uint)dimensions.Y, (uint)dimensions.X);
return (dimensions.Y, dimensions.X);
}

Expand All @@ -180,15 +184,17 @@ internal string GetSelectedText()
/// </summary>
/// <param name="rows">Number of rows to show.</param>
/// <param name="columns">Number of columns to show.</param>
internal void Resize(uint rows, uint columns)
/// <returns><see cref="long"/> pair with the new width and height size in pixels for the renderer.</returns>
internal (int width, int height) Resize(uint rows, uint columns)
{
NativeMethods.SIZE dimensionsInPixels;
NativeMethods.COORD dimensions = new NativeMethods.COORD
{
X = (short)columns,
Y = (short)rows,
};

NativeMethods.TerminalResize(this.terminal, dimensions);
NativeMethods.TerminalTriggerResizeWithDimension(this.terminal, dimensions, out dimensionsInPixels);

// If AutoFill is true, keep Rows and Columns in sync with MaxRows and MaxColumns.
// Otherwise, MaxRows and MaxColumns will be set on startup and on control resize by the user.
Expand All @@ -201,7 +207,9 @@ internal void Resize(uint rows, uint columns)
this.Columns = dimensions.X;
this.Rows = dimensions.Y;

this.connection?.Resize((uint)dimensions.Y, (uint)dimensions.X);
this.Connection?.Resize((uint)dimensions.Y, (uint)dimensions.X);

return (dimensionsInPixels.cx, dimensionsInPixels.cy);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -330,7 +338,7 @@ private IntPtr TerminalContainer_MessageHook(IntPtr hwnd, int msg, IntPtr wParam
// We only trigger a resize if we want to autofill to maximum size.
if (this.AutoFill)
{
NativeMethods.TerminalTriggerResize(this.terminal, windowpos.cx, windowpos.cy, out dimensions);
NativeMethods.TerminalTriggerResize(this.terminal, (short)windowpos.cx, (short)windowpos.cy, out dimensions);

this.Columns = dimensions.X;
this.Rows = dimensions.Y;
Expand All @@ -344,7 +352,7 @@ private IntPtr TerminalContainer_MessageHook(IntPtr hwnd, int msg, IntPtr wParam
this.MaxRows = dimensions.Y;
}

this.connection?.Resize((uint)dimensions.Y, (uint)dimensions.X);
this.Connection?.Resize((uint)dimensions.Y, (uint)dimensions.X);
break;

case NativeMethods.WindowMessage.WM_MOUSEWHEEL:
Expand Down Expand Up @@ -401,7 +409,7 @@ private void OnScroll(int viewTop, int viewHeight, int bufferSize)

private void OnWrite(string data)
{
this.connection?.WriteInput(data);
this.Connection?.WriteInput(data);
}
}
}
5 changes: 3 additions & 2 deletions src/cascadia/WpfTerminalControl/TerminalControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
xmlns:local="clr-namespace:Microsoft.Terminal.Wpf"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Focusable="True">
<Grid>
Focusable="True"
x:Name="terminalUserControl">
<Grid x:Name="terminalGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto"/>
Expand Down
44 changes: 43 additions & 1 deletion src/cascadia/WpfTerminalControl/TerminalControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace Microsoft.Terminal.Wpf
{
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
Expand All @@ -17,6 +18,8 @@ public partial class TerminalControl : UserControl
{
private int accumulatedDelta = 0;

private (int width, int height) terminalRendererSize;

/// <summary>
/// Initializes a new instance of the <see cref="TerminalControl"/> class.
/// </summary>
Expand Down Expand Up @@ -88,6 +91,13 @@ public void SetTheme(TerminalTheme theme, string fontFamily, short fontSize)
}

this.termContainer.SetTheme(theme, fontFamily, fontSize);

// DefaultBackground uses Win32 COLORREF syntax which is BGR instead of RGB.
byte b = Convert.ToByte((theme.DefaultBackground >> 16) & 0xff);
byte g = Convert.ToByte((theme.DefaultBackground >> 8) & 0xff);
byte r = Convert.ToByte(theme.DefaultBackground & 0xff);

this.terminalGrid.Background = new SolidColorBrush(Color.FromRgb(r, g, b));
}

/// <summary>
Expand All @@ -106,7 +116,18 @@ public string GetSelectedText()
/// <param name="columns">Number of columns to display.</param>
public void Resize(uint rows, uint columns)
{
this.termContainer.Resize(rows, columns);
var dpiScale = VisualTreeHelper.GetDpi(this);

this.terminalRendererSize = this.termContainer.Resize(rows, columns);

double width = ((this.terminalUserControl.RenderSize.Width * dpiScale.DpiScaleX) - this.terminalRendererSize.width) / dpiScale.DpiScaleX;
double height = ((this.terminalUserControl.RenderSize.Height * dpiScale.DpiScaleY) - this.terminalRendererSize.height) / dpiScale.DpiScaleY;

// Prevent negative margin size.
width = width < 0 ? 0 : width;
height = height < 0 ? 0 : height;

this.terminalGrid.Margin = new Thickness(0, 0, width, height);
}

/// <summary>
Expand All @@ -119,6 +140,27 @@ public void Resize(uint rows, uint columns)
return this.termContainer.TriggerResize(rendersize);
}

/// <inheritdoc/>
protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
{
// Renderer will not resize on control resize. We have to manually recalculate the margin to fill in the space.
if (this.AutoFill == false && this.terminalRendererSize.width != 0 && this.terminalRendererSize.height != 0)
{
var dpiScale = VisualTreeHelper.GetDpi(this);

double width = ((sizeInfo.NewSize.Width * dpiScale.DpiScaleX) - this.terminalRendererSize.width) / dpiScale.DpiScaleX;
double height = ((sizeInfo.NewSize.Height * dpiScale.DpiScaleY) - this.terminalRendererSize.height) / dpiScale.DpiScaleY;

// Prevent negative margin size.
width = width < 0 ? 0 : width;
height = height < 0 ? 0 : height;

this.terminalGrid.Margin = new Thickness(0, 0, width, height);
}

base.OnRenderSizeChanged(sizeInfo);
}

private void TerminalControl_GotFocus(object sender, RoutedEventArgs e)
{
e.Handled = true;
Expand Down

1 comment on commit 40d6124

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New misspellings found, please review:

  • autofill
  • autofit
To accept these changes, run the following commands
perl -e '
my @expect_files=qw('".github/actions/spell-check/expect/alphabet.txt
.github/actions/spell-check/expect/expect.txt
.github/actions/spell-check/expect/web.txt"');
@ARGV=@expect_files;
my @stale=qw('"akb appdata Autogenerated debian debugbreak decf DECLL DECSMBV esa guidgenerator Inplace keith keybound notypeopt openlogo restrictederrorinfo richturn Scs Switchto winver Wlk wslhome "');
my $re=join "|", @stale;
my $suffix=".".time();
my $previous="";
sub maybe_unlink { unlink($_[0]) if $_[0]; }
while (<>) {
  if ($ARGV ne $old_argv) { maybe_unlink($previous); $previous="$ARGV$suffix"; rename($ARGV, $previous); open(ARGV_OUT, ">$ARGV"); select(ARGV_OUT); $old_argv = $ARGV; }
  next if /^($re)(?:$| .*)/; print;
}; maybe_unlink($previous);'
perl -e '
my $new_expect_file=".github/actions/spell-check/expect/40d61245f4f53434681fe77b80f94c3084eae7e9.txt";
open FILE, q{<}, $new_expect_file; chomp(my @words = <FILE>); close FILE;
my @add=qw('"autofill autofit autogenerated Debian inplace WINVER "');
my %items; @items{@words} = @words x (1); @items{@add} = @add x (1);
@words = sort {lc($a) cmp lc($b)} keys %items;
open FILE, q{>}, $new_expect_file; for my $word (@words) { print FILE "$word\n" if $word =~ /\w/; };
close FILE;'
git add .github/actions/spell-check/expect || echo '... you want to ensure .github/actions/spell-check/expect/40d61245f4f53434681fe77b80f94c3084eae7e9.txt is added to your repository...'
✏️ Contributor please read this

By default the command suggestion will generate a file named based on your commit. That's generally ok as long as you add the file to your commit. Someone can reorganize it later.

⚠️ The command is written for posix shells. You can copy the contents of each perl command excluding the outer ' marks and dropping any '"/"' quotation mark pairs into a file and then run perl file.pl from the root of the repository to run the code. Alternatively, you can manually insert the items...

If the listed items are:

  • ... misspelled, then please correct them instead of using the command.
  • ... names, please add them to .github/actions/spell-check/dictionary/names.txt.
  • ... APIs, you can add them to a file in .github/actions/spell-check/dictionary/.
  • ... just things you're using, please add them to an appropriate file in .github/actions/spell-check/expect/.
  • ... tokens you only need in one place and shouldn't generally be used, you can add an item in an appropriate file in .github/actions/spell-check/patterns/.

See the README.md in each directory for more information.

🔬 You can test your commits without appending to a PR by creating a new branch with that extra change and pushing it to your fork. The :check-spelling action will run in response to your push -- it doesn't require an open pull request. By using such a branch, you can limit the number of typos your peers see you make. 😉

⚠️ Reviewers

At present, the action that triggered this message will not show its ❌ in this PR unless the branch is within this repository.
Thus, you should make sure that this comment has been addressed before encouraging the merge bot to merge this PR.

Please sign in to comment.