b3_core/
application.rs

1//! This module contains a platform independent Application implementation.
2use crate::{
3    platform::{ActiveApplicationApi, ApplicationApi, Wrapper},
4    platform_impl::{ActiveApplicationImpl, ApplicationImpl, ContextImpl},
5    Error,
6    EventHandler,
7    Icon,
8    Menu,
9};
10
11/// Native context.
12#[derive(Debug, Clone)]
13pub struct Context(ContextImpl);
14
15impl Context {
16    pub(crate) fn new(ctx: ContextImpl) -> Self { Self(ctx) }
17}
18
19impl Wrapper<ContextImpl> for Context {
20    #[inline]
21    fn get_impl(&self) -> &ContextImpl { &self.0 }
22
23    #[inline]
24    fn get_impl_mut(&mut self) -> &mut ContextImpl { &mut self.0 }
25}
26
27/// Context owner.
28pub trait ContextOwner {
29    /// Returns a system context.
30    fn context(&self) -> &Context;
31}
32
33/// This structure represents a platform independent running application.
34#[derive(Debug)]
35pub struct ActiveApplication(ActiveApplicationImpl);
36
37impl ActiveApplication {
38    pub(crate) fn new(app_impl: ActiveApplicationImpl) -> Self { Self(app_impl) }
39
40    /// Sets an application icon.
41    ///
42    /// # Parameters:
43    /// * `icon` - Icon.
44    pub fn set_icon(&mut self, icon: Option<&Icon>) { self.0.set_icon(icon); }
45
46    /// Sets an application menu.
47    ///
48    /// # Parameters:
49    /// * `menu` - Application menu.
50    pub fn set_menu(&mut self, menu: Option<&Menu>) { self.0.set_menu(menu); }
51
52    /// Stops a running applicaiton.
53    pub fn stop(&mut self) { self.0.stop(); }
54}
55
56impl Wrapper<ActiveApplicationImpl> for ActiveApplication {
57    #[inline]
58    fn get_impl(&self) -> &ActiveApplicationImpl { &self.0 }
59
60    #[inline]
61    fn get_impl_mut(&mut self) -> &mut ActiveApplicationImpl { &mut self.0 }
62}
63
64impl ContextOwner for ActiveApplication {
65    fn context(&self) -> &Context { self.0.context() }
66}
67
68unsafe impl Sync for ActiveApplication {}
69
70/// The main entity that provides entrypoints to the event loop and other API.
71///
72/// Any program that uses the **b3-core** crate must create an instance of
73/// this structure before using any other crate's entities.
74#[derive(Debug)]
75pub struct Application(ApplicationImpl);
76
77impl Application {
78    /// Creates a new [Application] instance.
79    ///
80    /// # Examples:
81    ///
82    /// ```rust
83    /// use b3_core::Application;
84    ///
85    /// let app = Application::new().unwrap();
86    /// ```
87    pub fn new() -> Result<Self, Error> { Ok(Self(ApplicationImpl::new()?)) }
88
89    /// Runs an application (event loop).
90    ///
91    /// # Parameters:
92    /// * `handler` - Event handler.
93    pub fn run(mut self, handler: impl EventHandler + 'static) { self.0.run(handler); }
94}
95
96impl Wrapper<ApplicationImpl> for Application {
97    #[inline]
98    fn get_impl(&self) -> &ApplicationImpl { &self.0 }
99
100    #[inline]
101    fn get_impl_mut(&mut self) -> &mut ApplicationImpl { &mut self.0 }
102}
103
104impl ContextOwner for Application {
105    fn context(&self) -> &Context { self.0.context() }
106}