ryo33/egui_cable

[Question] Store state in egui UI?

Closed this issue · 2 comments

Hey Ryo, nice project! I might use it in mine as well.

Quick question. Is it normal practice to store widget state in the egui::Ui?

I know that usually egui widgets are stateless and are just builders as it is stated in the doc. But I store state right in my widget (I am also creating widget for egui https://github.com/blitzarx1/egui_graphs) implementing Widget trait for &mut MyWidget. I wanted to ask why you chose to store state in the Ui, is it intended usage of the Ui, won't it affect egui performance is state is rather big.

Thanks for the answer in advance.

ryo33 commented

Thanks.

I believe that states of GUI applications can be categorized into two types of states: application state and UI state. Although the boundaries between them are not clearly defined, distinguishing these states can contribute to simpler code. In this library, I thought that actual screen positions of ports, whether it’s dragged or not, and such things are UI states, and I stored them in egui::Ui. Other states, e.g. where to place port or cables, whether connected or not, seems application state for me, so I make them stateless and data oriented. Official built-in widgets followed a similar design approach, and I think it makes egui easier.

As for performance, states are just something like Arc<Mutex<HashMap<Id, Any>>>. It’s not a resource-intensive operation, but excessive usage of it may result in a decrease of performance.

I hope this helps.

Thanks for clarifications. It helped!