Skip to content

Commit 525d435

Browse files
authored
Require a StrokeKind when painting rectangles with strokes (emilk#5648)
This is a breaking change, requiring users to think about wether the stroke is inside/centered/outside the rect. When in doubt, add `egui::StrokeKind::Inside` to the function call.
1 parent 8d2c8c2 commit 525d435

File tree

35 files changed

+184
-73
lines changed

35 files changed

+184
-73
lines changed

crates/egui/src/containers/collapsing_header.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
Response, Sense, Stroke, TextStyle, TextWrapMode, Ui, Vec2, WidgetInfo, WidgetText, WidgetType,
66
};
77
use emath::GuiRounding as _;
8-
use epaint::Shape;
8+
use epaint::{Shape, StrokeKind};
99

1010
#[derive(Clone, Copy, Debug)]
1111
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
@@ -576,15 +576,21 @@ impl CollapsingHeader {
576576
visuals.rounding,
577577
visuals.weak_bg_fill,
578578
visuals.bg_stroke,
579+
StrokeKind::Inside,
579580
));
580581
}
581582

582583
if selected || selectable && (header_response.hovered() || header_response.has_focus())
583584
{
584585
let rect = rect.expand(visuals.expansion);
585586

586-
ui.painter()
587-
.rect(rect, visuals.rounding, visuals.bg_fill, visuals.bg_stroke);
587+
ui.painter().rect(
588+
rect,
589+
visuals.rounding,
590+
visuals.bg_fill,
591+
visuals.bg_stroke,
592+
StrokeKind::Inside,
593+
);
588594
}
589595

590596
{

crates/egui/src/containers/combo_box.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ fn button_frame(
474474
visuals.rounding,
475475
visuals.weak_bg_fill,
476476
visuals.bg_stroke,
477+
epaint::StrokeKind::Inside,
477478
),
478479
);
479480
}

crates/egui/src/containers/frame.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -423,10 +423,13 @@ impl Frame {
423423
let fill_rect = self.fill_rect(content_rect);
424424
let widget_rect = self.widget_rect(content_rect);
425425

426-
let frame_shape = Shape::Rect(
427-
epaint::RectShape::new(fill_rect, rounding, fill, stroke)
428-
.with_stroke_kind(epaint::StrokeKind::Outside),
429-
);
426+
let frame_shape = Shape::Rect(epaint::RectShape::new(
427+
fill_rect,
428+
rounding,
429+
fill,
430+
stroke,
431+
epaint::StrokeKind::Outside,
432+
));
430433

431434
if shadow == Default::default() {
432435
frame_shape

crates/egui/src/containers/resize.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ impl Resize {
356356
rect,
357357
3.0,
358358
ui.visuals().widgets.noninteractive.bg_stroke,
359+
epaint::StrokeKind::Inside,
359360
));
360361
}
361362

crates/egui/src/context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use epaint::{
1111
tessellator,
1212
text::{FontInsert, FontPriority, Fonts},
1313
util::OrderedFloat,
14-
vec2, ClippedPrimitive, ClippedShape, Color32, ImageData, ImageDelta, Pos2, Rect,
14+
vec2, ClippedPrimitive, ClippedShape, Color32, ImageData, ImageDelta, Pos2, Rect, StrokeKind,
1515
TessellationOptions, TextureAtlas, TextureId, Vec2,
1616
};
1717

