mirror of
https://gitlab.com/gabmus/envision.git
synced 2025-08-02 22:29:01 +00:00
feat: add right click menu for select all and copy to term view
This commit is contained in:
parent
002d178bd5
commit
23ad1166f3
1 changed files with 72 additions and 8 deletions
|
@ -1,6 +1,6 @@
|
||||||
use gtk4::{gdk, glib::clone};
|
use gtk4::{gdk, glib::clone, prelude::*};
|
||||||
use relm4::adw;
|
use relm4::adw;
|
||||||
use vte4::{Terminal, TerminalExt, TerminalExtManual, WidgetExt};
|
use vte4::{Terminal, TerminalExt, TerminalExtManual};
|
||||||
|
|
||||||
use super::util::copy_text;
|
use super::util::copy_text;
|
||||||
|
|
||||||
|
@ -8,8 +8,9 @@ const MAX_SCROLLBACK: u32 = 2000;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct TermWidget {
|
pub struct TermWidget {
|
||||||
pub container: gtk4::ScrolledWindow,
|
pub container: gtk4::Box,
|
||||||
pub term: Terminal,
|
pub term: Terminal,
|
||||||
|
popover_menu: gtk4::PopoverMenu,
|
||||||
}
|
}
|
||||||
|
|
||||||
const ADW_LIGHT_FG: &str = "#000000";
|
const ADW_LIGHT_FG: &str = "#000000";
|
||||||
|
@ -30,19 +31,84 @@ impl TermWidget {
|
||||||
.build();
|
.build();
|
||||||
term.set_clear_background(false);
|
term.set_clear_background(false);
|
||||||
term.search_set_wrap_around(true);
|
term.search_set_wrap_around(true);
|
||||||
let container = gtk4::ScrolledWindow::builder()
|
let container = gtk4::Box::builder().hexpand(true).vexpand(true).build();
|
||||||
|
|
||||||
|
let sw = gtk4::ScrolledWindow::builder()
|
||||||
.hexpand(true)
|
.hexpand(true)
|
||||||
.vexpand(true)
|
.vexpand(true)
|
||||||
.child(&term)
|
.child(&term)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let this = Self { container, term };
|
container.append(&sw);
|
||||||
|
|
||||||
|
let action_grp = gtk4::gio::SimpleActionGroup::new();
|
||||||
|
let selectall_action = gtk4::gio::SimpleAction::new("selectall", None);
|
||||||
|
selectall_action.connect_activate(clone!(
|
||||||
|
#[strong]
|
||||||
|
term,
|
||||||
|
move |_, _| {
|
||||||
|
term.select_all();
|
||||||
|
}
|
||||||
|
));
|
||||||
|
action_grp.add_action(&selectall_action);
|
||||||
|
let copy_action = gtk4::gio::SimpleAction::new("copy", None);
|
||||||
|
copy_action.connect_activate(clone!(
|
||||||
|
#[strong]
|
||||||
|
term,
|
||||||
|
move |_, _| {
|
||||||
|
Self::copy_selected(&term);
|
||||||
|
}
|
||||||
|
));
|
||||||
|
action_grp.add_action(©_action);
|
||||||
|
|
||||||
|
container.insert_action_group("term", Some(&action_grp));
|
||||||
|
|
||||||
|
let menu = gtk4::gio::Menu::new();
|
||||||
|
menu.append(Some("Select all"), Some("term.selectall"));
|
||||||
|
menu.append(Some("Copy"), Some("term.copy"));
|
||||||
|
|
||||||
|
let popover_menu = gtk4::PopoverMenu::builder()
|
||||||
|
.pointing_to(&term.allocation())
|
||||||
|
.autohide(true)
|
||||||
|
.menu_model(&menu)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
container.append(&popover_menu);
|
||||||
|
|
||||||
|
let this = Self {
|
||||||
|
container,
|
||||||
|
term,
|
||||||
|
popover_menu,
|
||||||
|
};
|
||||||
|
|
||||||
this.setup_shortcuts();
|
this.setup_shortcuts();
|
||||||
|
this.setup_rightclick_gesture();
|
||||||
|
|
||||||
this
|
this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn setup_rightclick_gesture(&self) {
|
||||||
|
let gesture = gtk4::GestureClick::builder()
|
||||||
|
.button(gtk4::gdk::BUTTON_SECONDARY)
|
||||||
|
.build();
|
||||||
|
gesture.connect_pressed(clone!(
|
||||||
|
#[strong(rename_to=popover)]
|
||||||
|
self.popover_menu,
|
||||||
|
move |gesture, _, x, y| {
|
||||||
|
gesture.set_state(gtk4::EventSequenceState::Claimed);
|
||||||
|
popover.set_pointing_to(Some(>k4::gdk::Rectangle::new(x as i32, y as i32, 1, 1)));
|
||||||
|
popover.popup();
|
||||||
|
}
|
||||||
|
));
|
||||||
|
self.term.add_controller(gesture);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn copy_selected(term: &Terminal) {
|
||||||
|
if let Some(text) = term.text_selected(vte4::Format::Text) {
|
||||||
|
copy_text(text.as_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn setup_shortcuts(&self) {
|
fn setup_shortcuts(&self) {
|
||||||
let sc = gtk4::ShortcutController::new();
|
let sc = gtk4::ShortcutController::new();
|
||||||
["<Control>c", "<Shift><Control>c"]
|
["<Control>c", "<Shift><Control>c"]
|
||||||
|
@ -54,9 +120,7 @@ impl TermWidget {
|
||||||
#[strong(rename_to = term)]
|
#[strong(rename_to = term)]
|
||||||
self.term,
|
self.term,
|
||||||
move |_, _| {
|
move |_, _| {
|
||||||
if let Some(text) = term.text_selected(vte4::Format::Text) {
|
Self::copy_selected(&term);
|
||||||
copy_text(text.as_str());
|
|
||||||
}
|
|
||||||
gtk4::glib::Propagation::Proceed
|
gtk4::glib::Propagation::Proceed
|
||||||
}
|
}
|
||||||
))),
|
))),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue