@@ -2,21 +2,22 @@ use crate::window_handler::OpenWindowExample;
22use crate :: ExamplePluginMainThread ;
33use baseview:: dpi:: * ;
44use baseview:: gl:: GlConfig ;
5- use baseview:: { WindowHandle , WindowOpenOptions , WindowSize } ;
5+ use baseview:: host:: { Host , HostCallbacks , HostMainThreadCaller } ;
6+ use baseview:: { HandlerError , WindowHandle , WindowOpenOptions , WindowSize } ;
67use clack_extensions:: gui:: {
7- AspectRatioStrategy , GuiApiType , GuiConfiguration , GuiResizeHints , GuiSize , PluginGuiImpl ,
8- Window as ClapWindow ,
8+ AspectRatioStrategy , GuiApiType , GuiConfiguration , GuiResizeHints , GuiSize , HostGui ,
9+ PluginGuiImpl , Window as ClapWindow ,
910} ;
1011use clack_plugin:: plugin:: PluginError ;
11-
12+ use clack_plugin :: prelude :: { HostMainThreadHandle , HostSharedHandle } ;
1213#[ allow( deprecated) ]
1314use raw_window_handle:: HasRawWindowHandle ;
1415
1516pub struct ExamplePluginGui {
1617 handle : WindowHandle ,
1718}
1819
19- impl PluginGuiImpl for ExamplePluginMainThread {
20+ impl PluginGuiImpl for ExamplePluginMainThread < ' _ > {
2021 fn is_api_supported ( & mut self , configuration : GuiConfiguration ) -> bool {
2122 !configuration. is_floating
2223 && Some ( configuration. api_type ) == GuiApiType :: default_for_current_platform ( )
@@ -97,7 +98,17 @@ impl PluginGuiImpl for ExamplePluginMainThread {
9798 . with_gl_config ( GlConfig :: default ( ) )
9899 . with_parent ( & parent) ;
99100
100- let window = baseview:: create_window ( options, OpenWindowExample :: new) ?;
101+ let mut host = Host :: new ( ) . with_main_thread ( unsafe {
102+ MainThreadHandler { host : self . host . shared ( ) . with_arbitrary_lifetime ( ) }
103+ } ) ;
104+
105+ if let Some ( gui) = self . host_gui {
106+ host = host. with_callbacks ( unsafe {
107+ HostGuiCallbacks { ext : gui, host : self . host . with_arbitrary_lifetime ( ) }
108+ } ) ;
109+ }
110+
111+ let window = baseview:: create_window_with_host ( options, OpenWindowExample :: new, host) ?;
101112
102113 self . gui = Some ( ExamplePluginGui { handle : window } ) ;
103114
@@ -145,3 +156,30 @@ fn gui_size_to_window_size(size: GuiSize) -> Size {
145156 Size :: Physical ( PhysicalSize :: new ( size. width , size. height ) )
146157 }
147158}
159+
160+ struct MainThreadHandler {
161+ host : HostSharedHandle < ' static > ,
162+ }
163+
164+ impl HostMainThreadCaller for MainThreadHandler {
165+ fn call_main_thread ( & mut self ) {
166+ self . host . request_callback ( ) ;
167+ }
168+ }
169+
170+ struct HostGuiCallbacks {
171+ ext : HostGui ,
172+ host : HostMainThreadHandle < ' static > ,
173+ }
174+
175+ impl HostCallbacks for HostGuiCallbacks {
176+ fn resized ( & mut self , new_size : WindowSize ) -> Result < ( ) , HandlerError > {
177+ let size = window_size_to_gui_size ( new_size) ;
178+ self . ext . request_resize ( & self . host , size. width , size. height ) ?;
179+ Ok ( ( ) )
180+ }
181+
182+ fn destroyed ( & mut self ) {
183+ self . ext . closed ( & self . host , true ) ;
184+ }
185+ }
0 commit comments