fix(ui): fixed current game session selection when selected one is removed

This commit is contained in:
Observer KRypt0n_ 2023-06-14 10:59:08 +02:00
parent 422e66b7ed
commit ca838db7b9
No known key found for this signature in database
GPG key ID: 844DA47BA25FE1E2

View file

@ -49,7 +49,7 @@ impl AsyncFactoryComponent for GameSession {
set_valign: gtk::Align::Center, set_valign: gtk::Align::Center,
connect_clicked[sender, index] => move |_| { connect_clicked[sender, index] => move |_| {
sender.output(GamePageMsg::UpdateSession(index.clone())); sender.output(GamePageMsg::UpdateSession(index.current_index()));
} }
}, },
@ -62,14 +62,14 @@ impl AsyncFactoryComponent for GameSession {
set_valign: gtk::Align::Center, set_valign: gtk::Align::Center,
connect_clicked[sender, index] => move |_| { connect_clicked[sender, index] => move |_| {
sender.output(GamePageMsg::RemoveSession(index.clone())); sender.output(GamePageMsg::RemoveSession(index.current_index()));
} }
}, },
set_activatable: true, set_activatable: true,
connect_activated[sender, index] => move |_| { connect_activated[sender, index] => move |_| {
sender.output(GamePageMsg::SetCurrent(index.clone())); sender.output(GamePageMsg::SetCurrent(index.current_index()));
} }
} }
} }
@ -97,9 +97,9 @@ pub struct GamePage {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum GamePageMsg { pub enum GamePageMsg {
AddSession, AddSession,
UpdateSession(DynamicIndex), UpdateSession(usize),
RemoveSession(DynamicIndex), RemoveSession(usize),
SetCurrent(DynamicIndex) SetCurrent(usize)
} }
#[relm4::component(async, pub)] #[relm4::component(async, pub)]
@ -230,7 +230,7 @@ impl SimpleAsyncComponent for GamePage {
} }
GamePageMsg::UpdateSession(index) => { GamePageMsg::UpdateSession(index) => {
if let Some(session) = self.sessions.guard().get(index.current_index()) { if let Some(session) = self.sessions.guard().get(index) {
if let Ok(config) = Config::get() { if let Ok(config) = Config::get() {
if let Err(err) = Sessions::update(session.name.clone(), config.get_wine_prefix_path()) { if let Err(err) = Sessions::update(session.name.clone(), config.get_wine_prefix_path()) {
sender.output(EnhancementsAppMsg::Toast { sender.output(EnhancementsAppMsg::Toast {
@ -243,7 +243,7 @@ impl SimpleAsyncComponent for GamePage {
} }
GamePageMsg::RemoveSession(index) => { GamePageMsg::RemoveSession(index) => {
if let Some(session) = self.sessions.guard().get(index.current_index()) { if let Some(session) = self.sessions.guard().get(index) {
if let Err(err) = Sessions::remove(&session.name) { if let Err(err) = Sessions::remove(&session.name) {
sender.output(EnhancementsAppMsg::Toast { sender.output(EnhancementsAppMsg::Toast {
title: tr("game-session-remove-failed"), title: tr("game-session-remove-failed"),
@ -252,15 +252,17 @@ impl SimpleAsyncComponent for GamePage {
return; return;
} }
// TODO: select another available session?
} }
self.sessions.guard().remove(index.current_index()); self.sessions.guard().remove(index);
if !self.sessions.is_empty() {
sender.input(GamePageMsg::SetCurrent(0));
}
} }
GamePageMsg::SetCurrent(index) => { GamePageMsg::SetCurrent(index) => {
if let Some(session) = self.sessions.guard().get(index.current_index()) { if let Some(session) = self.sessions.guard().get(index) {
if let Ok(config) = Config::get() { if let Ok(config) = Config::get() {
if let Err(err) = Sessions::set_current(session.name.clone()) { if let Err(err) = Sessions::set_current(session.name.clone()) {
sender.output(EnhancementsAppMsg::Toast { sender.output(EnhancementsAppMsg::Toast {