Fix message box close behavior#4560
Conversation
There was a problem hiding this comment.
1 issue found across 3 files
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
📝 WalkthroughWalkthroughMessageBoxEx now hides the title bar close button for YesNo dialogs and pre-computes a default close result used when the dialog closes without a button click. KeyEsc_OnPress and Button_Cancel were updated accordingly, and CustomWindowTitleBar disables OS Aero caption buttons on host windows. ChangesMessageBoxEx close behavior
Estimated code review effort: 2 (Simple) | ~15 minutes Sequence Diagram(s)sequenceDiagram
participant User
participant MessageBoxEx
participant DefaultCloseResult
User->>MessageBoxEx: Show(...)
MessageBoxEx->>DefaultCloseResult: compute based on MessageBoxButton
DefaultCloseResult-->>MessageBoxEx: _result preset
User->>MessageBoxEx: Press Escape or click close button
alt YesNo dialog
MessageBoxEx-->>User: return without closing
else other button types
MessageBoxEx->>MessageBoxEx: DialogResult = false, close
MessageBoxEx-->>User: returns preset _result
end
Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
Flow.Launcher/MessageBoxEx.xaml.cs (1)
59-64: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winRedundant
_resultwrites inSetButtonVisibilityFocusAndResultare now dead code.
SetButtonVisibilityFocusAndResult(button, defaultResult)at Line 59 sets_resultper focus/defaultResult logic, but Line 63 unconditionally overwrites it withmsgBox.DefaultCloseResultbeforeShowDialog()is even called. The intermediate_result = ...assignments insideSetButtonVisibilityFocusAndResult(e.g. lines 85, 93, 98, 107, 112, 120, 125, 130) never have any observable effect anymore. Functionally this is fine today sinceDefaultCloseResultcorrectly encapsulates the intended close-without-button-click semantics, but the dead writes are misleading for anyone maintaining this method later (they'll assume changing them affects the close result).Consider removing the now-unused
_resultassignments fromSetButtonVisibilityFocusAndResult, or renaming/refactoring so it's clear that method only controls visibility/focus andDefaultCloseResultis the sole source of truth for the pre-set result.♻️ Example cleanup (illustrative)
case MessageBoxButton.OK: msgBox.btnCancel.Visibility = Visibility.Collapsed; msgBox.btnNo.Visibility = Visibility.Collapsed; msgBox.btnYes.Visibility = Visibility.Collapsed; msgBox.btnOk.Focus(); - _result = MessageBoxResult.OK; break;(repeat for other branches once confirmed the pre-
ShowDialogvalue is always superseded byDefaultCloseResult)Also applies to: 76-136
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@Flow.Launcher/MessageBoxEx.xaml.cs` around lines 59 - 64, The `_result` writes inside `SetButtonVisibilityFocusAndResult` are dead code because `MessageBoxEx` always overwrites `_result` with `msgBox.DefaultCloseResult` before `ShowDialog()`; remove the redundant `_result` assignments in that method and keep it focused on button visibility/focus only, or refactor its name/signature to make that responsibility clear. Use `SetButtonVisibilityFocusAndResult`, `_result`, and `DefaultCloseResult` as the key symbols when updating the logic.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@Flow.Launcher/MessageBoxEx.xaml.cs`:
- Around line 59-64: The `_result` writes inside
`SetButtonVisibilityFocusAndResult` are dead code because `MessageBoxEx` always
overwrites `_result` with `msgBox.DefaultCloseResult` before `ShowDialog()`;
remove the redundant `_result` assignments in that method and keep it focused on
button visibility/focus only, or refactor its name/signature to make that
responsibility clear. Use `SetButtonVisibilityFocusAndResult`, `_result`, and
`DefaultCloseResult` as the key symbols when updating the logic.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: cf839378-d166-43be-be51-66a74fa1a582
📒 Files selected for processing (3)
Flow.Launcher/MessageBoxEx.xamlFlow.Launcher/MessageBoxEx.xaml.csFlow.Launcher/Resources/Controls/CustomWindowTitleBar.xaml.cs
069d536 to
0a99d3d
Compare
This lets us properly disable the close button Previously even when we hid the custom one the default one was then clickable underneath
Since its should be unreachable we can safely go back to refusing to close the window if that button is somehow called when type is YesNo
…closed outside of response buttons
0a99d3d to
834ba11
Compare
Should fix #4503
Tries to make the message box follow the official behavior described in https://learn.microsoft.com/en-us/dotnet/api/system.windows.messageboxbutton?view=windowsdesktop-10.0#remarks
Particularly regarding the Yes/No type
For that type the close button is to be disabled ( I also hide it), and the close by esc functionality is also disabled
Its not really supposed to have a way to close it without selecting yes or no.
Despite that in the edge case of a forced close (e.g. alt-f4) we still have to decide on a result
It's not 100% clear what the result should be in that case, e.g. Cancel or None
But given how many users of this message box make the mistake of treating !No as Yes, it is probably safer to return No here
Summary by cubic
Aligns MessageBoxEx close behavior with Windows. Hides the close button for Yes/No, fixes titlebar close handling, and sets a safe default result when closed indirectly.
Summary of changes
TitleBar(TitleBar.ShowCloseButton = false).CustomWindowTitleBar(WindowChrome.UseAeroCaptionButtons = false) so the OS close isn’t clickable behind the custom bar.DefaultCloseResultto mirrorSystem.Windows.MessageBoxwhen closed indirectly: OK→OK, OKCancel/YesNoCancel→Cancel, YesNo→No, else→None._resulttoDefaultCloseResultbefore showing the dialog to ensure consistent outcomes._resultassignments in Esc/close handlers; rely onDefaultCloseResult.Release Note
Message boxes now follow Windows behavior: the close button is hidden for Yes/No, and closing the window picks a reasonable default answer.
Written for commit 834ba11. Summary will update on new commits.