Transpiled version (9771L) is out of date.
1 | // instantiate only once per component! |
2 | sclass InstantNeverHideToolTip > MouseAdapter { |
3 | JComponent component; |
4 | JToolTip toolTip; |
5 | Popup popup; |
6 | settable Pt offset = pt(20, 20); |
7 | int x, y; // popup position |
8 | S text; |
9 | IF0<S> calculateText; |
10 | IF1<Pt, S> calculateTextFromCoordinates; |
11 | settable bool verbose; |
12 | Pt mousePt = pt(0, 0); // last mouse position in component |
13 | |
14 | *(IF0<S> *calculateText, JComponent component) { this(component); } |
15 | *(IF1<Pt, S> *calculateTextFromCoordinates, JComponent component) { this(component); } |
16 | *(S *text, JComponent component) { this(component); } |
17 | |
18 | *(JComponent *component) { |
19 | swing { |
20 | component.addMouseListener(InstantNeverHideToolTip.this); |
21 | component.addMouseMotionListener(InstantNeverHideToolTip.this); |
22 | toolTip = component.createToolTip(); |
23 | |
24 | // Hide also when component is removed from hierarchy |
25 | bindToComponent(component, null, l0 hideToolTip); |
26 | } |
27 | } |
28 | |
29 | @Override public void mouseEntered(MouseEvent e) { |
30 | mousePt(ptFromEvent(e)); |
31 | showToolTip(); |
32 | } |
33 | |
34 | void mousePt(Pt p) { |
35 | mousePt = p; |
36 | if (p != null) { |
37 | var p2 = translatePt(convertPointToScreen(p, component), offset); |
38 | x = p2.x; |
39 | y = p2.y; |
40 | } |
41 | } |
42 | |
43 | @Override public void mouseDragged(MouseEvent e) { |
44 | mouseMoved(e); |
45 | } |
46 | |
47 | @Override public void mouseMoved(MouseEvent e) { |
48 | mousePt(ptFromEvent(e)); |
49 | if (verbose) printVars(+mousePt, +calculateTextFromCoordinates); |
50 | if (calculateTextFromCoordinates != null) { |
51 | if (toolTipShowing()) |
52 | text = null; // force moving tooltip around with mouse cursor |
53 | updateText(); |
54 | } |
55 | } |
56 | |
57 | void updateText() swing { |
58 | if (calculateText != null) |
59 | setText(calculateText!); |
60 | else if (calculateTextFromCoordinates != null) |
61 | setText(calculateTextFromCoordinates.get(mousePt)); |
62 | } |
63 | |
64 | public void showToolTip() { |
65 | updateText(); |
66 | showToolTip_noUpdateText(); |
67 | } |
68 | |
69 | public void showToolTip_noUpdateText() { |
70 | if (empty(text)) ret; |
71 | toolTip.setTipText(text); |
72 | popup = PopupFactory.getSharedInstance().getPopup(component, toolTip, x, y); |
73 | popup.show(); |
74 | } |
75 | |
76 | public void mouseExited(MouseEvent e) { |
77 | hideToolTip(); |
78 | } |
79 | |
80 | bool toolTipShowing() { ret popup != null; } |
81 | |
82 | void hideToolTip() { |
83 | if (popup != null) { |
84 | popup.hide(); |
85 | popup = null; |
86 | } |
87 | } |
88 | |
89 | S getText() { ret text; } |
90 | |
91 | void setText(S text) swing { |
92 | if (verbose) printVars("setText", +text); |
93 | if (eq(text, getText())) ret; |
94 | InstantNeverHideToolTip.this.text = text; |
95 | if (verbose) printVars("setText", showing := toolTipShowing(), +x, +y); |
96 | if (toolTipShowing()) { |
97 | hideToolTip(); |
98 | showToolTip_noUpdateText(); |
99 | } |
100 | } |
101 | |
102 | selfType offset(int pixels) { |
103 | ret offset(pt(pixels, pixels)); |
104 | } |
105 | } |
download show line numbers debug dex old transpilations
Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1034548 |
Snippet name: | InstantNeverHideToolTip |
Eternal ID of this version: | #1034548/30 |
Text MD5: | f54186a7221340130ff000b511351e4e |
Author: | stefan |
Category: | javax / gui |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-09-07 04:59:31 |
Source code size: | 2895 bytes / 105 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 344 / 763 |
Version history: | 29 change(s) |
Referenced in: | [show references] |