1mod application_menu;
2pub mod collab;
3mod onboarding_banner;
4pub mod platform_title_bar;
5mod platforms;
6mod system_window_tabs;
7mod title_bar_settings;
8
9#[cfg(feature = "stories")]
10mod stories;
11
12use crate::{
13 application_menu::{ApplicationMenu, show_menus},
14 platform_title_bar::PlatformTitleBar,
15 system_window_tabs::SystemWindowTabs,
16};
17
18#[cfg(not(target_os = "macos"))]
19use crate::application_menu::{
20 ActivateDirection, ActivateMenuLeft, ActivateMenuRight, OpenApplicationMenu,
21};
22
23use auto_update::AutoUpdateStatus;
24use call::ActiveCall;
25use client::{Client, UserStore, zed_urls};
26use cloud_llm_client::{Plan, PlanV1, PlanV2};
27use gpui::{
28 Action, AnyElement, App, Context, Corner, Element, Entity, Focusable, InteractiveElement,
29 IntoElement, MouseButton, ParentElement, Render, StatefulInteractiveElement, Styled,
30 Subscription, WeakEntity, Window, actions, div,
31};
32use onboarding_banner::OnboardingBanner;
33use project::{
34 Project, WorktreeSettings, git_store::GitStoreEvent, trusted_worktrees::TrustedWorktrees,
35};
36use remote::RemoteConnectionOptions;
37use settings::{Settings, SettingsLocation};
38use std::sync::Arc;
39use theme::ActiveTheme;
40use title_bar_settings::TitleBarSettings;
41use ui::{
42 Avatar, ButtonLike, Chip, ContextMenu, IconWithIndicator, Indicator, PopoverMenu,
43 PopoverMenuHandle, TintColor, Tooltip, prelude::*,
44};
45use util::{ResultExt, rel_path::RelPath};
46use workspace::{ToggleWorktreeSecurity, Workspace, notifications::NotifyResultExt};
47use zed_actions::{OpenRecent, OpenRemote};
48
49pub use onboarding_banner::restore_banner;
50
51#[cfg(feature = "stories")]
52pub use stories::*;
53
54const MAX_PROJECT_NAME_LENGTH: usize = 40;
55const MAX_BRANCH_NAME_LENGTH: usize = 40;
56const MAX_SHORT_SHA_LENGTH: usize = 8;
57
58actions!(
59 collab,
60 [
61 /// Toggles the user menu dropdown.
62 ToggleUserMenu,
63 /// Toggles the project menu dropdown.
64 ToggleProjectMenu,
65 /// Switches to a different git branch.
66 SwitchBranch
67 ]
68);
69
70pub fn init(cx: &mut App) {
71 SystemWindowTabs::init(cx);
72
73 cx.observe_new(|workspace: &mut Workspace, window, cx| {
74 let Some(window) = window else {
75 return;
76 };
77 let item = cx.new(|cx| TitleBar::new("title-bar", workspace, window, cx));
78 workspace.set_titlebar_item(item.into(), window, cx);
79
80 #[cfg(not(target_os = "macos"))]
81 workspace.register_action(|workspace, action: &OpenApplicationMenu, window, cx| {
82 if let Some(titlebar) = workspace
83 .titlebar_item()
84 .and_then(|item| item.downcast::<TitleBar>().ok())
85 {
86 titlebar.update(cx, |titlebar, cx| {
87 if let Some(ref menu) = titlebar.application_menu {
88 menu.update(cx, |menu, cx| menu.open_menu(action, window, cx));
89 }
90 });
91 }
92 });
93
94 #[cfg(not(target_os = "macos"))]
95 workspace.register_action(|workspace, _: &ActivateMenuRight, window, cx| {
96 if let Some(titlebar) = workspace
97 .titlebar_item()
98 .and_then(|item| item.downcast::<TitleBar>().ok())
99 {
100 titlebar.update(cx, |titlebar, cx| {
101 if let Some(ref menu) = titlebar.application_menu {
102 menu.update(cx, |menu, cx| {
103 menu.navigate_menus_in_direction(ActivateDirection::Right, window, cx)
104 });
105 }
106 });
107 }
108 });
109
110 #[cfg(not(target_os = "macos"))]
111 workspace.register_action(|workspace, _: &ActivateMenuLeft, window, cx| {
112 if let Some(titlebar) = workspace
113 .titlebar_item()
114 .and_then(|item| item.downcast::<TitleBar>().ok())
115 {
116 titlebar.update(cx, |titlebar, cx| {
117 if let Some(ref menu) = titlebar.application_menu {
118 menu.update(cx, |menu, cx| {
119 menu.navigate_menus_in_direction(ActivateDirection::Left, window, cx)
120 });
121 }
122 });
123 }
124 });
125 })
126 .detach();
127}
128
129pub struct TitleBar {
130 platform_titlebar: Entity<PlatformTitleBar>,
131 project: Entity<Project>,
132 user_store: Entity<UserStore>,
133 client: Arc<Client>,
134 workspace: WeakEntity<Workspace>,
135 application_menu: Option<Entity<ApplicationMenu>>,
136 _subscriptions: Vec<Subscription>,
137 banner: Entity<OnboardingBanner>,
138 screen_share_popover_handle: PopoverMenuHandle<ContextMenu>,
139}
140
141impl Render for TitleBar {
142 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
143 let title_bar_settings = *TitleBarSettings::get_global(cx);
144
145 let show_menus = show_menus(cx);
146
147 let mut children = Vec::new();
148
149 children.push(
150 h_flex()
151 .gap_1()
152 .map(|title_bar| {
153 let mut render_project_items = title_bar_settings.show_branch_name
154 || title_bar_settings.show_project_items;
155 title_bar
156 .when_some(
157 self.application_menu.clone().filter(|_| !show_menus),
158 |title_bar, menu| {
159 render_project_items &=
160 !menu.update(cx, |menu, cx| menu.all_menus_shown(cx));
161 title_bar.child(menu)
162 },
163 )
164 .when(render_project_items, |title_bar| {
165 title_bar
166 .when(title_bar_settings.show_project_items, |title_bar| {
167 title_bar
168 .children(self.render_restricted_mode(cx))
169 .children(self.render_project_host(cx))
170 .child(self.render_project_name(cx))
171 })
172 .when(title_bar_settings.show_branch_name, |title_bar| {
173 title_bar.children(self.render_project_repo(cx))
174 })
175 })
176 })
177 .on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation())
178 .into_any_element(),
179 );
180
181 children.push(self.render_collaborator_list(window, cx).into_any_element());
182
183 if title_bar_settings.show_onboarding_banner {
184 children.push(self.banner.clone().into_any_element())
185 }
186
187 let status = self.client.status();
188 let status = &*status.borrow();
189 let user = self.user_store.read(cx).current_user();
190
191 let signed_in = user.is_some();
192
193 children.push(
194 h_flex()
195 .map(|this| {
196 if signed_in {
197 this.pr_1p5()
198 } else {
199 this.pr_1()
200 }
201 })
202 .gap_1()
203 .on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation())
204 .children(self.render_call_controls(window, cx))
205 .children(self.render_connection_status(status, cx))
206 .when(
207 user.is_none() && TitleBarSettings::get_global(cx).show_sign_in,
208 |this| this.child(self.render_sign_in_button(cx)),
209 )
210 .when(TitleBarSettings::get_global(cx).show_user_menu, |this| {
211 this.child(self.render_user_menu_button(cx))
212 })
213 .into_any_element(),
214 );
215
216 if show_menus {
217 self.platform_titlebar.update(cx, |this, _| {
218 this.set_children(
219 self.application_menu
220 .clone()
221 .map(|menu| menu.into_any_element()),
222 );
223 });
224
225 let height = PlatformTitleBar::height(window);
226 let title_bar_color = self.platform_titlebar.update(cx, |platform_titlebar, cx| {
227 platform_titlebar.title_bar_color(window, cx)
228 });
229
230 v_flex()
231 .w_full()
232 .child(self.platform_titlebar.clone().into_any_element())
233 .child(
234 h_flex()
235 .bg(title_bar_color)
236 .h(height)
237 .pl_2()
238 .justify_between()
239 .w_full()
240 .children(children),
241 )
242 .into_any_element()
243 } else {
244 self.platform_titlebar.update(cx, |this, _| {
245 this.set_children(children);
246 });
247 self.platform_titlebar.clone().into_any_element()
248 }
249 }
250}
251
252impl TitleBar {
253 pub fn new(
254 id: impl Into<ElementId>,
255 workspace: &Workspace,
256 window: &mut Window,
257 cx: &mut Context<Self>,
258 ) -> Self {
259 let project = workspace.project().clone();
260 let git_store = project.read(cx).git_store().clone();
261 let user_store = workspace.app_state().user_store.clone();
262 let client = workspace.app_state().client.clone();
263 let active_call = ActiveCall::global(cx);
264
265 let platform_style = PlatformStyle::platform();
266 let application_menu = match platform_style {
267 PlatformStyle::Mac => {
268 if option_env!("ZED_USE_CROSS_PLATFORM_MENU").is_some() {
269 Some(cx.new(|cx| ApplicationMenu::new(window, cx)))
270 } else {
271 None
272 }
273 }
274 PlatformStyle::Linux | PlatformStyle::Windows => {
275 Some(cx.new(|cx| ApplicationMenu::new(window, cx)))
276 }
277 };
278
279 let mut subscriptions = Vec::new();
280 subscriptions.push(
281 cx.observe(&workspace.weak_handle().upgrade().unwrap(), |_, _, cx| {
282 cx.notify()
283 }),
284 );
285 subscriptions.push(cx.subscribe(&project, |_, _, _: &project::Event, cx| cx.notify()));
286 subscriptions.push(cx.observe(&active_call, |this, _, cx| this.active_call_changed(cx)));
287 subscriptions.push(cx.observe_window_activation(window, Self::window_activation_changed));
288 subscriptions.push(
289 cx.subscribe(&git_store, move |_, _, event, cx| match event {
290 GitStoreEvent::ActiveRepositoryChanged(_)
291 | GitStoreEvent::RepositoryUpdated(_, _, true) => {
292 cx.notify();
293 }
294 _ => {}
295 }),
296 );
297 subscriptions.push(cx.observe(&user_store, |_a, _, cx| cx.notify()));
298 if let Some(trusted_worktrees) = TrustedWorktrees::try_get_global(cx) {
299 subscriptions.push(cx.subscribe(&trusted_worktrees, |_, _, _, cx| {
300 cx.notify();
301 }));
302 }
303
304 let banner = cx.new(|cx| {
305 OnboardingBanner::new(
306 "ACP Claude Code Onboarding",
307 IconName::AiClaude,
308 "Claude Code",
309 Some("Introducing:".into()),
310 zed_actions::agent::OpenClaudeCodeOnboardingModal.boxed_clone(),
311 cx,
312 )
313 // When updating this to a non-AI feature release, remove this line.
314 .visible_when(|cx| !project::DisableAiSettings::get_global(cx).disable_ai)
315 });
316
317 let platform_titlebar = cx.new(|cx| PlatformTitleBar::new(id, cx));
318
319 Self {
320 platform_titlebar,
321 application_menu,
322 workspace: workspace.weak_handle(),
323 project,
324 user_store,
325 client,
326 _subscriptions: subscriptions,
327 banner,
328 screen_share_popover_handle: PopoverMenuHandle::default(),
329 }
330 }
331
332 fn project_name(&self, cx: &Context<Self>) -> Option<SharedString> {
333 self.project
334 .read(cx)
335 .visible_worktrees(cx)
336 .map(|worktree| {
337 let worktree = worktree.read(cx);
338 let settings_location = SettingsLocation {
339 worktree_id: worktree.id(),
340 path: RelPath::empty(),
341 };
342
343 let settings = WorktreeSettings::get(Some(settings_location), cx);
344 let name = match &settings.project_name {
345 Some(name) => name.as_str(),
346 None => worktree.root_name_str(),
347 };
348 SharedString::new(name)
349 })
350 .next()
351 }
352
353 fn render_remote_project_connection(&self, cx: &mut Context<Self>) -> Option<AnyElement> {
354 let options = self.project.read(cx).remote_connection_options(cx)?;
355 let host: SharedString = options.display_name().into();
356
357 let (nickname, tooltip_title, icon) = match options {
358 RemoteConnectionOptions::Ssh(options) => (
359 options.nickname.map(|nick| nick.into()),
360 "Remote Project",
361 IconName::Server,
362 ),
363 RemoteConnectionOptions::Wsl(_) => (None, "Remote Project", IconName::Linux),
364 RemoteConnectionOptions::Docker(_dev_container_connection) => {
365 (None, "Dev Container", IconName::Box)
366 }
367 };
368
369 let nickname = nickname.unwrap_or_else(|| host.clone());
370
371 let (indicator_color, meta) = match self.project.read(cx).remote_connection_state(cx)? {
372 remote::ConnectionState::Connecting => (Color::Info, format!("Connecting to: {host}")),
373 remote::ConnectionState::Connected => (Color::Success, format!("Connected to: {host}")),
374 remote::ConnectionState::HeartbeatMissed => (
375 Color::Warning,
376 format!("Connection attempt to {host} missed. Retrying..."),
377 ),
378 remote::ConnectionState::Reconnecting => (
379 Color::Warning,
380 format!("Lost connection to {host}. Reconnecting..."),
381 ),
382 remote::ConnectionState::Disconnected => {
383 (Color::Error, format!("Disconnected from {host}"))
384 }
385 };
386
387 let icon_color = match self.project.read(cx).remote_connection_state(cx)? {
388 remote::ConnectionState::Connecting => Color::Info,
389 remote::ConnectionState::Connected => Color::Default,
390 remote::ConnectionState::HeartbeatMissed => Color::Warning,
391 remote::ConnectionState::Reconnecting => Color::Warning,
392 remote::ConnectionState::Disconnected => Color::Error,
393 };
394
395 let meta = SharedString::from(meta);
396
397 Some(
398 ButtonLike::new("ssh-server-icon")
399 .child(
400 h_flex()
401 .gap_2()
402 .max_w_32()
403 .child(
404 IconWithIndicator::new(
405 Icon::new(icon).size(IconSize::Small).color(icon_color),
406 Some(Indicator::dot().color(indicator_color)),
407 )
408 .indicator_border_color(Some(cx.theme().colors().title_bar_background))
409 .into_any_element(),
410 )
411 .child(Label::new(nickname).size(LabelSize::Small).truncate()),
412 )
413 .tooltip(move |_window, cx| {
414 Tooltip::with_meta(
415 tooltip_title,
416 Some(&OpenRemote {
417 from_existing_connection: false,
418 create_new_window: false,
419 }),
420 meta.clone(),
421 cx,
422 )
423 })
424 .on_click(|_, window, cx| {
425 window.dispatch_action(
426 OpenRemote {
427 from_existing_connection: false,
428 create_new_window: false,
429 }
430 .boxed_clone(),
431 cx,
432 );
433 })
434 .into_any_element(),
435 )
436 }
437
438 pub fn render_restricted_mode(&self, cx: &mut Context<Self>) -> Option<AnyElement> {
439 let has_restricted_worktrees = TrustedWorktrees::try_get_global(cx)
440 .map(|trusted_worktrees| {
441 trusted_worktrees
442 .read(cx)
443 .has_restricted_worktrees(&self.project.read(cx).worktree_store(), cx)
444 })
445 .unwrap_or(false);
446 if !has_restricted_worktrees {
447 return None;
448 }
449
450 Some(
451 Button::new("restricted_mode_trigger", "Restricted Mode")
452 .style(ButtonStyle::Tinted(TintColor::Warning))
453 .label_size(LabelSize::Small)
454 .color(Color::Warning)
455 .icon(IconName::Warning)
456 .icon_color(Color::Warning)
457 .icon_size(IconSize::Small)
458 .icon_position(IconPosition::Start)
459 .tooltip(|_, cx| {
460 Tooltip::with_meta(
461 "You're in Restricted Mode",
462 Some(&ToggleWorktreeSecurity),
463 "Mark this project as trusted and unlock all features",
464 cx,
465 )
466 })
467 .on_click({
468 cx.listener(move |this, _, window, cx| {
469 this.workspace
470 .update(cx, |workspace, cx| {
471 workspace.show_worktree_trust_security_modal(true, window, cx)
472 })
473 .log_err();
474 })
475 })
476 .into_any_element(),
477 )
478 }
479
480 pub fn render_project_host(&self, cx: &mut Context<Self>) -> Option<AnyElement> {
481 if self.project.read(cx).is_via_remote_server() {
482 return self.render_remote_project_connection(cx);
483 }
484
485 if self.project.read(cx).is_disconnected(cx) {
486 return Some(
487 Button::new("disconnected", "Disconnected")
488 .disabled(true)
489 .color(Color::Disabled)
490 .style(ButtonStyle::Subtle)
491 .label_size(LabelSize::Small)
492 .into_any_element(),
493 );
494 }
495
496 let host = self.project.read(cx).host()?;
497 let host_user = self.user_store.read(cx).get_cached_user(host.user_id)?;
498 let participant_index = self
499 .user_store
500 .read(cx)
501 .participant_indices()
502 .get(&host_user.id)?;
503 Some(
504 Button::new("project_owner_trigger", host_user.github_login.clone())
505 .color(Color::Player(participant_index.0))
506 .style(ButtonStyle::Subtle)
507 .label_size(LabelSize::Small)
508 .tooltip(Tooltip::text(format!(
509 "{} is sharing this project. Click to follow.",
510 host_user.github_login
511 )))
512 .on_click({
513 let host_peer_id = host.peer_id;
514 cx.listener(move |this, _, window, cx| {
515 this.workspace
516 .update(cx, |workspace, cx| {
517 workspace.follow(host_peer_id, window, cx);
518 })
519 .log_err();
520 })
521 })
522 .into_any_element(),
523 )
524 }
525
526 pub fn render_project_name(&self, cx: &mut Context<Self>) -> impl IntoElement {
527 let name = self.project_name(cx);
528 let is_project_selected = name.is_some();
529 let name = if let Some(name) = name {
530 util::truncate_and_trailoff(&name, MAX_PROJECT_NAME_LENGTH)
531 } else {
532 "Open Recent Project".to_string()
533 };
534
535 Button::new("project_name_trigger", name)
536 .when(!is_project_selected, |b| b.color(Color::Muted))
537 .style(ButtonStyle::Subtle)
538 .label_size(LabelSize::Small)
539 .tooltip(move |_window, cx| {
540 Tooltip::for_action(
541 "Recent Projects",
542 &zed_actions::OpenRecent {
543 create_new_window: false,
544 },
545 cx,
546 )
547 })
548 .on_click(cx.listener(move |_, _, window, cx| {
549 window.dispatch_action(
550 OpenRecent {
551 create_new_window: false,
552 }
553 .boxed_clone(),
554 cx,
555 );
556 }))
557 }
558
559 pub fn render_project_repo(&self, cx: &mut Context<Self>) -> Option<impl IntoElement> {
560 let settings = TitleBarSettings::get_global(cx);
561 let repository = self.project.read(cx).active_repository(cx)?;
562 let repository_count = self.project.read(cx).repositories(cx).len();
563 let workspace = self.workspace.upgrade()?;
564 let repo = repository.read(cx);
565 let branch_name = repo
566 .branch
567 .as_ref()
568 .map(|branch| branch.name())
569 .map(|name| util::truncate_and_trailoff(name, MAX_BRANCH_NAME_LENGTH))
570 .or_else(|| {
571 repo.head_commit.as_ref().map(|commit| {
572 commit
573 .sha
574 .chars()
575 .take(MAX_SHORT_SHA_LENGTH)
576 .collect::<String>()
577 })
578 })?;
579 let project_name = self.project_name(cx);
580 let repo_name = repo
581 .work_directory_abs_path
582 .file_name()
583 .and_then(|name| name.to_str())
584 .map(SharedString::new);
585 let show_repo_name =
586 repository_count > 1 && repo.branch.is_some() && repo_name != project_name;
587 let branch_name = if let Some(repo_name) = repo_name.filter(|_| show_repo_name) {
588 format!("{repo_name}/{branch_name}")
589 } else {
590 branch_name
591 };
592
593 Some(
594 Button::new("project_branch_trigger", branch_name)
595 .color(Color::Muted)
596 .style(ButtonStyle::Subtle)
597 .label_size(LabelSize::Small)
598 .tooltip(move |_window, cx| {
599 Tooltip::with_meta(
600 "Recent Branches",
601 Some(&zed_actions::git::Branch),
602 "Local branches only",
603 cx,
604 )
605 })
606 .on_click(move |_, window, cx| {
607 let _ = workspace.update(cx, |this, cx| {
608 window.focus(&this.active_pane().focus_handle(cx), cx);
609 window.dispatch_action(zed_actions::git::Branch.boxed_clone(), cx);
610 });
611 })
612 .when(settings.show_branch_icon, |branch_button| {
613 let (icon, icon_color) = {
614 let status = repo.status_summary();
615 let tracked = status.index + status.worktree;
616 if status.conflict > 0 {
617 (IconName::Warning, Color::VersionControlConflict)
618 } else if tracked.modified > 0 {
619 (IconName::SquareDot, Color::VersionControlModified)
620 } else if tracked.added > 0 || status.untracked > 0 {
621 (IconName::SquarePlus, Color::VersionControlAdded)
622 } else if tracked.deleted > 0 {
623 (IconName::SquareMinus, Color::VersionControlDeleted)
624 } else {
625 (IconName::GitBranch, Color::Muted)
626 }
627 };
628
629 branch_button
630 .icon(icon)
631 .icon_position(IconPosition::Start)
632 .icon_color(icon_color)
633 .icon_size(IconSize::Indicator)
634 }),
635 )
636 }
637
638 fn window_activation_changed(&mut self, window: &mut Window, cx: &mut Context<Self>) {
639 if window.is_window_active() {
640 ActiveCall::global(cx)
641 .update(cx, |call, cx| call.set_location(Some(&self.project), cx))
642 .detach_and_log_err(cx);
643 } else if cx.active_window().is_none() {
644 ActiveCall::global(cx)
645 .update(cx, |call, cx| call.set_location(None, cx))
646 .detach_and_log_err(cx);
647 }
648 self.workspace
649 .update(cx, |workspace, cx| {
650 workspace.update_active_view_for_followers(window, cx);
651 })
652 .ok();
653 }
654
655 fn active_call_changed(&mut self, cx: &mut Context<Self>) {
656 cx.notify();
657 }
658
659 fn share_project(&mut self, cx: &mut Context<Self>) {
660 let active_call = ActiveCall::global(cx);
661 let project = self.project.clone();
662 active_call
663 .update(cx, |call, cx| call.share_project(project, cx))
664 .detach_and_log_err(cx);
665 }
666
667 fn unshare_project(&mut self, _: &mut Window, cx: &mut Context<Self>) {
668 let active_call = ActiveCall::global(cx);
669 let project = self.project.clone();
670 active_call
671 .update(cx, |call, cx| call.unshare_project(project, cx))
672 .log_err();
673 }
674
675 fn render_connection_status(
676 &self,
677 status: &client::Status,
678 cx: &mut Context<Self>,
679 ) -> Option<AnyElement> {
680 match status {
681 client::Status::ConnectionError
682 | client::Status::ConnectionLost
683 | client::Status::Reauthenticating
684 | client::Status::Reconnecting
685 | client::Status::ReconnectionError { .. } => Some(
686 div()
687 .id("disconnected")
688 .child(Icon::new(IconName::Disconnected).size(IconSize::Small))
689 .tooltip(Tooltip::text("Disconnected"))
690 .into_any_element(),
691 ),
692 client::Status::UpgradeRequired => {
693 let auto_updater = auto_update::AutoUpdater::get(cx);
694 let label = match auto_updater.map(|auto_update| auto_update.read(cx).status()) {
695 Some(AutoUpdateStatus::Updated { .. }) => "Please restart Zed to Collaborate",
696 Some(AutoUpdateStatus::Installing { .. })
697 | Some(AutoUpdateStatus::Downloading { .. })
698 | Some(AutoUpdateStatus::Checking) => "Updating...",
699 Some(AutoUpdateStatus::Idle)
700 | Some(AutoUpdateStatus::Errored { .. })
701 | None => "Please update Zed to Collaborate",
702 };
703
704 Some(
705 Button::new("connection-status", label)
706 .label_size(LabelSize::Small)
707 .on_click(|_, window, cx| {
708 if let Some(auto_updater) = auto_update::AutoUpdater::get(cx)
709 && auto_updater.read(cx).status().is_updated()
710 {
711 workspace::reload(cx);
712 return;
713 }
714 auto_update::check(&Default::default(), window, cx);
715 })
716 .into_any_element(),
717 )
718 }
719 _ => None,
720 }
721 }
722
723 pub fn render_sign_in_button(&mut self, _: &mut Context<Self>) -> Button {
724 let client = self.client.clone();
725 Button::new("sign_in", "Sign in")
726 .label_size(LabelSize::Small)
727 .on_click(move |_, window, cx| {
728 let client = client.clone();
729 window
730 .spawn(cx, async move |cx| {
731 client
732 .sign_in_with_optional_connect(true, cx)
733 .await
734 .notify_async_err(cx);
735 })
736 .detach();
737 })
738 }
739
740 pub fn render_user_menu_button(&mut self, cx: &mut Context<Self>) -> impl Element {
741 let user_store = self.user_store.read(cx);
742 let user = user_store.current_user();
743
744 let user_avatar = user.as_ref().map(|u| u.avatar_uri.clone());
745 let user_login = user.as_ref().map(|u| u.github_login.clone());
746
747 let is_signed_in = user.is_some();
748
749 let has_subscription_period = user_store.subscription_period().is_some();
750 let plan = user_store.plan().filter(|_| {
751 // Since the user might be on the legacy free plan we filter based on whether we have a subscription period.
752 has_subscription_period
753 });
754
755 let free_chip_bg = cx
756 .theme()
757 .colors()
758 .editor_background
759 .opacity(0.5)
760 .blend(cx.theme().colors().text_accent.opacity(0.05));
761
762 let pro_chip_bg = cx
763 .theme()
764 .colors()
765 .editor_background
766 .opacity(0.5)
767 .blend(cx.theme().colors().text_accent.opacity(0.2));
768
769 PopoverMenu::new("user-menu")
770 .anchor(Corner::TopRight)
771 .menu(move |window, cx| {
772 ContextMenu::build(window, cx, |menu, _, _cx| {
773 let user_login = user_login.clone();
774
775 let (plan_name, label_color, bg_color) = match plan {
776 None | Some(Plan::V1(PlanV1::ZedFree) | Plan::V2(PlanV2::ZedFree)) => {
777 ("Free", Color::Default, free_chip_bg)
778 }
779 Some(Plan::V1(PlanV1::ZedProTrial) | Plan::V2(PlanV2::ZedProTrial)) => {
780 ("Pro Trial", Color::Accent, pro_chip_bg)
781 }
782 Some(Plan::V1(PlanV1::ZedPro) | Plan::V2(PlanV2::ZedPro)) => {
783 ("Pro", Color::Accent, pro_chip_bg)
784 }
785 };
786
787 menu.when(is_signed_in, |this| {
788 this.custom_entry(
789 move |_window, _cx| {
790 let user_login = user_login.clone().unwrap_or_default();
791
792 h_flex()
793 .w_full()
794 .justify_between()
795 .child(Label::new(user_login))
796 .child(
797 Chip::new(plan_name.to_string())
798 .bg_color(bg_color)
799 .label_color(label_color),
800 )
801 .into_any_element()
802 },
803 move |_, cx| {
804 cx.open_url(&zed_urls::account_url(cx));
805 },
806 )
807 .separator()
808 })
809 .action("Settings", zed_actions::OpenSettings.boxed_clone())
810 .action("Keymap", Box::new(zed_actions::OpenKeymap))
811 .action(
812 "Themes…",
813 zed_actions::theme_selector::Toggle::default().boxed_clone(),
814 )
815 .action(
816 "Icon Themes…",
817 zed_actions::icon_theme_selector::Toggle::default().boxed_clone(),
818 )
819 .action(
820 "Extensions",
821 zed_actions::Extensions::default().boxed_clone(),
822 )
823 .when(is_signed_in, |this| {
824 this.separator()
825 .action("Sign Out", client::SignOut.boxed_clone())
826 })
827 })
828 .into()
829 })
830 .map(|this| {
831 if is_signed_in && TitleBarSettings::get_global(cx).show_user_picture {
832 this.trigger_with_tooltip(
833 ButtonLike::new("user-menu")
834 .children(user_avatar.clone().map(|avatar| Avatar::new(avatar))),
835 Tooltip::text("Toggle User Menu"),
836 )
837 } else {
838 this.trigger_with_tooltip(
839 IconButton::new("user-menu", IconName::ChevronDown)
840 .icon_size(IconSize::Small),
841 Tooltip::text("Toggle User Menu"),
842 )
843 }
844 })
845 .anchor(gpui::Corner::TopRight)
846 }
847}