1use db::kvp::Dismissable;
2use editor::Editor;
3use gpui::{Context, EventEmitter, Subscription};
4use ui::{Banner, FluentBuilder as _, prelude::*};
5use workspace::{ToolbarItemEvent, ToolbarItemLocation, ToolbarItemView, Workspace};
6
7pub struct BasedPyrightBanner {
8 dismissed: bool,
9 have_basedpyright: bool,
10 _subscriptions: [Subscription; 1],
11}
12
13impl Dismissable for BasedPyrightBanner {
14 const KEY: &str = "basedpyright-banner";
15}
16
17impl BasedPyrightBanner {
18 pub fn new(workspace: &Workspace, cx: &mut Context<Self>) -> Self {
19 let subscription = cx.subscribe(workspace.project(), |this, _, event, _| {
20 if let project::Event::LanguageServerAdded(_, name, _) = event
21 && name == "basedpyright"
22 {
23 this.have_basedpyright = true;
24 }
25 });
26 let dismissed = Self::dismissed(cx);
27 Self {
28 dismissed,
29 have_basedpyright: false,
30 _subscriptions: [subscription],
31 }
32 }
33
34 fn onboarding_banner_enabled(&self) -> bool {
35 !self.dismissed && self.have_basedpyright
36 }
37}
38
39impl EventEmitter<ToolbarItemEvent> for BasedPyrightBanner {}
40
41impl Render for BasedPyrightBanner {
42 fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
43 div()
44 .id("basedpyright-banner")
45 .when(self.onboarding_banner_enabled(), |el| {
46 el.child(
47 Banner::new()
48 .child(
49 v_flex()
50 .gap_0p5()
51 .child(Label::new("Basedpyright is now the only default language server for Python").mt_0p5())
52 .child(Label::new("We have disabled PyRight and pylsp by default. They can be re-enabled in your settings.").size(LabelSize::Small).color(Color::Muted))
53 )
54 .action_slot(
55 h_flex()
56 .gap_0p5()
57 .child(
58 Button::new("learn-more", "Learn More")
59 .label_size(LabelSize::Small)
60 .end_icon(Icon::new(IconName::ArrowUpRight).size(IconSize::XSmall).color(Color::Muted))
61 .on_click(|_, _, cx| {
62 cx.open_url("https://zed.dev/docs/languages/python")
63 }),
64 )
65 .child(IconButton::new("dismiss", IconName::Close).icon_size(IconSize::Small).on_click(
66 cx.listener(|this, _, _, cx| {
67 this.dismissed = true;
68 Self::set_dismissed(true, cx);
69 cx.notify();
70 }),
71 ))
72 )
73 .into_any_element(),
74 )
75 })
76 }
77}
78
79impl ToolbarItemView for BasedPyrightBanner {
80 fn set_active_pane_item(
81 &mut self,
82 active_pane_item: Option<&dyn workspace::ItemHandle>,
83 _window: &mut ui::Window,
84 cx: &mut Context<Self>,
85 ) -> ToolbarItemLocation {
86 if !self.onboarding_banner_enabled() {
87 return ToolbarItemLocation::Hidden;
88 }
89 if let Some(item) = active_pane_item
90 && let Some(editor) = item.act_as::<Editor>(cx)
91 && let Some(path) = editor.update(cx, |editor, cx| editor.target_file_abs_path(cx))
92 && let Some(file_name) = path.file_name()
93 && file_name.as_encoded_bytes().ends_with(".py".as_bytes())
94 {
95 return ToolbarItemLocation::Secondary;
96 }
97
98 ToolbarItemLocation::Hidden
99 }
100}