Skip to content

Commit

Permalink
Use extended Iosevka and improve cursor rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
melody-rs committed Feb 28, 2024
1 parent 26b4204 commit 893bad2
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 30 deletions.
Binary file added assets/fonts/IosevkaTermNerdFont-Extended.ttf
Binary file not shown.
Binary file removed assets/fonts/IosevkaTermNerdFont-Regular.ttf
Binary file not shown.
87 changes: 60 additions & 27 deletions crates/term/src/widget/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use alacritty_terminal::event::Event;
use alacritty_terminal::term::cell::Flags;
use alacritty_terminal::term::TermMode;
use alacritty_terminal::vte::ansi::CursorShape;
use alacritty_terminal::{event_loop::Msg, grid::Dimensions};

use crate::backends::Backend;
Expand All @@ -37,6 +38,7 @@ pub use theme::Theme;
pub struct Terminal<T> {
backend: T,
theme: Theme, // TODO convert into shared config (possibly do this in luminol-preferences)
stable_time: f32,
pub id: egui::Id,
pub title: String,
}
Expand All @@ -56,6 +58,7 @@ impl<T> Terminal<T> {
Self {
backend,
id: egui::Id::new("luminol_term_terminal"),
stable_time: 0.0,
theme: Theme::default(),
title: "Luminol Terminal".to_string(),
}
Expand Down Expand Up @@ -160,7 +163,6 @@ where
char_width * term.columns() as f32,
row_height * term.screen_lines() as f32,
);

let (response, painter) =
ui.allocate_painter(terminal_size, egui::Sense::click_and_drag());

Expand All @@ -171,15 +173,8 @@ where
let mut buf = [0; 4];
let text = cell.c.encode_utf8(&mut buf);

let (color, background) =
if cell.point == term.grid().cursor.point && response.has_focus() {
(egui::Color32::BLACK, egui::Color32::WHITE)
} else {
(
self.theme.get_ansi_color(cell.fg),
self.theme.get_ansi_color(cell.bg),
)
};
let color = self.theme.get_ansi_color(cell.fg);
let background = self.theme.get_ansi_color(cell.bg);

let italics = cell.flags.contains(Flags::ITALIC);
let underline = cell
Expand Down Expand Up @@ -229,25 +224,54 @@ where
response.request_focus();
}

let cursor_pos = term.grid().cursor.point;
let ppp = painter.ctx().pixels_per_point();
let mut cursor_shape = term.cursor_style().shape;
if !response.has_focus() {
cursor_shape = CursorShape::HollowBlock;
}

let cursor_rect = egui::Rect::from_min_size(
egui::pos2(
cursor_pos.column.0 as f32 * char_width + response.rect.min.x,
cursor_pos.line.0 as f32 * row_height + response.rect.min.y + 1.,
),
egui::vec2(char_width - ppp, row_height - ppp),
self.stable_time += ui.input(|i| i.stable_dt.min(0.1));
let (mut inner_color, outer_color) = match cursor_shape {
CursorShape::Block | CursorShape::Underline | CursorShape::Beam => {
(egui::Color32::WHITE, egui::Color32::WHITE)
}
CursorShape::HollowBlock => (egui::Color32::TRANSPARENT, egui::Color32::WHITE),
CursorShape::Hidden => (egui::Color32::TRANSPARENT, egui::Color32::TRANSPARENT),
};
if !term.cursor_style().blinking {
let sin_component = self.stable_time / std::f32::consts::FRAC_PI_2 * 13.;
let alpha = (sin_component.sin() + 1.) / 2.;
inner_color = inner_color.gamma_multiply(alpha);
}

let cursor_point = term.grid().cursor.point;
let mut cursor_pos = egui::pos2(
cursor_point.column.0 as f32 * char_width + response.rect.min.x + 1.,
cursor_point.line.0 as f32 * row_height + response.rect.min.y,
);

if !response.has_focus() {
painter.rect(
cursor_rect,
egui::Rounding::ZERO,
egui::Color32::TRANSPARENT,
egui::Stroke::new(1.0, egui::Color32::WHITE),
);
}
let cursor_rect = match cursor_shape {
CursorShape::Block | CursorShape::HollowBlock | CursorShape::Hidden => {
egui::Rect::from_min_size(cursor_pos, egui::vec2(char_width, row_height))
}
CursorShape::Beam => {
egui::Rect::from_min_size(cursor_pos, egui::vec2(2.0, row_height))
}
CursorShape::Underline => {
cursor_pos.y += row_height - 2.0;
egui::Rect::from_min_size(cursor_pos, egui::vec2(char_width, 2.0))
}
};

painter.rect(
cursor_rect,
egui::Rounding::ZERO,
inner_color,
egui::Stroke::new(1.0, outer_color),
);

painter
.ctx()
.request_repaint_after(std::time::Duration::from_millis(16));

response
});
Expand All @@ -264,12 +288,14 @@ where

fn process_egui_events(&mut self, events: Vec<egui::Event>, modifiers: egui::Modifiers) {
let term_mode = self.backend.with_term(|term| *term.mode());
let mut term_modified = false;
for event in events {
match event {
egui::Event::Paste(text) | egui::Event::Text(text) => {
let bytes = text.into_bytes();
let cow = std::borrow::Cow::Owned(bytes);
self.backend.send(Msg::Input(cow));
term_modified = true;
}
egui::Event::Key {
key, pressed: true, ..
Expand All @@ -278,9 +304,10 @@ where
let cow = std::borrow::Cow::Borrowed(bytes);
self.backend.send(Msg::Input(cow));
}
term_modified = true;
}
egui::Event::Scroll(scroll_delta) => {
let delta = scroll_delta.y as i32;
let delta = (scroll_delta.y / 16.) as i32;
let alt_scroll = self.backend.with_term(|term| {
term.mode()
.contains(TermMode::ALT_SCREEN | TermMode::ALTERNATE_SCROLL)
Expand All @@ -307,6 +334,12 @@ where
_ => {}
}
}

if term_modified {
self.backend.with_term(|term| {
term.scroll_display(alacritty_terminal::grid::Scroll::Bottom);
})
}
}

pub fn kill(&mut self) {
Expand Down
9 changes: 6 additions & 3 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ impl App {
);
#[cfg(not(target_arch = "wasm32"))]
fonts.font_data.insert(
"Iosevka Term Regular".to_owned(),
"Iosevka Term".to_owned(),
egui::FontData::from_static(luminol_macros::include_asset!(
"assets/fonts/IosevkaTermNerdFont-Regular.ttf"
"assets/fonts/IosevkaTermNerdFont-Extended.ttf"
)),
);

Expand All @@ -132,7 +132,10 @@ impl App {
#[cfg(not(target_arch = "wasm32"))]
fonts.families.insert(
egui::FontFamily::Name("Iosevka Term".into()),
vec!["Iosevka Term Regular".to_owned()],
vec![
"Iosevka Term".to_owned(),
"Source Han Sans Regular".to_owned(),
],
);
cc.egui_ctx.set_fonts(fonts);

Expand Down

0 comments on commit 893bad2

Please sign in to comment.