From fd858d2b4ac71efe121b1da96d59dce2b0b1de3c Mon Sep 17 00:00:00 2001 From: edouardparis Date: Mon, 23 Jun 2025 16:39:07 +0200 Subject: [PATCH] Close tabs of deleted wallet --- liana-gui/src/app/mod.rs | 6 +++- liana-gui/src/gui/mod.rs | 65 ++++++++++++++++++++++++++++++++++++++- liana-gui/src/gui/pane.rs | 2 +- liana-gui/src/launcher.rs | 23 +++++++++++--- 4 files changed, 88 insertions(+), 8 deletions(-) diff --git a/liana-gui/src/app/mod.rs b/liana-gui/src/app/mod.rs index 2222f9ac..03c06431 100644 --- a/liana-gui/src/app/mod.rs +++ b/liana-gui/src/app/mod.rs @@ -34,7 +34,7 @@ use state::{ use wallet::{sync_status, SyncStatus}; use crate::{ - app::{cache::Cache, error::Error, menu::Menu, wallet::Wallet}, + app::{cache::Cache, error::Error, menu::Menu, settings::WalletId, wallet::Wallet}, daemon::{embedded::EmbeddedDaemon, Daemon, DaemonBackend}, dir::LianaDirectory, node::{bitcoind::Bitcoind, NodeType}, @@ -182,6 +182,10 @@ impl App { ) } + pub fn wallet_id(&self) -> WalletId { + self.wallet.id() + } + pub fn title(&self) -> &str { if let Some(alias) = &self.wallet.alias { if !alias.is_empty() { diff --git a/liana-gui/src/gui/mod.rs b/liana-gui/src/gui/mod.rs index a9d9ae8c..9e443b87 100644 --- a/liana-gui/src/gui/mod.rs +++ b/liana-gui/src/gui/mod.rs @@ -15,7 +15,7 @@ use liana_ui::widget::{Column, Container, Element}; pub mod pane; pub mod tab; -use crate::{dir::LianaDirectory, logger::Logger, VERSION}; +use crate::{dir::LianaDirectory, launcher, logger::Logger, VERSION}; pub struct GUI { panes: pane_grid::State, @@ -135,6 +135,69 @@ impl GUI { } Task::none() } + // In case of wallet deletion, remove any tab where the wallet id is currently running. + Message::Pane(p, pane::Message::Tab(t, tab::Message::Launch(msg))) => { + let mut tasks = Vec::new(); + if let launcher::Message::View(launcher::ViewMessage::DeleteWallet( + launcher::DeleteWalletMessage::Confirm(wallet_id), + )) = msg.as_ref() + { + let mut panes_to_close = Vec::::new(); + for (id, pane) in self.panes.iter_mut() { + let tabs_to_close: Vec = pane + .tabs + .iter() + .enumerate() + .filter_map(|(i, tab)| { + if let tab::State::App(a) = &tab.state { + if a.wallet_id() == *wallet_id { + Some(i) + } else { + None + } + } else { + None + } + }) + .collect(); + for i in tabs_to_close { + pane.close_tab(i); + } + if pane.tabs.is_empty() { + panes_to_close.push(*id); + } + } + for id in panes_to_close { + self.panes.close(id); + } + for (&id, pane) in self.panes.iter() { + for tab in &pane.tabs { + if let tab::State::Launcher(l) = &tab.state { + let tab_id = tab.id; + tasks.push(l.reload().map(move |msg| { + Message::Pane( + id, + pane::Message::Tab( + tab_id, + tab::Message::Launch(Box::new(msg)), + ), + ) + })); + } + } + } + } + if let Some(pane) = self.panes.get_mut(p) { + tasks.push( + pane.update( + pane::Message::Tab(t, tab::Message::Launch(msg)), + &self.config, + ) + .map(move |msg| Message::Pane(p, msg)), + ); + } + Task::batch(tasks) + } Message::Pane(i, msg) => { if let Some(pane) = self.panes.get_mut(i) { return pane diff --git a/liana-gui/src/gui/pane.rs b/liana-gui/src/gui/pane.rs index d19e2d69..8a995580 100644 --- a/liana-gui/src/gui/pane.rs +++ b/liana-gui/src/gui/pane.rs @@ -60,7 +60,7 @@ impl Pane { task.map(move |msg| Message::Tab(id, msg)) } - fn close_tab(&mut self, i: usize) { + pub fn close_tab(&mut self, i: usize) { let mut tab = self.remove_tab(i); tab.stop(); } diff --git a/liana-gui/src/launcher.rs b/liana-gui/src/launcher.rs index eca793e3..3af47e1b 100644 --- a/liana-gui/src/launcher.rs +++ b/liana-gui/src/launcher.rs @@ -16,7 +16,7 @@ use tokio::runtime::Handle; use crate::{ app::{ self, - settings::{self, WalletSettings}, + settings::{self, WalletId, WalletSettings}, }, delete::{delete_wallet, DeleteError}, dir::{LianaDirectory, NetworkDirectory}, @@ -76,6 +76,13 @@ impl Launcher { ) } + pub fn reload(&self) -> Task { + Task::perform( + check_network_datadir(self.datadir_path.network_directory(self.network)), + Message::Checked, + ) + } + pub fn stop(&mut self) {} pub fn subscription(&self) -> Subscription { @@ -417,7 +424,7 @@ pub enum ViewMessage { pub enum DeleteWalletMessage { ShowModal(usize), CloseModal, - Confirm, + Confirm(WalletId), Deleted, } @@ -446,7 +453,12 @@ impl DeleteWalletModal { } fn update(&mut self, message: Message) -> Task { - if let Message::View(ViewMessage::DeleteWallet(DeleteWalletMessage::Confirm)) = message { + if let Message::View(ViewMessage::DeleteWallet(DeleteWalletMessage::Confirm(wallet_id))) = + message + { + if wallet_id != self.wallet_settings.wallet_id() { + return Task::none(); + } self.warning = None; if let Err(e) = Handle::current().block_on(delete_wallet( &self.network_directory, @@ -468,8 +480,9 @@ impl DeleteWalletModal { .width(Length::Fixed(200.0)) .style(theme::button::destructive); if self.warning.is_none() { - confirm_button = - confirm_button.on_press(ViewMessage::DeleteWallet(DeleteWalletMessage::Confirm)); + confirm_button = confirm_button.on_press(ViewMessage::DeleteWallet( + DeleteWalletMessage::Confirm(self.wallet_settings.wallet_id()), + )); } // Use separate `Row`s for help text in order to have better spacing. let help_text_1 = format!(