@@ -25,68 +25,56 @@ import com.mayakapps.compose.windowstyler.windows.jna.enums.DwmWindowAttribute
2525import com.sun.jna.platform.win32.WinDef
2626
2727internal class WindowsBackdropApis (private val hwnd : WinDef .HWND ) {
28- private var isSystemBackdropSet = false
29- private var isMicaEnabled = false
30- private var isAccentPolicySet = false
31- private var isSheetOfGlassApplied = false
3228
33- fun setSystemBackdrop (systemBackdrop : DwmSystemBackdrop ) {
34- createSheetOfGlassEffect()
35- if (Dwm .setSystemBackdrop(hwnd, systemBackdrop)) {
36- isSystemBackdropSet = systemBackdrop == DwmSystemBackdrop .DWMSBT_DISABLE
37- if (isSystemBackdropSet) resetAccentPolicy()
29+ var systemBackdrop: DwmSystemBackdrop ? = null
30+ set(value) {
31+ requireNotNull(value)
32+ if (field == value) return
33+
34+ val result = Dwm .setSystemBackdrop(hwnd, value)
35+ if (result) field = value
3836 }
39- }
4037
41- fun setMicaEffectEnabled (enabled : Boolean ) {
42- createSheetOfGlassEffect()
43- if (Dwm .setWindowAttribute(hwnd, DwmWindowAttribute .DWMWA_MICA_EFFECT , enabled)) {
44- isMicaEnabled = enabled
45- if (isMicaEnabled) resetAccentPolicy()
38+ var isMicaEffectEnabled: Boolean? = null
39+ set(value) {
40+ requireNotNull(value)
41+ if (field == value) return
42+
43+ val result = Dwm .setWindowAttribute(hwnd, DwmWindowAttribute .DWMWA_MICA_EFFECT , value)
44+ if (result) field = value
45+ }
46+
47+ var isSheetOfGlassEffectEnabled: Boolean? = null
48+ set(value) {
49+ requireNotNull(value)
50+
51+ val result = if (value) {
52+ // Negative margins have special meaning to DwmExtendFrameIntoClientArea.
53+ // Negative margins create the "sheet of glass" effect, where the client area is
54+ // rendered as a solid surface with no window border.
55+ Dwm .extendFrameIntoClientArea(hwnd = hwnd, margin = - 1 )
56+ } else {
57+ // At least one margin should be non-negative in order to show the DWM window shadow
58+ // created by handling [WM_NCCALCSIZE]. Matching value with bitsdojo_window:
59+ // https://github.com/bitsdojo/bitsdojo_window/blob/adad0cd40be3d3e12df11d864f18a96a2d0fb4fb/bitsdojo_window_windows/windows/bitsdojo_window.cpp#L149
60+ Dwm .extendFrameIntoClientArea(
61+ hwnd = hwnd,
62+ leftWidth = 0 ,
63+ rightWidth = 0 ,
64+ topHeight = 1 ,
65+ bottomHeight = 0 ,
66+ )
67+ }
68+
69+ if (result) field = value
4670 }
47- }
4871
4972 fun setAccentPolicy (
5073 accentState : AccentState = AccentState .ACCENT_DISABLED ,
5174 accentFlags : Set <AccentFlag > = emptySet(),
5275 color : Int = 0,
5376 animationId : Int = 0,
5477 ) {
55- if (User32 .setAccentPolicy(hwnd, accentState, accentFlags, color, animationId)) {
56- isAccentPolicySet = accentState != AccentState .ACCENT_DISABLED
57- if (isAccentPolicySet) {
58- resetSystemBackdrop()
59- resetMicaEffectEnabled()
60- resetWindowFrame()
61- }
62- }
63- }
64-
65- fun createSheetOfGlassEffect () {
66- if (! isSheetOfGlassApplied && Dwm .extendFrameIntoClientArea(hwnd, - 1 )) isSheetOfGlassApplied = true
67- }
68-
69-
70- fun resetSystemBackdrop () {
71- if (isSystemBackdropSet) setSystemBackdrop(DwmSystemBackdrop .DWMSBT_DISABLE )
72- }
73-
74- fun resetMicaEffectEnabled () {
75- if (isMicaEnabled) setMicaEffectEnabled(false )
76- }
77-
78- fun resetAccentPolicy () {
79- if (isAccentPolicySet) setAccentPolicy(AccentState .ACCENT_DISABLED )
80- }
81-
82- fun resetWindowFrame () {
83- // At least one margin should be non-negative in order to show the DWM
84- // window shadow created by handling [WM_NCCALCSIZE].
85- //
86- // Matching value with bitsdojo_window.
87- // https://github.com/bitsdojo/bitsdojo_window/blob/adad0cd40be3d3e12df11d864f18a96a2d0fb4fb/bitsdojo_window_windows/windows/bitsdojo_window.cpp#L149
88- if (isSheetOfGlassApplied && Dwm .extendFrameIntoClientArea(hwnd, 0 , 0 , 1 , 0 )) {
89- isSheetOfGlassApplied = false
90- }
78+ User32 .setAccentPolicy(hwnd, accentState, accentFlags, color, animationId)
9179 }
92- }
80+ }
0 commit comments