1pub mod reexports {
2 pub use winit;
3}
4
5use std::sync::Arc;
6
7use crate::{
8 config::LaunchConfig,
9 renderer::{
10 LaunchProxy,
11 NativeEvent,
12 NativeGenericEvent,
13 WinitRenderer,
14 },
15};
16mod accessibility;
17pub mod config;
18mod drivers;
19pub mod extensions;
20pub mod integration;
21pub mod plugins;
22pub mod renderer;
23#[cfg(feature = "tray")]
24mod tray_icon;
25mod window;
26mod winit_mappings;
27
28pub use extensions::*;
29use futures_util::task::{
30 ArcWake,
31 waker,
32};
33
34use crate::winit::event_loop::EventLoopProxy;
35
36pub mod winit {
37 pub use winit::*;
38}
39
40#[cfg(feature = "tray")]
41pub mod tray {
42 pub use tray_icon::*;
43
44 pub use crate::tray_icon::*;
45}
46
47pub fn launch(mut launch_config: LaunchConfig) {
52 use std::collections::HashMap;
53
54 use freya_core::integration::*;
55 use freya_engine::prelude::{
56 FontCollection,
57 FontMgr,
58 TypefaceFontProvider,
59 };
60 use winit::event_loop::EventLoop;
61
62 #[cfg(all(not(debug_assertions), not(target_os = "android")))]
63 {
64 let previous_hook = std::panic::take_hook();
65 std::panic::set_hook(Box::new(move |panic_info| {
66 rfd::MessageDialog::new()
67 .set_title("Fatal Error")
68 .set_description(&panic_info.to_string())
69 .set_level(rfd::MessageLevel::Error)
70 .show();
71 previous_hook(panic_info);
72 std::process::exit(1);
73 }));
74 }
75
76 let event_loop = launch_config.event_loop.take().unwrap_or_else(|| {
77 EventLoop::<NativeEvent>::with_user_event()
78 .build()
79 .expect("Failed to create event loop.")
80 });
81
82 let proxy = event_loop.create_proxy();
83
84 let mut font_collection = FontCollection::new();
85 let def_mgr = FontMgr::default();
86 let mut provider = TypefaceFontProvider::new();
87 for (font_name, font_data) in launch_config.embedded_fonts {
88 let ft_type = def_mgr
89 .new_from_data(&font_data, None)
90 .unwrap_or_else(|| panic!("Failed to load font {font_name}."));
91 provider.register_typeface(ft_type, Some(font_name.as_ref()));
92 }
93 let font_mgr: FontMgr = provider.into();
94 font_collection.set_default_font_manager(def_mgr, None);
95 font_collection.set_dynamic_font_manager(font_mgr.clone());
96 font_collection.paragraph_cache_mut().turn_on(false);
97
98 let screen_reader = ScreenReader::new();
99
100 struct FuturesWaker(EventLoopProxy<NativeEvent>);
101
102 impl ArcWake for FuturesWaker {
103 fn wake_by_ref(arc_self: &Arc<Self>) {
104 _ = arc_self
105 .0
106 .send_event(NativeEvent::Generic(NativeGenericEvent::PollFutures));
107 }
108 }
109
110 let waker = waker(Arc::new(FuturesWaker(proxy.clone())));
111
112 #[cfg(feature = "hotreload")]
113 freya_core::hotreload::connect_subsecond();
114
115 let mut renderer = WinitRenderer {
116 windows: HashMap::default(),
117 #[cfg(feature = "tray")]
118 tray: launch_config.tray,
119 #[cfg(all(feature = "tray", not(target_os = "linux")))]
120 tray_icon: None,
121 resumed: false,
122 futures: launch_config
123 .tasks
124 .into_iter()
125 .map(|task| task(LaunchProxy(proxy.clone())))
126 .collect::<Vec<_>>(),
127 proxy,
128 font_manager: font_mgr,
129 font_collection,
130 windows_configs: launch_config.windows_configs,
131 plugins: launch_config.plugins,
132 fallback_fonts: launch_config.fallback_fonts,
133 screen_reader,
134 waker,
135 exit_on_close: launch_config.exit_on_close,
136 };
137
138 #[cfg(feature = "tray")]
139 {
140 use crate::{
141 renderer::{
142 NativeTrayEvent,
143 NativeTrayEventAction,
144 },
145 tray::{
146 TrayIconEvent,
147 menu::MenuEvent,
148 },
149 };
150
151 let proxy = renderer.proxy.clone();
152 MenuEvent::set_event_handler(Some(move |event| {
153 let _ = proxy.send_event(NativeEvent::Tray(NativeTrayEvent {
154 action: NativeTrayEventAction::MenuEvent(event),
155 }));
156 }));
157 let proxy = renderer.proxy.clone();
158 TrayIconEvent::set_event_handler(Some(move |event| {
159 let _ = proxy.send_event(NativeEvent::Tray(NativeTrayEvent {
160 action: NativeTrayEventAction::TrayEvent(event),
161 }));
162 }));
163
164 #[cfg(target_os = "linux")]
165 if let Some(tray_icon) = renderer.tray.0.take() {
166 std::thread::spawn(move || {
167 if !gtk::is_initialized() {
168 if gtk::init().is_ok() {
169 tracing::debug!("Tray: GTK initialized");
170 } else {
171 tracing::error!("Tray: Failed to initialize GTK");
172 }
173 }
174
175 let _tray_icon = (tray_icon)();
176
177 gtk::main();
178 });
179 }
180 }
181
182 event_loop.run_app(&mut renderer).unwrap();
183}