2023-03-10 16:28:27 +00:00
|
|
|
use editor::Editor;
|
|
|
|
use gpui::{
|
2023-04-07 17:41:39 +00:00
|
|
|
elements::*,
|
|
|
|
platform::{CursorStyle, MouseButton},
|
|
|
|
Entity, RenderContext, Subscription, View, ViewContext, ViewHandle,
|
2023-03-10 16:28:27 +00:00
|
|
|
};
|
|
|
|
use settings::Settings;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use workspace::{item::ItemHandle, StatusItemView};
|
|
|
|
|
|
|
|
pub struct ActiveBufferLanguage {
|
2023-03-23 20:29:23 +00:00
|
|
|
active_language: Option<Option<Arc<str>>>,
|
2023-03-10 16:28:27 +00:00
|
|
|
_observe_active_editor: Option<Subscription>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for ActiveBufferLanguage {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ActiveBufferLanguage {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
active_language: None,
|
|
|
|
_observe_active_editor: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_language(&mut self, editor: ViewHandle<Editor>, cx: &mut ViewContext<Self>) {
|
2023-03-23 20:29:23 +00:00
|
|
|
self.active_language = Some(None);
|
2023-03-10 16:43:48 +00:00
|
|
|
|
|
|
|
let editor = editor.read(cx);
|
2023-03-10 16:28:27 +00:00
|
|
|
if let Some((_, buffer, _)) = editor.active_excerpt(cx) {
|
|
|
|
if let Some(language) = buffer.read(cx).language() {
|
2023-03-23 20:29:23 +00:00
|
|
|
self.active_language = Some(Some(language.name()));
|
2023-03-10 16:28:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cx.notify();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Entity for ActiveBufferLanguage {
|
|
|
|
type Event = ();
|
|
|
|
}
|
|
|
|
|
|
|
|
impl View for ActiveBufferLanguage {
|
|
|
|
fn ui_name() -> &'static str {
|
|
|
|
"ActiveBufferLanguage"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
|
2023-03-23 20:29:23 +00:00
|
|
|
if let Some(active_language) = self.active_language.as_ref() {
|
|
|
|
let active_language_text = if let Some(active_language_text) = active_language {
|
|
|
|
active_language_text.to_string()
|
|
|
|
} else {
|
|
|
|
"Unknown".to_string()
|
|
|
|
};
|
2023-03-17 18:34:17 +00:00
|
|
|
|
2023-03-23 20:29:23 +00:00
|
|
|
MouseEventHandler::<Self>::new(0, cx, |state, cx| {
|
|
|
|
let theme = &cx.global::<Settings>().theme.workspace.status_bar;
|
|
|
|
let style = theme.active_language.style_for(state, false);
|
|
|
|
Label::new(active_language_text, style.text.clone())
|
|
|
|
.contained()
|
|
|
|
.with_style(style.container)
|
|
|
|
.boxed()
|
|
|
|
})
|
|
|
|
.with_cursor_style(CursorStyle::PointingHand)
|
|
|
|
.on_click(MouseButton::Left, |_, cx| cx.dispatch_action(crate::Toggle))
|
|
|
|
.boxed()
|
|
|
|
} else {
|
|
|
|
Empty::new().boxed()
|
|
|
|
}
|
2023-03-10 16:28:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl StatusItemView for ActiveBufferLanguage {
|
|
|
|
fn set_active_pane_item(
|
|
|
|
&mut self,
|
|
|
|
active_pane_item: Option<&dyn ItemHandle>,
|
|
|
|
cx: &mut ViewContext<Self>,
|
|
|
|
) {
|
|
|
|
if let Some(editor) = active_pane_item.and_then(|item| item.act_as::<Editor>(cx)) {
|
|
|
|
self._observe_active_editor = Some(cx.observe(&editor, Self::update_language));
|
|
|
|
self.update_language(editor, cx);
|
|
|
|
} else {
|
|
|
|
self.active_language = None;
|
|
|
|
self._observe_active_editor = None;
|
|
|
|
}
|
|
|
|
|
|
|
|
cx.notify();
|
|
|
|
}
|
|
|
|
}
|