-
Notifications
You must be signed in to change notification settings - Fork 847
优化 Tooltip #5815
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
CiiLu
wants to merge
3
commits into
HMCL-dev:main
Choose a base branch
from
CiiLu:tt
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
优化 Tooltip #5815
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/JFXTooltip.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| /* | ||
| * Hello Minecraft! Launcher | ||
| * Copyright (C) 2026 huangyuhui <huanghongxun2008@126.com> and contributors | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| */ | ||
| package org.jackhuang.hmcl.ui.construct; | ||
|
|
||
| import javafx.animation.FadeTransition; | ||
| import javafx.animation.PauseTransition; | ||
| import javafx.beans.property.StringProperty; | ||
| import javafx.event.EventHandler; | ||
| import javafx.scene.Node; | ||
| import javafx.scene.control.Label; | ||
| import javafx.scene.input.MouseEvent; | ||
| import javafx.scene.layout.StackPane; | ||
| import javafx.stage.Popup; | ||
| import javafx.util.Duration; | ||
| import org.jackhuang.hmcl.ui.animation.AnimationUtils; | ||
|
|
||
| public class JFXTooltip { | ||
| // https://api.flutter.dev/flutter/material/Tooltip-class.html | ||
| private static final double FADE_IN_MS = AnimationUtils.isAnimationEnabled() ? 150 : 0; | ||
| private static final double FADE_OUT_MS = AnimationUtils.isAnimationEnabled() ? 75 : 0; | ||
|
|
||
| private final Popup popup; | ||
| private final Label label; | ||
| private final PauseTransition showDelayTransition; | ||
| private final PauseTransition showDurationTransition; | ||
|
|
||
| private final FadeTransition fadeIn; | ||
| private final FadeTransition fadeOut; | ||
|
|
||
| private double mouseX; | ||
| private double mouseY; | ||
|
|
||
| private EventHandler<MouseEvent> enteredHandler; | ||
| private EventHandler<MouseEvent> exitedHandler; | ||
| private EventHandler<MouseEvent> pressedHandler; | ||
| private Node attachedNode; | ||
|
|
||
| public JFXTooltip() { | ||
| this(""); | ||
| } | ||
|
|
||
| public JFXTooltip(String text) { | ||
| popup = new Popup(); | ||
| popup.setAutoHide(false); | ||
|
|
||
| label = new Label(text); | ||
| StackPane root = new StackPane(label); | ||
| root.getStyleClass().add("jfx-tooltip"); | ||
| root.setMouseTransparent(true); | ||
|
|
||
| popup.getContent().add(root); | ||
|
|
||
| fadeIn = new FadeTransition(Duration.millis(FADE_IN_MS), root); | ||
| fadeIn.setFromValue(0.0); | ||
| fadeIn.setToValue(1.0); | ||
|
|
||
| fadeOut = new FadeTransition(Duration.millis(FADE_OUT_MS), root); | ||
| fadeOut.setFromValue(1.0); | ||
| fadeOut.setToValue(0.0); | ||
| fadeOut.setOnFinished(event -> popup.hide()); | ||
|
|
||
| showDelayTransition = new PauseTransition(Duration.millis(500)); | ||
| showDurationTransition = new PauseTransition(Duration.millis(5000)); | ||
| showDurationTransition.setOnFinished(e -> hideTooltip()); | ||
| } | ||
|
|
||
| public void setShowDelay(Duration delay) { | ||
| this.showDelayTransition.setDuration(delay); | ||
| } | ||
|
|
||
| public void setShowDuration(Duration duration) { | ||
| this.showDurationTransition.setDuration(duration); | ||
| } | ||
|
|
||
| public final StringProperty textProperty() { | ||
| return label.textProperty(); | ||
| } | ||
|
|
||
| public final void setText(String value) { | ||
| label.setText(value); | ||
| } | ||
|
|
||
| private void hideTooltip() { | ||
| showDelayTransition.stop(); | ||
| showDurationTransition.stop(); | ||
| if (popup.isShowing()) { | ||
| fadeIn.stop(); | ||
| fadeOut.playFromStart(); | ||
| } | ||
| } | ||
|
|
||
| public void install(Node targetNode) { | ||
| if (attachedNode != null) { | ||
| uninstall(); | ||
| } | ||
| this.attachedNode = targetNode; | ||
|
|
||
| enteredHandler = event -> { | ||
| mouseX = event.getScreenX(); | ||
| mouseY = event.getScreenY(); | ||
| showDelayTransition.playFromStart(); | ||
| }; | ||
|
|
||
| exitedHandler = event -> hideTooltip(); | ||
| pressedHandler = event -> hideTooltip(); | ||
|
|
||
| targetNode.addEventHandler(MouseEvent.MOUSE_ENTERED, enteredHandler); | ||
| targetNode.addEventHandler(MouseEvent.MOUSE_EXITED, exitedHandler); | ||
| targetNode.addEventHandler(MouseEvent.MOUSE_PRESSED, pressedHandler); | ||
|
|
||
| showDelayTransition.setOnFinished(e -> { | ||
| if (targetNode.getScene() != null && targetNode.getScene().getWindow() != null) { | ||
| fadeOut.stop(); | ||
| popup.show(targetNode.getScene().getWindow(), mouseX + 5, mouseY); | ||
| fadeIn.playFromStart(); | ||
| showDurationTransition.playFromStart(); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| public void uninstall() { | ||
| if (attachedNode != null) { | ||
| hideTooltip(); | ||
| attachedNode.removeEventHandler(MouseEvent.MOUSE_ENTERED, enteredHandler); | ||
| attachedNode.removeEventHandler(MouseEvent.MOUSE_EXITED, exitedHandler); | ||
| attachedNode.removeEventHandler(MouseEvent.MOUSE_PRESSED, pressedHandler); | ||
|
|
||
| attachedNode = null; | ||
| enteredHandler = null; | ||
| exitedHandler = null; | ||
| pressedHandler = null; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
install(Node)always callsuninstall()wheneverattachedNode != null, even if the tooltip is being (re)installed onto the same node. In list cellupdateItempaths (e.g. whereFXUtils.installSlowTooltip(left, tooltip)is called repeatedly), this causes unnecessary handler churn and transition resets. Consider makinginstallidempotent for the sametargetNode(early-return ifattachedNode == targetNode) and/or avoid re-registering handlers unless the target node actually changes.