From edc2c7f5b9784310f150724a786ae3bc3be74490 Mon Sep 17 00:00:00 2001 From: Anders Rasmussen Date: Sun, 5 Feb 2017 21:01:26 +1100 Subject: Configurable window dimensions Adds a configuration option `dimensions` which will set initial window size by columns and lines. Changes to the config file will require restart. resolves #370 --- src/cli.rs | 23 ++++++++++------------- src/config.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/display.rs | 6 ++++-- 3 files changed, 65 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/cli.rs b/src/cli.rs index 1d19b353..51cdd4f9 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -14,7 +14,7 @@ extern crate log; use clap::{Arg, App}; use index::{Line, Column}; -use config::Shell; +use config::{Dimensions, Shell}; const DEFAULT_TITLE: &'static str = "Alacritty"; @@ -22,8 +22,7 @@ const DEFAULT_TITLE: &'static str = "Alacritty"; pub struct Options { pub print_events: bool, pub ref_test: bool, - pub columns: Column, - pub lines: Line, + pub dimensions: Option, pub title: String, pub log_level: log::LogLevelFilter, pub shell: Option>, @@ -34,8 +33,7 @@ impl Default for Options { Options { print_events: false, ref_test: false, - columns: Column(80), - lines: Line(24), + dimensions: None, title: DEFAULT_TITLE.to_owned(), log_level: log::LogLevelFilter::Warn, shell: None, @@ -95,8 +93,11 @@ impl Options { } if let Some(mut dimensions) = matches.values_of("dimensions") { - dimensions.next().map(|w| w.parse().map(|w| options.columns = Column(w))); - dimensions.next().map(|h| h.parse().map(|h| options.lines = Line(h))); + let width = dimensions.next().map(|w| w.parse().map(|w| Column(w))); + let height = dimensions.next().map(|h| h.parse().map(|h| Line(h))); + if let (Some(Ok(width)), Some(Ok(height))) = (width, height) { + options.dimensions = Some(Dimensions::new(width, height)); + } } if let Some(title) = matches.value_of("title") { @@ -128,12 +129,8 @@ impl Options { options } - pub fn lines_u32(&self) -> u32 { - self.lines.0 as u32 - } - - pub fn columns_u32(&self) -> u32 { - self.columns.0 as u32 + pub fn dimensions(&self) -> Option { + self.dimensions } pub fn shell(&self) -> Option<&Shell> { diff --git a/src/config.rs b/src/config.rs index d08b801b..362fe645 100644 --- a/src/config.rs +++ b/src/config.rs @@ -23,6 +23,7 @@ use serde::de::{Visitor, MapVisitor, Unexpected}; use notify::{Watcher as WatcherApi, RecommendedWatcher as FileWatcher, op}; use input::{Action, Binding, MouseBinding, KeyBinding}; +use index::{Line, Column}; use ansi; @@ -212,6 +213,10 @@ impl<'a> Shell<'a> { /// Top-level config type #[derive(Debug, Deserialize)] pub struct Config { + /// Initial dimensions + #[serde(default)] + dimensions: Dimensions, + /// Pixels per inch #[serde(default)] dpi: Dpi, @@ -273,6 +278,7 @@ impl Default for Config { fn default() -> Config { Config { draw_bold_text_with_bright_colors: true, + dimensions: Default::default(), dpi: Default::default(), font: Default::default(), render_timer: Default::default(), @@ -969,6 +975,12 @@ impl Config { &self.font } + /// Get window dimensions + #[inline] + pub fn dimensions(&self) -> Dimensions { + self.dimensions + } + /// Get dpi config #[inline] pub fn dpi(&self) -> &Dpi { @@ -1020,6 +1032,45 @@ impl Config { } } +/// Window Dimensions +/// +/// Newtype to avoid passing values incorrectly +#[derive(Debug, Copy, Clone, Deserialize)] +pub struct Dimensions { + /// Window width in character columns + columns: Column, + + /// Window Height in character lines + lines: Line, +} + +impl Default for Dimensions { + fn default() -> Dimensions { + Dimensions::new(Column(80), Line(24)) + } +} + +impl Dimensions { + pub fn new(columns: Column, lines: Line) -> Self { + Dimensions { + columns: columns, + lines: lines + } + } + + /// Get lines + #[inline] + pub fn lines_u32(&self) -> u32 { + self.lines.0 as u32 + } + + /// Get columns + #[inline] + pub fn columns_u32(&self) -> u32 { + self.columns.0 as u32 + } +} + /// Pixels per inch /// /// This is only used on `FreeType` systems diff --git a/src/display.rs b/src/display.rs index b1ab3ed9..0b242b33 100644 --- a/src/display.rs +++ b/src/display.rs @@ -177,8 +177,10 @@ impl Display { let cell_height = (metrics.line_height + font.offset().y() as f64) as u32; // Resize window to specified dimensions - let width = cell_width * options.columns_u32() + 4; - let height = cell_height * options.lines_u32() + 4; + let dimensions = options.dimensions() + .unwrap_or_else(|| config.dimensions()); + let width = cell_width * dimensions.columns_u32() + 4; + let height = cell_height * dimensions.lines_u32() + 4; let size = Size { width: Pixels(width), height: Pixels(height) }; info!("set_inner_size: {}", size); -- cgit