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 all 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 33 additions & 7 deletions crates/egui/src/containers/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,10 +405,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.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 @@ -420,13 +429,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 @@ -453,6 +455,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 @@ -489,6 +494,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 @@ -1982,6 +1982,11 @@ impl Context {
self.memory_mut(|mem| mem.areas_mut().move_to_top(layer_id));
}

/// Retrieve the [`LayerId`] of the top level windows.
pub fn top_layer_id(&self) -> Option<LayerId> {
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
15 changes: 11 additions & 4 deletions crates/egui/src/memory.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#![warn(missing_docs)] // Let's keep this file well-documented.` to memory.rs

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

use crate::{
area,
area, vec2,
window::{self, WindowInteraction},
EventFilter, Id, IdMap, LayerId, Pos2, Rect, Style, ViewportId, ViewportIdMap, ViewportIdSet,
EventFilter, Id, IdMap, LayerId, Order, Pos2, Rangef, Rect, Style, Vec2, ViewportId,
ViewportIdMap, ViewportIdSet,
};

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -908,6 +907,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 @@ -138,7 +138,10 @@ impl super::View for WindowOptions {
});

ui.separator();
let on_top = Some(ui.layer_id()) == ui.ctx().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
Loading