Dialogs and User Feedback (MessageDialog)
Modern applications must not fail silently.When a user performs an invalid action, the application should provide clear feedback.
In this Weather App, we use MessageDialog from Libadwaita to inform the user when:
- The city name is empty
- The city already exists in the list
MessageDialog is a modal dialog window that appears on top of the main window. It temporarily blocks interaction with the application until the user responds.
This ensures that the user understands what went wrong.
Why We Use MessageDialog
Imagine the user clicks Add without typing anything.If nothing happens, the user becomes confused.Instead, we show a dialog that clearly explains the problem.
This improves:
- Usability
- Error handling
- User experience
- Professional interface behavior
Dialogs are part of interaction design, not just layout design.
Example: Showing a Dialog for Empty Input
Below is a simplified version of how the dialog is created in the Weather App.
#![allow(unused)] fn main() { let dialog = MessageDialog::builder() .transient_for(&parent_window) .modal(true) .heading("Empty city name") .body("Please enter a valid city name.") .build(); dialog.add_response("ok", "OK"); dialog.present(); }
What happens here
MessageDialog::builder()creates a new dialog..transient_for(&parent_window)attaches it to the main window..modal(true)blocks interaction until the dialog is closed..heading()sets the main title..body()provides the explanation message..present()displays the dialog.
The dialog now appears on top of the main window.
Example: Duplicate City Warning
If the user tries to add a city that already exists, we show another dialog:
#![allow(unused)] fn main() { let dialog = MessageDialog::builder() .transient_for(&parent_window) .modal(true) .heading("City already exists") .body("This city is already in your saved list.") .build(); dialog.add_response("ok", "OK"); dialog.present(); }
This prevents duplicate entries and maintains clean data.
How MessageDialog Fits Into the UI Structure
MessageDialog is different from layout widgets like GTK Box or NavigationSplitView.
It is not part of the permanent UI structure.Instead, it appears temporarily when needed.
So in our app architecture:
- Layout widgets structure the interface.
- Content widgets display data.
- Interactive widgets respond to actions.
- Dialog widgets provide feedback.
This completes the full interaction cycle.
What Beginners Should Understand
MessageDialog teaches an important concept:
A good application does not just display information. It communicates with the user.Handling incorrect input is part of building professional software. Even small applications should:
- Validate user input
- Inform users about mistakes
- Prevent silent failures