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.
|
|
|
|
|
2023-07-04 00:39:13 +03:00
|
|
|
use egui::{RichText, ScrollArea};
|
2024-05-17 21:37:29 +03:00
|
|
|
use egui::scroll_area::ScrollBarVisibility;
|
2023-07-04 00:39:13 +03:00
|
|
|
|
2023-07-13 03:54:27 +03:00
|
|
|
use crate::gui::Colors;
|
2023-07-04 00:39:13 +03:00
|
|
|
use crate::gui::icons::ARROW_COUNTER_CLOCKWISE;
|
|
|
|
use crate::gui::platform::PlatformCallbacks;
|
2024-04-22 17:37:58 +03:00
|
|
|
use crate::gui::views::{Modal, Root, View};
|
2023-07-14 00:55:31 +03:00
|
|
|
use crate::gui::views::network::setup::{DandelionSetup, NodeSetup, P2PSetup, PoolSetup, StratumSetup};
|
2023-08-03 04:11:25 +03:00
|
|
|
use crate::gui::views::network::types::{NetworkTab, NetworkTabType};
|
|
|
|
use crate::gui::views::types::{ModalContainer, ModalPosition};
|
2023-07-04 00:39:13 +03:00
|
|
|
use crate::node::{Node, NodeConfig};
|
|
|
|
|
2023-07-11 03:02:44 +03:00
|
|
|
/// Integrated node settings tab content.
|
2023-07-04 00:39:13 +03:00
|
|
|
pub struct NetworkSettings {
|
2023-08-03 04:11:25 +03:00
|
|
|
/// Integrated node general setup content.
|
2023-07-05 16:06:14 +03:00
|
|
|
node: NodeSetup,
|
2023-08-03 04:11:25 +03:00
|
|
|
/// P2P server setup content.
|
2023-07-11 03:02:44 +03:00
|
|
|
p2p: P2PSetup,
|
2023-08-03 04:11:25 +03:00
|
|
|
/// Stratum server setup content.
|
2023-07-05 16:06:14 +03:00
|
|
|
stratum: StratumSetup,
|
2023-08-03 04:11:25 +03:00
|
|
|
/// Pool setup content.
|
2023-07-05 16:06:14 +03:00
|
|
|
pool: PoolSetup,
|
2023-08-03 04:11:25 +03:00
|
|
|
/// Dandelion server setup content.
|
|
|
|
dandelion: DandelionSetup,
|
|
|
|
|
|
|
|
/// [`Modal`] identifiers allowed at this ui container.
|
|
|
|
modal_ids: Vec<&'static str>
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Identifier for settings reset confirmation [`Modal`].
|
|
|
|
pub const RESET_SETTINGS_MODAL: &'static str = "reset_settings";
|
|
|
|
|
|
|
|
impl Default for NetworkSettings {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
node: NodeSetup::default(),
|
|
|
|
p2p: P2PSetup::default(),
|
|
|
|
stratum: StratumSetup::default(),
|
|
|
|
pool: PoolSetup::default(),
|
|
|
|
dandelion: DandelionSetup::default(),
|
|
|
|
modal_ids: vec![
|
|
|
|
RESET_SETTINGS_MODAL
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ModalContainer for NetworkSettings {
|
|
|
|
fn modal_ids(&self) -> &Vec<&'static str> {
|
|
|
|
&self.modal_ids
|
|
|
|
}
|
|
|
|
|
|
|
|
fn modal_ui(&mut self,
|
|
|
|
ui: &mut egui::Ui,
|
|
|
|
modal: &Modal,
|
|
|
|
_: &dyn PlatformCallbacks) {
|
|
|
|
match modal.id {
|
|
|
|
RESET_SETTINGS_MODAL => reset_settings_confirmation_modal(ui, modal),
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2023-07-04 00:39:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl NetworkTab for NetworkSettings {
|
|
|
|
fn get_type(&self) -> NetworkTabType {
|
|
|
|
NetworkTabType::Settings
|
|
|
|
}
|
|
|
|
|
2024-06-20 22:52:12 +03:00
|
|
|
fn ui(&mut self, ui: &mut egui::Ui, cb: &dyn PlatformCallbacks) {
|
2023-08-03 04:11:25 +03:00
|
|
|
// Draw modal content for current ui container.
|
2024-06-20 22:52:12 +03:00
|
|
|
self.current_modal_ui(ui, cb);
|
2023-08-03 04:11:25 +03:00
|
|
|
|
2023-07-04 00:39:13 +03:00
|
|
|
ScrollArea::vertical()
|
|
|
|
.id_source("network_settings")
|
2024-05-17 21:37:29 +03:00
|
|
|
.scroll_bar_visibility(ScrollBarVisibility::AlwaysHidden)
|
2023-07-04 00:39:13 +03:00
|
|
|
.auto_shrink([false; 2])
|
|
|
|
.show(ui, |ui| {
|
2023-08-11 01:02:08 +03:00
|
|
|
ui.add_space(1.0);
|
2024-04-22 17:37:58 +03:00
|
|
|
ui.vertical_centered(|ui| {
|
|
|
|
View::max_width_ui(ui, Root::SIDE_PANEL_WIDTH * 1.3, |ui| {
|
|
|
|
// Draw node setup section.
|
2024-06-20 22:52:12 +03:00
|
|
|
self.node.ui(ui, cb);
|
2023-08-11 01:02:08 +03:00
|
|
|
|
2024-04-22 17:37:58 +03:00
|
|
|
ui.add_space(6.0);
|
2024-05-29 22:47:17 +03:00
|
|
|
View::horizontal_line(ui, Colors::stroke());
|
2024-04-22 17:37:58 +03:00
|
|
|
ui.add_space(4.0);
|
2023-07-05 16:06:14 +03:00
|
|
|
|
2024-04-22 17:37:58 +03:00
|
|
|
// Draw P2P server setup section.
|
2024-06-20 22:52:12 +03:00
|
|
|
self.p2p.ui(ui, cb);
|
2023-07-05 16:06:14 +03:00
|
|
|
|
2024-04-22 17:37:58 +03:00
|
|
|
ui.add_space(6.0);
|
2024-05-29 22:47:17 +03:00
|
|
|
View::horizontal_line(ui, Colors::stroke());
|
2024-04-22 17:37:58 +03:00
|
|
|
ui.add_space(4.0);
|
2023-07-11 03:02:44 +03:00
|
|
|
|
2024-04-22 17:37:58 +03:00
|
|
|
// Draw Stratum server setup section.
|
2024-06-20 22:52:12 +03:00
|
|
|
self.stratum.ui(ui, cb);
|
2023-07-11 03:02:44 +03:00
|
|
|
|
2024-04-22 17:37:58 +03:00
|
|
|
ui.add_space(6.0);
|
2024-05-29 22:47:17 +03:00
|
|
|
View::horizontal_line(ui, Colors::stroke());
|
2024-04-22 17:37:58 +03:00
|
|
|
ui.add_space(4.0);
|
2023-07-05 16:06:14 +03:00
|
|
|
|
2024-04-22 17:37:58 +03:00
|
|
|
// Draw pool setup section.
|
2024-06-20 22:52:12 +03:00
|
|
|
self.pool.ui(ui, cb);
|
2023-07-05 16:06:14 +03:00
|
|
|
|
2024-04-22 17:37:58 +03:00
|
|
|
ui.add_space(6.0);
|
2024-05-29 22:47:17 +03:00
|
|
|
View::horizontal_line(ui, Colors::stroke());
|
2024-04-22 17:37:58 +03:00
|
|
|
ui.add_space(4.0);
|
2023-07-05 16:06:14 +03:00
|
|
|
|
2024-04-22 17:37:58 +03:00
|
|
|
// Draw Dandelion server setup section.
|
2024-06-20 22:52:12 +03:00
|
|
|
self.dandelion.ui(ui, cb);
|
2023-07-05 16:06:14 +03:00
|
|
|
|
2024-04-22 17:37:58 +03:00
|
|
|
ui.add_space(6.0);
|
2024-05-29 22:47:17 +03:00
|
|
|
View::horizontal_line(ui, Colors::stroke());
|
2024-04-22 17:37:58 +03:00
|
|
|
ui.add_space(6.0);
|
2023-07-05 16:06:14 +03:00
|
|
|
|
2024-04-22 17:37:58 +03:00
|
|
|
// Draw reset settings content.
|
|
|
|
reset_settings_ui(ui);
|
|
|
|
});
|
|
|
|
});
|
2023-07-04 00:39:13 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NetworkSettings {
|
|
|
|
/// Reminder to restart enabled node to show on edit setting at [`Modal`].
|
|
|
|
pub fn node_restart_required_ui(ui: &mut egui::Ui) {
|
|
|
|
if Node::is_running() {
|
|
|
|
ui.add_space(12.0);
|
|
|
|
ui.label(RichText::new(t!("network_settings.restart_node_required"))
|
|
|
|
.size(16.0)
|
2024-05-29 22:47:17 +03:00
|
|
|
.color(Colors::green())
|
2023-07-04 00:39:13 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Draw IP addresses as radio buttons.
|
|
|
|
pub fn ip_addrs_ui(ui: &mut egui::Ui,
|
|
|
|
saved_ip: &String,
|
2023-07-05 16:06:14 +03:00
|
|
|
ips: &Vec<String>,
|
2023-07-04 00:39:13 +03:00
|
|
|
on_change: impl FnOnce(&String)) {
|
2023-07-05 16:06:14 +03:00
|
|
|
let mut selected_ip = saved_ip;
|
2023-07-04 00:39:13 +03:00
|
|
|
|
|
|
|
// Set first IP address as current if saved is not present at system.
|
2023-07-05 16:06:14 +03:00
|
|
|
if !ips.contains(saved_ip) {
|
|
|
|
selected_ip = ips.get(0).unwrap();
|
2023-07-04 00:39:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ui.add_space(2.0);
|
2023-07-05 16:06:14 +03:00
|
|
|
|
2023-07-04 00:39:13 +03:00
|
|
|
// Show available IP addresses on the system.
|
2023-07-05 16:06:14 +03:00
|
|
|
let _ = ips.chunks(2).map(|x| {
|
2023-07-04 00:39:13 +03:00
|
|
|
if x.len() == 2 {
|
|
|
|
ui.columns(2, |columns| {
|
2023-07-05 16:06:14 +03:00
|
|
|
let ip_left = x.get(0).unwrap();
|
2023-07-04 00:39:13 +03:00
|
|
|
columns[0].vertical_centered(|ui| {
|
2023-07-05 16:06:14 +03:00
|
|
|
View::radio_value(ui, &mut selected_ip, ip_left, ip_left.to_string());
|
2023-07-04 00:39:13 +03:00
|
|
|
});
|
2023-07-05 16:06:14 +03:00
|
|
|
let ip_right = x.get(1).unwrap();
|
2023-07-04 00:39:13 +03:00
|
|
|
columns[1].vertical_centered(|ui| {
|
2023-07-05 16:06:14 +03:00
|
|
|
View::radio_value(ui, &mut selected_ip, ip_right, ip_right.to_string());
|
2023-07-04 00:39:13 +03:00
|
|
|
})
|
|
|
|
});
|
|
|
|
} else {
|
2023-07-05 16:06:14 +03:00
|
|
|
let ip = x.get(0).unwrap();
|
|
|
|
View::radio_value(ui, &mut selected_ip, ip, ip.to_string());
|
2023-07-04 00:39:13 +03:00
|
|
|
}
|
|
|
|
ui.add_space(12.0);
|
|
|
|
}).collect::<Vec<_>>();
|
|
|
|
|
2023-07-05 16:06:14 +03:00
|
|
|
if saved_ip != selected_ip {
|
|
|
|
(on_change)(&selected_ip.to_string());
|
2023-07-04 00:39:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Show message when IP addresses are not available at system.
|
|
|
|
pub fn no_ip_address_ui(ui: &mut egui::Ui) {
|
|
|
|
ui.vertical_centered(|ui| {
|
|
|
|
ui.label(RichText::new(t!("network.no_ips"))
|
|
|
|
.size(16.0)
|
2024-05-29 22:47:17 +03:00
|
|
|
.color(Colors::inactive_text())
|
2023-07-04 00:39:13 +03:00
|
|
|
);
|
|
|
|
ui.add_space(6.0);
|
|
|
|
});
|
|
|
|
}
|
2023-08-03 04:11:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Draw button to reset integrated node settings to default values.
|
|
|
|
fn reset_settings_ui(ui: &mut egui::Ui) {
|
|
|
|
ui.vertical_centered(|ui| {
|
|
|
|
ui.label(RichText::new(t!("network_settings.reset_settings_desc"))
|
|
|
|
.size(16.0)
|
2024-05-29 22:47:17 +03:00
|
|
|
.color(Colors::text(false)));
|
2023-08-03 04:11:25 +03:00
|
|
|
ui.add_space(8.0);
|
|
|
|
let button_text = format!("{} {}",
|
|
|
|
ARROW_COUNTER_CLOCKWISE,
|
|
|
|
t!("network_settings.reset_settings"));
|
2024-05-29 22:47:17 +03:00
|
|
|
View::action_button(ui, button_text, || {
|
2023-08-03 04:11:25 +03:00
|
|
|
// Show modal to confirm settings reset.
|
|
|
|
Modal::new(RESET_SETTINGS_MODAL)
|
|
|
|
.position(ModalPosition::Center)
|
|
|
|
.title(t!("modal.confirmation"))
|
|
|
|
.show();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Show reminder to restart enabled node.
|
|
|
|
if Node::is_running() {
|
|
|
|
ui.add_space(12.0);
|
|
|
|
ui.label(RichText::new(t!("network_settings.restart_node_required"))
|
|
|
|
.size(16.0)
|
2024-05-29 22:47:17 +03:00
|
|
|
.color(Colors::gray())
|
2023-08-03 04:11:25 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
ui.add_space(12.0);
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Confirmation to reset settings to default values.
|
|
|
|
fn reset_settings_confirmation_modal(ui: &mut egui::Ui, modal: &Modal) {
|
|
|
|
ui.add_space(6.0);
|
|
|
|
ui.vertical_centered(|ui| {
|
|
|
|
let reset_text = format!("{}?", t!("network_settings.reset_settings_desc"));
|
|
|
|
ui.label(RichText::new(reset_text)
|
|
|
|
.size(17.0)
|
2024-05-29 22:47:17 +03:00
|
|
|
.color(Colors::text(false)));
|
2023-08-03 04:11:25 +03:00
|
|
|
ui.add_space(8.0);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Show modal buttons.
|
|
|
|
ui.scope(|ui| {
|
|
|
|
// Setup spacing between buttons.
|
2024-05-29 22:47:17 +03:00
|
|
|
ui.spacing_mut().item_spacing = egui::Vec2::new(8.0, 0.0);
|
2023-08-03 04:11:25 +03:00
|
|
|
|
|
|
|
ui.columns(2, |columns| {
|
|
|
|
columns[0].vertical_centered_justified(|ui| {
|
2024-05-29 22:47:17 +03:00
|
|
|
View::button(ui, t!("network_settings.reset"), Colors::white_or_black(false), || {
|
2023-08-03 04:11:25 +03:00
|
|
|
NodeConfig::reset_to_default();
|
|
|
|
modal.close();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
columns[1].vertical_centered_justified(|ui| {
|
2024-05-29 22:47:17 +03:00
|
|
|
View::button(ui, t!("modal.cancel"), Colors::white_or_black(false), || {
|
2023-08-03 04:11:25 +03:00
|
|
|
modal.close();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
ui.add_space(6.0);
|
|
|
|
});
|
2023-07-04 00:39:13 +03:00
|
|
|
}
|