Skip to content

Move egui::util::cache to egui::cache; add FramePublisher - #5426

Merged
emilk merged 6 commits into
masterfrom
emilk/cache-refactor
Dec 3, 2024
Merged

emilk merged 6 commits into
masterfrom
emilk/cache-refactor

Conversation

@emilk

@emilk emilk commented Dec 3, 2024

Copy link
Copy Markdown
Owner

This moves egui::util::cache to egui::cache (the old path is deprecated, but still works).

It also adds the FramePublisher helper, which can be used to publish a value which will be retained for this frame and the next:

pub type MyPublisher = egui::cache::FramePublisher<MyKey, MyValue>;

// Publish:
ctx.memory_mut(|mem| {
    mem.caches.cache::<MyPublisher>().set(key, value);
});

// Retrieve:
let value: Option<MyValue> = ctx.memory_mut(|mem| {
    mem.caches
        .cache::<MyPublisher>()
        .get(key)
        .clone()
})

@emilk emilk added the egui label Dec 3, 2024
@github-actions

github-actions Bot commented Dec 3, 2024

Copy link
Copy Markdown

Preview available at https://egui-pr-preview.github.io/pr/5426-emilk/cache-refactor
Note that it might take a couple seconds for the update to show up after the preview_build workflow has completed.

@zhatuokun

Copy link
Copy Markdown
Contributor

If I call CacheStorage::cache somewhere, it will insert a Cache that implements the CacheTrait.

Although Cache will call evict_cache to clear the internal cache, even if the length of the internal cache of cache is 0, it still seems to exist in the CacheStorage.

So would it be better to delete the cache with a cache length of 0 in the CacheStorage::update?

@emilk

emilk commented Dec 3, 2024

Copy link
Copy Markdown
Owner Author

Meta-cache eviction! Yeah, that would make sense, but hopefully there won't be that many cache types… but maybe.

@emilk
emilk merged commit eac7ba0 into master Dec 3, 2024
@emilk
emilk deleted the emilk/cache-refactor branch December 3, 2024 13:28
emilk added a commit to rerun-io/rerun that referenced this pull request Dec 3, 2024
### Related
* Closes #3941
* Uses emilk/egui#5416
* Uses emilk/egui#5426
* Follow-up: #8264

### What
Right-click the name of any space view to get the option to copy its
contents as a screenshot, or save it to disk:


![image](https://github.com/user-attachments/assets/0a0c5215-9042-4989-9e4a-62f76823588a)

-> 


![image](https://github.com/user-attachments/assets/ec222323-4692-4fd2-ae7b-2342996829a3)


#### Details
The implementation simply takes a full-screen screenshot and crops the
result.

The previous attempt (behind a feature-flag) only captured things that
were rendered with `re_renderer`, i.e. did not include any egui
elements, like labels. So it also only worked on spatial views. This PR
work on _all_ space views, and could also be extended to work on
containers etc.

### Limitations
Not yet implemented on web. See
#8264

### TODO
* [x] Point commit hash to egui `master`

---------

Co-authored-by: Andreas Reich <andreas@rerun.io>
@zhatuokun

Copy link
Copy Markdown
Contributor

@emilk I tested this new feature and found that it works well in general, but when you have an Immediate Viewport, it will return None.

Here is my test code.

type MyPublisher = egui::cache::FramePublisher<String, u32>;

fn main() -> eframe::Result {
    eframe::run_simple_native("Example", eframe::NativeOptions::default(), |ctx, _| {
        let outer_key = String::from("outer_key");
        let value = ctx.memory_mut(|m| m.caches.cache::<MyPublisher>().get(&outer_key).copied());
        println!("outer {value:?}"); // None
        ctx.memory_mut(|m| m.caches.cache::<MyPublisher>().set(outer_key, 0));

        ctx.show_viewport_immediate(
            egui::ViewportId::from_hash_of("ExampleViewport"),
            Default::default(),
            |ctx, _| {
                let inner_key = String::from("inner_key");
                let value = ctx.memory_mut(|m| m.caches.cache::<MyPublisher>().get(&inner_key).copied());
                println!("inner: {value:?}"); // None
                ctx.memory_mut(|m| m.caches.cache::<MyPublisher>().set(inner_key, 0));
            },
        );
    })
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants