wallet + ui: accounts methods, remove keychain arguments, transaction list, receive, cancel transactions, update translations

This commit is contained in:
ardocrat
2023-08-21 08:30:50 +03:00
parent e6e18f07ba
commit e26174d061
8 changed files with 613 additions and 87 deletions
+4 -8
View File
@@ -73,8 +73,8 @@ impl WalletContent {
.show_animated_inside(ui, show_balance, |ui| {
ui.vertical_centered(|ui| {
// Draw wallet tabs.
View::max_width_ui(ui, Root::SIDE_PANEL_WIDTH * 1.3, |ui| {
Self::account_balance_ui(ui, data.as_ref().unwrap(), &wallet.config.account);
View::max_width_ui(ui, Root::SIDE_PANEL_WIDTH * 1.35, |ui| {
Self::account_ui(ui, data.as_ref().unwrap(), &wallet.config.account);
});
});
});
@@ -120,8 +120,8 @@ impl WalletContent {
}
}
/// Draw wallet account balance.
fn account_balance_ui(ui: &mut egui::Ui, data: &WalletData, account: &Option<String>) {
/// Draw wallet account content.
fn account_ui(ui: &mut egui::Ui, data: &WalletData, account: &Option<String>) {
let mut rect = ui.available_rect_before_wrap();
rect.set_height(75.0);
// Draw round background.
@@ -131,10 +131,6 @@ impl WalletContent {
ui.allocate_ui_with_layout(rect.size(), Layout::right_to_left(Align::Center), |ui| {
// Setup padding for item buttons.
ui.style_mut().spacing.button_padding = egui::vec2(14.0, 0.0);
// Setup rounding for item buttons.
ui.style_mut().visuals.widgets.inactive.rounding = Rounding::same(8.0);
ui.style_mut().visuals.widgets.hovered.rounding = Rounding::same(8.0);
ui.style_mut().visuals.widgets.active.rounding = Rounding::same(8.0);
// Draw button to add new account.
View::item_button(ui, View::item_rounding(0, 2, true), PLUS, None, || {
+238 -41
View File
@@ -12,17 +12,18 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use egui::{Margin, RichText};
use egui::{Align, Id, Layout, Margin, RichText, Rounding, ScrollArea};
use egui::scroll_area::ScrollBarVisibility;
use grin_core::core::amount_to_hr_string;
use grin_wallet_libwallet::{TxLogEntry, TxLogEntryType};
use crate::gui::Colors;
use crate::gui::icons::{DOWNLOAD, GEAR_FINE, UPLOAD};
use crate::gui::icons::{ARROW_CIRCLE_DOWN, ARROW_CIRCLE_UP, CALENDAR_CHECK, CHECK_CIRCLE, DOTS_THREE_CIRCLE, DOWNLOAD, FILE_TEXT, GEAR_FINE, PROHIBIT, UPLOAD, X_CIRCLE};
use crate::gui::platform::PlatformCallbacks;
use crate::gui::views::{Root, View};
use crate::gui::views::wallets::types::WalletTab;
use crate::gui::views::wallets::wallet::types::WalletTabType;
use crate::gui::views::wallets::wallet::WalletContent;
use crate::wallet::types::WalletData;
use crate::wallet::Wallet;
/// Wallet info tab content.
@@ -38,13 +39,11 @@ impl WalletTab for WalletInfo {
ui: &mut egui::Ui,
frame: &mut eframe::Frame,
wallet: &mut Wallet,
cb: &dyn PlatformCallbacks) {
_: &dyn PlatformCallbacks) {
if WalletContent::sync_ui(ui, frame, wallet) {
return;
}
let data = wallet.get_data().unwrap();
// Show wallet transactions panel.
egui::CentralPanel::default()
.frame(egui::Frame {
@@ -60,47 +59,245 @@ impl WalletTab for WalletInfo {
})
.show_inside(ui, |ui| {
ui.vertical_centered(|ui| {
View::max_width_ui(ui, Root::SIDE_PANEL_WIDTH * 1.3, |ui| {
self.txs_ui(ui, &data);
});
self.txs_ui(ui, wallet);
});
if data.txs.is_empty() {
View::center_content(ui, 96.0, |ui| {
let empty_text = t!(
"wallets.txs_empty",
"receive" => DOWNLOAD,
"send" => UPLOAD,
"settings" => GEAR_FINE
);
ui.label(RichText::new(empty_text).size(16.0).color(Colors::INACTIVE_TEXT));
});
} else {
}
});
}
}
impl WalletInfo {
/// Draw transactions content.
fn txs_ui(&self, ui: &mut egui::Ui, data: &WalletData) {
// Show awaiting confirmation amount.
let awaiting_conf = amount_to_hr_string(data.info.amount_awaiting_confirmation, false);
View::rounded_box(ui,
format!("{} ツ", awaiting_conf),
t!("wallets.await_conf_amount"),
[false, false, false, false]);
// Show awaiting finalization amount.
let awaiting_conf = amount_to_hr_string(data.info.amount_awaiting_finalization, false);
View::rounded_box(ui,
format!("{} ツ", awaiting_conf),
t!("wallets.await_fin_amount"),
[false, false, false, false]);
// Show locked amount.
let awaiting_conf = amount_to_hr_string(data.info.amount_locked, false);
View::rounded_box(ui,
format!("{} ツ", awaiting_conf),
t!("wallets.locked_amount"),
[false, false, true, true]);
fn txs_ui(&self, ui: &mut egui::Ui, wallet: &mut Wallet) {
let data = wallet.get_data().unwrap();
let txs_size = data.txs.len();
// Show transactions info.
View::max_width_ui(ui, Root::SIDE_PANEL_WIDTH * 1.35, |ui| {
let amount_awaiting_conf = data.info.amount_awaiting_confirmation;
let amount_awaiting_fin = data.info.amount_awaiting_finalization;
let amount_locked = data.info.amount_locked;
// Show non-zero awaiting confirmation amount.
if amount_awaiting_conf != 0 {
let awaiting_conf = amount_to_hr_string(amount_awaiting_conf, false);
let rounding = if amount_awaiting_fin != 0 || amount_locked != 0 {
[false, false, false, false]
} else {
[false, false, true, true]
};
View::rounded_box(ui,
format!("{} ツ", awaiting_conf),
t!("wallets.await_conf_amount"),
rounding);
}
// Show non-zero awaiting finalization amount.
if amount_awaiting_fin != 0 {
let awaiting_conf = amount_to_hr_string(amount_awaiting_fin, false);
let rounding = if amount_locked != 0 {
[false, false, false, false]
} else {
[false, false, true, true]
};
View::rounded_box(ui,
format!("{} ツ", awaiting_conf),
t!("wallets.await_fin_amount"),
rounding);
}
// Show non-zero locked amount.
if amount_locked != 0 {
let awaiting_conf = amount_to_hr_string(amount_locked, false);
View::rounded_box(ui,
format!("{} ツ", awaiting_conf),
t!("wallets.locked_amount"),
[false, false, true, true]);
}
// Show message when wallet txs are empty.
if txs_size == 0 {
View::center_content(ui, 96.0, |ui| {
let empty_text = t!(
"wallets.txs_empty",
"receive" => DOWNLOAD,
"send" => UPLOAD,
"settings" => GEAR_FINE
);
ui.label(RichText::new(empty_text).size(16.0).color(Colors::INACTIVE_TEXT));
});
return;
}
});
// Show list of transactions.
ui.add_space(3.0);
ScrollArea::vertical()
.scroll_bar_visibility(ScrollBarVisibility::AlwaysVisible)
.id_source(Id::from("txs_content").with(wallet.config.id))
.auto_shrink([false; 2])
.show_rows(ui, TX_ITEM_HEIGHT, txs_size, |ui, row_range| {
ui.add_space(4.0);
View::max_width_ui(ui, Root::SIDE_PANEL_WIDTH * 1.3, |ui| {
for index in row_range {
let tx = data.txs.get(index).unwrap();
// Setup item rounding.
let item_rounding = View::item_rounding(index, txs_size, false);
// Show transaction item.
tx_item_ui(ui, tx, item_rounding, data.info.last_confirmed_height, wallet);
}
});
ui.add_space(2.0);
});
// for tx in &data.txs {
// if tx.tx_type != TxLogEntryType::TxReceivedCancelled && tx.tx_type != TxLogEntryType::TxSentCancelled {
// println!("tx: {}", serde_json::to_string::<TxLogEntry>(tx).unwrap());
// }
// }
}
}
/// Height of transaction list item.
const TX_ITEM_HEIGHT: f32 = 75.0;
/// Draw transaction item.
fn tx_item_ui(ui: &mut egui::Ui,
tx: &TxLogEntry,
mut rounding: Rounding,
last_height: u64,
wallet: &mut Wallet) {
// Setup layout size.
let mut rect = ui.available_rect_before_wrap();
rect.min += egui::vec2(6.0, 0.0);
rect.set_height(TX_ITEM_HEIGHT);
// Draw round background.
let bg_rect = rect.clone();
ui.painter().rect(bg_rect, rounding, Colors::BUTTON, View::ITEM_STROKE);
// Setup transaction flags.
let is_canceled = tx.tx_type == TxLogEntryType::TxSentCancelled
|| tx.tx_type == TxLogEntryType::TxReceivedCancelled;
let is_cancelling = wallet.is_cancelling(&tx.id);
ui.vertical(|ui| {
ui.allocate_ui_with_layout(rect.size(), Layout::right_to_left(Align::Center), |ui| {
ui.add_space(-6.0);
// Draw button to show transaction info.
rounding.nw = 0.0;
rounding.sw = 0.0;
View::item_button(ui, rounding, FILE_TEXT, None, || {
//TODO: Show tx info
});
if !is_cancelling && !tx.confirmed && tx.tx_type != TxLogEntryType::TxReceivedCancelled
&& tx.tx_type != TxLogEntryType::TxSentCancelled {
View::item_button(ui, Rounding::none(), PROHIBIT, Some(Colors::RED), || {
wallet.cancel(tx.id);
//TODO: Cancel tx
});
}
let layout_size = ui.available_size();
ui.allocate_ui_with_layout(layout_size, Layout::left_to_right(Align::Center), |ui| {
ui.add_space(12.0);
ui.vertical(|ui| {
// Setup transaction amount.
ui.add_space(3.0);
let amount = amount_to_hr_string(tx.amount_credited - tx.amount_debited, true);
let amount_text = if tx.amount_credited > tx.amount_debited {
format!("+{}", amount)
} else {
amount
};
// Setup amount color.
let amount_color = match tx.tx_type {
TxLogEntryType::ConfirmedCoinbase => Colors::BLACK,
TxLogEntryType::TxReceived => Colors::BLACK,
TxLogEntryType::TxSent => Colors::BLACK,
TxLogEntryType::TxReceivedCancelled => Colors::TEXT,
TxLogEntryType::TxSentCancelled => Colors::TEXT,
TxLogEntryType::TxReverted => Colors::TEXT
};
View::ellipsize_text(ui, amount_text, 18.0, amount_color);
ui.add_space(-2.0);
// Setup transaction status text.
let status_text = if !tx.confirmed {
if wallet.is_cancelling(&tx.id) {
format!("{} {}", DOTS_THREE_CIRCLE, t!("wallets.tx_cancelling"))
} else if is_canceled {
format!("{} {}", X_CIRCLE, t!("wallets.tx_canceled"))
} else {
match tx.tx_type {
TxLogEntryType::TxReceived => {
format!("{} {}", DOTS_THREE_CIRCLE, t!("wallets.tx_receiving"))
},
TxLogEntryType::TxSent => {
format!("{} {}", DOTS_THREE_CIRCLE, t!("wallets.tx_sending"))
},
_ => {
format!("{} {}", DOTS_THREE_CIRCLE, t!("wallets.tx_confirming"))
}
}
}
} else {
let tx_height = tx.kernel_lookup_min_height.unwrap_or(0);
match tx.tx_type {
TxLogEntryType::ConfirmedCoinbase => {
format!("{} {}", CHECK_CIRCLE, t!("wallets.tx_confirmed"))
},
TxLogEntryType::TxReceived => {
if last_height - tx_height > wallet.config.min_confirmations {
format!("{} {}", ARROW_CIRCLE_DOWN, t!("wallets.tx_received"))
} else {
format!("{} {}",
DOTS_THREE_CIRCLE,
t!("wallets.tx_awaiting_conf"))
}
},
TxLogEntryType::TxSent => {
if last_height - tx_height > wallet.config.min_confirmations {
format!("{} {}", ARROW_CIRCLE_DOWN, t!("wallets.tx_sent"))
} else {
format!("{} {}", DOTS_THREE_CIRCLE, t!("wallets.tx_confirming"))
}
},
_ => format!("{} {}", ARROW_CIRCLE_UP, t!("wallets.canceled"))
}
};
// Setup status text color.
let status_color = match tx.tx_type {
TxLogEntryType::ConfirmedCoinbase => Colors::TEXT,
TxLogEntryType::TxReceived => if tx.confirmed {
Colors::GREEN
} else {
Colors::TEXT
},
TxLogEntryType::TxSent => if tx.confirmed {
Colors::RED
} else {
Colors::TEXT
},
TxLogEntryType::TxReceivedCancelled => Colors::INACTIVE_TEXT,
TxLogEntryType::TxSentCancelled => Colors::INACTIVE_TEXT,
TxLogEntryType::TxReverted => Colors::INACTIVE_TEXT,
};
ui.label(RichText::new(status_text).size(15.0).color(status_color));
// Setup transaction time.
let tx_ts = if tx.confirmed && tx.confirmation_ts.is_some() {
tx.confirmation_ts.unwrap()
} else {
tx.creation_ts
}.timestamp();
let tx_time = View::format_time(tx_ts);
let tx_time_text = format!("{} {}", CALENDAR_CHECK, tx_time);
ui.label(RichText::new(tx_time_text).size(15.0).color(Colors::GRAY));
});
});
});
});
}
+173 -6
View File
@@ -12,18 +12,38 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use egui::Margin;
use egui::{Id, Margin, RichText, ScrollArea, Widget};
use crate::gui::Colors;
use crate::gui::icons::{ARCHIVE_BOX, BROOM, CLIPBOARD_TEXT, COPY, HAND_COINS};
use crate::gui::platform::PlatformCallbacks;
use crate::gui::views::View;
use crate::gui::views::{Root, View};
use crate::gui::views::wallets::wallet::types::{WalletTab, WalletTabType};
use crate::gui::views::wallets::wallet::WalletContent;
use crate::wallet::Wallet;
/// Receiving tab content.
#[derive(Default)]
pub struct WalletReceive;
pub struct WalletReceive {
/// Slatepack text from sender to create response.
message_edit: String,
/// Generated Slatepack response.
response_edit: String,
/// Flag to check if there is an error happened on receive.
receive_error: bool,
/// Flag to check if response was copied to the clipboard.
response_copied: bool,
}
impl Default for WalletReceive {
fn default() -> Self {
Self {
message_edit: "".to_string(),
response_edit: "".to_string(),
receive_error: false,
response_copied: false,
}
}
}
impl WalletTab for WalletReceive {
fn get_type(&self) -> WalletTabType {
@@ -53,14 +73,161 @@ impl WalletTab for WalletReceive {
..Default::default()
})
.show_inside(ui, |ui| {
self.receive_ui(ui, wallet);
ui.vertical_centered(|ui| {
View::max_width_ui(ui, Root::SIDE_PANEL_WIDTH * 1.3, |ui| {
self.receive_ui(ui, wallet, cb);
});
});
});
}
}
/// Hint for Slatepack Message input.
const RECEIVE_SLATEPACK_HINT: &'static str = "BEGINSLATEPACK.\n...\n...\n...\nENDSLATEPACK.";
impl WalletReceive {
/// Draw receiving content.
pub fn receive_ui(&self, ui: &mut egui::Ui, wallet: &mut Wallet) {
pub fn receive_ui(&mut self,
ui: &mut egui::Ui,
wallet: &mut Wallet,
cb: &dyn PlatformCallbacks) {
ui.add_space(2.0);
View::sub_title(ui, format!("{} {}", HAND_COINS, t!("wallets.manually")));
View::horizontal_line(ui, Colors::ITEM_STROKE);
ui.add_space(3.0);
// Setup manual sending description.
let response_empty = self.response_edit.is_empty();
let desc_text = if response_empty {
t!("wallets.receive_paste_slatepack")
} else {
t!("wallets.receive_send_slatepack")
};
ui.label(RichText::new(desc_text).size(16.0).color(Colors::INACTIVE_TEXT));
ui.add_space(3.0);
// Show Slatepack text input.
let message = if response_empty {
&mut self.message_edit
} else {
&mut self.response_edit
};
View::horizontal_line(ui, Colors::ITEM_STROKE);
ui.add_space(3.0);
ScrollArea::vertical()
.max_height(128.0)
.id_source(Id::from("receive_input").with(wallet.config.id))
.auto_shrink([false; 2])
.show(ui, |ui| {
ui.add_space(7.0);
let message_before = message.clone();
egui::TextEdit::multiline(message)
.font(egui::TextStyle::Small)
.desired_rows(5)
.interactive(response_empty)
.hint_text(RECEIVE_SLATEPACK_HINT)
.desired_width(f32::INFINITY)
.show(ui);
// Clear an error when message changed.
if &message_before != message {
self.receive_error = false;
}
ui.add_space(6.0);
});
ui.add_space(2.0);
View::horizontal_line(ui, Colors::ITEM_STROKE);
ui.add_space(10.0);
// Show receiving input control buttons.
self.receive_buttons_ui(ui, wallet, cb);
}
/// Draw manual receiving input control buttons.
fn receive_buttons_ui(&mut self,
ui: &mut egui::Ui,
wallet: &mut Wallet,
cb: &dyn PlatformCallbacks) {
let field_is_empty = self.message_edit.is_empty() && self.response_edit.is_empty();
let columns_num = if !field_is_empty { 2 } else { 1 };
// Draw buttons to clear/copy/paste.
ui.scope(|ui| {
// Setup spacing between buttons.
ui.spacing_mut().item_spacing = egui::Vec2::new(8.0, 0.0);
ui.columns(columns_num, |columns| {
let first_column_content = |ui: &mut egui::Ui| {
if !field_is_empty {
let clear_text = format!("{} {}", BROOM, t!("clear"));
View::button(ui, clear_text, Colors::BUTTON, || {
self.receive_error = false;
self.response_copied = false;
self.message_edit.clear();
self.response_edit.clear();
});
} else if self.message_edit.is_empty() {
let paste_text = format!("{} {}", CLIPBOARD_TEXT, t!("paste"));
View::button(ui, paste_text, Colors::BUTTON, || {
self.message_edit = cb.get_string_from_buffer();
self.receive_error = false;
});
}
};
if columns_num == 1 {
columns[0].vertical_centered(first_column_content);
} else {
columns[0].vertical_centered_justified(first_column_content);
}
if !field_is_empty {
columns[1].vertical_centered_justified(|ui| {
if !self.message_edit.is_empty() {
let paste_text = format!("{} {}", CLIPBOARD_TEXT, t!("paste"));
View::button(ui, paste_text, Colors::BUTTON, || {
self.message_edit = cb.get_string_from_buffer();
self.receive_error = false;
});
} else if !self.response_edit.is_empty() {
let copy_text = format!("{} {}", COPY, t!("copy"));
View::button(ui, copy_text, Colors::BUTTON, || {
cb.copy_string_to_buffer(self.response_edit.clone());
self.response_copied = true;
});
}
});
}
});
});
// Draw button to create response.
if !self.message_edit.is_empty() && !self.receive_error {
ui.add_space(8.0);
let create_text = format!("{} {}", ARCHIVE_BOX, t!("wallets.create_response"));
View::button(ui, create_text, Colors::GOLD, || {
match wallet.receive(self.message_edit.clone()) {
Ok(response) => {
self.response_edit = response.trim().to_string();
self.message_edit.clear();
// Copy response to clipboard.
cb.copy_string_to_buffer(response);
self.response_copied = true;
},
Err(_) => self.receive_error = true
}
});
ui.add_space(8.0);
} else if self.receive_error {
ui.add_space(8.0);
ui.label(RichText::new(t!("wallets.receive_slatepack_err"))
.size(16.0)
.color(Colors::RED));
ui.add_space(8.0);
} else if self.response_copied {
ui.add_space(8.0);
ui.label(RichText::new(t!("wallets.response_copied"))
.size(16.0)
.color(Colors::GREEN));
ui.add_space(8.0);
}
}
}