diff --git a/app/src/main/java/io/github/ghosthack/microhm/app/ui/MainWindow.java b/app/src/main/java/io/github/ghosthack/microhm/app/ui/MainWindow.java index 3531934..5bde9e7 100644 --- a/app/src/main/java/io/github/ghosthack/microhm/app/ui/MainWindow.java +++ b/app/src/main/java/io/github/ghosthack/microhm/app/ui/MainWindow.java @@ -53,6 +53,7 @@ import javax.swing.Timer; import javax.swing.ToolTipManager; import javax.swing.UIManager; +import javax.swing.border.Border; import java.awt.BorderLayout; import java.awt.CardLayout; import java.awt.Color; @@ -115,11 +116,6 @@ public final class MainWindow extends JFrame { * unless the user dragged the divider somewhere else first. */ private static final int SIDEBAR_WIDTH = 280; - /** Retains the sidebar's neutral one-pixel boundary while suppressing FlatLaf's - * wider blue focus ring and focused-border recoloring. */ - private static final String SIDEBAR_NO_FOCUS_BORDER_STYLE = - "focusWidth: 0; focusedBorderColor: $Component.borderColor"; - /** Keep the session portion of the native window title useful without letting a * prompt-derived Codex preview crowd out the project and application names. */ private static final int WINDOW_TITLE_SESSION_MAX_CHARS = 64; @@ -1443,6 +1439,11 @@ Component visibleCardForTesting() { return null; } + /** The session list scroll pane's current border. EDT only. */ + Border sidebarScrollBorderForTesting() { + return sidebarScrollPane.getBorder(); + } + /** The widget of {@code name}'s landed terminal, or null while it has none. EDT only. */ Component terminalCardForTesting(String name) { SessionTerminal terminal = attached.get(name); @@ -2616,9 +2617,8 @@ private void openSettingsDialog() { toolbarMatchesSidebar = result.toolbarMatchesSidebar(); sidebarSectionsMatchBackground = result.sidebarSectionsMatchBackground(); terminalBackgroundOverride = result.backgroundOverride(); - applySidebarBackground(); sidebarFocusBorderHidden = result.sidebarFocusBorderHidden(); - applySidebarFocusBorder(); + applySidebarBackground(); sidebarReticule = result.sidebarReticule(); sidebarCellRenderer.setReticuleEnabled(sidebarReticule); sidebarFolderGroups = result.sidebarFolderGroups(); @@ -2720,6 +2720,8 @@ private void applySidebarBackground() { harnessBudgetPanel.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createMatteBorder(0, 0, 0, 1, edge), BorderFactory.createEmptyBorder(0, 0, 3, 0))); + // The list's own boundary is theme-derived too, so it refreshes with the rest. + applySidebarFocusBorder(); sidebarList.repaint(); applyRightPaneBackground(); applyToolbarBackground(); @@ -2815,8 +2817,37 @@ private static void applySidebarPanelBackground(Container container, Color backg /** Applies the sidebar-only focus treatment live without changing row selection paint. */ private void applySidebarFocusBorder() { - sidebarScrollPane.putClientProperty(FlatClientProperties.STYLE, - sidebarFocusBorderHidden ? SIDEBAR_NO_FOCUS_BORDER_STYLE : null); + if (sidebarFocusBorderHidden) { + // A plain matte rather than FlatLaf's scroll border: the same neutral + // boundary, with no blue focus ring to suppress. The style property only + // styles FlatLaf borders, so it is cleared with the border it styled. + sidebarScrollPane.putClientProperty(FlatClientProperties.STYLE, null); + sidebarScrollPane.setBorder( + sidebarScrollBorder(UIManager.getColor("Component.borderColor"))); + } else { + sidebarScrollPane.setBorder(UIManager.getBorder("ScrollPane.border")); + sidebarScrollPane.putClientProperty(FlatClientProperties.STYLE, null); + } + } + + /** + * The session list's neutral one-pixel boundary, minus its left edge. The sidebar + * sits flush against the window's left edge, where the platform composites the + * window's own one-pixel outline over the content; a border edge painted there + * stacks onto the outline and reads darker than the rest of the boundary. The + * empty outer border keeps the usual 1px insets on every side — the viewport and + * the surrounding dividers keep their geometry — while the matte starts its top + * and bottom edges one pixel in, leaving the window edge single-coated. The left + * edge stays omitted on every platform, not just where the outline composites: + * the surrounding sidebar sections draw no left edge anywhere, so one here would + * be a line only the list carries; where the window frame sits outside the + * content area the sidebar simply ends flush with the frame, matching its + * neighbors. + */ + static Border sidebarScrollBorder(Color edge) { + return BorderFactory.createCompoundBorder( + BorderFactory.createEmptyBorder(0, 1, 0, 0), + BorderFactory.createMatteBorder(1, 0, 1, 1, edge)); } // ------------------------------------------------------------------ diff --git a/app/src/test/java/io/github/ghosthack/microhm/app/ui/MainWindowSidebarBorderTest.java b/app/src/test/java/io/github/ghosthack/microhm/app/ui/MainWindowSidebarBorderTest.java new file mode 100644 index 0000000..4f5cf5b --- /dev/null +++ b/app/src/test/java/io/github/ghosthack/microhm/app/ui/MainWindowSidebarBorderTest.java @@ -0,0 +1,126 @@ +package io.github.ghosthack.microhm.app.ui; + +import io.github.ghosthack.microhm.app.host.FakeSessionHost; +import io.github.ghosthack.microhm.app.mcp.McpService; +import io.github.ghosthack.microhm.app.registry.RegistryPaths; +import io.github.ghosthack.microhm.app.registry.SessionRegistry; +import io.github.ghosthack.microhm.app.session.SessionEvents; +import io.github.ghosthack.microhm.app.session.SessionLabeller; +import io.github.ghosthack.microhm.app.session.SessionLauncher; +import io.github.ghosthack.microhm.app.settings.AppSettings; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import javax.swing.JPanel; +import javax.swing.SwingUtilities; +import javax.swing.UIManager; +import javax.swing.border.Border; +import javax.swing.border.CompoundBorder; +import javax.swing.border.EmptyBorder; +import javax.swing.border.MatteBorder; +import java.awt.GraphicsEnvironment; +import java.awt.Insets; +import java.nio.file.Path; +import java.util.concurrent.Callable; +import java.util.concurrent.atomic.AtomicReference; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assumptions.assumeFalse; + +/** + * Drives a real {@link MainWindow} (never shown) and pins the session list's + * installed border both ways: hidden focus decoration installs the neutral + * boundary without a left edge (see {@link SidebarScrollBorderTest} for its + * painted shape), keeping the same one-pixel insets as the look-and-feel border + * it replaces; the visible opt-out restores the stock look-and-feel border. + * Needs a display: {@code MainWindow} is a {@code JFrame}. + */ +final class MainWindowSidebarBorderTest { + + @TempDir + Path dir; + + private String previousRegistryOverride; + private MainWindow window; + + @BeforeEach + void pointSettingsAtTempDir() { + // AppSettings resolves settings.properties beside the registry file + previousRegistryOverride = System.getProperty(RegistryPaths.OVERRIDE_PROPERTY); + System.setProperty(RegistryPaths.OVERRIDE_PROPERTY, dir.resolve("sessions.json").toString()); + } + + @AfterEach + void tearDown() throws Exception { + if (window != null) { + SwingUtilities.invokeAndWait(window::shutdown); + } + if (previousRegistryOverride == null) { + System.clearProperty(RegistryPaths.OVERRIDE_PROPERTY); + } else { + System.setProperty(RegistryPaths.OVERRIDE_PROPERTY, previousRegistryOverride); + } + } + + @Test + void sidebarListKeepsItsBoundaryWithoutALeftEdge() throws Exception { + assumeFalse(GraphicsEnvironment.isHeadless(), "MainWindow is a JFrame"); + AppSettings.saveSidebarFocusBorderHidden(true); + + openWindow(); + + Border border = onEdt(window::sidebarScrollBorderForTesting); + CompoundBorder compound = + assertInstanceOf(CompoundBorder.class, border, "the matte replaces the LaF border"); + assertEquals(new Insets(1, 1, 1, 1), + onEdt(() -> border.getBorderInsets(new JPanel())), "viewport geometry is unchanged"); + EmptyBorder outside = + assertInstanceOf(EmptyBorder.class, compound.getOutsideBorder(), "outer spacer"); + assertEquals(new Insets(0, 1, 0, 0), + onEdt(() -> outside.getBorderInsets(new JPanel())), "left edge is empty, not painted"); + MatteBorder inside = + assertInstanceOf(MatteBorder.class, compound.getInsideBorder(), "inner matte"); + assertEquals(onEdt(() -> UIManager.getColor("Component.borderColor")), + onEdt(inside::getMatteColor), "matte follows the theme's border color"); + } + + @Test + void sidebarListRestoresStockBorderWhenFocusRingVisible() throws Exception { + assumeFalse(GraphicsEnvironment.isHeadless(), "MainWindow is a JFrame"); + AppSettings.saveSidebarFocusBorderHidden(false); + + openWindow(); + + assertSame(onEdt(() -> UIManager.getBorder("ScrollPane.border")), + onEdt(window::sidebarScrollBorderForTesting), + "the visible opt-out restores the stock LaF border"); + } + + private void openWindow() throws Exception { + SessionRegistry registry = SessionRegistry.load(dir.resolve("sessions.json")); + FakeSessionHost host = new FakeSessionHost(); + window = onEdt(() -> new MainWindow(registry, host, + SessionLauncher.withoutMcp(registry, host, SessionLabeller.NONE), + new McpService(McpService.NO_TOOLS), new SessionEvents())); + } + + private static T onEdt(Callable call) throws Exception { + AtomicReference result = new AtomicReference<>(); + AtomicReference failure = new AtomicReference<>(); + SwingUtilities.invokeAndWait(() -> { + try { + result.set(call.call()); + } catch (Exception e) { + failure.set(e); + } + }); + if (failure.get() != null) { + throw failure.get(); + } + return result.get(); + } +} diff --git a/app/src/test/java/io/github/ghosthack/microhm/app/ui/SidebarScrollBorderTest.java b/app/src/test/java/io/github/ghosthack/microhm/app/ui/SidebarScrollBorderTest.java new file mode 100644 index 0000000..6959321 --- /dev/null +++ b/app/src/test/java/io/github/ghosthack/microhm/app/ui/SidebarScrollBorderTest.java @@ -0,0 +1,57 @@ +package io.github.ghosthack.microhm.app.ui; + +import org.junit.jupiter.api.Test; + +import javax.swing.JPanel; +import javax.swing.border.Border; +import java.awt.Color; +import java.awt.Graphics2D; +import java.awt.Insets; +import java.awt.image.BufferedImage; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Pins the session list boundary's shape: the usual one-pixel insets on every + * side, paint on the top/right/bottom edges, and nothing on the left edge, which + * sits flush against the window edge where the platform draws its own outline — + * painting there stacks onto that outline and reads darker than the rest of the + * boundary. + */ +final class SidebarScrollBorderTest { + + private static final int SIZE = 21; + + @Test + void keepsOnePixelInsetsOnEverySide() { + Border border = MainWindow.sidebarScrollBorder(Color.WHITE); + assertEquals(new Insets(1, 1, 1, 1), border.getBorderInsets(new JPanel())); + } + + @Test + void paintsEveryEdgeExceptTheLeft() { + BufferedImage painted = paintBorder(MainWindow.sidebarScrollBorder(Color.WHITE)); + int black = Color.BLACK.getRGB(); + int white = Color.WHITE.getRGB(); + for (int y = 0; y < SIZE; y++) { + assertEquals(black, painted.getRGB(0, y), "left edge stays unpainted at y=" + y); + assertEquals(white, painted.getRGB(SIZE - 1, y), "right edge painted at y=" + y); + } + for (int x = 1; x < SIZE; x++) { + assertEquals(white, painted.getRGB(x, 0), "top edge painted at x=" + x); + assertEquals(white, painted.getRGB(x, SIZE - 1), "bottom edge painted at x=" + x); + } + assertEquals(black, painted.getRGB(0, 0), "top-left corner stays unpainted"); + assertEquals(black, painted.getRGB(0, SIZE - 1), "bottom-left corner stays unpainted"); + } + + private static BufferedImage paintBorder(Border border) { + BufferedImage image = new BufferedImage(SIZE, SIZE, BufferedImage.TYPE_INT_RGB); + Graphics2D g = image.createGraphics(); + g.setColor(Color.BLACK); + g.fillRect(0, 0, SIZE, SIZE); + border.paintBorder(new JPanel(), g, 0, 0, SIZE, SIZE); + g.dispose(); + return image; + } +}