Intellij 自适应颜色方案
Intellij 自适应颜色方案
本文描述了如何使 IntelliJ 中的用户界面组件的颜色适应 IDE 主题的变化。
创建一个 UI 组件
Section titled “创建一个 UI 组件”假设我们现在有一个像这样的组件
public class UIContainer extends JBPanel<UIContainer> {
private static final EditorColorsManager editorColorsManager = EditorColorsManager.getInstance(); private static EditorColorsScheme scheme = editorColorsManager.getGlobalScheme(); private static final ColorKey notificationColor = ColorKey.createColorKey("NOTIFICATION_BACKGROUND");
public MessageComponent() { // TODO init some actions...
Color color = scheme.getColor(notificationColor); setBackground(color) }}
我们为 UIContainer
设置了一个背景颜色,它来自当前 ColorsScheme 的 NOTIFICATION_BACKGROUND
,
但是很明显,在这种情况下,当我们切换主题时,UIContainer
的颜色不会与主题的颜色一起切换。
UIContainer
的颜色不会在切换主题时与主题的颜色一起切换。然而,在大多数情况下,
这可能会导致 UI 具有相似的颜色和字体,因此显示的内容不可见。
那么,我们如何让 UIContainer
的背景颜色随着主题的变化而自动变化呢?
适应 IDE 主题
Section titled “适应 IDE 主题”让 UIContainer
实现 Disposable 和 EditorColorsListener
public class UIContainer extends JBPanel<UIContainer> implements Disposable, EditorColorsListener { {... rest of code}}
添加消息事件
Section titled “添加消息事件”public class UIContainer extends JBPanel<UIContainer> implements Disposable, EditorColorsListener {
private static final EditorColorsManager editorColorsManager = EditorColorsManager.getInstance(); private static EditorColorsScheme scheme = editorColorsManager.getGlobalScheme(); private static final ColorKey notificationColor = ColorKey.createColorKey("NOTIFICATION_BACKGROUND");
public MessageComponent() { // TODO init some actions...
Color color = scheme.getColor(notificationColor); setBackground(color)
MessageBusConnection connection = ApplicationManager.getApplication().getMessageBus().connect(this); connection.subscribe(EditorColorsManager.TOPIC, this); }}
实现 globalSchemeChange
Section titled “实现 globalSchemeChange”当主题改变时,globalSchemeChange 方法将被调用。
currentColorsScheme 将是选定的主题,因此我们从 currentColorsScheme 中重新获得 notificationColor
并重置组件的颜色。
public class UIContainer extends JBPanel<UIContainer> implements Disposable, EditorColorsListener {
{... rest of code}
@Override public void globalSchemeChange(@Nullable EditorColorsScheme currentColorsScheme) { Color color = currentColorsScheme == null ? scheme.getColor(notificationColor) : currentColorsScheme.getColor(notificationColor); setBackground(color); revalidate(); repaint(); }}
实现 dispose
Section titled “实现 dispose”public class UIContainer extends JBPanel<UIContainer> implements Disposable, EditorColorsListener {
{... rest of code}
@Override public void dispose() { Disposer.dispose(this); }}
Finally Code
Section titled “Finally Code”那么,最终的代码如下
public class UIContainer extends JBPanel<UIContainer> implements Disposable, EditorColorsListener {
private static final EditorColorsManager editorColorsManager = EditorColorsManager.getInstance(); private static EditorColorsScheme scheme = editorColorsManager.getGlobalScheme(); private static final ColorKey notificationColor = ColorKey.createColorKey("NOTIFICATION_BACKGROUND");
public MessageComponent() { // TODO init some actions...
Color color = scheme.getColor(notificationColor); setBackground(color)
MessageBusConnection connection = ApplicationManager.getApplication().getMessageBus().connect(this); connection.subscribe(EditorColorsManager.TOPIC, this); }
@Override public void globalSchemeChange(@Nullable EditorColorsScheme currentColorsScheme) { if (currentColorsScheme == null) { return; } Color color = scheme.getColor(notificationColor); setBackground(color); revalidate(); repaint(); }
@Override public void dispose() { Disposer.dispose(this); }}
现在当你切换主题时,组件的颜色会自动与主题的颜色一起变化。希望这对你有帮助。