Skip to content

Commit 571c99d

Browse files
committed
Fixed several found bugs
1 parent 06d3958 commit 571c99d

File tree

8 files changed

+81
-81
lines changed

8 files changed

+81
-81
lines changed

.vimspector.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
"configuration": {
66
"type": "lldb",
77
"request": "launch",
8-
"sourceLanguages": ["rust"],
9-
"program": "${workspaceRoot}/target/debug/examples/windows",
8+
"sourceLanguages": [
9+
"rust"
10+
],
11+
"program": "${workspaceRoot}/target/debug/examples/gui",
1012
"MIMode": "gdb",
1113
"setupCommands": [
1214
{

examples/gui.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ struct Gui {
1919
advanced_slider: f32,
2020
select_box: usize,
2121
select_list: usize,
22+
collapsing_header_list: usize,
2223
font_size: u32,
2324
font_family: usize,
2425
theme: usize,
@@ -46,6 +47,7 @@ impl Gui {
4647
advanced_slider: 0.5,
4748
select_box: 0,
4849
select_list: 0,
50+
collapsing_header_list: 0,
4951
font_size: 12,
5052
font_family: 0,
5153
theme: 0,
@@ -287,7 +289,7 @@ impl Gui {
287289
s.collapsing_header("Collapsing Header", |s: &mut PixState| {
288290
s.next_width(300);
289291
let items = ["Item 1", "Item 2", "Item 3", "Item 4"];
290-
s.select_list("Some list", &mut self.select_list, &items, 3)?;
292+
s.select_list("Some list", &mut self.collapsing_header_list, &items, 3)?;
291293
Ok(())
292294
})?;
293295

src/gui/layout.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ impl PixState {
180180

181181
// Calculate tab size
182182
let (width, height) = s.text_size(tab_label)?;
183-
let tab = rect![pos + fpad.x(), width, height].offset_size(2 * ipad);
183+
let tab = rect![pos + fpad, width, height].offset_size(2 * ipad);
184184

185185
// Check hover/active/keyboard focus
186186
let hovered = s.ui.try_hover(id, &tab);
@@ -247,8 +247,8 @@ impl PixState {
247247
let fpad = s.theme.spacing.frame_pad;
248248
s.push();
249249
s.stroke(colors.disabled());
250-
let y = pos.y() + 1;
251-
let line_width = s.ui_width()? - fpad.x();
250+
let y = pos.y() + fpad.y() + 1;
251+
let line_width = s.ui_width()?;
252252
s.line(line_![fpad.x(), y, line_width, y])?;
253253
s.pop();
254254
s.advance_cursor([line_width, fpad.y()]);

src/gui/state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ impl PixState {
792792
self.ui.cursor = point![padx + offset_x, pos.y() + line_height + pady];
793793
self.ui.pline_height = line_height;
794794
self.ui.line_height = 0;
795-
self.ui.last_size = Some(rect![self.ui.cursor, size.x(), size.y()]);
795+
self.ui.last_size = Some(rect![pos, size.x(), size.y()]);
796796
}
797797

798798
/// Get or create a UI texture to render to

src/gui/widgets.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ impl PixState {
367367
}
368368

369369
/// Render an arrow aligned with the current font size.
370+
///
370371
/// # Errors
371372
///
372373
/// If the renderer fails to draw to the current render target, then an error is returned.

src/gui/widgets/select.rs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use crate::{
2626
ops::clamp_size,
2727
prelude::*,
2828
};
29-
use anyhow::bail;
3029
use std::cmp;
3130

3231
/// The maximum number of select elements that can be displayed at once.
@@ -63,7 +62,7 @@ impl PixState {
6362
label: S,
6463
selected: &mut usize,
6564
items: &[I],
66-
displayed_count: usize,
65+
mut displayed_count: usize,
6766
) -> PixResult<bool>
6867
where
6968
S: AsRef<str>,
@@ -72,13 +71,10 @@ impl PixState {
7271
let label = label.as_ref();
7372

7473
if displayed_count > MAX_DISPLAYED {
75-
bail!("displayed_count exceeds maximum of: {}", MAX_DISPLAYED);
76-
} else if *selected > items.len() {
77-
bail!(
78-
"selected out of bounds: the len is {} but the value is {}",
79-
items.len(),
80-
*selected
81-
);
74+
displayed_count = MAX_DISPLAYED;
75+
}
76+
if *selected >= items.len() {
77+
*selected = items.len() - 1;
8278
}
8379

8480
let s = self;
@@ -140,10 +136,14 @@ impl PixState {
140136
if arrow_x + arrow_width - fpad.x() <= select_box.right() {
141137
s.no_stroke();
142138
s.fill(fg);
139+
s.clip(arrow_box)?;
143140
s.arrow(
144-
[arrow_x + fpad.y(), select_y + fpad.y()],
141+
[
142+
arrow_x + fpad.y(),
143+
select_y + arrow_box.height() / 2 - arrow_width / 4,
144+
],
145145
Direction::Down,
146-
1.0,
146+
fpad.y() as Scalar / 8.0,
147147
)?;
148148
}
149149

@@ -221,7 +221,7 @@ impl PixState {
221221
label: S,
222222
selected: &mut usize,
223223
items: &[I],
224-
displayed_count: usize,
224+
mut displayed_count: usize,
225225
) -> PixResult<bool>
226226
where
227227
S: AsRef<str>,
@@ -230,13 +230,10 @@ impl PixState {
230230
let label = label.as_ref();
231231

232232
if displayed_count > MAX_DISPLAYED {
233-
bail!("displayed_count exceeds maximum of: {}", MAX_DISPLAYED);
234-
} else if *selected > items.len() {
235-
bail!(
236-
"selected out of bounds: the len is {} but the value is {}",
237-
items.len(),
238-
*selected
239-
);
233+
displayed_count = MAX_DISPLAYED;
234+
}
235+
if *selected >= items.len() {
236+
*selected = items.len() - 1;
240237
}
241238

242239
let s = self;

src/gui/widgets/text.rs

Lines changed: 52 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -194,73 +194,71 @@ impl PixState {
194194
angle_radians = angle.map(Scalar::to_radians);
195195
}
196196

197-
let mut render_text = |mut color: Color, outline: u8| -> PixResult<(u32, u32)> {
198-
s.push();
197+
let mut render_text =
198+
|pos: PointI2, mut color: Color, outline: u8| -> PixResult<Rect<i32>> {
199+
s.push();
199200

200-
// Make sure to offset the text if an outline was drawn
201-
if stroke.is_some() && stroke_weight > 0 && outline == 0 {
202-
pos += i32::from(stroke_weight);
203-
}
204-
if disabled {
205-
color = color.blended(colors.background, 0.38);
206-
}
207-
208-
let (w, h) = if wrap_width.is_some() {
209-
s.renderer.text(
210-
pos,
211-
text,
212-
wrap_width,
213-
angle,
214-
center,
215-
flipped,
216-
Some(color),
217-
outline,
218-
)?
219-
} else {
220-
let mut x = pos.x();
221-
let mut y = pos.y();
222-
let (mut total_width, mut total_height) = (0, 0);
223-
for line in text.split('\n') {
224-
let (line_width, line_height) = s.renderer.size_of(line, wrap_width)?;
225-
let rect = rect![0, 0, clamp_size(line_width), clamp_size(line_height)];
226-
let bounding_box =
227-
angle_radians.map_or(rect, |angle| rect.rotated(angle, center));
228-
x -= bounding_box.x();
229-
y -= bounding_box.y();
201+
if disabled {
202+
color = color.blended(colors.background, 0.38);
203+
}
204+
let (w, h) = if wrap_width.is_some() {
230205
s.renderer.text(
231-
point![x, y],
232-
line,
206+
pos,
207+
text,
233208
wrap_width,
234209
angle,
235210
center,
236211
flipped,
237212
Some(color),
238213
outline,
239-
)?;
240-
total_width += bounding_box.width() as u32;
241-
total_height += bounding_box.height() as u32;
242-
y += bounding_box.height();
243-
}
244-
(total_width, total_height)
214+
)?
215+
} else {
216+
let mut x = pos.x();
217+
let mut y = pos.y();
218+
let (mut total_width, mut total_height) = (0, 0);
219+
for line in text.split('\n') {
220+
let (line_width, line_height) = s.renderer.size_of(line, wrap_width)?;
221+
let rect = rect![0, 0, clamp_size(line_width), clamp_size(line_height)];
222+
let bounding_box =
223+
angle_radians.map_or(rect, |angle| rect.rotated(angle, center));
224+
x -= bounding_box.x();
225+
y -= bounding_box.y();
226+
s.renderer.text(
227+
point![x, y],
228+
line,
229+
wrap_width,
230+
angle,
231+
center,
232+
flipped,
233+
Some(color),
234+
outline,
235+
)?;
236+
total_width += bounding_box.width() as u32;
237+
total_height += bounding_box.height() as u32;
238+
y += bounding_box.height();
239+
}
240+
(total_width, total_height)
241+
};
242+
let rect = rect![pos, clamp_size(w), clamp_size(h)];
243+
244+
s.pop();
245+
Ok(rect)
245246
};
246-
let rect = rect![pos, clamp_size(w), clamp_size(h)];
247247

248-
// Only advance the cursor if we're not drawing a text outline
249-
if outline == 0 {
250-
s.advance_cursor(rect.size());
248+
let rect = {
249+
let stroke_rect = match stroke {
250+
Some(stroke) if stroke_weight > 0 => Some(render_text(pos, stroke, stroke_weight)?),
251+
_ => None,
252+
};
253+
if stroke_rect.is_some() {
254+
pos += i32::from(stroke_weight);
251255
}
252-
253-
s.pop();
254-
Ok((w, h))
255-
};
256-
257-
let stroke_size = match stroke {
258-
Some(stroke) if stroke_weight > 0 => Some(render_text(stroke, stroke_weight)?),
259-
_ => None,
256+
let text_rect = render_text(pos, fill, 0)?;
257+
stroke_rect.unwrap_or(text_rect)
260258
};
261-
let size = render_text(fill, 0)?;
259+
s.advance_cursor(rect.size());
262260

263-
Ok(stroke_size.unwrap_or(size))
261+
Ok((rect.width() as u32, rect.height() as u32))
264262
}
265263

266264
/// Draw bulleted text to the current canvas.

src/gui/widgets/tooltip.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ impl PixState {
268268
}
269269
if rect.bottom() > win_height {
270270
let offset = (rect.bottom() - win_height) + pad.y();
271-
rect = rect.offset([-offset, 0]);
271+
rect = rect.offset([0, -offset]);
272272
let mpos = s.mouse_pos();
273273
if rect.contains_point(mpos) {
274274
rect.set_bottom(mpos.y() - pad.y());

0 commit comments

Comments
 (0)