add_context_server_modal.rs

  1use context_server::{ContextServerSettings, ServerCommand, ServerConfig};
  2use gpui::{DismissEvent, Entity, EventEmitter, FocusHandle, Focusable, WeakEntity, prelude::*};
  3use serde_json::json;
  4use settings::update_settings_file;
  5use ui::{KeyBinding, Modal, ModalFooter, ModalHeader, Section, Tooltip, prelude::*};
  6use ui_input::SingleLineInput;
  7use workspace::{ModalView, Workspace};
  8
  9use crate::AddContextServer;
 10
 11pub struct AddContextServerModal {
 12    workspace: WeakEntity<Workspace>,
 13    name_editor: Entity<SingleLineInput>,
 14    command_editor: Entity<SingleLineInput>,
 15}
 16
 17impl AddContextServerModal {
 18    pub fn register(
 19        workspace: &mut Workspace,
 20        _window: Option<&mut Window>,
 21        _cx: &mut Context<Workspace>,
 22    ) {
 23        workspace.register_action(|workspace, _: &AddContextServer, window, cx| {
 24            let workspace_handle = cx.entity().downgrade();
 25            workspace.toggle_modal(window, cx, |window, cx| {
 26                Self::new(workspace_handle, window, cx)
 27            })
 28        });
 29    }
 30
 31    pub fn new(
 32        workspace: WeakEntity<Workspace>,
 33        window: &mut Window,
 34        cx: &mut Context<Self>,
 35    ) -> Self {
 36        let name_editor =
 37            cx.new(|cx| SingleLineInput::new(window, cx, "my-custom-server").label("Name"));
 38        let command_editor = cx.new(|cx| {
 39            SingleLineInput::new(window, cx, "Command").label("Command to run the MCP server")
 40        });
 41
 42        Self {
 43            name_editor,
 44            command_editor,
 45            workspace,
 46        }
 47    }
 48
 49    fn confirm(&mut self, _: &menu::Confirm, cx: &mut Context<Self>) {
 50        let name = self
 51            .name_editor
 52            .read(cx)
 53            .editor()
 54            .read(cx)
 55            .text(cx)
 56            .trim()
 57            .to_string();
 58        let command = self
 59            .command_editor
 60            .read(cx)
 61            .editor()
 62            .read(cx)
 63            .text(cx)
 64            .trim()
 65            .to_string();
 66
 67        if name.is_empty() || command.is_empty() {
 68            return;
 69        }
 70
 71        let mut command_parts = command.split(' ').map(|part| part.trim().to_string());
 72        let Some(path) = command_parts.next() else {
 73            return;
 74        };
 75        let args = command_parts.collect::<Vec<_>>();
 76
 77        if let Some(workspace) = self.workspace.upgrade() {
 78            workspace.update(cx, |workspace, cx| {
 79                let fs = workspace.app_state().fs.clone();
 80                update_settings_file::<ContextServerSettings>(fs.clone(), cx, |settings, _| {
 81                    settings.context_servers.insert(
 82                        name.into(),
 83                        ServerConfig {
 84                            command: Some(ServerCommand {
 85                                path,
 86                                args,
 87                                env: None,
 88                            }),
 89                            settings: Some(json!({})),
 90                        },
 91                    );
 92                });
 93            });
 94        }
 95
 96        cx.emit(DismissEvent);
 97    }
 98
 99    fn cancel(&mut self, _: &menu::Cancel, cx: &mut Context<Self>) {
100        cx.emit(DismissEvent);
101    }
102}
103
104impl ModalView for AddContextServerModal {}
105
106impl Focusable for AddContextServerModal {
107    fn focus_handle(&self, cx: &App) -> FocusHandle {
108        self.name_editor.focus_handle(cx).clone()
109    }
110}
111
112impl EventEmitter<DismissEvent> for AddContextServerModal {}
113
114impl Render for AddContextServerModal {
115    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
116        let is_name_empty = self.name_editor.read(cx).is_empty(cx);
117        let is_command_empty = self.command_editor.read(cx).is_empty(cx);
118
119        let focus_handle = self.focus_handle(cx);
120
121        div()
122            .elevation_3(cx)
123            .w(rems(34.))
124            .key_context("AddContextServerModal")
125            .on_action(
126                cx.listener(|this, _: &menu::Cancel, _window, cx| this.cancel(&menu::Cancel, cx)),
127            )
128            .on_action(
129                cx.listener(|this, _: &menu::Confirm, _window, cx| {
130                    this.confirm(&menu::Confirm, cx)
131                }),
132            )
133            .capture_any_mouse_down(cx.listener(|this, _, window, cx| {
134                this.focus_handle(cx).focus(window);
135            }))
136            .on_mouse_down_out(cx.listener(|_this, _, _, cx| cx.emit(DismissEvent)))
137            .child(
138                Modal::new("add-context-server", None)
139                    .header(ModalHeader::new().headline("Add MCP Server"))
140                    .section(
141                        Section::new().child(
142                            v_flex()
143                                .gap_2()
144                                .child(self.name_editor.clone())
145                                .child(self.command_editor.clone()),
146                        ),
147                    )
148                    .footer(
149                        ModalFooter::new()
150                            .start_slot(
151                                Button::new("cancel", "Cancel")
152                                    .key_binding(
153                                        KeyBinding::for_action_in(
154                                            &menu::Cancel,
155                                            &focus_handle,
156                                            window,
157                                            cx,
158                                        )
159                                        .map(|kb| kb.size(rems_from_px(12.))),
160                                    )
161                                    .on_click(cx.listener(|this, _event, _window, cx| {
162                                        this.cancel(&menu::Cancel, cx)
163                                    })),
164                            )
165                            .end_slot(
166                                Button::new("add-server", "Add Server")
167                                    .disabled(is_name_empty || is_command_empty)
168                                    .key_binding(
169                                        KeyBinding::for_action_in(
170                                            &menu::Confirm,
171                                            &focus_handle,
172                                            window,
173                                            cx,
174                                        )
175                                        .map(|kb| kb.size(rems_from_px(12.))),
176                                    )
177                                    .map(|button| {
178                                        if is_name_empty {
179                                            button.tooltip(Tooltip::text("Name is required"))
180                                        } else if is_command_empty {
181                                            button.tooltip(Tooltip::text("Command is required"))
182                                        } else {
183                                            button
184                                        }
185                                    })
186                                    .on_click(cx.listener(|this, _event, _window, cx| {
187                                        this.confirm(&menu::Confirm, cx)
188                                    })),
189                            ),
190                    ),
191            )
192    }
193}