platform_title_bar.rs

  1use gpui::{
  2    AnyElement, Context, Decorations, Entity, Hsla, InteractiveElement, IntoElement, MouseButton,
  3    ParentElement, Pixels, StatefulInteractiveElement, Styled, Window, WindowControlArea, div, px,
  4};
  5use smallvec::SmallVec;
  6use std::mem;
  7use ui::prelude::*;
  8
  9use crate::{
 10    platforms::{platform_linux, platform_mac, platform_windows},
 11    system_window_tabs::SystemWindowTabs,
 12};
 13
 14pub struct PlatformTitleBar {
 15    id: ElementId,
 16    platform_style: PlatformStyle,
 17    children: SmallVec<[AnyElement; 2]>,
 18    should_move: bool,
 19    system_window_tabs: Entity<SystemWindowTabs>,
 20}
 21
 22impl PlatformTitleBar {
 23    pub fn new(id: impl Into<ElementId>, cx: &mut Context<Self>) -> Self {
 24        let platform_style = PlatformStyle::platform();
 25        let system_window_tabs = cx.new(|_cx| SystemWindowTabs::new());
 26
 27        Self {
 28            id: id.into(),
 29            platform_style,
 30            children: SmallVec::new(),
 31            should_move: false,
 32            system_window_tabs,
 33        }
 34    }
 35
 36    #[cfg(not(target_os = "windows"))]
 37    pub fn height(window: &mut Window) -> Pixels {
 38        (1.75 * window.rem_size()).max(px(34.))
 39    }
 40
 41    #[cfg(target_os = "windows")]
 42    pub fn height(_window: &mut Window) -> Pixels {
 43        // todo(windows) instead of hard coded size report the actual size to the Windows platform API
 44        px(32.)
 45    }
 46
 47    pub fn title_bar_color(&self, window: &mut Window, cx: &mut Context<Self>) -> Hsla {
 48        if cfg!(any(target_os = "linux", target_os = "freebsd")) {
 49            if window.is_window_active() && !self.should_move {
 50                cx.theme().colors().title_bar_background
 51            } else {
 52                cx.theme().colors().title_bar_inactive_background
 53            }
 54        } else {
 55            cx.theme().colors().title_bar_background
 56        }
 57    }
 58
 59    pub fn set_children<T>(&mut self, children: T)
 60    where
 61        T: IntoIterator<Item = AnyElement>,
 62    {
 63        self.children = children.into_iter().collect();
 64    }
 65}
 66
 67impl Render for PlatformTitleBar {
 68    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
 69        let supported_controls = window.window_controls();
 70        let decorations = window.window_decorations();
 71        let height = Self::height(window);
 72        let titlebar_color = self.title_bar_color(window, cx);
 73        let close_action = Box::new(workspace::CloseWindow);
 74        let children = mem::take(&mut self.children);
 75
 76        let title_bar = h_flex()
 77            .window_control_area(WindowControlArea::Drag)
 78            .w_full()
 79            .h(height)
 80            .map(|this| {
 81                if window.is_fullscreen() {
 82                    this.pl_2()
 83                } else if self.platform_style == PlatformStyle::Mac {
 84                    this.pl(px(platform_mac::TRAFFIC_LIGHT_PADDING))
 85                } else {
 86                    this.pl_2()
 87                }
 88            })
 89            .map(|el| match decorations {
 90                Decorations::Server => el,
 91                Decorations::Client { tiling, .. } => el
 92                    .when(!(tiling.top || tiling.right), |el| {
 93                        el.rounded_tr(theme::CLIENT_SIDE_DECORATION_ROUNDING)
 94                    })
 95                    .when(!(tiling.top || tiling.left), |el| {
 96                        el.rounded_tl(theme::CLIENT_SIDE_DECORATION_ROUNDING)
 97                    })
 98                    // this border is to avoid a transparent gap in the rounded corners
 99                    .mt(px(-1.))
100                    .border(px(1.))
101                    .border_color(titlebar_color),
102            })
103            .bg(titlebar_color)
104            .content_stretch()
105            .child(
106                div()
107                    .id(self.id.clone())
108                    .flex()
109                    .flex_row()
110                    .items_center()
111                    .justify_between()
112                    .overflow_x_hidden()
113                    .w_full()
114                    // Note: On Windows the title bar behavior is handled by the platform implementation.
115                    .when(self.platform_style == PlatformStyle::Mac, |this| {
116                        this.on_click(|event, window, _| {
117                            if event.click_count() == 2 {
118                                window.titlebar_double_click();
119                            }
120                        })
121                    })
122                    .when(self.platform_style == PlatformStyle::Linux, |this| {
123                        this.on_click(|event, window, _| {
124                            if event.click_count() == 2 {
125                                window.zoom_window();
126                            }
127                        })
128                    })
129                    .children(children),
130            )
131            .when(!window.is_fullscreen(), |title_bar| {
132                match self.platform_style {
133                    PlatformStyle::Mac => title_bar,
134                    PlatformStyle::Linux => {
135                        if matches!(decorations, Decorations::Client { .. }) {
136                            title_bar
137                                .child(platform_linux::LinuxWindowControls::new(close_action))
138                                .when(supported_controls.window_menu, |titlebar| {
139                                    titlebar
140                                        .on_mouse_down(MouseButton::Right, move |ev, window, _| {
141                                            window.show_window_menu(ev.position)
142                                        })
143                                })
144                                .on_mouse_move(cx.listener(move |this, _ev, window, _| {
145                                    if this.should_move {
146                                        this.should_move = false;
147                                        window.start_window_move();
148                                    }
149                                }))
150                                .on_mouse_down_out(cx.listener(move |this, _ev, _window, _cx| {
151                                    this.should_move = false;
152                                }))
153                                .on_mouse_up(
154                                    MouseButton::Left,
155                                    cx.listener(move |this, _ev, _window, _cx| {
156                                        this.should_move = false;
157                                    }),
158                                )
159                                .on_mouse_down(
160                                    MouseButton::Left,
161                                    cx.listener(move |this, _ev, _window, _cx| {
162                                        this.should_move = true;
163                                    }),
164                                )
165                        } else {
166                            title_bar
167                        }
168                    }
169                    PlatformStyle::Windows => {
170                        title_bar.child(platform_windows::WindowsWindowControls::new(height))
171                    }
172                }
173            });
174
175        v_flex()
176            .w_full()
177            .child(title_bar)
178            .child(self.system_window_tabs.clone().into_any_element())
179    }
180}
181
182impl ParentElement for PlatformTitleBar {
183    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
184        self.children.extend(elements)
185    }
186}