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<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 let editor = editor.read(cx);
31 self.active_language.take();
32 if let Some((_, buffer, _)) = editor.active_excerpt(cx) {
33 if let Some(language) = buffer.read(cx).language() {
34 self.active_language = Some(language.name());
35 }
36 }
37
38 cx.notify();
39 }
40}
41
42impl Entity for ActiveBufferLanguage {
43 type Event = ();
44}
45
46impl View for ActiveBufferLanguage {
47 fn ui_name() -> &'static str {
48 "ActiveBufferLanguage"
49 }
50
51 fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
52 if let Some(active_language) = self.active_language.as_ref() {
53 MouseEventHandler::<Self>::new(0, cx, |state, cx| {
54 let theme = &cx.global::<Settings>().theme.workspace.status_bar;
55 let style = theme.active_language.style_for(state, false);
56 Label::new(active_language.to_string(), style.text.clone())
57 .contained()
58 .with_style(style.container)
59 .boxed()
60 })
61 .with_cursor_style(CursorStyle::PointingHand)
62 .on_click(MouseButton::Left, |_, cx| cx.dispatch_action(crate::Toggle))
63 .boxed()
64 } else {
65 Empty::new().boxed()
66 }
67 }
68}
69
70impl StatusItemView for ActiveBufferLanguage {
71 fn set_active_pane_item(
72 &mut self,
73 active_pane_item: Option<&dyn ItemHandle>,
74 cx: &mut ViewContext<Self>,
75 ) {
76 if let Some(editor) = active_pane_item.and_then(|item| item.act_as::<Editor>(cx)) {
77 self._observe_active_editor = Some(cx.observe(&editor, Self::update_language));
78 self.update_language(editor, cx);
79 } else {
80 self.active_language = None;
81 self._observe_active_editor = None;
82 }
83
84 cx.notify();
85 }
86}