Skip to content

A free, open-source alternative to Burp Suite: an intercepting proxy and web app security testing suite with Repeater, Intruder, Collaborator and a built-in OWASP ZAP scanner. Linux, AGPL-3.0.

8.5 KB
Raw Blame
package opensuite.ui.theme;

import opensuite.ui.Theme;

import javax.swing.*;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicTabbedPaneUI;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

/**
 * Tabs as a row of labels on the page background. The selected tab carries a
 * short accent underline and full-strength text; the others are muted and
 * brighten under the pointer. The tab row is closed by one hairline. Titles
 * are shown with a leading capital so the strip reads as a set of headings.
 */
public class FlatTabbedPaneUI extends BasicTabbedPaneUI {

    private int hover = -1;

    /**
     * Held so they can be taken off again. Swing calls uninstallUI whenever a
     * pane's UI is replaced (a theme switch, or any setUI), and that nulls
     * tabPane. A listener left attached to the pane then fires against a null
     * tabPane and throws on the event thread, once per mouse move, until the
     * interface stops repainting.
     */
    private java.awt.event.MouseMotionListener hoverMotion;
    private java.awt.event.MouseListener hoverExit;

    public static ComponentUI createUI(JComponent c) { return new FlatTabbedPaneUI(); }

    @Override protected void installDefaults() {
        super.installDefaults();
        tabPane.setOpaque(true);
        tabPane.setBackground(Theme.BG);
    }

    @Override protected void installListeners() {
        super.installListeners();
        hoverMotion = new MouseMotionAdapter() {
            @Override public void mouseMoved(MouseEvent e) {
                JTabbedPane p = tabPane;
                if (p == null) return;
                int i = tabForCoordinate(p, e.getX(), e.getY());
                if (i != hover) { hover = i; p.repaint(); }
            }
        };
        hoverExit = new java.awt.event.MouseAdapter() {
            @Override public void mouseExited(MouseEvent e) {
                JTabbedPane p = tabPane;
                if (p == null) return;
                hover = -1;
                p.repaint();
            }
        };
        tabPane.addMouseMotionListener(hoverMotion);
        tabPane.addMouseListener(hoverExit);
    }

    @Override protected void uninstallListeners() {
        if (tabPane != null) {
            if (hoverMotion != null) tabPane.removeMouseMotionListener(hoverMotion);
            if (hoverExit != null) tabPane.removeMouseListener(hoverExit);
        }
        hoverMotion = null;
        hoverExit = null;
        hover = -1;
        super.uninstallListeners();
    }

    /** Never stretch a row of tabs to fill the width; tabs keep their natural size. */
    @Override protected boolean shouldPadTabRun(int p, int run) { return false; }

    @Override protected int getTabLabelShiftX(int p, int i, boolean s) { return 0; }
    /** Client property (Integer): pixels to move every tab's label down by; negative moves it up. */
    public static final String LABEL_SHIFT = "opensuite.tabs.labelShift";

    @Override protected int getTabLabelShiftY(int p, int i, boolean s) {
        return tabPane.getClientProperty(LABEL_SHIFT) instanceof Integer shift ? shift : 0;
    }
    /** Client property: how far the first tab starts from the left edge of the strip. */
    public static final String LEFT_INSET = "opensuite.tabs.leftInset";

    @Override protected Insets getTabAreaInsets(int placement) {
        Insets in = super.getTabAreaInsets(placement);
        if (tabPane.getClientProperty(LEFT_INSET) instanceof Integer left) return new Insets(in.top, in.left + left, in.bottom, in.right);
        return in;
    }

    @Override protected int calculateTabHeight(int p, int i, int fontHeight) { return fontHeight + tabInsets.top + tabInsets.bottom + 3; }

    /**
     * The strip takes the pane's own background. Scrolling tabs are drawn in a
     * viewport of their own, which would otherwise keep the viewport default.
     */
    @Override protected void paintTabArea(Graphics g, int tabPlacement, int selectedIndex) {
        Rectangle clip = g.getClipBounds();
        if (clip != null) {
            g.setColor(tabPane.getBackground());
            g.fillRect(clip.x, clip.y, clip.width, clip.height);
        }
        super.paintTabArea(g, tabPlacement, selectedIndex);
    }

    /** How far into its tab a plain label starts. */
    protected int labelInset() { return 15; }

