Skip to content

Checkbox label is now Into<String>#260

Merged
hecrj merged 3 commits into
iced-rs:masterfrom
0x7CFE:fix-checkbox-label
Apr 6, 2020
Merged

Checkbox label is now Into<String>#260
hecrj merged 3 commits into
iced-rs:masterfrom
0x7CFE:fix-checkbox-label

Conversation

@0x7CFE

@0x7CFE 0x7CFE commented Apr 4, 2020

Copy link
Copy Markdown
Contributor

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()
    }
}

@hecrj hecrj left a comment

Copy link
Copy Markdown
Member

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 0x7CFE force-pushed the fix-checkbox-label branch from f5cc1da to b1f484b Compare April 5, 2020 03:32
@0x7CFE

0x7CFE commented Apr 5, 2020

Copy link
Copy Markdown
Contributor Author

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.

@0x7CFE 0x7CFE force-pushed the fix-checkbox-label branch from b1f484b to 2cdd454 Compare April 5, 2020 04:50
@0x7CFE 0x7CFE force-pushed the fix-checkbox-label branch from 2cdd454 to 15f5b93 Compare April 5, 2020 05:43
@0x7CFE 0x7CFE requested a review from hecrj April 5, 2020 06:18

@hecrj hecrj left a comment

Copy link
Copy Markdown
Member

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
Comment thread examples/tour/src/main.rs
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.

2 participants