Skip to content

Commit

Permalink
add zooming
Browse files Browse the repository at this point in the history
  • Loading branch information
Kl4rry committed Dec 4, 2024
1 parent 8dec49d commit 059bc5d
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 76 deletions.
9 changes: 9 additions & 0 deletions crates/ferrite-core/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ pub enum Cmd {
Number(Option<i64>),
OpenFileExplorer(Option<PathBuf>),
TrimTrailingWhitespace,
ZoomIn,
ZoomOut,
ResetZoom,
}

impl Cmd {
Expand Down Expand Up @@ -260,6 +263,9 @@ impl Cmd {
Number(_) => "Number",
OpenFileExplorer(_) => "Open file explorer",
TrimTrailingWhitespace => "Trim trailing whitespace",
ZoomIn => "Zoom in",
ZoomOut => "Zoom out",
ResetZoom => "Reset zoom",
}
}

Expand Down Expand Up @@ -363,6 +369,9 @@ impl Cmd {
Number(_) => false,
OpenFileExplorer(_) => false,
TrimTrailingWhitespace => false,
ZoomIn => false,
ZoomOut => false,
ResetZoom => false,
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions crates/ferrite-core/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub struct Engine {
pub buffer_watcher: Option<BufferWatcher>,
pub buffer_area: Rect,
pub force_redraw: bool,
pub scale: f32,
}

impl Engine {
Expand Down Expand Up @@ -273,6 +274,7 @@ impl Engine {
height: 10,
},
force_redraw: false,
scale: 1.0,
})
}

Expand Down Expand Up @@ -896,6 +898,15 @@ impl Engine {
.panes
.switch_pane_direction(direction, self.buffer_area);
}
Cmd::ZoomIn => {
self.scale += 0.1;
}
Cmd::ZoomOut => {
self.scale -= 0.1;
}
Cmd::ResetZoom => {
self.scale = 1.0;
}
input => {
if self.palette.has_focus() {
let _ = self.palette.handle_input(input);
Expand Down
10 changes: 10 additions & 0 deletions crates/ferrite-core/src/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,16 @@ pub fn get_default_mappings() -> Vec<(Key, Cmd, Exclusiveness)> {
Cmd::OpenFileExplorer(None),
Exclusiveness::Exclusive,
),
(
Key::new(KeyCode::Char('+'), KeyModifiers::CONTROL),
Cmd::ZoomIn,
Exclusiveness::Exclusive,
),
(
Key::new(KeyCode::Char('-'), KeyModifiers::CONTROL),
Cmd::ZoomOut,
Exclusiveness::Exclusive,
),
(
Key::new(KeyCode::Char('k'), KeyModifiers::CONTROL),
Cmd::InputMode {
Expand Down
1 change: 1 addition & 0 deletions crates/ferrite-core/src/palette/cmd_parser/cmds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub static COMMANDS: LazyLock<Vec<CommandTemplate>> = LazyLock::new(|| {
CmdBuilder::new("trash", None, true).build(|_| Cmd::Trash),
CmdBuilder::new("url-open", None, true).build(|_| Cmd::UrlOpen),
CmdBuilder::new("save-all", None, true).build(|_| Cmd::SaveAll),
CmdBuilder::new("zoom-reset", None, true).build(|_| Cmd::ResetZoom),
CmdBuilder::new("trim-trailing-whitespace", None, true).build(|_| Cmd::TrimTrailingWhitespace),
CmdBuilder::new("open-file-explorer", Some(("path", CmdTemplateArg::Path)), true).build(|args| Cmd::OpenFileExplorer(args[0].take().map(|arg| arg.unwrap_path()))),
CmdBuilder::new("number", Some(("start", CmdTemplateArg::Int)), true).build(|args| Cmd::Number(args[0].take().map(|arg| arg.unwrap_int()))),
Expand Down
58 changes: 41 additions & 17 deletions crates/ferrite-gui/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,29 @@ use crate::glue::convert_style;

mod quad_renderer;

fn calculate_cell_size(
font_system: &mut FontSystem,
metrics: Metrics,
font_weight: FontWeight,
) -> (f32, f32) {
let mut buffer = Buffer::new(font_system, metrics);
buffer.set_wrap(font_system, glyphon::Wrap::None);

// Use size of space to determine cell size
buffer.set_text(
font_system,
" ",
Attrs::new()
.weight(Weight(font_weight as u16))
.family(Family::Monospace),
Shaping::Basic,
);
let layout = buffer.line_layout(font_system, 0).unwrap();
let w = layout[0].w;
buffer.set_monospace_width(font_system, Some(w));
(w, metrics.line_height)
}

pub struct WgpuBackend {
font_system: FontSystem,
swash_cache: SwashCache,
Expand All @@ -37,6 +60,7 @@ pub struct WgpuBackend {
pub redraw: bool,
buffer: Buffer,
cells: Vec<Vec<Cell>>,
scale: f32,
// font config
font_family: String,
font_weight: FontWeight,
Expand Down Expand Up @@ -72,23 +96,7 @@ impl WgpuBackend {
let metrics = Metrics::relative(15.0, 1.20);
let mut buffer = Buffer::new(&mut font_system, metrics);
// borrowed from cosmic term
let (cell_width, cell_height) = {
buffer.set_wrap(&mut font_system, glyphon::Wrap::None);

// Use size of space to determine cell size
buffer.set_text(
&mut font_system,
" ",
Attrs::new()
.weight(Weight(font_weight as u16))
.family(Family::Monospace),
Shaping::Basic,
);
let layout = buffer.line_layout(&mut font_system, 0).unwrap();
let w = layout[0].w;
buffer.set_monospace_width(&mut font_system, Some(w));
(w, metrics.line_height)
};
let (cell_width, cell_height) = calculate_cell_size(&mut font_system, metrics, font_weight);
buffer.set_wrap(&mut font_system, glyphon::Wrap::None);

let columns = (width / cell_width) as u16;
Expand Down Expand Up @@ -123,6 +131,7 @@ impl WgpuBackend {
buffer,
cells,
redraw: true,
scale: 1.0,
font_family,
font_weight,
}
Expand Down Expand Up @@ -296,6 +305,21 @@ impl WgpuBackend {
pub fn set_font_weight(&mut self, weight: FontWeight) {
self.font_weight = weight;
}

pub fn set_scale(&mut self, scale: f32) {
self.scale = scale;
let metrics = Metrics::relative(15.0 * self.scale, 1.20);
self.buffer.set_metrics(&mut self.font_system, metrics);
let (cell_width, cell_height) =
calculate_cell_size(&mut self.font_system, metrics, self.font_weight);
self.cell_width = cell_width;
self.cell_height = cell_height;
self.resize(self.width, self.height);
}

pub fn scale(&self) -> f32 {
self.scale
}
}

impl Backend for WgpuBackend {
Expand Down
116 changes: 57 additions & 59 deletions crates/ferrite-gui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ impl GuiApp {
event => self.input(event_loop, event),
},
Event::AboutToWait => {
let backend = self.tui_app.terminal.backend_mut();
if backend.scale() != self.tui_app.engine.scale {
backend.set_scale(self.tui_app.engine.scale);
}
self.tui_app.engine.do_polling(&mut self.control_flow);
match self.control_flow {
EventLoopControlFlow::Poll => {
Expand Down Expand Up @@ -316,72 +320,66 @@ impl GuiApp {
return;
}

match event.logical_key {
Key::Named(key) => {
let modifier = match key {
winit::keyboard::NamedKey::Control => KeyModifiers::CONTROL,
winit::keyboard::NamedKey::Alt => KeyModifiers::ALT,
winit::keyboard::NamedKey::Shift => KeyModifiers::SHIFT,
winit::keyboard::NamedKey::Super => KeyModifiers::SUPER,
winit::keyboard::NamedKey::Hyper => KeyModifiers::HYPER,
winit::keyboard::NamedKey::Meta => KeyModifiers::META,
_ => KeyModifiers::NONE,
};
if !modifier.is_empty() {
self.modifiers |= modifier;
return;
}

if let Some(keycode) = convert_keycode(key) {
let cmd = keymap::get_command_from_input(
keycode,
self.modifiers,
self.tui_app.engine.get_current_keymappings(),
);
if let Some(cmd) = cmd {
self.tui_app
.engine
.handle_input_command(cmd, &mut control_flow);
if control_flow == EventLoopControlFlow::Exit {
event_loop.exit();
}
let cmd = 'block: {
match event.logical_key {
Key::Named(key) => {
let modifier = match key {
winit::keyboard::NamedKey::Control => KeyModifiers::CONTROL,
winit::keyboard::NamedKey::Alt => KeyModifiers::ALT,
winit::keyboard::NamedKey::Shift => KeyModifiers::SHIFT,
winit::keyboard::NamedKey::Super => KeyModifiers::SUPER,
winit::keyboard::NamedKey::Hyper => KeyModifiers::HYPER,
winit::keyboard::NamedKey::Meta => KeyModifiers::META,
_ => KeyModifiers::NONE,
};
if !modifier.is_empty() {
self.modifiers |= modifier;
return;
}
}
}
Key::Character(s) => {
if s.chars().count() == 1 {
let ch = s.chars().next().unwrap();
let cmd = if LineEnding::from_char(ch).is_some() {
Some(Cmd::Char('\n'))
} else {
keymap::get_command_from_input(
keymap::keycode::KeyCode::Char(s.chars().next().unwrap()),

if let Some(keycode) = convert_keycode(key) {
let cmd = keymap::get_command_from_input(
keycode,
self.modifiers,
self.tui_app.engine.get_current_keymappings(),
)
};
if let Some(cmd) = cmd {
self.tui_app
.engine
.handle_input_command(cmd, &mut control_flow);
if control_flow == EventLoopControlFlow::Exit {
event_loop.exit();
);
if cmd.is_some() {
break 'block cmd;
}
return;
}
} else {
self.tui_app.engine.handle_input_command(
Cmd::Insert(s.to_string()),
&mut control_flow,
);
if control_flow == EventLoopControlFlow::Exit {
event_loop.exit();
}
return;
};
}
Key::Character(s) => {
if s.chars().count() == 1 {
let ch = s.chars().next().unwrap();
let cmd = if LineEnding::from_char(ch).is_some() {
Some(Cmd::Char('\n'))
} else {
keymap::get_command_from_input(
keymap::keycode::KeyCode::Char(s.chars().next().unwrap()),
self.modifiers,
self.tui_app.engine.get_current_keymappings(),
)
};
if cmd.is_some() {
break 'block cmd;
}
} else {
break 'block Some(Cmd::Insert(s.to_string()));
};
}
_ => (),
}
_ => (),
None
};

if let Some(cmd) = cmd {
self.tui_app
.engine
.handle_input_command(cmd, &mut control_flow);
if control_flow == EventLoopControlFlow::Exit {
event_loop.exit();
}
return;
}

if let Some(text) = event.text {
Expand Down

0 comments on commit 059bc5d

Please sign in to comment.