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

Replace stateful widgets with the new iced_pure API #1393

Merged
merged 20 commits into from
Aug 5, 2022

Conversation

hecrj
Copy link
Member

@hecrj hecrj commented Jul 27, 2022

This PR replaces the current stateful widgets with the new widget API introduced by iced_pure in #1284.

The changes here remove a lot of code and prepare the codebase for the soon-to-land widget operations described in iced-rs/rfcs#7.

Some of the most relevant changes are:

  • iced_pure code has been removed and the crate is deprecated.

  • The pure feature has been removed.

  • Widget types are no longer re-exported in the root iced crate. Instead, they have to be properly imported with the full path (eg. use iced::widget::Text).

  • The widget function helpers in iced_pure have been moved to iced::widget.

  • A couple of new macros have been added in iced::widget: column and row. They are analogous to vec!. Therefore, the old approach

    use iced::widget::Column;
    
    Column::new().push("I am a column").push("with 2 children");

    can now be written as follows:

    use iced::widget::column;
    
    column!["I am a column", "with 2 children!"]
  • The row and column function helpers are also available, and they are equivalent to Row::with_children and Column::with_children. These are useful when you have an iterator or a Vec of elements!

  • Text::new now can take any ToString instead of Into<String>.

  • All of the internal State types are no longer exported in iced.

Thanks to all of these changes, all of the examples are now much simpler and less verbose! For instance, here is the new counter example:

use iced::widget::{button, column, text};
use iced::{Alignment, Element, Sandbox, Settings};

pub fn main() -> iced::Result {
    Counter::run(Settings::default())
}

struct Counter {
    value: i32,
}

#[derive(Debug, Clone, Copy)]
enum Message {
    IncrementPressed,
    DecrementPressed,
}

impl Sandbox for Counter {
    type Message = Message;

    fn new() -> Self {
        Self { value: 0 }
    }

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

    fn update(&mut self, message: Message) {
        match message {
            Message::IncrementPressed => {
                self.value += 1;
            }
            Message::DecrementPressed => {
                self.value -= 1;
            }
        }
    }

    fn view(&self) -> Element<Message> {
        column![
            button("Increment").on_press(Message::IncrementPressed),
            text(self.value).size(50),
            button("Decrement").on_press(Message::DecrementPressed)
        ]
        .padding(20)
        .align_items(Alignment::Center)
        .into()
    }
}

Once the widget operations land soon, this API will satisfy all of the use cases supported by the stateful one (and more!), while being way less verbose and cumbersome.

@hecrj hecrj added improvement An internal improvement feature New feature or request developer experience widget shell labels Jul 27, 2022
@hecrj hecrj added this to the 0.5.0 milestone Jul 27, 2022
@artursapek
Copy link
Contributor

dat delta
image

@hecrj hecrj mentioned this pull request Aug 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
developer experience feature New feature or request improvement An internal improvement shell widget
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants