Skip to content

Commit

Permalink
Merge pull request #71 from bric3/new-color-provider
Browse files Browse the repository at this point in the history
New color provider
  • Loading branch information
bric3 authored Jun 10, 2022
2 parents e0b3878 + 2048f29 commit 27d3d22
Show file tree
Hide file tree
Showing 8 changed files with 318 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright 2021 Datadog, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.github.bric3.fireplace;

import io.github.bric3.fireplace.core.ui.Colors;
import io.github.bric3.fireplace.core.ui.DarkLightColor;
import io.github.bric3.fireplace.flamegraph.FrameBox;
import io.github.bric3.fireplace.flamegraph.FrameColorProvider;

import java.awt.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

import static io.github.bric3.fireplace.flamegraph.FrameRenderingFlags.isFocusedFrame;
import static io.github.bric3.fireplace.flamegraph.FrameRenderingFlags.isFocusing;
import static io.github.bric3.fireplace.flamegraph.FrameRenderingFlags.isHighlightedFrame;
import static io.github.bric3.fireplace.flamegraph.FrameRenderingFlags.isHighlighting;
import static io.github.bric3.fireplace.flamegraph.FrameRenderingFlags.isHovered;
import static io.github.bric3.fireplace.flamegraph.FrameRenderingFlags.isMinimapMode;

