Giter Site home page Giter Site logo

Comments (5)

Kaiden42 avatar Kaiden42 commented on July 18, 2024

Based on the error message, I assume you have a problem with the message types. It expects an Element<Message> but you are pushing an Element<TimePlotMessage>. The Message type of the container, you are pushing your plot to, is from type FerrisMessage not TimePlotMessage (If replacing the adding of the ferris image with the plot is the only thing you have changed). The tabs example contains of different message types for each part in the application. These types build a hierarchically structure. You have to wrap your message to the higher message type once your message is bubbling up the structure.

You could add a variant like Plot(TimePlotMessage) to the FerrisMessage enum like this:

#[derive(Debug, Clone)]
pub enum FerrisMessage {
    ImageWidthChanged(u16),
    Plot(TimePlotMessage),
}

Than wrap the message the plot produces in a FerrisMessage with the map method of the Element type like this:

.push(self.chart.view().map(FerrisMessage::Plot))

from iced_aw.

bobgates avatar bobgates commented on July 18, 2024

Thanks. That's moved me on a lot, but I forgot to mention that I'd renamed Ferris and everything in it to TimePlot, so the structure looks like this:

pub enum TimePlotMessage {
    ImageWidthChanged(u16),
}

It is referenced in update:

    pub fn update(&mut self, message: TimePlotMessage) {
        match message {
            TimePlotMessage::ImageWidthChanged(value) => self.timeplot_width = value,
        }
    }

and then in fn content, where the problem occurs. I think I'm using map wrong:

    fn content(&mut self) -> Element<'_, Self::Message> {
        let content = Column::new()
      . . .
            .push(self.chart.view().map(TimePlotMessage::ImageWidthChanged));

and the other step is here:

    fn view(&mut self) -> Element<Message> {
        let chart = ChartWidget::new(self)
            .width(Length::Fill)
            .height(Length::Fill);

        chart.into()
    }

I'm now getting an error about how a function is expected, but what is passed isn't a function. I'm still getting used to passing functions around, and to map. Many thanks for your help so far, can you help further?

from iced_aw.

Kaiden42 avatar Kaiden42 commented on July 18, 2024

Thanks. That's moved me on a lot, but I forgot to mention that I'd renamed Ferris and everything in it to TimePlot, so the structure looks like this:

Than it might be the other side around. What type of message is the element producing, that is created by self.chart.view()? You have to make sure, that all the elements of the surrounding element (like the column in this example) are producing the same type of message.

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

Is this the method signature of your plot view? If so, than it produces the wrong kind of message. Your TimePlotMessage is part of Message but Message is not part of TimePlotMessage. Therefore you cant wrap a Message in a TimePlotMessage which is than wrapped again in a Message. (Well you could if the TimePlotMessage has a variant Message::(Message), but this adds a nasty loop while processing the message and I wouldn't recommend this).

You could of cause handle every message of your application in only one enum type. But I wouldn't recommend this, as your enum and the update method of your application will get bigger and bigger over time and becomes nearly unreadable once you have a very large application. Therefore, the tabs example splits the message into different enums, one for each part of the application.

and then in fn content, where the problem occurs. I think I'm using map wrong:

The map function takes an Fn(A) -> B as an argument. In other words, you have to provide a function that can map one kind of message to another one. This line of the example:

content.map(Message::Ferris)

Is nothing else than a shorthand of the compiler and is equivalent to (see here):

content.map( |ferris_message| Message::Ferris(ferris_message) )

as the enum variant with unnamed fields can act like a function that creates the enum value.

from iced_aw.

bobgates avatar bobgates commented on July 18, 2024

Thanks for that. I'm currently implementing what I want to see in a non-tabbed environment, and will get back to tabs once that's done. But your explanation has helped a lot.

from iced_aw.

bobgates avatar bobgates commented on July 18, 2024

The mapping to appropriate messages helped. It is a lot to get my head around, but is working nicely now.

from iced_aw.

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.