Skip to main content

egui_pop_core/
window_surface.rs

1//! What draws into a window's own surfaces besides egui, per window, as
2//! the part drawing it says it: the image viewport's image surface (its
3//! format, colour space, route, or why it has none). The window registry
4//! (`windows.list`) shows it with each window; the part sets it each
5//! frame it draws and clears it when the window closes.
6//!
7//! Also how the window itself was built for such a part
8//! ([`Transparency`]): a surface below egui shows through a transparent
9//! window only, so whoever builds a window that may show one says
10//! whether it is transparent, or why not ([`set_transparency`]); the part
11//! reads it before making its surface.
12//!
13//! <div class="warning">Experimental: may change in any release
14//! (docs/stability.md).</div>
15
16use egui::{Context, ViewportId};
17use serde_json::Value as Json;
18
19use crate::state::Slot;
20
21const SURFACE: Slot<Json> = Slot::new("core.window_surface");
22const TRANSPARENCY: Slot<Transparency> = Slot::new("core.window_transparency");
23
24/// How a window was built for a surface below egui (the image surface):
25/// transparent, so it shows through, or opaque and why.
26#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
27#[serde(rename_all = "snake_case")]
28pub enum Transparency {
29    /// Built transparent (`ViewportBuilder::with_transparent(true)`).
30    Transparent,
31    /// Built opaque, and why (the platform has no surface to show
32    /// through it, or the application did not ask).
33    Opaque(String),
34}
35
36impl Transparency {
37    /// Whether the window is transparent.
38    pub fn is_transparent(&self) -> bool {
39        matches!(self, Transparency::Transparent)
40    }
41
42    /// Why the window is opaque; none when it is transparent.
43    pub fn why_opaque(&self) -> Option<&str> {
44        match self {
45            Transparency::Transparent => None,
46            Transparency::Opaque(why) => Some(why),
47        }
48    }
49}
50
51/// Says how `viewport`'s window was built ([`Transparency`]); `None`
52/// forgets it.
53pub fn set_transparency(ctx: &Context, viewport: ViewportId, transparency: Option<Transparency>) {
54    let slot = TRANSPARENCY.keyed(viewport);
55    match transparency {
56        Some(t) => {
57            if slot.get(ctx).as_ref() != Some(&t) {
58                slot.set(ctx, t);
59            }
60        }
61        None => {
62            slot.remove(ctx);
63        }
64    }
65}
66
67/// How `viewport`'s window was built, as said ([`set_transparency`]).
68pub fn transparency(ctx: &Context, viewport: ViewportId) -> Option<Transparency> {
69    TRANSPARENCY.keyed(viewport).get(ctx)
70}
71
72/// Says what draws into `viewport`'s own surface now; `None` forgets it
73/// (the window closed, or nothing draws there any more).
74pub fn set(ctx: &Context, viewport: ViewportId, state: Option<Json>) {
75    let slot = SURFACE.keyed(viewport);
76    match state {
77        Some(s) => slot.set(ctx, s),
78        None => {
79            slot.remove(ctx);
80        }
81    }
82}
83
84/// What was said for `viewport` ([`set`]).
85pub fn get(ctx: &Context, viewport: ViewportId) -> Option<Json> {
86    SURFACE.keyed(viewport).get(ctx)
87}
88
89#[cfg(test)]
90mod tests {
91    use super::*;
92
93    #[test]
94    fn kept_per_window_and_forgotten() {
95        let ctx = Context::default();
96        let other = ViewportId::from_hash_of("other");
97        set(&ctx, other, Some(serde_json::json!({ "route": "tagged" })));
98        assert_eq!(get(&ctx, other).unwrap()["route"], "tagged");
99        assert_eq!(get(&ctx, ViewportId::ROOT), None);
100        set(&ctx, other, None);
101        assert_eq!(get(&ctx, other), None);
102    }
103
104    #[test]
105    fn transparency_kept_per_window_with_its_reason() {
106        let ctx = Context::default();
107        let other = ViewportId::from_hash_of("other");
108        set_transparency(&ctx, other, Some(Transparency::Opaque("X11".into())));
109        set_transparency(&ctx, ViewportId::ROOT, Some(Transparency::Transparent));
110        assert_eq!(transparency(&ctx, other).unwrap().why_opaque(), Some("X11"));
111        assert!(
112            transparency(&ctx, ViewportId::ROOT)
113                .unwrap()
114                .is_transparent()
115        );
116        assert_eq!(
117            serde_json::to_value(Transparency::Opaque("X11".into())).unwrap(),
118            serde_json::json!({ "opaque": "X11" })
119        );
120        set_transparency(&ctx, other, None);
121        assert_eq!(transparency(&ctx, other), None);
122    }
123}