Fix Windows console hide path targeting the foreground window

This PR addresses [Ryubing/Issues#345](https://github.com/Ryubing/Issues/issues/345) by fixing the Windows console hide/show path so it only acts on Ryujinx’s own console window instead of whatever window happens to be focused during startup. Previously, when Show Console was disabled, the helper could race with focus changes and end up affecting another app or shell window while leaving the console visible; this change removes that foreground-window dependency and keeps the startup behavior scoped to the Ryujinx console.
This commit is contained in:
Babib3l
2026-06-14 23:18:49 -05:00
committed by KeatonTheBot
parent 1486d2db40
commit 5734ad9090
7 changed files with 51 additions and 26 deletions
+16 -5
View File
@@ -136,11 +136,7 @@ namespace Ryujinx.Common.Logging
_time = Stopwatch.StartNew();
// Logger should log to console by default
AddTarget(new AsyncLogTargetWrapper(
new ConsoleLogTarget("console"),
1000,
AsyncLogTargetOverflowAction.Discard));
SetConsoleTargetEnabled(true);
Notice = new Log(LogLevel.Notice);
@@ -173,6 +169,21 @@ namespace Ryujinx.Common.Logging
Updated += target.Log;
}
public static void SetConsoleTargetEnabled(bool enabled)
{
if (enabled)
{
AddTarget(new AsyncLogTargetWrapper(
new ConsoleLogTarget("console"),
1000,
AsyncLogTargetOverflowAction.Discard));
}
else
{
RemoveTarget("console");
}
}
public static void RemoveTarget(string target)
{
ILogTarget logTarget = GetTarget(target);
+22 -21
View File
@@ -12,20 +12,12 @@ namespace Ryujinx.UI.Common.Helper
private static partial nint GetConsoleWindow();
[SupportedOSPlatform("windows")]
[LibraryImport("user32")]
[LibraryImport("kernel32", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool ShowWindow(nint hWnd, int nCmdShow);
[SupportedOSPlatform("windows")]
[LibraryImport("user32")]
private static partial nint GetForegroundWindow();
[SupportedOSPlatform("windows")]
[LibraryImport("user32")]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool SetForegroundWindow(nint hWnd);
private static partial bool FreeConsole();
public static bool SetConsoleWindowStateSupported => OperatingSystem.IsWindows();
public static bool HasConsoleWindow => OperatingSystem.IsWindows() && GetConsoleWindow() != nint.Zero;
public static void SetConsoleWindowState(bool show)
{
@@ -42,22 +34,31 @@ namespace Ryujinx.UI.Common.Helper
[SupportedOSPlatform("windows")]
private static void SetConsoleWindowStateWindows(bool show)
{
const int SW_HIDE = 0;
const int SW_SHOW = 5;
nint hWnd = GetConsoleWindow();
if (hWnd == nint.Zero)
if (show)
{
Logger.Warning?.Print(LogClass.Application, "Attempted to show/hide console window but console window does not exist");
if (GetConsoleWindow() != nint.Zero)
{
Logger.SetConsoleTargetEnabled(true);
}
return;
}
SetForegroundWindow(hWnd);
Logger.SetConsoleTargetEnabled(false);
DetachConsole();
}
hWnd = GetForegroundWindow();
[SupportedOSPlatform("windows")]
private static void DetachConsole()
{
if (GetConsoleWindow() == nint.Zero)
{
return;
}
ShowWindow(hWnd, show ? SW_SHOW : SW_HIDE);
if (!FreeConsole())
{
Logger.Warning?.Print(LogClass.Application, "Attempted to detach console window but the operation failed");
}
}
}
}
+1
View File
@@ -850,6 +850,7 @@
"SettingsTabGraphicsPreferredGpu": "Preferred GPU",
"SettingsTabGraphicsPreferredGpuTooltip": "Select the graphics card that will be used with the Vulkan graphics backend.\n\nDoes not affect the GPU that OpenGL will use.\n\nSet to the GPU flagged as \"dGPU\" if unsure. If there isn't one, leave untouched.",
"SettingsAppRequiredRestartMessage": "Ryujinx Restart Required",
"SettingsShowConsoleRestartMessage": "The console will be available the next time Ryujinx starts.",
"SettingsGpuBackendRestartMessage": "Graphics Backend or GPU settings have been modified. This will require a restart to be applied",
"SettingsGpuBackendRestartSubMessage": "Do you want to restart now?",
"RyujinxUpdaterMessage": "Do you want to update Ryujinx to the latest version?",
+1
View File
@@ -757,6 +757,7 @@
"SettingsTabGraphicsPreferredGpu": "GPU preferida",
"SettingsTabGraphicsPreferredGpuTooltip": "Selecciona la tarjeta gráfica que se utilizará con los back-end de gráficos Vulkan.\n\nNo afecta la GPU que utilizará OpenGL.\n\nFije a la GPU marcada como \"dGUP\" ante dudas. Si no hay una, no haga modificaciones.",
"SettingsAppRequiredRestartMessage": "Reinicio de Ryujinx requerido.",
"SettingsShowConsoleRestartMessage": "La consola estará disponible la próxima vez que se inicie Ryujinx.",
"SettingsGpuBackendRestartMessage": "La configuración de la GPU o del back-end de los gráficos fue modificada. Es necesario reiniciar para que se aplique.",
"SettingsGpuBackendRestartSubMessage": "¿Quieres reiniciar ahora?",
"RyujinxUpdaterMessage": "¿Quieres actualizar Ryujinx a la última versión?",
+1
View File
@@ -768,6 +768,7 @@
"SettingsTabGraphicsPreferredGpu": "GPU préféré",
"SettingsTabGraphicsPreferredGpuTooltip": "Sélectionnez la carte graphique qui sera utilisée avec l'interface graphique Vulkan.\n\nCela ne change pas le GPU qu'OpenGL utilisera.\n\nChoisissez le GPU noté \"dGPU\" si vous n'êtes pas sûr. S'il n'y en a pas, ne pas modifier.",
"SettingsAppRequiredRestartMessage": "Redémarrage de Ryujinx requis",
"SettingsShowConsoleRestartMessage": "La console sera disponible au prochain démarrage de Ryujinx.",
"SettingsGpuBackendRestartMessage": "Les paramètres de l'interface graphique ou du GPU ont été modifiés. Cela nécessitera un redémarrage pour être appliqué",
"SettingsGpuBackendRestartSubMessage": "\n\nVoulez-vous redémarrer maintenant ?",
"RyujinxUpdaterMessage": "Voulez-vous mettre à jour Ryujinx vers la dernière version ?",
+1
View File
@@ -762,6 +762,7 @@
"SettingsTabGraphicsPreferredGpu": "首选 GPU:",
"SettingsTabGraphicsPreferredGpuTooltip": "选择 Vulkan 图形引擎使用的 GPU。\n\n此选项不会影响 OpenGL 使用的 GPU。\n\n如果不确定,建议选择\"独立显卡(dGPU)\"。如果没有独立显卡,则无需改动此选项。",
"SettingsAppRequiredRestartMessage": "Ryujinx 模拟器需要重启",
"SettingsShowConsoleRestartMessage": "控制台将会在下次启动 Ryujinx 时可用。",
"SettingsGpuBackendRestartMessage": "您修改了图形引擎或 GPU 设置,需要重启模拟器才能生效",
"SettingsGpuBackendRestartSubMessage": "是否要立即重启模拟器?",
"RyujinxUpdaterMessage": "是否更新 Ryujinx 到最新的版本?",
@@ -521,10 +521,19 @@ namespace Ryujinx.Ava.UI.ViewModels
get => ConfigurationState.Instance.UI.ShowConsole;
set
{
bool restartRequired = value && !ConsoleHelper.HasConsoleWindow;
ConfigurationState.Instance.UI.ShowConsole.Value = value;
ConfigurationState.Instance.ToFileFormat().SaveConfig(Program.ConfigurationPath);
if (restartRequired)
{
NotificationHelper.ShowInformation(
LocaleManager.Instance[LocaleKeys.SettingsAppRequiredRestartMessage],
LocaleManager.Instance[LocaleKeys.SettingsShowConsoleRestartMessage]);
}
OnPropertyChanged();
}
}