-
Notifications
You must be signed in to change notification settings - Fork 0
Exit Dialog
Relevant source files
This document covers the ExitDialog class and its role in managing application exit behavior. The Exit Dialog presents users with options for how the application should behave when the main window is closed, and allows them to remember their preference for future sessions.
For information about general application settings, see Settings Dialog. For information about cache persistence mechanisms, see CacheManager.
The ExitDialog is a modal dialog that appears when the user attempts to close the main application window and has not yet saved a default close operation preference. It provides two exit strategies and an option to remember the user's choice.
Primary Responsibilities:
- Present exit behavior options to the user
- Capture and persist user preferences via
CacheManager - Execute the selected exit action (terminate or minimize to tray)
- Apply localization and theming to dialog components
Sources: src/io/github/samera2022/mouse_macros/ui/frame/ExitDialog.java L1-L103
The ExitDialog is instantiated with a reference to the parent MainFrame and immediately configured as a modal dialog. The constructor performs all initialization, including UI assembly, localization, theming, and event handler registration.
Initialization Sequence:
| Step | Action | Code Reference |
|---|---|---|
| 1 | Set dialog properties (title, icon, modality) | ExitDialog.java L20-L23 |
| 2 | Create layout with BorderLayout and BoxLayout | ExitDialog.java L24-L27 |
| 3 | Build title label with bold 18pt font | ExitDialog.java L29-L37 |
| 4 | Create radio button group for exit options | ExitDialog.java L43-L53 |
| 5 | Add "remember this option" checkbox | ExitDialog.java L57-L63 |
| 6 | Add "Finish" button | ExitDialog.java L68-L72 |
| 7 | Apply theme based on dark mode setting | ExitDialog.java L75-L76 |
| 8 | Adjust frame size with caching | ExitDialog.java L79 |
| 9 | Register event handlers | ExitDialog.java L81-L101 |
Sources: src/io/github/samera2022/mouse_macros/ui/frame/ExitDialog.java L19-L102
The dialog uses a nested layout structure:
-
Root Container:
BorderLayoutwith 10px horizontal and vertical gaps -
Center Region:
BoxLayout.Y_AXISwith 20-30px border padding - South Region: Button panel with 10px top/bottom padding
Vertical Spacing:
- 10px between title and separator
- 10px between separator and radio buttons
- 10px between radio buttons and remember checkbox
- 10px between remember checkbox and bottom edge
Sources: src/io/github/samera2022/mouse_macros/ui/frame/ExitDialog.java L24-L72
The dialog presents two mutually exclusive exit behaviors through radio buttons:
When selected, clicking "Finish" terminates the application immediately by calling System.exit(0).
Implementation:
case CacheManager.EXIT_ON_CLOSE:
System.exit(0);
break;
Constant: CacheManager.EXIT_ON_CLOSE = "exit_on_close"
When selected, clicking "Finish" hides the main window and places an icon in the system tray by calling mf.minimizeToTray().
Implementation:
case CacheManager.MINIMIZE_TO_TRAY:
mf.minimizeToTray();
break;
Constant: CacheManager.MINIMIZE_TO_TRAY = "minimize_to_tray"
The checkbox allows users to persist their choice. When checked, the selected exit behavior is saved to CacheManager.cache.defaultCloseOperation and persisted to cache.json.
Persistence Logic:
| Checkbox State | Radio Selection | Action |
|---|---|---|
| Unchecked | Any | Execute action once, do not save preference |
| Checked | Exit on Close | Save "exit_on_close" to cache, then exit |
| Checked | Minimize to Tray | Save "minimize_to_tray" to cache, then minimize |
Sources: src/io/github/samera2022/mouse_macros/ui/frame/ExitDialog.java L81-L100, src/io/github/samera2022/mouse_macros/manager/CacheManager.java L19-L27
The finish button's action listener executes the following sequence:
-
Determine Selection: Read which radio button is selected and assign the corresponding constant to
op -
Conditional Persistence: If "remember this option" is checked, write
optocache.defaultCloseOperationand callCacheManager.saveCache() -
Execute Action: Switch on
opvalue and perform corresponding exit behavior
Sources: src/io/github/samera2022/mouse_macros/ui/frame/ExitDialog.java L81-L100
The dialog registers a WindowClosingAdapter to handle the window close event (X button). This adapter simply disposes the dialog without executing any exit action.
Sources: src/io/github/samera2022/mouse_macros/ui/frame/ExitDialog.java L101, src/io/github/samera2022/mouse_macros/adapter/WindowClosingAdapter.java
The ExitDialog interacts with CacheManager for preference persistence:
Cache Structure Used:
public class Cache {
// ... other fields
public String defaultCloseOperation = "";
}Operations:
| Method | Purpose | Code Location |
|---|---|---|
| Read current preference | Determine if dialog should be shown | External to ExitDialog |
| Write preference | Persist user's choice | ExitDialog.java L86-L87 |
| Save to disk | Write cache.json file | ExitDialog.java L87 |
Cache File Location: {LOCAL_STORAGE_PATH}/cache.json
Sources: src/io/github/samera2022/mouse_macros/ui/frame/ExitDialog.java L85-L87, src/io/github/samera2022/mouse_macros/manager/CacheManager.java L23-L36
All visible text is loaded from the Localizer using translation keys:
| Component | Translation Key | Purpose |
|---|---|---|
| Dialog title | "exit" |
Window title bar |
| Title label | "exit.title" |
Main heading |
| Exit radio button | "exit.exit_on_close" |
First option label |
| Minimize radio button | "exit.minimize_to_tray" |
Second option label |
| Remember checkbox | "exit.remember_this_option" |
Checkbox label |
| Finish button | "exit.finish" |
Action button label |
Sources: src/io/github/samera2022/mouse_macros/ui/frame/ExitDialog.java L20-L68
The dialog applies dark or light mode based on config.enableDarkMode:
int mode = config.enableDarkMode ? OtherConsts.DARK_MODE : OtherConsts.LIGHT_MODE;
ComponentUtil.setMode(getContentPane(), mode);
This recursively applies color schemes to all child components, including:
- Panel backgrounds
- Label foregrounds
- Button colors
- Radio button styling
- Checkbox appearance
Sources: src/io/github/samera2022/mouse_macros/ui/frame/ExitDialog.java L75-L76
The dialog uses ComponentUtil.adjustFrameWithCache() to determine its dimensions based on:
- Cached window size from previous sessions
- Current language's text length
-
readjustFrameModeconfiguration setting
Components Passed for Sizing Calculation:
ComponentUtil.adjustFrameWithCache(
this, // Dialog to resize
70, // Base width offset
new JComponent[]{titleLabel}, // Title components
new JComponent[]{exitOnCloseRadio, minimizeToTrayRadio}, // Radio buttons
new JComponent[]{rememberLabel, rememberOptionBox}, // Remember section
new JComponent[]{finishButton} // Action button
);
Sources: src/io/github/samera2022/mouse_macros/ui/frame/ExitDialog.java L79
| Parameter | Type | Purpose |
|---|---|---|
mf |
MainFrame |
Reference to parent window for minimize operation and dialog positioning |
The ExitDialog class imports and uses:
-
Swing Components:
JDialog,JLabel,JPanel,JButton,JCheckBox,JSeparator,ButtonGroup -
Layout Managers:
BorderLayout,BoxLayout,FlowLayout -
Custom Components:
CustomRadioButton -
Managers:
CacheManager,ConfigManager -
Utilities:
Localizer,ComponentUtil -
Adapters:
WindowClosingAdapter -
Constants:
OtherConsts,IconConsts
Sources: src/io/github/samera2022/mouse_macros/ui/frame/ExitDialog.java L1-L16
The exit behavior options are defined as string constants in CacheManager:
public static final String EXIT_ON_CLOSE = "exit_on_close";
public static final String MINIMIZE_TO_TRAY = "minimize_to_tray";
public static final String UNKNOWN = "";
These constants are used for:
- Storing the user's preference in
cache.defaultCloseOperation - Matching against the switch statement in the finish button handler
- Comparing cached preferences in external code that determines whether to show the dialog
Sources: src/io/github/samera2022/mouse_macros/manager/CacheManager.java L19-L21