Make option button wider than simple button

This commit is contained in:
Konstantin Kolosovsky
2018-01-30 01:13:44 +03:00
parent 0bd3a9a573
commit 939f6e5ffd
3 changed files with 67 additions and 8 deletions
@@ -665,7 +665,7 @@ public abstract class DialogWrapper {
@NotNull
private JPanel createButtonsPanel(@NotNull List<JButton> buttons) {
int hgap = SystemInfo.isMacOSLeopard ? UIUtil.isUnderIntelliJLaF() ? 8 : 0 : 5;
JPanel buttonsPanel = new NonOpaquePanel(new GridLayout(1, buttons.size(), hgap, 0));
JPanel buttonsPanel = new NonOpaquePanel(new DialogWrapperButtonLayout(buttons.size(), hgap));
for (final JButton button : buttons) {
buttonsPanel.add(button);
}
@@ -0,0 +1,63 @@
/*
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package com.intellij.openapi.ui
import com.intellij.util.ui.AbstractLayoutManager
import java.awt.Component
import java.awt.Container
import java.awt.Dimension
import javax.swing.JComponent
/*
Based on [java.awt.GridLayout], but with support for extra width for option buttons.
*/
class DialogWrapperButtonLayout(val size: Int, val hgap: Int) : AbstractLayoutManager() {
override fun layoutContainer(parent: Container) {
synchronized(parent.treeLock) {
val insets = parent.insets
val totalExtraWidth = getTotalExtraWidth(parent)
val totalGapsWidth = (size - 1) * hgap
val widthWOInsets = parent.width - (insets.left + insets.right)
val widthOnComponent = (widthWOInsets - totalGapsWidth - totalExtraWidth) / size
val heightWOInsets = parent.height - (insets.top + insets.bottom)
var x = insets.left + (widthWOInsets - (widthOnComponent * size + totalGapsWidth + totalExtraWidth)) / 2
val y = insets.top
for (comp in parent.components) {
val w = widthOnComponent + getExtraWidth(comp)
comp.setBounds(x, y, w, heightWOInsets)
x += w + hgap
}
}
}
override fun preferredLayoutSize(parent: Container) = layoutSize(parent) { it.preferredSize }
override fun minimumLayoutSize(parent: Container) = layoutSize(parent) { it.minimumSize }
private fun layoutSize(parent: Container, sizeProvider: (Component) -> Dimension): Dimension {
synchronized(parent.treeLock) {
val insets = parent.insets
var w = 0
var h = 0
for (comp in parent.components) {
val d = sizeProvider(comp)
w = maxOf(w, d.width)
h = maxOf(h, d.height)
}
return Dimension(insets.left + insets.right + size * w + (size - 1) * hgap + getTotalExtraWidth(parent),
insets.top + insets.bottom + h)
}
}
private fun getTotalExtraWidth(parent: Container) = parent.components.sumBy { getExtraWidth(it) }
private fun getExtraWidth(comp: Component) = (comp as? JComponent)?.getClientProperty(EXTRA_WIDTH_KEY) as? Int ?: 0
companion object {
const val EXTRA_WIDTH_KEY = "ExtraWidth"
}
}
@@ -18,6 +18,7 @@ package com.intellij.ui.components;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.CustomShortcutSet;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.ui.DialogWrapperButtonLayout;
import com.intellij.openapi.ui.GraphicsConfig;
import com.intellij.openapi.ui.JBMenuItem;
import com.intellij.openapi.ui.JBPopupMenu;
@@ -59,6 +60,8 @@ public class JBOptionButton extends JButton implements Weighted {
applyOptions();
installShowPopupShortcut();
putClientProperty(DialogWrapperButtonLayout.EXTRA_WIDTH_KEY, myMoreRec.width);
}
@Override
@@ -66,13 +69,6 @@ public class JBOptionButton extends JButton implements Weighted {
return 0.5;
}
@Override
public Dimension getPreferredSize() {
final Dimension size = super.getPreferredSize();
size.width += myMoreRec.width;
return size;
}
@Override
public void doLayout() {
super.doLayout();