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