@@ -231,6 +231,11 @@ impl ViewportRepaintInfo {
231
231
232
232
// ----------------------------------------------------------------------------
233
233
234
+ struct DebugText {
235
+ location : String ,
236
+ text : WidgetText ,
237
+ }
238
+
234
239
#[ derive( Default ) ]
235
240
struct ContextImpl {
236
241
/// Since we could have multiple viewport across multiple monitors with
@@ -278,6 +283,8 @@ struct ContextImpl {
278
283
accesskit_node_classes : accesskit:: NodeClassSet ,
279
284
280
285
loaders : Arc < Loaders > ,
286
+
287
+ debug_texts : Vec < DebugText > ,
281
288
}
282
289
283
290
impl ContextImpl {
@@ -1060,6 +1067,31 @@ impl Context {
1060
1067
Self :: layer_painter ( self , LayerId :: debug ( ) )
1061
1068
}
1062
1069
1070
+ /// Print this text next to the cursor at the end of the frame.
1071
+ ///
1072
+ /// If you call this multiple times, the text will be appended.
1073
+ ///
1074
+ /// This only works if compiled with `debug_assertions`.
1075
+ ///
1076
+ /// ```
1077
+ /// # let ctx = egui::Context::default();
1078
+ /// # let state = true;
1079
+ /// ctx.debug_text(format!("State: {state:?}"));
1080
+ /// ```
1081
+ #[ track_caller]
1082
+ pub fn debug_text ( & self , text : impl Into < WidgetText > ) {
1083
+ if cfg ! ( debug_assertions) {
1084
+ let location = std:: panic:: Location :: caller ( ) ;
1085
+ let location = format ! ( "{}:{}" , location. file( ) , location. line( ) ) ;
1086
+ self . write ( |c| {
1087
+ c. debug_texts . push ( DebugText {
1088
+ location,
1089
+ text : text. into ( ) ,
1090
+ } ) ;
1091
+ } ) ;
1092
+ }
1093
+ }
1094
+
1063
1095
/// What operating system are we running on?
1064
1096
///
1065
1097
/// When compiling natively, this is
@@ -1578,6 +1610,63 @@ impl Context {
1578
1610
crate :: gui_zoom:: zoom_with_keyboard ( self ) ;
1579
1611
}
1580
1612
1613
+ let debug_texts = self . write ( |ctx| std:: mem:: take ( & mut ctx. debug_texts ) ) ;
1614
+ if !debug_texts. is_empty ( ) {
1615
+ // Show debug-text next to the cursor.
1616
+ let mut pos = self
1617
+ . input ( |i| i. pointer . latest_pos ( ) )
1618
+ . unwrap_or_else ( || self . screen_rect ( ) . center ( ) )
1619
+ + 8.0 * Vec2 :: Y ;
1620
+
1621
+ let painter = self . debug_painter ( ) ;
1622
+ let where_to_put_background = painter. add ( Shape :: Noop ) ;
1623
+
1624
+ let mut bounding_rect = Rect :: from_points ( & [ pos] ) ;
1625
+
1626
+ let color = Color32 :: GRAY ;
1627
+ let font_id = FontId :: new ( 10.0 , FontFamily :: Proportional ) ;
1628
+
1629
+ for DebugText { location, text } in debug_texts {
1630
+ {
1631
+ // Paint location to left of `pos`:
1632
+ let location_galley =
1633
+ self . fonts ( |f| f. layout ( location, font_id. clone ( ) , color, f32:: INFINITY ) ) ;
1634
+ let location_rect =
1635
+ Align2 :: RIGHT_TOP . anchor_size ( pos - 4.0 * Vec2 :: X , location_galley. size ( ) ) ;
1636
+ painter. galley ( location_rect. min , location_galley, color) ;
1637
+ bounding_rect = bounding_rect. union ( location_rect) ;
1638
+ }
1639
+
1640
+ {
1641
+ // Paint `text` to right of `pos`:
1642
+ let wrap = true ;
1643
+ let available_width = self . screen_rect ( ) . max . x - pos. x ;
1644
+ let galley = text. into_galley_impl (
1645
+ self ,
1646
+ & self . style ( ) ,
1647
+ wrap,
1648
+ available_width,
1649
+ font_id. clone ( ) . into ( ) ,
1650
+ Align :: TOP ,
1651
+ ) ;
1652
+ let rect = Align2 :: LEFT_TOP . anchor_size ( pos, galley. size ( ) ) ;
1653
+ painter. galley ( rect. min , galley, color) ;
1654
+ bounding_rect = bounding_rect. union ( rect) ;
1655
+ }
1656
+
1657
+ pos. y = bounding_rect. max . y + 4.0 ;
1658
+ }
1659
+
1660
+ painter. set (
1661
+ where_to_put_background,
1662
+ Shape :: rect_filled (
1663
+ bounding_rect. expand ( 4.0 ) ,
1664
+ 2.0 ,
1665
+ Color32 :: from_black_alpha ( 192 ) ,
1666
+ ) ,
1667
+ ) ;
1668
+ }
1669
+
1581
1670
self . write ( |ctx| ctx. end_frame ( ) )
1582
1671
}
1583
1672
}
0 commit comments