maycoon_core/app/mod.rs
1use crate::app::context::AppContext;
2use crate::app::runner::MayRunner;
3use crate::config::MayConfig;
4use crate::plugin::PluginManager;
5use crate::vgi::VectorGraphicsInterface;
6use crate::widget::Widget;
7use maycoon_theme::theme::Theme;
8
9/// Contains diagnostics data for the application.
10pub mod diagnostics;
11
12/// Contains the font context structure.
13pub mod font_ctx;
14
15/// Contains the application handler.
16pub mod handler;
17
18/// Contains the application information structure.
19pub mod info;
20
21/// Contains the update mode bitflag.
22pub mod update;
23
24/// Contains the [AppContext] structure for access to the application lifecycle.
25pub mod context;
26
27/// Contains the [MayRunner] structure to create and run an application using `winit`.
28pub mod runner;
29
30/// The main application interface.
31///
32/// Contains basic functions for the [MayRunner] to create and run an application.
33pub trait Application: Sized {
34 /// The theme of the application and its widgets.
35 ///
36 /// See [maycoon_theme::theme] for built-in themes.
37 type Theme: Theme;
38
39 // TODO: Change to default type, once (associated type defaults)[https://github.com/rust-lang/rust/issues/29661] is stabilized.
40 /// The vector graphics interface to use for rendering.
41 ///
42 /// See [VectorGraphicsInterface] for more.
43 type Graphics: VectorGraphicsInterface;
44
45 /// The global state of the application.
46 type State;
47
48 /// Renders/builds the application's widgets.
49 ///
50 /// This function will be passed to the [MayRunner] to create and run the application.
51 fn build(context: AppContext, state: Self::State) -> impl Widget;
52
53 /// Returns the [MayConfig] for the application.
54 fn config(&self) -> MayConfig<Self::Theme, Self::Graphics>;
55
56 /// Builds and returns the [PluginManager] for the application.
57 #[inline(always)]
58 fn plugins(&self) -> PluginManager<Self::Theme, Self::Graphics> {
59 PluginManager::new()
60 }
61
62 /// Initializes the backend application data.
63 ///
64 /// This function is called before the actual launch of the app.
65 ///
66 /// The default implementation just initializes the task runner.
67 #[inline(always)]
68 fn init(&self) {
69 #[cfg(feature = "tokio-runner")]
70 {
71 tracing::info!("initializing tokio task runner");
72 crate::tasks::init(crate::tasks::runner::TaskRunner::Tokio(
73 crate::tasks::runner::tokio::TaskRunner::new(
74 true, None, None, None, None, None, None, None,
75 ),
76 ));
77 }
78 }
79
80 /// Runs the application using the [MayRunner].
81 ///
82 /// Override this method if you want to use a custom event loop.
83 #[inline(always)]
84 fn run(self, state: Self::State) {
85 let config = self.config();
86
87 tracing::info_span!("init").in_scope(|| self.init());
88
89 tracing::info!("launching application runner with config {config:?}");
90 MayRunner::<Self::Theme, Self::Graphics>::new(config).run(
91 state,
92 Self::build,
93 self.plugins(),
94 );
95 }
96}