transparent_window/
transparent_window.rs

1// Copyright 2025 the Xilem Authors
2// SPDX-License-Identifier: Apache-2.0
3
4//! Showcase a trasnsparent window.
5
6use masonry::properties::types::AsUnit;
7use winit::error::EventLoopError;
8use xilem::{Color, EventLoop, WindowId, WindowView, Xilem, style::Style, view::*};
9
10#[derive(Debug, Clone)]
11struct AppState {
12    alpha: f32,
13
14    window_id: WindowId,
15    is_running: bool,
16}
17
18impl xilem::AppState for AppState {
19    fn keep_running(&self) -> bool {
20        self.is_running
21    }
22}
23
24fn app_logic(state: &mut AppState) -> impl Iterator<Item = WindowView<AppState>> + use<> {
25    let base_color = Color::new([0., 0., 0., state.alpha]);
26
27    let root_view = flex_col((
28        FlexSpacer::Flex(1.),
29        flex_row((
30            text_button("-", |state: &mut AppState| {
31                state.alpha = (state.alpha - 0.25).max(0.);
32            }),
33            text_button("+", |state: &mut AppState| {
34                state.alpha = (state.alpha + 0.25).min(1.);
35            }),
36        ))
37        .gap(10.px()),
38        FlexSpacer::Flex(1.),
39    ))
40    .padding(20.);
41
42    std::iter::once(
43        xilem::window(state.window_id, "Transparency Demo", root_view)
44            .with_base_color(base_color)
45            .with_options(|o| {
46                o.with_transparent(true)
47                    .on_close(|state: &mut AppState| state.is_running = false)
48            }),
49    )
50}
51
52fn main() -> Result<(), EventLoopError> {
53    Xilem::new(
54        AppState {
55            window_id: WindowId::next(),
56            is_running: true,
57            alpha: 0.5,
58        },
59        app_logic,
60    )
61    .run_in(EventLoop::with_user_event())
62}