mirror of
https://github.com/emilk/egui.git
synced 2026-08-30 13:20:05 -04:00
WIP: get rid of clip_rect_margin
This commit is contained in:
@@ -434,8 +434,15 @@ impl<'open> Window<'open> {
|
|||||||
let header_color =
|
let header_color =
|
||||||
frame.map_or_else(|| ctx.style().visuals.widgets.open.weak_bg_fill, |f| f.fill);
|
frame.map_or_else(|| ctx.style().visuals.widgets.open.weak_bg_fill, |f| f.fill);
|
||||||
let mut window_frame = frame.unwrap_or_else(|| Frame::window(&ctx.style()));
|
let mut window_frame = frame.unwrap_or_else(|| Frame::window(&ctx.style()));
|
||||||
|
|
||||||
// Keep the original inner margin for later use
|
// Keep the original inner margin for later use
|
||||||
let window_margin = window_frame.inner_margin;
|
let original_window_margin = window_frame.inner_margin;
|
||||||
|
|
||||||
|
// We apply the inner margin inside any scroll area.
|
||||||
|
let innermost_margin = std::mem::take(&mut window_frame.inner_margin);
|
||||||
|
|
||||||
|
// The margin we use elsewhere is only half the border stroke width:
|
||||||
|
|
||||||
let border_padding = window_frame.stroke.width / 2.0;
|
let border_padding = window_frame.stroke.width / 2.0;
|
||||||
// Add border padding to the inner margin to prevent it from covering the contents
|
// Add border padding to the inner margin to prevent it from covering the contents
|
||||||
window_frame.inner_margin += border_padding;
|
window_frame.inner_margin += border_padding;
|
||||||
@@ -467,15 +474,15 @@ impl<'open> Window<'open> {
|
|||||||
let mut area = area.begin(ctx);
|
let mut area = area.begin(ctx);
|
||||||
|
|
||||||
// Calculate roughly how much larger the window size is compared to the inner rect
|
// Calculate roughly how much larger the window size is compared to the inner rect
|
||||||
let (title_bar_height, title_content_spacing) = if with_title_bar {
|
let title_bar_height = if with_title_bar {
|
||||||
let style = ctx.style();
|
let style = ctx.style();
|
||||||
let spacing = window_margin.top + window_margin.bottom;
|
let spacing = original_window_margin.sum().y;
|
||||||
let height = ctx.fonts(|f| title.font_height(f, &style)) + spacing;
|
let height = ctx.fonts(|f| title.font_height(f, &style)) + spacing;
|
||||||
window_frame.rounding.ne = window_frame.rounding.ne.clamp(0.0, height / 2.0);
|
window_frame.rounding.ne = window_frame.rounding.ne.clamp(0.0, height / 2.0);
|
||||||
window_frame.rounding.nw = window_frame.rounding.nw.clamp(0.0, height / 2.0);
|
window_frame.rounding.nw = window_frame.rounding.nw.clamp(0.0, height / 2.0);
|
||||||
(height, spacing)
|
height
|
||||||
} else {
|
} else {
|
||||||
(0.0, 0.0)
|
0.0
|
||||||
};
|
};
|
||||||
|
|
||||||
{
|
{
|
||||||
@@ -492,18 +499,18 @@ impl<'open> Window<'open> {
|
|||||||
let resize_interaction =
|
let resize_interaction =
|
||||||
resize_interaction(ctx, possible, area_layer_id, last_frame_outer_rect);
|
resize_interaction(ctx, possible, area_layer_id, last_frame_outer_rect);
|
||||||
|
|
||||||
let margins = window_frame.outer_margin.sum()
|
{
|
||||||
+ window_frame.inner_margin.sum()
|
let margins = window_frame.total_margin().sum() + vec2(0.0, title_bar_height);
|
||||||
+ vec2(0.0, title_bar_height);
|
|
||||||
|
|
||||||
resize_response(
|
resize_response(
|
||||||
resize_interaction,
|
resize_interaction,
|
||||||
ctx,
|
ctx,
|
||||||
margins,
|
margins,
|
||||||
area_layer_id,
|
area_layer_id,
|
||||||
&mut area,
|
&mut area,
|
||||||
resize_id,
|
resize_id,
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
let mut area_content_ui = area.content_ui(ctx);
|
let mut area_content_ui = area.content_ui(ctx);
|
||||||
if is_open {
|
if is_open {
|
||||||
@@ -522,38 +529,43 @@ impl<'open> Window<'open> {
|
|||||||
|
|
||||||
let where_to_put_header_background = &area_content_ui.painter().add(Shape::Noop);
|
let where_to_put_header_background = &area_content_ui.painter().add(Shape::Noop);
|
||||||
|
|
||||||
// Backup item spacing before the title bar
|
// Backup item spacing
|
||||||
let item_spacing = frame.content_ui.spacing().item_spacing;
|
let old_item_spacing = frame.content_ui.spacing().item_spacing;
|
||||||
// Use title bar spacing as the item spacing before the content
|
|
||||||
frame.content_ui.spacing_mut().item_spacing.y = title_content_spacing;
|
// We do manual spacing for a while
|
||||||
|
frame.content_ui.spacing_mut().item_spacing.y = 0.0;
|
||||||
|
|
||||||
|
let inner_frame = Frame {
|
||||||
|
inner_margin: innermost_margin,
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
let title_bar = if with_title_bar {
|
let title_bar = if with_title_bar {
|
||||||
let title_bar = TitleBar::new(
|
let title_bar = inner_frame
|
||||||
&mut frame.content_ui,
|
.show(&mut frame.content_ui, |ui| {
|
||||||
title,
|
TitleBar::new(ui, title, show_close_button, &mut collapsing, collapsible)
|
||||||
show_close_button,
|
})
|
||||||
&mut collapsing,
|
.inner;
|
||||||
collapsible,
|
|
||||||
);
|
|
||||||
resize.min_size.x = resize.min_size.x.at_least(title_bar.rect.width()); // Prevent making window smaller than title bar width
|
resize.min_size.x = resize.min_size.x.at_least(title_bar.rect.width()); // Prevent making window smaller than title bar width
|
||||||
|
|
||||||
Some(title_bar)
|
Some(title_bar)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
// Remove item spacing after the title bar
|
|
||||||
frame.content_ui.spacing_mut().item_spacing.y = 0.0;
|
|
||||||
|
|
||||||
let (content_inner, mut content_response) = collapsing
|
let (content_inner, mut content_response) = collapsing
|
||||||
.show_body_unindented(&mut frame.content_ui, |ui| {
|
.show_body_unindented(&mut frame.content_ui, |ui| {
|
||||||
// Restore item spacing for the content
|
// Restore item spacing for the content
|
||||||
ui.spacing_mut().item_spacing.y = item_spacing.y;
|
ui.spacing_mut().item_spacing = old_item_spacing;
|
||||||
|
|
||||||
resize.show(ui, |ui| {
|
resize.show(ui, |ui| {
|
||||||
if scroll.is_any_scroll_enabled() {
|
if scroll.is_any_scroll_enabled() {
|
||||||
scroll.show(ui, add_contents).inner
|
scroll
|
||||||
|
.show(ui, |ui| inner_frame.show(ui, add_contents).inner)
|
||||||
|
.inner
|
||||||
} else {
|
} else {
|
||||||
add_contents(ui)
|
inner_frame.show(ui, add_contents).inner
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -606,6 +618,7 @@ impl<'open> Window<'open> {
|
|||||||
title_bar.ui(
|
title_bar.ui(
|
||||||
&mut area_content_ui,
|
&mut area_content_ui,
|
||||||
title_rect,
|
title_rect,
|
||||||
|
innermost_margin,
|
||||||
&content_response,
|
&content_response,
|
||||||
open,
|
open,
|
||||||
&mut collapsing,
|
&mut collapsing,
|
||||||
@@ -1041,6 +1054,7 @@ struct TitleBar {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl TitleBar {
|
impl TitleBar {
|
||||||
|
/// Only paints the collapse button - the rest is painted by [`TitleBar::ui`].
|
||||||
fn new(
|
fn new(
|
||||||
ui: &mut Ui,
|
ui: &mut Ui,
|
||||||
title: WidgetText,
|
title: WidgetText,
|
||||||
@@ -1112,6 +1126,7 @@ impl TitleBar {
|
|||||||
mut self,
|
mut self,
|
||||||
ui: &mut Ui,
|
ui: &mut Ui,
|
||||||
outer_rect: Rect,
|
outer_rect: Rect,
|
||||||
|
margin: Margin,
|
||||||
content_response: &Option<Response>,
|
content_response: &Option<Response>,
|
||||||
open: Option<&mut bool>,
|
open: Option<&mut bool>,
|
||||||
collapsing: &mut CollapsingState,
|
collapsing: &mut CollapsingState,
|
||||||
@@ -1124,7 +1139,7 @@ impl TitleBar {
|
|||||||
|
|
||||||
if let Some(open) = open {
|
if let Some(open) = open {
|
||||||
// Add close button now that we know our full width:
|
// Add close button now that we know our full width:
|
||||||
if self.close_button_ui(ui).clicked() {
|
if self.close_button_ui(ui, margin).clicked() {
|
||||||
*open = false;
|
*open = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1141,7 +1156,7 @@ impl TitleBar {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if let Some(content_response) = &content_response {
|
if let Some(content_response) = &content_response {
|
||||||
// paint separator between title and content:
|
// Paint separator between title and content:
|
||||||
let y = content_response.rect.top();
|
let y = content_response.rect.top();
|
||||||
// let y = lerp(self.rect.bottom()..=content_response.rect.top(), 0.5);
|
// let y = lerp(self.rect.bottom()..=content_response.rect.top(), 0.5);
|
||||||
let stroke = ui.visuals().widgets.noninteractive.bg_stroke;
|
let stroke = ui.visuals().widgets.noninteractive.bg_stroke;
|
||||||
@@ -1168,12 +1183,12 @@ impl TitleBar {
|
|||||||
///
|
///
|
||||||
/// The button is square and its size is determined by the
|
/// The button is square and its size is determined by the
|
||||||
/// [`crate::style::Spacing::icon_width`] setting.
|
/// [`crate::style::Spacing::icon_width`] setting.
|
||||||
fn close_button_ui(&self, ui: &mut Ui) -> Response {
|
fn close_button_ui(&self, ui: &mut Ui, margin: Margin) -> Response {
|
||||||
let button_size = Vec2::splat(ui.spacing().icon_width);
|
let button_size = Vec2::splat(ui.spacing().icon_width);
|
||||||
let pad = (self.rect.height() - button_size.y) / 2.0; // calculated so that the icon is on the diagonal (if window padding is symmetrical)
|
let pad = (self.rect.height() - button_size.y) / 2.0; // calculated so that the icon is on the diagonal (if window padding is symmetrical)
|
||||||
let button_rect = Rect::from_min_size(
|
let button_rect = Rect::from_min_size(
|
||||||
pos2(
|
pos2(
|
||||||
self.rect.right() - pad - button_size.x,
|
self.rect.right() - margin.right - pad - button_size.x,
|
||||||
self.rect.center().y - 0.5 * button_size.y,
|
self.rect.center().y - 0.5 * button_size.y,
|
||||||
),
|
),
|
||||||
button_size,
|
button_size,
|
||||||
|
|||||||
@@ -1302,7 +1302,7 @@ impl Visuals {
|
|||||||
|
|
||||||
text_cursor: Default::default(),
|
text_cursor: Default::default(),
|
||||||
|
|
||||||
clip_rect_margin: 3.0, // should be at least half the size of the widest frame stroke + max WidgetVisuals::expansion
|
clip_rect_margin: 0.0, // should be at least half the size of the widest frame stroke + max WidgetVisuals::expansion
|
||||||
button_frame: true,
|
button_frame: true,
|
||||||
collapsing_header_frame: false,
|
collapsing_header_frame: false,
|
||||||
indent_has_left_vline: true,
|
indent_has_left_vline: true,
|
||||||
|
|||||||
Reference in New Issue
Block a user