Skip to content

Commit

Permalink
refactor: ModeOverlay
Browse files Browse the repository at this point in the history
  • Loading branch information
ktgw0316 committed Feb 18, 2024
1 parent 3e9ebaa commit 823b5bd
Showing 1 changed file with 17 additions and 35 deletions.
52 changes: 17 additions & 35 deletions lightcrafts/src/main/java/com/lightcrafts/ui/mode/ModeOverlay.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package com.lightcrafts.ui.mode;

import com.lightcrafts.utils.awt.geom.HiDpi;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import javax.swing.event.MouseInputListener;
Expand All @@ -24,16 +25,16 @@ public class ModeOverlay extends JLayeredPane {

private Integer OverlayLayer = 1;

private Component underlay;
private Stack<Mode> modes;
private MouseMotionListener autoscroller;
private final Component underlay;
private final Stack<Mode> modes;
private final MouseMotionListener autoscroller;

private Collection<MouseInputListener> mouseListeners;
private final Collection<MouseInputListener> mouseListeners;

public ModeOverlay(Component underlay) {
this.underlay = underlay;
modes = new Stack<Mode>();
mouseListeners = new LinkedList<MouseInputListener>();
modes = new Stack<>();
mouseListeners = new LinkedList<>();
setLayout(null);
add(underlay, UnderlayLayer);

Expand All @@ -52,32 +53,23 @@ public void mouseDragged(MouseEvent e) {
}

public Mode peekMode() {
try {
return modes.peek();
}
catch (EmptyStackException e) {
return null;
}
return modes.empty() ? null : modes.peek();
}

public Mode popMode() {
try {
Mode oldMode = modes.pop();
if (oldMode != null) {
removeMode(oldMode);
decLayer();
repaint();
}
return oldMode;
}
catch (EmptyStackException e) {
if (modes.empty())
return null;
}

@NotNull Mode oldMode = modes.pop();
removeMode(oldMode);
OverlayLayer--;
repaint();
return oldMode;
}

public void pushMode(Mode mode) {
public void pushMode(@NotNull Mode mode) {
modes.push(mode);
incLayer();
OverlayLayer++;
addMode(mode);
validate();
}
Expand Down Expand Up @@ -115,16 +107,6 @@ public void removeMouseInputListener(MouseInputListener listener) {
removeMouseMotionListener(listener);
}

private void incLayer() {
int i = OverlayLayer;
OverlayLayer = ++i;
}

private void decLayer() {
int i = OverlayLayer;
OverlayLayer = --i;
}

private void addMode(Mode mode) {
JComponent overlay = mode.getOverlay();
overlay.setOpaque(false);
Expand Down

0 comments on commit 823b5bd

Please sign in to comment.