diff --git a/Ink Canvas/Helpers/SecurityManager.cs b/Ink Canvas/Helpers/SecurityManager.cs index 252187627..37d2a45e6 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 ?? ""; diff --git a/Ink Canvas/Helpers/SecurityPromptDialog.cs b/Ink Canvas/Helpers/SecurityPromptDialog.cs new file mode 100644 index 000000000..c2aa55ef3 --- /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; + } + } +}