Turn off Window title bars with window.title_bar(false)

You can still resize and move them assuming there is some area that
does not steal the drag input. In particular, if a scroll area covers
the window than dragging the window contents will scroll, not move it.

Closes https://github.com/emilk/egui/issues/66
This commit is contained in:
Emil Ernerfeldt 2020-12-16 23:48:23 +01:00
parent c422ab930a
commit d6d9c4828e
2 changed files with 50 additions and 25 deletions

View file

@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Added ⭐ ### Added ⭐
* Turn off `Window` title bars with `window.title_bar(false)`.
* `ImageButton` - `ui.add(ImageButton::new(...))`. * `ImageButton` - `ui.add(ImageButton::new(...))`.
* `ui.vertical_centered` and `ui.vertical_centered_justified`. * `ui.vertical_centered` and `ui.vertical_centered_justified`.
* Mouse-over explanation to duplicate ID warning * Mouse-over explanation to duplicate ID warning

View file

@ -22,10 +22,12 @@ pub struct Window<'open> {
pub resize: Resize, pub resize: Resize,
pub scroll: Option<ScrollArea>, pub scroll: Option<ScrollArea>,
pub collapsible: bool, pub collapsible: bool,
pub with_title_bar: bool,
} }
impl<'open> Window<'open> { impl<'open> Window<'open> {
/// The winodw title must be unique, and should not change. /// The window title is used as a unique Id and must be unique, and should not change.
/// This is true even if you disable the title bar with `.title_bar(false)`.
pub fn new(title: impl Into<String>) -> Self { pub fn new(title: impl Into<String>) -> Self {
let title = title.into(); let title = title.into();
let area = Area::new(&title); let area = Area::new(&title);
@ -40,9 +42,10 @@ impl<'open> Window<'open> {
resize: Resize::default() resize: Resize::default()
.with_stroke(false) .with_stroke(false)
.min_size([96.0, 32.0]) .min_size([96.0, 32.0])
.default_size([420.0, 420.0]), // Default inner size of a winodw .default_size([420.0, 420.0]), // Default inner size of a window
scroll: None, scroll: None,
collapsible: true, collapsible: true,
with_title_bar: true,
} }
} }
@ -146,6 +149,13 @@ impl<'open> Window<'open> {
self self
} }
/// Show title bar on top of the window?
/// If `false`, the window will not be collapsible nor have a close-button.
pub fn title_bar(mut self, title_bar: bool) -> Self {
self.with_title_bar = title_bar;
self
}
/// Not resizable, just takes the size of its contents. /// Not resizable, just takes the size of its contents.
/// Also disabled scrolling. /// Also disabled scrolling.
/// Text will not wrap, but will instead make your window width expand. /// Text will not wrap, but will instead make your window width expand.
@ -190,6 +200,7 @@ impl<'open> Window<'open> {
resize, resize,
scroll, scroll,
collapsible, collapsible,
with_title_bar,
} = self; } = self;
if matches!(open, Some(false)) && !ctx.memory().all_windows_are_open { if matches!(open, Some(false)) && !ctx.memory().all_windows_are_open {
@ -201,8 +212,8 @@ impl<'open> Window<'open> {
let resize_id = window_id.with("resize"); let resize_id = window_id.with("resize");
let collapsing_id = window_id.with("collapsing"); let collapsing_id = window_id.with("collapsing");
let is_maximized = let is_maximized = !with_title_bar
collapsing_header::State::is_open(ctx, collapsing_id).unwrap_or_default(); || collapsing_header::State::is_open(ctx, collapsing_id).unwrap_or_default();
let possible = PossibleInteractions { let possible = PossibleInteractions {
movable: area.is_movable(), movable: area.is_movable(),
resizable: resize.is_resizable() && is_maximized, resizable: resize.is_resizable() && is_maximized,
@ -228,8 +239,12 @@ impl<'open> Window<'open> {
) )
.and_then(|window_interaction| { .and_then(|window_interaction| {
// 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_label.font_height(ctx.fonts(), &ctx.style()) let title_bar_height = if with_title_bar {
+ 1.0 * ctx.style().spacing.item_spacing.y; // this could be better title_label.font_height(ctx.fonts(), &ctx.style())
+ 1.0 * ctx.style().spacing.item_spacing.y // this could be better
} else {
0.0
};
let margins = 2.0 * frame.margin + vec2(0.0, title_bar_height); let margins = 2.0 * frame.margin + vec2(0.0, title_bar_height);
interact( interact(
@ -260,21 +275,28 @@ impl<'open> Window<'open> {
default_expanded, default_expanded,
); );
let show_close_button = open.is_some(); let show_close_button = open.is_some();
let title_bar = show_title_bar( let title_bar = if with_title_bar {
&mut frame.content_ui, let title_bar = show_title_bar(
title_label, &mut frame.content_ui,
show_close_button, title_label,
collapsing_id, show_close_button,
&mut collapsing, collapsing_id,
collapsible, &mut collapsing,
); 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)
} else {
None
};
let content_response = collapsing let content_response = collapsing
.add_contents(&mut frame.content_ui, collapsing_id, |ui| { .add_contents(&mut frame.content_ui, collapsing_id, |ui| {
resize.show(ui, |ui| { resize.show(ui, |ui| {
// Add some spacing between title and content: if title_bar.is_some() {
ui.allocate_space(ui.style().spacing.item_spacing); // Add some spacing between title and content:
ui.allocate_space(ui.style().spacing.item_spacing);
}
if let Some(scroll) = scroll { if let Some(scroll) = scroll {
scroll.show(ui, add_contents); scroll.show(ui, add_contents);
@ -293,14 +315,16 @@ impl<'open> Window<'open> {
// END FRAME -------------------------------- // END FRAME --------------------------------
title_bar.ui( if let Some(title_bar) = title_bar {
&mut area_content_ui, title_bar.ui(
outer_rect, &mut area_content_ui,
&content_response, outer_rect,
open, &content_response,
&mut collapsing, open,
collapsible, &mut collapsing,
); collapsible,
);
}
area_content_ui area_content_ui
.memory() .memory()