Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 51 additions & 94 deletions crates/runner-app/src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@ impl NativeRoot {
if !self.tabs.activate(tab_id) {
return;
}
self.new_chat_target = None;
self.layout_picker_open = false;
match self.ensure_active_tab_attached(window, cx) {
Ok(()) => {
self.error = None;
self.remember_active_runner();
self.focus_active_terminal(window);
}
Err(error) => self.error = Some(error.to_string()),
Expand All @@ -161,11 +161,26 @@ impl NativeRoot {
}

pub(crate) fn focus_pane(&mut self, pane_id: &str, cx: &mut Context<Self>) {
let runner_id = self.tabs.active().and_then(|layout| {
layout
.root
.leaves()
.into_iter()
.find(|leaf| leaf.id == pane_id)
.and_then(|leaf| leaf.session_id.as_deref())
.map(|session_id| {
self.session_entry(session_id)
.and_then(|entry| entry.runner_id.clone())
})
});
if self
.tabs
.active_mut()
.is_some_and(|layout| layout.focus_pane(pane_id))
{
if let Some(runner_id) = runner_id {
self.last_focused_runner_id = runner_id;
}
cx.notify();
}
}
Expand All @@ -178,6 +193,9 @@ impl NativeRoot {
cx: &mut Context<Self>,
) {
self.focus_pane(pane_id, cx);
self.last_focused_runner_id = self
.session_entry(session_id)
.and_then(|entry| entry.runner_id.clone());
if let Some(chat) = self.attached.get(session_id) {
chat.terminal_focus.focus(window);
}
Expand Down Expand Up @@ -267,98 +285,6 @@ impl NativeRoot {
}
}

pub(crate) fn begin_new_tab(&mut self, _: &NewTab, _: &mut Window, cx: &mut Context<Self>) {
self.new_chat_target = Some(NewChatTarget::NewTab);
self.layout_picker_open = false;
cx.notify();
}

pub(crate) fn begin_pane_chat(&mut self, pane_id: &str, cx: &mut Context<Self>) {
let Some(tab_id) = self.tabs.active_tab_id().map(str::to_owned) else {
return;
};
self.new_chat_target = Some(NewChatTarget::Pane {
tab_id,
pane_id: pane_id.to_owned(),
});
cx.notify();
}

pub(crate) fn start_chat(
&mut self,
runner_id: &str,
window: &mut Window,
cx: &mut Context<Self>,
) {
let Some(target) = self.new_chat_target.clone() else {
return;
};
let initial_size = match &target {
NewChatTarget::NewTab => (INITIAL_COLS, INITIAL_ROWS),
NewChatTarget::Pane { tab_id, pane_id }
if self.tabs.active_tab_id() == Some(tab_id.as_str()) =>
{
self.tabs
.active()
.map(|layout| self.estimated_terminal_size(layout, pane_id, window))
.unwrap_or((INITIAL_COLS, INITIAL_ROWS))
}
NewChatTarget::Pane { .. } => {
self.error = Some("The target tab is no longer active".into());
return;
}
};
let mut spawned_id = None;
let result = (|| -> Result<String> {
let spawned = runner_backend::ops::session::session_start_direct(
&self.core,
runner_id.to_owned(),
None,
None,
None,
None,
None,
Some(initial_size.0),
Some(initial_size.1),
)?;
spawned_id = Some(spawned.id.clone());
self.refresh_sessions();
match target {
NewChatTarget::NewTab => {
self.reload_tabs()?;
self.tabs.activate_session(&spawned.id);
}
NewChatTarget::Pane { pane_id, .. } => {
self.tabs.assign_to_active(&pane_id, &spawned.id)?;
self.persist_active_tab()?;
self.reload_tabs()?;
self.tabs.activate_session(&spawned.id);
}
}
self.new_chat_target = None;
self.ensure_active_tab_attached(window, cx)?;
Ok(spawned.id)
})();
match result {
Ok(session_id) => {
self.error = None;
if let Some(chat) = self.attached.get(&session_id) {
chat.terminal_focus.focus(window);
}
}
Err(error) => {
if let Some(session_id) = spawned_id {
self.new_chat_target = None;
let _ = self.reload_tabs();
self.tabs.activate_session(&session_id);
let _ = self.ensure_active_tab_attached(window, cx);
}
self.error = Some(error.to_string());
}
}
cx.notify();
}

pub(crate) fn resume_chat(
&mut self,
pane_id: &str,
Expand Down Expand Up @@ -420,8 +346,9 @@ impl NativeRoot {
Ok(empty_pane_id) => {
self.error = None;
if let Some(pane_id) = empty_pane_id {
self.begin_pane_chat(&pane_id, cx);
self.open_pane_chat_modal(&pane_id, window, cx);
} else {
self.remember_active_runner();
self.focus_active_terminal(window);
}
}
Expand All @@ -440,6 +367,36 @@ impl NativeRoot {
Ok(())
}

pub(crate) fn close_pane(
&mut self,
pane_id: &str,
window: &mut Window,
cx: &mut Context<Self>,
) {
let result = (|| -> Result<bool> {
let Some(layout) = self.tabs.active_mut() else {
return Ok(false);
};
if !layout.close_pane(pane_id) {
return Ok(false);
}
self.persist_active_tab()?;
self.reload_tabs()?;
self.ensure_active_tab_attached(window, cx)?;
Ok(true)
})();
match result {
Ok(true) => {
self.error = None;
self.remember_active_runner();
self.focus_active_terminal(window);
}
Ok(false) => {}
Err(error) => self.error = Some(error.to_string()),
}
cx.notify();
}

pub(crate) fn resize_split(
&mut self,
split_id: &str,
Expand Down
95 changes: 67 additions & 28 deletions crates/runner-app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ use terminal_element::TerminalElement;
actions!(runner_app_ui, [Quit, TermPaste, NewTab]);

mod chat;
mod modal_text_input;
mod panes;
mod sidebar;
mod start_chat;

use panes::pane_fractions;
use sidebar::session_label;
use start_chat::StartChatModal;

const INITIAL_COLS: u16 = 100;
const INITIAL_ROWS: u16 = 30;
Expand All @@ -48,12 +51,6 @@ struct AttachedChat {
scroll_accumulator: f32,
}

#[derive(Clone)]
enum NewChatTarget {
NewTab,
Pane { tab_id: String, pane_id: String },
}

#[derive(Clone)]
struct SplitResizeDrag {
split_id: String,
Expand All @@ -75,7 +72,8 @@ struct NativeRoot {
attached: HashMap<String, AttachedChat>,
root_focus: FocusHandle,
waker: Arc<dyn Fn() + Send + Sync>,
new_chat_target: Option<NewChatTarget>,
start_chat_modal: Option<StartChatModal>,
last_focused_runner_id: Option<String>,
layout_picker_open: bool,
split_sizes_dirty: bool,
error: Option<String>,
Expand All @@ -100,6 +98,35 @@ impl NativeRoot {
})
.detach();

let (runtime_event_tx, mut runtime_event_rx) = futures::channel::mpsc::unbounded::<()>();
let mut app_events = core.events.subscribe();
cx.background_spawn(async move {
loop {
match app_events.recv().await {
Ok(event) if event.name == "runtime/changed" => {
if runtime_event_tx.unbounded_send(()).is_err() {
break;
}
}
Ok(_) | Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => {}
Err(tokio::sync::broadcast::error::RecvError::Closed) => break,
}
}
})
.detach();
cx.spawn(async move |weak, cx| {
while runtime_event_rx.next().await.is_some() {
while runtime_event_rx.try_recv().is_ok() {}
if weak
.update(cx, |this, cx| this.refresh_start_chat_runtimes(cx))
.is_err()
{
break;
}
}
})
.detach();

let mut errors = Vec::new();
let sessions = match runner_backend::ops::session::session_list_recent_direct(&core) {
Ok(sessions) => sessions,
Expand Down Expand Up @@ -127,6 +154,11 @@ impl NativeRoot {
};

let root_focus = cx.focus_handle();
let last_focused_runner_id = tabs
.active()
.and_then(PaneLayout::focused_session_id)
.and_then(|session_id| sessions.iter().find(|entry| entry.session_id == session_id))
.and_then(|entry| entry.runner_id.clone());
let mut root = Self {
core,
bridge,
Expand All @@ -136,7 +168,8 @@ impl NativeRoot {
attached: HashMap::new(),
root_focus,
waker,
new_chat_target: None,
start_chat_modal: None,
last_focused_runner_id,
layout_picker_open: false,
split_sizes_dirty: false,
error: (!errors.is_empty()).then(|| errors.join("\n")),
Expand Down Expand Up @@ -175,32 +208,38 @@ impl Render for NativeRoot {

let sidebar = self.render_sidebar(cx);
let workspace = self.render_active_tab(window, cx);
let modal = self
.start_chat_modal
.as_ref()
.map(|_| self.render_start_chat_modal(cx));
div()
.relative()
.size_full()
.flex()
.track_focus(&self.root_focus)
.bg(theme::bg())
.child(sidebar)
.child(
div()
.flex_1()
.min_w(px(0.))
.h_full()
.flex()
.flex_col()
.child(workspace)
.children(self.error.as_ref().map(|error| {
div()
.flex_none()
.px_3()
.py_2()
.bg(gpui::rgb(0x3b1d2b))
.text_sm()
.text_color(gpui::rgb(0xf7768e))
.child(SharedString::from(error.clone()))
})),
div().size_full().flex().child(sidebar).child(
div()
.flex_1()
.min_w(px(0.))
.h_full()
.flex()
.flex_col()
.child(workspace)
.children(self.error.as_ref().map(|error| {
div()
.flex_none()
.px_3()
.py_2()
.bg(gpui::rgb(0x3b1d2b))
.text_sm()
.text_color(gpui::rgb(0xf7768e))
.child(SharedString::from(error.clone()))
})),
),
)
.on_action(cx.listener(Self::begin_new_tab))
.children(modal)
.on_action(cx.listener(Self::open_new_tab_modal))
}
}

Expand Down
Loading