Skip to content

Commit

Permalink
Application: Make memory indicator less ugly
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadelessFox committed Mar 11, 2024
1 parent 8d807aa commit 386de0a
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import com.shade.decima.model.app.ProjectChangeListener;
import com.shade.decima.model.app.ProjectContainer;
import com.shade.decima.model.app.ProjectManager;
import com.shade.decima.ui.controls.MemoryIndicator;
import com.shade.decima.ui.editor.NodeEditorInputLazy;
import com.shade.decima.ui.editor.ProjectEditorInput;
import com.shade.decima.ui.menu.menus.HelpMenu;
Expand All @@ -35,6 +34,7 @@
import com.shade.platform.model.runtime.VoidProgressMonitor;
import com.shade.platform.ui.PlatformMenuConstants;
import com.shade.platform.ui.UIColor;
import com.shade.platform.ui.controls.MemoryIndicator;
import com.shade.platform.ui.editors.Editor;
import com.shade.platform.ui.editors.EditorChangeListener;
import com.shade.platform.ui.editors.EditorInput;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package com.shade.platform.ui.controls;

import com.shade.platform.ui.util.UIUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.HierarchyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;

public class MemoryIndicator extends JComponent {
private static final int MB = 1024 * 1024;
private static final int UPDATE_INTERVAL = 1000;

private final MemoryMXBean bean = ManagementFactory.getMemoryMXBean();
private MemoryUsage usage = bean.getHeapMemoryUsage();

public MemoryIndicator() {
final Timer timer = new Timer(UPDATE_INTERVAL, e -> refresh());
timer.setInitialDelay(0);

addHierarchyListener(e -> {
if (e.getID() == HierarchyEvent.HIERARCHY_CHANGED && (e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0) {
if (isShowing()) {
timer.start();
} else {
timer.stop();
}
}
});

addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
bean.gc();
}

@Override
public void mouseEntered(MouseEvent e) {
repaint();
}

@Override
public void mouseExited(MouseEvent e) {
repaint();
}
});

ToolTipManager.sharedInstance().registerComponent(this);
}

public void refresh() {
usage = bean.getHeapMemoryUsage();
repaint();
}

@Override
protected void paintComponent(Graphics g) {
final Graphics2D g2 = (Graphics2D) g.create();

final int width = getWidth();
final int height = getHeight();
final int used = (int) (width * usage.getUsed() / usage.getMax());
final int committed = (int) (width * usage.getCommitted() / usage.getMax());

g2.setColor(UIManager.getColor("MemoryIndicator.usedBackground"));
g2.fillRect(0, 0, used, height);
g2.setColor(UIManager.getColor("MemoryIndicator.committedBackground"));
g2.fillRect(used, 0, committed - used, height);
g2.setColor(UIManager.getColor("MemoryIndicator.maxBackground"));
g2.fillRect(committed, 0, width - committed, height);

UIUtils.setRenderingHints(g2);
g2.setColor(UIManager.getColor(getMousePosition() != null ? "MemoryIndicator.hoverForeground" : "MemoryIndicator.foreground"));
g2.setFont(UIManager.getFont("MemoryIndicator.font"));

final FontMetrics metrics = g2.getFontMetrics();
final String text = "%d of %dM".formatted(usage.getUsed() / MB, usage.getMax() / MB);
g2.drawString(text, (width - metrics.stringWidth(text)) / 2, (height - metrics.getHeight()) / 2 + metrics.getAscent());

g2.dispose();
}

@Override
public String getToolTipText(MouseEvent event) {
return """
<html>
<table>
<tr><td>Used:</td><td align=right>%sM</td></tr>
<tr><td>Committed:</td><td align=right>%sM</td></tr>
<tr><td>Max:</td><td align=right>%sM</td></tr>
</table>
</html>"""
.formatted(
usage.getUsed() / MB,
usage.getCommitted() / MB,
usage.getMax() / MB
);
}

@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(100, 22);
}

@Override
public Dimension getMinimumSize() {
if (isMinimumSizeSet()) {
return super.getMinimumSize();
}
return getPreferredSize();
}

@Override
public Dimension getMaximumSize() {
if (isMaximumSizeSet()) {
return super.getMaximumSize();
}
return getPreferredSize();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ Text.numberForeground = #4dacf0
Text.stringForeground = #62a362
Text.stringEscapeForeground = #e0957b

# Memory Indicator
MemoryIndicator.foreground = @foreground
MemoryIndicator.hoverForeground = lighten(@foreground,40%)
MemoryIndicator.maxBackground = mix($Component.accentColor,@background,5%)
MemoryIndicator.committedBackground = mix($Component.accentColor,$MemoryIndicator.maxBackground,20%)
MemoryIndicator.usedBackground = mix($Component.accentColor,$MemoryIndicator.maxBackground,40%)

# Hex Editor
HexEditor.background = @background
HexEditor.oddBackground = darken(HexEditor.background,5%,lazy)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ Text.numberForeground = #0000ff
Text.stringForeground = #008000
Text.stringEscapeForeground = #000080

# Memory Indicator
MemoryIndicator.foreground = lighten(@foreground,40%)
MemoryIndicator.hoverForeground = @foreground
MemoryIndicator.font = $medium.font
MemoryIndicator.maxBackground = mix($Component.accentColor,@background,5%)
MemoryIndicator.committedBackground = mix($Component.accentColor,$MemoryIndicator.maxBackground,15%)
MemoryIndicator.usedBackground = mix($Component.accentColor,$MemoryIndicator.maxBackground,30%)

# Hex Editor
HexEditor.background = #fff
HexEditor.oddBackground = darken(HexEditor.background,6%,lazy)
Expand Down

0 comments on commit 386de0a

Please sign in to comment.