Skip to content

Commit

Permalink
Terminal: right-click to add "clear all" function (lapce#3368)
Browse files Browse the repository at this point in the history
* Terminal: right-click to add "clear all" function

* format imports

* format imports
  • Loading branch information
jm-observer authored Jul 16, 2024
1 parent 8236725 commit 6ffa4bb
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 20 deletions.
6 changes: 6 additions & 0 deletions lapce-app/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{path::PathBuf, rc::Rc, sync::Arc};
pub use floem::views::editor::command::CommandExecuted;
use floem::{
keyboard::Modifiers, peniko::kurbo::Vec2, views::editor::command::Command,
ViewId,
};
use indexmap::IndexMap;
use lapce_core::command::{
Expand Down Expand Up @@ -732,6 +733,11 @@ pub enum InternalCommand {
program: String,
arguments: Vec<String>,
},
ClearTerminalBuffer {
view_id: ViewId,
tab_index: usize,
terminal_index: usize,
},
}

#[derive(Clone)]
Expand Down
73 changes: 53 additions & 20 deletions lapce-app/src/panel/terminal_view.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
use std::rc::Rc;

use floem::{
action::show_context_menu,
event::{Event, EventListener, EventPropagation},
kurbo::Size,
menu::{Menu, MenuItem},
reactive::create_rw_signal,
views::{
container, dyn_stack, empty, label,
scroll::{scroll, Thickness, VerticalScrollAsHorizontal},
stack, svg, tab, Decorators,
},
View,
View, ViewId,
};

use super::kind::PanelKind;
use crate::{
app::clickable_icon,
command::LapceWorkbenchCommand,
command::{InternalCommand, LapceWorkbenchCommand},
config::{color::LapceColor, icon::LapceIcons},
debug::RunDebugMode,
listener::Listener,
terminal::{
panel::TerminalPanelData, tab::TerminalTabData, view::terminal_view,
},
Expand Down Expand Up @@ -252,6 +255,7 @@ fn terminal_tab_header(window_tab_data: Rc<WindowTabData>) -> impl View {
fn terminal_tab_split(
terminal_panel_data: TerminalPanelData,
terminal_tab_data: TerminalTabData,
tab_index: usize,
) -> impl View {
let config = terminal_panel_data.common.config;
let internal_command = terminal_panel_data.common.internal_command;
Expand All @@ -273,7 +277,7 @@ fn terminal_tab_split(
let terminal_panel_data = terminal_panel_data.clone();
let terminal_scope = terminal.scope;
container({
terminal_view(
let terminal_view = terminal_view(
terminal.term_id,
terminal.raw.read_only(),
terminal.mode.read_only(),
Expand All @@ -282,22 +286,32 @@ fn terminal_tab_split(
terminal.launch_error,
internal_command,
workspace.clone(),
)
.on_event_cont(EventListener::PointerDown, move |_| {
active.set(index.get_untracked());
})
.on_event(EventListener::PointerWheel, move |event| {
if let Event::PointerWheel(pointer_event) = event {
terminal.clone().wheel_scroll(pointer_event.delta.y);
EventPropagation::Stop
} else {
EventPropagation::Continue
}
})
.on_cleanup(move || {
terminal_scope.dispose();
})
.style(|s| s.size_pct(100.0, 100.0))
);
let view_id = terminal_view.id();
terminal_view
.on_event_cont(EventListener::PointerDown, move |_| {
active.set(index.get_untracked());
})
.on_secondary_click_stop(move |_| {
tab_secondary_click(
internal_command,
view_id,
tab_index,
index.get_untracked(),
);
})
.on_event(EventListener::PointerWheel, move |event| {
if let Event::PointerWheel(pointer_event) = event {
terminal.clone().wheel_scroll(pointer_event.delta.y);
EventPropagation::Stop
} else {
EventPropagation::Continue
}
})
.on_cleanup(move || {
terminal_scope.dispose();
})
.style(|s| s.size_pct(100.0, 100.0))
})
.style(move |s| {
s.size_pct(100.0, 100.0).padding_horiz(10.0).apply_if(
Expand All @@ -323,7 +337,26 @@ fn terminal_tab_content(window_tab_data: Rc<WindowTabData>) -> impl View {
move || terminal.tab_info.with(|info| info.active),
move || terminal.tab_info.with(|info| info.tabs.clone()),
|(_, tab)| tab.terminal_tab_id,
move |(_, tab)| terminal_tab_split(terminal.clone(), tab),
move |(tab_index, tab)| {
terminal_tab_split(terminal.clone(), tab, tab_index.get_untracked())
},
)
.style(|s| s.size_pct(100.0, 100.0))
}

fn tab_secondary_click(
internal_command: Listener<InternalCommand>,
view_id: ViewId,
tab_index: usize,
terminal_index: usize,
) {
let mut menu = Menu::new("");
menu = menu.entry(MenuItem::new("Clear All").action(move || {
internal_command.send(InternalCommand::ClearTerminalBuffer {
view_id,
tab_index,
terminal_index,
});
}));
show_context_menu(menu, None);
}
33 changes: 33 additions & 0 deletions lapce-app/src/window_tab.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alacritty_terminal::vte::ansi::Handler;
use std::{
collections::{BTreeMap, HashSet},
env,
Expand Down Expand Up @@ -1790,6 +1791,38 @@ impl WindowTabData {
}
};
}
InternalCommand::ClearTerminalBuffer {
view_id,
tab_index,
terminal_index,
} => {
let Some(tab) = self.terminal.tab_info.with_untracked(|x| {
x.tabs.iter().find_map(|(index, data)| {
if index.get_untracked() == tab_index {
Some(data.clone())
} else {
None
}
})
}) else {
error!("cound not find terminal tab data: index={tab_index}");
return;
};
let Some(raw) = tab.terminals.with_untracked(|x| {
x.iter().find_map(|(index, data)| {
if index.get_untracked() == terminal_index {
Some(data.raw.get_untracked())
} else {
None
}
})
}) else {
error!("cound not find terminal data: index={terminal_index}");
return;
};
raw.write().term.reset_state();
view_id.request_paint();
}
}
}

Expand Down

0 comments on commit 6ffa4bb

Please sign in to comment.