2023-07-13 03:54:27 +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-21 04:17:57 +03:00
|
|
|
|
2023-07-13 03:54:27 +03:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
2023-07-21 04:17:57 +03:00
|
|
|
|
2023-07-14 00:55:31 +03:00
|
|
|
use egui::os::OperatingSystem;
|
|
|
|
use egui::RichText;
|
2023-07-13 03:54:27 +03:00
|
|
|
use lazy_static::lazy_static;
|
|
|
|
|
2023-07-21 04:17:57 +03:00
|
|
|
use crate::gui::Colors;
|
2023-07-13 03:54:27 +03:00
|
|
|
use crate::gui::platform::PlatformCallbacks;
|
2023-08-03 04:11:25 +03:00
|
|
|
use crate::gui::views::{Modal, NetworkContent, View, WalletsContent};
|
|
|
|
use crate::gui::views::types::ModalContainer;
|
2023-07-14 00:55:31 +03:00
|
|
|
use crate::node::Node;
|
2023-07-13 03:54:27 +03:00
|
|
|
|
|
|
|
lazy_static! {
|
2023-07-31 16:23:15 +03:00
|
|
|
/// Global state to check if [`NetworkContent`] panel is open.
|
2023-07-21 04:17:57 +03:00
|
|
|
static ref NETWORK_PANEL_OPEN: AtomicBool = AtomicBool::new(false);
|
2023-07-13 03:54:27 +03:00
|
|
|
}
|
|
|
|
|
2023-07-14 00:55:31 +03:00
|
|
|
/// Contains main ui content, handles side panel state.
|
2023-07-13 03:54:27 +03:00
|
|
|
pub struct Root {
|
2023-07-31 16:23:15 +03:00
|
|
|
/// Side panel [`NetworkContent`] content.
|
|
|
|
network: NetworkContent,
|
2023-07-25 03:42:52 +03:00
|
|
|
/// Central panel [`WalletsContent`] content.
|
|
|
|
wallets: WalletsContent,
|
2023-07-14 00:55:31 +03:00
|
|
|
|
2023-07-21 04:17:57 +03:00
|
|
|
/// Check if app exit is allowed on close event of [`eframe::App`] implementation.
|
2023-07-14 00:55:31 +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 Root {
|
|
|
|
fn default() -> Self {
|
|
|
|
// Exit from eframe only for non-mobile platforms.
|
|
|
|
let os = OperatingSystem::from_target_os();
|
|
|
|
let exit_allowed = os == OperatingSystem::Android || os == OperatingSystem::IOS;
|
|
|
|
Self {
|
2023-07-31 16:23:15 +03:00
|
|
|
network: NetworkContent::default(),
|
2023-07-25 03:42:52 +03:00
|
|
|
wallets: WalletsContent::default(),
|
2023-07-14 00:55:31 +03:00
|
|
|
exit_allowed,
|
|
|
|
show_exit_progress: false,
|
|
|
|
allowed_modal_ids: vec![
|
|
|
|
Self::EXIT_MODAL_ID
|
|
|
|
],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ModalContainer for Root {
|
|
|
|
fn modal_ids(&self) -> &Vec<&'static str> {
|
|
|
|
&self.allowed_modal_ids
|
|
|
|
}
|
2023-08-03 04:11:25 +03:00
|
|
|
|
|
|
|
fn modal_ui(&mut self,
|
|
|
|
ui: &mut egui::Ui,
|
|
|
|
frame: &mut eframe::Frame,
|
|
|
|
modal: &Modal,
|
|
|
|
_: &dyn PlatformCallbacks) {
|
|
|
|
match modal.id {
|
|
|
|
Self::EXIT_MODAL_ID => self.exit_modal_content(ui, frame, modal),
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2023-07-13 03:54:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Root {
|
2023-07-14 00:55:31 +03:00
|
|
|
/// Identifier for exit confirmation [`Modal`].
|
|
|
|
pub const EXIT_MODAL_ID: &'static str = "exit_confirmation";
|
|
|
|
|
2023-07-13 03:54:27 +03:00
|
|
|
/// Default width of side panel at application UI.
|
2023-07-31 01:04:41 +03:00
|
|
|
pub const SIDE_PANEL_WIDTH: f32 = 400.0;
|
2023-07-13 03:54:27 +03:00
|
|
|
|
|
|
|
pub fn ui(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame, cb: &dyn PlatformCallbacks) {
|
2023-08-03 04:11:25 +03:00
|
|
|
// Draw modal content for current ui container.
|
|
|
|
self.current_modal_ui(ui, frame, cb);
|
2023-07-14 00:55:31 +03:00
|
|
|
|
2023-07-21 04:17:57 +03:00
|
|
|
let (is_panel_open, panel_width) = Self::network_panel_state_width(frame);
|
|
|
|
// Show network content.
|
2023-07-13 03:54:27 +03:00
|
|
|
egui::SidePanel::left("network_panel")
|
|
|
|
.resizable(false)
|
|
|
|
.exact_width(panel_width)
|
2023-07-21 04:17:57 +03:00
|
|
|
.frame(egui::Frame {
|
|
|
|
fill: Colors::WHITE,
|
|
|
|
..Default::default()
|
|
|
|
})
|
2023-07-13 03:54:27 +03:00
|
|
|
.show_animated_inside(ui, is_panel_open, |ui| {
|
2023-08-10 23:07:17 +03:00
|
|
|
// Set content height as window height.
|
|
|
|
let mut rect = ui.available_rect_before_wrap();
|
|
|
|
rect.set_height(frame.info().window_info.size.y);
|
|
|
|
ui.allocate_ui_at_rect(rect, |ui| {
|
|
|
|
self.network.ui(ui, frame, cb);
|
|
|
|
});
|
2023-07-13 03:54:27 +03:00
|
|
|
});
|
|
|
|
|
2023-07-21 04:17:57 +03:00
|
|
|
// Show wallets content.
|
2023-07-13 03:54:27 +03:00
|
|
|
egui::CentralPanel::default()
|
2023-07-21 04:17:57 +03:00
|
|
|
.frame(egui::Frame {
|
|
|
|
fill: Colors::FILL_DARK,
|
|
|
|
..Default::default()
|
|
|
|
})
|
2023-07-13 03:54:27 +03:00
|
|
|
.show_inside(ui, |ui| {
|
2023-08-10 23:07:17 +03:00
|
|
|
// Set content height as window height.
|
|
|
|
let mut rect = ui.available_rect_before_wrap();
|
|
|
|
rect.set_height(frame.info().window_info.size.y);
|
|
|
|
ui.allocate_ui_at_rect(rect, |ui| {
|
|
|
|
self.wallets.ui(ui, frame, cb);
|
|
|
|
});
|
2023-07-13 03:54:27 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-31 16:23:15 +03:00
|
|
|
/// Get [`NetworkContent`] panel state and width.
|
2023-07-21 04:17:57 +03:00
|
|
|
fn network_panel_state_width(frame: &mut eframe::Frame) -> (bool, f32) {
|
2023-07-13 03:54:27 +03:00
|
|
|
let dual_panel_mode = Self::is_dual_panel_mode(frame);
|
2023-07-21 04:17:57 +03:00
|
|
|
let is_panel_open = dual_panel_mode || Self::is_network_panel_open();
|
2023-07-13 03:54:27 +03:00
|
|
|
let panel_width = if dual_panel_mode {
|
2023-07-31 01:04:41 +03:00
|
|
|
Self::SIDE_PANEL_WIDTH + View::get_left_inset()
|
2023-07-13 03:54:27 +03:00
|
|
|
} else {
|
2023-07-16 11:23:56 +03:00
|
|
|
frame.info().window_info.size.x
|
2023-07-13 03:54:27 +03:00
|
|
|
};
|
|
|
|
(is_panel_open, panel_width)
|
|
|
|
}
|
|
|
|
|
2023-07-31 16:23:15 +03:00
|
|
|
/// Check if ui can show [`NetworkContent`] and [`WalletsContent`] at same time.
|
2023-07-13 03:54:27 +03:00
|
|
|
pub fn is_dual_panel_mode(frame: &mut eframe::Frame) -> bool {
|
|
|
|
let w = frame.info().window_info.size.x;
|
|
|
|
let h = frame.info().window_info.size.y;
|
|
|
|
// Screen is wide if width is greater than height or just 20% smaller.
|
|
|
|
let is_wide_screen = w > h || w + (w * 0.2) >= h;
|
|
|
|
// Dual panel mode is available when window is wide and its width is at least 2 times
|
2023-07-16 11:23:56 +03:00
|
|
|
// greater than minimal width of the side panel plus display insets from both sides.
|
|
|
|
let side_insets = View::get_left_inset() + View::get_right_inset();
|
2023-07-31 01:04:41 +03:00
|
|
|
is_wide_screen && w >= (Self::SIDE_PANEL_WIDTH * 2.0) + side_insets
|
2023-07-13 03:54:27 +03:00
|
|
|
}
|
|
|
|
|
2023-07-31 16:23:15 +03:00
|
|
|
/// Toggle [`NetworkContent`] panel state.
|
2023-07-21 04:17:57 +03:00
|
|
|
pub fn toggle_network_panel() {
|
|
|
|
let is_open = NETWORK_PANEL_OPEN.load(Ordering::Relaxed);
|
|
|
|
NETWORK_PANEL_OPEN.store(!is_open, Ordering::Relaxed);
|
2023-07-13 03:54:27 +03:00
|
|
|
}
|
|
|
|
|
2023-07-31 16:23:15 +03:00
|
|
|
/// Check if [`NetworkContent`] panel is open.
|
2023-07-21 04:17:57 +03:00
|
|
|
pub fn is_network_panel_open() -> bool {
|
|
|
|
NETWORK_PANEL_OPEN.load(Ordering::Relaxed)
|
2023-07-13 03:54:27 +03:00
|
|
|
}
|
|
|
|
|
2023-07-14 00:55:31 +03:00
|
|
|
/// Show exit confirmation modal.
|
|
|
|
pub fn show_exit_modal() {
|
2023-07-23 11:48:28 +03:00
|
|
|
Modal::new(Self::EXIT_MODAL_ID)
|
2023-08-05 22:39:42 +03:00
|
|
|
.title(t!("modal_exit.exit"))
|
2023-07-23 11:48:28 +03:00
|
|
|
.show();
|
2023-07-14 00:55:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Draw exit confirmation modal content.
|
2023-08-03 04:11:25 +03:00
|
|
|
fn exit_modal_content(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame, modal: &Modal) {
|
|
|
|
if self.show_exit_progress {
|
|
|
|
if !Node::is_running() {
|
|
|
|
self.exit(frame);
|
|
|
|
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(17.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(17.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);
|
2023-07-14 00:55:31 +03:00
|
|
|
modal.close();
|
2023-08-03 04:11:25 +03:00
|
|
|
} 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();
|
2023-07-14 00:55:31 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2023-08-03 04:11:25 +03:00
|
|
|
ui.add_space(6.0);
|
|
|
|
});
|
|
|
|
}
|
2023-07-14 00:55:31 +03:00
|
|
|
}
|
|
|
|
|
2023-08-01 01:03:51 +03:00
|
|
|
/// Exit from the application.
|
|
|
|
fn exit(&mut self, frame: &mut eframe::Frame) {
|
|
|
|
self.exit_allowed = true;
|
|
|
|
frame.close();
|
2023-07-14 00:55:31 +03:00
|
|
|
}
|
|
|
|
|
2023-07-21 04:17:57 +03:00
|
|
|
/// Handle Back key event.
|
|
|
|
pub fn on_back(&mut self) {
|
2023-07-13 03:54:27 +03:00
|
|
|
if Modal::on_back() {
|
2023-07-21 04:17:57 +03:00
|
|
|
if self.wallets.on_back() {
|
|
|
|
Self::show_exit_modal()
|
|
|
|
}
|
2023-07-13 03:54:27 +03:00
|
|
|
}
|
|
|
|
}
|
2023-07-21 04:17:57 +03:00
|
|
|
}
|