Break out terminal creation to remove duplicate code

This commit is contained in:
Mattias Eriksson 2024-01-18 10:14:32 +01:00
parent 8fabf94f03
commit d9ae46598e

View file

@ -589,6 +589,50 @@ impl App {
widget::settings::view_column(vec![settings_view.into()]).into()
}
fn create_and_focus_new_terminal(&mut self, pane: pane_grid::Pane) {
self.pane_model.focus = pane;
match &self.term_event_tx_opt {
Some(term_event_tx) => match self.themes.get(self.config.syntax_theme()) {
Some(colors) => {
let current_pane = self.pane_model.focus;
if let Some(tab_model) = self.pane_model.active_mut() {
let entity = tab_model
.insert()
.text("New Terminal")
.closable()
.activate()
.id();
// Use the startup options, or defaults
let options = self.startup_options.take().unwrap_or_default();
let mut terminal = Terminal::new(
current_pane,
entity,
term_event_tx.clone(),
self.term_config.clone(),
options,
&self.config,
*colors,
);
terminal.set_config(&self.config, &self.themes, self.zoom_adj);
tab_model.data_set::<Mutex<Terminal>>(entity, Mutex::new(terminal));
} else {
log::error!("Found no active pane");
}
}
None => {
log::error!(
"failed to find terminal theme {:?}",
self.config.syntax_theme()
);
//TODO: fall back to known good theme
}
},
None => {
log::warn!("tried to create new tab before having event channel");
}
}
}
}
/// Implement [`Application`] to integrate with COSMIC.
@ -955,49 +999,7 @@ impl Application for App {
);
if let Some((pane, _)) = result {
self.terminal_ids.insert(pane, widget::Id::unique());
self.pane_model.focus = pane;
match &self.term_event_tx_opt {
Some(term_event_tx) => match self.themes.get(self.config.syntax_theme()) {
Some(colors) => {
let current_pane = self.pane_model.focus;
if let Some(tab_model) = self.pane_model.active_mut() {
let entity = tab_model
.insert()
.text("New Terminal")
.closable()
.activate()
.id();
// Use the startup options, or defaults
let options = self.startup_options.take().unwrap_or_default();
let mut terminal = Terminal::new(
current_pane,
entity,
term_event_tx.clone(),
self.term_config.clone(),
options,
&self.config,
*colors,
);
terminal.set_config(&self.config, &self.themes, self.zoom_adj);
tab_model
.data_set::<Mutex<Terminal>>(entity, Mutex::new(terminal));
} else {
log::error!("Found no active pane");
}
}
None => {
log::error!(
"failed to find terminal theme {:?}",
self.config.syntax_theme()
);
//TODO: fall back to known good theme
}
},
None => {
log::warn!("tried to create new tab before having event channel");
}
}
self.create_and_focus_new_terminal(pane);
self.pane_model.panes_created += 1;
return self.update_title(Some(pane));
}
@ -1147,46 +1149,7 @@ impl Application for App {
}
}
}
Message::TabNew => match &self.term_event_tx_opt {
Some(term_event_tx) => match self.themes.get(self.config.syntax_theme()) {
Some(colors) => {
let current_pane = self.pane_model.focus;
if let Some(tab_model) = self.pane_model.active_mut() {
let entity = tab_model
.insert()
.text("New Terminal")
.closable()
.activate()
.id();
// Use the startup options, or defaults
let options = self.startup_options.take().unwrap_or_default();
let mut terminal = Terminal::new(
current_pane,
entity,
term_event_tx.clone(),
self.term_config.clone(),
options,
&self.config,
*colors,
);
terminal.set_config(&self.config, &self.themes, self.zoom_adj);
tab_model.data_set::<Mutex<Terminal>>(entity, Mutex::new(terminal));
} else {
log::error!("Found no active pane");
}
}
None => {
log::error!(
"failed to find terminal theme {:?}",
self.config.syntax_theme()
);
//TODO: fall back to known good theme
}
},
None => {
log::warn!("tried to create new tab before having event channel");
}
},
Message::TabNew => self.create_and_focus_new_terminal(self.pane_model.focus),
Message::TermEvent(pane, entity, event) => {
match event {
TermEvent::Bell => {