Skip to content

Commit 70068d2

Browse files
authored
fix: streamline tooltip management and enhance event handling (#254)
This pull request refactors the tooltip management system in `tooltip_manager.ts` to simplify the logic and improve maintainability. The most significant changes include removing redundant checks for tooltip enablement in multiple functions, updating the `showTooltip` function to accept an `HTMLElement` parameter, and streamlining the tooltip state update process. ### Refactoring of Tooltip Management: * **Simplified `attachTooltip` logic**: Removed the redundant `tooltipsEnabled` check and moved the addition of the `"has-tooltip"` class to the `showTooltip` function. This reduces duplication and ensures consistent behavior. * **Enhanced mutation observer in `attachTooltip`**: Cleaned up the code for detecting element removal by removing unnecessary conditions and ensuring the observer disconnects after hiding the tooltip. ### Updates to Tooltip Behavior: * **Modified `showTooltip` function**: Added an `HTMLElement` parameter to allow the function to directly manage the tooltip's associated element, including adding the `"has-tooltip"` class. This centralizes tooltip activation logic. * **Streamlined `updateTooltipsState`**: Simplified the logic by only removing the `"has-tooltip"` class when tooltips are disabled, reducing unnecessary conditional branches.
1 parent 33052b9 commit 70068d2

File tree

1 file changed

+23
-38
lines changed

1 file changed

+23
-38
lines changed

src/graphics/renderables/tooltip_manager.ts

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -24,58 +24,45 @@ export function attachTooltip(
2424
key: string,
2525
hideDelay = false,
2626
): void {
27-
const tooltipsEnabled =
28-
globalContext
29-
?.getConfigMenu()
30-
.getConfigSwitchValue(CONFIG_SWITCH_KEYS.ENABLE_TOOLTIPS) ?? true;
31-
3227
if (key in TOOLTIP_CONTENT) {
33-
if (tooltipsEnabled) {
34-
element.classList.add("has-tooltip");
35-
36-
const showHandler = () => showTooltip(key);
37-
const hideHandler = () => {
38-
if (hideDelay) {
39-
startHideTooltipDelay();
40-
} else {
41-
hideTooltip();
28+
const showHandler = () => showTooltip(key, element);
29+
const hideHandler = () => {
30+
if (hideDelay) {
31+
startHideTooltipDelay();
32+
} else {
33+
hideTooltip();
34+
}
35+
};
36+
37+
element.addEventListener("mouseenter", showHandler);
38+
element.addEventListener("mouseleave", hideHandler);
39+
40+
// Add a mutation observer to detect when the element is removed
41+
const observer = new MutationObserver((mutations) => {
42+
mutations.forEach((mutation) => {
43+
if (mutation.type === "childList" && !document.body.contains(element)) {
44+
hideTooltip(); // Hide the tooltip if the element is removed
45+
observer.disconnect(); // Stop observing
4246
}
43-
};
44-
45-
element.addEventListener("mouseenter", showHandler);
46-
element.addEventListener("mouseleave", hideHandler);
47-
48-
// Add a mutation observer to detect when the element is removed
49-
const observer = new MutationObserver((mutations) => {
50-
mutations.forEach((mutation) => {
51-
if (
52-
mutation.type === "childList" &&
53-
!document.body.contains(element)
54-
) {
55-
hideTooltip(); // Hide the tooltip if the element is removed
56-
observer.disconnect(); // Stop observing
57-
}
58-
});
5947
});
48+
});
6049

61-
observer.observe(document.body, { childList: true, subtree: true });
62-
} else {
63-
element.classList.remove("has-tooltip");
64-
}
50+
observer.observe(document.body, { childList: true, subtree: true });
6551
}
6652
}
6753

6854
/**
6955
* Shows a tooltip with the specified key.
7056
* @param key - The key for the tooltip content.
7157
*/
72-
export function showTooltip(key: string): void {
58+
export function showTooltip(key: string, element: HTMLElement): void {
7359
const tooltipsEnabled =
7460
globalContext
7561
?.getConfigMenu()
7662
.getConfigSwitchValue(CONFIG_SWITCH_KEYS.ENABLE_TOOLTIPS) ?? true;
7763
if (!tooltipsEnabled) return;
7864

65+
element.classList.add("has-tooltip");
7966
const text = TOOLTIP_CONTENT[key as keyof typeof TOOLTIP_CONTENT];
8067
const tooltip = document.getElementById("global-tooltip");
8168
if (tooltip) {
@@ -125,9 +112,7 @@ export function updateTooltipsState(): void {
125112
tooltipElements.forEach((element) => {
126113
const htmlElement = element as HTMLElement;
127114

128-
if (tooltipsEnabled) {
129-
htmlElement.classList.add("has-tooltip");
130-
} else {
115+
if (!tooltipsEnabled) {
131116
htmlElement.classList.remove("has-tooltip");
132117
}
133118
});

0 commit comments

Comments
 (0)