@@ -54,7 +54,8 @@ impl FromWorld for TooltipRoot {
54
54
font : FONT_HANDLE ,
55
55
..default ( )
56
56
} ,
57
- ) ,
57
+ )
58
+ . with_text_justify ( JustifyText :: Center ) ,
58
59
// TODO: Adjustable font sizes in ThemeConfig
59
60
DynamicFontSize :: new ( Px ( 16.0 ) ) ,
60
61
ThemeColorForText ( vec ! [ ThemeColor :: BodyText ] ) ,
@@ -85,10 +86,12 @@ pub struct Tooltip {
85
86
impl Configure for Tooltip {
86
87
fn configure ( app : & mut App ) {
87
88
app. register_type :: < Self > ( ) ;
88
- app. add_systems ( Update , show_tooltip_on_hover. in_set ( UpdateSet :: SyncLate ) ) ;
89
+ app. add_systems ( Update , show_tooltip_on_hover. in_set ( UpdateSet :: RecordInput ) ) ;
89
90
}
90
91
}
91
92
93
+ // TODO: Set text in an early system, then set position in a late system.
94
+ // That way the tooltip can use its own calculated size to support centering.
92
95
fn show_tooltip_on_hover (
93
96
window_root : Res < WindowRoot > ,
94
97
window_query : Query < & Window > ,
@@ -97,35 +100,41 @@ fn show_tooltip_on_hover(
97
100
mut text_query : Query < & mut Text > ,
98
101
interaction_query : Query < ( & Interaction , & Tooltip , & GlobalTransform , & Node ) > ,
99
102
) {
100
- let window = r ! ( window_query. get( window_root. primary) ) ;
101
103
let ( mut visibility, mut style) = r ! ( container_query. get_mut( tooltip_root. container) ) ;
102
104
let mut text = r ! ( text_query. get_mut( tooltip_root. text) ) ;
105
+ let window = r ! ( window_query. get( window_root. primary) ) ;
106
+ let width = window. width ( ) ;
107
+ let height = window. height ( ) ;
103
108
104
109
for ( interaction, tooltip, gt, node) in & interaction_query {
110
+ // Skip nodes that are not hovered.
105
111
if matches ! ( interaction, Interaction :: None ) {
106
112
* visibility = Visibility :: Hidden ;
107
113
continue ;
108
114
}
109
115
110
- let rect = node. logical_rect ( gt) ;
116
+ // Set the tooltip text and make it visible.
117
+ * visibility = Visibility :: Inherited ;
118
+ text. sections [ 0 ] . value . clone_from ( & tooltip. text ) ;
111
119
112
- let width = window . width ( ) ;
113
- let height = window . height ( ) ;
120
+ // Get the left, right, top, bottom of the target node.
121
+ let rect = node . logical_rect ( gt ) ;
114
122
let ( left, right, top, bottom) = (
115
123
rect. min . x + tooltip. offset . x ,
116
124
rect. max . x + tooltip. offset . x ,
117
125
rect. min . y + tooltip. offset . y ,
118
126
rect. max . y + tooltip. offset . y ,
119
127
) ;
120
128
121
- * visibility = Visibility :: Inherited ;
122
- text. sections [ 0 ] . value . clone_from ( & tooltip. text ) ;
129
+ // Set the left, right, top, bottom of the tooltip node.
123
130
( style. left , style. right , style. top , style. bottom ) = match tooltip. side {
124
131
TooltipSide :: Left => ( Auto , Px ( width - left) , Auto , Px ( height - bottom) ) ,
125
132
TooltipSide :: Right => ( Px ( right) , Auto , Auto , Px ( height - bottom) ) ,
126
133
TooltipSide :: Top => ( Px ( left) , Auto , Auto , Px ( height - top) ) ,
127
134
TooltipSide :: Bottom => ( Px ( left) , Auto , Px ( bottom) , Auto ) ,
128
135
} ;
136
+
137
+ // Exit early (because there's only one tooltip).
129
138
return ;
130
139
}
131
140
}
0 commit comments