From 7d8c7ecb59ef03bfdebc585b62ccf4a68d4a9a83 Mon Sep 17 00:00:00 2001 From: tsukinaha Date: Fri, 22 Nov 2024 19:37:26 +0800 Subject: [PATCH] chore: refactor some downcast Signed-off-by: tsukinaha --- src/ui/widgets/tu_list_item.rs | 34 ++++++++++++------------------ src/ui/widgets/tu_overview_item.rs | 34 ++++++++++++------------------ 2 files changed, 28 insertions(+), 40 deletions(-) diff --git a/src/ui/widgets/tu_list_item.rs b/src/ui/widgets/tu_list_item.rs index c922d2d8..86ed3b63 100644 --- a/src/ui/widgets/tu_list_item.rs +++ b/src/ui/widgets/tu_list_item.rs @@ -864,29 +864,23 @@ impl TuListItem { async move { let parent = obj.parent().unwrap().parent().unwrap(); if let Some(list_view) = parent.downcast_ref::() { - let selection = list_view + list_view .model() - .unwrap() - .downcast::() - .unwrap(); - let store = selection - .model() - .unwrap() - .downcast::() - .unwrap(); - store.remove(selection.selected()); + .and_downcast::() + .map(|sel| { + sel.model() + .and_downcast::() + .map(|store| store.remove(sel.selected())) + }); } else if let Some(grid_view) = parent.downcast_ref::() { - let selection = grid_view - .model() - .unwrap() - .downcast::() - .unwrap(); - let store = selection + grid_view .model() - .unwrap() - .downcast::() - .unwrap(); - store.remove(selection.selected()); + .and_downcast::() + .map(|sel| { + sel.model() + .and_downcast::() + .map(|store| store.remove(sel.selected())) + }); } } )); diff --git a/src/ui/widgets/tu_overview_item.rs b/src/ui/widgets/tu_overview_item.rs index 9b71520c..e64b2fbe 100644 --- a/src/ui/widgets/tu_overview_item.rs +++ b/src/ui/widgets/tu_overview_item.rs @@ -643,29 +643,23 @@ impl TuOverviewItem { async move { let parent = obj.parent().unwrap().parent().unwrap(); if let Some(list_view) = parent.downcast_ref::() { - let selection = list_view + list_view .model() - .unwrap() - .downcast::() - .unwrap(); - let store = selection - .model() - .unwrap() - .downcast::() - .unwrap(); - store.remove(selection.selected()); + .and_downcast::() + .map(|sel| { + sel.model() + .and_downcast::() + .map(|store| store.remove(sel.selected())) + }); } else if let Some(grid_view) = parent.downcast_ref::() { - let selection = grid_view - .model() - .unwrap() - .downcast::() - .unwrap(); - let store = selection + grid_view .model() - .unwrap() - .downcast::() - .unwrap(); - store.remove(selection.selected()); + .and_downcast::() + .map(|sel| { + sel.model() + .and_downcast::() + .map(|store| store.remove(sel.selected())) + }); } } ));