Skip to repository content145 lines · 4.8 KB · rust
tenant.openagents/omega
No repository description is available.
OpenAgents Git authority 2026-07-28T02:22:39.027Z Public web read
NIP-34 coordinate
30617:7649603503856e5148d571eac2766b288a8ff1e9e35d380337a1d2b0015b4f92:omegaMaintainersHidden in public view
References2 branches · 1 tag
Read-only clone
git clone https://openagents.com/git/tenant.openagents/omega.gitBrowse files
active_buffer_encoding.rs
1use crate::{EncodingSelector, Toggle};
2
3use editor::Editor;
4use encoding_rs::{Encoding, UTF_8};
5use gpui::{
6 App, Context, Entity, IntoElement, ParentElement, Render, Styled, Subscription, WeakEntity,
7 Window, div,
8};
9use project::Project;
10use ui::{Button, ButtonCommon, Clickable, LabelSize, Tooltip};
11use workspace::{
12 EncodingDisplayOptions, HideStatusItem, StatusBarSettings, StatusItemView, Workspace,
13 item::{ItemHandle, Settings},
14};
15
16pub struct ActiveBufferEncoding {
17 active_encoding: Option<&'static Encoding>,
18 workspace: WeakEntity<Workspace>,
19 project: Entity<Project>,
20 _observe_active_editor: Option<Subscription>,
21 has_bom: bool,
22 is_dirty: bool,
23 is_shared: bool,
24 is_via_remote_server: bool,
25}
26
27impl ActiveBufferEncoding {
28 pub fn new(workspace: &Workspace) -> Self {
29 Self {
30 active_encoding: None,
31 workspace: workspace.weak_handle(),
32 project: workspace.project().clone(),
33 _observe_active_editor: None,
34 has_bom: false,
35 is_dirty: false,
36 is_shared: false,
37 is_via_remote_server: false,
38 }
39 }
40
41 fn update_encoding(&mut self, editor: Entity<Editor>, _: &mut Window, cx: &mut Context<Self>) {
42 self.active_encoding = None;
43 self.has_bom = false;
44 self.is_dirty = false;
45
46 let project = self.project.read(cx);
47 self.is_shared = project.is_shared();
48 self.is_via_remote_server = project.is_via_remote_server();
49
50 if let Some(buffer) = editor.read(cx).active_buffer(cx) {
51 let buffer = buffer.read(cx);
52 self.active_encoding = Some(buffer.encoding());
53 self.has_bom = buffer.has_bom();
54 self.is_dirty = buffer.is_dirty();
55 }
56
57 cx.notify();
58 }
59}
60
61impl Render for ActiveBufferEncoding {
62 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
63 let Some(active_encoding) = self.active_encoding else {
64 return div().hidden();
65 };
66
67 let display_option = StatusBarSettings::get_global(cx).active_encoding_button;
68 let is_utf8 = active_encoding == UTF_8;
69 if !display_option.should_show(is_utf8, self.has_bom) {
70 return div().hidden();
71 }
72
73 let mut text = active_encoding.name().to_string();
74 if self.has_bom {
75 text.push_str(" (BOM)");
76 }
77
78 let (disabled, tooltip_text) = if self.is_dirty {
79 (true, "Save file to change encoding")
80 } else if self.is_shared {
81 (true, "Cannot change encoding during collaboration")
82 } else if self.is_via_remote_server {
83 (true, "Cannot change encoding of remote server file")
84 } else {
85 (false, "Reopen with Encoding")
86 };
87
88 div().child(
89 Button::new("change-encoding", text)
90 .label_size(LabelSize::Small)
91 .tab_index(0isize)
92 .on_click(cx.listener(move |this, _, window, cx| {
93 if disabled {
94 return;
95 }
96 if let Some(workspace) = this.workspace.upgrade() {
97 workspace.update(cx, |workspace, cx| {
98 EncodingSelector::toggle(workspace, window, cx)
99 });
100 }
101 }))
102 .tooltip(move |_window, cx| {
103 if disabled {
104 Tooltip::text(tooltip_text)(_window, cx)
105 } else {
106 Tooltip::for_action(tooltip_text, &Toggle, cx)
107 }
108 }),
109 )
110 }
111}
112
113impl StatusItemView for ActiveBufferEncoding {
114 fn set_active_pane_item(
115 &mut self,
116 active_pane_item: Option<&dyn ItemHandle>,
117 window: &mut Window,
118 cx: &mut Context<Self>,
119 ) {
120 if let Some(editor) = active_pane_item.and_then(|item| item.downcast::<Editor>()) {
121 self._observe_active_editor =
122 Some(cx.observe_in(&editor, window, Self::update_encoding));
123 self.update_encoding(editor, window, cx);
124 } else {
125 self.active_encoding = None;
126 self.has_bom = false;
127 self.is_dirty = false;
128 self.is_shared = false;
129 self.is_via_remote_server = false;
130 self._observe_active_editor = None;
131 }
132
133 cx.notify();
134 }
135
136 fn hide_setting(&self, _: &App) -> Option<HideStatusItem> {
137 Some(HideStatusItem::new(|settings| {
138 settings
139 .status_bar
140 .get_or_insert_default()
141 .active_encoding_button = Some(EncodingDisplayOptions::Disabled);
142 }))
143 }
144}
145