1use editor::Editor;
2use gpui::{Entity, Subscription, WeakEntity};
3use language::LineEnding;
4use ui::{Tooltip, prelude::*};
5use workspace::{StatusBarSettings, StatusItemView, item::ItemHandle, item::Settings};
6
7use crate::{LineEndingSelector, Toggle};
8
9#[derive(Default)]
10pub struct LineEndingIndicator {
11 line_ending: Option<LineEnding>,
12 active_editor: Option<WeakEntity<Editor>>,
13 _observe_active_editor: Option<Subscription>,
14}
15
16impl LineEndingIndicator {
17 fn update(&mut self, editor: Entity<Editor>, _: &mut Window, cx: &mut Context<Self>) {
18 self.line_ending = None;
19 self.active_editor = None;
20
21 if let Some((_, buffer, _)) = editor.read(cx).active_excerpt(cx) {
22 let line_ending = buffer.read(cx).line_ending();
23 self.line_ending = Some(line_ending);
24 self.active_editor = Some(editor.downgrade());
25 }
26
27 cx.notify();
28 }
29}
30
31impl Render for LineEndingIndicator {
32 fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
33 if !StatusBarSettings::get_global(cx).line_endings_button {
34 return div();
35 }
36
37 div().when_some(self.line_ending.as_ref(), |el, line_ending| {
38 el.child(
39 Button::new("change-line-ending", line_ending.label())
40 .label_size(LabelSize::Small)
41 .on_click(cx.listener(|this, _, window, cx| {
42 if let Some(editor) = this.active_editor.as_ref() {
43 LineEndingSelector::toggle(editor, window, cx);
44 }
45 }))
46 .tooltip(|_window, cx| Tooltip::for_action("Select Line Ending", &Toggle, cx)),
47 )
48 })
49 }
50}
51
52impl StatusItemView for LineEndingIndicator {
53 fn set_active_pane_item(
54 &mut self,
55 active_pane_item: Option<&dyn ItemHandle>,
56 window: &mut Window,
57 cx: &mut Context<Self>,
58 ) {
59 if let Some(editor) = active_pane_item.and_then(|item| item.downcast::<Editor>()) {
60 self._observe_active_editor = Some(cx.observe_in(&editor, window, Self::update));
61 self.update(editor, window, cx);
62 } else {
63 self.line_ending = None;
64 self._observe_active_editor = None;
65 }
66 cx.notify();
67 }
68}