Skip to content

Commit

Permalink
Supporting other SVG Color Patchers
Browse files Browse the repository at this point in the history
  • Loading branch information
Unthrottled committed Apr 15, 2021
1 parent 4251262 commit 22566a8
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 10 deletions.
43 changes: 43 additions & 0 deletions src/main/java/com/mallowigi/icons/SvgLoaderHacker.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.mallowigi.icons

import com.intellij.util.SVGLoader
import java.net.URL
import java.util.Optional

typealias PatcherProvider = SVGLoader.SvgElementColorPatcherProvider

object SvgLoaderHacker {

private lateinit var collectedPatcherProvider: PatcherProvider

@JvmStatic
fun collectOtherPatcher(): PatcherProvider =
extractPatcher()
.filter { it is PatcherProvider }
.filter { it !is TintedIconsComponent.TintedColorPatcher }
.map {
val otherPatcher = it as PatcherProvider
collectedPatcherProvider = otherPatcher
otherPatcher
}
.orElseGet { useFallBackPatcher() }

private fun extractPatcher() = Optional.ofNullable(
SVGLoader::class.java.declaredFields
.firstOrNull { it.name == "ourColorPatcher" }
)
.map { ourColorPatcherField ->
ourColorPatcherField.isAccessible = true
ourColorPatcherField.get(null)
}


private val noOpPatcherProvider =
object : PatcherProvider {
override fun forURL(url: URL?): SVGLoader.SvgElementColorPatcher? = null
}

private fun useFallBackPatcher(): PatcherProvider =
if (this::collectedPatcherProvider.isInitialized) collectedPatcherProvider
else noOpPatcherProvider
}
28 changes: 18 additions & 10 deletions src/main/java/com/mallowigi/icons/TintedIconsComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
*/
public final class TintedIconsComponent implements DynamicPluginListener, AppLifecycleListener, DumbAware {
private static final PluginId PLUGIN_ID = PluginId.getId("com.mallowigi");
private TintedColorPatcher colorPatcher;
private final MessageBusConnection connect;

public TintedIconsComponent() {
Expand Down Expand Up @@ -83,20 +82,17 @@ public void pluginUnloaded(@NotNull final IdeaPluginDescriptor pluginDescriptor,
}

private void initComponent() {
colorPatcher = new TintedColorPatcher();
SVGLoader.setColorPatcherProvider(colorPatcher);
SVGLoader.setColorPatcherProvider(new TintedColorPatcher(SvgLoaderHacker.collectOtherPatcher()));

// Listen for changes on the settings
connect.subscribe(LafManagerListener.TOPIC, source -> {
SVGLoader.setColorPatcherProvider(null);
SVGLoader.setColorPatcherProvider(colorPatcher);
SVGLoader.setColorPatcherProvider(new TintedColorPatcher(SvgLoaderHacker.collectOtherPatcher()));

TintedColorPatcher.refreshThemeColor(getThemedColor());
TintedColorPatcher.refreshAccentColor(getTintedColor());
});
connect.subscribe(AtomConfigNotifier.TOPIC, atomFileIconsConfig -> {
SVGLoader.setColorPatcherProvider(null);
SVGLoader.setColorPatcherProvider(colorPatcher);
SVGLoader.setColorPatcherProvider(new TintedColorPatcher(SvgLoaderHacker.collectOtherPatcher()));

TintedColorPatcher.refreshThemeColor(getThemedColor());
TintedColorPatcher.refreshAccentColor(getTintedColor());
Expand All @@ -115,12 +111,14 @@ private static ColorUIResource getTintedColor() {
return new ColorUIResource(ColorUtil.fromHex(AtomFileIconsConfig.getInstance().getCurrentAccentColor()));
}

private static final class TintedColorPatcher implements SVGLoader.SvgElementColorPatcherProvider {
protected static final class TintedColorPatcher implements SVGLoader.SvgElementColorPatcherProvider {
@NonNls
private static ColorUIResource themedColor = getThemedColor();
private static ColorUIResource tintedColor = getTintedColor();
private final SVGLoader.SvgElementColorPatcherProvider otherPatcherProvider;

private TintedColorPatcher() {
private TintedColorPatcher(SVGLoader.SvgElementColorPatcherProvider otherPatcherProvider) {
this.otherPatcherProvider = otherPatcherProvider;
refreshColors();
}

Expand All @@ -139,10 +137,20 @@ private static void refreshColors() {

@NotNull
@Override
public SVGLoader.SvgElementColorPatcher forURL(@Nullable final URL url) {
public SVGLoader.SvgElementColorPatcher forPath(@Nullable String path) {
return createPatcher(otherPatcherProvider.forPath(path));
}

@NotNull
private SVGLoader.SvgElementColorPatcher createPatcher(
final @Nullable SVGLoader.SvgElementColorPatcher otherPatcher
) {
return new SVGLoader.SvgElementColorPatcher() {
@Override
public void patchColors(@NonNls final Element svg) {
if(otherPatcher != null) {
otherPatcher.patchColors(svg);
}
@NonNls final String tint = svg.getAttribute("tint");
@NonNls final String themed = svg.getAttribute("themed");
final String hexColor = getColorHex(themedColor);
Expand Down

0 comments on commit 22566a8

Please sign in to comment.