Skip to main content

iced_widget/pane_grid/
title_bar.rs

1use crate::container;
2use crate::core::layout;
3use crate::core::mouse;
4use crate::core::overlay;
5use crate::core::renderer;
6use crate::core::widget::{self, Tree};
7use crate::core::{self, Event, Layout, Padding, Point, Rectangle, Shell, Size, Vector, Widget};
8use crate::pane_grid::controls::Controls;
9
10/// The title bar of a [`Pane`].
11///
12/// [`Pane`]: super::Pane
13pub struct TitleBar<
14    'a,
15    Message,
16    C = crate::Element<'a, Message>,
17    Theme = crate::Theme,
18    Renderer = crate::Renderer,
19> where
20    Theme: container::Catalog,
21{
22    content: C,
23    controls: Option<Controls<'a, Message, Theme, Renderer>>,
24    padding: Padding,
25    always_show_controls: bool,
26    class: Theme::Class<'a>,
27}
28
29impl<'a, Message, C, Theme, Renderer> TitleBar<'a, Message, C, Theme, Renderer>
30where
31    Theme: container::Catalog,
32{
33    /// Creates a new [`TitleBar`] with the given content.
34    pub fn new(content: C) -> Self {
35        Self {
36            content,
37            controls: None,
38            padding: Padding::ZERO,
39            always_show_controls: false,
40            class: Theme::default(),
41        }
42    }
43
44    /// Sets the controls of the [`TitleBar`].
45    pub fn controls(mut self, controls: impl Into<Controls<'a, Message, Theme, Renderer>>) -> Self {
46        self.controls = Some(controls.into());
47        self
48    }
49
50    /// Sets the [`Padding`] of the [`TitleBar`].
51    pub fn padding<P: Into<Padding>>(mut self, padding: P) -> Self {
52        self.padding = padding.into();
53        self
54    }
55
56    /// Sets whether or not the [`controls`] attached to this [`TitleBar`] are
57    /// always visible.
58    ///
59    /// By default, the controls are only visible when the [`Pane`] of this
60    /// [`TitleBar`] is hovered.
61    ///
62    /// [`controls`]: Self::controls
63    /// [`Pane`]: super::Pane
64    pub fn always_show_controls(mut self) -> Self {
65        self.always_show_controls = true;
66        self
67    }
68
69    /// Sets the style of the [`TitleBar`].
70    #[must_use]
71    pub fn style(mut self, style: impl Fn(&Theme) -> container::Style + 'a) -> Self
72    where
73        Theme::Class<'a>: From<container::StyleFn<'a, Theme>>,
74    {
75        self.class = (Box::new(style) as container::StyleFn<'a, Theme>).into();
76        self
77    }
78
79    /// Sets the style class of the [`TitleBar`].
80    #[cfg(feature = "advanced")]
81    #[must_use]
82    pub fn class(mut self, class: impl Into<Theme::Class<'a>>) -> Self {
83        self.class = class.into();
84        self
85    }
86}
87
88impl<Message, C, Theme, Renderer> TitleBar<'_, Message, C, Theme, Renderer>
89where
90    C: Widget<Message, Theme, Renderer>,
91    Theme: container::Catalog,
92    Renderer: core::Renderer,
93{
94    pub(super) fn state(&self) -> Tree {
95        let children = match self.controls.as_ref() {
96            Some(controls) => match controls.compact.as_ref() {
97                Some(compact) => vec![
98                    Tree::new(&self.content),
99                    Tree::new(&controls.full),
100                    Tree::new(compact),
101                ],
102                None => vec![
103                    Tree::new(&self.content),
104                    Tree::new(&controls.full),
105                    Tree::empty(),
106                ],
107            },
108            None => {
109                vec![Tree::new(&self.content), Tree::empty(), Tree::empty()]
110            }
111        };
112
113        Tree {
114            children,
115            ..Tree::empty()
116        }
117    }
118
119    pub(super) fn diff(&mut self, tree: &mut Tree) {
120        if tree.children.len() != 3 {
121            *tree = self.state();
122        }
123
124        if let Some(controls) = &mut self.controls {
125            if let Some(compact) = &mut controls.compact {
126                tree.children[2].diff(compact);
127            }
128
129            tree.children[1].diff(&mut controls.full);
130        }
131
132        tree.children[0].diff(&mut self.content);
133    }
134
135    /// Draws the [`TitleBar`] with the provided [`Renderer`] and [`Layout`].
136    ///
137    /// [`Renderer`]: core::Renderer
138    pub fn draw(
139        &self,
140        tree: &Tree,
141        renderer: &mut Renderer,
142        theme: &Theme,
143        inherited_style: &renderer::Style,
144        layout: Layout,
145        cursor: mouse::Cursor,
146        viewport: &Rectangle,
147        show_controls: bool,
148    ) {
149        let bounds = layout.bounds();
150        let style = theme.style(&self.class);
151
152        let inherited_style = renderer::Style {
153            text_color: style.text_color.unwrap_or(inherited_style.text_color),
154        };
155
156        container::draw_background(renderer, &style, bounds);
157
158        let mut children = layout.iter(&tree.children);
159        let (title_layout, title_tree) = children.next().unwrap();
160        let (controls_layout, controls_tree) = children.next().unwrap();
161        let (compact_layout, compact_tree) = children.next().unwrap();
162        let mut show_title = true;
163
164        let padded_width = layout.bounds().width - self.padding.left + self.padding.right;
165
166        if let Some(controls) = &self.controls
167            && (show_controls || self.always_show_controls)
168        {
169            if title_layout.bounds().width + controls_layout.bounds().width > padded_width {
170                if let Some(compact) = controls.compact.as_ref() {
171                    compact.draw(
172                        compact_tree,
173                        renderer,
174                        theme,
175                        &inherited_style,
176                        compact_layout,
177                        cursor,
178                        viewport,
179                    );
180                } else {
181                    show_title = false;
182
183                    controls.full.draw(
184                        controls_tree,
185                        renderer,
186                        theme,
187                        &inherited_style,
188                        controls_layout,
189                        cursor,
190                        viewport,
191                    );
192                }
193            } else {
194                controls.full.draw(
195                    controls_tree,
196                    renderer,
197                    theme,
198                    &inherited_style,
199                    controls_layout,
200                    cursor,
201                    viewport,
202                );
203            }
204        }
205
206        if show_title {
207            self.content.draw(
208                title_tree,
209                renderer,
210                theme,
211                &inherited_style,
212                title_layout,
213                cursor,
214                viewport,
215            );
216        }
217    }
218
219    /// Returns whether the mouse cursor is over the pick area of the
220    /// [`TitleBar`] or not.
221    ///
222    /// The whole [`TitleBar`] is a pick area, except its controls.
223    pub fn is_over_pick_area(&self, tree: &Tree, layout: Layout, cursor_position: Point) -> bool {
224        if layout.bounds().contains(cursor_position) {
225            let mut children = layout.iter(&tree.children);
226            let (title_layout, _) = children.next().unwrap();
227            let (controls_layout, _) = children.next().unwrap();
228            let (compact_layout, _) = children.next().unwrap();
229
230            let padded_width = layout.bounds().width - self.padding.left + self.padding.right;
231
232            if let Some(controls) = self.controls.as_ref() {
233                if title_layout.bounds().width + controls_layout.bounds().width > padded_width {
234                    if controls.compact.is_some() {
235                        !compact_layout.bounds().contains(cursor_position)
236                            && !title_layout.bounds().contains(cursor_position)
237                    } else {
238                        !controls_layout.bounds().contains(cursor_position)
239                    }
240                } else {
241                    !controls_layout.bounds().contains(cursor_position)
242                        && !title_layout.bounds().contains(cursor_position)
243                }
244            } else {
245                !title_layout.bounds().contains(cursor_position)
246            }
247        } else {
248            false
249        }
250    }
251
252    pub(crate) fn layout(&mut self, tree: &mut Tree, renderer: &Renderer, limits: &layout::Limits) {
253        let limits = limits.shrink(self.padding);
254        let max_size = limits.max;
255
256        self.content.layout(
257            &mut tree.children[0],
258            renderer,
259            &layout::Limits::new(Size::ZERO, max_size),
260        );
261
262        let title_size = tree.children[0].size;
263
264        let inner_size = if let Some(controls) = &mut self.controls {
265            controls.full.layout(
266                &mut tree.children[1],
267                renderer,
268                &layout::Limits::new(Size::ZERO, max_size),
269            );
270
271            let controls_size = tree.children[1].size;
272
273            if title_size.width + controls_size.width > max_size.width {
274                if let Some(compact) = controls.compact.as_mut() {
275                    compact.layout(
276                        &mut tree.children[2],
277                        renderer,
278                        &layout::Limits::new(Size::ZERO, max_size),
279                    );
280
281                    let compact_size = tree.children[2].size;
282                    let space_before_controls = max_size.width - compact_size.width;
283
284                    tree.children[1].translation = Vector::ZERO;
285                    tree.children[2].translation = Vector::new(space_before_controls, 0.0);
286
287                    Size::new(max_size.width, title_size.height.max(compact_size.height))
288                } else {
289                    let space_before_controls = max_size.width - controls_size.width;
290
291                    tree.children[1].translation = Vector::new(space_before_controls, 0.0);
292
293                    Size::new(max_size.width, title_size.height.max(controls_size.height))
294                }
295            } else {
296                let space_before_controls = max_size.width - controls_size.width;
297
298                tree.children[1].translation = Vector::new(space_before_controls, 0.0);
299
300                Size::new(max_size.width, title_size.height.max(controls_size.height))
301            }
302        } else {
303            Size::new(max_size.width, title_size.height)
304        };
305
306        tree.children[0].translation = Vector::ZERO;
307
308        for child in &mut tree.children {
309            child.translation += Vector::new(self.padding.left, self.padding.top);
310        }
311
312        tree.size = inner_size.expand(self.padding);
313    }
314
315    pub(crate) fn operate(
316        &mut self,
317        tree: &mut Tree,
318        layout: Layout,
319        viewport: &Rectangle,
320        renderer: &Renderer,
321        operation: &mut dyn widget::Operation,
322    ) {
323        let mut children = layout.iter_mut(&mut tree.children);
324        let (title_layout, title_tree) = children.next().unwrap();
325        let (controls_layout, controls_tree) = children.next().unwrap();
326        let (compact_layout, compact_tree) = children.next().unwrap();
327        let padded_width = layout.bounds().width - self.padding.left + self.padding.right;
328        let mut show_title = true;
329
330        if let Some(controls) = &mut self.controls {
331            if title_layout.bounds().width + controls_layout.bounds().width > padded_width {
332                if let Some(compact) = controls.compact.as_mut() {
333                    compact.operate(compact_tree, compact_layout, viewport, renderer, operation);
334                } else {
335                    show_title = false;
336
337                    controls.full.operate(
338                        controls_tree,
339                        controls_layout,
340                        viewport,
341                        renderer,
342                        operation,
343                    );
344                }
345            } else {
346                controls.full.operate(
347                    controls_tree,
348                    controls_layout,
349                    viewport,
350                    renderer,
351                    operation,
352                );
353            }
354        };
355
356        if show_title {
357            self.content
358                .operate(title_tree, title_layout, viewport, renderer, operation);
359        }
360    }
361
362    pub(crate) fn update(
363        &mut self,
364        tree: &mut Tree,
365        event: &Event,
366        layout: Layout,
367        cursor: mouse::Cursor,
368        renderer: &Renderer,
369        shell: &mut Shell<'_, Message>,
370        viewport: &Rectangle,
371    ) {
372        let mut children = layout.iter_mut(&mut tree.children);
373        let (title_layout, title_tree) = children.next().unwrap();
374        let (controls_layout, controls_tree) = children.next().unwrap();
375        let (compact_layout, compact_tree) = children.next().unwrap();
376        let padded_width = layout.bounds().width - self.padding.left + self.padding.right;
377        let mut show_title = true;
378
379        if let Some(controls) = &mut self.controls {
380            if title_layout.bounds().width + controls_layout.bounds().width > padded_width {
381                if let Some(compact) = controls.compact.as_mut() {
382                    compact.update(
383                        compact_tree,
384                        event,
385                        compact_layout,
386                        cursor,
387                        renderer,
388                        shell,
389                        viewport,
390                    );
391                } else {
392                    show_title = false;
393
394                    controls.full.update(
395                        controls_tree,
396                        event,
397                        controls_layout,
398                        cursor,
399                        renderer,
400                        shell,
401                        viewport,
402                    );
403                }
404            } else {
405                controls.full.update(
406                    controls_tree,
407                    event,
408                    controls_layout,
409                    cursor,
410                    renderer,
411                    shell,
412                    viewport,
413                );
414            }
415        }
416
417        if show_title {
418            self.content.update(
419                title_tree,
420                event,
421                title_layout,
422                cursor,
423                renderer,
424                shell,
425                viewport,
426            );
427        }
428    }
429
430    pub(crate) fn mouse_interaction(
431        &self,
432        tree: &Tree,
433        layout: Layout,
434        cursor: mouse::Cursor,
435        viewport: &Rectangle,
436        renderer: &Renderer,
437    ) -> mouse::Interaction {
438        let mut children = layout.iter(&tree.children);
439        let (title_layout, title_tree) = children.next().unwrap();
440        let (controls_layout, controls_tree) = children.next().unwrap();
441        let (compact_layout, compact_tree) = children.next().unwrap();
442        let padded_width = layout.bounds().width - self.padding.left + self.padding.right;
443
444        let title_interaction =
445            self.content
446                .mouse_interaction(title_tree, title_layout, cursor, viewport, renderer);
447
448        if let Some(controls) = &self.controls {
449            let controls_interaction = controls.full.mouse_interaction(
450                controls_tree,
451                controls_layout,
452                cursor,
453                viewport,
454                renderer,
455            );
456
457            if title_layout.bounds().width + controls_layout.bounds().width > padded_width {
458                if let Some(compact) = controls.compact.as_ref() {
459                    let compact_interaction = compact.mouse_interaction(
460                        compact_tree,
461                        compact_layout,
462                        cursor,
463                        viewport,
464                        renderer,
465                    );
466
467                    compact_interaction.max(title_interaction)
468                } else {
469                    controls_interaction
470                }
471            } else {
472                controls_interaction.max(title_interaction)
473            }
474        } else {
475            title_interaction
476        }
477    }
478
479    pub(crate) fn overlay<'b>(
480        &'b mut self,
481        tree: &'b mut Tree,
482        layout: Layout,
483        renderer: &Renderer,
484        viewport: &Rectangle,
485        translation: Vector,
486        window: Size,
487    ) -> Vec<overlay::Element<'b, Message, Theme, Renderer>> {
488        let mut children = layout.iter_mut(&mut tree.children);
489        let (title_layout, title_tree) = children.next().unwrap();
490        let (controls_layout, controls_tree) = children.next().unwrap();
491        let (compact_layout, compact_tree) = children.next().unwrap();
492        let padded_width = layout.bounds().width - self.padding.left + self.padding.right;
493
494        let Self {
495            content, controls, ..
496        } = self;
497
498        let mut overlays = content.overlay(
499            title_tree,
500            title_layout,
501            renderer,
502            viewport,
503            translation,
504            window,
505        );
506
507        if let Some(controls) = controls {
508            if title_layout.bounds().width + controls_layout.bounds().width > padded_width {
509                if let Some(compact) = &mut controls.compact {
510                    overlays.extend(compact.overlay(
511                        compact_tree,
512                        compact_layout,
513                        renderer,
514                        viewport,
515                        translation,
516                        window,
517                    ));
518                } else {
519                    overlays.extend(controls.full.overlay(
520                        controls_tree,
521                        controls_layout,
522                        renderer,
523                        viewport,
524                        translation,
525                        window,
526                    ));
527                }
528            } else {
529                overlays.extend(controls.full.overlay(
530                    controls_tree,
531                    controls_layout,
532                    renderer,
533                    viewport,
534                    translation,
535                    window,
536                ));
537            }
538        }
539
540        overlays
541    }
542}