Iced + Canvas + Update Fn: Does canvas update if state does not actually change

⚓ Rust    📅 2026-05-25    👤 surdeus    👁️ 1      

surdeus

I have an iced canvas program that displays a line graph. Below the graph are 4 text items that can be clicked which change the date range on the x axis and in turn updates the line graph accordingly. This is accomplished by testing for cursor clicks in bounding rectangles and if so, a message and value are sent to the main program and a state variable is updated. If I click the text that displays 90 days worth of data, the graph updates. If I click it again does the graph update again or when the state variable in the main program is updated does rust see that it the old value and new value are the same thus there is no true state change thus no redraw is required? If it does redraw again, should I trap the value in the main update function or in the canvas update function to keep the redraw from being completed? Below is my canvas update function code. Any assistance will be appreciated.

    fn update(
        &self,
        _state: &mut Self::State,
        event: &iced::Event,
        bounds: Rectangle,
        cursor: Cursor,
    ) -> Option<iced::widget::Action<Message>>   {

        let mut rectangles: Vec<Rectangle> = Vec::new();
        rectangles.push(Rectangle{x: 210.0, y: 755.0, width: 65.0, height: 10.0});
        rectangles.push(Rectangle{x: 350.0, y: 755.0, width: 65.0, height: 10.0});
        rectangles.push(Rectangle{x: 490.0, y: 755.0, width: 65.0, height: 10.0});
        rectangles.push(Rectangle{x: 630.0, y: 755.0, width: 65.0, height: 10.0});

        match (event, cursor.position_in(bounds)) {

            (canvas::Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)), cursor_position) => {

                let clicked_rect = rectangles.iter().position(|rect| rect.contains(cursor_position.expect("no point")));
                match clicked_rect {
                    Some(0) => {
                        return Some(iced::widget::Action::publish(Message::DateRange(0)))                         
                    },
                    Some(1) => {
                        return Some(iced::widget::Action::publish(Message::DateRange(1)))
                    },
                    Some(2) => {
                        return Some(iced::widget::Action::publish(Message::DateRange(2)))
                    },
                    Some(3) => {
                        return Some(iced::widget::Action::publish(Message::DateRange(3)))
                    },
                    _ => {println!("nothing")}
                }
                return None
            }
            _ => return None
        } // end of match statement
    } // end update function
}

2 posts - 2 participants

Read full topic

🏷️ Rust_feed