Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Highlight the header of the topmost Window #3515

Merged
merged 18 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions crates/egui/src/containers/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub struct Frame {

pub fill: Color32,

pub selected_header_color: Color32,
emilk marked this conversation as resolved.
Show resolved Hide resolved

pub stroke: Stroke,
}

Expand Down Expand Up @@ -232,6 +234,7 @@ impl Frame {
rounding,
shadow,
fill,
selected_header_color: _,
stroke,
} = *self;

Expand Down
40 changes: 33 additions & 7 deletions crates/egui/src/containers/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,19 @@ impl<'open> Window<'open> {
let resize = resize.resizable(false); // We move it manually
let mut resize = resize.id(resize_id);

let on_top = Some(area_layer_id) == ctx.get_top_layer_id();
let mut area = area.begin(ctx);

let title_content_spacing = 2.0 * ctx.style().spacing.item_spacing.y;

// Calculate roughly how much larger the window size is compared to the inner rect
let title_bar_height = if with_title_bar {
let style = ctx.style();
ctx.fonts(|f| title.font_height(f, &style)) + title_content_spacing * 2.0
Copy link

@margual56 margual56 Feb 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This (the * 2.0) is the culprit. When set to 1.0, the result is the expected one:

image

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious why only your window is broken, and no other one on egui.rs

Maybe you can make a PR to fix it?

} else {
0.0
};

// First interact (move etc) to avoid frame delay:
let last_frame_outer_rect = area.state().rect();
let interaction = if possible.movable || possible.resizable() {
Expand All @@ -365,13 +374,6 @@ impl<'open> Window<'open> {
last_frame_outer_rect,
)
.and_then(|window_interaction| {
// Calculate roughly how much larger the window size is compared to the inner rect
let title_bar_height = if with_title_bar {
let style = ctx.style();
ctx.fonts(|f| title.font_height(f, &style)) + title_content_spacing
} else {
0.0
};
let margins = frame.outer_margin.sum()
+ frame.inner_margin.sum()
+ vec2(0.0, title_bar_height);
Expand All @@ -398,6 +400,9 @@ impl<'open> Window<'open> {
let mut frame = frame.begin(&mut area_content_ui);

let show_close_button = open.is_some();

let where_to_put_header_background = &area_content_ui.painter().add(Shape::Noop);

let title_bar = if with_title_bar {
let title_bar = show_title_bar(
&mut frame.content_ui,
Expand Down Expand Up @@ -434,6 +439,27 @@ impl<'open> Window<'open> {
// END FRAME --------------------------------

if let Some(title_bar) = title_bar {
if on_top {
let rect = Rect::from_min_size(
outer_rect.min,
Vec2 {
x: outer_rect.size().x,
y: title_bar_height,
},
);
let mut round = area_content_ui.visuals().window_rounding;
if !is_collapsed {
round.se = 0.0;
round.sw = 0.0;
}
let header_color = area_content_ui.visuals().widgets.hovered.bg_fill;

area_content_ui.painter().set(
*where_to_put_header_background,
RectShape::filled(rect, round, header_color),
);
};

title_bar.ui(
&mut area_content_ui,
outer_rect,
Expand Down
5 changes: 5 additions & 0 deletions crates/egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1518,6 +1518,11 @@ impl Context {
self.memory_mut(|mem| mem.areas.move_to_top(layer_id));
}

/// Retrieve the `LayerId` of the top level windows.
pub fn get_top_layer_id(&self) -> Option<LayerId> {
GuillaumeSchmid marked this conversation as resolved.
Show resolved Hide resolved
self.memory(|mem| mem.areas.top_layer_id(Order::Middle))
}

pub(crate) fn rect_contains_pointer(&self, layer_id: LayerId, rect: Rect) -> bool {
rect.is_positive() && {
let pointer_pos = self.input(|i| i.pointer.interact_pos());
Expand Down
10 changes: 9 additions & 1 deletion crates/egui/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use epaint::{emath::Rangef, vec2, Vec2};

use crate::{area, window, EventFilter, Id, IdMap, InputState, LayerId, Pos2, Rect, Style};
use crate::{area, window, EventFilter, Id, IdMap, InputState, LayerId, Order, Pos2, Rect, Style};

// ----------------------------------------------------------------------------

Expand Down Expand Up @@ -819,6 +819,14 @@ impl Areas {
}
}

pub fn top_layer_id(&self, order: Order) -> Option<LayerId> {
self.order
.iter()
.filter(|layer| layer.order == order)
.last()
.copied()
}

pub(crate) fn end_frame(&mut self) {
let Self {
visible_last_frame,
Expand Down
3 changes: 3 additions & 0 deletions crates/egui_demo_lib/src/demo/window_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ impl super::View for WindowOptions {
});

ui.separator();
let on_top = Some(ui.layer_id()) == ui.ctx().get_top_layer_id();
ui.label(format!("This window is on top: {on_top}."));

ui.separator();
ui.horizontal(|ui| {
if ui.button("Disable for 2 seconds").clicked() {
self.disabled_time = ui.input(|i| i.time);
Expand Down