Skip to content

Exit Dialog

Samera2022 edited this page Jan 30, 2026 · 1 revision

Exit Dialog

Relevant source files

Purpose and Scope

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.


Overview

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


Dialog Instantiation and Lifecycle

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


User Interface Components

Component Hierarchy


Layout Details

The dialog uses a nested layout structure:

  • Root Container: BorderLayout with 10px horizontal and vertical gaps
  • Center Region: BoxLayout.Y_AXIS with 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


Exit Options

The dialog presents two mutually exclusive exit behaviors through radio buttons:

Exit on Close

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"

Minimize to Tray

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"

Remember This Option

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


Dialog Behavior and State Management

Event Handling


The finish button's action listener executes the following sequence:

  1. Determine Selection: Read which radio button is selected and assign the corresponding constant to op
  2. Conditional Persistence: If "remember this option" is checked, write op to cache.defaultCloseOperation and call CacheManager.saveCache()
  3. Execute Action: Switch on op value and perform corresponding exit behavior

Sources: src/io/github/samera2022/mouse_macros/ui/frame/ExitDialog.java L81-L100

Window Closing

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


Integration with Other Systems

CacheManager Integration

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

Localization Integration

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

Theming Integration

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

Dynamic Sizing

The dialog uses ComponentUtil.adjustFrameWithCache() to determine its dimensions based on:

  • Cached window size from previous sessions
  • Current language's text length
  • readjustFrameMode configuration 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


Class Structure

Class Definition


Constructor Parameters

Parameter Type Purpose
mf MainFrame Reference to parent window for minimize operation and dialog positioning

Dependencies

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


Constants

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

Clone this wiki locally