浏览代码

Conhost now works; added toggle to uicatalog

Tigger Kindel 2 年之前
父节点
当前提交
732723976f
共有 3 个文件被更改,包括 65 次插入15 次删除
  1. 41 12
      Terminal.Gui/ConsoleDrivers/WindowsDriver.cs
  2. 2 2
      UICatalog/KeyBindingsDialog.cs
  3. 22 1
      UICatalog/UICatalog.cs

+ 41 - 12
Terminal.Gui/ConsoleDrivers/WindowsDriver.cs

@@ -11,6 +11,9 @@ using System.Threading;
 using System.Threading.Tasks;
 using System.Diagnostics;
 using System.Management;
+using static Terminal.Gui.WindowsConsole;
+using static Unix.Terminal.Curses;
+using System.Drawing;
 
 namespace Terminal.Gui;
 
@@ -47,6 +50,7 @@ internal class WindowsConsole {
 			ReadFromConsoleOutput (size, coords, ref window);
 		}
 
+		bool result = false;
 		if (force16Colors) {
 			int i = 0;
 			CharInfo [] ci = new CharInfo [charInfoBuffer.Length];
@@ -56,7 +60,8 @@ internal class WindowsConsole {
 					Attributes = (ushort)info.Attribute.Value
 				};
 			}
-			return WriteConsoleOutput (_screenBuffer, ci, coords, new Coord () { X = window.Left, Y = window.Top }, ref window);
+
+			result = WriteConsoleOutput (_screenBuffer, ci, coords, new Coord () { X = window.Left, Y = window.Top }, ref window);
 		} else {
 
 			_stringBuilder.Clear ();
@@ -88,8 +93,17 @@ internal class WindowsConsole {
 
 			string s = _stringBuilder.ToString ();
 
-			return WriteConsole (_screenBuffer, s, (uint)(s.Length), out uint _, null);
+			result = WriteConsole (_screenBuffer, s, (uint)(s.Length), out uint _, null);
+		}
+
+		if (!result) {
+			var err = Marshal.GetLastWin32Error ();
+			if (err != 0) {
+				throw new System.ComponentModel.Win32Exception (err);
+			}
 		}
+
+		return result;
 	}
 
 	public void ReadFromConsoleOutput (Size size, Coord coords, ref SmallRect window)
@@ -771,17 +785,18 @@ internal class WindowsDriver : ConsoleDriver {
 			base.Force16Colors = value;
 			// BUGBUG: This is a hack until we fully support VirtualTerminalSequences
 			WinConsole = new WindowsConsole ();
+			Refresh ();
 		}
 	}
 
-	bool _isWindowsTerminal = false;
+	readonly bool _isWindowsTerminal = false;
 
 	public WindowsDriver ()
 	{
 		WinConsole = new WindowsConsole ();
 		Clipboard = new WindowsClipboard ();
 
-		_isWindowsTerminal = false;//Environment.GetEnvironmentVariable ("WT_SESSION") != null;
+		_isWindowsTerminal = Environment.GetEnvironmentVariable ("WT_SESSION") != null;
 
 	}
 
