8226964: [Yaru] GTK L&F: There is no difference between menu selected and de-selected
authorpbansal
Sat, 03 Aug 2019 13:53:19 +0530
changeset 57648 2b38409a336e
parent 57647 bcc706c719d7
child 57649 b2fde6701654
8226964: [Yaru] GTK L&F: There is no difference between menu selected and de-selected Reviewed-by: prr, kcr
src/java.desktop/share/classes/com/sun/java/swing/plaf/gtk/GTKPainter.java
src/java.desktop/share/classes/com/sun/java/swing/plaf/gtk/GTKStyle.java
--- a/src/java.desktop/share/classes/com/sun/java/swing/plaf/gtk/GTKPainter.java	Thu Jul 25 12:15:27 2019 +0200
+++ b/src/java.desktop/share/classes/com/sun/java/swing/plaf/gtk/GTKPainter.java	Sat Aug 03 13:53:19 2019 +0530
@@ -535,6 +535,34 @@
         }
     }
 
+    private int getBrightness(Color c) {
+        return Math.max(c.getRed(), Math.max(c.getGreen(), c.getBlue()));
+    }
+
+    private int getMaxColorDiff(Color c1, Color c2) {
+        return Math.max(Math.abs(c1.getRed() - c2.getRed()),
+                Math.max(Math.abs(c1.getGreen() - c2.getGreen()),
+                        Math.abs(c1.getBlue() - c2.getBlue())));
+    }
+
+    private int scaleColorComponent(int color, double scaleFactor) {
+        return (int)(color + color * scaleFactor);
+    }
+    private Color deriveColor(Color originalColor, int originalBrightness,
+                              int targetBrightness) {
+        int r, g, b;
+        if (originalBrightness == 0) {
+            r = g = b = targetBrightness;
+        } else {
+            double scaleFactor = (targetBrightness - originalBrightness)
+                    / originalBrightness    ;
+            r = scaleColorComponent(originalColor.getRed(), scaleFactor);
+            g = scaleColorComponent(originalColor.getGreen(), scaleFactor);
+            b = scaleColorComponent(originalColor.getBlue(), scaleFactor);
+        }
+        return new Color(r, g, b);
+    }
+
     //
     // MENU
     //
@@ -551,6 +579,57 @@
         int gtkState = GTKLookAndFeel.synthStateToGTKState(
                 context.getRegion(), context.getComponentState());
         if (gtkState == SynthConstants.MOUSE_OVER) {
+            if (GTKLookAndFeel.is3() && context.getRegion() == Region.MENU) {
+                GTKStyle style = (GTKStyle)context.getStyle();
+                Color highlightColor = style.getGTKColor(
+                        GTKEngine.WidgetType.MENU_ITEM.ordinal(),
+                        gtkState, ColorType.BACKGROUND.getID());
+                Color backgroundColor = style.getGTKColor(
+                        GTKEngine.WidgetType.MENU_BAR.ordinal(),
+                        SynthConstants.ENABLED, ColorType.BACKGROUND.getID());
+
+                int minBrightness = 0, maxBrightness = 255;
+                int minBrightnessDifference = 100;
+                int actualBrightnessDifference =
+                        getMaxColorDiff(highlightColor, backgroundColor);
+                if (actualBrightnessDifference < minBrightnessDifference) {
+                    int highlightBrightness =
+                            getBrightness(highlightColor);
+                    int backgroundBrightness =
+                            getBrightness(backgroundColor);
+                    int originalHighlightBrightness =
+                            highlightBrightness;
+                    if (highlightBrightness >= backgroundBrightness) {
+                        if (backgroundBrightness + minBrightnessDifference <=
+                                maxBrightness) {
+                            highlightBrightness =
+                                    backgroundBrightness +
+                                            minBrightnessDifference;
+                        } else {
+                            highlightBrightness =
+                                    backgroundBrightness -
+                                            minBrightnessDifference;
+                        }
+                    } else {
+                        if (backgroundBrightness - minBrightnessDifference >=
+                                minBrightness) {
+                            highlightBrightness =
+                                    backgroundBrightness -
+                                            minBrightnessDifference;
+                        } else {
+                            highlightBrightness =
+                                    backgroundBrightness +
+                                            minBrightnessDifference;
+                        }
+                    }
+
+                    g.setColor(deriveColor(highlightColor,
+                            originalHighlightBrightness,
+                            highlightBrightness));
+                    g.fillRect(x, y, w, h);
+                    return;
+                }
+            }
             Region id = Region.MENU_ITEM;
             synchronized (UNIXToolkit.GTK_LOCK) {
                 if (! ENGINE.paintCachedImage(g, x, y, w, h, id)) {
--- a/src/java.desktop/share/classes/com/sun/java/swing/plaf/gtk/GTKStyle.java	Thu Jul 25 12:15:27 2019 +0200
+++ b/src/java.desktop/share/classes/com/sun/java/swing/plaf/gtk/GTKStyle.java	Sat Aug 03 13:53:19 2019 +0530
@@ -205,6 +205,14 @@
         return getGTKColor(null, state, type);
     }
 
+    Color getGTKColor(int widgetType, int state, int colorType) {
+        synchronized (sun.awt.UNIXToolkit.GTK_LOCK) {
+            int rgb = nativeGetColorForState(widgetType, state,
+                    colorType);
+            return new ColorUIResource(rgb);
+        }
+    }
+
     /**
      * Returns the color for the specified state.
      *