// 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 egui::Id; use egui::style::Margin; use egui_extras::{Size, StripBuilder}; use crate::gui::Colors; use crate::gui::views::View; pub struct TitleAction { pub(crate) icon: Box<&'static str>, pub(crate) on_click: Box, } impl TitleAction { pub fn new(icon: &'static str, on_click: fn()) -> Option { Option::from(Self { icon: Box::new(icon), on_click: Box::new(on_click) }) } } /// Represents title content, can be text or callback to draw custom title. pub enum TitleContent { Text(String), /// First argument is identifier for panel. Custom(String, Box) } pub struct TitlePanel; impl TitlePanel { pub const DEFAULT_HEIGHT: f32 = 52.0; pub fn test_ui(title: TitleContent, l: Option, r: Option, ui: &mut egui::Ui) { let id = match &title { TitleContent::Text(text) => Id::from(text.clone()), TitleContent::Custom(text, _) => Id::from(text.clone()) }; egui::TopBottomPanel::top(id) .resizable(false) .exact_height(Self::DEFAULT_HEIGHT) .frame(egui::Frame { outer_margin: Margin::same(-1.0), fill: Colors::YELLOW, ..Default::default() }) .show_inside(ui, |ui| { StripBuilder::new(ui) .size(Size::exact(Self::DEFAULT_HEIGHT)) .size(Size::remainder()) .size(Size::exact(Self::DEFAULT_HEIGHT)) .horizontal(|mut strip| { strip.cell(|ui| { Self::draw_action(ui, l); }); strip.cell(|ui| { match title { TitleContent::Text(text) => { Self::draw_title(ui, text); } TitleContent::Custom(_, cb) => { (cb)(ui); } } }); strip.cell(|ui| { Self::draw_action(ui, r); }); }); }); } pub fn ui(title: String, l: Option, r: Option, ui: &mut egui::Ui) { egui::TopBottomPanel::top(Id::from(title.clone())) .resizable(false) .exact_height(Self::DEFAULT_HEIGHT) .frame(egui::Frame { outer_margin: Margin::same(-1.0), fill: Colors::YELLOW, ..Default::default() }) .show_inside(ui, |ui| { StripBuilder::new(ui) .size(Size::exact(Self::DEFAULT_HEIGHT)) .size(Size::remainder()) .size(Size::exact(Self::DEFAULT_HEIGHT)) .horizontal(|mut strip| { strip.cell(|ui| { Self::draw_action(ui, l); }); strip.cell(|ui| { Self::draw_title(ui, title); }); strip.cell(|ui| { Self::draw_action(ui, r); }); }); }); } fn draw_action(ui: &mut egui::Ui, action: Option) { if action.is_some() { let action = action.unwrap(); ui.centered_and_justified(|ui| { View::title_button(ui, &action.icon, || { (action.on_click)(); }); }); } } fn draw_title(ui: &mut egui::Ui, title: String) { ui.add_space(2.0); ui.centered_and_justified(|ui| { View::ellipsize_text(ui, title.to_uppercase(), 20.0, Colors::TITLE); }); } }