Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dynamic pick list state tracking #467

Closed
raymanfx opened this issue Jul 29, 2020 · 2 comments
Closed

dynamic pick list state tracking #467

raymanfx opened this issue Jul 29, 2020 · 2 comments

Comments

@raymanfx
Copy link

raymanfx commented Jul 29, 2020

Hi, I'm not sure whether I'm missing something here.
In my code, I keep a list of items (DeviceInfoModel) which I need to mutate at runtime. I'd like to represent these as a pick list. All the existing examples (pick_list and game_of_life) use statically typed enumerations as pick list backing, so those do not represent my usecase.

My code looks like this (simplified):

use iced::{executor, pick_list, Application, Column, Command, Element, PickList, Row, Settings};

fn main() {
    Eyece::run(Settings::default())
}

#[derive(Default)]
struct Eyece {
    devices: Vec<DeviceInfoModel>,
    device_list: pick_list::State<DeviceInfoModel>,
    device_selection: DeviceInfoModel,
}

#[derive(Debug, Clone)]
enum Message {
    DeviceSelected(DeviceInfoModel),
}

impl Application for Eyece {
    type Executor = executor::Default;
    type Message = Message;
    type Flags = ();

    fn new(_flags: ()) -> (Self, Command<Message>) {
        // initial device enumeration
        let mut devices = Vec::new();
        devices.push(DeviceInfoModel { index: 0 });
        devices.push(DeviceInfoModel { index: 1 });

        (
            Eyece {
                devices,
                ..Default::default()
            },
            Command::none(),
        )
    }

    fn title(&self) -> String {
        String::from("Eyece")
    }

    fn update(&mut self, _message: Message) -> Command<Message> {
        Command::none()
    }

    fn view(&mut self) -> Element<Message> {
        const SPACING: u16 = 10;
        const PADDING: u16 = 10;

        let config = Row::new().spacing(SPACING).push(PickList::new(
            &mut self.device_list,
            &self.devices,
            Some(self.device_selection),
            Message::DeviceSelected,
        ));

        Column::new().padding(PADDING).push(config).into()
    }
}

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
struct DeviceInfoModel {
    pub index: u32,
    //pub name: String,
}

impl std::fmt::Display for DeviceInfoModel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.index)
    }
}

If you click on an item, the view does not "update" correctly, the first item is still selected.

@Songtronix
Copy link
Contributor

fn update(&mut self, _message: Message) -> Command<Message> {
       Command::none()
}

Do you handle the message in the actual update function?

@raymanfx
Copy link
Author

Not sure how I missed that, sorry for the noise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants