2023-04-12 21:52:35 +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.
|
|
|
|
|
2023-07-11 23:36:48 +03:00
|
|
|
use egui::{Context, RichText, Stroke};
|
2023-06-02 02:05:34 +03:00
|
|
|
use egui::os::OperatingSystem;
|
2023-07-13 03:54:27 +03:00
|
|
|
use crate::gui::Colors;
|
2023-04-27 01:28:55 +03:00
|
|
|
|
|
|
|
use crate::gui::platform::PlatformCallbacks;
|
2023-07-13 03:54:27 +03:00
|
|
|
use crate::gui::views::{Modal, ModalContainer, Root, View};
|
2023-06-13 23:45:03 +03:00
|
|
|
use crate::node::Node;
|
2023-04-19 02:14:39 +03:00
|
|
|
|
2023-07-11 23:36:48 +03:00
|
|
|
/// To be implemented at platform-specific module.
|
2023-04-18 16:19:43 +03:00
|
|
|
pub struct PlatformApp<Platform> {
|
2023-04-27 01:28:55 +03:00
|
|
|
pub(crate) app: App,
|
2023-04-18 16:19:43 +03:00
|
|
|
pub(crate) platform: Platform,
|
2023-04-19 02:14:39 +03:00
|
|
|
}
|
|
|
|
|
2023-07-13 03:54:27 +03:00
|
|
|
/// Contains main ui, handles exit and visual style setup.
|
2023-04-27 01:28:55 +03:00
|
|
|
pub struct App {
|
2023-07-13 03:54:27 +03:00
|
|
|
/// Main ui content.
|
2023-04-27 01:28:55 +03:00
|
|
|
root: Root,
|
2023-07-13 03:54:27 +03:00
|
|
|
/// Check if app exit is allowed on close event of [`eframe::App`] platform implementation.
|
2023-07-11 23:36:48 +03:00
|
|
|
pub(crate) exit_allowed: bool,
|
|
|
|
/// Flag to show exit progress at modal.
|
|
|
|
show_exit_progress: bool,
|
|
|
|
/// List of allowed modal ids for this [`ModalContainer`].
|
|
|
|
allowed_modal_ids: Vec<&'static str>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for App {
|
|
|
|
fn default() -> Self {
|
|
|
|
let os = OperatingSystem::from_target_os();
|
|
|
|
// Exit from eframe only for non-mobile platforms.
|
2023-07-13 03:54:27 +03:00
|
|
|
let exit_allowed = os == OperatingSystem::Android || os == OperatingSystem::IOS;
|
2023-07-11 23:36:48 +03:00
|
|
|
Self {
|
|
|
|
root: Root::default(),
|
2023-07-13 03:54:27 +03:00
|
|
|
exit_allowed,
|
2023-07-11 23:36:48 +03:00
|
|
|
show_exit_progress: false,
|
|
|
|
allowed_modal_ids: vec![
|
2023-07-13 03:54:27 +03:00
|
|
|
Self::EXIT_MODAL
|
2023-07-11 23:36:48 +03:00
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ModalContainer for App {
|
|
|
|
fn modal_ids(&self) -> &Vec<&'static str> {
|
|
|
|
&self.allowed_modal_ids
|
|
|
|
}
|
2023-04-27 01:28:55 +03:00
|
|
|
}
|
2023-04-21 03:45:59 +03:00
|
|
|
|
2023-04-27 01:28:55 +03:00
|
|
|
impl App {
|
2023-07-13 03:54:27 +03:00
|
|
|
/// Identifier for exit confirmation [`Modal`].
|
|
|
|
pub const EXIT_MODAL: &'static str = "exit_confirmation";
|
|
|
|
|
2023-06-15 23:54:41 +03:00
|
|
|
/// Draw content on main screen panel.
|
2023-05-17 00:36:59 +03:00
|
|
|
pub fn ui(&mut self, ctx: &Context, frame: &mut eframe::Frame, cb: &dyn PlatformCallbacks) {
|
2023-04-22 21:16:17 +03:00
|
|
|
egui::CentralPanel::default()
|
2023-06-02 02:05:34 +03:00
|
|
|
.frame(egui::Frame {
|
2023-06-03 11:22:51 +03:00
|
|
|
fill: Colors::FILL,
|
2023-07-03 21:17:49 +03:00
|
|
|
..Default::default()
|
2023-04-21 23:59:59 +03:00
|
|
|
})
|
2023-04-22 21:16:17 +03:00
|
|
|
.show(ctx, |ui| {
|
2023-07-13 03:54:27 +03:00
|
|
|
// Draw modal content if it's open.
|
|
|
|
if self.can_draw_modal() {
|
2023-07-11 23:36:48 +03:00
|
|
|
self.exit_modal_content(ui, frame, cb);
|
|
|
|
}
|
2023-07-13 03:54:27 +03:00
|
|
|
// Draw main content.
|
2023-06-02 02:05:34 +03:00
|
|
|
self.root.ui(ui, frame, cb);
|
2023-06-02 21:19:34 +03:00
|
|
|
});
|
2023-04-19 02:14:39 +03:00
|
|
|
}
|
2023-04-21 03:45:59 +03:00
|
|
|
|
2023-07-11 23:36:48 +03:00
|
|
|
/// Draw exit confirmation modal content.
|
|
|
|
fn exit_modal_content(&mut self,
|
|
|
|
ui: &mut egui::Ui,
|
|
|
|
frame: &mut eframe::Frame,
|
|
|
|
cb: &dyn PlatformCallbacks) {
|
2023-07-13 03:54:27 +03:00
|
|
|
Modal::ui(ui, |ui, modal| {
|
2023-07-11 23:36:48 +03:00
|
|
|
if self.show_exit_progress {
|
|
|
|
if !Node::is_running() {
|
|
|
|
self.exit(frame, cb);
|
|
|
|
modal.close();
|
|
|
|
}
|
|
|
|
ui.add_space(16.0);
|
|
|
|
ui.vertical_centered(|ui| {
|
|
|
|
View::small_loading_spinner(ui);
|
|
|
|
ui.add_space(12.0);
|
|
|
|
ui.label(RichText::new(t!("sync_status.shutdown"))
|
|
|
|
.size(18.0)
|
|
|
|
.color(Colors::TEXT));
|
|
|
|
});
|
|
|
|
ui.add_space(10.0);
|
|
|
|
} else {
|
|
|
|
ui.add_space(8.0);
|
|
|
|
ui.vertical_centered(|ui| {
|
|
|
|
ui.label(RichText::new(t!("modal_exit.description"))
|
|
|
|
.size(18.0)
|
|
|
|
.color(Colors::TEXT));
|
|
|
|
});
|
|
|
|
ui.add_space(10.0);
|
|
|
|
|
|
|
|
// Show modal buttons.
|
|
|
|
ui.scope(|ui| {
|
|
|
|
// Setup spacing between buttons.
|
|
|
|
ui.spacing_mut().item_spacing = egui::Vec2::new(6.0, 0.0);
|
|
|
|
|
|
|
|
ui.columns(2, |columns| {
|
|
|
|
columns[0].vertical_centered_justified(|ui| {
|
|
|
|
View::button(ui, t!("modal_exit.exit"), Colors::WHITE, || {
|
|
|
|
if !Node::is_running() {
|
|
|
|
self.exit(frame, cb);
|
|
|
|
modal.close();
|
|
|
|
} else {
|
|
|
|
Node::stop(true);
|
|
|
|
modal.disable_closing();
|
|
|
|
self.show_exit_progress = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
columns[1].vertical_centered_justified(|ui| {
|
|
|
|
View::button(ui, t!("modal.cancel"), Colors::WHITE, || {
|
|
|
|
modal.close();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
ui.add_space(6.0);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Platform-specific exit from the application.
|
|
|
|
fn exit(&mut self, frame: &mut eframe::Frame, cb: &dyn PlatformCallbacks) {
|
2023-06-02 02:05:34 +03:00
|
|
|
match OperatingSystem::from_target_os() {
|
|
|
|
OperatingSystem::Android => {
|
|
|
|
cb.exit();
|
|
|
|
}
|
|
|
|
OperatingSystem::IOS => {
|
2023-07-13 03:54:27 +03:00
|
|
|
//TODO: exit on iOS.
|
2023-06-02 02:05:34 +03:00
|
|
|
}
|
|
|
|
OperatingSystem::Nix | OperatingSystem::Mac | OperatingSystem::Windows => {
|
2023-07-11 23:36:48 +03:00
|
|
|
self.exit_allowed = true;
|
2023-06-02 02:05:34 +03:00
|
|
|
frame.close();
|
|
|
|
}
|
|
|
|
OperatingSystem::Unknown => {}
|
|
|
|
}
|
|
|
|
}
|
2023-06-02 22:55:07 +03:00
|
|
|
|
2023-06-15 23:54:41 +03:00
|
|
|
/// Setup application styles.
|
2023-06-02 22:55:07 +03:00
|
|
|
pub fn setup_visuals(ctx: &Context) {
|
|
|
|
let mut style = (*ctx.style()).clone();
|
|
|
|
// Setup spacing for buttons.
|
|
|
|
style.spacing.button_padding = egui::vec2(12.0, 8.0);
|
|
|
|
// Make scroll-bar thinner.
|
|
|
|
style.spacing.scroll_bar_width = 4.0;
|
|
|
|
// Disable spacing between items.
|
|
|
|
style.spacing.item_spacing = egui::vec2(0.0, 0.0);
|
2023-06-17 13:23:31 +03:00
|
|
|
// Setup radio button/checkbox size and spacing.
|
|
|
|
style.spacing.icon_width = 24.0;
|
|
|
|
style.spacing.icon_width_inner = 14.0;
|
|
|
|
style.spacing.icon_spacing = 10.0;
|
2023-06-23 22:12:30 +03:00
|
|
|
// Setup style
|
2023-06-02 22:55:07 +03:00
|
|
|
ctx.set_style(style);
|
|
|
|
|
|
|
|
let mut visuals = egui::Visuals::light();
|
2023-06-23 22:12:30 +03:00
|
|
|
// Setup selection color.
|
|
|
|
visuals.selection.stroke = Stroke { width: 1.0, color: Colors::TEXT };
|
|
|
|
visuals.selection.bg_fill = Colors::GOLD;
|
2023-06-02 22:55:07 +03:00
|
|
|
// Disable stroke around panels by default
|
|
|
|
visuals.widgets.noninteractive.bg_stroke = Stroke::NONE;
|
2023-06-23 22:12:30 +03:00
|
|
|
// Setup visuals
|
2023-06-02 22:55:07 +03:00
|
|
|
ctx.set_visuals(visuals);
|
|
|
|
}
|
|
|
|
|
2023-06-15 23:54:41 +03:00
|
|
|
/// Setup application fonts.
|
2023-06-02 22:55:07 +03:00
|
|
|
pub fn setup_fonts(ctx: &Context) {
|
|
|
|
use egui::FontFamily::Proportional;
|
|
|
|
|
|
|
|
let mut fonts = egui::FontDefinitions::default();
|
|
|
|
|
|
|
|
fonts.font_data.insert(
|
|
|
|
"phosphor".to_owned(),
|
|
|
|
egui::FontData::from_static(include_bytes!(
|
|
|
|
"../../fonts/phosphor.ttf"
|
|
|
|
)).tweak(egui::FontTweak {
|
|
|
|
scale: 1.0,
|
2023-07-07 03:50:08 +03:00
|
|
|
y_offset_factor: -0.30,
|
|
|
|
y_offset: 0.0,
|
|
|
|
baseline_offset_factor: 0.30,
|
2023-06-02 22:55:07 +03:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
fonts
|
|
|
|
.families
|
|
|
|
.entry(Proportional)
|
|
|
|
.or_default()
|
|
|
|
.insert(0, "phosphor".to_owned());
|
|
|
|
|
|
|
|
fonts.font_data.insert(
|
|
|
|
"noto".to_owned(),
|
|
|
|
egui::FontData::from_static(include_bytes!(
|
|
|
|
"../../fonts/noto_sc_reg.otf"
|
|
|
|
)).tweak(egui::FontTweak {
|
|
|
|
scale: 1.0,
|
|
|
|
y_offset_factor: -0.25,
|
2023-07-07 03:50:08 +03:00
|
|
|
y_offset: 0.0,
|
|
|
|
baseline_offset_factor: 0.17,
|
2023-06-02 22:55:07 +03:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
fonts
|
|
|
|
.families
|
|
|
|
.entry(Proportional)
|
|
|
|
.or_default()
|
|
|
|
.insert(0, "noto".to_owned());
|
|
|
|
|
|
|
|
ctx.set_fonts(fonts);
|
|
|
|
|
|
|
|
use egui::FontId;
|
|
|
|
use egui::TextStyle::*;
|
|
|
|
|
|
|
|
let mut style = (*ctx.style()).clone();
|
|
|
|
style.text_styles = [
|
|
|
|
(Heading, FontId::new(20.0, Proportional)),
|
|
|
|
(Body, FontId::new(16.0, Proportional)),
|
|
|
|
(Button, FontId::new(18.0, Proportional)),
|
|
|
|
(Small, FontId::new(12.0, Proportional)),
|
|
|
|
(Monospace, FontId::new(16.0, Proportional)),
|
|
|
|
].into();
|
|
|
|
|
|
|
|
ctx.set_style(style);
|
|
|
|
}
|
2023-05-04 20:09:26 +03:00
|
|
|
|
2023-07-13 03:54:27 +03:00
|
|
|
/// Show exit confirmation modal.
|
|
|
|
pub fn show_exit_modal() {
|
|
|
|
let exit_modal = Modal::new(Self::EXIT_MODAL).title(t!("modal_exit.exit"));
|
|
|
|
Modal::show(exit_modal);
|
|
|
|
}
|
2023-06-13 23:45:03 +03:00
|
|
|
}
|