From bd4ac06c3e0437b8e318e4bc84321c2fc9567dcc Mon Sep 17 00:00:00 2001 From: Kenny Kerr Date: Thu, 28 Apr 2022 09:33:17 -0700 Subject: [PATCH] compose --- crates/libs/implement/src/lib.rs | 3 ++- crates/samples/xaml_app/src/main.rs | 12 +++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/crates/libs/implement/src/lib.rs b/crates/libs/implement/src/lib.rs index 2978393fc5..8e435b894a 100644 --- a/crates/libs/implement/src/lib.rs +++ b/crates/libs/implement/src/lib.rs @@ -143,7 +143,8 @@ pub fn implement(attributes: proc_macro::TokenStream, original_type: proc_macro: impl <#constraints> ::windows::core::Compose for #original_ident::<#(#generics,)*> { unsafe fn compose<'a>(implementation: Self) -> (::windows::core::IInspectable, &'a mut ::core::option::Option<::windows::core::IInspectable>) { let inspectable: ::windows::core::IInspectable = implementation.into(); - let this = (&inspectable as *const _ as *mut ::windows::core::RawPtr).sub(1) as *mut #impl_ident::<#(#generics,)*>; + let this: ::windows::core::RawPtr = ::core::mem::transmute_copy(&inspectable); + let this = (this as *mut ::windows::core::RawPtr).sub(1) as *mut #impl_ident::<#(#generics,)*>; (inspectable, &mut (*this).base) } } diff --git a/crates/samples/xaml_app/src/main.rs b/crates/samples/xaml_app/src/main.rs index f6c8200a93..701ac4a6cb 100644 --- a/crates/samples/xaml_app/src/main.rs +++ b/crates/samples/xaml_app/src/main.rs @@ -14,12 +14,18 @@ use windows::{ }; #[implement(IApplicationOverrides)] -struct MyApp(); +struct MyApp { + text: &'static str, + placeholder: &'static str, +} impl IApplicationOverrides_Impl for MyApp { fn OnLaunched(&self, _: &Option) -> Result<()> { let window = Window::Current()?; - window.SetContent(TextBox::new()?)?; + let text_box = TextBox::new()?; + text_box.SetText(self.text)?; + text_box.SetPlaceholderText(self.placeholder)?; + window.SetContent(text_box)?; window.Activate() } } @@ -35,7 +41,7 @@ fn main() -> Result<()> { } Application::Start(ApplicationInitializationCallback::new(|_| { - Application::compose(MyApp())?; + Application::compose(MyApp { text: "Hello world", placeholder: "What are you going to build today?" })?; Ok(()) })) }