From 501852d5f7a01ec74bf013161120264e197e293f Mon Sep 17 00:00:00 2001 From: weisj Date: Wed, 9 Sep 2020 19:09:16 +0200 Subject: [PATCH] Properly calculate preferred width of windows title pane. --- core/src/test/java/ui/ComponentDemo.java | 3 +- core/src/test/java/ui/label/LabelDemo.java | 2 - .../platform/windows/ui/WindowsTitlePane.java | 72 ++++++++++++++----- 3 files changed, 55 insertions(+), 22 deletions(-) diff --git a/core/src/test/java/ui/ComponentDemo.java b/core/src/test/java/ui/ComponentDemo.java index ffe2cc36..59e4b494 100644 --- a/core/src/test/java/ui/ComponentDemo.java +++ b/core/src/test/java/ui/ComponentDemo.java @@ -106,9 +106,8 @@ public interface ComponentDemo { targetSize.width = Math.min(targetSize.width, dim.width); targetSize.height = Math.min(targetSize.height, dim.height); window.setSize(targetSize); - } else { - window.setSize(dimension); } + window.setVisible(true); window.setLocationRelativeTo(null); }); diff --git a/core/src/test/java/ui/label/LabelDemo.java b/core/src/test/java/ui/label/LabelDemo.java index 3862209d..631ff333 100644 --- a/core/src/test/java/ui/label/LabelDemo.java +++ b/core/src/test/java/ui/label/LabelDemo.java @@ -24,8 +24,6 @@ */ package ui.label; -import java.awt.*; - import javax.swing.*; import ui.ComponentDemo; diff --git a/windows/src/main/java/com/github/weisj/darklaf/platform/windows/ui/WindowsTitlePane.java b/windows/src/main/java/com/github/weisj/darklaf/platform/windows/ui/WindowsTitlePane.java index 13ae4dab..e55cf0c3 100644 --- a/windows/src/main/java/com/github/weisj/darklaf/platform/windows/ui/WindowsTitlePane.java +++ b/windows/src/main/java/com/github/weisj/darklaf/platform/windows/ui/WindowsTitlePane.java @@ -727,8 +727,9 @@ public class WindowsTitlePane extends CustomTitlePane { @Override public Dimension getPreferredSize() { if (hideTitleBar()) return new Dimension(0, 0); - int size = computeHeight(); - return new Dimension(size + 1, size + 1); + int height = computeHeight(); + int width = computeWidth(); + return new Dimension(width, height + 1); } private int computeHeight() { @@ -741,6 +742,35 @@ public class WindowsTitlePane extends CustomTitlePane { return Math.max(BAR_HEIGHT, height); } + private int computeWidth() { + int width = 0; + Icon windowIcon = windowIconButton.getIcon(); + if (windowIcon != null) { + width += Math.min(ICON_WIDTH, Math.max(windowIcon.getIconHeight(), windowIcon.getIconWidth())); + width += 2 * PAD; + } + if (menuBar != null) { + width += getPreferredMenuSize().width; + width += PAD; + } + boolean frame = getDecorationStyle() == JRootPane.FRAME; + boolean undecorated = getDecorationStyle() == JRootPane.NONE; + if (!undecorated) { + width += BUTTON_WIDTH; + } + if (frame) { + width += BUTTON_WIDTH; + if (Toolkit.getDefaultToolkit().isFrameStateSupported(Frame.MAXIMIZED_BOTH)) { + width += BUTTON_WIDTH; + } + } + if (!titleLabel.getText().isEmpty()) { + width += titleLabel.getPreferredSize().width; + width += PAD; + } + return width; + } + protected void updateDragArea(final GraphicsConfiguration gc) { JNIDecorationsWindows.updateValues(windowHandle, (int) Scale.scaleWidth(left, gc), @@ -748,6 +778,15 @@ public class WindowsTitlePane extends CustomTitlePane { (int) Scale.scaleHeight(height, gc)); } + private Dimension getPreferredMenuSize() { + LayoutManager menuBarLayout = menuBar.getLayout(); + Dimension size = null; + if (menuBarLayout != null) { + size = menuBarLayout.preferredLayoutSize(menuBar); + } + return (size != null) ? size : menuBar.getPreferredSize(); + } + private class TitlePaneLayout implements LayoutManager { public void addLayoutComponent(final String name, final Component c) {} @@ -781,12 +820,9 @@ public class WindowsTitlePane extends CustomTitlePane { left = 0; right = 0; - if (windowIconButton.getIcon() != null) { - int windowButtonWidth = windowIconButton.getIcon() != null ? Math.max(windowIconButton.getIcon() - .getIconHeight(), - windowIconButton.getIcon() - .getIconWidth()) - : ICON_WIDTH; + Icon windowIcon = windowIconButton.getIcon(); + if (windowIcon != null) { + int windowButtonWidth = Math.max(windowIcon.getIconHeight(), windowIcon.getIconWidth()); windowButtonWidth = Math.min(ICON_WIDTH, windowButtonWidth); windowIconButton.setBounds(start + PAD, y, windowButtonWidth, height); start += windowButtonWidth + 2 * PAD; @@ -815,9 +851,18 @@ public class WindowsTitlePane extends CustomTitlePane { x -= BUTTON_WIDTH; right += BUTTON_WIDTH; minimizeButton.setBounds(x, y, BUTTON_WIDTH, height); + x -= PAD; } start = Math.max(start, PAD); - titleLabel.setBounds(start, 0, x - start, height); + x = Math.min(getWidth() - PAD, x); + int labelWidth = x - start; + int prefLabelWidth = titleLabel.getPreferredSize().width; + if (prefLabelWidth > labelWidth) { + int extra = Math.min(prefLabelWidth - labelWidth, 2 * PAD); + start -= extra / 2; + labelWidth += extra; + } + titleLabel.setBounds(start, 0, labelWidth, height); if (!leftToRight) { mirror(windowIconButton, w); @@ -840,15 +885,6 @@ public class WindowsTitlePane extends CustomTitlePane { component.getY()); } } - - private Dimension getPreferredMenuSize() { - LayoutManager menuBarLayout = menuBar.getLayout(); - Dimension size = null; - if (menuBarLayout != null) { - size = menuBarLayout.preferredLayoutSize(menuBar); - } - return (size != null) ? size : menuBar.getPreferredSize(); - } } protected class WindowPropertyChangeListener implements PropertyChangeListener {