// SPDX-License-Identifier: EUPL-1.2+ // SPDX-FileCopyrightText: 2025-2026 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, destination_device::DestinationDevice}; use iced::{ Length, Task, widget::{ Scrollable, button, column, scrollable::{Direction, Scrollbar}, space, text, }, }; pub struct PageDiskSelection { destination_device: Option, } impl PageDiskSelection { pub fn new() -> Self { Self { destination_device: None, } } } #[derive(Clone, Debug)] pub enum PageDiskSelectionEvent { Back, SelectDevice(DestinationDevice), ContinueWithDevice(DestinationDevice), } 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 destination_devices = DestinationDevice::get_destination_devices().unwrap_or(vec![]); let destination_selection = if destination_devices.is_empty() { column![text("No applicable devices to install Spectrum to found.")] } else { column(destination_devices.iter().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.name == device.name) { button::success } else { button::primary } }) .into() })) .spacing(8) .into() }; let main_content = column![ space().height(MARGIN_VERTICAL), center_horizontal_in_container!( text("Select the device to install Spectrum to").size(TITLE_TEXT_SIZE) ), space().height(MARGIN_VERTICAL), center_horizontal_in_container!( Scrollable::new(destination_selection) .height(Length::Fill) .direction(Direction::Vertical(Scrollbar::new())) ), space().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(), ) } }