@@ -1087,7 +1087,7 @@ impl Context {
10871087
let text = format!("🔥 {text}");
10881088
let color = self.style().visuals.error_fg_color;
10891089
let painter = self.debug_painter();
1090-
painter.rect_stroke(widget_rect, 0.0, (1.0, color));
1090+
painter.rect_stroke(widget_rect, 0.0, (1.0, color), StrokeKind::Outside);
10911091

10921092
let below = widget_rect.bottom() + 32.0 < screen_rect.bottom();
10931093

crates/egui/src/grid.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,12 @@ impl GridLayout {
208208

209209
if (debug_expand_width && too_wide) || (debug_expand_height && too_high) {
210210
let painter = self.ctx.debug_painter();
211-
painter.rect_stroke(rect, 0.0, (1.0, Color32::LIGHT_BLUE));
211+
painter.rect_stroke(
212+
rect,
213+
0.0,
214+
(1.0, Color32::LIGHT_BLUE),
215+
crate::StrokeKind::Inside,
216+
);
212217

213218
let stroke = Stroke::new(2.5, Color32::from_rgb(200, 0, 0));
214219
let paint_line_seg = |a, b| painter.line_segment([a, b], stroke);

crates/egui/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ pub use epaint::{
465465
text::{FontData, FontDefinitions, FontFamily, FontId, FontTweak},
466466
textures::{TextureFilter, TextureOptions, TextureWrapMode, TexturesDelta},
467467
ClippedPrimitive, ColorImage, FontImage, ImageData, Margin, Mesh, PaintCallback,
468-
PaintCallbackInfo, Rounding, Shadow, Shape, Stroke, TextureHandle, TextureId,
468+
PaintCallbackInfo, Rounding, Shadow, Shape, Stroke, StrokeKind, TextureHandle, TextureId,
469469
};
470470

471471
pub mod text {

crates/egui/src/painter.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::sync::Arc;
33
use emath::GuiRounding as _;
44
use epaint::{
55
text::{Fonts, Galley, LayoutJob},
6-
CircleShape, ClippedShape, PathStroke, RectShape, Rounding, Shape, Stroke,
6+
CircleShape, ClippedShape, PathStroke, RectShape, Rounding, Shape, Stroke, StrokeKind,
77
};
88

99
use crate::{
@@ -301,6 +301,7 @@ impl Painter {
301301
0.0,
302302
color.additive().linear_multiply(0.015),
303303
(1.0, color),
304+
StrokeKind::Outside,
304305
);
305306
self.text(
306307
rect.min,
@@ -407,15 +408,22 @@ impl Painter {
407408
})
408409
}
409410

410-
/// The stroke extends _outside_ the [`Rect`].
411+
/// See also [`Self::rect_filled`] and [`Self::rect_stroke`].
411412
pub fn rect(
412413
&self,
413414
rect: Rect,
414415
rounding: impl Into<Rounding>,
415416
fill_color: impl Into<Color32>,
416417
stroke: impl Into<Stroke>,
418+
stroke_kind: StrokeKind,
417419
) -> ShapeIdx {
418-
self.add(RectShape::new(rect, rounding, fill_color, stroke))
420+
self.add(RectShape::new(
421+
rect,
422+
rounding,
423+
fill_color,
424+
stroke,
425+
stroke_kind,
426+
))
419427
}
420428

421429
pub fn rect_filled(
@@ -433,8 +441,9 @@ impl Painter {
433441
rect: Rect,
434442
rounding: impl Into<Rounding>,
435443
stroke: impl Into<Stroke>,
444+
stroke_kind: StrokeKind,
436445
) -> ShapeIdx {
437-
self.add(RectShape::stroke(rect, rounding, stroke))
446+
self.add(RectShape::stroke(rect, rounding, stroke, stroke_kind))
438447
}
439448

440449
/// Show an arrow starting at `origin` and going in the direction of `vec`, with the length `vec.length()`.

crates/egui/src/pass_state.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,13 @@ impl DebugRect {
121121
Color32::LIGHT_BLUE
122122
};
123123
let rect_bg_color = Color32::BLUE.gamma_multiply(0.5);
124-
painter.rect(rect, 0.0, rect_bg_color, (1.0, rect_fg_color));
124+
painter.rect(
125+
rect,
126+
0.0,
127+
rect_bg_color,
128+
(1.0, rect_fg_color),
129+
crate::StrokeKind::Outside,
130+
);
125131
}
126132

127133
if !callstack.is_empty() {
@@ -157,7 +163,13 @@ impl DebugRect {
157163
text_bg_color
158164
};
159165
let text_rect = Rect::from_min_size(text_pos, galley.size());
160-
painter.rect(text_rect, 0.0, text_bg_color, (1.0, text_rect_stroke_color));
166+
painter.rect(
167+
text_rect,
168+
0.0,
169+
text_bg_color,
170+
(1.0, text_rect_stroke_color),
171+
crate::StrokeKind::Middle,
172+
);
161173
painter.galley(text_pos, galley, text_color);
162174

163175
if is_clicking {

crates/egui/src/placer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ impl Placer {
281281

282282
if let Some(grid) = &self.grid {
283283
let rect = grid.next_cell(self.cursor(), Vec2::splat(0.0));
284-
painter.rect_stroke(rect, 1.0, stroke);
284+
painter.rect_stroke(rect, 1.0, stroke, epaint::StrokeKind::Inside);
285285
let align = Align2::CENTER_CENTER;
286286
painter.debug_text(align.pos_in_rect(&rect), align, stroke.color, text);
287287
} else {

0 commit comments

Comments
 (0)