active_buffer_language.rs

 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        self.active_language.take();
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(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        let active_language = if let Some(active_language) = self.active_language.as_ref() {
54            active_language.to_string()
55        } else {
56            "Unknown".to_string()
57        };
58
59        MouseEventHandler::<Self>::new(0, cx, |state, cx| {
60            let theme = &cx.global::<Settings>().theme.workspace.status_bar;
61            let style = theme.active_language.style_for(state, false);
62            Label::new(active_language, style.text.clone())
63                .contained()
64                .with_style(style.container)
65                .boxed()
66        })
67        .with_cursor_style(CursorStyle::PointingHand)
68        .on_click(MouseButton::Left, |_, cx| cx.dispatch_action(crate::Toggle))
69        .boxed()
70    }
71}
72
73impl StatusItemView for ActiveBufferLanguage {
74    fn set_active_pane_item(
75        &mut self,
76        active_pane_item: Option<&dyn ItemHandle>,
77        cx: &mut ViewContext<Self>,
78    ) {
79        if let Some(editor) = active_pane_item.and_then(|item| item.act_as::<Editor>(cx)) {
80            self._observe_active_editor = Some(cx.observe(&editor, Self::update_language));
81            self.update_language(editor, cx);
82        } else {
83            self.active_language = None;
84            self._observe_active_editor = None;
85        }
86
87        cx.notify();
88    }
89}