Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

focusWidth isn't being properly taken into account for component size or painting #317

Closed
gtimbo opened this issue Apr 29, 2021 · 2 comments

Comments

@gtimbo
Copy link

gtimbo commented Apr 29, 2021

I'm loving FlatLaf, having only recently discovered it, but I'm having some painting issues when using the IntelliJ theme, which has a focusWidth of 2.0.

When I place various components (JButtons, JSpinners, etc) into a panel with no insets (using MigLayout("ins 0")), the outer border of the components is not being fully drawn. For example:

image

I'm on Windows 10, AdoptOpenJDK 11, using FlatLaf 1.1.2.

Here's an SSCCE that shows the issue:

import javax.swing.*;
import com.formdev.flatlaf.FlatIntelliJLaf;
import net.miginfocom.swing.MigLayout;
import static javax.swing.WindowConstants.EXIT_ON_CLOSE;

public class FocusWidthTest
{
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            try {
                UIManager.setLookAndFeel(new FlatIntelliJLaf());

                JFrame frame = new JFrame("Focus Width Test");
                frame.setLayout(new MigLayout("ins 0"));
                frame.add(new JButton("Test Button"));
                frame.pack();
                frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                frame.setVisible(true);
            } catch (Exception x) {
            }
        });
    }
}

I haven't looked into the code too much, sorry, hence I'm not sure if it's a problem with the preferred size being incorrect, or with the painting not being offset correctly.

@DevCharly
Copy link
Collaborator

This is normal behavior. It is a feature of MigLayout called "visual padding".

FlatLaf tells MigLayout (via client property) the insets of the "invisible" space around a component (e.g. the focus border in IntelliJ/Darcula themes) and MigLayout then ignores the invisible space while computing its grid layout.

Following screenshot shows "FlatLaf IntelliJ" theme on left side (with focusWidth = 2)
and "FlatLaf Light" theme on right side (with focusWidth = 0).
Red rectangles indicate component sizes, green lines indicate grid rows.
Although buttons in IntelliJ theme are larger than in Light theme (because of the focus border),
the layout is the same and the (visual) gap between the (unfocused) buttons is the same.

image

With the help of MigLayout "visual paddings", the layout in the FlatLaf Demo stays stable when you switch from IntelliJ to Light theme:

image

image

Generally this is a great feature, but it has the problem that MigLayout places the component partly outside of its container if the container does not have insets.

image

IMHO MigLayout should take this into account and avoid placing components partly outside of container.
I consider creating a PR to improve this in MigLayout.

To fix this either use insets for the container:

frame.setLayout(new MigLayout("insets 2"));

or disable visual padding for the container:

frame.setLayout(new MigLayout(" novisualpadding, insets 0"));

See: https://www.formdev.com/jformdesigner/doc/layouts/miglayout-whitepaper/#layout_cons_novisualpadding

@gtimbo
Copy link
Author

gtimbo commented May 11, 2021

Thanks very much Karl, especially for going to so much effort in your reply! I've never had to deal with visual padding before in MigLayout, but that makes sense.

It does surprise me that MigLayout is able to essentially place components outside of their container -- although I do see how it makes the layout stable between LnF changes. A PR to improve MigLayout would probably help to ease confusion for other users.

I'll just use insets for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants