Add clock in top right corner using reverse layout in menu bar

This commit is contained in:
Emil Ernerfeldt 2020-05-13 22:56:37 +02:00
parent cd1bbddaca
commit 152e644fb2
4 changed files with 41 additions and 10 deletions

View file

@ -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)
})
})

View file

@ -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();
}

View file

@ -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)

View file

@ -182,6 +182,7 @@ impl Widget for Hyperlink {
pub struct Button {
text: String,
text_color: Option<Color>,
text_style: TextStyle,
/// None means default for interact
fill_color: Option<Color>,
}
@ -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<Color>) -> 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)
}