Code Monkey home page Code Monkey logo

Comments (6)

rebo avatar rebo commented on May 11, 2024 3

Hey sure I understand and I actually like the Elm Architecture very much, just find it a little overkill if you have to run everything through it. It's good that you can have internal states on widgets though.

from iced.

dmarcuse avatar dmarcuse commented on May 11, 2024 2

I'd also be interested in this to support the case where messages can't be cloned: I use an enum for my Message type, and while the values yielded on button presses are unit variants, some of the other variants contain more complicated data that can't be copied. I could start wrapping things in Arc, but that adds significantly more work.

from iced.

hecrj avatar hecrj commented on May 11, 2024

Thanks for the suggestion!

TextInput and Slider take a function so you can capture their new value. Have you tried something like this?

button.on_press(if some_context { Message::A } else { Message::B })

Is there any reason this may not work for your use case? Could you share a bit more about it? Why is a closure necessary?

from iced.

rebo avatar rebo commented on May 11, 2024

Hey hecrj, the other reason for having a closure is that non-iced functions can be triggered. For instance I could implement a counter like the below and keep all my state in the counter without having to store anything in the App model. Useful for encapsulation of local self contained state (i.e. React Hooks)

    fn view(&mut self) -> Element<Message> {

        let (count, count_access) = use_istate(|| 0);
      
        Column::new()
            .padding(20)
            .push(
                Button::new(Text::new("Increment"))
                    .on_press_callback(move || count_accesss.set(count + 1)),
            )
            .push(Text::new(count.to_string()).size(50))
            .push(
                Button::new(Text::new("Decrement"))
                .on_press_callback(move || count_accesss.set(count - 1)),
            )
            .into()
    }

from iced.

hecrj avatar hecrj commented on May 11, 2024

Iced is inspired by Elm and The Elm Architecture.

Basically, state mutations are centralized in update and view is a pure function producing a description of the GUI. The whole runtime is built based on the assumption that your view code is pure. This guarantee is what will allow us to build a time travelling debugger in the future!

Hooks (or any kind of mutable callback) are based on side-effects in view code, which is directly against the core foundations of the library.

That said, widgets themselves can have local state, and using something like hooks may be an interesting strategy to track internal state. This should be completely hidden from end-users, though.

from iced.

hecrj avatar hecrj commented on May 11, 2024

@dmarcuse After #155, it is possible to have non-cloneable messages by separating your UI interactions from your non-cloneable events. I detailed how to do this here: #155 (comment)

from iced.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.