fix formatting issues

macos-click-through
Keith Simmons 3 years ago
parent 551236a290
commit b7b06c59a0

@ -1,230 +1,228 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::Arc; use std::sync::Arc;
use skia_safe::Color4f; use skia_safe::Color4f;
use super::style::{Colors, Style}; use super::style::{Colors, Style};
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum CursorShape { pub enum CursorShape {
Block, Block,
Horizontal, Horizontal,
Vertical, Vertical,
} }
impl CursorShape { impl CursorShape {
pub fn from_type_name(name: &str) -> Option<CursorShape> { pub fn from_type_name(name: &str) -> Option<CursorShape> {
match name { match name {
"block" => Some(CursorShape::Block), "block" => Some(CursorShape::Block),
"horizontal" => Some(CursorShape::Horizontal), "horizontal" => Some(CursorShape::Horizontal),
"vertical" => Some(CursorShape::Vertical), "vertical" => Some(CursorShape::Vertical),
_ => None, _ => None,
} }
} }
} }
#[derive(Default, Debug, Clone, PartialEq)] #[derive(Default, Debug, Clone, PartialEq)]
pub struct CursorMode { pub struct CursorMode {
pub shape: Option<CursorShape>, pub shape: Option<CursorShape>,
pub style_id: Option<u64>, pub style_id: Option<u64>,
pub cell_percentage: Option<f32>, pub cell_percentage: Option<f32>,
pub blinkwait: Option<u64>, pub blinkwait: Option<u64>,
pub blinkon: Option<u64>, pub blinkon: Option<u64>,
pub blinkoff: Option<u64>, pub blinkoff: Option<u64>,
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub struct Cursor { pub struct Cursor {
pub grid_position: (u64, u64), pub grid_position: (u64, u64),
pub parent_window_id: u64, pub parent_window_id: u64,
pub shape: CursorShape, pub shape: CursorShape,
pub cell_percentage: Option<f32>, pub cell_percentage: Option<f32>,
pub blinkwait: Option<u64>, pub blinkwait: Option<u64>,
pub blinkon: Option<u64>, pub blinkon: Option<u64>,
pub blinkoff: Option<u64>, pub blinkoff: Option<u64>,
pub style: Option<Arc<Style>>, pub style: Option<Arc<Style>>,
pub enabled: bool, pub enabled: bool,
pub double_width: bool, pub double_width: bool,
pub character: String, pub character: String,
} }
impl Cursor { impl Cursor {
pub fn new() -> Cursor { pub fn new() -> Cursor {
Cursor { Cursor {
grid_position: (0, 0), grid_position: (0, 0),
parent_window_id: 0, parent_window_id: 0,
shape: CursorShape::Block, shape: CursorShape::Block,
style: None, style: None,
cell_percentage: None, cell_percentage: None,
blinkwait: None, blinkwait: None,
blinkon: None, blinkon: None,
blinkoff: None, blinkoff: None,
enabled: true, enabled: true,
double_width: false, double_width: false,
character: " ".to_string(), character: " ".to_string(),
} }
} }
pub fn foreground(&self, default_colors: &Colors) -> Color4f { pub fn foreground(&self, default_colors: &Colors) -> Color4f {
self self.style
.style .as_ref()
.as_ref() .and_then(|s| s.colors.foreground)
.and_then(|s| s.colors.foreground) .unwrap_or(default_colors.background.unwrap())
.unwrap_or(default_colors.background.unwrap()) }
}
pub fn background(&self, default_colors: &Colors) -> Color4f {
pub fn background(&self, default_colors: &Colors) -> Color4f { self.style
self .as_ref()
.style .and_then(|s| s.colors.background)
.as_ref() .unwrap_or(default_colors.foreground.unwrap())
.and_then(|s| s.colors.background) }
.unwrap_or(default_colors.foreground.unwrap())
} pub fn change_mode(&mut self, cursor_mode: &CursorMode, styles: &HashMap<u64, Arc<Style>>) {
let CursorMode {
pub fn change_mode(&mut self, cursor_mode: &CursorMode, styles: &HashMap<u64, Arc<Style>>) { shape,
let CursorMode { style_id,
shape, cell_percentage,
style_id, blinkwait,
cell_percentage, blinkon,
blinkwait, blinkoff,
blinkon, } = cursor_mode;
blinkoff,
} = cursor_mode; if let Some(shape) = shape {
self.shape = shape.clone();
if let Some(shape) = shape { }
self.shape = shape.clone();
} if let Some(style_id) = style_id {
self.style = styles.get(style_id).cloned();
if let Some(style_id) = style_id { }
self.style = styles.get(style_id).cloned();
} self.cell_percentage = *cell_percentage;
self.blinkwait = *blinkwait;
self.cell_percentage = *cell_percentage; self.blinkon = *blinkon;
self.blinkwait = *blinkwait; self.blinkoff = *blinkoff;
self.blinkon = *blinkon; }
self.blinkoff = *blinkoff; }
}
} #[cfg(test)]
mod tests {
#[cfg(test)] use super::*;
mod tests {
use super::*; const COLORS: Colors = Colors {
foreground: Some(Color4f::new(0.1, 0.1, 0.1, 0.1)),
const COLORS: Colors = Colors { background: Some(Color4f::new(0.2, 0.1, 0.1, 0.1)),
foreground: Some(Color4f::new(0.1, 0.1, 0.1, 0.1)), special: Some(Color4f::new(0.3, 0.1, 0.1, 0.1)),
background: Some(Color4f::new(0.2, 0.1, 0.1, 0.1)), };
special: Some(Color4f::new(0.3, 0.1, 0.1, 0.1)),
}; const DEFAULT_COLORS: Colors = Colors {
foreground: Some(Color4f::new(0.1, 0.2, 0.1, 0.1)),
const DEFAULT_COLORS: Colors = Colors { background: Some(Color4f::new(0.2, 0.2, 0.1, 0.1)),
foreground: Some(Color4f::new(0.1, 0.2, 0.1, 0.1)), special: Some(Color4f::new(0.3, 0.2, 0.1, 0.1)),
background: Some(Color4f::new(0.2, 0.2, 0.1, 0.1)), };
special: Some(Color4f::new(0.3, 0.2, 0.1, 0.1)),
}; const NONE_COLORS: Colors = Colors {
foreground: None,
const NONE_COLORS: Colors = Colors { background: None,
foreground: None, special: None,
background: None, };
special: None,
}; #[test]
fn test_from_type_name() {
#[test] assert_eq!(
fn test_from_type_name() { CursorShape::from_type_name("block"),
assert_eq!( Some(CursorShape::Block)
CursorShape::from_type_name("block"), );
Some(CursorShape::Block) assert_eq!(
); CursorShape::from_type_name("horizontal"),
assert_eq!( Some(CursorShape::Horizontal)
CursorShape::from_type_name("horizontal"), );
Some(CursorShape::Horizontal) assert_eq!(
); CursorShape::from_type_name("vertical"),
assert_eq!( Some(CursorShape::Vertical)
CursorShape::from_type_name("vertical"), );
Some(CursorShape::Vertical) }
);
} #[test]
fn test_foreground() {
#[test] let mut cursor = Cursor::new();
fn test_foreground() { let style = Some(Arc::new(Style::new(COLORS)));
let mut cursor = Cursor::new();
let style = Some(Arc::new(Style::new(COLORS))); assert_eq!(
cursor.foreground(&DEFAULT_COLORS),
assert_eq!( DEFAULT_COLORS.background.unwrap()
cursor.foreground(&DEFAULT_COLORS), );
DEFAULT_COLORS.background.unwrap() cursor.style = style.clone();
); assert_eq!(
cursor.style = style.clone(); cursor.foreground(&DEFAULT_COLORS),
assert_eq!( COLORS.foreground.unwrap()
cursor.foreground(&DEFAULT_COLORS), );
COLORS.foreground.unwrap()
); cursor.style = Some(Arc::new(Style::new(NONE_COLORS)));
assert_eq!(
cursor.style = Some(Arc::new(Style::new(NONE_COLORS))); cursor.foreground(&DEFAULT_COLORS),
assert_eq!( DEFAULT_COLORS.background.unwrap()
cursor.foreground(&DEFAULT_COLORS), );
DEFAULT_COLORS.background.unwrap() }
);
} #[test]
fn test_background() {
#[test] let mut cursor = Cursor::new();
fn test_background() { let style = Some(Arc::new(Style::new(COLORS)));
let mut cursor = Cursor::new();
let style = Some(Arc::new(Style::new(COLORS))); assert_eq!(
cursor.background(&DEFAULT_COLORS),
assert_eq!( DEFAULT_COLORS.foreground.unwrap()
cursor.background(&DEFAULT_COLORS), );
DEFAULT_COLORS.foreground.unwrap() cursor.style = style.clone();
); assert_eq!(
cursor.style = style.clone(); cursor.background(&DEFAULT_COLORS),
assert_eq!( COLORS.background.unwrap()
cursor.background(&DEFAULT_COLORS), );
COLORS.background.unwrap()
); cursor.style = Some(Arc::new(Style::new(NONE_COLORS)));
assert_eq!(
cursor.style = Some(Arc::new(Style::new(NONE_COLORS))); cursor.background(&DEFAULT_COLORS),
assert_eq!( DEFAULT_COLORS.foreground.unwrap()
cursor.background(&DEFAULT_COLORS), );
DEFAULT_COLORS.foreground.unwrap() }
);
} #[test]
fn test_change_mode() {
#[test] let cursor_mode = CursorMode {
fn test_change_mode() { shape: Some(CursorShape::Horizontal),
let cursor_mode = CursorMode { style_id: Some(1),
shape: Some(CursorShape::Horizontal), cell_percentage: Some(100.0),
style_id: Some(1), blinkwait: Some(1),
cell_percentage: Some(100.0), blinkon: Some(1),
blinkwait: Some(1), blinkoff: Some(1),
blinkon: Some(1), };
blinkoff: Some(1), let mut styles = HashMap::new();
}; styles.insert(1, Arc::new(Style::new(COLORS)));
let mut styles = HashMap::new();
styles.insert(1, Arc::new(Style::new(COLORS))); let mut cursor = Cursor::new();
let mut cursor = Cursor::new(); cursor.change_mode(&cursor_mode, &styles);
assert_eq!(cursor.shape, CursorShape::Horizontal);
cursor.change_mode(&cursor_mode, &styles); assert_eq!(cursor.style, styles.get(&1).cloned());
assert_eq!(cursor.shape, CursorShape::Horizontal); assert_eq!(cursor.cell_percentage, Some(100.0));
assert_eq!(cursor.style, styles.get(&1).cloned()); assert_eq!(cursor.blinkwait, Some(1));
assert_eq!(cursor.cell_percentage, Some(100.0)); assert_eq!(cursor.blinkon, Some(1));
assert_eq!(cursor.blinkwait, Some(1)); assert_eq!(cursor.blinkoff, Some(1));
assert_eq!(cursor.blinkon, Some(1));
assert_eq!(cursor.blinkoff, Some(1)); let cursor_mode_with_none = CursorMode {
shape: None,
let cursor_mode_with_none = CursorMode { style_id: None,
shape: None, cell_percentage: None,
style_id: None, blinkwait: None,
cell_percentage: None, blinkon: None,
blinkwait: None, blinkoff: None,
blinkon: None, };
blinkoff: None, cursor.change_mode(&cursor_mode_with_none, &styles);
}; assert_eq!(cursor.shape, CursorShape::Horizontal);
cursor.change_mode(&cursor_mode_with_none, &styles); assert_eq!(cursor.style, styles.get(&1).cloned());
assert_eq!(cursor.shape, CursorShape::Horizontal); assert_eq!(cursor.cell_percentage, None);
assert_eq!(cursor.style, styles.get(&1).cloned()); assert_eq!(cursor.blinkwait, None);
assert_eq!(cursor.cell_percentage, None); assert_eq!(cursor.blinkon, None);
assert_eq!(cursor.blinkwait, None); assert_eq!(cursor.blinkoff, None);
assert_eq!(cursor.blinkon, None); }
assert_eq!(cursor.blinkoff, None); }
}
}

@ -164,8 +164,9 @@ impl GlutinWindowWrapper {
self.handle_focus_lost(); self.handle_focus_lost();
} }
} }
Event::RedrawRequested(..) | Event::RedrawRequested(..) | Event::WindowEvent { .. } => {
Event::WindowEvent { .. } => REDRAW_SCHEDULER.queue_next_frame(), REDRAW_SCHEDULER.queue_next_frame()
}
_ => {} _ => {}
} }
} }

Loading…
Cancel
Save