ui: wallet connection selector/creator modal, separate modal content for connection creation, move settings into wallet

This commit is contained in:
ardocrat
2024-07-10 18:54:23 +03:00
parent 8d66087d68
commit f142ae110a
21 changed files with 514 additions and 359 deletions
+2 -1
View File
@@ -24,8 +24,9 @@ use crate::gui::icons::{ARROWS_CLOCKWISE, BRIDGE, CHAT_CIRCLE_TEXT, CHECK, CHECK
use crate::gui::platform::PlatformCallbacks;
use crate::gui::views::{CameraContent, Modal, Content, View};
use crate::gui::views::types::{ModalPosition, QrScanResult, TextEditOptions};
use crate::gui::views::wallets::{WalletTransactions, WalletMessages, WalletTransport, WalletSettings};
use crate::gui::views::wallets::{WalletTransactions, WalletMessages, WalletTransport};
use crate::gui::views::wallets::types::{GRIN, WalletTab, WalletTabType};
use crate::gui::views::wallets::settings::WalletSettings;
use crate::node::Node;
use crate::wallet::{Wallet, WalletConfig};
use crate::wallet::types::{WalletAccount, WalletData};
+1 -3
View File
@@ -13,6 +13,7 @@
// limitations under the License.
pub mod types;
pub mod settings;
mod txs;
pub use txs::WalletTransactions;
@@ -23,8 +24,5 @@ pub use messages::WalletMessages;
mod transport;
pub use transport::WalletTransport;
mod settings;
pub use settings::WalletSettings;
mod content;
pub use content::WalletContent;
@@ -0,0 +1,381 @@
// 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, RichText};
use crate::gui::Colors;
use crate::gui::icons::{CLOCK_COUNTDOWN, PASSWORD, PENCIL};
use crate::gui::platform::PlatformCallbacks;
use crate::gui::views::{Modal, View};
use crate::gui::views::types::{ModalPosition, TextEditOptions};
use crate::wallet::Wallet;
/// Common wallet settings content.
pub struct CommonSettings {
/// Wallet name [`Modal`] value.
name_edit: String,
/// Flag to check if password change [`Modal`] was opened at first time to focus input field.
first_edit_pass_opening: bool,
/// Flag to check if wrong password was entered.
wrong_pass: bool,
/// Current wallet password [`Modal`] value.
old_pass_edit: String,
/// New wallet password [`Modal`] value.
new_pass_edit: String,
/// Minimum confirmations number value.
min_confirmations_edit: String
}
/// Identifier for wallet name [`Modal`].
const NAME_EDIT_MODAL: &'static str = "wallet_name_edit_modal";
/// Identifier for wallet password [`Modal`].
const PASS_EDIT_MODAL: &'static str = "wallet_pass_edit_modal";
/// Identifier for minimum confirmations [`Modal`].
const MIN_CONFIRMATIONS_EDIT_MODAL: &'static str = "wallet_min_conf_edit_modal";
impl Default for CommonSettings {
fn default() -> Self {
Self {
name_edit: "".to_string(),
first_edit_pass_opening: true,
wrong_pass: false,
old_pass_edit: "".to_string(),
new_pass_edit: "".to_string(),
min_confirmations_edit: "".to_string()
}
}
}
impl CommonSettings {
pub fn ui(&mut self, ui: &mut egui::Ui, wallet: &mut Wallet, cb: &dyn PlatformCallbacks) {
// Show modal content for this ui container.
self.modal_content_ui(ui, wallet, cb);
ui.vertical_centered(|ui| {
let wallet_name = wallet.get_config().name;
// Show wallet name.
ui.add_space(2.0);
ui.label(RichText::new(t!("wallets.name"))
.size(16.0)
.color(Colors::gray()));
ui.add_space(2.0);
ui.label(RichText::new(wallet_name.clone())
.size(16.0)
.color(Colors::white_or_black(true)));
ui.add_space(8.0);
// Show wallet name setup.
let name_text = format!("{} {}", PENCIL, t!("change"));
View::button(ui, name_text, Colors::button(), || {
self.name_edit = wallet_name;
// Show wallet name modal.
Modal::new(NAME_EDIT_MODAL)
.position(ModalPosition::CenterTop)
.title(t!("wallets.wallet"))
.show();
cb.show_keyboard();
});
ui.add_space(12.0);
View::horizontal_line(ui, Colors::item_stroke());
ui.add_space(6.0);
ui.label(RichText::new(t!("wallets.pass")).size(16.0).color(Colors::gray()));
ui.add_space(6.0);
// Show wallet password setup.
let pass_text = format!("{} {}", PASSWORD, t!("change"));
View::button(ui, pass_text, Colors::button(), || {
// Setup modal values.
self.first_edit_pass_opening = true;
self.old_pass_edit = "".to_string();
self.new_pass_edit = "".to_string();
self.wrong_pass = false;
// Show wallet password modal.
Modal::new(PASS_EDIT_MODAL)
.position(ModalPosition::CenterTop)
.title(t!("wallets.wallet"))
.show();
cb.show_keyboard();
});
ui.add_space(12.0);
View::horizontal_line(ui, Colors::item_stroke());
ui.add_space(6.0);
ui.label(RichText::new(t!("wallets.min_tx_conf_count")).size(16.0).color(Colors::gray()));
ui.add_space(6.0);
// Show minimum amount of confirmations value setup.
let min_confirmations = wallet.get_config().min_confirmations;
let min_conf_text = format!("{} {}", CLOCK_COUNTDOWN, min_confirmations);
View::button(ui, min_conf_text, Colors::button(), || {
self.min_confirmations_edit = min_confirmations.to_string();
// Show minimum amount of confirmations value modal.
Modal::new(MIN_CONFIRMATIONS_EDIT_MODAL)
.position(ModalPosition::CenterTop)
.title(t!("network_settings.change_value"))
.show();
cb.show_keyboard();
});
ui.add_space(12.0);
View::horizontal_line(ui, Colors::stroke());
ui.add_space(4.0);
});
}
/// Draw [`Modal`] content for this ui container.
fn modal_content_ui(&mut self,
ui: &mut egui::Ui,
wallet: &mut Wallet,
cb: &dyn PlatformCallbacks) {
match Modal::opened() {
None => {}
Some(id) => {
match id {
NAME_EDIT_MODAL => {
Modal::ui(ui.ctx(), |ui, modal| {
self.name_modal_ui(ui, wallet, modal, cb);
});
}
PASS_EDIT_MODAL => {
Modal::ui(ui.ctx(), |ui, modal| {
self.pass_modal_ui(ui, wallet, modal, cb);
});
}
MIN_CONFIRMATIONS_EDIT_MODAL => {
Modal::ui(ui.ctx(), |ui, modal| {
self.min_conf_modal_ui(ui, wallet, modal, cb);
});
}
_ => {}
}
}
}
}
/// Draw wallet name [`Modal`] content.
fn name_modal_ui(&mut self,
ui: &mut egui::Ui,
wallet: &mut Wallet,
modal: &Modal,
cb: &dyn PlatformCallbacks) {
ui.add_space(6.0);
ui.vertical_centered(|ui| {
ui.label(RichText::new(t!("wallets.name"))
.size(17.0)
.color(Colors::gray()));
ui.add_space(8.0);
// Show wallet name text edit.
let name_edit_id = Id::from(modal.id).with(wallet.get_config().id);
let mut name_edit_opts = TextEditOptions::new(name_edit_id);
View::text_edit(ui, cb, &mut self.name_edit, &mut name_edit_opts);
ui.add_space(12.0);
});
// Show modal buttons.
ui.scope(|ui| {
// Setup spacing between buttons.
ui.spacing_mut().item_spacing = egui::Vec2::new(8.0, 0.0);
ui.columns(2, |columns| {
columns[0].vertical_centered_justified(|ui| {
View::button(ui, t!("modal.cancel"), Colors::white_or_black(false), || {
// Close modal.
cb.hide_keyboard();
modal.close();
});
});
columns[1].vertical_centered_justified(|ui| {
// Save button callback.
let on_save = || {
if !self.name_edit.is_empty() {
wallet.change_name(self.name_edit.clone());
cb.hide_keyboard();
modal.close();
}
};
View::on_enter_key(ui, || {
(on_save)();
});
View::button(ui, t!("modal.save"), Colors::white_or_black(false), on_save);
});
});
ui.add_space(6.0);
});
}
/// Draw wallet pass [`Modal`] content.
fn pass_modal_ui(&mut self,
ui: &mut egui::Ui,
wallet: &mut Wallet,
modal: &Modal,
cb: &dyn PlatformCallbacks) {
let wallet_id = wallet.get_config().id;
ui.add_space(6.0);
ui.vertical_centered(|ui| {
ui.label(RichText::new(t!("wallets.current_pass"))
.size(17.0)
.color(Colors::gray()));
ui.add_space(8.0);
// Draw old password text edit.
let pass_edit_id = Id::from(modal.id).with(wallet_id).with("old_pass");
let mut pass_edit_opts = TextEditOptions::new(pass_edit_id).password().no_focus();
if self.first_edit_pass_opening {
self.first_edit_pass_opening = false;
pass_edit_opts.focus = true;
}
View::text_edit(ui, cb, &mut self.old_pass_edit, &mut pass_edit_opts);
ui.add_space(8.0);
ui.label(RichText::new(t!("wallets.new_pass"))
.size(17.0)
.color(Colors::gray()));
ui.add_space(8.0);
// Draw new password text edit.
let new_pass_edit_id = Id::from(modal.id).with(wallet_id).with("new_pass");
let mut new_pass_edit_opts = TextEditOptions::new(new_pass_edit_id)
.password()
.no_focus();
View::text_edit(ui, cb, &mut self.new_pass_edit, &mut new_pass_edit_opts);
// Show information when password is empty.
if self.old_pass_edit.is_empty() || self.new_pass_edit.is_empty() {
ui.add_space(10.0);
ui.label(RichText::new(t!("wallets.pass_empty"))
.size(17.0)
.color(Colors::inactive_text()));
} else if self.wrong_pass {
ui.add_space(10.0);
ui.label(RichText::new(t!("wallets.wrong_pass"))
.size(17.0)
.color(Colors::red()));
}
ui.add_space(12.0);
});
// Show modal buttons.
ui.scope(|ui| {
// Setup spacing between buttons.
ui.spacing_mut().item_spacing = egui::Vec2::new(8.0, 0.0);
ui.columns(2, |columns| {
columns[0].vertical_centered_justified(|ui| {
View::button(ui, t!("modal.cancel"), Colors::white_or_black(false), || {
// Close modal.
cb.hide_keyboard();
modal.close();
});
});
columns[1].vertical_centered_justified(|ui| {
// Callback for button to continue.
let mut on_continue = || {
if self.new_pass_edit.is_empty() {
return;
}
let old_pass = self.old_pass_edit.clone();
let new_pass = self.new_pass_edit.clone();
match wallet.change_password(old_pass, new_pass) {
Ok(_) => {
// Clear password values.
self.old_pass_edit = "".to_string();
self.new_pass_edit = "".to_string();
// Close modal.
cb.hide_keyboard();
modal.close();
}
Err(_) => self.wrong_pass = true
}
};
// Continue on Enter key press.
View::on_enter_key(ui, || {
(on_continue)();
});
View::button(ui, t!("change"), Colors::white_or_black(false), on_continue);
});
});
ui.add_space(6.0);
});
}
/// Draw wallet name [`Modal`] content.
fn min_conf_modal_ui(&mut self,
ui: &mut egui::Ui,
wallet: &mut Wallet,
modal: &Modal,
cb: &dyn PlatformCallbacks) {
ui.add_space(6.0);
ui.vertical_centered(|ui| {
ui.label(RichText::new(t!("wallets.min_tx_conf_count"))
.size(17.0)
.color(Colors::gray()));
ui.add_space(8.0);
// Minimum amount of confirmations text edit.
let mut text_edit_opts = TextEditOptions::new(Id::from(modal.id)).h_center();
View::text_edit(ui, cb, &mut self.min_confirmations_edit, &mut text_edit_opts);
// Show error when specified value is not valid or reminder to restart enabled node.
if self.min_confirmations_edit.parse::<u64>().is_err() {
ui.add_space(12.0);
ui.label(RichText::new(t!("network_settings.not_valid_value"))
.size(17.0)
.color(Colors::red()));
}
ui.add_space(12.0);
});
// Show modal buttons.
ui.scope(|ui| {
// Setup spacing between buttons.
ui.spacing_mut().item_spacing = egui::Vec2::new(8.0, 0.0);
ui.columns(2, |columns| {
columns[0].vertical_centered_justified(|ui| {
View::button(ui, t!("modal.cancel"), Colors::white_or_black(false), || {
// Close modal.
cb.hide_keyboard();
modal.close();
});
});
columns[1].vertical_centered_justified(|ui| {
// Save button callback.
let on_save = || {
if let Ok(min_conf) = self.min_confirmations_edit.parse::<u64>() {
wallet.update_min_confirmations(min_conf);
cb.hide_keyboard();
modal.close();
}
};
View::on_enter_key(ui, || {
(on_save)();
});
View::button(ui, t!("modal.save"), Colors::white_or_black(false), on_save);
});
});
ui.add_space(6.0);
});
}
}
@@ -0,0 +1,262 @@
// 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::{Align, Layout, RichText};
use crate::gui::Colors;
use crate::gui::icons::{CHECK, CHECK_CIRCLE, CHECK_FAT, DOTS_THREE_CIRCLE, GLOBE, GLOBE_SIMPLE, PLUS_CIRCLE, X_CIRCLE};
use crate::gui::platform::PlatformCallbacks;
use crate::gui::views::{ConnectionsContent, Modal, View};
use crate::gui::views::modals::ExternalConnectionModal;
use crate::gui::views::types::{ModalContainer, ModalPosition};
use crate::wallet::{ConnectionsConfig, ExternalConnection, Wallet};
use crate::wallet::types::ConnectionMethod;
/// Wallet connection settings content.
pub struct ConnectionSettings {
/// Selected connection method.
pub method: ConnectionMethod,
/// Current wallet external connection.
curr_ext_conn: Option<ExternalConnection>,
/// External connection [`Modal`] content.
ext_conn_modal: ExternalConnectionModal,
/// [`Modal`] identifiers allowed at this ui container.
modal_ids: Vec<&'static str>
}
impl Default for ConnectionSettings {
fn default() -> Self {
ExternalConnection::check_ext_conn_availability(None);
Self {
method: ConnectionMethod::Integrated,
curr_ext_conn: None,
ext_conn_modal: ExternalConnectionModal::new(None),
modal_ids: vec![
ExternalConnectionModal::WALLET_ID
]
}
}
}
impl ModalContainer for ConnectionSettings {
fn modal_ids(&self) -> &Vec<&'static str> {
&self.modal_ids
}
fn modal_ui(&mut self,
ui: &mut egui::Ui,
modal: &Modal,
cb: &dyn PlatformCallbacks) {
match modal.id {
ExternalConnectionModal::WALLET_ID => {
self.ext_conn_modal.ui(ui, cb, modal, |_| {});
},
_ => {}
}
}
}
impl ConnectionSettings {
/// Draw wallet creation setup content.
pub fn create_ui(&mut self, ui: &mut egui::Ui, cb: &dyn PlatformCallbacks) {
self.ui(ui, None, cb);
}
/// Draw existing wallet connection setup content.
pub fn wallet_ui(&mut self, ui: &mut egui::Ui, w: &mut Wallet, cb: &dyn PlatformCallbacks) {
// Setup connection value from provided wallet.
match w.get_config().ext_conn_id {
None => self.method = ConnectionMethod::Integrated,
Some(id) => self.method = ConnectionMethod::External(id)
}
// Draw setup content.
self.ui(ui, Some(w), cb);
// Setup wallet connection value after change.
let changed = match self.method {
ConnectionMethod::Integrated => {
let changed = w.get_current_ext_conn().is_some();
if changed {
w.update_ext_conn_id(None);
}
changed
}
ConnectionMethod::External(id) => {
let changed = w.get_config().ext_conn_id != Some(id);
if changed {
w.update_ext_conn_id(Some(id));
}
changed
}
};
// Reopen wallet if connection changed.
if changed {
if !w.reopen_needed() {
w.set_reopen(true);
w.close();
}
}
}
/// Draw connection setup content.
fn ui(&mut self,
ui: &mut egui::Ui,
wallet: Option<&Wallet>,
cb: &dyn PlatformCallbacks) {
// Draw modal content for current ui container.
self.current_modal_ui(ui, cb);
ui.add_space(2.0);
View::sub_title(ui, format!("{} {}", GLOBE, t!("wallets.conn_method")));
View::horizontal_line(ui, Colors::stroke());
ui.add_space(6.0);
ui.vertical_centered(|ui| {
// Show integrated node selection.
ui.add_space(6.0);
ConnectionsContent::integrated_node_item_ui(ui, |ui| {
// Draw button to select integrated node if it was not selected.
let is_current_method = self.method == ConnectionMethod::Integrated;
if !is_current_method {
View::item_button(ui, View::item_rounding(0, 1, true), CHECK, None, || {
self.method = ConnectionMethod::Integrated;
});
} else {
ui.add_space(14.0);
ui.label(RichText::new(CHECK_FAT).size(20.0).color(Colors::green()));
ui.add_space(14.0);
}
});
// Show external connections.
ui.add_space(8.0);
ui.label(RichText::new(t!("wallets.ext_conn")).size(16.0).color(Colors::gray()));
ui.add_space(6.0);
// Show button to add new external node connection.
let add_node_text = format!("{} {}", PLUS_CIRCLE, t!("wallets.add_node"));
View::button(ui, add_node_text, Colors::button(), || {
self.show_add_ext_conn_modal(cb);
});
ui.add_space(4.0);
let mut ext_conn_list = ConnectionsConfig::ext_conn_list();
// Check if current external connection was deleted to show at 1st place.
if let Some(wallet) = wallet {
if let Some(conn) = wallet.get_current_ext_conn() {
if ext_conn_list.iter()
.filter(|c| c.id == conn.id)
.collect::<Vec<&ExternalConnection>>().is_empty() {
if self.curr_ext_conn.is_none() {
self.curr_ext_conn = Some(conn);
}
ext_conn_list.insert(0, self.curr_ext_conn.as_ref().unwrap().clone());
}
}
}
if !ext_conn_list.is_empty() {
ui.add_space(8.0);
for (index, conn) in ext_conn_list.iter().enumerate() {
ui.horizontal_wrapped(|ui| {
// Draw external connection item.
self.ext_conn_item_ui(ui, wallet, conn, index, ext_conn_list.len());
});
}
}
});
}
/// Draw external connection item content.
fn ext_conn_item_ui(&mut self,
ui: &mut egui::Ui,
wallet: Option<&Wallet>,
conn: &ExternalConnection,
index: usize,
len: usize) {
// Setup layout size.
let mut rect = ui.available_rect_before_wrap();
rect.set_height(52.0);
// Draw round background.
let bg_rect = rect.clone();
let item_rounding = View::item_rounding(index, len, false);
ui.painter().rect(bg_rect, item_rounding, Colors::fill(), View::item_stroke());
ui.vertical(|ui| {
ui.allocate_ui_with_layout(rect.size(), Layout::right_to_left(Align::Center), |ui| {
// Draw button to select connection.
let is_current_method = if let Some(wallet) = wallet {
if let Some(cur) = wallet.get_config().ext_conn_id {
cur == conn.id
} else {
false
}
} else {
self.method == ConnectionMethod::External(conn.id)
};
if !is_current_method {
let button_rounding = View::item_rounding(index, len, true);
View::item_button(ui, button_rounding, CHECK, None, || {
self.method = ConnectionMethod::External(conn.id);
});
} else {
ui.add_space(12.0);
ui.label(RichText::new(CHECK_FAT).size(20.0).color(Colors::green()));
}
let layout_size = ui.available_size();
ui.allocate_ui_with_layout(layout_size, Layout::left_to_right(Align::Center), |ui| {
ui.add_space(6.0);
ui.vertical(|ui| {
// Draw connections URL.
ui.add_space(4.0);
let conn_text = format!("{} {}", GLOBE_SIMPLE, conn.url);
View::ellipsize_text(ui, conn_text, 15.0, Colors::title(false));
ui.add_space(1.0);
// Setup connection status text.
let status_text = if let Some(available) = conn.available {
if available {
format!("{} {}", CHECK_CIRCLE, t!("network.available"))
} else {
format!("{} {}", X_CIRCLE, t!("network.not_available"))
}
} else {
format!("{} {}", DOTS_THREE_CIRCLE, t!("network.availability_check"))
};
ui.label(RichText::new(status_text).size(15.0).color(Colors::gray()));
ui.add_space(3.0);
});
});
});
});
}
/// Show external connection adding [`Modal`].
fn show_add_ext_conn_modal(&mut self, cb: &dyn PlatformCallbacks) {
self.ext_conn_modal = ExternalConnectionModal::new(None);
// Show modal.
Modal::new(ExternalConnectionModal::WALLET_ID)
.position(ModalPosition::CenterTop)
.title(t!("wallets.add_node"))
.show();
cb.show_keyboard();
}
}
@@ -18,29 +18,27 @@ use egui::scroll_area::ScrollBarVisibility;
use crate::gui::Colors;
use crate::gui::platform::PlatformCallbacks;
use crate::gui::views::{Content, View};
use crate::gui::views::wallets::setup::{CommonSetup, ConnectionSetup, RecoverySetup};
use crate::gui::views::wallets::wallet::types::{WalletTab, WalletTabType};
use crate::gui::views::wallets::wallet::WalletContent;
use crate::wallet::{ExternalConnection, Wallet};
use crate::gui::views::wallets::settings::{CommonSettings, ConnectionSettings, RecoverySettings};
use crate::gui::views::wallets::types::{WalletTab, WalletTabType};
use crate::gui::views::wallets::WalletContent;
use crate::wallet::Wallet;
/// Wallet settings tab content.
pub struct WalletSettings {
/// Common setup content.
common_setup: CommonSetup,
common_setup: CommonSettings,
/// Connection setup content.
conn_setup: ConnectionSetup,
conn_setup: ConnectionSettings,
/// Recovery setup content.
recovery_setup: RecoverySetup
recovery_setup: RecoverySettings
}
impl Default for WalletSettings {
fn default() -> Self {
// Check external connections availability on opening.
ExternalConnection::start_ext_conn_availability_check();
Self {
common_setup: CommonSetup::default(),
conn_setup: ConnectionSetup::default(),
recovery_setup: RecoverySetup::default()
common_setup: CommonSettings::default(),
conn_setup: ConnectionSettings::default(),
recovery_setup: RecoverySettings::default()
}
}
}
@@ -0,0 +1,25 @@
// Copyright 2024 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.
mod content;
pub use content::*;
mod connection;
pub use connection::ConnectionSettings;
mod common;
pub use common::CommonSettings;
mod recovery;
pub use recovery::RecoverySettings;
@@ -0,0 +1,288 @@
// 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, RichText};
use grin_chain::SyncStatus;
use grin_util::ZeroingString;
use crate::gui::Colors;
use crate::gui::icons::{EYE, LIFEBUOY, STETHOSCOPE, TRASH, WRENCH};
use crate::gui::platform::PlatformCallbacks;
use crate::gui::views::{Modal, View};
use crate::gui::views::types::{ModalPosition, TextEditOptions};
use crate::node::Node;
use crate::wallet::Wallet;
/// Wallet recovery settings content.
pub struct RecoverySettings {
/// Wallet password [`Modal`] value.
pass_edit: String,
/// Flag to check if wrong password was entered.
wrong_pass: bool,
/// Recovery phrase value.
recovery_phrase: Option<ZeroingString>,
}
/// Identifier for recovery phrase [`Modal`].
const RECOVERY_PHRASE_MODAL: &'static str = "recovery_phrase_modal";
/// Identifier to confirm wallet deletion [`Modal`].
const DELETE_CONFIRMATION_MODAL: &'static str = "delete_wallet_confirmation_modal";
impl Default for RecoverySettings {
fn default() -> Self {
Self {
wrong_pass: false,
pass_edit: "".to_string(),
recovery_phrase: None,
}
}
}
impl RecoverySettings {
pub fn ui(&mut self, ui: &mut egui::Ui, wallet: &mut Wallet, cb: &dyn PlatformCallbacks) {
// Show modal content for this ui container.
self.modal_content_ui(ui, wallet, cb);
ui.add_space(10.0);
View::horizontal_line(ui, Colors::stroke());
ui.add_space(6.0);
View::sub_title(ui, format!("{} {}", WRENCH, t!("wallets.recovery")));
View::horizontal_line(ui, Colors::stroke());
ui.add_space(4.0);
ui.vertical_centered(|ui| {
let integrated_node = wallet.get_current_ext_conn().is_none();
let integrated_node_ready = Node::get_sync_status() == Some(SyncStatus::NoSync);
if wallet.sync_error() || (integrated_node && !integrated_node_ready) {
ui.add_space(2.0);
ui.label(RichText::new(t!("wallets.repair_unavailable"))
.size(16.0)
.color(Colors::red()));
ui.add_space(2.0);
} else if !wallet.is_repairing() {
ui.add_space(6.0);
// Draw button to repair the wallet.
let repair_text = format!("{} {}", STETHOSCOPE, t!("wallets.repair_wallet"));
View::action_button(ui, repair_text, || {
wallet.repair();
});
ui.add_space(6.0);
ui.label(RichText::new(t!("wallets.repair_desc"))
.size(16.0)
.color(Colors::inactive_text()));
}
ui.add_space(6.0);
View::horizontal_line(ui, Colors::item_stroke());
ui.add_space(6.0);
// Draw button to restore the wallet.
let recover_text = format!("{} {}", LIFEBUOY, t!("wallets.recover"));
ui.add_space(4.0);
View::colored_text_button(ui, recover_text, Colors::green(), Colors::button(), || {
wallet.delete_db(true);
});
ui.add_space(6.0);
ui.label(RichText::new(t!("wallets.restore_wallet_desc"))
.size(16.0)
.color(Colors::inactive_text()));
ui.add_space(6.0);
View::horizontal_line(ui, Colors::item_stroke());
ui.add_space(6.0);
let recovery_text = format!("{}:", t!("wallets.recovery_phrase"));
ui.label(RichText::new(recovery_text).size(16.0).color(Colors::gray()));
ui.add_space(6.0);
// Draw button to show recovery phrase.
let show_text = format!("{} {}", EYE, t!("show"));
View::button(ui, show_text, Colors::button(), || {
self.show_recovery_phrase_modal(cb);
});
ui.add_space(12.0);
View::horizontal_line(ui, Colors::item_stroke());
ui.add_space(6.0);
ui.label(RichText::new(t!("wallets.delete_desc")).size(16.0).color(Colors::red()));
ui.add_space(6.0);
// Draw button to delete the wallet.
let delete_text = format!("{} {}", TRASH, t!("wallets.delete"));
View::colored_text_button(ui, delete_text, Colors::red(), Colors::button(), || {
Modal::new(DELETE_CONFIRMATION_MODAL)
.position(ModalPosition::Center)
.title(t!("modal.confirmation"))
.show();
});
ui.add_space(8.0);
});
}
/// Draw [`Modal`] content for this ui container.
fn modal_content_ui(&mut self,
ui: &mut egui::Ui,
wallet: &mut Wallet,
cb: &dyn PlatformCallbacks) {
match Modal::opened() {
None => {}
Some(id) => {
match id {
RECOVERY_PHRASE_MODAL => {
Modal::ui(ui.ctx(), |ui, modal| {
self.recovery_phrase_modal_ui(ui, wallet, modal, cb);
});
}
DELETE_CONFIRMATION_MODAL => {
Modal::ui(ui.ctx(), |ui, modal| {
self.deletion_modal_ui(ui, wallet, modal);
});
}
_ => {}
}
}
}
}
/// Show recovery phrase [`Modal`].
fn show_recovery_phrase_modal(&mut self, cb: &dyn PlatformCallbacks) {
// Setup modal values.
self.pass_edit = "".to_string();
self.wrong_pass = false;
self.recovery_phrase = None;
// Show recovery phrase modal.
Modal::new(RECOVERY_PHRASE_MODAL)
.position(ModalPosition::CenterTop)
.title(t!("wallets.recovery_phrase"))
.show();
cb.show_keyboard();
}
/// Draw recovery phrase [`Modal`] content.
fn recovery_phrase_modal_ui(&mut self,
ui: &mut egui::Ui,
wallet: &mut Wallet,
modal: &Modal,
cb: &dyn PlatformCallbacks) {
ui.add_space(6.0);
if self.recovery_phrase.is_some() {
ui.vertical_centered(|ui| {
ui.label(RichText::new(self.recovery_phrase.clone().unwrap().to_string())
.size(17.0)
.color(Colors::white_or_black(true)));
});
ui.add_space(10.0);
ui.vertical_centered_justified(|ui| {
View::button(ui, t!("close"), Colors::white_or_black(false), || {
self.recovery_phrase = None;
modal.close();
});
});
} else {
ui.vertical_centered(|ui| {
ui.label(RichText::new(t!("wallets.pass"))
.size(17.0)
.color(Colors::gray()));
ui.add_space(8.0);
// Draw current wallet password text edit.
let pass_edit_id = Id::from(modal.id).with(wallet.get_config().id);
let mut pass_edit_opts = TextEditOptions::new(pass_edit_id).password();
View::text_edit(ui, cb, &mut self.pass_edit, &mut pass_edit_opts);
// Show information when password is empty or wrong.
if self.pass_edit.is_empty() {
ui.add_space(12.0);
ui.label(RichText::new(t!("wallets.pass_empty"))
.size(17.0)
.color(Colors::inactive_text()));
} else if self.wrong_pass {
ui.add_space(12.0);
ui.label(RichText::new(t!("wallets.wrong_pass"))
.size(17.0)
.color(Colors::red()));
}
});
ui.add_space(12.0);
// Show modal buttons.
ui.scope(|ui| {
// Setup spacing between buttons.
ui.spacing_mut().item_spacing = egui::Vec2::new(8.0, 0.0);
ui.columns(2, |columns| {
columns[0].vertical_centered_justified(|ui| {
View::button(ui, t!("modal.cancel"), Colors::white_or_black(false), || {
self.recovery_phrase = None;
modal.close();
});
});
columns[1].vertical_centered_justified(|ui| {
View::button(ui, "OK".to_owned(), Colors::white_or_black(false), || {
match wallet.get_recovery(self.pass_edit.clone()) {
Ok(phrase) => {
self.wrong_pass = false;
self.recovery_phrase = Some(phrase);
cb.hide_keyboard();
}
Err(_) => {
self.wrong_pass = true;
}
}
});
});
});
});
}
ui.add_space(6.0);
}
/// Draw wallet deletion [`Modal`] content.
fn deletion_modal_ui(&mut self,
ui: &mut egui::Ui,
wallet: &mut Wallet,
modal: &Modal) {
ui.add_space(8.0);
ui.vertical_centered(|ui| {
ui.label(RichText::new(t!("wallets.delete_conf"))
.size(17.0)
.color(Colors::text(false)));
});
ui.add_space(12.0);
// Show modal buttons.
ui.scope(|ui| {
// Setup spacing between buttons.
ui.spacing_mut().item_spacing = egui::Vec2::new(8.0, 0.0);
ui.columns(2, |columns| {
columns[0].vertical_centered_justified(|ui| {
View::button(ui, t!("modal.cancel"), Colors::white_or_black(false), || {
modal.close();
});
});
columns[1].vertical_centered_justified(|ui| {
View::button(ui, t!("delete"), Colors::white_or_black(false), || {
wallet.delete_wallet();
modal.close();
});
});
});
ui.add_space(6.0);
});
}
}