BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / title_bar.rs

title_bar.rs

title_bar.rs 4.3 KiB Download Raw
1use std::sync::{Arc, OnceLock};
2
3use gpui_kit::assets::IconName;
4use gpui_kit::component::{TitleBar, h_flex};
5use gpui_kit::prelude::FluentBuilder as _;
6use gpui_kit::*;
7
8use super::avatar::{person_avatar, with_presence};
9use super::widgets::{count_badge, icon, symbol};
10use crate::data::Directory;
11use crate::theme;
12
13pub const TITLE_BAR_HEIGHT: f32 = 40.;
14const SEARCH_WIDTH: f32 = 380.;
15const SEARCH_HINT: &str = "Search chats, people and channels";
16const SHORTCUT_HINT: &str = "Ctrl K";
17const OWN_AVATAR_SIZE: f32 = 26.;
18const APP_ICON_SIZE: f32 = 18.;
19const APP_ICON_PNG: &[u8] = include_bytes!("../../assets/icon/teams-fast-256.png");
20
21fn app_icon() -> Arc<Image> {
22 static ICON: OnceLock<Arc<Image>> = OnceLock::new();
23 ICON.get_or_init(|| Arc::new(Image::from_bytes(ImageFormat::Png, APP_ICON_PNG.to_vec())))
24 .clone()
25}
26
27pub fn render_title_bar(
28 directory: &Directory,
29 activity_unread: usize,
30 on_search: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
31 on_saved: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
32 on_notifications: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
33) -> impl IntoElement {
34 let saved = div()
35 .id("title-saved")
36 .occlude()
37 .size(px(28.))
38 .flex()
39 .flex_none()
40 .items_center()
41 .justify_center()
42 .rounded(px(6.))
43 .cursor_pointer()
44 .hover(|button| button.bg(theme::surface()))
45 .child(icon(IconName::Bookmark, 18., theme::text_muted()))
46 .on_click(on_saved);
47 let notifications = div()
48 .id("title-notifications")
49 .relative()
50 .occlude()
51 .size(px(28.))
52 .flex()
53 .flex_none()
54 .items_center()
55 .justify_center()
56 .rounded(px(6.))
57 .cursor_pointer()
58 .hover(|button| button.bg(theme::surface()))
59 .child(symbol("notifications", 18., theme::text_muted()))
60 .when(activity_unread > 0, |bell| {
61 bell.child(
62 div()
63 .absolute()
64 .top(px(-4.))
65 .right(px(-6.))
66 .child(count_badge(
67 activity_unread.min(u32::MAX as usize) as u32,
68 false,
69 )),
70 )
71 })
72 .on_click(on_notifications);
73 let search = h_flex()
74 .id("title-search")
75 .occlude()
76 .w(px(SEARCH_WIDTH))
77 .h(px(28.))
78 .px(px(10.))
79 .gap(px(8.))
80 .items_center()
81 .rounded(px(6.))
82 .bg(theme::surface())
83 .border_1()
84 .border_color(theme::border())
85 .cursor_pointer()
86 .text_size(px(12.))
87 .text_color(theme::text_muted())
88 .hover(|field| field.border_color(theme::border_strong()))
89 .child(icon(IconName::Search, 14., theme::text_muted()))
90 .child(div().flex_1().child(SEARCH_HINT))
91 .child(
92 div()
93 .px(px(6.))
94 .py(px(1.))
95 .rounded(px(4.))
96 .border_1()
97 .border_color(theme::border_strong())
98 .text_size(px(11.))
99 .child(SHORTCUT_HINT),
100 )
101 .on_click(on_search);
102 let own = directory.me.as_ref().map(|me| {
103 let avatar = person_avatar(
104 directory,
105 Some(&me.user_id),
106 &me.display_name,
107 OWN_AVATAR_SIZE,
108 );
109 with_presence(
110 avatar,
111 directory.presence_of(&me.user_id),
112 OWN_AVATAR_SIZE,
113 theme::background(),
114 )
115 });
116 TitleBar::new()
117 .h(px(TITLE_BAR_HEIGHT))
118 .bg(theme::background())
119 .child(
120 h_flex()
121 .flex_1()
122 .h_full()
123 .gap(px(12.))
124 .items_center()
125 .child(img(app_icon()).size(px(APP_ICON_SIZE)).flex_none())
126 .child(
127 div()
128 .text_size(px(13.))
129 .font_weight(FontWeight::SEMIBOLD)
130 .text_color(theme::text())
131 .child(crate::APP_NAME),
132 )
133 .child(div().flex_1().flex().justify_center().child(search))
134 .child(saved)
135 .child(notifications)
136 .children(own)
137 .child(div().w(px(4.))),
138 )
139}