|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.hop.ui.core.gui; |
| 19 | + |
| 20 | +import lombok.Setter; |
| 21 | +import org.apache.hop.ui.core.ConstUi; |
| 22 | +import org.apache.hop.ui.core.PropsUi; |
| 23 | +import org.eclipse.swt.SWT; |
| 24 | +import org.eclipse.swt.graphics.Point; |
| 25 | +import org.eclipse.swt.layout.FillLayout; |
| 26 | +import org.eclipse.swt.widgets.Display; |
| 27 | +import org.eclipse.swt.widgets.Label; |
| 28 | +import org.eclipse.swt.widgets.Shell; |
| 29 | + |
| 30 | +/** |
| 31 | + * A custom tooltip implementation using a Shell and Label that supports proper dark mode styling. |
| 32 | + * This replaces the SWT ToolTip widget which doesn't support background/foreground color |
| 33 | + * customization. |
| 34 | + */ |
| 35 | +public class HopToolTip { |
| 36 | + |
| 37 | + private final Shell tipShell; |
| 38 | + private final Label tipLabel; |
| 39 | + @Setter private boolean autoHide = true; |
| 40 | + |
| 41 | + /** |
| 42 | + * Creates a new custom tooltip |
| 43 | + * |
| 44 | + * @param parent The parent shell |
| 45 | + */ |
| 46 | + public HopToolTip(Shell parent) { |
| 47 | + tipShell = new Shell(parent, SWT.ON_TOP | SWT.NO_FOCUS | SWT.TOOL); |
| 48 | + FillLayout layout = new FillLayout(); |
| 49 | + layout.marginWidth = ConstUi.SMALL_MARGIN; |
| 50 | + layout.marginHeight = ConstUi.SMALL_MARGIN; |
| 51 | + tipShell.setLayout(layout); |
| 52 | + |
| 53 | + tipLabel = new Label(tipShell, SWT.NONE); |
| 54 | + |
| 55 | + // Apply dark mode styling |
| 56 | + GuiResource gui = GuiResource.getInstance(); |
| 57 | + if (PropsUi.getInstance().isDarkMode()) { |
| 58 | + tipShell.setBackground(gui.getColor(32, 31, 27)); // Dark background |
| 59 | + tipLabel.setBackground(gui.getColor(32, 31, 27)); |
| 60 | + tipLabel.setForeground(gui.getColor(224, 224, 224)); // Light text |
| 61 | + } else { |
| 62 | + tipShell.setBackground(gui.getColorWhite()); // Light background |
| 63 | + tipLabel.setBackground(gui.getColorWhite()); |
| 64 | + tipLabel.setForeground(gui.getColorBlack()); // Dark text |
| 65 | + } |
| 66 | + |
| 67 | + // Auto-hide when user clicks anywhere |
| 68 | + Display.getCurrent() |
| 69 | + .addFilter( |
| 70 | + SWT.MouseDown, |
| 71 | + event -> { |
| 72 | + if (autoHide && !tipShell.isDisposed() && tipShell.isVisible()) { |
| 73 | + setVisible(false); |
| 74 | + } |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Sets the tooltip text |
| 80 | + * |
| 81 | + * @param text The text to display |
| 82 | + */ |
| 83 | + public void setText(String text) { |
| 84 | + if (tipLabel != null && !tipLabel.isDisposed()) { |
| 85 | + tipLabel.setText(text != null ? text : ""); |
| 86 | + tipShell.pack(); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Gets the tooltip text |
| 92 | + * |
| 93 | + * @return The current text |
| 94 | + */ |
| 95 | + public String getText() { |
| 96 | + if (tipLabel != null && !tipLabel.isDisposed()) { |
| 97 | + return tipLabel.getText(); |
| 98 | + } |
| 99 | + return ""; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Sets the tooltip location |
| 104 | + * |
| 105 | + * @param x The x coordinate |
| 106 | + * @param y The y coordinate |
| 107 | + */ |
| 108 | + public void setLocation(int x, int y) { |
| 109 | + if (tipShell != null && !tipShell.isDisposed()) { |
| 110 | + tipShell.setLocation(x, y); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Sets the tooltip location |
| 116 | + * |
| 117 | + * @param location The location point |
| 118 | + */ |
| 119 | + public void setLocation(Point location) { |
| 120 | + setLocation(location.x, location.y); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Shows or hides the tooltip |
| 125 | + * |
| 126 | + * @param visible true to show, false to hide |
| 127 | + */ |
| 128 | + public void setVisible(boolean visible) { |
| 129 | + if (tipShell != null && !tipShell.isDisposed()) { |
| 130 | + tipShell.setVisible(visible); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Checks if the tooltip is visible |
| 136 | + * |
| 137 | + * @return true if visible |
| 138 | + */ |
| 139 | + public boolean isVisible() { |
| 140 | + return tipShell != null && !tipShell.isDisposed() && tipShell.isVisible(); |
| 141 | + } |
| 142 | + |
| 143 | + /** Disposes the tooltip */ |
| 144 | + public void dispose() { |
| 145 | + if (tipShell != null && !tipShell.isDisposed()) { |
| 146 | + tipShell.dispose(); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Checks if the tooltip is disposed |
| 152 | + * |
| 153 | + * @return true if disposed |
| 154 | + */ |
| 155 | + public boolean isDisposed() { |
| 156 | + return tipShell == null || tipShell.isDisposed(); |
| 157 | + } |
| 158 | +} |
0 commit comments