MATLAB App Designer uialert vs uiconfirm: Stacking Order and Focus Management
0 reputation · 25 Sept 2020, 08:31 UTC
0 reputation · 25 Sept 2020, 08:31 UTC
In an App Designer application that streams data, the goal is to display critical warnings and decision prompts without interrupting the user’s workflow or causing alert fatigue.
Both uialert and uiconfirm are modal and block interaction until dismissed. When multiple asynchronous callbacks (e.g., timers or listeners) trigger these dialogs, the order in which they appear and which dialog retains focus is not clearly documented.
Unresolved behavior: when two modal dialogs are requested nearly simultaneously, MATLAB may stack them or allow the second to overwrite the first. This can lead to lost focus and confusing user experience.
uialert or uiconfirm calls are made from concurrent callbacks?35275 reputation · 25 Sept 2020, 09:55 UTC
uialert and uiconfirm are modal functions that block the execution of the calling callback until the user provides input. When multiple asynchronous callbacks—such as timers or listeners—trigger these dialogs simultaneously, the behavior is governed by the MATLAB event queue.
When two alerts are requested nearly simultaneously, MATLAB does not 'stack' them in a way that allows both to be visible at. Because these functions are modal, the first call blocks the execution of its specific callback. The second callback remains in the event queue until the first dialog is dismissed and its associated callback completes.
uialert line until the first alert is cleared.To prevent alert fatigue and ensure a predictable user experience, developers should implement a manual notification queue. Instead of calling uialert directly inside asynchronous listeners, push the notification data into a cell array and use a manager function.
% Example logic for a notification queue
if isempty(app.AlertQueue)
app.processNextAlert();
end
app.AlertQueue{end+1} = newNotification;
function processNextAlert(app)
if isempty(app.AlertQueue), return; end
msg = app.AlertQueue{1};
app.AlertQueue(1) = [];
uialert(app.UIFigure, msg, 'Title', 'CloseButton', @(src, event) app.processNextAlert());
Assumption: This assumes you are using the standard modal UIFigure. If you are using drawnow or pause within your callbacks, the event queue may flush prematurely, leading to unexpected dialog stacking.
Use comments to ask for clarification. Post a solution as an answer.
35,275 reputation · 25 Sept 2020, 16:50 UTC
While the event queue manages the sequence of callbacks, it is important to note that uialert and uiconfirm are blocking calls. If a callback is triggered by a timer or listener while a modal dialog is already active, the second callback will not execute until the first dialog is dismissed and the original callback completes.
A critical detail for developers is that focus recovery is not always guaranteed to return to the specific UI component that triggered the event. Depending on the OS window manager and MATLAB release, focus may default back to the main app figure. To ensure a predictable user experience, consider the following: