1use editor::Editor;
2use encoding::Encoding;
3use gpui::{ClickEvent, Entity, Subscription, WeakEntity};
4use ui::{Button, ButtonCommon, Context, LabelSize, Render, Tooltip, Window, div};
5use ui::{Clickable, ParentElement};
6use workspace::{ItemHandle, StatusItemView, Workspace};
7
8use crate::selectors::save_or_reopen::{EncodingSaveOrReopenSelector, get_current_encoding};
9
10pub struct EncodingIndicator {
11 pub encoding: Option<&'static dyn Encoding>,
12 pub workspace: WeakEntity<Workspace>,
13 observe: Option<Subscription>,
14}
15
16pub mod selectors;
17
18impl Render for EncodingIndicator {
19 fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl ui::IntoElement {
20 let status_element = div();
21
22 status_element.child(
23 Button::new("encoding", get_current_encoding())
24 .label_size(LabelSize::Small)
25 .tooltip(Tooltip::text("Select Encoding"))
26 .on_click(cx.listener(|indicator, _: &ClickEvent, window, cx| {
27 if let Some(workspace) = indicator.workspace.upgrade() {
28 workspace.update(cx, |workspace, cx| {
29 EncodingSaveOrReopenSelector::toggle(workspace, window, cx)
30 })
31 } else {
32 }
33 })),
34 )
35 }
36}
37
38impl EncodingIndicator {
39 pub fn get_current_encoding(&self, cx: &mut Context<Self>, editor: WeakEntity<Editor>) {}
40
41 pub fn new(
42 encoding: Option<&'static dyn encoding::Encoding>,
43 workspace: WeakEntity<Workspace>,
44 observe: Option<Subscription>,
45 ) -> EncodingIndicator {
46 EncodingIndicator {
47 encoding,
48 workspace,
49 observe,
50 }
51 }
52
53 pub fn update(
54 &mut self,
55 editor: Entity<Editor>,
56 _: &mut Window,
57 cx: &mut Context<EncodingIndicator>,
58 ) {
59 let editor = editor.read(cx);
60 if let Some((_, buffer, _)) = editor.active_excerpt(cx) {
61 let encoding = buffer.read(cx).encoding;
62 self.encoding = Some(encoding);
63 }
64
65 cx.notify();
66 }
67}
68
69impl StatusItemView for EncodingIndicator {
70 fn set_active_pane_item(
71 &mut self,
72 active_pane_item: Option<&dyn ItemHandle>,
73 window: &mut Window,
74 cx: &mut Context<Self>,
75 ) {
76 match active_pane_item.and_then(|item| item.downcast::<Editor>()) {
77 Some(editor) => {
78 self.observe = Some(cx.observe_in(&editor, window, Self::update));
79 self.update(editor, window, cx);
80 }
81 None => {
82 self.encoding = None;
83 self.observe = None;
84 }
85 }
86 }
87}