Skip to content

Commit

Permalink
fill default background if window isn't transparent
Browse files Browse the repository at this point in the history
  • Loading branch information
dzhou121 committed Nov 1, 2023
1 parent 40e1700 commit 09596fb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/app_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ impl ApplicationHandle {
config: Option<WindowConfig>,
) {
let mut window_builder = winit::window::WindowBuilder::new();
let transparent = config.as_ref().and_then(|c| c.transparent).unwrap_or(false);
if let Some(config) = config {
if let Some(size) = config.size {
let size = if size.width == 0.0 || size.height == 0.0 {
Expand Down Expand Up @@ -194,7 +195,7 @@ impl ApplicationHandle {
window_builder = window_builder.with_decorations(false);
}
}
if let Some(transparent) = config.with_transparency {
if let Some(transparent) = config.transparent {
window_builder = window_builder.with_transparent(transparent);
}
if let Some(fullscreen) = config.fullscreen {
Expand All @@ -213,7 +214,7 @@ impl ApplicationHandle {
Err(_) => return,
};
let window_id = window.id();
let window_handle = WindowHandle::new(window, view_fn);
let window_handle = WindowHandle::new(window, view_fn, transparent);
self.window_handles.insert(window_id, window_handle);
}

Expand Down
6 changes: 3 additions & 3 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct WindowConfig {
pub(crate) size: Option<Size>,
pub(crate) position: Option<Point>,
pub(crate) show_titlebar: Option<bool>,
pub(crate) with_transparency: Option<bool>,
pub(crate) transparent: Option<bool>,
pub(crate) fullscreen: Option<Fullscreen>,
pub(crate) window_icon: Option<bool>,
pub(crate) title: Option<String>,
Expand All @@ -41,8 +41,8 @@ impl WindowConfig {
self
}

pub fn with_transparency(mut self, with_transparency: bool) -> Self {
self.with_transparency = Some(with_transparency);
pub fn with_transparent(mut self, transparent: bool) -> Self {
self.transparent = Some(transparent);
self
}

Expand Down
11 changes: 11 additions & 0 deletions src/window_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub(crate) struct WindowHandle {
size: RwSignal<Size>,
theme: RwSignal<Option<Theme>>,
is_maximized: bool,
transparent: bool,
pub(crate) scale: f64,
pub(crate) modifiers: ModifiersState,
pub(crate) cursor_position: Point,
Expand All @@ -67,6 +68,7 @@ impl WindowHandle {
pub(crate) fn new(
window: winit::window::Window,
view_fn: impl FnOnce(winit::window::WindowId) -> Box<dyn View> + 'static,
transparent: bool,
) -> Self {
let scope = Scope::new();
let window_id = window.id();
Expand Down Expand Up @@ -115,6 +117,7 @@ impl WindowHandle {
size,
theme,
is_maximized,
transparent,
scale,
modifiers: ModifiersState::default(),
cursor_position: Point::ZERO,
Expand Down Expand Up @@ -493,6 +496,14 @@ impl WindowHandle {
saved_scroll_bar_edge_widths: Vec::new(),
};
cx.paint_state.renderer.begin();
if !self.transparent {
// fill window with default white background if it's not transparent
cx.fill(
&self.size.get_untracked().to_rect(),
peniko::Color::WHITE,
0.0,
);
}
self.view.paint_main(&mut cx);
if let Some(window) = self.window.as_ref() {
window.pre_present_notify();
Expand Down

0 comments on commit 09596fb

Please sign in to comment.