use super::*;
impl App {
pub(super) fn shortcut(&mut self, event_loop: &ActiveEventLoop, event: &KeyEvent) -> bool {
let Some(st) = self.state.as_ref() else { return false };
let Some(chord) = Chord::from_event(event, st.mods) else { return false };
let Some(action) = self.keymap.get(&chord) else { return false };
self.run_action(event_loop, action, event.repeat)
}
pub(super) fn run_action(&mut self, event_loop: &ActiveEventLoop, action: Action, repeat: bool) -> bool {
let Some(st) = self.state.as_ref() else { return false };
let focused = st.focused;
let font_pt = st.font_pt;
match action {
Action::Code => {
if !repeat {
self.toggle_code();
}
}
Action::Chat => {
if !repeat {
self.toggle_chat();
}
}
Action::Embed => {
if !repeat {
self.toggle_embed();
}
}
Action::SplitLeft | Action::SplitRight | Action::SplitUp | Action::SplitDown => {
if !repeat {
self.split(match action {
Action::SplitLeft => Dir::Left,
Action::SplitRight => Dir::Right,
Action::SplitUp => Dir::Up,
_ => Dir::Down,
});
}
}
Action::FocusLeft | Action::FocusRight | Action::FocusUp | Action::FocusDown => {
let dir = match action {
Action::FocusLeft => Dir::Left,
Action::FocusRight => Dir::Right,
Action::FocusUp => Dir::Up,
_ => Dir::Down,
};
let rects = self.target_rects(st);
if let Some(next) = layout::neighbor(&rects, focused, dir) {
let st = self.state.as_mut().unwrap();
st.focused = next;
notify_focus(st, focused);
st.window.request_redraw();
}
}
Action::ResizeLeft | Action::ResizeRight | Action::ResizeUp | Action::ResizeDown => {
let dir = match action {
Action::ResizeLeft => Dir::Left,
Action::ResizeRight => Dir::Right,
Action::ResizeUp => Dir::Up,
_ => Dir::Down,
};
if let Some(st) = self.state.as_mut()
&& st.layout.resize(focused, dir, 0.04)
{
self.relayout(false, false);
}
}
Action::ClosePane => {
if !repeat && !self.close_web(focused) && !self.close_host(focused) && !self.close_review(focused) {
self.close_pane(event_loop, focused);
}
}
Action::Zoom => {
if !repeat {
self.toggle_zoom();
}
}
Action::Copy => {
self.copy_selection(false);
}
Action::SmartCopy => {
if !self.copy_selection(true) {
return false;
}
self.state.as_ref().unwrap().window.request_redraw();
}
Action::Paste => self.paste(),
Action::SelectAll => return self.select_all(),
Action::Find => {
if !repeat {
self.toggle_search();
}
}
Action::CopyOutput => {
if !repeat {
self.copy_last_output();
}
}
Action::OpenFolder => {
if !repeat {
self.open_cwd();
}
}
Action::FontBigger => self.set_font_size(font_pt + 1.0),
Action::FontSmaller => self.set_font_size(font_pt - 1.0),
Action::FontReset => {
let pt = self.config.font_size;
self.set_font_size(pt);
}
Action::ScrollPageUp => return self.scroll_focused(Scroll::PageUp),
Action::ScrollPageDown => return self.scroll_focused(Scroll::PageDown),
Action::ScrollTop => return self.scroll_focused(Scroll::Top),
Action::ScrollBottom => return self.scroll_focused(Scroll::Bottom),
Action::Review => {
if !repeat {
self.toggle_review();
}
}
Action::Git => {
if !repeat {
self.open_git_workspace();
}
}
Action::Palette => {
if !repeat {
self.open_palette();
}
}
Action::Settings => {
if !repeat {
self.open_settings();
}
}
}
true
}
pub(super) fn run_command(&mut self, event_loop: &ActiveEventLoop, command: Command) {
match command {
Command::Action(action) => {
self.run_action(event_loop, action, false);
}
Command::Review => self.open_review_list(),
Command::ReviewDemo => self.open_review_demo(),
Command::ReloadConfig => self.reload_config(true),
Command::OpenConfigFile => {
if let Some(path) = crate::config::config_path() {
links::open(&Target::Path(path), &self.config.editor);
}
}
Command::SaveLayout(name) => self.save_layout(&name),
Command::LoadLayout(name) => self.load_layout(&name),
Command::RestoreSession => match layouts::load_session() {
Some(layout) => {
if !self.apply_layout(layout, false) {
self.toast("Could not open the last session");
}
}
None => self.toast("No saved session yet"),
},
Command::Update => self.run_update(),
Command::OpenLayoutsFolder => {
if let Some(dir) = layouts::dir() {
let _ = std::fs::create_dir_all(&dir);
links::open(&Target::Path(dir), &self.config.editor);
}
}
}
}
pub(super) fn run_update(&mut self) {
let Ok(exe) = std::env::current_exe() else {
self.toast("Could not find the rustimal executable");
return;
};
self.split(Dir::Down);
let command = format!("{} --update", links::shell_word(&links::msys_path(&exe)));
if let Some(st) = self.state.as_mut()
&& let Some(pane) = st.panes.iter_mut().find(|p| p.id == st.focused)
{
pane.start_typing(command, None);
}
}
pub(super) fn toggle_summon(&mut self) {
self.web_window_focus();
let dropdown = self.config.dropdown;
let height = self.config.dropdown_height.clamp(0.2, 1.0);
let Some(st) = self.state.as_mut() else { return };
let w = &st.window;
let in_front = w.is_visible().unwrap_or(true) && st.window_focused && !w.is_minimized().unwrap_or(false);
if in_front {
w.set_visible(false);
return;
}
if dropdown && let Some(monitor) = w.current_monitor().or_else(|| w.primary_monitor()) {
let (pos, size) = (monitor.position(), monitor.size());
w.set_maximized(false);
w.set_outer_position(winit::dpi::PhysicalPosition::new(pos.x, pos.y));
let _ =
w.request_inner_size(winit::dpi::PhysicalSize::new(size.width, (size.height as f32 * height) as u32));
}
w.set_visible(true);
w.set_minimized(false);
w.focus_window();
st.window_focused = true;
st.window.request_redraw();
}
pub(super) fn copy_last_output(&mut self) {
let Some(st) = self.state.as_mut() else { return };
let Some(pane) = st.panes.iter().find(|p| p.id == st.focused) else { return };
let text = crate::terminal::prompt::last_output(&pane.term.lock());
let message = match text {
Some(text) => {
let lines = text.lines().count();
match st.clipboard.as_mut().map(|c| c.set_text(text)) {
Some(Ok(())) => {
format!("Copied the last output ({lines} line{})", if lines == 1 { "" } else { "s" })
}
_ => "Could not copy to the clipboard".to_string(),
}
}
None => "No output to copy".to_string(),
};
self.toast(message);
}
pub(super) fn open_cwd(&self) {
let Some(p) = self.focused_pane() else { return };
if let Some(dir) = p.cwd.clone().or_else(|| self.start_dir.clone()).or_else(dirs::home_dir) {
links::open(&links::Target::Path(dir), &self.config.editor);
}
}
pub(super) fn scroll_focused(&mut self, how: Scroll) -> bool {
let Some(p) = self.focused_pane() else { return false };
let mut term = p.term.lock();
if term.mode().contains(TermMode::ALT_SCREEN) {
return false;
}
term.scroll_display(how);
drop(term);
self.state.as_ref().unwrap().window.request_redraw();
true
}
pub(super) fn select_all(&mut self) -> bool {
let Some(st) = self.state.as_mut() else { return false };
let focused = st.focused;
let Some(p) = st.panes.iter_mut().find(|p| p.id == focused) else { return false };
if !p.edit_ready() || !p.select_command_line() {
return false;
}
st.window.request_redraw();
true
}
pub(super) fn toggle_search(&mut self) {
let Some(st) = self.state.as_mut() else { return };
let focused = st.focused;
if let Some(p) = st.panes.iter_mut().find(|p| p.id == focused) {
p.search = if p.search.is_some() { None } else { Some(Search::new()) };
st.window.request_redraw();
}
}
pub(super) fn paste(&mut self) {
let Some(st) = self.state.as_mut() else { return };
let focused = st.focused;
if let Some(p) = st.panes.iter_mut().find(|p| p.id == focused)
&& p.has_selection()
{
p.delete_selection();
}
let text = st.clipboard.as_mut().and_then(|c| c.get_text().ok());
let Some(pane) = st.panes.iter().find(|p| p.id == st.focused) else { return };
match text {
Some(text) if !text.is_empty() => {
let mode = *pane.term.lock().mode();
let text = text.replace("\r\n", "\r").replace('\n', "\r");
let bytes = if mode.contains(TermMode::BRACKETED_PASTE) {
format!("\x1b[200~{}\x1b[201~", text.replace('\x1b', ""))
} else {
text
};
pane.term.lock().scroll_display(Scroll::Bottom);
pane.write(bytes.into_bytes());
}
_ => pane.write(vec![0x16]),
}
}
pub(super) fn copy_selection(&mut self, clear: bool) -> bool {
let Some(st) = self.state.as_mut() else { return false };
let Some(pane) = st.panes.iter().find(|p| p.id == st.focused) else { return false };
let mut term = pane.term.lock();
let text = term.selection_to_string().filter(|s| !s.is_empty());
if clear {
term.selection = None;
}
drop(term);
match text {
Some(t) => {
if let Some(c) = st.clipboard.as_mut() {
let _ = c.set_text(t);
}
true
}
None => false,
}
}
}