Add class for detecting information about console in extensible way

This commit is contained in:
tznind
2025-03-30 12:16:46 +01:00
parent 39d4c7dd3d
commit 7e4253cf28
4 changed files with 96 additions and 2 deletions

View File

@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Win32;
namespace Terminal.Gui;
/// <summary>
/// Attempts to determine information about the terminal and what features it
/// does/not support based on runtime operations e.g. registry etc
/// </summary>
internal class ConsoleFeatureFinder
{
public ConsoleFeatureFinderResults GetResults ()
{
var results = new ConsoleFeatureFinderResults ();
PlatformID p = Environment.OSVersion.Platform;
results.IsWindows = p is PlatformID.Win32NT or PlatformID.Win32S or PlatformID.Win32Windows;
if (results.IsWindows)
{
DetectWindowsSpecificFeatures (results.Windows);
}
return results;
}
private void DetectWindowsSpecificFeatures (WindowsFeatureSet windowsFeatures)
{
windowsFeatures.ConHostLegacyMode = IsLegacyConsoleEnabled ();
}
bool IsLegacyConsoleEnabled ()
{
try
{
using (RegistryKey key = Registry.CurrentUser.OpenSubKey (@"Console"))
{
if (key != null)
{
object value = key.GetValue ("ForceV2");
if (value is int intValue)
{
return intValue == 0; // Legacy Mode enabled if ForceV2 is 0
}
}
}
}
catch (Exception ex)
{
Logging.Warning ("Error reading registry: " + ex.Message);
}
return false; // Assume new console mode if check fails
}
}

View File

@@ -0,0 +1,16 @@
namespace Terminal.Gui;
/// <summary>
/// Results of console feature detection
/// </summary>
internal class ConsoleFeatureFinderResults
{
public WindowsFeatureSet Windows { get; set; } = new WindowsFeatureSet();
public bool IsWindows { get; set; }
/// <inheritdoc />
public override string ToString ()
{
return $"{nameof(IsWindows)}:{IsWindows} {nameof(Windows)}:{Windows}";
}
}

View File

@@ -0,0 +1,15 @@
using static Unix.Terminal.Curses;
namespace Terminal.Gui;
/// <summary>
/// Features specific to the windows operating system
/// </summary>
internal class WindowsFeatureSet
{
public bool ConHostLegacyMode { get; set; }
public override string ToString () { return $"{nameof(ConHostLegacyMode)}:{ConHostLegacyMode}"; }
}

View File

@@ -85,11 +85,15 @@ public class ApplicationV2 : ApplicationImpl
private void CreateDriver (string? driverName)
{
PlatformID p = Environment.OSVersion.Platform;
bool definetlyWin = driverName?.Contains ("win") ?? false;
bool definetlyNet = driverName?.Contains ("net") ?? false;
var finder = new ConsoleFeatureFinder ();
var result = finder.GetResults ();
Logging.Logger.LogInformation ($"Feature detection results:{ result}");
if (definetlyWin)
{
_coordinator = CreateWindowsSubcomponents ();
@@ -98,7 +102,7 @@ public class ApplicationV2 : ApplicationImpl
{
_coordinator = CreateNetSubcomponents ();
}
else if (p == PlatformID.Win32NT || p == PlatformID.Win32S || p == PlatformID.Win32Windows)
else if (result.IsWindows)
{
_coordinator = CreateWindowsSubcomponents ();
}