If you're closing the dropdown menu by selecting an option, you should include the reset actions in the DropDownValueChanged callback function. However, if a selection is not made, that function will not be evoked in which case this demo may be helpful.
How to simulate a DropDownClosing function
There may be an undocumented event that could be used by a listener to detect when the menu is closed but I don't know of such an event at the moment.
Instead, this demo shows how to use the mouse position to detect when the mouse leaves the dropdown menu area which triggers a callback function that you can define. This uses a pointer manager which became available in AppDesigner in Matlab R2021a (see announcement). Steps
properties (Access = private)
pointMngr
end
function startupFcn(app)
app.pointMngr.enterFcn = [];
app.pointMngr.exitFcn = [];
app.pointMngr.traverseFcn = [];
iptSetPointerBehavior(app.DropDown, app.pointMngr)
iptPointerManager(app.UIFigure,'enable');
end
3. Add a private function that defines "closing" actions (how to add a private function). This function will be evoked when the mouse leaves the dropdown menu area or when a selection is made in the dropdown menu. Importantly, the last two lines reset the exit behavior so that this function isn't triggered every time the mouse travels across the dropdown button without interacting with it. The exit function will be defined again by the DropDownOpeningFcn (step 4). The rest of this block merely indicates that the function was evoked. function cursorEnterLeavePanelFcn(app, ~)
cla(app.UIAxes)
text(app.UIAxes, 0.1, 0.7, ['CLOSED', newline, datestr(now())],...
'FontSize', 18)
app.pointMngr.exitFcn = [];
iptSetPointerBehavior(app.DropDown, app.pointMngr)
end
4. Assign dropdown opening function. In addition to whatever actions this callback function already contains in your code, set the exitFcn for the pointer manager (first two lines below). The rest of this block merely changes the text within the app axes to indicate that the function was evoked.
function DropDownOpening(app, event)
app.pointMngr.exitFcn = @(~,~) cursorEnterLeavePanelFcn(app, app.DropDown);
iptSetPointerBehavior(app.DropDown, app.pointMngr)
cla(app.UIAxes)
text(app.UIAxes, 0.1, 0.7, ['OPENED', newline, datestr(now())],...
'FontSize', 18, 'Color', 'r')
end
5. Assign value changed function to dropdown. The menu will close when a selection is made so this evokes the closing actions instead of relying on mouse movement.
function DropDownValueChanged(app, event)
cursorEnterLeavePanelFcn(app, [])
end
Limitations
- Pressing escape will close the dropdown window without the need to move the mouse. To evoke the simulated closing function, the mouse needs to move by at least 1 pixel after pressing escape.
- The simulated closing function is evoked when the mouse moves outside of the dropdown object even if the dropdown window is still opened. Re-entering the window does nothing since the app already executing the closing function. There are alternative behaviors you could arrange such as using an underlying uipanel as a pointer object or by assigning a EnterFcn to detect when the mouse re-enters the dropdown menu.
- Pointer manager is supported with AppDesigner starting with Matlab R2021a.