Browse Source

Properly calculate preferred width of windows title pane.

pull/214/head
weisj 4 years ago
parent
commit
501852d5f7
  1. 3
      core/src/test/java/ui/ComponentDemo.java
  2. 2
      core/src/test/java/ui/label/LabelDemo.java
  3. 72
      windows/src/main/java/com/github/weisj/darklaf/platform/windows/ui/WindowsTitlePane.java

3
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);
});

2
core/src/test/java/ui/label/LabelDemo.java

@ -24,8 +24,6 @@
*/
package ui.label;
import java.awt.*;
import javax.swing.*;
import ui.ComponentDemo;

72
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 {

Loading…
Cancel
Save