support cell percentage configuration

macos-click-through
keith 5 years ago
parent 1d620acc5a
commit 4ab94b3cae

@ -26,13 +26,16 @@ pub struct CursorMode {
#[new(default)]
pub shape: Option<CursorShape>,
#[new(default)]
pub style_id: Option<u64>
pub style_id: Option<u64>,
#[new(default)]
pub cell_percentage: Option<f32>
}
#[derive(Clone)]
pub struct Cursor {
pub position: (u64, u64),
pub shape: CursorShape,
pub cell_percentage: Option<f32>,
pub style: Option<Style>,
pub enabled: bool,
pub mode_list: Vec<CursorMode>
@ -44,6 +47,7 @@ impl Cursor {
position: (0, 0),
shape: CursorShape::Block,
style: None,
cell_percentage: None,
enabled: true,
mode_list: Vec::new()
}
@ -66,7 +70,7 @@ impl Cursor {
}
pub fn change_mode(&mut self, mode_index: u64, styles: &HashMap<u64, Style>) {
if let Some(CursorMode { shape, style_id }) = self.mode_list.get(mode_index as usize) {
if let Some(CursorMode { shape, style_id, cell_percentage }) = self.mode_list.get(mode_index as usize) {
if let Some(shape) = shape {
self.shape = shape.clone();
}
@ -76,6 +80,8 @@ impl Cursor {
.get(style_id)
.map(|style_reference| style_reference.clone());
}
self.cell_percentage = cell_percentage.clone();
}
}
}

@ -13,6 +13,8 @@ pub enum EventParseError {
InvalidString(Value),
InvalidU64(Value),
InvalidI64(Value),
InvalidF32(Value),
InvalidF64(Value),
InvalidBool(Value),
InvalidEventFormat
}
@ -26,6 +28,8 @@ impl fmt::Display for EventParseError {
EventParseError::InvalidString(value) => write!(f, "invalid string format {}", value),
EventParseError::InvalidU64(value) => write!(f, "invalid u64 format {}", value),
EventParseError::InvalidI64(value) => write!(f, "invalid i64 format {}", value),
EventParseError::InvalidF32(value) => write!(f, "invalid f32 format {}", value),
EventParseError::InvalidF64(value) => write!(f, "invalid f64 format {}", value),
EventParseError::InvalidBool(value) => write!(f, "invalid bool format {}", value),
EventParseError::InvalidEventFormat => write!(f, "invalid event format")
}
@ -167,6 +171,22 @@ fn parse_i64(i64_value: &Value) -> Result<i64> {
}
}
fn parse_f32(f32_value: &Value) -> Result<f32> {
if let Value::F32(content) = f32_value.clone() {
Ok(content)
} else {
Err(EventParseError::InvalidF32(f32_value.clone()))
}
}
fn parse_f64(f64_value: &Value) -> Result<f64> {
if let Value::F64(content) = f64_value.clone() {
Ok(content)
} else {
Err(EventParseError::InvalidF64(f64_value.clone()))
}
}
fn parse_bool(bool_value: &Value) -> Result<bool> {
if let Value::Boolean(content) = bool_value.clone() {
Ok(content)
@ -198,6 +218,9 @@ fn parse_mode_info_set(mode_info_set_arguments: Vec<Value>) -> Result<RedrawEven
"cursor_shape" => {
mode_info.shape = CursorShape::from_type_name(&parse_string(&value)?);
},
"cell_percentage" => {
mode_info.cell_percentage = Some(parse_u64(&value)? as f32 / 100.0);
},
"attr_id" => {
mode_info.style_id = Some(parse_u64(&value)?);
},

@ -8,7 +8,7 @@ use crate::editor::{Colors, Cursor, CursorShape, Editor};
const AVERAGE_MOTION_PERCENTAGE: f32 = 0.6;
const MOTION_PERCENTAGE_SPREAD: f32 = 0.5;
const BAR_WIDTH: f32 = 1.0 / 8.0;
const DEFAULT_CELL_PERCENTAGE: f32 = 1.0 / 8.0;
const STANDARD_CORNERS: &[(f32, f32); 4] = &[(-0.5, -0.5), (0.5, -0.5), (0.5, 0.5), (-0.5, 0.5)];
@ -67,11 +67,11 @@ impl CursorRenderer {
let mut renderer = CursorRenderer {
corners: vec![Corner::new((0.0, 0.0).into()); 4]
};
renderer.set_cursor_shape(&CursorShape::Block);
renderer.set_cursor_shape(&CursorShape::Block, DEFAULT_CELL_PERCENTAGE);
renderer
}
fn set_cursor_shape(&mut self, cursor_shape: &CursorShape) {
fn set_cursor_shape(&mut self, cursor_shape: &CursorShape, cell_percentage: f32) {
self.corners = self.corners
.clone()
.into_iter().enumerate()
@ -80,8 +80,13 @@ impl CursorRenderer {
Corner {
relative_position: match cursor_shape {
CursorShape::Block => (x, y).into(),
CursorShape::Vertical => ((x + 0.5) * BAR_WIDTH - 0.5, y).into(),
CursorShape::Horizontal => (x, (y + 0.5) * BAR_WIDTH - 0.5).into()
// Transform the x position so that the right side is translated over to
// the BAR_WIDTH position
CursorShape::Vertical => ((x + 0.5) * cell_percentage - 0.5, y).into(),
// Do the same as above, but flip the y coordinate and then flip the result
// so that the horizontal bar is at the bottom of the character space
// instead of the top.
CursorShape::Horizontal => (x, -((-y + 0.5) * cell_percentage - 0.5)).into()
},
.. corner
}
@ -102,7 +107,7 @@ impl CursorRenderer {
grid_y as f32 * font_height + font_height / 2.0
).into();
self.set_cursor_shape(&cursor.shape);
self.set_cursor_shape(&cursor.shape, cursor.cell_percentage.unwrap_or(DEFAULT_CELL_PERCENTAGE));
let mut animating = false;
if !center_destination.is_zero() {

Loading…
Cancel
Save