Skip to content

Commit 2801a1d

Browse files
committed
chore: clean code
1 parent a3bbf6d commit 2801a1d

File tree

9 files changed

+83
-86
lines changed

9 files changed

+83
-86
lines changed

crates/egui-term/examples/custom_bindings.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ impl eframe::App for App {
9292
active_tab_id: &mut self.active_id,
9393
};
9494
let terminal = TerminalView::new(ui, term_ctx, term_opt)
95-
.set_focus(true)
9695
.add_bindings(self.custom_terminal_bindings.clone())
9796
.set_size(Vec2::new(ui.available_width(), ui.available_height()));
9897

crates/egui-term/examples/tabs.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,8 @@ impl eframe::App for App {
8181
default_font_size: 14.,
8282
active_tab_id: &mut self.active_tab,
8383
};
84-
let terminal = TerminalView::new(ui, term_ctx, term_opt)
85-
.set_focus(true)
86-
.set_size(ui.available_size());
84+
let terminal =
85+
TerminalView::new(ui, term_ctx, term_opt).set_size(ui.available_size());
8786

8887
ui.add(terminal);
8988
}

crates/egui-term/examples/themes.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ impl eframe::App for App {
107107
active_tab_id: &mut self.active_id,
108108
};
109109
let terminal = TerminalView::new(ui, term_ctx, term_opt)
110-
.set_focus(true)
111110
.set_size(Vec2::new(ui.available_width(), ui.available_height()));
112111

113112
ui.add(terminal);

crates/egui-term/src/display/mod.rs

Lines changed: 1 addition & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,8 @@ use alacritty_terminal::grid::GridCell;
88
use alacritty_terminal::term::cell::Flags;
99
use alacritty_terminal::term::TermMode;
1010
use alacritty_terminal::vte::ansi::{Color, NamedColor};
11-
use copypasta::ClipboardProvider;
1211
use egui::epaint::RectShape;
13-
use egui::{
14-
Align2, Button, CornerRadius, CursorIcon, Key, KeyboardShortcut, Modifiers, Painter, Pos2,
15-
Rect, Response, Vec2, WidgetText,
16-
};
12+
use egui::{Align2, CornerRadius, CursorIcon, Painter, Pos2, Rect, Response, Vec2};
1713
use egui::{Shape, Stroke};
1814

1915
impl TerminalView<'_> {
@@ -146,75 +142,3 @@ impl TerminalView<'_> {
146142
painter.extend(shapes);
147143
}
148144
}
149-
150-
impl TerminalView<'_> {
151-
pub fn context_menu(&mut self, layout: &Response) {
152-
layout.context_menu(|ui| {
153-
let width = 200.;
154-
ui.set_width(width);
155-
// copy btn
156-
self.copy_btn(ui, layout, width);
157-
// paste btn
158-
self.paste_btn(ui, width);
159-
160-
ui.separator();
161-
// select all btn
162-
self.select_all_btn(ui, width);
163-
});
164-
}
165-
166-
fn copy_btn(&mut self, ui: &mut egui::Ui, layout: &Response, btn_width: f32) {
167-
#[cfg(not(target_os = "macos"))]
168-
let copy_shortcut = KeyboardShortcut::new(Modifiers::CTRL | Modifiers::SHIFT, Key::C);
169-
#[cfg(target_os = "macos")]
170-
let copy_shortcut = KeyboardShortcut::new(Modifiers::MAC_CMD, Key::C);
171-
let copy_shortcut = ui.ctx().format_shortcut(&copy_shortcut);
172-
let copy_btn = context_btn("Copy", btn_width, Some(copy_shortcut));
173-
if ui.add(copy_btn).clicked() {
174-
let data = self.term_ctx.selection_content();
175-
layout.ctx.copy_text(data);
176-
ui.close();
177-
}
178-
}
179-
180-
fn paste_btn(&mut self, ui: &mut egui::Ui, btn_width: f32) {
181-
#[cfg(not(target_os = "macos"))]
182-
let paste_shortcut = KeyboardShortcut::new(Modifiers::CTRL | Modifiers::SHIFT, Key::V);
183-
#[cfg(target_os = "macos")]
184-
let paste_shortcut = KeyboardShortcut::new(Modifiers::MAC_CMD, Key::V);
185-
let paste_shortcut = ui.ctx().format_shortcut(&paste_shortcut);
186-
let paste_btn = context_btn("Paste", btn_width, Some(paste_shortcut));
187-
if ui.add(paste_btn).clicked() {
188-
if let Ok(data) = self.term_ctx.clipboard.get_contents() {
189-
self.term_ctx.write_data(data.into_bytes());
190-
self.term_ctx.terminal.selection = None;
191-
}
192-
ui.close();
193-
}
194-
}
195-
196-
fn select_all_btn(&mut self, ui: &mut egui::Ui, btn_width: f32) {
197-
#[cfg(not(target_os = "macos"))]
198-
let select_all_shortcut = KeyboardShortcut::new(Modifiers::CTRL, Key::A);
199-
#[cfg(target_os = "macos")]
200-
let select_all_shortcut = KeyboardShortcut::new(Modifiers::MAC_CMD, Key::A);
201-
let select_all_shortcut = ui.ctx().format_shortcut(&select_all_shortcut);
202-
let select_all_btn = context_btn("Select All", btn_width, Some(select_all_shortcut));
203-
if ui.add(select_all_btn).clicked() {
204-
self.term_ctx.select_all();
205-
ui.close();
206-
}
207-
}
208-
}
209-
210-
fn context_btn<'a>(
211-
text: impl Into<WidgetText>,
212-
width: f32,
213-
shortcut: Option<String>,
214-
) -> Button<'a> {
215-
let mut btn = Button::new(text).min_size((width, 0.).into());
216-
if let Some(shortcut) = shortcut {
217-
btn = btn.shortcut_text(shortcut);
218-
}
219-
btn
220-
}

