1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
//! Convert a cursor into an iterator of rects.
use alacritty_terminal::vte::ansi::CursorShape;
use crate::display::color::Rgb;
use crate::display::content::RenderableCursor;
use crate::display::SizeInfo;
use crate::renderer::rects::RenderRect;
/// Trait for conversion into the iterator.
pub trait IntoRects {
/// Consume the cursor for an iterator of rects.
fn rects(self, size_info: &SizeInfo, thickness: f32) -> CursorRects;
}
impl IntoRects for RenderableCursor {
fn rects(self, size_info: &SizeInfo, thickness: f32) -> CursorRects {
let point = self.point();
let x = point.column.0 as f32 * size_info.cell_width() + size_info.padding_x();
let y = point.line as f32 * size_info.cell_height() + size_info.padding_y();
let mut width = size_info.cell_width();
let height = size_info.cell_height();
let thickness = (thickness * width).round().max(1.);
if self.is_wide() {
width *= 2.;
}
let crosshairs = crosshair(x, y, size_info);
let mut shape = match self.shape() {
CursorShape::Beam => beam(x, y, height, thickness, self.color()),
CursorShape::Underline => underline(x, y, width, height, thickness, self.color()),
CursorShape::HollowBlock => hollow(x, y, width, height, thickness, self.color()),
_ => CursorRects::default(),
};
// shape.append(&mut crosshairs);
let mut i = 0;
let mut ci = 0;
while i < shape.rects.len() && ci < crosshairs.len() {
match shape.rects[i] {
None => {
shape.rects[i] = Some(crosshairs[ci]);
ci = ci + 1;
}
Some(_) => {}
}
i = i + 1;
}
shape
}
}
fn abs_rect(x0: f32, y0: f32, x1: f32, y1: f32, color: Rgb, opacity: f32) -> Option<RenderRect> {
if x0 >= x1 || y0 >= y1 {
None
} else {
Some(RenderRect::new(x0, y0, x1 - x0, y1 - y0, color, opacity))
}
}
fn crosshair(x: f32, y: f32, size_info: &SizeInfo) -> Vec<RenderRect> {
let width = size_info.cell_width();
let height = size_info.cell_height();
let y_inner_padding = 2.; // config.y_inner_padding as f32;
let x_inner_padding = 4.; // config.x_inner_padding as f32;
let opacity = 0.05; // config.opacity.as_f32();
let color = Rgb::new(255,255,255); // config.color;
let crosshair_max_height =
20. * size_info.cell_height();
let crosshair_max_width =
40. * size_info.cell_width();
(vec![
abs_rect(
x,
(y - crosshair_max_height).max(0.),
x + width,
y - (size_info.cell_height() * y_inner_padding),
color,
opacity,
),
abs_rect(
x,
y + size_info.cell_height() * (y_inner_padding + 1.),
x + width,
size_info.height().min(y + crosshair_max_height + height),
color,
opacity,
),
abs_rect(
(x - crosshair_max_width).max(0.),
y,
x - (size_info.cell_width() * x_inner_padding),
y + height,
color,
opacity,
),
abs_rect(
x + size_info.cell_width()
* (x_inner_padding + if x_inner_padding > 0. { 1. } else { 0. }),
y,
size_info.width().min(x + crosshair_max_width + width),
y + height,
color,
opacity,
),
])
.into_iter()
.flatten()
.collect::<Vec<RenderRect>>()
}
/// Cursor rect iterator.
#[derive(Default)]
pub struct CursorRects {
rects: [Option<RenderRect>; 8],
index: usize,
}
impl From<RenderRect> for CursorRects {
fn from(rect: RenderRect) -> Self {
Self { rects: [Some(rect), None, None, None, None, None, None, None], index: 0 }
}
}
impl Iterator for CursorRects {
type Item = RenderRect;
fn next(&mut self) -> Option<Self::Item> {
let rect = self.rects.get_mut(self.index)?;
self.index += 1;
rect.take()
}
}
/// Create an iterator yielding a single beam rect.
fn beam(x: f32, y: f32, height: f32, thickness: f32, color: Rgb) -> CursorRects {
RenderRect::new(x, y, thickness, height, color, 1.).into()
}
/// Create an iterator yielding a single underline rect.
fn underline(x: f32, y: f32, width: f32, height: f32, thickness: f32, color: Rgb) -> CursorRects {
let y = y + height - thickness;
RenderRect::new(x, y, width, thickness, color, 1.).into()
}
/// Create an iterator yielding a rect for each side of the hollow block cursor.
fn hollow(x: f32, y: f32, width: f32, height: f32, thickness: f32, color: Rgb) -> CursorRects {
let top_line = RenderRect::new(x, y, width, thickness, color, 1.);
let vertical_y = y + thickness;
let vertical_height = height - 2. * thickness;
let left_line = RenderRect::new(x, vertical_y, thickness, vertical_height, color, 1.);
let bottom_y = y + height - thickness;
let bottom_line = RenderRect::new(x, bottom_y, width, thickness, color, 1.);
let right_x = x + width - thickness;
let right_line = RenderRect::new(right_x, vertical_y, thickness, vertical_height, color, 1.);
CursorRects {
rects: [Some(top_line), Some(bottom_line), Some(left_line), Some(right_line), None, None, None, None],
index: 0,
}
}
|