From f343eebbebee9da5e122f7178776d823425193df Mon Sep 17 00:00:00 2001 From: Alex Taylor Date: Sun, 14 Oct 2018 07:07:24 -0700 Subject: [PATCH 1/5] Windows: Fix transparency (#260) --- src/platform/windows/window.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/platform/windows/window.rs b/src/platform/windows/window.rs index c0d1aa96cf..624b05c308 100644 --- a/src/platform/windows/window.rs +++ b/src/platform/windows/window.rs @@ -14,6 +14,7 @@ use winapi::um::{combaseapi, dwmapi, libloaderapi, winuser}; use winapi::um::objbase::COINIT_MULTITHREADED; use winapi::um::shobjidl_core::{CLSID_TaskbarList, ITaskbarList2}; use winapi::um::winnt::{LONG, LPCWSTR}; +use winapi::um::wingdi::CreateRectRgn; use { CreationError, @@ -893,7 +894,7 @@ unsafe fn init( } ) } else { - (winuser::WS_EX_APPWINDOW | winuser::WS_EX_WINDOWEDGE, + (winuser::WS_EX_APPWINDOW | winuser::WS_EX_WINDOWEDGE | winuser::WS_EX_LAYERED, winuser::WS_OVERLAPPEDWINDOW | winuser::WS_CLIPSIBLINGS | winuser::WS_CLIPCHILDREN) }; @@ -1018,13 +1019,22 @@ unsafe fn init( // making the window transparent if attributes.transparent && !pl_attribs.no_redirection_bitmap { let bb = dwmapi::DWM_BLURBEHIND { - dwFlags: 0x1, // FIXME: DWM_BB_ENABLE; + dwFlags: dwmapi::DWM_BB_ENABLE | dwmapi::DWM_BB_BLURREGION, fEnable: 1, - hRgnBlur: ptr::null_mut(), + hRgnBlur: CreateRectRgn(0, 0, -1, -1), // makes the window transparent fTransitionOnMaximized: 0, }; dwmapi::DwmEnableBlurBehindWindow(real_window.0, &bb); + + if attributes.decorations { + // HACK: When opaque (opacity 255), there is a trail whenever + // the transparent window is moved. By reducing it to 254, + // the window is rendered properly. + let opacity = 254; + + winuser::SetLayeredWindowAttributes(real_window.0, 0x0030c100, opacity, winuser::LWA_ALPHA); + } } let win = Window { From f89fa620f4170e4c0092f58765dd1b67615426c8 Mon Sep 17 00:00:00 2001 From: Alex Taylor Date: Sun, 14 Oct 2018 07:26:59 -0700 Subject: [PATCH 2/5] Windows: Only enable WS_EX_LAYERED for transparent windows --- src/platform/windows/window.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/platform/windows/window.rs b/src/platform/windows/window.rs index 624b05c308..54783247bc 100644 --- a/src/platform/windows/window.rs +++ b/src/platform/windows/window.rs @@ -894,7 +894,7 @@ unsafe fn init( } ) } else { - (winuser::WS_EX_APPWINDOW | winuser::WS_EX_WINDOWEDGE | winuser::WS_EX_LAYERED, + (winuser::WS_EX_APPWINDOW | winuser::WS_EX_WINDOWEDGE, winuser::WS_OVERLAPPEDWINDOW | winuser::WS_CLIPSIBLINGS | winuser::WS_CLIPCHILDREN) }; @@ -904,6 +904,9 @@ unsafe fn init( if pl_attribs.no_redirection_bitmap { ex_style |= winuser::WS_EX_NOREDIRECTIONBITMAP; } + if attributes.transparent && attributes.decorations { + ex_style |= winuser::WS_EX_LAYERED; + } // adjusting the window coordinates using the style winuser::AdjustWindowRectEx(&mut rect, style, 0, ex_style); From 1feb5ef59e8e94f79c235beea4133dff363ac012 Mon Sep 17 00:00:00 2001 From: Alex Taylor Date: Mon, 15 Oct 2018 03:44:01 -0700 Subject: [PATCH 3/5] Update winapi to 0.3.6 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index e27c85e4b0..bfdcfc803e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,7 +35,7 @@ core-foundation = "0.6" core-graphics = "0.16" [target.'cfg(target_os = "windows")'.dependencies.winapi] -version = "0.3.5" +version = "0.3.6" features = [ "combaseapi", "dwmapi", From b2dfff30db97489def511a1e4ff3000b10d4b060 Mon Sep 17 00:00:00 2001 From: Alex Taylor Date: Mon, 15 Oct 2018 03:47:58 -0700 Subject: [PATCH 4/5] Windows: Amend transparency code --- src/platform/windows/window.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/platform/windows/window.rs b/src/platform/windows/window.rs index 54783247bc..d7d789a13a 100644 --- a/src/platform/windows/window.rs +++ b/src/platform/windows/window.rs @@ -13,8 +13,8 @@ use winapi::shared::windef::{HWND, LPPOINT, POINT, RECT}; use winapi::um::{combaseapi, dwmapi, libloaderapi, winuser}; use winapi::um::objbase::COINIT_MULTITHREADED; use winapi::um::shobjidl_core::{CLSID_TaskbarList, ITaskbarList2}; +use winapi::um::wingdi::{CreateRectRgn, DeleteObject}; use winapi::um::winnt::{LONG, LPCWSTR}; -use winapi::um::wingdi::CreateRectRgn; use { CreationError, @@ -1021,14 +1021,17 @@ unsafe fn init( // making the window transparent if attributes.transparent && !pl_attribs.no_redirection_bitmap { + let region = CreateRectRgn(0, 0, -1, -1); // makes the window transparent + let bb = dwmapi::DWM_BLURBEHIND { dwFlags: dwmapi::DWM_BB_ENABLE | dwmapi::DWM_BB_BLURREGION, fEnable: 1, - hRgnBlur: CreateRectRgn(0, 0, -1, -1), // makes the window transparent + hRgnBlur: region, fTransitionOnMaximized: 0, }; dwmapi::DwmEnableBlurBehindWindow(real_window.0, &bb); + DeleteObject(region as _); if attributes.decorations { // HACK: When opaque (opacity 255), there is a trail whenever @@ -1036,7 +1039,10 @@ unsafe fn init( // the window is rendered properly. let opacity = 254; - winuser::SetLayeredWindowAttributes(real_window.0, 0x0030c100, opacity, winuser::LWA_ALPHA); + // The color key can be any value except for black (0x0). + let color_key = 0x0030c100; + + winuser::SetLayeredWindowAttributes(real_window.0, color_key, opacity, winuser::LWA_ALPHA); } } From 97e47e0a0df206cf8940f12e5c340ee48e0de2b4 Mon Sep 17 00:00:00 2001 From: Alex Taylor Date: Mon, 15 Oct 2018 04:07:05 -0700 Subject: [PATCH 5/5] Add transparency fix to CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a88b63e3e0..7d14021f41 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ - Fixed UTF8 handling bug in X11 `set_title` function. - On Windows, `Window::set_cursor` now applies immediately instead of requiring specific events to occur first. - On Windows, fix window.set_maximized(). +- On Windows, fix transparency (#260). # Version 0.17.2 (2018-08-19)