feat: add signal option to dialog.showMessageBox#26102
Conversation
I did not know it exists, it is a better design than a custom ID system. So I think the new API would look like this? const controller = new AbortController();
dialog.showMessageBox(win, {
signal: controller.signal,
message: 'message',
})
controller.abort();There are 2 downsides though:
So I think maybe we can invent our own const controller = new dialog.MessageBoxAbortController()
dialog.showMessageBox(win, {
signal: controller.signal,
message: 'message',
})
controller.abort();
// future extensions.
controller.close({button: 1}) |
|
I'm not convinced we need the option to "pretend" that a particular button was pressed. User code can handle that plumbing by itself if needed, e.g. const controller = new AbortController()
let abortedSignal = null
dialog.showMessageBox(win, {
signal: controller.signal,
message: 'message'
}).then(status => {
if (status == null)
status = abortedSignal
})
abortedSignal = 1
controller.abort();as for Node adding its own, it seems like they are planning to match the DOM implementation (similar to their implementation of |
|
I'm going to wait for the next major upgrade of Node in Electron, adding typescript declarations for our own |
97d0eff to
1e3e800
Compare
a088707 to
c1fd58a
Compare
|
I have updated this PR to use |
| const signal = controller.signal; | ||
| const w = new BrowserWindow(); | ||
| const p = dialog.showMessageBox(w, { signal, message: 'i am message' }); | ||
| await delay(500); |
There was a problem hiding this comment.
Why 500ms here? Would 0ms work equally well? If not, is there some event we can listen for?
delay(500) smells very much like flake to me.
There was a problem hiding this comment.
On Windows it takes some time before we can know the dialog's HWND (the dialog runs in a new thread and the dialog HWND is created asynchronously), the delay here is to make the test run the code path that closes the window after HWND is available.
Sometimes even 500ms is not enough for the HWND to be available, but this is fine as no matter which code path it runs the showMessageBox should be cancelled successfully.
There was a problem hiding this comment.
Hm, is there any other way we could detect this event? e.g. a blur event?
There was a problem hiding this comment.
Showing a a child dialog does not remove the focus of parent window, so browser window's blur event will not emit. The only way I can think of is to load a web page and setup a window.onblur listener in it to send the event, which needs to careful to be not flaky.
Waiting for fixed time is usually bad because it makes test flaky, but this test is not flaky no matter what the timeout is, it only needs to confirm dialog can be closed after fully opened, and I think delay(500) does the work well.
There was a problem hiding this comment.
Yeah, it won't necessarily make the test fail, but it does mean the test will test different things, undetectably, depending on how fast the underlying OS is. I'd prefer that our tests test the same thing every time if possible.
I won't block the PR on this concern though.
|
Release Notes Persisted
|
|
I was unable to backport this PR to "14-x-y" cleanly; |
* mac: add dialog.closeMessageBox API * win: Implement dialog.closeMessageBox * mac: Return cancelId with closeMessageBox * gtk: Implement dialog.closeMessageBox * win: Fix 32bit build * win: Reduce the scope of lock * fix: Build error after rebase * feat: Use AbortSignal to close message box * chore: silently handle duplicate ID * win: Add more notes about the threads * chore: apply reviews * fix: base::NoDestructor should be warpped in function * chore: fix style on windows
* mac: add dialog.closeMessageBox API * win: Implement dialog.closeMessageBox * mac: Return cancelId with closeMessageBox * gtk: Implement dialog.closeMessageBox * win: Fix 32bit build * win: Reduce the scope of lock * fix: Build error after rebase * feat: Use AbortSignal to close message box * chore: silently handle duplicate ID * win: Add more notes about the threads * chore: apply reviews * fix: base::NoDestructor should be warpped in function * chore: fix style on windows

Description of Change
This PR adds a
signaloption to close message box, once this API is approved, I'll implement same API to close file dialogs. Refs #5577.Duplicate code
There are some duplicate code when checking dialog IDs, which I would like to refactor after adding the API for closing file dialogs. Optimizing them in this PR is too early.
Checklist
npm testpassesRelease Notes
Notes: Add
signaloption todialog.showMessageBox.