iced-rs / iced

A cross-platform GUI library for Rust, inspired by Elm
https://iced.rs
MIT License
24.94k stars 1.18k forks source link

Trouble using canvas with pane_grid #894

Closed toastmod closed 3 years ago

toastmod commented 3 years ago

Hey I currently have a canvas Program stored in my Application, but self gets moved when I try to access it in the PaneGrid's function. Is there any way I can view my canvas in a PaneGrid?

    fn view(&mut self) -> Element<'_, Self::Message> {
        let panegrid = pane_grid::PaneGrid::new(&mut self.panes, |id, pane|{
            if pane.id == 0 {
                let canvas = Canvas::new(&mut self.canvas)
                    .width(Length::Fill)
                    .height(Length::Fill);

                let canv_container = Container::new(canvas)
                    .width(Length::Fill)
                    .height(Length::Fill)
                    .center_x()
                    .center_y();
                let mut mainstack = Column::new().push(canv_container);
                let mut mainrow = Row::new().push(mainstack);
                pane_grid::Content::new(mainrow)
            }else{
                pane_grid::Content::new(Text::new("empty"))
            }
        });

        Container::new(panegrid).into()
    }

This errors to this:

error[E0495]: cannot infer an appropriate lifetime for borrow expression due to conflicting requirements
   --> src\app.rs:345:42
    |
345 |                 let canvas = Canvas::new(&mut self.canvas)
    |                                          ^^^^^^^^^^^^^^^^
    |
note: first, the lifetime cannot outlive the lifetime `'_` as defined on the body at 343:66...
   --> src\app.rs:343:66
    |
343 |         let panegrid = pane_grid::PaneGrid::new(&mut self.panes, |id, pane|{
    |                                                                  ^^^^^^^^^^
note: ...so that closure can access `self`
   --> src\app.rs:345:42
    |
345 |                 let canvas = Canvas::new(&mut self.canvas)
    |                                          ^^^^^^^^^^^^^^^^
note: but, the lifetime must be valid for the anonymous lifetime defined on the method body at 332:13...
   --> src\app.rs:332:13
    |
332 |     fn view(&mut self) -> Element<'_, Self::Message> {
    |             ^^^^^^^^^
note: ...so that the expression is assignable
   --> src\app.rs:362:9
    |
362 |         Container::new(panegrid).into()
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    = note: expected `iced_native::element::Element<'_, _, _>`
               found `iced_native::element::Element<'_, _, _>`
toastmod commented 3 years ago

Closing this because I just realized it can be worked around pretty easily. My method was by using a trait to abstract the result of view() in my custom pane struct.