diff --git a/platform/platform-api/src/com/intellij/ui/components/DefaultScrollBarUI.java b/platform/platform-api/src/com/intellij/ui/components/DefaultScrollBarUI.java index 9b8521c0541b..04ea33cf23c5 100644 --- a/platform/platform-api/src/com/intellij/ui/components/DefaultScrollBarUI.java +++ b/platform/platform-api/src/com/intellij/ui/components/DefaultScrollBarUI.java @@ -17,15 +17,11 @@ package com.intellij.ui.components; import com.intellij.openapi.util.Key; import com.intellij.openapi.util.registry.Registry; -import com.intellij.ui.ColorUtil; -import com.intellij.ui.JBColor; import com.intellij.ui.components.JBScrollPane.Alignment; -import com.intellij.util.NotNullProducer; import com.intellij.util.ui.JBInsets; import com.intellij.util.ui.JBUI; import com.intellij.util.ui.RegionPainter; import com.intellij.util.ui.UIUtil; -import org.jetbrains.annotations.NotNull; import javax.swing.*; import javax.swing.event.ChangeEvent; @@ -36,7 +32,6 @@ import java.awt.event.*; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; -import static com.intellij.ui.components.JBScrollPane.BRIGHTNESS_FROM_VIEW; import static java.awt.Adjustable.VERTICAL; /** @@ -116,7 +111,7 @@ class DefaultScrollBarUI extends ScrollBarUI { } void paintTrack(Graphics2D g, int x, int y, int width, int height, JComponent c) { - RegionPainter p = isDark(c) ? JBScrollPane.TRACK_DARK_PAINTER : JBScrollPane.TRACK_PAINTER; + RegionPainter p = ScrollColorProducer.isDark(c) ? JBScrollPane.TRACK_DARK_PAINTER : JBScrollPane.TRACK_PAINTER; if (!isTrackExpandable() && Registry.is("ide.scroll.background.wide")) { p.paint(g, x, y, width, height, myTrackAnimator.myValue); return; // temporary registry key for designer @@ -125,7 +120,7 @@ class DefaultScrollBarUI extends ScrollBarUI { } void paintThumb(Graphics2D g, int x, int y, int width, int height, JComponent c) { - RegionPainter p = isDark(c) ? JBScrollPane.THUMB_DARK_PAINTER : JBScrollPane.THUMB_PAINTER; + RegionPainter p = ScrollColorProducer.isDark(c) ? JBScrollPane.THUMB_DARK_PAINTER : JBScrollPane.THUMB_PAINTER; paint(p, g, x, y, width, height, c, myThumbAnimator.myValue, Registry.is("ide.scroll.thumb.small.if.opaque")); } @@ -192,8 +187,8 @@ class DefaultScrollBarUI extends ScrollBarUI { @Override public void installUI(JComponent c) { myScrollBar = (JScrollBar)c; - myScrollBar.setBackground(new JBColor(new ColorProducer(c, 0xF5F5F5, 0x3C3F41))); - myScrollBar.setForeground(new JBColor(new ColorProducer(c, 0xE6E6E6, 0x3C3F41))); + ScrollColorProducer.setBackground(c); + ScrollColorProducer.setForeground(c); myScrollBar.setFocusable(false); myScrollBar.addMouseListener(myListener); myScrollBar.addMouseMotionListener(myListener); @@ -232,15 +227,10 @@ class DefaultScrollBarUI extends ScrollBarUI { Alignment alignment = Alignment.get(c); if (alignment != null && g instanceof Graphics2D) { Container parent = c.getParent(); - Color background = null; - if (c.isOpaque()) { - background = Registry.is("ide.scroll.background.auto") && parent instanceof JScrollPane - ? JBScrollPane.getViewBackground((JScrollPane)parent) - : c.getBackground(); - if (background != null) { - g.setColor(background); - g.fillRect(0, 0, c.getWidth(), c.getHeight()); - } + Color background = !c.isOpaque() ? null : c.getBackground(); + if (background != null) { + g.setColor(background); + g.fillRect(0, 0, c.getWidth(), c.getHeight()); } Rectangle bounds = new Rectangle(c.getWidth(), c.getHeight()); JBInsets.removeFrom(bounds, c.getInsets()); @@ -631,41 +621,4 @@ class DefaultScrollBarUI extends ScrollBarUI { } } } - - static boolean isDark(JComponent c) { - if (c instanceof JScrollBar) { - Container parent = c.getParent(); - if (parent instanceof JScrollPane) { - JScrollPane pane = (JScrollPane)parent; - Object property = c.getClientProperty(BRIGHTNESS_FROM_VIEW); - if (property == null) { - property = pane.getClientProperty(BRIGHTNESS_FROM_VIEW); - } - if (property instanceof Boolean && (Boolean)property) { - Color color = JBScrollPane.getViewBackground(pane); - if (color != null) return ColorUtil.isDark(color); - } - } - } - return UIUtil.isUnderDarcula(); - } - - private static final class ColorProducer implements NotNullProducer { - private final JComponent myComponent; - private final Color myBrightColor; - private final Color myDarkColor; - - @SuppressWarnings("UseJBColor") - private ColorProducer(JComponent component, int bright, int dark) { - myComponent = component; - myBrightColor = new Color(bright); - myDarkColor = new Color(dark); - } - - @NotNull - @Override - public Color produce() { - return isDark(myComponent) ? myDarkColor : myBrightColor; - } - } } diff --git a/platform/platform-api/src/com/intellij/ui/components/JBScrollPane.java b/platform/platform-api/src/com/intellij/ui/components/JBScrollPane.java index a005adaef583..26e7eee544b9 100644 --- a/platform/platform-api/src/com/intellij/ui/components/JBScrollPane.java +++ b/platform/platform-api/src/com/intellij/ui/components/JBScrollPane.java @@ -361,14 +361,17 @@ public class JBScrollPane extends JScrollPane { public Corner(String pos) { myPos = pos; + ScrollColorProducer.setBackground(this); + ScrollColorProducer.setForeground(this); } @Override protected void paintComponent(Graphics g) { - g.setColor(ButtonlessScrollBarUI.getTrackBackgroundDefault()); + g.setColor(getBackground()); g.fillRect(0, 0, getWidth(), getHeight()); - g.setColor(ButtonlessScrollBarUI.getTrackBorderColorDefault()); + if (SystemInfo.isMac || !Registry.is("ide.scroll.track.border.paint")) return; + g.setColor(getForeground()); int x2 = getWidth() - 1; int y2 = getHeight() - 1; diff --git a/platform/platform-api/src/com/intellij/ui/components/MacScrollBarUI.java b/platform/platform-api/src/com/intellij/ui/components/MacScrollBarUI.java index af453d1df1b2..2a72c70ffa47 100644 --- a/platform/platform-api/src/com/intellij/ui/components/MacScrollBarUI.java +++ b/platform/platform-api/src/com/intellij/ui/components/MacScrollBarUI.java @@ -100,7 +100,7 @@ final class MacScrollBarUI extends DefaultScrollBarUI { @Override void paintThumb(Graphics2D g, int x, int y, int width, int height, JComponent c) { - RegionPainter p = isDark(c) ? JBScrollPane.MAC_THUMB_DARK_PAINTER : JBScrollPane.MAC_THUMB_PAINTER; + RegionPainter p = ScrollColorProducer.isDark(c) ? JBScrollPane.MAC_THUMB_DARK_PAINTER : JBScrollPane.MAC_THUMB_PAINTER; if (c.isOpaque()) { paint(p, g, x, y, width, height, c, myThumbAnimator.myValue, true); } diff --git a/platform/platform-api/src/com/intellij/ui/components/ScrollColorProducer.java b/platform/platform-api/src/com/intellij/ui/components/ScrollColorProducer.java new file mode 100644 index 000000000000..9f2a0821f496 --- /dev/null +++ b/platform/platform-api/src/com/intellij/ui/components/ScrollColorProducer.java @@ -0,0 +1,89 @@ +/* + * Copyright 2000-2016 JetBrains s.r.o. + * + * 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 com.intellij.ui.components; + +import com.intellij.openapi.util.registry.Registry; +import com.intellij.ui.ColorUtil; +import com.intellij.ui.JBColor; +import com.intellij.util.NotNullProducer; +import com.intellij.util.ui.UIUtil; +import org.jetbrains.annotations.NotNull; + +import java.awt.Color; +import java.awt.Component; +import java.awt.Container; +import javax.swing.JScrollPane; + +import static com.intellij.ui.components.JBScrollPane.BRIGHTNESS_FROM_VIEW; + +/** + * @author Sergey.Malenkov + */ +final class ScrollColorProducer implements NotNullProducer { + private final Component myComponent; + private final Color myBrightColor; + private final Color myDarkColor; + private final boolean isBackground; + // cached color values + private volatile Color myOriginal; + private volatile Color myModified; + + @SuppressWarnings("UseJBColor") + private ScrollColorProducer(Component component, int bright, int dark, boolean background) { + myComponent = component; + myBrightColor = new Color(bright); + myDarkColor = new Color(dark); + isBackground = background; + } + + @NotNull + @Override + public Color produce() { + Container parent = myComponent.getParent(); + if (isBackground && parent instanceof JScrollPane && Registry.is("ide.scroll.background.auto")) { + Color background = JBScrollPane.getViewBackground((JScrollPane)parent); + if (background != null) { + if (!background.equals(myOriginal)) { + myModified = ColorUtil.shift(background, ColorUtil.isDark(background) ? 1.05 : 0.98); + myOriginal = background; + } + return myModified; + } + } + return isDark(myComponent) ? myDarkColor : myBrightColor; + } + + static void setForeground(Component component) { + component.setForeground(new JBColor(new ScrollColorProducer(component, 0xE6E6E6, 0x3C3F41, false))); + } + + static void setBackground(Component component) { + component.setBackground(new JBColor(new ScrollColorProducer(component, 0xF5F5F5, 0x3C3F41, true))); + } + + static boolean isDark(Component c) { + Container parent = c.getParent(); + if (parent instanceof JScrollPane) { + JScrollPane pane = (JScrollPane)parent; + Object property = pane.getClientProperty(BRIGHTNESS_FROM_VIEW); + if (property instanceof Boolean && (Boolean)property) { + Color color = JBScrollPane.getViewBackground(pane); + if (color != null) return ColorUtil.isDark(color); + } + } + return UIUtil.isUnderDarcula(); + } +}