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

Checkbox label is now Into<String> #260

Merged
merged 3 commits into from Apr 6, 2020
Merged

Conversation

0x7CFE
Copy link
Contributor

@0x7CFE 0x7CFE commented Apr 4, 2020

This PR allows checkbox labels to be dynamically modified depending on the business logic.

For example:
image

use iced::{Sandbox, Settings, Text, Align, Column};

fn main() {
    CheckUi::run(Settings::default());
}

#[derive(Default)]
struct CheckUi {
    checks: Vec<bool>,
}

#[derive(Debug, Copy, Clone)]
enum CheckMessage {
    InputChecked(usize, bool),
}

impl Sandbox for CheckUi {
    type Message = CheckMessage;

    fn new() -> Self {
        Self {
            checks: (0..10).map(|_| false).collect(),
            ..Default::default()
        }
    }
    
    fn title(&self) -> String { "Dynamic checkbox label".into() }
    
    fn update(&mut self, message: Self::Message) {
        match message {
            CheckMessage::InputChecked(index, value) => self.checks[index] = value,
        }
    }

    fn view(&mut self) -> iced::Element<'_, Self::Message> {
        let mut inputs = Column::new()
            .height(iced::Length::Fill)
            .width(iced::Length::Fill)
            .align_items(Align::Start);

        for (index, checked) in self.checks.iter().enumerate() {
            inputs = inputs.push(
                iced::Checkbox::new(
                    *checked,
                    format!("Checkbox {}", index),
                    move |state| CheckMessage::InputChecked(index, state)
                )
                .width(iced::Length::Fill)
            );
        }

        inputs.into()
    }
}

Copy link
Member

@hecrj hecrj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! I forgot about changing this.

Could you do the same for the Radio widget?

@0x7CFE
Copy link
Contributor Author

0x7CFE commented Apr 5, 2020

Could you do the same for the Radio widget?

Done.

By the way, would you mind refactoring the text in the same manner as it is done for normal push button? I mean so that Checkbox would accept content instead of label. For example that would allow editing text styles without need to delegate a ton of methods.

When working on my project I already hacked an option to set the color of the text.

Copy link
Member

@hecrj hecrj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Eventually, I believe we will change Checkbox to only be the actual checkbox without any text. Also, Radio needs to change to address #71.

@hecrj hecrj merged commit 0f60253 into iced-rs:master Apr 6, 2020
@hecrj hecrj added the improvement An internal improvement label Apr 6, 2020
@hecrj hecrj added this to the 0.1.1 milestone Apr 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improvement An internal improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants