2023-07-03 21:17:49 +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 egui::{Color32, lerp, Rgba, RichText};
|
|
|
|
use egui::style::Margin;
|
|
|
|
use egui_extras::{Size, StripBuilder};
|
|
|
|
use grin_chain::SyncStatus;
|
|
|
|
|
|
|
|
use crate::AppConfig;
|
2023-07-13 03:54:27 +03:00
|
|
|
use crate::gui::Colors;
|
2023-07-11 03:02:44 +03:00
|
|
|
use crate::gui::icons::{CARDHOLDER, DATABASE, DOTS_THREE_OUTLINE_VERTICAL, FACTORY, FADERS, GAUGE, POWER};
|
2023-07-03 21:17:49 +03:00
|
|
|
use crate::gui::platform::PlatformCallbacks;
|
2023-07-13 03:54:27 +03:00
|
|
|
use crate::gui::views::{Modal, ModalContainer, Root, TitlePanel, View};
|
2023-07-13 16:23:38 +03:00
|
|
|
use crate::gui::views::network::setup::dandelion::DandelionSetup;
|
|
|
|
use crate::gui::views::network::setup::node::NodeSetup;
|
|
|
|
use crate::gui::views::network::setup::p2p::P2PSetup;
|
|
|
|
use crate::gui::views::network::setup::pool::PoolSetup;
|
|
|
|
use crate::gui::views::network::setup::stratum::StratumSetup;
|
2023-07-03 21:17:49 +03:00
|
|
|
use crate::gui::views::network::metrics::NetworkMetrics;
|
|
|
|
use crate::gui::views::network::mining::NetworkMining;
|
|
|
|
use crate::gui::views::network::node::NetworkNode;
|
2023-07-04 00:39:13 +03:00
|
|
|
use crate::gui::views::network::settings::NetworkSettings;
|
2023-07-03 21:17:49 +03:00
|
|
|
use crate::node::Node;
|
|
|
|
|
|
|
|
pub trait NetworkTab {
|
|
|
|
fn get_type(&self) -> NetworkTabType;
|
|
|
|
fn ui(&mut self, ui: &mut egui::Ui, cb: &dyn PlatformCallbacks);
|
|
|
|
fn on_modal_ui(&mut self, ui: &mut egui::Ui, modal: &Modal, cb: &dyn PlatformCallbacks);
|
|
|
|
}
|
|
|
|
|
2023-07-13 03:54:27 +03:00
|
|
|
|
2023-07-03 21:17:49 +03:00
|
|
|
#[derive(PartialEq)]
|
|
|
|
pub enum NetworkTabType {
|
|
|
|
Node,
|
|
|
|
Metrics,
|
|
|
|
Mining,
|
|
|
|
Settings
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NetworkTabType {
|
2023-07-13 03:54:27 +03:00
|
|
|
pub fn title(&self) -> String {
|
2023-07-03 21:17:49 +03:00
|
|
|
match *self {
|
|
|
|
NetworkTabType::Node => { t!("network.node") }
|
|
|
|
NetworkTabType::Metrics => { t!("network.metrics") }
|
|
|
|
NetworkTabType::Mining => { t!("network.mining") }
|
|
|
|
NetworkTabType::Settings => { t!("network.settings") }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-13 03:54:27 +03:00
|
|
|
/// Network side panel content.
|
|
|
|
pub struct NetworkContent {
|
|
|
|
/// Current tab view to show at ui.
|
2023-07-03 21:17:49 +03:00
|
|
|
current_tab: Box<dyn NetworkTab>,
|
2023-07-13 03:54:27 +03:00
|
|
|
/// [`Modal`] ids allowed at this ui container.
|
2023-07-03 21:17:49 +03:00
|
|
|
modal_ids: Vec<&'static str>,
|
|
|
|
}
|
|
|
|
|
2023-07-13 03:54:27 +03:00
|
|
|
impl Default for NetworkContent {
|
2023-07-03 21:17:49 +03:00
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
current_tab: Box::new(NetworkNode::default()),
|
|
|
|
modal_ids: vec![
|
2023-07-05 16:06:14 +03:00
|
|
|
// Network settings modals.
|
2023-07-04 00:39:13 +03:00
|
|
|
NetworkSettings::NODE_RESTART_REQUIRED_MODAL,
|
|
|
|
NetworkSettings::RESET_SETTINGS_MODAL,
|
2023-07-05 16:06:14 +03:00
|
|
|
// Node setup modals.
|
|
|
|
NodeSetup::API_PORT_MODAL,
|
|
|
|
NodeSetup::API_SECRET_MODAL,
|
|
|
|
NodeSetup::FOREIGN_API_SECRET_MODAL,
|
|
|
|
NodeSetup::FTL_MODAL,
|
2023-07-11 03:02:44 +03:00
|
|
|
// P2P setup modals.
|
|
|
|
P2PSetup::PORT_MODAL,
|
|
|
|
P2PSetup::CUSTOM_SEED_MODAL,
|
|
|
|
P2PSetup::ALLOW_PEER_MODAL,
|
|
|
|
P2PSetup::DENY_PEER_MODAL,
|
|
|
|
P2PSetup::PREFER_PEER_MODAL,
|
|
|
|
P2PSetup::BAN_WINDOW_MODAL,
|
|
|
|
P2PSetup::MAX_INBOUND_MODAL,
|
|
|
|
P2PSetup::MAX_OUTBOUND_MODAL,
|
|
|
|
P2PSetup::MIN_OUTBOUND_MODAL,
|
2023-07-05 16:06:14 +03:00
|
|
|
// Stratum setup modals.
|
|
|
|
StratumSetup::STRATUM_PORT_MODAL,
|
|
|
|
StratumSetup::ATTEMPT_TIME_MODAL,
|
|
|
|
StratumSetup::MIN_SHARE_DIFF_MODAL,
|
|
|
|
// Pool setup modals.
|
|
|
|
PoolSetup::FEE_BASE_MODAL,
|
|
|
|
PoolSetup::REORG_PERIOD_MODAL,
|
|
|
|
PoolSetup::POOL_SIZE_MODAL,
|
|
|
|
PoolSetup::STEMPOOL_SIZE_MODAL,
|
|
|
|
PoolSetup::MAX_WEIGHT_MODAL,
|
|
|
|
// Dandelion setup modals.
|
|
|
|
DandelionSetup::EPOCH_MODAL,
|
|
|
|
DandelionSetup::EMBARGO_MODAL,
|
|
|
|
DandelionSetup::AGGREGATION_MODAL,
|
|
|
|
DandelionSetup::STEM_PROBABILITY_MODAL,
|
2023-07-03 21:17:49 +03:00
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-13 03:54:27 +03:00
|
|
|
impl ModalContainer for NetworkContent {
|
2023-07-03 21:17:49 +03:00
|
|
|
fn modal_ids(&self) -> &Vec<&'static str> {
|
|
|
|
self.modal_ids.as_ref()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-13 03:54:27 +03:00
|
|
|
impl NetworkContent {
|
2023-07-03 21:17:49 +03:00
|
|
|
pub fn ui(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame, cb: &dyn PlatformCallbacks) {
|
|
|
|
// Show modal content if it's opened.
|
2023-07-13 03:54:27 +03:00
|
|
|
if self.can_draw_modal() {
|
|
|
|
Modal::ui(ui, |ui, modal| {
|
2023-07-03 21:17:49 +03:00
|
|
|
self.current_tab.as_mut().on_modal_ui(ui, modal, cb);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
egui::TopBottomPanel::top("network_title")
|
2023-07-11 03:02:44 +03:00
|
|
|
.exact_height(TitlePanel::DEFAULT_HEIGHT)
|
2023-07-03 21:17:49 +03:00
|
|
|
.resizable(false)
|
|
|
|
.frame(egui::Frame {
|
|
|
|
fill: Colors::YELLOW,
|
|
|
|
inner_margin: Margin::same(0.0),
|
|
|
|
outer_margin: Margin::same(0.0),
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.show_inside(ui, |ui| {
|
|
|
|
self.title_ui(ui, frame);
|
|
|
|
});
|
|
|
|
|
|
|
|
egui::TopBottomPanel::bottom("network_tabs")
|
|
|
|
.frame(egui::Frame {
|
2023-07-07 03:50:08 +03:00
|
|
|
outer_margin: Margin::same(4.0),
|
2023-07-03 21:17:49 +03:00
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.show_inside(ui, |ui| {
|
|
|
|
self.tabs_ui(ui);
|
|
|
|
});
|
|
|
|
|
|
|
|
egui::CentralPanel::default()
|
|
|
|
.frame(egui::Frame {
|
|
|
|
stroke: View::DEFAULT_STROKE,
|
2023-07-11 21:02:36 +03:00
|
|
|
inner_margin: Margin {
|
|
|
|
left: 4.0,
|
|
|
|
right: 4.0,
|
|
|
|
top: 3.0,
|
|
|
|
bottom: 4.0,
|
|
|
|
},
|
2023-07-03 21:17:49 +03:00
|
|
|
fill: Colors::WHITE,
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.show_inside(ui, |ui| {
|
|
|
|
self.current_tab.ui(ui, cb);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Draw tab buttons in the bottom of the screen.
|
|
|
|
fn tabs_ui(&mut self, ui: &mut egui::Ui) {
|
|
|
|
ui.scope(|ui| {
|
|
|
|
// Setup spacing between tabs.
|
2023-07-07 03:50:08 +03:00
|
|
|
ui.style_mut().spacing.item_spacing = egui::vec2(4.0, 0.0);
|
2023-07-03 21:17:49 +03:00
|
|
|
// Setup vertical padding inside tab button.
|
2023-07-07 03:50:08 +03:00
|
|
|
ui.style_mut().spacing.button_padding = egui::vec2(0.0, 8.0);
|
2023-07-03 21:17:49 +03:00
|
|
|
|
2023-07-13 03:54:27 +03:00
|
|
|
// Draw tab buttons.
|
|
|
|
let current_type = self.current_tab.get_type();
|
2023-07-03 21:17:49 +03:00
|
|
|
ui.columns(4, |columns| {
|
|
|
|
columns[0].vertical_centered_justified(|ui| {
|
2023-07-13 03:54:27 +03:00
|
|
|
View::tab_button(ui, DATABASE, current_type == NetworkTabType::Node, || {
|
2023-07-03 21:17:49 +03:00
|
|
|
self.current_tab = Box::new(NetworkNode::default());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
columns[1].vertical_centered_justified(|ui| {
|
2023-07-13 03:54:27 +03:00
|
|
|
View::tab_button(ui, GAUGE, current_type == NetworkTabType::Metrics, || {
|
2023-07-03 21:17:49 +03:00
|
|
|
self.current_tab = Box::new(NetworkMetrics::default());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
columns[2].vertical_centered_justified(|ui| {
|
2023-07-13 03:54:27 +03:00
|
|
|
View::tab_button(ui, FACTORY, current_type == NetworkTabType::Mining, || {
|
2023-07-03 21:17:49 +03:00
|
|
|
self.current_tab = Box::new(NetworkMining::default());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
columns[3].vertical_centered_justified(|ui| {
|
2023-07-13 03:54:27 +03:00
|
|
|
View::tab_button(ui, FADERS, current_type == NetworkTabType::Settings, || {
|
2023-07-04 00:39:13 +03:00
|
|
|
self.current_tab = Box::new(NetworkSettings::default());
|
2023-07-03 21:17:49 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Draw title content.
|
|
|
|
fn title_ui(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame) {
|
|
|
|
StripBuilder::new(ui)
|
2023-07-11 03:02:44 +03:00
|
|
|
.size(Size::exact(52.0))
|
|
|
|
.size(Size::remainder())
|
|
|
|
.size(Size::exact(52.0))
|
|
|
|
.horizontal(|mut strip| {
|
|
|
|
strip.cell(|ui| {
|
|
|
|
ui.centered_and_justified(|ui| {
|
|
|
|
View::title_button(ui, DOTS_THREE_OUTLINE_VERTICAL, || {
|
|
|
|
//TODO: Show connections
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2023-07-03 21:17:49 +03:00
|
|
|
strip.strip(|builder| {
|
2023-07-11 03:02:44 +03:00
|
|
|
self.title_text_ui(builder);
|
|
|
|
});
|
|
|
|
strip.cell(|ui| {
|
2023-07-13 03:54:27 +03:00
|
|
|
if !Root::is_dual_panel_mode(frame) {
|
2023-07-11 03:02:44 +03:00
|
|
|
ui.centered_and_justified(|ui| {
|
|
|
|
View::title_button(ui, CARDHOLDER, || {
|
2023-07-13 03:54:27 +03:00
|
|
|
Root::toggle_network_panel();
|
2023-07-03 21:17:49 +03:00
|
|
|
});
|
|
|
|
});
|
2023-07-11 03:02:44 +03:00
|
|
|
}
|
2023-07-03 21:17:49 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Draw title text.
|
|
|
|
fn title_text_ui(&self, builder: StripBuilder) {
|
|
|
|
builder
|
|
|
|
.size(Size::remainder())
|
2023-07-11 23:37:17 +03:00
|
|
|
.size(Size::exact(28.0))
|
2023-07-03 21:17:49 +03:00
|
|
|
.vertical(|mut strip| {
|
|
|
|
strip.cell(|ui| {
|
2023-07-11 23:37:17 +03:00
|
|
|
ui.add_space(4.0);
|
2023-07-03 21:17:49 +03:00
|
|
|
ui.vertical_centered(|ui| {
|
2023-07-13 03:54:27 +03:00
|
|
|
ui.label(RichText::new(self.current_tab.get_type().title().to_uppercase())
|
2023-07-11 23:37:17 +03:00
|
|
|
.size(19.0)
|
2023-07-03 21:17:49 +03:00
|
|
|
.color(Colors::TITLE));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
strip.cell(|ui| {
|
|
|
|
ui.centered_and_justified(|ui| {
|
|
|
|
let sync_status = Node::get_sync_status();
|
|
|
|
|
|
|
|
// Setup text color animation based on sync status
|
|
|
|
let idle = match sync_status {
|
|
|
|
None => !Node::is_starting(),
|
|
|
|
Some(ss) => ss == SyncStatus::NoSync
|
|
|
|
};
|
|
|
|
let (dark, bright) = (0.3, 1.0);
|
|
|
|
let color_factor = if !idle {
|
2023-07-07 03:50:08 +03:00
|
|
|
lerp(dark..=bright, ui.input(|i| i.time).cos().abs()) as f32
|
2023-07-03 21:17:49 +03:00
|
|
|
} else {
|
|
|
|
bright as f32
|
|
|
|
};
|
|
|
|
|
2023-07-11 03:02:44 +03:00
|
|
|
// Draw sync status text.
|
2023-07-03 21:17:49 +03:00
|
|
|
let status_color_rgba = Rgba::from(Colors::TEXT) * color_factor;
|
|
|
|
let status_color = Color32::from(status_color_rgba);
|
|
|
|
View::ellipsize_text(ui, Node::get_sync_status_text(), 15.0, status_color);
|
|
|
|
|
2023-07-11 03:02:44 +03:00
|
|
|
// Repaint delay based on sync status.
|
2023-07-03 21:17:49 +03:00
|
|
|
if idle {
|
2023-07-11 03:02:44 +03:00
|
|
|
ui.ctx().request_repaint_after(Node::STATS_UPDATE_DELAY);
|
2023-07-03 21:17:49 +03:00
|
|
|
} else {
|
|
|
|
ui.ctx().request_repaint();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Content to draw when node is disabled.
|
|
|
|
pub fn disabled_node_ui(ui: &mut egui::Ui) {
|
|
|
|
View::center_content(ui, 162.0, |ui| {
|
|
|
|
let text = t!("network.disabled_server", "dots" => DOTS_THREE_OUTLINE_VERTICAL);
|
|
|
|
ui.label(RichText::new(text)
|
|
|
|
.size(16.0)
|
|
|
|
.color(Colors::INACTIVE_TEXT)
|
|
|
|
);
|
|
|
|
ui.add_space(10.0);
|
2023-07-11 03:02:44 +03:00
|
|
|
View::button(ui, format!("{} {}", POWER, t!("network.enable_node")), Colors::GOLD, || {
|
2023-07-03 21:17:49 +03:00
|
|
|
Node::start();
|
|
|
|
});
|
|
|
|
ui.add_space(2.0);
|
|
|
|
Self::autorun_node_ui(ui);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-13 03:54:27 +03:00
|
|
|
/// Draw checkbox to run integrated node on application launch.
|
2023-07-03 21:17:49 +03:00
|
|
|
pub fn autorun_node_ui(ui: &mut egui::Ui) {
|
|
|
|
let autostart = AppConfig::autostart_node();
|
|
|
|
View::checkbox(ui, autostart, t!("network.autorun"), || {
|
|
|
|
AppConfig::toggle_node_autostart();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|