-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from bric3/new-color-provider
New color provider
- Loading branch information
Showing
8 changed files
with
318 additions
and
41 deletions.
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
fireplace-app/src/main/java/io/github/bric3/fireplace/DimmingFrameColorProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.