crates/egui-term/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ mod scroll_bar;
88
mod ssh;
99
mod theme;
1010
mod types;
11+
mod ui;
1112
mod view;
1213

1314
pub use alacritty::{PtyEvent, TermType, Terminal, TerminalContext};

crates/egui-term/src/ui/menu.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
use crate::TerminalView;
2+
use copypasta::ClipboardProvider;
3+
use egui::{Button, Key, KeyboardShortcut, Modifiers, Response, WidgetText};
4+
5+
impl TerminalView<'_> {
6+
pub fn context_menu(&mut self, layout: &Response) {
7+
layout.context_menu(|ui| {
8+
let width = 200.;
9+
ui.set_width(width);
10+
// copy btn
11+
self.copy_btn(ui, layout, width);
12+
// paste btn
13+
self.paste_btn(ui, width);
14+
15+
ui.separator();
16+
// select all btn
17+
self.select_all_btn(ui, width);
18+
});
19+
}
20+
21+
fn copy_btn(&mut self, ui: &mut egui::Ui, layout: &Response, btn_width: f32) {
22+
#[cfg(not(target_os = "macos"))]
23+
let copy_shortcut = KeyboardShortcut::new(Modifiers::CTRL | Modifiers::SHIFT, Key::C);
24+
#[cfg(target_os = "macos")]
25+
let copy_shortcut = KeyboardShortcut::new(Modifiers::MAC_CMD, Key::C);
26+
let copy_shortcut = ui.ctx().format_shortcut(&copy_shortcut);
27+
let copy_btn = context_btn("Copy", btn_width, Some(copy_shortcut));
28+
if ui.add(copy_btn).clicked() {
29+
let data = self.term_ctx.selection_content();
30+
layout.ctx.copy_text(data);
31+
ui.close();
32+
}
33+
}
34+
35+
fn paste_btn(&mut self, ui: &mut egui::Ui, btn_width: f32) {
36+
#[cfg(not(target_os = "macos"))]
37+
let paste_shortcut = KeyboardShortcut::new(Modifiers::CTRL | Modifiers::SHIFT, Key::V);
38+
#[cfg(target_os = "macos")]
39+
let paste_shortcut = KeyboardShortcut::new(Modifiers::MAC_CMD, Key::V);
40+
let paste_shortcut = ui.ctx().format_shortcut(&paste_shortcut);
41+
let paste_btn = context_btn("Paste", btn_width, Some(paste_shortcut));
42+
if ui.add(paste_btn).clicked() {
43+
if let Ok(data) = self.term_ctx.clipboard.get_contents() {
44+
self.term_ctx.write_data(data.into_bytes());
45+
self.term_ctx.terminal.selection = None;
46+
}
47+
ui.close();
48+
}
49+
}
50+
51+
fn select_all_btn(&mut self, ui: &mut egui::Ui, btn_width: f32) {
52+
#[cfg(not(target_os = "macos"))]
53+
let select_all_shortcut = KeyboardShortcut::new(Modifiers::CTRL, Key::A);
54+
#[cfg(target_os = "macos")]
55+
let select_all_shortcut = KeyboardShortcut::new(Modifiers::MAC_CMD, Key::A);
56+
let select_all_shortcut = ui.ctx().format_shortcut(&select_all_shortcut);
57+
let select_all_btn = context_btn("Select All", btn_width, Some(select_all_shortcut));
58+
if ui.add(select_all_btn).clicked() {
59+
self.term_ctx.select_all();
60+
ui.close();
61+
}
62+
}
63+
}
64+
65+
fn context_btn<'a>(
66+
text: impl Into<WidgetText>,
67+
width: f32,
68+
shortcut: Option<String>,
69+
) -> Button<'a> {
70+
let mut btn = Button::new(text).min_size((width, 0.).into());
71+
if let Some(shortcut) = shortcut {
72+
btn = btn.shortcut_text(shortcut);
73+
}
74+
btn
75+
}

crates/egui-term/src/ui/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mod menu;

crates/egui-term/src/view.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ impl<'a> TerminalView<'a> {
139139

140140
Self {
141141
widget_id,
142-
has_focus: false,
142+
has_focus: true,
143143
size: ui.available_size(),
144144
term_ctx,
145145
options,

nxshell/src/ui/tab_view/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,8 @@ impl egui_dock::TabViewer for TabViewer<'_> {
118118
active_tab_id: &mut self.options.active_tab_id,
119119
};
120120

121-
let terminal = TerminalView::new(ui, term_ctx, term_opt)
122-
.set_focus(true)
123-
.set_size(ui.available_size());
121+
let terminal =
122+
TerminalView::new(ui, term_ctx, term_opt).set_size(ui.available_size());
124123
ui.add(terminal);
125124
}
126125
TabInner::SessionList(_list) => {

0 commit comments

Comments
 (0)