Skip to content

Commit d72aa6e

Browse files
pporvatovintellij-monorepo-bot
authored andcommitted
IDEA-286480 CheckBox baseline is incorrect on JBR17
- Fixed baseline GitOrigin-RevId: b1df5d09c9e4aa0dc109c5fe68a01347a73c59e4
1 parent 0c9c9f7 commit d72aa6e

File tree

5 files changed

+68
-10
lines changed

5 files changed

+68
-10
lines changed

platform/platform-impl/src/com/intellij/ide/ui/laf/darcula/ui/AbstractButtonLayout.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
package com.intellij.ide.ui.laf.darcula.ui;
33

44
import com.intellij.util.ui.JBInsets;
5+
import com.intellij.util.ui.UIUtil;
56
import com.intellij.util.ui.UIUtilities;
7+
import org.jetbrains.annotations.NotNull;
68

79
import javax.swing.*;
810
import javax.swing.plaf.basic.BasicHTML;
@@ -25,7 +27,7 @@ class AbstractButtonLayout {
2527
* CheckBox.borderInsets (RadioButton as well) property ignores top/bottom offsets while vertical align.
2628
* Normally should be always true and removed later
2729
*/
28-
AbstractButtonLayout(AbstractButton button, Dimension size, boolean removeInsetsBeforeLayout, Icon defaultIcon) {
30+
AbstractButtonLayout(@NotNull AbstractButton button, @NotNull Dimension size, boolean removeInsetsBeforeLayout, Icon defaultIcon) {
2931
this.button = button;
3032
this.size = size;
3133
this.removeInsetsBeforeLayout = removeInsetsBeforeLayout;
@@ -53,7 +55,7 @@ public void paint(Graphics g, Color disabledTextColor, int mnemonicIndex) {
5355
drawText(g, disabledTextColor, mnemonicIndex);
5456
}
5557

56-
public Dimension getPreferredSize() {
58+
public @NotNull Dimension getPreferredSize() {
5759
Insets insets = button.getInsets();
5860
Rectangle iconRectResult;
5961
// todo a strange logic that should be revised together with removeInsetsBeforeLayout
@@ -75,6 +77,13 @@ public Dimension getPreferredSize() {
7577
return new Dimension(rect.width, rect.height);
7678
}
7779

80+
public int getBaseline() {
81+
if (button.getText() == null) {
82+
return -1;
83+
}
84+
return UIUtil.getBaseline(button, textRect.y, fontMetrics.getAscent(), textRect.width, textRect.height);
85+
}
86+
7887
private void drawText(Graphics g, Color disabledTextColor, int mnemonicIndex) {
7988
if (text != null) {
8089
View v = (View)button.getClientProperty(BasicHTML.propertyKey);

platform/platform-impl/src/com/intellij/ide/ui/laf/darcula/ui/DarculaCheckBoxUI.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.intellij.util.ui.LafIconLookup;
1010
import com.intellij.util.ui.MacUIUtil;
1111
import com.intellij.util.ui.ThreeStateCheckBox;
12+
import org.jetbrains.annotations.NotNull;
1213

1314
import javax.swing.*;
1415
import javax.swing.plaf.ComponentUI;
@@ -73,12 +74,18 @@ protected int textIconGap() {
7374
public void paint(Graphics g2d, JComponent c) {
7475
Graphics2D g = (Graphics2D)g2d;
7576
AbstractButton button = (AbstractButton)c;
76-
AbstractButtonLayout layout = new AbstractButtonLayout(button, button.getSize(), removeInsetsBeforeLayout(button), getDefaultIcon());
77+
AbstractButtonLayout layout = createLayout(button, button.getSize());
7778

7879
layout.paint(g, getDisabledTextColor(), getMnemonicIndex(button));
7980
drawCheckIcon(c, g, button, layout.iconRect, button.isSelected(), button.isEnabled());
8081
}
8182

83+
@Override
84+
public int getBaseline(JComponent c, int width, int height) {
85+
AbstractButtonLayout layout = createLayout(c, new Dimension(width, height));
86+
return layout.getBaseline();
87+
}
88+
8289
protected boolean removeInsetsBeforeLayout(AbstractButton b) {
8390
return !(b.getBorder() instanceof DarculaCheckBoxBorder);
8491
}
@@ -119,9 +126,7 @@ protected int getMnemonicIndex(AbstractButton b) {
119126

120127
@Override
121128
public Dimension getPreferredSize(JComponent c) {
122-
AbstractButton button = (AbstractButton)c;
123-
AbstractButtonLayout layout = new AbstractButtonLayout(button, new Dimension(Short.MAX_VALUE, Short.MAX_VALUE),
124-
removeInsetsBeforeLayout(button), getDefaultIcon());
129+
AbstractButtonLayout layout = createLayout(c, new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
125130
return layout.getPreferredSize();
126131
}
127132

@@ -139,4 +144,9 @@ protected boolean isIndeterminate(AbstractButton checkBox) {
139144
return "indeterminate".equals(checkBox.getClientProperty("JButton.selectedState")) ||
140145
checkBox instanceof ThreeStateCheckBox && ((ThreeStateCheckBox)checkBox).getState() == ThreeStateCheckBox.State.DONT_CARE;
141146
}
147+
148+
private @NotNull AbstractButtonLayout createLayout(JComponent c, Dimension size) {
149+
AbstractButton button = (AbstractButton)c;
150+
return new AbstractButtonLayout(button, size, removeInsetsBeforeLayout(button), getDefaultIcon());
151+
}
142152
}

platform/platform-impl/src/com/intellij/ide/ui/laf/darcula/ui/DarculaRadioButtonUI.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.intellij.ui.scale.JBUIScale;
66
import com.intellij.util.ui.EmptyIcon;
77
import com.intellij.util.ui.LafIconLookup;
8+
import org.jetbrains.annotations.NotNull;
89

910
import javax.swing.*;
1011
import javax.swing.plaf.ComponentUI;
@@ -67,7 +68,7 @@ private static void updateTextPosition(AbstractButton b) {
6768
public void paint(Graphics g2d, JComponent c) {
6869
Graphics2D g = (Graphics2D)g2d;
6970
AbstractButton button = (AbstractButton)c;
70-
AbstractButtonLayout layout = new AbstractButtonLayout(button, button.getSize(), removeInsetsBeforeLayout(button), getDefaultIcon());
71+
AbstractButtonLayout layout = createLayout(button, button.getSize());
7172

7273
layout.paint(g, getDisabledTextColor(), getMnemonicIndex(button));
7374
paintFocus(button, g, layout.textRect);
@@ -90,15 +91,19 @@ private void paintFocus(AbstractButton b, Graphics2D g, Rectangle textRect) {
9091
}
9192
}
9293

94+
@Override
95+
public int getBaseline(JComponent c, int width, int height) {
96+
AbstractButtonLayout layout = createLayout(c, new Dimension(width, height));
97+
return layout.getBaseline();
98+
}
99+
93100
protected int getMnemonicIndex(AbstractButton b) {
94101
return DarculaLaf.isAltPressed() ? b.getDisplayedMnemonicIndex() : -1;
95102
}
96103

97104
@Override
98105
public Dimension getPreferredSize(JComponent c) {
99-
AbstractButton button = (AbstractButton)c;
100-
AbstractButtonLayout layout = new AbstractButtonLayout(button, new Dimension(Short.MAX_VALUE, Short.MAX_VALUE),
101-
removeInsetsBeforeLayout(button), getDefaultIcon());
106+
AbstractButtonLayout layout = createLayout(c, new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
102107
return layout.getPreferredSize();
103108
}
104109

@@ -114,4 +119,9 @@ protected void paintFocus(Graphics g, Rectangle t, Dimension d) {}
114119
public Icon getDefaultIcon() {
115120
return DEFAULT_ICON;
116121
}
122+
123+
private @NotNull AbstractButtonLayout createLayout(JComponent c, Dimension size) {
124+
AbstractButton button = (AbstractButton)c;
125+
return new AbstractButtonLayout(button, size, removeInsetsBeforeLayout(button), getDefaultIcon());
126+
}
117127
}

platform/platform-impl/src/com/intellij/internal/ui/uiDslTestAction/CheckBoxRadioButtonPanel.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ internal class CheckBoxRadioButtonPanel {
8282
}
8383
cell(panel)
8484
}
85+
86+
buttonsGroup {
87+
row {
88+
checkBox("Base line check")
89+
comment("Some comment")
90+
}
91+
row {
92+
radioButton("Base line check")
93+
comment("Some comment")
94+
}
95+
}
8596
}
8697
}
8798

platform/util/ui/src/com/intellij/util/ui/UIUtil.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import javax.swing.plaf.FontUIResource;
4444
import javax.swing.plaf.UIResource;
4545
import javax.swing.plaf.basic.BasicComboBoxUI;
46+
import javax.swing.plaf.basic.BasicHTML;
4647
import javax.swing.plaf.basic.BasicRadioButtonUI;
4748
import javax.swing.plaf.basic.ComboPopup;
4849
import javax.swing.text.*;
@@ -147,6 +148,23 @@ public static int getMultiClickInterval() {
147148
return 500;
148149
}
149150

151+
/**
152+
* A public method from BasicHTML
153+
*
154+
* @see BasicHTML#getBaseline(JComponent, int, int, int, int)
155+
*/
156+
public static int getBaseline(@NotNull JComponent c, int y, int ascent, int w, int h) {
157+
View view = (View)c.getClientProperty(BasicHTML.propertyKey);
158+
if (view != null) {
159+
int baseline = BasicHTML.getHTMLBaseline(view, w, h);
160+
if (baseline < 0) {
161+
return baseline;
162+
}
163+
return y + baseline;
164+
}
165+
return y + ascent;
166+
}
167+
150168
private static final NotNullLazyValue<Boolean> X_RENDER_ACTIVE = NotNullLazyValue.atomicLazy(() -> {
151169
if (!SystemInfoRt.isXWindow) {
152170
return false;

0 commit comments

Comments
 (0)