    /** Whether the selected tab is filled with the page colour on a strip of another colour. */
    protected boolean fillsSelectedTab() { return true; }

    /** Marks a tab icon whose tab is parted from the one before it by a vertical line. */
    public interface Divided {}

    @Override protected void paintTabBackground(Graphics g, int p, int i, int x, int y, int w, int h, boolean sel) {
        // On a strip that is not the page colour (the white top-level row), the
        // selected tab is filled with the page colour so it joins the page below.
        if (sel && fillsSelectedTab() && !Theme.BG.equals(tabPane.getBackground())) {
            g.setColor(Theme.BG);
            g.fillRect(x, y, w, h);
        }
        if (tabPane.getIconAt(i) instanceof Divided) {
            g.setColor(Theme.BORDER);
            g.fillRect(x + 5, y + 5, 1, 24);
        }
        Color custom = tabPane.getBackgroundAt(i);
        if (custom != null && !custom.equals(tabPane.getBackground())) {
            // A tool asking for attention flashes its tab with the accent.
            Graphics2D g2 = (Graphics2D) g.create();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setColor(Theme.ACCENT_LIGHT);
            g2.fillRoundRect(x + 2, y + 3, w - 4, h - 6, 8, 8);
            g2.dispose();
        }
    }

    /** Client property (Color): the bar under the chosen tab, where it is not the accent. */
    public static final String UNDERLINE = "opensuite.tabs.underline";

    @Override protected void paintTabBorder(Graphics g, int p, int i, int x, int y, int w, int h, boolean sel) {
        if (sel) {
            g.setColor(tabPane.getClientProperty(UNDERLINE) instanceof Color c ? c : Theme.ACCENT);
            // A 3px bar across the whole tab, sitting on the hairline that closes the row.
            g.fillRect(x, y + h - 3, w, 3);
        }
    }

    @Override protected void paintText(Graphics g, int p, Font font, FontMetrics fm, int i, String title,
                                       Rectangle r, boolean sel) {
        String shown = title == null || title.isEmpty() ? "" : Character.toUpperCase(title.charAt(0)) + title.substring(1);
        Graphics2D g2 = (Graphics2D) g.create();
        opensuite.ui.Theme.textSmoothing(g2);
        g2.setFont(font);
        Color fg = tabPane.getForegroundAt(i);
        if (!tabPane.isEnabledAt(i)) fg = Theme.BORDER;
        else if (sel) fg = Theme.TAB_TEXT_ON;
        else if (fg == null || fg instanceof javax.swing.plaf.UIResource) fg = Theme.TAB_TEXT;
        g2.setColor(fg);
        // A plain label starts a fixed distance into its tab rather than being
        // centred, so a row of tabs keeps one pitch whatever the label widths.
        int x = tabPane.getIconAt(i) == null && tabPane.getTabComponentAt(i) == null && i < rects.length
                ? rects[i].x + labelInset() : r.x + (r.width - fm.stringWidth(shown)) / 2;
        g2.drawString(shown, x, r.y + fm.getAscent());
        g2.dispose();
    }

    @Override protected void paintContentBorder(Graphics g, int p, int sel) {
        // The hairline under the tab row runs the full width, even when a right
        // inset keeps the end of the row clear for the suite's tool icons.
        int w = tabPane.getWidth();
        Insets ins = tabPane.getInsets();
        // It sits in the row just under the tabs (the content border), so it is
        // drawn the same whether the tabs wrap or scroll in their own viewport.
        int y = ins.top + calculateTabAreaHeight(p, runCount, maxTabHeight);
        g.setColor(Theme.BORDER);
        g.fillRect(0, y, w, 1);
    }

    @Override protected void paintFocusIndicator(Graphics g, int p, Rectangle[] rects, int i, Rectangle ir, Rectangle tr, boolean sel) {}

    @Override protected int calculateTabWidth(int p, int i, FontMetrics fm) {
        // Measure the capitalised title so the label is never clipped.
        String t = tabPane.getTitleAt(i);
        int base = super.calculateTabWidth(p, i, fm);
        if (t != null && !t.isEmpty()) {
            String shown = Character.toUpperCase(t.charAt(0)) + t.substring(1);
            base += Math.max(0, fm.stringWidth(shown) - fm.stringWidth(t));
        }
        return base;
    }
}