edit_deck.rs (2147B)
1 use egui::Widget; 2 3 use crate::deck_state::DeckState; 4 5 use super::{ 6 configure_deck::{ConfigureDeckResponse, ConfigureDeckView}, 7 padding, 8 }; 9 10 pub struct EditDeckView<'a> { 11 config_view: ConfigureDeckView<'a>, 12 } 13 14 static EDIT_TEXT: &str = "Edit Deck"; 15 16 pub enum EditDeckResponse { 17 Edit(ConfigureDeckResponse), 18 Delete, 19 } 20 21 impl<'a> EditDeckView<'a> { 22 pub fn new(state: &'a mut DeckState) -> Self { 23 let config_view = ConfigureDeckView::new(state).with_create_text(EDIT_TEXT); 24 Self { config_view } 25 } 26 27 pub fn ui(&mut self, ui: &mut egui::Ui) -> Option<EditDeckResponse> { 28 let mut edit_deck_resp = None; 29 30 padding(egui::Margin::symmetric(16.0, 4.0), ui, |ui| { 31 if ui.add(delete_button()).clicked() { 32 edit_deck_resp = Some(EditDeckResponse::Delete); 33 } 34 }); 35 36 if let Some(config_resp) = self.config_view.ui(ui) { 37 edit_deck_resp = Some(EditDeckResponse::Edit(config_resp)) 38 } 39 40 edit_deck_resp 41 } 42 } 43 44 fn delete_button() -> impl Widget { 45 |ui: &mut egui::Ui| { 46 let size = egui::vec2(108.0, 40.0); 47 ui.allocate_ui_with_layout(size, egui::Layout::top_down(egui::Align::Center), |ui| { 48 ui.add( 49 egui::Button::new("Delete Deck") 50 .fill(ui.visuals().error_fg_color) 51 .min_size(size), 52 ) 53 }) 54 .inner 55 } 56 } 57 58 mod preview { 59 use crate::{ 60 deck_state::DeckState, 61 ui::{Preview, PreviewConfig, View}, 62 }; 63 64 use super::EditDeckView; 65 66 pub struct EditDeckPreview { 67 state: DeckState, 68 } 69 70 impl EditDeckPreview { 71 fn new() -> Self { 72 let state = DeckState::default(); 73 74 EditDeckPreview { state } 75 } 76 } 77 78 impl View for EditDeckPreview { 79 fn ui(&mut self, ui: &mut egui::Ui) { 80 EditDeckView::new(&mut self.state).ui(ui); 81 } 82 } 83 84 impl Preview for EditDeckView<'_> { 85 type Prev = EditDeckPreview; 86 87 fn preview(_cfg: PreviewConfig) -> Self::Prev { 88 EditDeckPreview::new() 89 } 90 } 91 }