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