diff --git a/src/cascadia/WpfTerminalControl/TerminalContainer.cs b/src/cascadia/WpfTerminalControl/TerminalContainer.cs index 25b6746dab4..be212ef7995 100644 --- a/src/cascadia/WpfTerminalControl/TerminalContainer.cs +++ b/src/cascadia/WpfTerminalControl/TerminalContainer.cs @@ -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); } @@ -180,15 +184,17 @@ internal string GetSelectedText() /// /// Number of rows to show. /// Number of columns to show. - internal void Resize(uint rows, uint columns) + /// pair with the new width and height size in pixels for the renderer. + 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. @@ -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); } /// @@ -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; @@ -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: @@ -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); } } } diff --git a/src/cascadia/WpfTerminalControl/TerminalControl.xaml b/src/cascadia/WpfTerminalControl/TerminalControl.xaml index b820e73df71..90a1c89205a 100644 --- a/src/cascadia/WpfTerminalControl/TerminalControl.xaml +++ b/src/cascadia/WpfTerminalControl/TerminalControl.xaml @@ -6,8 +6,9 @@ xmlns:local="clr-namespace:Microsoft.Terminal.Wpf" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800" - Focusable="True"> - + Focusable="True" + x:Name="terminalUserControl"> + diff --git a/src/cascadia/WpfTerminalControl/TerminalControl.xaml.cs b/src/cascadia/WpfTerminalControl/TerminalControl.xaml.cs index 1b31c573308..6700ec2c011 100644 --- a/src/cascadia/WpfTerminalControl/TerminalControl.xaml.cs +++ b/src/cascadia/WpfTerminalControl/TerminalControl.xaml.cs @@ -5,6 +5,7 @@ namespace Microsoft.Terminal.Wpf { + using System; using System.Windows; using System.Windows.Controls; using System.Windows.Input; @@ -17,6 +18,8 @@ public partial class TerminalControl : UserControl { private int accumulatedDelta = 0; + private (int width, int height) terminalRendererSize; + /// /// Initializes a new instance of the class. /// @@ -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)); } /// @@ -106,7 +116,18 @@ public string GetSelectedText() /// Number of columns to display. 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); } /// @@ -119,6 +140,27 @@ public void Resize(uint rows, uint columns) return this.termContainer.TriggerResize(rendersize); } + /// + 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;