Skip to content

Commit 22c1e6f

Browse files
fix: make attachListeners idempotent to dedupe registrations
Addresses Codex P2 review on #210: `initialize()` walks Window.getWindows() to cover already-open windows, and the WINDOW_OPENED AWTEventListener also lands the same listener when those windows subsequently fire WINDOW_OPENED. Without dedupe each graphicsConfiguration change runs the relayout path multiple times, and uninitialize()'s single removeListener call leaves stale registrations behind across repeated setLookAndFeel cycles. Fix: in attachListeners, call removePropertyChangeListener / removeComponentListener before the add. Both are no-ops if the listener was not previously registered, so it is safe on the first call too. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 77c92e1 commit 22c1e6f

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

src/main/java/mdlaf/MaterialLookAndFeel.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,18 @@ private void maybeRevalidate(Window window) {
926926
}
927927

928928
private void attachListeners(Window window) {
929+
// Remove-before-add keeps this method idempotent. initialize() walks
930+
// Window.getWindows() to cover already-open windows, and the WINDOW_OPENED
931+
// AWTEventListener also lands here when those same windows subsequently fire
932+
// WINDOW_OPENED (a window created before initialize() but shown afterward).
933+
// Without this dedupe each graphicsConfiguration change would run the relayout
934+
// path multiple times, and uninitialize()'s single removeListener call would
935+
// leave stale registrations behind across repeated setLookAndFeel cycles.
936+
// PropertyChangeSupport.removePropertyChangeListener and
937+
// Component.removeComponentListener are no-ops if the listener was not previously
938+
// registered, so this is safe on the first call too.
939+
window.removePropertyChangeListener("graphicsConfiguration", graphicsConfigurationListener);
940+
window.removeComponentListener(componentResizeListener);
929941
window.addPropertyChangeListener("graphicsConfiguration", graphicsConfigurationListener);
930942
window.addComponentListener(componentResizeListener);
931943
GraphicsConfiguration gc = window.getGraphicsConfiguration();

0 commit comments

Comments
 (0)