grim/src/gui/views/title_panel.rs

146 lines
4.9 KiB
Rust
Raw Normal View History

2023-04-27 01:28:55 +03:00
// Copyright 2023 The Grim Developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use eframe::epaint::{FontId, Stroke};
2023-04-27 21:20:22 +03:00
use eframe::epaint::text::{LayoutJob, TextFormat, TextWrapping};
2023-04-27 01:28:55 +03:00
use egui::style::Margin;
use egui_extras::{Size, StripBuilder};
use crate::gui::colors::{COLOR_DARK, COLOR_YELLOW};
2023-05-04 20:09:26 +03:00
2023-04-27 21:20:22 +03:00
use crate::gui::screens::Navigator;
use crate::gui::views::common::title_button;
2023-04-27 01:28:55 +03:00
2023-05-04 20:09:26 +03:00
pub struct TitlePanelAction {
2023-04-27 21:20:22 +03:00
pub(crate) icon: Box<str>,
pub(crate) on_click: Box<dyn Fn(&mut Navigator)>,
2023-04-27 01:28:55 +03:00
}
#[derive(Default)]
2023-05-04 20:09:26 +03:00
pub struct TitlePanelActions {
left: Option<TitlePanelAction>,
right: Option<TitlePanelAction>
2023-04-27 01:28:55 +03:00
}
2023-05-04 20:09:26 +03:00
pub struct TitlePanel<'nav> {
title: Option<String>,
2023-05-04 20:09:26 +03:00
actions: TitlePanelActions,
nav: &'nav mut Navigator
2023-04-27 01:28:55 +03:00
}
2023-05-04 20:09:26 +03:00
impl<'nav> TitlePanel<'nav> {
pub fn new(nav: &'nav mut Navigator) -> TitlePanel<'nav> {
Self {
title: None,
actions: Default::default(),
nav,
}
}
pub fn title(mut self, title: String) -> Self {
2023-04-27 01:28:55 +03:00
self.title = Some(title);
self
}
2023-05-04 20:09:26 +03:00
pub fn left_action(mut self, action: TitlePanelAction) -> Self {
2023-04-27 22:02:10 +03:00
self.actions.left = Some(action);
2023-04-27 01:28:55 +03:00
self
}
2023-05-04 20:09:26 +03:00
pub fn right_action(mut self, action: TitlePanelAction) -> Self {
2023-04-27 22:02:10 +03:00
self.actions.right = Some(action);
2023-04-27 01:28:55 +03:00
self
}
2023-05-04 20:09:26 +03:00
pub fn ui(&mut self, ui: &mut egui::Ui) {
// Disable stroke around panel buttons on hover
2023-04-27 01:28:55 +03:00
ui.style_mut().visuals.widgets.active.bg_stroke = Stroke::NONE;
let Self { actions, title, nav } = self;
2023-04-27 01:28:55 +03:00
egui::TopBottomPanel::top("title_panel")
.resizable(false)
.frame(egui::Frame {
fill: COLOR_YELLOW,
inner_margin: Margin::same(0.0),
outer_margin: Margin::same(0.0),
2023-04-27 21:20:22 +03:00
stroke: Stroke::NONE,
2023-04-27 01:28:55 +03:00
..Default::default()
})
.show_inside(ui, |ui| {
StripBuilder::new(ui)
2023-04-27 21:20:22 +03:00
.size(Size::exact(52.0))
2023-04-27 01:28:55 +03:00
.vertical(|mut strip| {
strip.strip(|builder| {
builder
2023-04-27 21:20:22 +03:00
.size(Size::exact(52.0))
2023-04-27 01:28:55 +03:00
.size(Size::remainder())
2023-04-27 21:20:22 +03:00
.size(Size::exact(52.0))
2023-04-27 01:28:55 +03:00
.horizontal(|mut strip| {
strip.cell(|ui| {
Self::show_action(ui, actions.left.as_ref(), nav);
2023-04-27 01:28:55 +03:00
});
strip.strip(|builder| {
builder
.size(Size::remainder())
.vertical(|mut strip| {
strip.cell(|ui| {
Self::show_title(&*title, ui);
2023-04-27 01:28:55 +03:00
});
});
});
strip.cell(|ui| {
Self::show_action(ui, actions.right.as_ref(), nav);
2023-04-27 01:28:55 +03:00
});
});
});
});
});
}
2023-05-04 20:09:26 +03:00
fn show_action(ui: &mut egui::Ui,
action: Option<&TitlePanelAction>,
navigator: &mut Navigator) {
if action.is_some() {
let action = action.unwrap();
ui.centered_and_justified(|ui| {
title_button(ui, &action.icon, || {
(action.on_click)(navigator);
});
});
}
}
fn show_title(title: &Option<String>, ui: &mut egui::Ui) {
if title.is_some() {
ui.centered_and_justified(|ui| {
let title_text = title.as_ref().unwrap().to_uppercase();
let mut job = LayoutJob::single_section(title_text, TextFormat {
font_id: FontId::proportional(20.0),
color: COLOR_DARK,
.. Default::default()
});
job.wrap = TextWrapping {
max_rows: 1,
break_anywhere: false,
overflow_character: Option::from(''),
..Default::default()
};
ui.label(job);
});
}
2023-05-04 20:09:26 +03:00
}
2023-04-27 21:20:22 +03:00
}