From 67bbbd271c14744e0188919a5a3faf0b5d687132 Mon Sep 17 00:00:00 2001
From: CJK_mkp <113243675+CJKmkp@users.noreply.github.com>
Date: Mon, 30 Nov 2026 11:50:23 +0800
Subject: [PATCH 1/4] fix(security): add owner-bound WPF security prompt dialog
---
Ink Canvas/Helpers/SecurityPromptDialog.cs | 227 +++++++++++++++++++++
1 file changed, 227 insertions(+)
create mode 100644 Ink Canvas/Helpers/SecurityPromptDialog.cs
diff --git a/Ink Canvas/Helpers/SecurityPromptDialog.cs b/Ink Canvas/Helpers/SecurityPromptDialog.cs
new file mode 100644
index 000000000..33ece2e7c
--- /dev/null
+++ b/Ink Canvas/Helpers/SecurityPromptDialog.cs
@@ -0,0 +1,227 @@
+using System;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Input;
+using System.Windows.Media;
+
+namespace Ink_Canvas.Helpers
+{
+ internal enum SecurityPromptDialogResult
+ {
+ None,
+ Primary,
+ Secondary
+ }
+
+ ///
+ /// 安全验证专用模态窗口。使用原生 WPF Window 并显式绑定调用方窗口,
+ /// 避免 iNKORE ContentDialog 在不同窗口宿主并存时丢失激活状态或鼠标输入。
+ ///
+ internal sealed class SecurityPromptDialog
+ {
+ private Window _window;
+ private SecurityPromptDialogResult _result;
+
+ public string Title { get; set; }
+ public string PrimaryButtonText { get; set; }
+ public string SecondaryButtonText { get; set; }
+ public object Content { get; set; }
+ public Window Owner { get; set; }
+
+ public event EventHandler Opened;
+
+ public Task ShowAsync()
+ {
+ _result = SecurityPromptDialogResult.None;
+
+ var owner = ResolveOwner();
+ var panel = new StackPanel
+ {
+ Margin = new Thickness(24),
+ MinWidth = 340,
+ MaxWidth = 548
+ };
+
+ if (!string.IsNullOrWhiteSpace(Title))
+ {
+ panel.Children.Add(new TextBlock
+ {
+ Text = Title,
+ FontSize = 20,
+ FontWeight = FontWeights.SemiBold,
+ TextWrapping = TextWrapping.Wrap,
+ Margin = new Thickness(0, 0, 0, 16)
+ });
+ }
+
+ if (Content is UIElement contentElement)
+ {
+ panel.Children.Add(contentElement);
+ }
+ else if (Content != null)
+ {
+ panel.Children.Add(new TextBlock
+ {
+ Text = Content.ToString(),
+ TextWrapping = TextWrapping.Wrap
+ });
+ }
+
+ var buttons = new StackPanel
+ {
+ Orientation = Orientation.Horizontal,
+ HorizontalAlignment = HorizontalAlignment.Right,
+ Margin = new Thickness(0, 24, 0, 0)
+ };
+
+ if (!string.IsNullOrEmpty(SecondaryButtonText))
+ {
+ var secondaryButton = CreateButton(SecondaryButtonText, "DefaultButtonStyle");
+ secondaryButton.IsCancel = true;
+ secondaryButton.Click += (s, e) => Complete(SecurityPromptDialogResult.Secondary);
+ buttons.Children.Add(secondaryButton);
+ }
+
+ if (!string.IsNullOrEmpty(PrimaryButtonText))
+ {
+ var primaryButton = CreateButton(PrimaryButtonText, "AccentButtonStyle");
+ primaryButton.IsDefault = true;
+ primaryButton.Click += (s, e) => Complete(SecurityPromptDialogResult.Primary);
+ buttons.Children.Add(primaryButton);
+ }
+
+ if (buttons.Children.Count > 0)
+ {
+ panel.Children.Add(buttons);
+ }
+
+ _window = new Window
+ {
+ Title = Title ?? string.Empty,
+ Content = panel,
+ MinWidth = 388,
+ MaxWidth = 596,
+ SizeToContent = SizeToContent.WidthAndHeight,
+ WindowStartupLocation = owner != null
+ ? WindowStartupLocation.CenterOwner
+ : WindowStartupLocation.CenterScreen,
+ ResizeMode = ResizeMode.NoResize,
+ ShowInTaskbar = false,
+ ShowActivated = true,
+ Topmost = owner != null && owner.Topmost,
+ Background = SystemColors.WindowBrush,
+ Foreground = SystemColors.WindowTextBrush,
+ UseLayoutRounding = true
+ };
+
+ if (owner != null)
+ {
+ _window.Owner = owner;
+ }
+
+ _window.ContentRendered += Window_ContentRendered;
+ _window.KeyDown += Window_KeyDown;
+ _window.Closed += (s, e) => _window = null;
+
+ // ShowDialog 提供真正的模态输入和独立 HWND;所有者关系保证窗口层级与输入路由正确。
+ _window.ShowDialog();
+
+ return Task.FromResult(_result);
+ }
+
+ public void Hide()
+ {
+ if (_window == null) return;
+
+ _result = SecurityPromptDialogResult.None;
+ _window.Close();
+ }
+
+ private Window ResolveOwner()
+ {
+ if (Owner != null && Owner.IsVisible)
+ {
+ return Owner;
+ }
+
+ var focusedElement = Keyboard.FocusedElement as DependencyObject;
+ var focusedWindow = focusedElement != null ? Window.GetWindow(focusedElement) : null;
+ if (focusedWindow != null && focusedWindow.IsVisible)
+ {
+ return focusedWindow;
+ }
+
+ var application = Application.Current;
+ if (application != null)
+ {
+ foreach (Window window in application.Windows)
+ {
+ if (window != null && window.IsVisible && window.IsActive)
+ {
+ return window;
+ }
+ }
+
+ var mainWindow = application.MainWindow;
+ if (mainWindow != null && mainWindow.IsVisible)
+ {
+ return mainWindow;
+ }
+ }
+
+ return null;
+ }
+
+ private void Window_ContentRendered(object sender, EventArgs e)
+ {
+ var window = _window;
+ if (window == null) return;
+
+ window.Activate();
+ window.Focus();
+ Opened?.Invoke(this, EventArgs.Empty);
+ }
+
+ private void Window_KeyDown(object sender, KeyEventArgs e)
+ {
+ if (e.Key != Key.Escape) return;
+
+ Complete(SecurityPromptDialogResult.Secondary);
+ e.Handled = true;
+ }
+
+ private void Complete(SecurityPromptDialogResult result)
+ {
+ var window = _window;
+ if (window == null) return;
+
+ _result = result;
+ window.Close();
+ }
+
+ private static Button CreateButton(string text, string styleKey)
+ {
+ var button = new Button
+ {
+ Content = text,
+ MinWidth = 88,
+ Height = 32,
+ Margin = new Thickness(8, 0, 0, 0),
+ Padding = new Thickness(12, 0, 12, 0)
+ };
+
+ try
+ {
+ var style = Application.Current?.FindResource(styleKey) as Style;
+ if (style != null) button.Style = style;
+ }
+ catch
+ {
+ // 主题资源不可用时保留 WPF 默认按钮样式,不影响输入和确认。
+ }
+
+ return button;
+ }
+ }
+}
From fcff71b17118839d5f2b0dc5d9c2ff30d656ca18 Mon Sep 17 00:00:00 2001
From: CJK_mkp <113243675+CJKmkp@users.noreply.github.com>
Date: Mon, 30 Nov 2026 11:50:33 +0800
Subject: [PATCH 2/4] fix(security): bind password prompts to their owner
window
---
Ink Canvas/Helpers/SecurityManager.cs | 20 ++++++++++++--------
1 file changed, 12 insertions(+), 8 deletions(-)
diff --git a/Ink Canvas/Helpers/SecurityManager.cs b/Ink Canvas/Helpers/SecurityManager.cs
index 252187627..4272f18e1 100644
--- a/Ink Canvas/Helpers/SecurityManager.cs
+++ b/Ink Canvas/Helpers/SecurityManager.cs
@@ -138,8 +138,9 @@ public static async Task PromptAndVerifyAsync(Settings settings, Window ow
}
}
- var dialog = new ContentDialog
+ var dialog = new SecurityPromptDialog
{
+ Owner = owner,
Title = title,
PrimaryButtonText = CommonStrings.Common_OK,
SecondaryButtonText = CommonStrings.Common_Cancel
@@ -212,7 +213,7 @@ public static async Task PromptAndVerifyAsync(Settings settings, Window ow
var result = await dialog.ShowAsync();
if (usbVerified) return true;
- if (result != ContentDialogResult.Primary) return false;
+ if (result != SecurityPromptDialogResult.Primary) return false;
return VerifyPassword(settings, passwordBox.Password);
}
@@ -254,8 +255,9 @@ public static async Task PromptAndVerifyPasswordOrTotpAsync(Settings setti
}
}
- var dialog = new ContentDialog
+ var dialog = new SecurityPromptDialog
{
+ Owner = owner,
Title = title,
PrimaryButtonText = CommonStrings.Common_OK,
SecondaryButtonText = CommonStrings.Common_Cancel
@@ -344,7 +346,7 @@ public static async Task PromptAndVerifyPasswordOrTotpAsync(Settings setti
var result = await dialog.ShowAsync();
if (usbVerified) return true;
- if (result != ContentDialogResult.Primary) return false;
+ if (result != SecurityPromptDialogResult.Primary) return false;
string input = inputBox.Password ?? "";
if (!totpOnlyMode && hasPassword && VerifyPassword(settings, input)) return true;
@@ -372,8 +374,9 @@ public static async Task PromptAndVerifyPasswordOrTotpAsync(Settings setti
/// 用户输入的新密码;如果用户取消或输入无效(长度不足或两次不匹配),则返回 null。
public static async Task PromptSetNewPasswordAsync(Window owner)
{
- var dialog = new ContentDialog
+ var dialog = new SecurityPromptDialog
{
+ Owner = owner,
Title = MainWindowStrings.Main_Security_SetPasswordTitle,
PrimaryButtonText = CommonStrings.Common_OK,
SecondaryButtonText = CommonStrings.Common_Cancel
@@ -425,7 +428,7 @@ public static async Task PromptSetNewPasswordAsync(Window owner)
};
var result = await dialog.ShowAsync();
- if (result != ContentDialogResult.Primary) return null;
+ if (result != SecurityPromptDialogResult.Primary) return null;
var pwd = newPwdBox.Password ?? "";
var confirm = confirmPwdBox.Password ?? "";
@@ -466,8 +469,9 @@ public static async Task PromptChangePasswordAsync(Settings settings, Wi
return await PromptSetNewPasswordAsync(owner);
}
- var dialog = new ContentDialog
+ var dialog = new SecurityPromptDialog
{
+ Owner = owner,
Title = MainWindowStrings.Main_Security_ChangePasswordTitle,
PrimaryButtonText = CommonStrings.Common_OK,
SecondaryButtonText = CommonStrings.Common_Cancel
@@ -518,7 +522,7 @@ public static async Task PromptChangePasswordAsync(Settings settings, Wi
};
var result = await dialog.ShowAsync();
- if (result != ContentDialogResult.Primary) return null;
+ if (result != SecurityPromptDialogResult.Primary) return null;
var current = currentBox.Password ?? "";
var newPwd = newPwdBox.Password ?? "";
From fbc4ca758287b8e880b92188c4a66c7bdad1b1fd Mon Sep 17 00:00:00 2001
From: CJK_mkp <113243675+CJKmkp@users.noreply.github.com>
Date: Mon, 30 Nov 2026 11:52:01 +0800
Subject: [PATCH 3/4] style(security): preserve source line endings
---
Ink Canvas/Helpers/SecurityManager.cs | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/Ink Canvas/Helpers/SecurityManager.cs b/Ink Canvas/Helpers/SecurityManager.cs
index 4272f18e1..37d2a45e6 100644
--- a/Ink Canvas/Helpers/SecurityManager.cs
+++ b/Ink Canvas/Helpers/SecurityManager.cs
@@ -140,7 +140,7 @@ public static async Task PromptAndVerifyAsync(Settings settings, Window ow
var dialog = new SecurityPromptDialog
{
- Owner = owner,
+ Owner = owner,
Title = title,
PrimaryButtonText = CommonStrings.Common_OK,
SecondaryButtonText = CommonStrings.Common_Cancel
@@ -257,7 +257,7 @@ public static async Task PromptAndVerifyPasswordOrTotpAsync(Settings setti
var dialog = new SecurityPromptDialog
{
- Owner = owner,
+ Owner = owner,
Title = title,
PrimaryButtonText = CommonStrings.Common_OK,
SecondaryButtonText = CommonStrings.Common_Cancel
@@ -376,7 +376,7 @@ public static async Task PromptSetNewPasswordAsync(Window owner)
{
var dialog = new SecurityPromptDialog
{
- Owner = owner,
+ Owner = owner,
Title = MainWindowStrings.Main_Security_SetPasswordTitle,
PrimaryButtonText = CommonStrings.Common_OK,
SecondaryButtonText = CommonStrings.Common_Cancel
@@ -471,7 +471,7 @@ public static async Task PromptChangePasswordAsync(Settings settings, Wi
var dialog = new SecurityPromptDialog
{
- Owner = owner,
+ Owner = owner,
Title = MainWindowStrings.Main_Security_ChangePasswordTitle,
PrimaryButtonText = CommonStrings.Common_OK,
SecondaryButtonText = CommonStrings.Common_Cancel
From cd81170efd58f1219bc78c060729b77297cc7778 Mon Sep 17 00:00:00 2001
From: CJK_mkp <113243675+CJKmkp@users.noreply.github.com>
Date: Mon, 30 Nov 2026 11:52:09 +0800
Subject: [PATCH 4/4] style(security): match repository line endings
---
Ink Canvas/Helpers/SecurityPromptDialog.cs | 454 ++++++++++-----------
1 file changed, 227 insertions(+), 227 deletions(-)
diff --git a/Ink Canvas/Helpers/SecurityPromptDialog.cs b/Ink Canvas/Helpers/SecurityPromptDialog.cs
index 33ece2e7c..c2aa55ef3 100644
--- a/Ink Canvas/Helpers/SecurityPromptDialog.cs
+++ b/Ink Canvas/Helpers/SecurityPromptDialog.cs
@@ -1,227 +1,227 @@
-using System;
-using System.Threading.Tasks;
-using System.Windows;
-using System.Windows.Controls;
-using System.Windows.Input;
-using System.Windows.Media;
-
-namespace Ink_Canvas.Helpers
-{
- internal enum SecurityPromptDialogResult
- {
- None,
- Primary,
- Secondary
- }
-
- ///
- /// 安全验证专用模态窗口。使用原生 WPF Window 并显式绑定调用方窗口,
- /// 避免 iNKORE ContentDialog 在不同窗口宿主并存时丢失激活状态或鼠标输入。
- ///
- internal sealed class SecurityPromptDialog
- {
- private Window _window;
- private SecurityPromptDialogResult _result;
-
- public string Title { get; set; }
- public string PrimaryButtonText { get; set; }
- public string SecondaryButtonText { get; set; }
- public object Content { get; set; }
- public Window Owner { get; set; }
-
- public event EventHandler Opened;
-
- public Task ShowAsync()
- {
- _result = SecurityPromptDialogResult.None;
-
- var owner = ResolveOwner();
- var panel = new StackPanel
- {
- Margin = new Thickness(24),
- MinWidth = 340,
- MaxWidth = 548
- };
-
- if (!string.IsNullOrWhiteSpace(Title))
- {
- panel.Children.Add(new TextBlock
- {
- Text = Title,
- FontSize = 20,
- FontWeight = FontWeights.SemiBold,
- TextWrapping = TextWrapping.Wrap,
- Margin = new Thickness(0, 0, 0, 16)
- });
- }
-
- if (Content is UIElement contentElement)
- {
- panel.Children.Add(contentElement);
- }
- else if (Content != null)
- {
- panel.Children.Add(new TextBlock
- {
- Text = Content.ToString(),
- TextWrapping = TextWrapping.Wrap
- });
- }
-
- var buttons = new StackPanel
- {
- Orientation = Orientation.Horizontal,
- HorizontalAlignment = HorizontalAlignment.Right,
- Margin = new Thickness(0, 24, 0, 0)
- };
-
- if (!string.IsNullOrEmpty(SecondaryButtonText))
- {
- var secondaryButton = CreateButton(SecondaryButtonText, "DefaultButtonStyle");
- secondaryButton.IsCancel = true;
- secondaryButton.Click += (s, e) => Complete(SecurityPromptDialogResult.Secondary);
- buttons.Children.Add(secondaryButton);
- }
-
- if (!string.IsNullOrEmpty(PrimaryButtonText))
- {
- var primaryButton = CreateButton(PrimaryButtonText, "AccentButtonStyle");
- primaryButton.IsDefault = true;
- primaryButton.Click += (s, e) => Complete(SecurityPromptDialogResult.Primary);
- buttons.Children.Add(primaryButton);
- }
-
- if (buttons.Children.Count > 0)
- {
- panel.Children.Add(buttons);
- }
-
- _window = new Window
- {
- Title = Title ?? string.Empty,
- Content = panel,
- MinWidth = 388,
- MaxWidth = 596,
- SizeToContent = SizeToContent.WidthAndHeight,
- WindowStartupLocation = owner != null
- ? WindowStartupLocation.CenterOwner
- : WindowStartupLocation.CenterScreen,
- ResizeMode = ResizeMode.NoResize,
- ShowInTaskbar = false,
- ShowActivated = true,
- Topmost = owner != null && owner.Topmost,
- Background = SystemColors.WindowBrush,
- Foreground = SystemColors.WindowTextBrush,
- UseLayoutRounding = true
- };
-
- if (owner != null)
- {
- _window.Owner = owner;
- }
-
- _window.ContentRendered += Window_ContentRendered;
- _window.KeyDown += Window_KeyDown;
- _window.Closed += (s, e) => _window = null;
-
- // ShowDialog 提供真正的模态输入和独立 HWND;所有者关系保证窗口层级与输入路由正确。
- _window.ShowDialog();
-
- return Task.FromResult(_result);
- }
-
- public void Hide()
- {
- if (_window == null) return;
-
- _result = SecurityPromptDialogResult.None;
- _window.Close();
- }
-
- private Window ResolveOwner()
- {
- if (Owner != null && Owner.IsVisible)
- {
- return Owner;
- }
-
- var focusedElement = Keyboard.FocusedElement as DependencyObject;
- var focusedWindow = focusedElement != null ? Window.GetWindow(focusedElement) : null;
- if (focusedWindow != null && focusedWindow.IsVisible)
- {
- return focusedWindow;
- }
-
- var application = Application.Current;
- if (application != null)
- {
- foreach (Window window in application.Windows)
- {
- if (window != null && window.IsVisible && window.IsActive)
- {
- return window;
- }
- }
-
- var mainWindow = application.MainWindow;
- if (mainWindow != null && mainWindow.IsVisible)
- {
- return mainWindow;
- }
- }
-
- return null;
- }
-
- private void Window_ContentRendered(object sender, EventArgs e)
- {
- var window = _window;
- if (window == null) return;
-
- window.Activate();
- window.Focus();
- Opened?.Invoke(this, EventArgs.Empty);
- }
-
- private void Window_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.Key != Key.Escape) return;
-
- Complete(SecurityPromptDialogResult.Secondary);
- e.Handled = true;
- }
-
- private void Complete(SecurityPromptDialogResult result)
- {
- var window = _window;
- if (window == null) return;
-
- _result = result;
- window.Close();
- }
-
- private static Button CreateButton(string text, string styleKey)
- {
- var button = new Button
- {
- Content = text,
- MinWidth = 88,
- Height = 32,
- Margin = new Thickness(8, 0, 0, 0),
- Padding = new Thickness(12, 0, 12, 0)
- };
-
- try
- {
- var style = Application.Current?.FindResource(styleKey) as Style;
- if (style != null) button.Style = style;
- }
- catch
- {
- // 主题资源不可用时保留 WPF 默认按钮样式,不影响输入和确认。
- }
-
- return button;
- }
- }
-}
+using System;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Input;
+using System.Windows.Media;
+
+namespace Ink_Canvas.Helpers
+{
+ internal enum SecurityPromptDialogResult
+ {
+ None,
+ Primary,
+ Secondary
+ }
+
+ ///
+ /// 安全验证专用模态窗口。使用原生 WPF Window 并显式绑定调用方窗口,
+ /// 避免 iNKORE ContentDialog 在不同窗口宿主并存时丢失激活状态或鼠标输入。
+ ///
+ internal sealed class SecurityPromptDialog
+ {
+ private Window _window;
+ private SecurityPromptDialogResult _result;
+
+ public string Title { get; set; }
+ public string PrimaryButtonText { get; set; }
+ public string SecondaryButtonText { get; set; }
+ public object Content { get; set; }
+ public Window Owner { get; set; }
+
+ public event EventHandler Opened;
+
+ public Task ShowAsync()
+ {
+ _result = SecurityPromptDialogResult.None;
+
+ var owner = ResolveOwner();
+ var panel = new StackPanel
+ {
+ Margin = new Thickness(24),
+ MinWidth = 340,
+ MaxWidth = 548
+ };
+
+ if (!string.IsNullOrWhiteSpace(Title))
+ {
+ panel.Children.Add(new TextBlock
+ {
+ Text = Title,
+ FontSize = 20,
+ FontWeight = FontWeights.SemiBold,
+ TextWrapping = TextWrapping.Wrap,
+ Margin = new Thickness(0, 0, 0, 16)
+ });
+ }
+
+ if (Content is UIElement contentElement)
+ {
+ panel.Children.Add(contentElement);
+ }
+ else if (Content != null)
+ {
+ panel.Children.Add(new TextBlock
+ {
+ Text = Content.ToString(),
+ TextWrapping = TextWrapping.Wrap
+ });
+ }
+
+ var buttons = new StackPanel
+ {
+ Orientation = Orientation.Horizontal,
+ HorizontalAlignment = HorizontalAlignment.Right,
+ Margin = new Thickness(0, 24, 0, 0)
+ };
+
+ if (!string.IsNullOrEmpty(SecondaryButtonText))
+ {
+ var secondaryButton = CreateButton(SecondaryButtonText, "DefaultButtonStyle");
+ secondaryButton.IsCancel = true;
+ secondaryButton.Click += (s, e) => Complete(SecurityPromptDialogResult.Secondary);
+ buttons.Children.Add(secondaryButton);
+ }
+
+ if (!string.IsNullOrEmpty(PrimaryButtonText))
+ {
+ var primaryButton = CreateButton(PrimaryButtonText, "AccentButtonStyle");
+ primaryButton.IsDefault = true;
+ primaryButton.Click += (s, e) => Complete(SecurityPromptDialogResult.Primary);
+ buttons.Children.Add(primaryButton);
+ }
+
+ if (buttons.Children.Count > 0)
+ {
+ panel.Children.Add(buttons);
+ }
+
+ _window = new Window
+ {
+ Title = Title ?? string.Empty,
+ Content = panel,
+ MinWidth = 388,
+ MaxWidth = 596,
+ SizeToContent = SizeToContent.WidthAndHeight,
+ WindowStartupLocation = owner != null
+ ? WindowStartupLocation.CenterOwner
+ : WindowStartupLocation.CenterScreen,
+ ResizeMode = ResizeMode.NoResize,
+ ShowInTaskbar = false,
+ ShowActivated = true,
+ Topmost = owner != null && owner.Topmost,
+ Background = SystemColors.WindowBrush,
+ Foreground = SystemColors.WindowTextBrush,
+ UseLayoutRounding = true
+ };
+
+ if (owner != null)
+ {
+ _window.Owner = owner;
+ }
+
+ _window.ContentRendered += Window_ContentRendered;
+ _window.KeyDown += Window_KeyDown;
+ _window.Closed += (s, e) => _window = null;
+
+ // ShowDialog 提供真正的模态输入和独立 HWND;所有者关系保证窗口层级与输入路由正确。
+ _window.ShowDialog();
+
+ return Task.FromResult(_result);
+ }
+
+ public void Hide()
+ {
+ if (_window == null) return;
+
+ _result = SecurityPromptDialogResult.None;
+ _window.Close();
+ }
+
+ private Window ResolveOwner()
+ {
+ if (Owner != null && Owner.IsVisible)
+ {
+ return Owner;
+ }
+
+ var focusedElement = Keyboard.FocusedElement as DependencyObject;
+ var focusedWindow = focusedElement != null ? Window.GetWindow(focusedElement) : null;
+ if (focusedWindow != null && focusedWindow.IsVisible)
+ {
+ return focusedWindow;
+ }
+
+ var application = Application.Current;
+ if (application != null)
+ {
+ foreach (Window window in application.Windows)
+ {
+ if (window != null && window.IsVisible && window.IsActive)
+ {
+ return window;
+ }
+ }
+
+ var mainWindow = application.MainWindow;
+ if (mainWindow != null && mainWindow.IsVisible)
+ {
+ return mainWindow;
+ }
+ }
+
+ return null;
+ }
+
+ private void Window_ContentRendered(object sender, EventArgs e)
+ {
+ var window = _window;
+ if (window == null) return;
+
+ window.Activate();
+ window.Focus();
+ Opened?.Invoke(this, EventArgs.Empty);
+ }
+
+ private void Window_KeyDown(object sender, KeyEventArgs e)
+ {
+ if (e.Key != Key.Escape) return;
+
+ Complete(SecurityPromptDialogResult.Secondary);
+ e.Handled = true;
+ }
+
+ private void Complete(SecurityPromptDialogResult result)
+ {
+ var window = _window;
+ if (window == null) return;
+
+ _result = result;
+ window.Close();
+ }
+
+ private static Button CreateButton(string text, string styleKey)
+ {
+ var button = new Button
+ {
+ Content = text,
+ MinWidth = 88,
+ Height = 32,
+ Margin = new Thickness(8, 0, 0, 0),
+ Padding = new Thickness(12, 0, 12, 0)
+ };
+
+ try
+ {
+ var style = Application.Current?.FindResource(styleKey) as Style;
+ if (style != null) button.Style = style;
+ }
+ catch
+ {
+ // 主题资源不可用时保留 WPF 默认按钮样式,不影响输入和确认。
+ }
+
+ return button;
+ }
+ }
+}