Skip to content

Commit

Permalink
Add PlotUi::add_item(Box<dyn PlotItem>) (#51)
Browse files Browse the repository at this point in the history
<!--
Please read the "Making a PR" section of
[`CONTRIBUTING.md`](https://github.com/emilk/egui_plot/blob/master/CONTRIBUTING.md)
before opening a Pull Request!

* Keep your PR:s small and focused.
* The PR title is what ends up in the changelog, so make it descriptive!
* If applicable, add a screenshot or gif.
* If it is a non-trivial addition, consider adding a demo for a new
example.
* Do NOT open PR:s from your `master` or `main` branch, as that makes it
hard for maintainers to test and add commits to your PR.
* Remember to run `cargo fmt` and `cargo clippy`.
* Open the PR as a draft until you have self-reviewed it and run
`./scripts/check.sh`.
* When you have addressed a PR comment, mark it as resolved.

Please be patient! We will review your PR, but our time is limited!
-->

* Closes <#53>
* [x] I have followed the instructions in the PR template

We have a somewhat unique setup in which we use dioxus for state
management and egui for all of the underlying component rendering. When
passing attributes through dioxus we have to cast the incoming type
which requires that the trait objects be "object safe". `PlotItem` is
not object safe, so we can't pass it through directly. But we also
cannot pass in our own trait which has a method such as `fn
as_egui_plot_item(&self) -> impl PlotItem` because that is also not
object safe so instead we have a trait which has a method `fn
as_egui_plot_item(&self) -> Box<dyn PlotItem>`. This cannot be passed to
the existing `fn add(&self, impl PlotItem)` method because `Box<dyn
PlotItem>` does not satisfy the `impl PlotItem` argument but since the
`plot_items` are stored as `Vec<Box<dyn PlotItem>>` I've added this
method to give access to insert directly.

---------

Co-authored-by: Emil Ernerfeldt <[email protected]>
  • Loading branch information
freeformstu and emilk authored Dec 17, 2024
1 parent 3074792 commit 64acd9d
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions egui_plot/src/plot_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ impl PlotUi {
self.items.push(Box::new(item));
}

/// Add an arbitrary item.
pub fn add_item(&mut self, item: Box<dyn PlotItem>) {
self.items.push(item);
}

/// Add a data line.
pub fn line(&mut self, mut line: crate::Line) {
if line.series.is_empty() {
Expand Down

0 comments on commit 64acd9d

Please sign in to comment.