class DimmingFrameColorProvider<T> implements FrameColorProvider<T> {
public static final Color DIMMED_TEXT = new DarkLightColor(
Colors.rgba(28, 43, 52, 0.68f),
Colors.rgba(255, 255, 255, 0.51f)
);

public static final Color ROOT_NODE = new DarkLightColor(
new Color(0xffeaf6fc),
new Color(0xff091222)
);
private final Function<FrameBox<T>, Color> baseColorFunction;
private final ColorModel reusableDataStructure = new ColorModel(null, null);

private final ConcurrentHashMap<Color, Color> dimmedColorCache = new ConcurrentHashMap<>();

public DimmingFrameColorProvider(Function<FrameBox<T>, Color> baseColorFunction) {
this.baseColorFunction = baseColorFunction;
}


@Override
public ColorModel getColors(FrameBox<T> frame, int flags) {
Color backgroundColor;
Color foreground;

var rootNode = frame.isRoot();
if (rootNode) {
backgroundColor = ROOT_NODE;
} else {
backgroundColor = baseColorFunction.apply(frame);
}

if (!rootNode && shouldDim(flags) && !isMinimapMode(flags)) {
backgroundColor = cachedDim(backgroundColor);
foreground = DIMMED_TEXT;
} else {
foreground = Colors.foregroundColor(backgroundColor);
}

if (isHovered(flags)) {
backgroundColor = Colors.blend(backgroundColor, Colors.translucent_black_40);
}

return reusableDataStructure.set(
backgroundColor,
foreground
);
}

/**
* Dim only if not highlighted or not focused
*
* - highlighting and not highlighted => dim
* - focusing and not focused => dim
* - highlighting and focusing
* - highlighted => nope
* - focusing => nope
*/
private boolean shouldDim(int flags) {
var highlighting = isHighlighting(flags);
var highlightedFrame = isHighlightedFrame(flags);
var focusing = isFocusing(flags);
var focusedFrame = isFocusedFrame(flags);


var dimmedForHighlighting = highlighting && !highlightedFrame;
var dimmedForFocus = focusing && !focusedFrame;


return (dimmedForHighlighting || dimmedForFocus)
&& !(highlighting
&& focusing
&& (highlightedFrame || focusedFrame));
}

private Color cachedDim(Color color) {
return dimmedColorCache.computeIfAbsent(color, Colors::dim);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@

import io.github.bric3.fireplace.core.ui.Colors;
import io.github.bric3.fireplace.core.ui.Colors.Palette;
import io.github.bric3.fireplace.core.ui.DarkLightColor;
import io.github.bric3.fireplace.flamegraph.ColorMapper;
import io.github.bric3.fireplace.flamegraph.FlamegraphView;
import io.github.bric3.fireplace.flamegraph.FrameFontProvider;
import io.github.bric3.fireplace.flamegraph.FrameTextsProvider;
import io.github.bric3.fireplace.flamegraph.ZoomAnimation;
import org.openjdk.jmc.common.util.FormatToolkit;
Expand Down Expand Up @@ -47,7 +49,8 @@ public FlameGraphTab() {
jfrFlamegraphView.configureCanvas(ToolTipManager.sharedInstance()::registerComponent);
jfrFlamegraphView.putClientProperty(FlamegraphView.SHOW_STATS, true);
// jfrFlameGraph.setTooltipComponentSupplier(BalloonToolTip::new);
jfrFlamegraphView.setMinimapShadeColorSupplier(() -> Colors.isDarkMode() ? Colors.translucent_black_40 : Colors.translucent_white_80);
var minimapShade = new DarkLightColor(Colors.translucent_white_80, Colors.translucent_black_40);
jfrFlamegraphView.setMinimapShadeColorSupplier(() -> minimapShade);
var zoomAnimation = new ZoomAnimation();
zoomAnimation.install(jfrFlamegraphView);

Expand All @@ -57,10 +60,10 @@ public FlameGraphTab() {
colorModeJComboBox.setSelectedItem(defaultFrameColorMode);

ActionListener updateColorSettingsListener = e -> {
jfrFlamegraphView.setColorFunction(
((JfrFrameColorMode) colorModeJComboBox.getSelectedItem())
.colorMapperUsing(ColorMapper.ofObjectHashUsing(
((Palette) colorPaletteJComboBox.getSelectedItem()).colors())));
var frameBoxColorFunction = ((JfrFrameColorMode) colorModeJComboBox.getSelectedItem())
.colorMapperUsing(ColorMapper.ofObjectHashUsing(
((Palette) colorPaletteJComboBox.getSelectedItem()).colors()));
jfrFlamegraphView.setFrameColorProvider(new DimmingFrameColorProvider<>(frameBoxColorFunction));
jfrFlamegraphView.requestRepaint();
};
colorPaletteJComboBox.addActionListener(updateColorSettingsListener);
Expand Down Expand Up @@ -98,7 +101,7 @@ public FlameGraphTab() {
jfrFlamegraphView.showMinimap(defaultShowMinimap);
jfrFlamegraphView.configureCanvas(ToolTipManager.sharedInstance()::registerComponent);
jfrFlamegraphView.putClientProperty(FlamegraphView.SHOW_STATS, true);
jfrFlamegraphView.setMinimapShadeColorSupplier(() -> Colors.isDarkMode() ? Colors.translucent_black_40 : Colors.translucent_white_80);
jfrFlamegraphView.setMinimapShadeColorSupplier(() -> minimapShade);
zoomAnimation.install(jfrFlamegraphView);
if (dataApplier != null) {
dataApplier.accept(jfrFlamegraphView);
Expand Down Expand Up @@ -175,12 +178,7 @@ public FlameGraphTab() {
add(controlPanel, BorderLayout.NORTH);
add(wrapper, BorderLayout.CENTER);
}

public FlameGraphTab(StacktraceTreeModel stacktraceTreeModel) {
this();
setStacktraceTreeModel(stacktraceTreeModel);
}


public void setStacktraceTreeModel(StacktraceTreeModel stacktraceTreeModel) {
dataApplier = dataApplier(stacktraceTreeModel);
dataApplier.accept(jfrFlamegraphView);
Expand All @@ -205,7 +203,8 @@ private Consumer<FlamegraphView<Node>> dataApplier(StacktraceTreeModel stacktrac
frame -> frame.isRoot() ? "" : FormatToolkit.getHumanReadable(frame.actualNode.getFrame().getMethod(), false, false, false, false, true, false),
frame -> frame.isRoot() ? "" : frame.actualNode.getFrame().getMethod().getMethodName()
),
defaultFrameColorMode.colorMapperUsing(ColorMapper.ofObjectHashUsing(defaultColorPalette.colors())),
new DimmingFrameColorProvider(defaultFrameColorMode.colorMapperUsing(ColorMapper.ofObjectHashUsing(defaultColorPalette.colors()))),
FrameFontProvider.defaultFontProvider(),
frame -> {
if (frame.isRoot()) {
return "";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package io.github.bric3.fireplace.ui;

import io.github.bric3.fireplace.core.ui.Colors;
import io.github.bric3.fireplace.core.ui.DarkLightColor;

import javax.swing.*;
import java.awt.*;

public class GlassJPanel extends JPanel {
private static final Color TRANSLUCENT_BACKGROUND = new DarkLightColor(
Colors.translucent_white_D0,
Colors.translucent_black_80
);

public GlassJPanel() {
this(new GridBagLayout());
}
Expand All @@ -17,7 +23,7 @@ public GlassJPanel(LayoutManager layout) {
@Override
protected void paintComponent(Graphics g) {
var g2 = (Graphics2D) g;
g2.setColor(Colors.isDarkMode() ? Colors.translucent_black_80 : Colors.translucent_white_D0);
g2.setColor(TRANSLUCENT_BACKGROUND);
g2.fillRect(0, 0, getWidth(), getHeight());
}
}
Loading

0 comments on commit 27d3d22

Please sign in to comment.