Use f32 instead of i32 for lengths

This allows users to use logical coordinates instead of physical ones.
This commit is contained in:
Héctor Ramón Jiménez 2023-02-04 11:13:53 +01:00
parent f08bea22ed
commit 4320ae6329
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
15 changed files with 203 additions and 155 deletions

View file

@ -424,14 +424,14 @@ impl<'a> Edit<'a> for Editor<'a> {
self.buffer.set_redraw(true);
}
Action::PageUp => {
self.action(Action::Vertical(-self.buffer.size().1));
self.action(Action::Vertical(-self.buffer.size().1 as i32));
}
Action::PageDown => {
self.action(Action::Vertical(self.buffer.size().1));
self.action(Action::Vertical(self.buffer.size().1 as i32));
}
Action::Vertical(px) => {
// TODO more efficient
let lines = px / self.buffer.metrics().line_height;
let lines = px / self.buffer.metrics().line_height as i32;
if lines < 0 {
for _ in 0..-lines {
self.action(Action::Up);
@ -541,7 +541,7 @@ impl<'a> Edit<'a> for Editor<'a> {
Action::Click { x, y } => {
self.select_opt = None;
if let Some(new_cursor) = self.buffer.hit(x, y) {
if let Some(new_cursor) = self.buffer.hit(x as f32, y as f32) {
if new_cursor != self.cursor {
self.cursor = new_cursor;
self.buffer.set_redraw(true);
@ -554,7 +554,7 @@ impl<'a> Edit<'a> for Editor<'a> {
self.buffer.set_redraw(true);
}
if let Some(new_cursor) = self.buffer.hit(x, y) {
if let Some(new_cursor) = self.buffer.hit(x as f32, y as f32) {
if new_cursor != self.cursor {
self.cursor = new_cursor;
self.buffer.set_redraw(true);
@ -753,7 +753,7 @@ impl<'a> Edit<'a> for Editor<'a> {
} else if let Some((min, max)) = range_opt.take() {
f(
min,
line_y - font_size,
(line_y - font_size) as i32,
cmp::max(0, max - min) as u32,
line_height as u32,
Color::rgba(color.r(), color.g(), color.b(), 0x33),
@ -765,7 +765,7 @@ impl<'a> Edit<'a> for Editor<'a> {
if run.glyphs.is_empty() && end.line > line_i {
// Highlight all of internal empty lines
range_opt = Some((0, self.buffer.size().0));
range_opt = Some((0, self.buffer.size().0 as i32));
}
if let Some((mut min, mut max)) = range_opt.take() {
@ -774,12 +774,12 @@ impl<'a> Edit<'a> for Editor<'a> {
if run.rtl {
min = 0;
} else {
max = self.buffer.size().0;
max = self.buffer.size().0 as i32;
}
}
f(
min,
line_y - font_size,
(line_y - font_size) as i32,
cmp::max(0, max - min) as u32,
line_height as u32,
Color::rgba(color.r(), color.g(), color.b(), 0x33),
@ -815,7 +815,7 @@ impl<'a> Edit<'a> for Editor<'a> {
},
};
f(x, line_y - font_size, 1, line_height as u32, color);
f(x, (line_y - font_size) as i32, 1, line_height as u32, color);
}
for glyph in run.glyphs.iter() {
@ -827,7 +827,7 @@ impl<'a> Edit<'a> for Editor<'a> {
};
cache.with_pixels(cache_key, glyph_color, |x, y, color| {
f(x_int + x, line_y + y_int + y, 1, 1, color);
f(x_int + x, line_y as i32 + y_int + y, 1, 1, color);
});
}
}

View file

@ -237,7 +237,7 @@ impl<'a> Edit<'a> for ViEditor<'a> {
let cursor_glyph_opt = |cursor: &Cursor| -> Option<(usize, f32, f32)> {
//TODO: better calculation of width
let default_width = (font_size as f32) / 2.0;
let default_width = font_size / 2.0;
if cursor.line == line_i {
for (glyph_i, glyph) in run.glyphs.iter().enumerate() {
if cursor.index >= glyph.start && cursor.index < glyph.end {
@ -312,7 +312,7 @@ impl<'a> Edit<'a> for ViEditor<'a> {
} else if let Some((min, max)) = range_opt.take() {
f(
min,
line_y - font_size,
(line_y - font_size) as i32,
cmp::max(0, max - min) as u32,
line_height as u32,
Color::rgba(color.r(), color.g(), color.b(), 0x33),
@ -324,7 +324,7 @@ impl<'a> Edit<'a> for ViEditor<'a> {
if run.glyphs.is_empty() && end.line > line_i {
// Highlight all of internal empty lines
range_opt = Some((0, self.buffer().size().0));
range_opt = Some((0, self.buffer().size().0 as i32));
}
if let Some((mut min, mut max)) = range_opt.take() {
@ -333,12 +333,12 @@ impl<'a> Edit<'a> for ViEditor<'a> {
if run.rtl {
min = 0;
} else {
max = self.buffer().size().0;
max = self.buffer().size().0 as i32;
}
}
f(
min,
line_y - font_size,
(line_y - font_size) as i32,
cmp::max(0, max - min) as u32,
line_height as u32,
Color::rgba(color.r(), color.g(), color.b(), 0x33),
@ -397,13 +397,19 @@ impl<'a> Edit<'a> for ViEditor<'a> {
let right_x = cmp::max(start_x, end_x);
f(
left_x,
line_y - font_size,
(line_y - font_size) as i32,
(right_x - left_x) as u32,
line_height as u32,
Color::rgba(color.r(), color.g(), color.b(), 0x33),
);
} else {
f(start_x, line_y - font_size, 1, line_height as u32, color);
f(
start_x,
(line_y - font_size) as i32,
1,
line_height as u32,
color,
);
}
}
@ -416,7 +422,7 @@ impl<'a> Edit<'a> for ViEditor<'a> {
};
cache.with_pixels(cache_key, glyph_color, |x, y, color| {
f(x_int + x, line_y + y_int + y, 1, 1, color)
f(x_int + x, line_y as i32 + y_int + y, 1, 1, color);
});
}
}