From 152e644fb2134fa39cace9a385a5a5e9962a1a23 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Wed, 13 May 2020 22:56:37 +0200 Subject: [PATCH] Add clock in top right corner using reverse layout in menu bar --- emigui/src/containers/menu.rs | 4 +++- emigui/src/examples/app.rs | 20 +++++++++++++++++++- emigui/src/ui.rs | 4 ++-- emigui/src/widgets.rs | 23 +++++++++++++++++------ 4 files changed, 41 insertions(+), 10 deletions(-) diff --git a/emigui/src/containers/menu.rs b/emigui/src/containers/menu.rs index 0b5c0373..ff8a8c4f 100644 --- a/emigui/src/containers/menu.rs +++ b/emigui/src/containers/menu.rs @@ -31,7 +31,9 @@ pub fn bar(ui: &mut Ui, add_contents: impl FnOnce(&mut Ui)) -> InteractInfo { ui.set_style(style); // Take full width and fixed height: - ui.expand_to_size(vec2(ui.available().width(), ui.style().menu_bar.height)); + let height = ui.style().menu_bar.height; + ui.set_desired_height(height); + ui.expand_to_size(vec2(ui.available().width(), height)); add_contents(ui) }) }) diff --git a/emigui/src/examples/app.rs b/emigui/src/examples/app.rs index 16c9dea8..6193d9d2 100644 --- a/emigui/src/examples/app.rs +++ b/emigui/src/examples/app.rs @@ -135,6 +135,24 @@ fn show_menu_bar(ui: &mut Ui, windows: &mut OpenWindows) { ui.add(label!("This is Emigui")); ui.add(Hyperlink::new("https://github.com/emilk/emigui/").text("Emigui home page")); }); + + if let Some(time) = ui.input().seconds_since_midnight { + let time = format!( + "{:02}:{:02}:{:02}.{:02}", + (time.rem_euclid(24.0 * 60.0 * 60.0) / 3600.0).floor(), + (time.rem_euclid(60.0 * 60.0) / 60.0).floor(), + (time.rem_euclid(60.0)).floor(), + (time.rem_euclid(1.0) * 100.0).floor() + ); + ui.inner_layout(Layout::horizontal(Align::Max).reverse(), |ui| { + if ui + .add(Button::new(time).text_style(TextStyle::Monospace)) + .clicked + { + windows.fractal_clock = !windows.fractal_clock; + } + }); + } }); } @@ -474,7 +492,7 @@ impl LayoutExample { ui.set_layout(layout); } - ui.add(label!("Available space: {:?}", ui.available().size())); + // ui.add(label!("Available space: {:?}", ui.available().size())); if ui.add(Button::new("Reset")).clicked { *self = Default::default(); } diff --git a/emigui/src/ui.rs b/emigui/src/ui.rs index 79659ccb..2a984680 100644 --- a/emigui/src/ui.rs +++ b/emigui/src/ui.rs @@ -204,7 +204,7 @@ impl Ui { /// Size of content pub fn bounding_size(&self) -> Vec2 { - self.child_bounds.max - self.top_left() + self.child_bounds.size() } /// Expand the bounding rect of this ui to include a child at the given rect. @@ -602,9 +602,9 @@ impl Ui { ) -> InteractInfo { let child_rect = Rect::from_min_max(self.cursor, self.bottom_right()); let mut child_ui = Self { - layout, ..self.child_ui(child_rect) }; + child_ui.set_layout(layout); // HACK: need a separate call right now add_contents(&mut child_ui); let size = child_ui.bounding_size(); self.reserve_space(size, None) diff --git a/emigui/src/widgets.rs b/emigui/src/widgets.rs index 3a9a981c..42e087f4 100644 --- a/emigui/src/widgets.rs +++ b/emigui/src/widgets.rs @@ -182,6 +182,7 @@ impl Widget for Hyperlink { pub struct Button { text: String, text_color: Option, + text_style: TextStyle, /// None means default for interact fill_color: Option, } @@ -191,6 +192,7 @@ impl Button { Self { text: text.into(), text_color: None, + text_style: TextStyle::Button, fill_color: None, } } @@ -200,6 +202,11 @@ impl Button { self } + pub fn text_style(mut self, text_style: TextStyle) -> Self { + self.text_style = text_style; + self + } + pub fn fill_color(mut self, fill_color: Option) -> Self { self.fill_color = fill_color; self @@ -208,19 +215,23 @@ impl Button { impl Widget for Button { fn ui(self, ui: &mut Ui) -> GuiResponse { + let Button { + text, + text_color, + text_style, + fill_color, + } = self; + let id = ui.make_position_id(); - let text_style = TextStyle::Button; let font = &ui.fonts()[text_style]; - let (text, text_size) = font.layout_multiline(&self.text, ui.available().width()); + let (text, text_size) = font.layout_multiline(&text, ui.available().width()); let padding = ui.style().button_padding; let mut size = text_size + 2.0 * padding; size.y = size.y.max(ui.style().clickable_diameter); let interact = ui.reserve_space(size, Some(id)); let mut text_cursor = interact.rect.left_center() + vec2(padding.x, -0.5 * text_size.y); text_cursor.y += 2.0; // TODO: why is this needed? - let fill_color = self - .fill_color - .or(ui.style().interact(&interact).fill_color); + let fill_color = fill_color.or(ui.style().interact(&interact).fill_color); ui.add_paint_cmd(PaintCmd::Rect { corner_radius: ui.style().interact(&interact).corner_radius, fill_color: fill_color, @@ -228,7 +239,7 @@ impl Widget for Button { rect: interact.rect, }); let stroke_color = ui.style().interact(&interact).stroke_color; - let text_color = self.text_color.unwrap_or(stroke_color); + let text_color = text_color.unwrap_or(stroke_color); ui.add_text(text_cursor, text_style, text, Some(text_color)); ui.response(interact) }