Skip to content

Add repo file tree middle click open #34730

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

bytedream
Copy link
Contributor

When clicking a file in the repo view file tree with the middle mouse button, the file will be now opened in a new tab.

file-tree-mouse-click.mp4

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Jun 15, 2025
@lunny lunny added type/enhancement An improvement of existing functionality topic/ui Change the appearance of the Gitea UI labels Jun 15, 2025
@lunny lunny added this to the 1.25.0 milestone Jun 15, 2025
@wxiaoguang
Copy link
Contributor

It should also follow the browser's default behavior: Ctrl+Click / Command+Click means "Open in New Window"

@bytedream
Copy link
Contributor Author

Done, thanks for pointing this out

@@ -68,6 +72,7 @@ const doGotoSubModule = () => {
:class="{'selected': selectedItem === item.fullPath}"
:title="item.entryName"
@click.stop="doLoadFileContent"
@click.middle.stop="doLoadFileContent"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything is handled in @click.stop, so the @click.middle.stop should be removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@click.stop does not fire for aux clicks. @click.middle.stop is transformed into a mousup event by vue internally, which is able to recognize aux clicks. @auxclick.stop also exists, but Safari only added support for it lately, so it may not work for some users with older browsers.
@click.stop only fires if the click is released on the same button where it was pressed. The mouseup event always fires when the click is released, regardless of the starting point. This difference in behavior is why I didn't unify both handlers into a single @mouseup.stop.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@click.stop only fires if the click is released on the same button where it was pressed.

But that's the expected behavior.

For example, if you "mousedown" in a normal <a>, then move mouse out, the link won't open.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I just wanted to say is that I didn't unify both calls into a mouseup because that would mess with the current behavior

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would happen if "Ctrl+LeftClick"? I guess it won't trigger click.middle?

@@ -94,6 +100,7 @@ const doGotoSubModule = () => {
:class="{'selected': selectedItem === item.fullPath}"
:title="item.entryName"
@click.stop="doLoadDirContent"
@click.middle.stop="doLoadFileContent"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't seem right. Here it is doLoadDirContent, not doLoadFileContent. And does it really work here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

9c4a8e2. Yes it works here, it doesn't matter if the target is a file or directory

@wxiaoguang
Copy link
Contributor

By doing some searches, I think "middle key" is not a widely used approach to "open a new window". I do not see browsers officially support it.

The widely supported approach is "Ctrl+LeftClick"

@wxiaoguang
Copy link
Contributor

If you'd like to support "middle click", the ideal approach can be like this:

  • make the items to be real <a> links, then middle-click works for users by default.
  • use our @click handler to handle ctrl/meta keys, and stop the default link's behavior.

@silverwind
Copy link
Member

silverwind commented Jun 17, 2025

I think we should just render a native <a> element and only preventDefault the click event with button 0, while letting the browser handle all other interactions.

This way, stuff like middle click, ctrl+click, cmd+click will all work natively and don't even need explicit handlers.

@wxiaoguang
Copy link
Contributor

I think we should just render a native <a> element and only preventDefault the click event with button 0, while letting the browser handle all other interactions.

This way, stuff like middle click, ctrl+click, cmd+click will all work natively and don't even need explicit handlers.

The same as #34730 (comment) ? Or is there something I missed?

@silverwind
Copy link
Member

silverwind commented Jun 17, 2025

I think we should just render a native <a> element and only preventDefault the click event with button 0, while letting the browser handle all other interactions.
This way, stuff like middle click, ctrl+click, cmd+click will all work natively and don't even need explicit handlers.

The same as #34730 (comment) ? Or is there something I missed?

Basically the same, but I guess the code still need to check whether any modifier key is held, and only e.preventDefault when none are because deciding only on e.button is not enough.

@wxiaoguang
Copy link
Contributor

I see, maybe ideally it could be like this:

  • use <a> for tree items with real links.
  • if no ctrl/meta/alt key, then use our "fetch" to load the file view content without reload, and preventDefault.
  • otherwise, we do nothing and leave the work to the browser.

@silverwind
Copy link
Member

silverwind commented Jun 17, 2025

Yes, so basically:

if (e.button !== 0 || e.ctrlKey || e.metaKey || e.altKey || e.shiftKey) {
  return; // let browser handle the click
}

e.preventDefault();
// update view to clicked file

Maybe shiftKey can be removed, I'm not aware of any special functionality tied to it. altKey is useful for text selection within a link.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. modifies/frontend topic/ui Change the appearance of the Gitea UI type/enhancement An improvement of existing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants