From 939f6e5ffd25600dbba6eada0abe83b20c25de93 Mon Sep 17 00:00:00 2001 From: Konstantin Kolosovsky Date: Mon, 8 Jan 2018 21:18:18 +0300 Subject: [PATCH] Make option button wider than simple button --- .../intellij/openapi/ui/DialogWrapper.java | 2 +- .../openapi/ui/DialogWrapperButtonLayout.kt | 63 +++++++++++++++++++ .../ui/components/JBOptionButton.java | 10 +-- 3 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 platform/platform-api/src/com/intellij/openapi/ui/DialogWrapperButtonLayout.kt diff --git a/platform/platform-api/src/com/intellij/openapi/ui/DialogWrapper.java b/platform/platform-api/src/com/intellij/openapi/ui/DialogWrapper.java index cd3363fd1bef..2d56fc3aada9 100644 --- a/platform/platform-api/src/com/intellij/openapi/ui/DialogWrapper.java +++ b/platform/platform-api/src/com/intellij/openapi/ui/DialogWrapper.java @@ -665,7 +665,7 @@ public abstract class DialogWrapper { @NotNull private JPanel createButtonsPanel(@NotNull List 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); } diff --git a/platform/platform-api/src/com/intellij/openapi/ui/DialogWrapperButtonLayout.kt b/platform/platform-api/src/com/intellij/openapi/ui/DialogWrapperButtonLayout.kt new file mode 100644 index 000000000000..58ac9db5bb1d --- /dev/null +++ b/platform/platform-api/src/com/intellij/openapi/ui/DialogWrapperButtonLayout.kt @@ -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" + } +} \ No newline at end of file diff --git a/platform/platform-api/src/com/intellij/ui/components/JBOptionButton.java b/platform/platform-api/src/com/intellij/ui/components/JBOptionButton.java index b1871d81adac..c270cda24f0e 100644 --- a/platform/platform-api/src/com/intellij/ui/components/JBOptionButton.java +++ b/platform/platform-api/src/com/intellij/ui/components/JBOptionButton.java @@ -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();