ZIm/crates/language_selector/src/active_buffer_language.rs
zumbalogy 2c7251e4f9
Add setting to hide active language button in the status bar (#33977)
Release Notes:

- Added settings status_bar.show_active_language_button to show/hide the
language button in the status bar.

The motivation for this is visual, I have had zero issues with its
functionality.

The language switcher can still be accessed by the command palette,
menu, or a keyboard shortcut.

------

This is my first Zed and first Rust PR, so criticism is very welcome. 

I know there has been discussion around how the status bar settings are
structured and named, and I am happy to change it to whatever is best. I
was also not sure what order to put it in in the settings default.json.
Feedback welcome.

Here is a picture of it in action:


![image](https://github.com/user-attachments/assets/c50131e2-71aa-4fab-8db0-8b2aae586e71)

---------

Co-authored-by: zumbalogy <3770982+zumbalogy@users.noreply.github.com>
Co-authored-by: Kirill Bulatov <kirill@zed.dev>
2025-08-08 05:04:30 +00:00

93 lines
3.1 KiB
Rust

use editor::{Editor, EditorSettings};
use gpui::{
Context, Entity, IntoElement, ParentElement, Render, Subscription, WeakEntity, Window, div,
};
use language::LanguageName;
use settings::Settings as _;
use ui::{Button, ButtonCommon, Clickable, FluentBuilder, LabelSize, Tooltip};
use workspace::{StatusItemView, Workspace, item::ItemHandle};
use crate::{LanguageSelector, Toggle};
pub struct ActiveBufferLanguage {
active_language: Option<Option<LanguageName>>,
workspace: WeakEntity<Workspace>,
_observe_active_editor: Option<Subscription>,
}
impl ActiveBufferLanguage {
pub fn new(workspace: &Workspace) -> Self {
Self {
active_language: None,
workspace: workspace.weak_handle(),
_observe_active_editor: None,
}
}
fn update_language(&mut self, editor: Entity<Editor>, _: &mut Window, cx: &mut Context<Self>) {
self.active_language = Some(None);
let editor = editor.read(cx);
if let Some((_, buffer, _)) = editor.active_excerpt(cx) {
if let Some(language) = buffer.read(cx).language() {
self.active_language = Some(Some(language.name()));
}
}
cx.notify();
}
}
impl Render for ActiveBufferLanguage {
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
if !EditorSettings::get_global(cx)
.status_bar
.active_language_button
{
return div();
}
div().when_some(self.active_language.as_ref(), |el, active_language| {
let active_language_text = if let Some(active_language_text) = active_language {
active_language_text.to_string()
} else {
"Unknown".to_string()
};
el.child(
Button::new("change-language", active_language_text)
.label_size(LabelSize::Small)
.on_click(cx.listener(|this, _, window, cx| {
if let Some(workspace) = this.workspace.upgrade() {
workspace.update(cx, |workspace, cx| {
LanguageSelector::toggle(workspace, window, cx)
});
}
}))
.tooltip(|window, cx| {
Tooltip::for_action("Select Language", &Toggle, window, cx)
}),
)
})
}
}
impl StatusItemView for ActiveBufferLanguage {
fn set_active_pane_item(
&mut self,
active_pane_item: Option<&dyn ItemHandle>,
window: &mut Window,
cx: &mut Context<Self>,
) {
if let Some(editor) = active_pane_item.and_then(|item| item.downcast::<Editor>()) {
self._observe_active_editor =
Some(cx.observe_in(&editor, window, Self::update_language));
self.update_language(editor, window, cx);
} else {
self.active_language = None;
self._observe_active_editor = None;
}
cx.notify();
}
}