Use platform shortcuts and scale terminal text per window
Cache font metrics across pane and tab switches. Resize text, cursors, and PTY grids together, using the active window’s font size when multiple windows share a shell.
This commit is contained in:
+92
-54
@@ -5,6 +5,7 @@ use crate::{
|
||||
colors,
|
||||
commands::{self, CommandAction},
|
||||
cursor::CursorAnimator,
|
||||
fonts::{self, TerminalFont},
|
||||
input,
|
||||
label::middle_label,
|
||||
palette::CommandPalette,
|
||||
@@ -13,7 +14,7 @@ use crate::{
|
||||
ClientMessage, Direction, PaneSnapshot, ScrollKind, SelectionCommand, SelectionKind,
|
||||
SelectionSide, Snapshot, StackPaneInfo, VerticalDirection,
|
||||
},
|
||||
render::{FONT_SIZE, LINE_HEIGHT, RenderCell, Renderer},
|
||||
render::{RenderCell, Renderer},
|
||||
transition::Slide,
|
||||
};
|
||||
use alacritty_terminal::{
|
||||
@@ -38,6 +39,7 @@ struct TabRename {
|
||||
pub struct Workspace {
|
||||
client: Client,
|
||||
connection: SharedString,
|
||||
font: TerminalFont,
|
||||
snapshot: Option<Arc<Snapshot>>,
|
||||
panes: HashMap<u64, Entity<TerminalPane>>,
|
||||
error: Option<String>,
|
||||
@@ -157,28 +159,25 @@ impl Workspace {
|
||||
}
|
||||
|
||||
fn key_down(&mut self, event: &KeyDownEvent, window: &mut Window, cx: &mut Context<Self>) {
|
||||
let key = &event.keystroke;
|
||||
let command = (key.modifiers.control || key.modifiers.platform)
|
||||
&& key.modifiers.shift
|
||||
&& !key.modifiers.alt;
|
||||
if command && key.key.eq_ignore_ascii_case("p") {
|
||||
self.open_palette(window, cx);
|
||||
cx.stop_propagation();
|
||||
let Some(action) = crate::bindings::action_for_key(&event.keystroke) else {
|
||||
return;
|
||||
}
|
||||
if self.palette.is_some()
|
||||
};
|
||||
// Window commands remain available while a text field has focus.
|
||||
if !matches!(
|
||||
action,
|
||||
CommandAction::TogglePalette
|
||||
| CommandAction::IncreaseFontSize
|
||||
| CommandAction::DecreaseFontSize
|
||||
) && (self.palette.is_some()
|
||||
|| self.tab_rename.is_some()
|
||||
|| self
|
||||
.focused_pane()
|
||||
.is_some_and(|pane| pane.read(cx).rename.is_some())
|
||||
.is_some_and(|pane| pane.read(cx).rename.is_some()))
|
||||
{
|
||||
return;
|
||||
}
|
||||
let action = crate::bindings::action_for_key(key);
|
||||
if let Some(action) = action {
|
||||
self.execute(action, window, cx);
|
||||
cx.stop_propagation();
|
||||
}
|
||||
self.execute(action, window, cx);
|
||||
cx.stop_propagation();
|
||||
}
|
||||
|
||||
fn execute(&mut self, action: CommandAction, window: &mut Window, cx: &mut Context<Self>) {
|
||||
@@ -186,6 +185,28 @@ impl Workspace {
|
||||
let pane = self.focused_pane();
|
||||
let pane_id = pane.as_ref().map(|pane| pane.read(cx).data().id);
|
||||
let message = match action {
|
||||
TogglePalette => {
|
||||
self.open_palette(window, cx);
|
||||
None
|
||||
}
|
||||
IncreaseFontSize | DecreaseFontSize => {
|
||||
let delta = if matches!(action, IncreaseFontSize) {
|
||||
1.
|
||||
} else {
|
||||
-1.
|
||||
};
|
||||
if self.font.adjust_size(delta, window.text_system()) {
|
||||
for pane in self.panes.values().chain(self.outgoing.values()) {
|
||||
pane.update(cx, |pane, cx| {
|
||||
pane.renderer = Renderer::new(self.font.clone());
|
||||
pane.prepared_revision = None;
|
||||
pane.requested_size = None;
|
||||
cx.notify();
|
||||
});
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
NewTab => Some(ClientMessage::NewTab { cwd: None }),
|
||||
SplitPane => pane_id.map(|pane| ClientMessage::Split { pane, cwd: None }),
|
||||
StackAbove | StackBelow => Some(ClientMessage::NewStackPane {
|
||||
@@ -278,6 +299,9 @@ impl Workspace {
|
||||
) -> Self {
|
||||
let focus = cx.focus_handle();
|
||||
window.focus(&focus);
|
||||
// Scene transitions rebuild pane views. Keep system font discovery and
|
||||
// cell measurement outside that path, once per workspace.
|
||||
let font = fonts::terminal_font(window.text_system());
|
||||
let task = cx.spawn_in(window, async move |view, cx| {
|
||||
while let Some(update) = updates.recv().await {
|
||||
if cx
|
||||
@@ -374,10 +398,13 @@ impl Workspace {
|
||||
frame,
|
||||
index,
|
||||
active == Some(data.id),
|
||||
focus_allowed,
|
||||
view.font.clone(),
|
||||
window,
|
||||
cx,
|
||||
);
|
||||
if pane.active && focus_allowed {
|
||||
window.focus(&pane.focus);
|
||||
}
|
||||
pane.transitioning = view.transition.is_some();
|
||||
pane
|
||||
});
|
||||
@@ -417,6 +444,7 @@ impl Workspace {
|
||||
Self {
|
||||
client,
|
||||
connection,
|
||||
font,
|
||||
snapshot: None,
|
||||
panes: HashMap::new(),
|
||||
error: None,
|
||||
@@ -703,9 +731,7 @@ struct TerminalPane {
|
||||
read_only: bool,
|
||||
transitioning: bool,
|
||||
focus: FocusHandle,
|
||||
font: Font,
|
||||
bounds: Bounds<Pixels>,
|
||||
cell_width: f32,
|
||||
selecting: bool,
|
||||
scroll_remainder: f32,
|
||||
preedit: String,
|
||||
@@ -725,23 +751,17 @@ impl TerminalPane {
|
||||
snapshot: Arc<Snapshot>,
|
||||
index: usize,
|
||||
active: bool,
|
||||
focus_allowed: bool,
|
||||
font: TerminalFont,
|
||||
window: &mut Window,
|
||||
cx: &mut Context<Self>,
|
||||
) -> Self {
|
||||
let font = crate::fonts::terminal_font(window.text_system());
|
||||
let font_id = window.text_system().resolve_font(&font);
|
||||
let cell_width = window
|
||||
.text_system()
|
||||
.advance(font_id, px(FONT_SIZE), 'M')
|
||||
.map(|s| f32::from(s.width))
|
||||
.unwrap_or(9.)
|
||||
.max(1.);
|
||||
let focus = cx.focus_handle();
|
||||
if active && focus_allowed {
|
||||
window.focus(&focus);
|
||||
}
|
||||
let activation = cx.observe_window_activation(window, |pane, _, cx| {
|
||||
let activation = cx.observe_window_activation(window, |pane, window, cx| {
|
||||
if window.is_window_active() {
|
||||
// Each attached window has its own zoom and viewport. The focused
|
||||
// window sets the shared PTY dimensions.
|
||||
pane.requested_size = None;
|
||||
}
|
||||
pane.blink_timer = None;
|
||||
cx.notify();
|
||||
});
|
||||
@@ -753,15 +773,13 @@ impl TerminalPane {
|
||||
read_only: false,
|
||||
transitioning: false,
|
||||
focus,
|
||||
font,
|
||||
bounds: Bounds::default(),
|
||||
cell_width,
|
||||
selecting: false,
|
||||
scroll_remainder: 0.,
|
||||
preedit: String::new(),
|
||||
cursor_bounds: Bounds::default(),
|
||||
cursor: CursorAnimator::new(),
|
||||
renderer: Renderer::default(),
|
||||
renderer: Renderer::new(font),
|
||||
rename: None,
|
||||
prepared_revision: None,
|
||||
requested_size: None,
|
||||
@@ -916,9 +934,11 @@ impl TerminalPane {
|
||||
}
|
||||
}
|
||||
fn grid_point(&self, position: Point<Pixels>) -> (u16, u16, SelectionSide) {
|
||||
let x = ((f32::from(position.x - self.bounds.left()) - PADDING).max(0.) / self.cell_width)
|
||||
let x = ((f32::from(position.x - self.bounds.left()) - PADDING).max(0.)
|
||||
/ self.renderer.font.cell_width)
|
||||
.min(f32::from(self.data().columns - 1));
|
||||
let y = ((f32::from(position.y - self.bounds.top()) - PADDING).max(0.) / LINE_HEIGHT)
|
||||
let y = ((f32::from(position.y - self.bounds.top()) - PADDING).max(0.)
|
||||
/ self.renderer.font.line_height)
|
||||
.min(f32::from(self.data().lines - 1));
|
||||
(
|
||||
x as u16,
|
||||
@@ -970,7 +990,7 @@ impl TerminalPane {
|
||||
}
|
||||
self.scroll_remainder += match event.delta {
|
||||
ScrollDelta::Lines(d) => d.y * 3.,
|
||||
ScrollDelta::Pixels(d) => f32::from(d.y) / LINE_HEIGHT,
|
||||
ScrollDelta::Pixels(d) => f32::from(d.y) / self.renderer.font.line_height,
|
||||
};
|
||||
let lines = self.scroll_remainder.trunc() as i32;
|
||||
self.scroll_remainder -= lines as f32;
|
||||
@@ -996,20 +1016,24 @@ impl TerminalPane {
|
||||
}
|
||||
}
|
||||
fn prepare(&mut self, bounds: Bounds<Pixels>, window: &mut Window) {
|
||||
let columns = ((f32::from(bounds.size.width) - 2. * PADDING) / self.cell_width)
|
||||
let font = &self.renderer.font;
|
||||
let columns = ((f32::from(bounds.size.width) - 2. * PADDING) / font.cell_width)
|
||||
.floor()
|
||||
.clamp(2., 1000.) as u16;
|
||||
let lines = ((f32::from(bounds.size.height) - 2. * PADDING) / LINE_HEIGHT)
|
||||
let lines = ((f32::from(bounds.size.height) - 2. * PADDING) / font.line_height)
|
||||
.floor()
|
||||
.clamp(1., 500.) as u16;
|
||||
if !self.read_only && self.requested_size != Some((columns, lines)) {
|
||||
if !self.read_only
|
||||
&& window.is_window_active()
|
||||
&& self.requested_size != Some((columns, lines))
|
||||
{
|
||||
self.requested_size = Some((columns, lines));
|
||||
self.command(ClientMessage::Resize {
|
||||
pane: self.data().id,
|
||||
columns,
|
||||
lines,
|
||||
cell_width: self.cell_width.round() as u16,
|
||||
cell_height: LINE_HEIGHT as u16,
|
||||
cell_width: font.cell_width.round() as u16,
|
||||
cell_height: font.line_height as u16,
|
||||
});
|
||||
}
|
||||
let prepare_rows = self.prepared_revision != Some(self.snapshot.revision)
|
||||
@@ -1038,12 +1062,12 @@ impl TerminalPane {
|
||||
let cursor_bounds = Bounds::new(
|
||||
bounds.origin
|
||||
+ point(
|
||||
px(PADDING + f32::from(cursor.column) * self.cell_width),
|
||||
px(PADDING + cursor.row as f32 * LINE_HEIGHT),
|
||||
px(PADDING + f32::from(cursor.column) * font.cell_width),
|
||||
px(PADDING + cursor.row as f32 * font.line_height),
|
||||
),
|
||||
size(
|
||||
px(self.cell_width * if wide { 2. } else { 1. }),
|
||||
px(LINE_HEIGHT),
|
||||
px(font.cell_width * if wide { 2. } else { 1. }),
|
||||
px(font.line_height),
|
||||
),
|
||||
);
|
||||
let mut cursor_color = colors::resolve(Color::Named(NamedColor::Cursor), &palette);
|
||||
@@ -1087,8 +1111,7 @@ impl TerminalPane {
|
||||
.collect()
|
||||
})
|
||||
.collect();
|
||||
self.renderer
|
||||
.prepare(rows, &self.font, self.cell_width, window);
|
||||
self.renderer.prepare(rows, window);
|
||||
self.prepared_revision = Some(self.snapshot.revision);
|
||||
log::debug!(target:"psyterm::timing","prepare_us={}",started.elapsed().as_micros());
|
||||
}
|
||||
@@ -1150,11 +1173,26 @@ impl Render for TerminalPane {
|
||||
}
|
||||
});
|
||||
if !pane.preedit.is_empty() {
|
||||
let run=TextRun{len:pane.preedit.len(),font:pane.font.clone(),color:rgb(chrome::TEXT).into(),background_color:Some(rgb(chrome::TERMINAL_BACKGROUND).into()),
|
||||
underline:Some(UnderlineStyle{thickness:px(1.),color:None,wavy:false}),strikethrough:None};
|
||||
let shaped=window.text_system().shape_line(pane.preedit.clone().into(),px(FONT_SIZE),&[run],None);
|
||||
let _=shaped.paint_background(pane.cursor_bounds.origin,px(LINE_HEIGHT),window,cx);
|
||||
let _=shaped.paint(pane.cursor_bounds.origin,px(LINE_HEIGHT),window,cx);
|
||||
let font = &pane.renderer.font;
|
||||
let run = TextRun {
|
||||
len: pane.preedit.len(),
|
||||
font: font.face.clone(),
|
||||
color: rgb(chrome::TEXT).into(),
|
||||
background_color: Some(rgb(chrome::TERMINAL_BACKGROUND).into()),
|
||||
underline: Some(UnderlineStyle {
|
||||
thickness: px(1.),
|
||||
color: None,
|
||||
wavy: false,
|
||||
}),
|
||||
strikethrough: None,
|
||||
};
|
||||
let line = window.text_system().shape_line(
|
||||
pane.preedit.clone().into(), px(font.size), &[run], None,
|
||||
);
|
||||
let origin = pane.cursor_bounds.origin;
|
||||
let height = px(font.line_height);
|
||||
let _ = line.paint_background(origin, height, window, cx);
|
||||
let _ = line.paint(origin, height, window, cx);
|
||||
}
|
||||
});
|
||||
pane.schedule_cursor(delay,window,cx);
|
||||
|
||||
Reference in New Issue
Block a user