// SPDX-License-Identifier: EUPL-1.2+ // SPDX-FileCopyrightText: 2025 Johannes Süllner use super::{ BUTTON_TEXT_SIZE, Event, MARGIN_LR, MARGIN_VERTICAL, Page, TITLE_TEXT_SIZE, UpdateResult, WINDOW_WIDTH, }; use crate::center_horizontal_in_container; use iced::{ Length, Task, widget::{ Scrollable, Space, button, column, scrollable::{Direction, Scrollbar}, text, }, }; use lsblk::BlockDevice; pub struct PageDiskSelection { destination_device: Option, } impl PageDiskSelection { pub fn new() -> Self { Self { destination_device: None, } } } #[derive(Clone, Debug)] pub enum PageDiskSelectionEvent { Back, SelectDevice(BlockDevice), ContinueWithDevice(BlockDevice), } impl Page for PageDiskSelection { fn update(&mut self, event: Event) -> UpdateResult { if let Event::PageDiskSelection(page_disk_selection_event) = event { match page_disk_selection_event { PageDiskSelectionEvent::Back => { return ( Some(Box::new(super::welcome::PageWelcome::new())), Task::none(), ); } PageDiskSelectionEvent::SelectDevice(device) => { self.destination_device = Some(device); } PageDiskSelectionEvent::ContinueWithDevice(device) => { return ( Some(Box::new(super::confirmation::PageConfirmation::new(device))), Task::none(), ); } } } (None, Task::none()) } fn view(&self) -> iced::Element<'_, Event> { let mut block_devices = BlockDevice::list().expect("Failed to get block devices"); block_devices.sort_by_key(|device| device.name.clone()); let mut possible_block_devices = block_devices .iter() .filter(|device| { BlockDevice::is_disk(device) && !&device.name.starts_with("loop") && !&device.name.starts_with("sr") }) .peekable(); let destinations: iced::Element = if possible_block_devices.peek().is_none() { text("No applicable devices to install Spectrum to found.").into() } else { column(possible_block_devices.map(|device| { button(super::layout::disk_element(&device)) .on_press(Event::PageDiskSelection( PageDiskSelectionEvent::SelectDevice(device.clone()), )) .width(WINDOW_WIDTH - 2.0 * MARGIN_LR) .style({ if self .destination_device .as_ref() .is_some_and(|d| d.fullname == device.fullname) { button::success } else { button::primary } }) .into() })) .spacing(8) .into() }; let main_content = column![ Space::with_height(MARGIN_VERTICAL), center_horizontal_in_container!( text("Select the device to install Spectrum to").size(TITLE_TEXT_SIZE) ), Space::with_height(MARGIN_VERTICAL), center_horizontal_in_container!( Scrollable::new(destinations) .height(Length::Fill) .direction(Direction::Vertical(Scrollbar::new())) ), Space::with_height(MARGIN_VERTICAL), ]; super::layout::bottom_buttons_layout( [[ ( text("Back").size(BUTTON_TEXT_SIZE).into(), Some(Event::PageDiskSelection(PageDiskSelectionEvent::Back)), ), ( text("Next").size(BUTTON_TEXT_SIZE).into(), match &self.destination_device { None => None, Some(device) => Some(Event::PageDiskSelection( PageDiskSelectionEvent::ContinueWithDevice(device.clone()), )), }, ), ]], main_content.into(), ) } }