Last active
May 11, 2020 12:35
-
-
Save markusmo3/ee46e5fe81d4dacea7110134f4ca953f to your computer and use it in GitHub Desktop.
An IntelliJ IDEA Live Plugin to change the window icon to something more useful. (LivePlugin: https://github.com/dkandalov/live-plugin)
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
import com.intellij.ide.plugins.PluginUtil | |
import java.awt.* | |
import javax.imageio.* | |
import com.intellij.openapi.project.* | |
import com.intellij.openapi.wm.* | |
import com.intellij.openapi.wm.impl.* | |
import sun.misc.* | |
import java.awt.image.BufferedImage | |
import static liveplugin.PluginUtil.invokeLaterOnEDT | |
import static liveplugin.PluginUtil.registerProjectListener | |
invokeLaterOnEDT { | |
IdeFrameImpl.frames.each { Frame frame -> | |
if (frame instanceof IdeFrameImpl) { | |
def project = frame.getProject() | |
println("frame found $frame $project") | |
setIdeaWindowIcon(project, frame, getIdentifyingLetters(project)) | |
} | |
} | |
registerProjectListener("icon-changer-live", new ProjectManagerListener() { | |
@Override | |
void projectOpened(Project project) { | |
invokeLaterOnEDT { | |
setIdeaWindowIcon(project, getIdentifyingLetters(project)) | |
} | |
} | |
}) | |
} | |
static def setIdeaWindowIcon(Project project, IdeFrameImpl frame = findFrameForProject(project), | |
String overlayText) { | |
if (frame == null) { | |
return | |
} | |
def moreThanTwoChars = overlayText.length() > 2 | |
def img = toBufferedImage(frame.getIconImage()) | |
def g = img.createGraphics() | |
g.color = Color.BLACK | |
g.fillRect(5, 5, 20, 16) | |
g.color = Color.WHITE | |
g.font = new Font("Consolas", Font.BOLD, moreThanTwoChars ? 13 : 15) | |
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF) | |
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON) | |
g.drawString(overlayText, | |
moreThanTwoChars ? 5 : 6, | |
moreThanTwoChars ? 14 : 16) | |
frame.setIconImage(img) | |
} | |
static String getIdentifyingLetters(Project project, int i = 0) { | |
if (project == null) { | |
return "???" | |
} | |
def prjName = project.name | |
if (i >= Constants.identLettersSplitRegexes.length) { | |
return prjName.substring(0, Math.min(3, prjName.length())) | |
} | |
def regex = Constants.identLettersSplitRegexes[i] | |
def split = prjName.split(regex) - "" | |
if (split.length >= 3) { | |
return split[0].substring(0, 1) + split[1].substring(0, 1) + split[2].substring(0, 1) | |
} else if (split.length == 2) { | |
return split[0].substring(0, 1) + split[1].substring(0, 1) | |
// } else if (split.length == 1) { | |
// return split[0].substring(0, 1) | |
} else { | |
return getIdentifyingLetters(project, i + 1) | |
} | |
} | |
static IdeFrame findFrameForProject(Project project) { | |
for (Frame frame : IdeFrameImpl.frames) { | |
if (frame instanceof IdeFrame && frame.getProject() == project) { | |
return frame | |
} | |
} | |
return null | |
} | |
/** | |
* Converts a given Image into a BufferedImage | |
* | |
* @param img The Image to be converted | |
* @return The converted BufferedImage | |
*/ | |
public static BufferedImage toBufferedImage(Image img) { | |
if (img instanceof BufferedImage) { | |
return (BufferedImage) img; | |
} | |
// Create a buffered image with transparency | |
BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB); | |
// Draw the image on to the buffered image | |
Graphics2D bGr = bimage.createGraphics(); | |
bGr.drawImage(img, 0, 0, null); | |
bGr.dispose(); | |
// Return the buffered image | |
return bimage; | |
} | |
class Constants { | |
static String ideaIconWithBar_base64_28px = "iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAYAAAByDd+UAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAASdEVYdFNvZnR3YXJlAEdyZWVuc2hvdF5VCAUAAANSSURBVEhLtVZBaxNBFP5md5ukSdqKjUhLpV4FsRepHix4EEEPgt4sHrwoHoqIgqAULx4EqT/BPyFaFA+ilyJFLHjQQotCtQ1FbZs0abPZ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9_-]+", "[0-9_-]+"] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Showcase: