Skip to content

Commit

Permalink
ctrl|cmd +/- to manipulate font size with glutin backend
Browse files Browse the repository at this point in the history
Simliar code needs to be added to the x11 backend in a follow on
commit.

Refs: #2
  • Loading branch information
wez committed Feb 22, 2019
1 parent 89da009 commit c818225
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/gliumwindows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,24 @@ impl term::TerminalHost for Host {
.request_spawn_tab(self.display.gl_window().id())
.ok();
}

fn increase_font_size(&mut self) {
self.event_loop
.request_increase_font_size(self.display.gl_window().id())
.ok();
}

fn decrease_font_size(&mut self) {
self.event_loop
.request_decrease_font_size(self.display.gl_window().id())
.ok();
}

fn reset_font_size(&mut self) {
self.event_loop
.request_reset_font_size(self.display.gl_window().id())
.ok();
}
}

pub struct TerminalWindow {
Expand Down Expand Up @@ -620,7 +638,7 @@ impl TerminalWindow {

/// Called in response to either the screen dpi changing,
/// or the font scaling factor changing
fn scaling_changed(&mut self, font_scale: Option<f64>) -> Result<(), Error> {
pub fn scaling_changed(&mut self, font_scale: Option<f64>) -> Result<(), Error> {
let dpi_scale = self.host.display.gl_window().get_hidpi_factor();
let font_scale = font_scale.unwrap_or_else(|| self.fonts.get_font_scale());
eprintln!(
Expand Down
48 changes: 48 additions & 0 deletions src/guiloop/glutinloop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ struct Windows {
enum SpawnRequest {
Window,
Tab(WindowId),
FontLarger(WindowId),
FontSmaller(WindowId),
FontReset(WindowId),
}

/// The `GuiEventLoop` represents the combined gui event processor,
Expand Down Expand Up @@ -116,6 +119,41 @@ impl GlutinGuiSystem {
Ok(SpawnRequest::Tab(_window_id)) => {
eprintln!("Spawning tabs is not yet implemented for glutin");
}
Ok(SpawnRequest::FontLarger(window_id)) => {
let scale = fonts.get_font_scale() * 1.1;
if let Some(window) = self
.event_loop
.windows
.borrow_mut()
.by_id
.get_mut(&window_id)
{
window.scaling_changed(Some(scale)).ok();
}
}
Ok(SpawnRequest::FontSmaller(window_id)) => {
let scale = fonts.get_font_scale() * 0.9;
if let Some(window) = self
.event_loop
.windows
.borrow_mut()
.by_id
.get_mut(&window_id)
{
window.scaling_changed(Some(scale)).ok();
}
}
Ok(SpawnRequest::FontReset(window_id)) => {
if let Some(window) = self
.event_loop
.windows
.borrow_mut()
.by_id
.get_mut(&window_id)
{
window.scaling_changed(Some(1.0)).ok();
}
}
Err(TryRecvError::Empty) => return Ok(()),
Err(err) => bail!("spawn_rx disconnected {:?}", err),
}
Expand Down Expand Up @@ -206,6 +244,16 @@ impl GuiEventLoop {
})
}

pub fn request_increase_font_size(&self, window_id: WindowId) -> Result<(), Error> {
self.spawn_tx.send(SpawnRequest::FontLarger(window_id))
}
pub fn request_decrease_font_size(&self, window_id: WindowId) -> Result<(), Error> {
self.spawn_tx.send(SpawnRequest::FontSmaller(window_id))
}
pub fn request_reset_font_size(&self, window_id: WindowId) -> Result<(), Error> {
self.spawn_tx.send(SpawnRequest::FontReset(window_id))
}

pub fn request_spawn_window(&self) -> Result<(), Error> {
self.spawn_tx.send(SpawnRequest::Window)
}
Expand Down
9 changes: 9 additions & 0 deletions term/src/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ pub trait TerminalHost {

/// Toggle full-screen mode
fn toggle_full_screen(&mut self) {}

/// Increase font size by one step
fn increase_font_size(&mut self) {}

/// Decrease font size by one step
fn decrease_font_size(&mut self) {}

/// Reset font size
fn reset_font_size(&mut self) {}
}

pub struct Terminal {
Expand Down
16 changes: 16 additions & 0 deletions term/src/terminalstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,22 @@ impl TerminalState {
return Ok(());
}

if (mods == KeyModifiers::SUPER || mods == KeyModifiers::CTRL) && key == KeyCode::Char('-')
{
host.decrease_font_size();
return Ok(());
}
if (mods == KeyModifiers::SUPER || mods == KeyModifiers::CTRL) && key == KeyCode::Char('=')
{
host.increase_font_size();
return Ok(());
}
if (mods == KeyModifiers::SUPER || mods == KeyModifiers::CTRL) && key == KeyCode::Char('0')
{
host.reset_font_size();
return Ok(());
}

if mods == KeyModifiers::SUPER {
if let Char(c) = key {
if c >= '0' && c <= '9' {
Expand Down

0 comments on commit c818225

Please sign in to comment.