From 19181dc5ab683cebfead74cdd03f004702731a9f Mon Sep 17 00:00:00 2001 From: BDisp Date: Sat, 2 Sep 2023 00:23:56 +0100 Subject: [PATCH] Fix the resize and only use escape sequences if it's running on Windows Terminal. --- Terminal.Gui/ConsoleDrivers/WindowsDriver.cs | 40 +++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs b/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs index 4ae08b3266..458224ef98 100644 --- a/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs +++ b/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs @@ -803,8 +803,46 @@ public WindowsDriver () Clipboard = new FakeDriver.FakeClipboard (); } - _isWindowsTerminal = Environment.GetEnvironmentVariable ("WT_SESSION") != null; + _isWindowsTerminal = GetParentProcessName () == "WindowsTerminal"; + } + + private static string GetParentProcessName () + { +#pragma warning disable CA1416 // Validate platform compatibility + var myId = Process.GetCurrentProcess ().Id; + var query = string.Format ($"SELECT ParentProcessId FROM Win32_Process WHERE ProcessId = {myId}"); + var search = new ManagementObjectSearcher ("root\\CIMV2", query); + var queryObj = search.Get ().OfType ().FirstOrDefault (); + if (queryObj == null) { + return null; + } + var parentId = (uint)queryObj ["ParentProcessId"]; + var parent = Process.GetProcessById ((int)parentId); + var prevParent = parent; + + // Check if the parent is from other parent + while (queryObj != null) { + query = string.Format ($"SELECT ParentProcessId FROM Win32_Process WHERE ProcessId = {parentId}"); + search = new ManagementObjectSearcher ("root\\CIMV2", query); + queryObj = search.Get ().OfType ().FirstOrDefault (); + if (queryObj == null) { + return parent.ProcessName; + } + parentId = (uint)queryObj ["ParentProcessId"]; + try { + parent = Process.GetProcessById ((int)parentId); + if (string.Equals (parent.ProcessName, "explorer", StringComparison.InvariantCultureIgnoreCase)) { + return prevParent.ProcessName; + } + prevParent = parent; + } catch (ArgumentException) { + + return prevParent.ProcessName; + } + } + return parent.ProcessName; +#pragma warning restore CA1416 // Validate platform compatibility } public override void PrepareToRun (MainLoop mainLoop, Action keyHandler, Action keyDownHandler, Action keyUpHandler, Action mouseHandler)