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                    .mb(px(-1.))
101                    .border(px(1.))
102                    .border_color(titlebar_color),
103            })
104            .bg(titlebar_color)
105            .content_stretch()
106            .child(
107                div()
108                    .id(self.id.clone())
109                    .flex()
110                    .flex_row()
111                    .items_center()
112                    .justify_between()
113                    .overflow_x_hidden()
114                    .w_full()
115                    // Note: On Windows the title bar behavior is handled by the platform implementation.
116                    .when(self.platform_style == PlatformStyle::Mac, |this| {
117                        this.on_click(|event, window, _| {
118                            if event.click_count() == 2 {
119                                window.titlebar_double_click();
120                            }
121                        })
122                    })
123                    .when(self.platform_style == PlatformStyle::Linux, |this| {
124                        this.on_click(|event, window, _| {
125                            if event.click_count() == 2 {
126                                window.zoom_window();
127                            }
128                        })
129                    })
130                    .children(children),
131            )
132            .when(!window.is_fullscreen(), |title_bar| {
133                match self.platform_style {
134                    PlatformStyle::Mac => title_bar,
135                    PlatformStyle::Linux => {
136                        if matches!(decorations, Decorations::Client { .. }) {
137                            title_bar
138                                .child(platform_linux::LinuxWindowControls::new(close_action))
139                                .when(supported_controls.window_menu, |titlebar| {
140                                    titlebar
141                                        .on_mouse_down(MouseButton::Right, move |ev, window, _| {
142                                            window.show_window_menu(ev.position)
143                                        })
144                                })
145                                .on_mouse_move(cx.listener(move |this, _ev, window, _| {
146                                    if this.should_move {
147                                        this.should_move = false;
148                                        window.start_window_move();
149                                    }
150                                }))
151                                .on_mouse_down_out(cx.listener(move |this, _ev, _window, _cx| {
152                                    this.should_move = false;
153                                }))
154                                .on_mouse_up(
155                                    MouseButton::Left,
156                                    cx.listener(move |this, _ev, _window, _cx| {
157                                        this.should_move = false;
158                                    }),
159                                )
160                                .on_mouse_down(
161                                    MouseButton::Left,
162                                    cx.listener(move |this, _ev, _window, _cx| {
163                                        this.should_move = true;
164                                    }),
165                                )
166                        } else {
167                            title_bar
168                        }
169                    }
170                    PlatformStyle::Windows => {
171                        title_bar.child(platform_windows::WindowsWindowControls::new(height))
172                    }
173                }
174            });
175
176        v_flex()
177            .w_full()
178            .child(title_bar)
179            .child(self.system_window_tabs.clone().into_any_element())
180    }
181}
182
183impl ParentElement for PlatformTitleBar {
184    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
185        self.children.extend(elements)
186    }
187}