1use editor::Editor;
2use gpui::{
3 elements::*,
4 platform::{CursorStyle, MouseButton},
5 Entity, Subscription, View, ViewContext, ViewHandle, WeakViewHandle,
6};
7use settings::Settings;
8use std::sync::Arc;
9use workspace::{item::ItemHandle, StatusItemView, Workspace};
10
11pub struct ActiveBufferLanguage {
12 active_language: Option<Option<Arc<str>>>,
13 workspace: WeakViewHandle<Workspace>,
14 _observe_active_editor: Option<Subscription>,
15}
16
17impl ActiveBufferLanguage {
18 pub fn new(workspace: &Workspace) -> Self {
19 Self {
20 active_language: None,
21 workspace: workspace.weak_handle(),
22 _observe_active_editor: None,
23 }
24 }
25
26 fn update_language(&mut self, editor: ViewHandle<Editor>, cx: &mut ViewContext<Self>) {
27 self.active_language = Some(None);
28
29 let editor = editor.read(cx);
30 if let Some((_, buffer, _)) = editor.active_excerpt(cx) {
31 if let Some(language) = buffer.read(cx).language() {
32 self.active_language = Some(Some(language.name()));
33 }
34 }
35
36 cx.notify();
37 }
38}
39
40impl Entity for ActiveBufferLanguage {
41 type Event = ();
42}
43
44impl View for ActiveBufferLanguage {
45 fn ui_name() -> &'static str {
46 "ActiveBufferLanguage"
47 }
48
49 fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
50 if let Some(active_language) = self.active_language.as_ref() {
51 let active_language_text = if let Some(active_language_text) = active_language {
52 active_language_text.to_string()
53 } else {
54 "Unknown".to_string()
55 };
56
57 MouseEventHandler::<Self, Self>::new(0, cx, |state, cx| {
58 let theme = &cx.global::<Settings>().theme.workspace.status_bar;
59 let style = theme.active_language.style_for(state, false);
60 Label::new(active_language_text, style.text.clone())
61 .contained()
62 .with_style(style.container)
63 })
64 .with_cursor_style(CursorStyle::PointingHand)
65 .on_click(MouseButton::Left, |_, this, cx| {
66 if let Some(workspace) = this.workspace.upgrade(cx) {
67 workspace.update(cx, |workspace, cx| {
68 crate::toggle(workspace, &Default::default(), cx)
69 });
70 }
71 })
72 .into_any()
73 } else {
74 Empty::new().into_any()
75 }
76 }
77}
78
79impl StatusItemView for ActiveBufferLanguage {
80 fn set_active_pane_item(
81 &mut self,
82 active_pane_item: Option<&dyn ItemHandle>,
83 cx: &mut ViewContext<Self>,
84 ) {
85 if let Some(editor) = active_pane_item.and_then(|item| item.act_as::<Editor>(cx)) {
86 self._observe_active_editor = Some(cx.observe(&editor, Self::update_language));
87 self.update_language(editor, cx);
88 } else {
89 self.active_language = None;
90 self._observe_active_editor = None;
91 }
92
93 cx.notify();
94 }
95}