@@ -1422,7 +1437,14 @@ internal class WindowsDriver : ConsoleDriver {
 	{
 		TerminalResized = terminalResized;
 
+		
 		try {
+
+			var winSize = WinConsole.GetConsoleOutputWindow (out Point pos);
+			Cols = winSize.Width;
+			Rows = winSize.Height;
+			WindowsConsole.SmallRect.MakeEmpty (ref _damageRegion);
+
 			// Needed for Windows Terminal
 			// ESC [ ? 1047 h  Save cursor position and activate xterm alternative buffer (no backscroll)
 			// ESC [ ? 1047 l  Restore cursor position and restore xterm working buffer (with backscroll)
@@ -1435,14 +1457,9 @@ internal class WindowsDriver : ConsoleDriver {
 			if (_isWindowsTerminal) {
 				Console.Out.Write (EscSeqUtils.CSI_SaveCursorAndActivateAltBufferNoBackscroll);
 			}
-
-			var winSize = WinConsole.GetConsoleOutputWindow (out Point pos);
-			Cols = winSize.Width;
-			Rows = winSize.Height;
-			WindowsConsole.SmallRect.MakeEmpty (ref _damageRegion);
-
-		} catch (Win32Exception) {
+		} catch (Win32Exception e) {
 			// Likely running unit tests. Set WinConsole to null so we can test it elsewhere.
+			Debug.WriteLine ($"Likely running unit tests. Setting WinConsole to null so we can test it elsewhere. Exception: {e}");
 			WinConsole = null;
 		}
 
@@ -1524,7 +1541,19 @@ internal class WindowsDriver : ConsoleDriver {
 			}
 		}
 
-		WinConsole.WriteToConsole (new Size (Cols, Rows), _outputBuffer, bufferCoords, _damageRegion, Force16Colors);
+		_damageRegion = new WindowsConsole.SmallRect () {
+			Top = 0,
+			Left = 0,
+			Bottom = (short)Rows,
+			Right = (short)Cols
+		};
+
+		if (!WinConsole.WriteToConsole (new Size (Cols, Rows), _outputBuffer, bufferCoords, _damageRegion, Force16Colors)) {
+			var err = Marshal.GetLastWin32Error ();
+			if (err != 0) {
+				throw new System.ComponentModel.Win32Exception (err);
+			}
+		}
 		WindowsConsole.SmallRect.MakeEmpty (ref _damageRegion);
 	}
 

+ 2 - 2
UICatalog/KeyBindingsDialog.cs

@@ -123,8 +123,8 @@ namespace UICatalog {
 		public KeyBindingsDialog () : base()
 		{
 			Title = "Keybindings";
-			Height = 50;
-			Width = 10;
+			//Height = 50;
+			//Width = 10;
 			if (ViewTracker.Instance == null) {
 				ViewTracker.Initialize ();
 			}

+ 22 - 1
UICatalog/UICatalog.cs

@@ -247,6 +247,7 @@ namespace UICatalog {
 		public class UICatalogTopLevel : Toplevel {
 			public MenuItem? miUseSubMenusSingleFrame;
 			public MenuItem? miIsMenuBorderDisabled;
+			public MenuItem? miForce16Colors;
 			public MenuItem? miIsMouseDisabled;
 
 			public ListView CategoryList;
@@ -419,7 +420,7 @@ namespace UICatalog {
 				ConfigChanged ();
 
 				miIsMouseDisabled!.Checked = Application.IsMouseDisabled;
-				DriverName.Title = $"Driver: {Driver.GetVersionInfo()}";
+				DriverName.Title = $"Driver: {Driver.GetVersionInfo ()}";
 				OS.Title = $"OS: {Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment.OperatingSystem} {Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment.OperatingSystemVersion}";
 
 				if (_selectedScenario != null) {
@@ -482,6 +483,8 @@ namespace UICatalog {
 				List<MenuItem []> menuItems = new List<MenuItem []> {
 					CreateDiagnosticFlagsMenuItems (),
 					new MenuItem [] { null! },
+					CreateForce16ColorItems (),
+					new MenuItem [] { null! },
 					CreateDisabledEnabledMouseItems (),
 					CreateDisabledEnabledMenuBorder (),
 					CreateDisabledEnableUseSubMenusSingleFrame (),
@@ -526,6 +529,24 @@ namespace UICatalog {
 				return menuItems.ToArray ();
 			}
 
+
+			MenuItem [] CreateForce16ColorItems ()
+			{
+				List<MenuItem> menuItems = new List<MenuItem> ();
+				miForce16Colors = new MenuItem {
+					Title = "Force 16 _Colors"
+				};
+				miForce16Colors.Shortcut = Key.CtrlMask | Key.AltMask | (Key)miForce16Colors!.Title!.Substring (10, 1) [0];
+				miForce16Colors.CheckType |= MenuItemCheckStyle.Checked;
+				miForce16Colors.Action += () => {
+					miForce16Colors.Checked = Application.Driver.Force16Colors = (bool)!miForce16Colors.Checked!;
+
+				};
+				menuItems.Add (miForce16Colors);
+
+				return menuItems.ToArray ();
+			}
+
 			MenuItem [] CreateDisabledEnabledMouseItems ()
 			{
 				List<MenuItem> menuItems = new List<MenuItem> ();