11use crate :: event:: { self , Event } ;
22use crate :: layout;
33use crate :: pane_grid;
4- use crate :: { Clipboard , Element , Hasher , Layout , Point , Rectangle , Size } ;
4+ use crate :: { Clipboard , Element , Hasher , Layout , Point , Size } ;
55
66/// The title bar of a [`Pane`].
77///
88/// [`Pane`]: crate::widget::pane_grid::Pane
99#[ allow( missing_debug_implementations) ]
1010pub struct TitleBar < ' a , Message , Renderer : pane_grid:: Renderer > {
11- title : String ,
12- title_size : Option < u16 > ,
11+ content : Element < ' a , Message , Renderer > ,
1312 controls : Option < Element < ' a , Message , Renderer > > ,
1413 padding : u16 ,
1514 always_show_controls : bool ,
@@ -20,24 +19,20 @@ impl<'a, Message, Renderer> TitleBar<'a, Message, Renderer>
2019where
2120 Renderer : pane_grid:: Renderer ,
2221{
23- /// Creates a new [`TitleBar`] with the given title.
24- pub fn new ( title : impl Into < String > ) -> Self {
22+ /// Creates a new [`TitleBar`] with the given content.
23+ pub fn new < E > ( content : E ) -> Self
24+ where
25+ E : Into < Element < ' a , Message , Renderer > > ,
26+ {
2527 Self {
26- title : title. into ( ) ,
27- title_size : None ,
28+ content : content. into ( ) ,
2829 controls : None ,
2930 padding : 0 ,
3031 always_show_controls : false ,
3132 style : Renderer :: Style :: default ( ) ,
3233 }
3334 }
3435
35- /// Sets the size of the title of the [`TitleBar`].
36- pub fn title_size ( mut self , size : u16 ) -> Self {
37- self . title_size = Some ( size) ;
38- self
39- }
40-
4136 /// Sets the controls of the [`TitleBar`].
4237 pub fn controls (
4338 mut self ,
@@ -91,48 +86,29 @@ where
9186 let mut children = layout. children ( ) ;
9287 let padded = children. next ( ) . unwrap ( ) ;
9388
94- if let Some ( controls) = & self . controls {
95- let mut children = padded. children ( ) ;
96- let title_layout = children. next ( ) . unwrap ( ) ;
89+ let mut children = padded. children ( ) ;
90+ let title_layout = children. next ( ) . unwrap ( ) ;
91+
92+ let controls = if let Some ( controls) = & self . controls {
9793 let controls_layout = children. next ( ) . unwrap ( ) ;
9894
99- let ( title_bounds, controls) =
100- if show_controls || self . always_show_controls {
101- ( title_layout. bounds ( ) , Some ( ( controls, controls_layout) ) )
102- } else {
103- (
104- Rectangle {
105- width : padded. bounds ( ) . width ,
106- ..title_layout. bounds ( )
107- } ,
108- None ,
109- )
110- } ;
111-
112- renderer. draw_title_bar (
113- defaults,
114- layout. bounds ( ) ,
115- & self . style ,
116- & self . title ,
117- self . title_size . unwrap_or ( renderer. default_size ( ) ) ,
118- Renderer :: Font :: default ( ) ,
119- title_bounds,
120- controls,
121- cursor_position,
122- )
95+ if show_controls || self . always_show_controls {
96+ Some ( ( controls, controls_layout) )
97+ } else {
98+ None
99+ }
123100 } else {
124- renderer. draw_title_bar :: < ( ) > (
125- defaults,
126- layout. bounds ( ) ,
127- & self . style ,
128- & self . title ,
129- self . title_size . unwrap_or ( renderer. default_size ( ) ) ,
130- Renderer :: Font :: default ( ) ,
131- padded. bounds ( ) ,
132- None ,
133- cursor_position,
134- )
135- }
101+ None
102+ } ;
103+
104+ renderer. draw_title_bar (
105+ defaults,
106+ layout. bounds ( ) ,
107+ & self . style ,
108+ ( & self . content , title_layout) ,
109+ controls,
110+ cursor_position,
111+ )
136112 }
137113
138114 /// Returns whether the mouse cursor is over the pick area of the
@@ -165,8 +141,7 @@ where
165141 pub ( crate ) fn hash_layout ( & self , hasher : & mut Hasher ) {
166142 use std:: hash:: Hash ;
167143
168- self . title . hash ( hasher) ;
169- self . title_size . hash ( hasher) ;
144+ self . content . hash_layout ( hasher) ;
170145 self . padding . hash ( hasher) ;
171146 }
172147
@@ -179,15 +154,10 @@ where
179154 let limits = limits. pad ( padding) ;
180155 let max_size = limits. max ( ) ;
181156
182- let title_size = self . title_size . unwrap_or ( renderer. default_size ( ) ) ;
183- let title_font = Renderer :: Font :: default ( ) ;
184-
185- let ( title_width, title_height) = renderer. measure (
186- & self . title ,
187- title_size,
188- title_font,
189- Size :: new ( f32:: INFINITY , max_size. height ) ,
190- ) ;
157+ let title_layout = self
158+ . content
159+ . layout ( renderer, & layout:: Limits :: new ( Size :: ZERO , max_size) ) ;
160+ let title_size = title_layout. size ( ) ;
191161
192162 let mut node = if let Some ( controls) = & self . controls {
193163 let mut controls_layout = controls
@@ -196,24 +166,19 @@ where
196166 let controls_size = controls_layout. size ( ) ;
197167 let space_before_controls = max_size. width - controls_size. width ;
198168
199- let mut title_layout = layout:: Node :: new ( Size :: new (
200- title_width. min ( space_before_controls) ,
201- title_height,
202- ) ) ;
203-
204- let title_size = title_layout. size ( ) ;
205169 let height = title_size. height . max ( controls_size. height ) ;
206170
207- title_layout
208- . move_to ( Point :: new ( 0.0 , ( height - title_size. height ) / 2.0 ) ) ;
209171 controls_layout. move_to ( Point :: new ( space_before_controls, 0.0 ) ) ;
210172
211173 layout:: Node :: with_children (
212174 Size :: new ( max_size. width , height) ,
213175 vec ! [ title_layout, controls_layout] ,
214176 )
215177 } else {
216- layout:: Node :: new ( Size :: new ( max_size. width , title_height) )
178+ layout:: Node :: with_children (
179+ Size :: new ( max_size. width , title_size. height ) ,
180+ vec ! [ title_layout] ,
181+ )
217182 } ;
218183
219184 node. move_to ( Point :: new ( padding, padding) ) ;
0 commit comments