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

Middle Click on Tabs #515

Merged
merged 4 commits into from
Sep 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@
package the.bytecode.club.bytecodeviewer.gui.resourceviewer;

import com.github.weisj.darklaf.components.CloseButton;
import com.github.weisj.darklaf.ui.tabbedpane.*;
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
import the.bytecode.club.bytecodeviewer.gui.components.listeners.MouseClickedListener;
import the.bytecode.club.bytecodeviewer.gui.resourceviewer.viewer.ResourceViewer;
import the.bytecode.club.bytecodeviewer.translation.TranslatedStrings;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.*;
import java.util.Objects;

public class TabComponent extends JPanel
{
Expand Down Expand Up @@ -69,11 +71,8 @@ public String getText()
rightClickMenu.add(closeTab);
button.setComponentPopupMenu(rightClickMenu);

//TODO add left-click close: when we add a new listener the parent listener (jTabbedPane) conflicts and won't respect the opaque flag.
// button.addMouseListener(new MouseClickedListener(e ->
// if (e.getButton() == MouseEvent.BUTTON2) // middle-click
// closePane();
// }));
addMouseListener(new TabMouseListener());
addMouseMotionListener(new TabMouseListener());

button.addMouseListener(new MouseClickedListener(e ->
{
Expand Down Expand Up @@ -138,4 +137,147 @@ private void removeTab(int index)
pane.remove(index);
}

/**
* Get the tab panel for mouse positions.
*
* @return the panel.
*/
private ScrollableTabPanel getTabPanel()
{
for (Component component : Objects.requireNonNull(viewport()).getComponents())
if (component instanceof ScrollableTabPanel)
return (ScrollableTabPanel) component;

return null;
}

/**
* Get the viewport from darklaf.
*
* @return the viewport.
*/
private DarkScrollableTabViewport viewport()
{
for (Component component : pane.getComponents())
if (component instanceof DarkScrollableTabViewport)
return (DarkScrollableTabViewport) component;

return null;
}

/**
* Get the tabbed pane handler from darklaf.
*
* @return the handler.
*/
private DarkScrollTabbedPaneHandler getHandler()
{
for (Component component : pane.getComponents())
{
if (component instanceof DarkScrollableTabViewport)
{
DarkScrollableTabViewport viewport = (DarkScrollableTabViewport) component;
for (MouseListener mouseListener : viewport.getMouseListeners())
if (mouseListener instanceof DarkScrollTabbedPaneHandler)
return (DarkScrollTabbedPaneHandler) mouseListener;
}
}

return null;
}

/**
* Create our own listener that redirects events back to darklaf.
*/
private class TabMouseListener extends MouseAdapter
{
@Override
public void mouseClicked(MouseEvent e)
{
e = convert(e);
if (e == null)
return;

Objects.requireNonNull(getHandler()).mouseClicked(e);
}

@Override
public void mousePressed(MouseEvent e)
{
if (e.getButton() == MouseEvent.BUTTON2)
{
closePane();
return;
}

e = convert(e);
if (e == null)
return;

Objects.requireNonNull(getHandler()).mousePressed(e);
}

@Override
public void mouseEntered(MouseEvent e)
{
e = convert(e);
if (e == null)
return;

Objects.requireNonNull(getHandler()).mouseEntered(e);
}

@Override
public void mouseExited(MouseEvent e)
{
e = convert(e);
if (e == null)
return;

Objects.requireNonNull(getHandler()).mouseExited(e);
}

@Override
public void mouseReleased(MouseEvent e)
{
e = convert(e);
if (e == null)
return;

Objects.requireNonNull(getHandler()).mouseReleased(e);
}

@Override
public void mouseDragged(MouseEvent e)
{
e = convert(e);
if (e == null)
return;

Objects.requireNonNull(getHandler()).mouseDragged(e);
}

@Override
public void mouseMoved(MouseEvent e)
{
e = convert(e);
if (e == null)
return;

Objects.requireNonNull(getHandler()).mouseMoved(e);
}

private MouseEvent convert(MouseEvent e)
{
ScrollableTabPanel tabPanel = getTabPanel();
if (tabPanel == null || tabPanel.getMousePosition() == null)
return null;

int x = tabPanel.getMousePosition().x;
int y = tabPanel.getMousePosition().y;
return new MouseEvent((Component) e.getSource(), e.getID(), e.getWhen(), e.getModifiersEx(), x, y,
e.getClickCount(), e.isPopupTrigger());
}
}

}