Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 6 commits into from
Dec 3, 2024

Conversation

emilk
Copy link
Owner

@emilk emilk commented Dec 3, 2024

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
Copy link

github-actions bot commented Dec 3, 2024

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
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
Copy link
Owner Author

emilk commented Dec 3, 2024

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
46 checks passed
@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 <[email protected]>
@zhatuokun
Copy link
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