|
| 1 | +use std::cell::RefCell; |
| 2 | + |
| 3 | +use windows::{ |
| 4 | + UI::Color, |
| 5 | + core::{IInspectable_Vtbl, Interface, Ref, Result, imp::WeakRefCount, implement}, |
| 6 | +}; |
| 7 | +use winio_primitive::ColorTheme; |
| 8 | +use winui3::{ |
| 9 | + ChildClass, Compose, CreateInstanceFn, |
| 10 | + Microsoft::UI::{ |
| 11 | + Composition::{ |
| 12 | + ICompositionSupportsSystemBackdrop, |
| 13 | + SystemBackdrops::{DesktopAcrylicController, SystemBackdropConfiguration}, |
| 14 | + }, |
| 15 | + Xaml::{ |
| 16 | + self as MUX, |
| 17 | + Media::{ |
| 18 | + ISystemBackdropFactory, ISystemBackdropOverrides, ISystemBackdropOverrides_Impl, |
| 19 | + SystemBackdrop, |
| 20 | + }, |
| 21 | + }, |
| 22 | + }, |
| 23 | +}; |
| 24 | + |
| 25 | +use crate::color_theme; |
| 26 | + |
| 27 | +// Magic colors to match Win32. |
| 28 | +const fn color(dark: bool) -> Color { |
| 29 | + if dark { |
| 30 | + Color { |
| 31 | + A: 0xFF, |
| 32 | + R: 0x54, |
| 33 | + G: 0x54, |
| 34 | + B: 0x54, |
| 35 | + } |
| 36 | + } else { |
| 37 | + Color { |
| 38 | + A: 0xFF, |
| 39 | + R: 0xD3, |
| 40 | + G: 0xD3, |
| 41 | + B: 0xD3, |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +fn update_color(controller: &DesktopAcrylicController) -> Result<()> { |
| 47 | + let color = color(color_theme() == ColorTheme::Dark); |
| 48 | + controller.SetTintColor(color)?; |
| 49 | + controller.SetFallbackColor(color)?; |
| 50 | + Ok(()) |
| 51 | +} |
| 52 | + |
| 53 | +struct CustomDesktopAcrylicBackdropControllerEntry { |
| 54 | + controller: DesktopAcrylicController, |
| 55 | + target: ICompositionSupportsSystemBackdrop, |
| 56 | +} |
| 57 | + |
| 58 | +impl CustomDesktopAcrylicBackdropControllerEntry { |
| 59 | + pub fn new( |
| 60 | + target: ICompositionSupportsSystemBackdrop, |
| 61 | + controller: DesktopAcrylicController, |
| 62 | + configuration: &SystemBackdropConfiguration, |
| 63 | + ) -> Result<Self> { |
| 64 | + controller.AddSystemBackdropTarget(&target)?; |
| 65 | + controller.SetSystemBackdropConfiguration(configuration)?; |
| 66 | + Ok(Self { target, controller }) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +impl Drop for CustomDesktopAcrylicBackdropControllerEntry { |
| 71 | + fn drop(&mut self) { |
| 72 | + self.controller |
| 73 | + .RemoveSystemBackdropTarget(&self.target) |
| 74 | + .ok(); |
| 75 | + self.controller.Close().ok(); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +#[implement(ISystemBackdropOverrides, Agile = false)] |
| 80 | +pub struct CustomDesktopAcrylicBackdrop { |
| 81 | + controllers: RefCell<Vec<CustomDesktopAcrylicBackdropControllerEntry>>, |
| 82 | +} |
| 83 | + |
| 84 | +impl CustomDesktopAcrylicBackdrop { |
| 85 | + pub fn compose() -> Result<SystemBackdrop> { |
| 86 | + Compose::compose(Self { |
| 87 | + controllers: RefCell::new(vec![]), |
| 88 | + }) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +impl ISystemBackdropOverrides_Impl for CustomDesktopAcrylicBackdrop_Impl { |
| 93 | + fn OnTargetConnected( |
| 94 | + &self, |
| 95 | + target: Ref<ICompositionSupportsSystemBackdrop>, |
| 96 | + root: Ref<MUX::XamlRoot>, |
| 97 | + ) -> Result<()> { |
| 98 | + let base = unsafe { Compose::<CustomDesktopAcrylicBackdrop>::base(self) }; |
| 99 | + let target = target.ok()?; |
| 100 | + let root = root.ok()?; |
| 101 | + |
| 102 | + unsafe { |
| 103 | + let base = base.cast::<ISystemBackdropOverrides>()?; |
| 104 | + (base.vtable().OnTargetConnected)(base.as_raw(), target.as_raw(), root.as_raw()) |
| 105 | + .ok()?; |
| 106 | + } |
| 107 | + |
| 108 | + let configuration = base |
| 109 | + .cast::<SystemBackdrop>()? |
| 110 | + .GetDefaultSystemBackdropConfiguration(target, root)?; |
| 111 | + let controller = DesktopAcrylicController::new()?; |
| 112 | + // Magic number to match Win32. |
| 113 | + controller.SetLuminosityOpacity(0.65)?; |
| 114 | + update_color(&controller)?; |
| 115 | + |
| 116 | + self.controllers |
| 117 | + .borrow_mut() |
| 118 | + .push(CustomDesktopAcrylicBackdropControllerEntry::new( |
| 119 | + target.clone(), |
| 120 | + controller, |
| 121 | + &configuration, |
| 122 | + )?); |
| 123 | + Ok(()) |
| 124 | + } |
| 125 | + |
| 126 | + fn OnTargetDisconnected(&self, target: Ref<ICompositionSupportsSystemBackdrop>) -> Result<()> { |
| 127 | + let base = unsafe { Compose::<CustomDesktopAcrylicBackdrop>::base(self) }; |
| 128 | + let target = target.ok()?; |
| 129 | + unsafe { |
| 130 | + let base = base.cast::<ISystemBackdropOverrides>()?; |
| 131 | + (base.vtable().OnTargetDisconnected)(base.as_raw(), target.as_raw()).ok()?; |
| 132 | + } |
| 133 | + let mut controllers = self.controllers.borrow_mut(); |
| 134 | + if let Some(pos) = controllers.iter().position(|entry| entry.target == *target) { |
| 135 | + controllers.remove(pos); |
| 136 | + } |
| 137 | + Ok(()) |
| 138 | + } |
| 139 | + |
| 140 | + fn OnDefaultSystemBackdropConfigurationChanged( |
| 141 | + &self, |
| 142 | + target: Ref<ICompositionSupportsSystemBackdrop>, |
| 143 | + root: Ref<MUX::XamlRoot>, |
| 144 | + ) -> Result<()> { |
| 145 | + let base = unsafe { Compose::<CustomDesktopAcrylicBackdrop>::base(self) }; |
| 146 | + let target = target.ok()?; |
| 147 | + let root = root.ok()?; |
| 148 | + unsafe { |
| 149 | + let base = base.cast::<ISystemBackdropOverrides>()?; |
| 150 | + (base.vtable().OnDefaultSystemBackdropConfigurationChanged)( |
| 151 | + base.as_raw(), |
| 152 | + target.as_raw(), |
| 153 | + root.as_raw(), |
| 154 | + ) |
| 155 | + .ok()?; |
| 156 | + } |
| 157 | + for entry in self.controllers.borrow().iter() { |
| 158 | + if entry.target == *target { |
| 159 | + update_color(&entry.controller)?; |
| 160 | + break; |
| 161 | + } |
| 162 | + } |
| 163 | + Ok(()) |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +impl ChildClass for CustomDesktopAcrylicBackdrop { |
| 168 | + type BaseType = SystemBackdrop; |
| 169 | + type FactoryInterface = ISystemBackdropFactory; |
| 170 | + |
| 171 | + fn create_interface_fn( |
| 172 | + vtable: &<Self::FactoryInterface as Interface>::Vtable, |
| 173 | + ) -> CreateInstanceFn { |
| 174 | + vtable.CreateInstance |
| 175 | + } |
| 176 | + |
| 177 | + fn identity_vtable(vtable: &mut Self::Outer) -> &mut &'static IInspectable_Vtbl { |
| 178 | + &mut vtable.identity |
| 179 | + } |
| 180 | + |
| 181 | + fn ref_count(vtable: &Self::Outer) -> &WeakRefCount { |
| 182 | + &vtable.count |
| 183 | + } |
| 184 | + |
| 185 | + fn into_outer(self) -> Self::Outer { |
| 186 | + Self::into_outer(self) |
| 187 | + } |
| 188 | +} |
0 commit comments