maycoon_core/
config.rs

1use nalgebra::{Point2, Vector2};
2use std::num::NonZeroUsize;
3pub use vello::AaConfig;
4pub use wgpu_types::PresentMode;
5pub use winit::window::{
6    BadIcon, Cursor, CursorIcon, CustomCursor, Icon as WindowIcon, WindowButtons, WindowLevel,
7};
8
9use maycoon_theme::theme::celeste::CelesteTheme;
10use maycoon_theme::theme::Theme;
11
12/// Maycoon Application Configuration Structure.
13#[derive(Clone)]
14pub struct MayConfig<T: Theme> {
15    /// Window Configuration
16    pub window: WindowConfig,
17    /// Renderer Configuration.
18    pub render: RenderConfig,
19    /// Task Runner Configuration. If [`None`] (default), the task runner won't be enabled.
20    pub tasks: Option<TasksConfig>,
21    /// Theme of the Application.
22    pub theme: T,
23}
24
25impl Default for MayConfig<CelesteTheme> {
26    fn default() -> Self {
27        Self {
28            window: WindowConfig::default(),
29            render: RenderConfig::default(),
30            tasks: None,
31            theme: CelesteTheme::light(),
32        }
33    }
34}
35
36/// Window configuration.
37#[derive(Clone)]
38pub struct WindowConfig {
39    /// The title of the window.
40    pub title: String,
41    /// The inner size of the window.
42    pub size: Vector2<f64>,
43    /// The minimum size of the window.
44    pub min_size: Option<Vector2<f64>>,
45    /// The maximum size of the window.
46    pub max_size: Option<Vector2<f64>>,
47    /// If the window should be resizeable.
48    pub resizable: bool,
49    /// If the window should be maximized on startup.
50    pub maximized: bool,
51    /// The window mode.
52    pub mode: WindowMode,
53    /// The window level.
54    pub level: WindowLevel,
55    /// If the window should be visible on startup.
56    pub visible: bool,
57    /// If the window background should be blurred.
58    pub blur: bool,
59    /// If the window background should be transparent. May not be compatible on all system.
60    pub transparent: bool,
61    /// The desired initial position for the window.
62    pub position: Option<Point2<f64>>,
63    /// If the window should be active/focused on startup.
64    pub active: bool,
65    /// The enabled window buttons.
66    pub buttons: WindowButtons,
67    /// If the window should be decorated (have borders).
68    pub decorations: bool,
69    /// The resize increments of the window. Not supported everywhere.
70    pub resize_increments: Option<Vector2<f64>>,
71    /// Prevents window capturing by some apps (not all though).
72    pub content_protected: bool,
73    /// The window icon.
74    pub icon: Option<WindowIcon>,
75    /// The window cursor.
76    pub cursor: Cursor,
77    /// If the window should exit/close on close request (pressing the close window button).
78    pub close_on_request: bool,
79}
80
81impl Default for WindowConfig {
82    fn default() -> Self {
83        Self {
84            title: "New App".to_string(),
85            size: Vector2::new(800.0, 600.0),
86            min_size: None,
87            max_size: None,
88            resizable: true,
89            maximized: false,
90            mode: WindowMode::default(),
91            level: Default::default(),
92            visible: true,
93            blur: false,
94            transparent: false,
95            position: None,
96            active: true,
97            buttons: WindowButtons::all(),
98            decorations: true,
99            resize_increments: None,
100            content_protected: false,
101            icon: None,
102            cursor: Cursor::default(),
103            close_on_request: true,
104        }
105    }
106}
107
108/// Renderer configuration.
109#[derive(Clone)]
110pub struct RenderConfig {
111    /// The antialiasing config
112    pub antialiasing: AaConfig,
113    /// If the backend should use the CPU for most drawing operations.
114    ///
115    /// **NOTE:** The GPU is still used during rasterization.
116    pub cpu: bool,
117    /// The presentation mode of the window/surface.
118    pub present_mode: PresentMode,
119    /// The number of threads to use for initialization in [vello].
120    pub init_threads: Option<NonZeroUsize>,
121}
122
123impl Default for RenderConfig {
124    fn default() -> Self {
125        Self {
126            antialiasing: AaConfig::Area,
127            cpu: false,
128            present_mode: PresentMode::AutoNoVsync,
129            init_threads: None,
130        }
131    }
132}
133
134/// The window mode.
135#[derive(Clone, Debug, Default)]
136pub enum WindowMode {
137    /// The default windowed mode.
138    #[default]
139    Windowed,
140    /// Size the window to fill the screen and remove borders. This is more modern, than default Fullscreen.
141    Borderless,
142    /// Legacy Fullscreen mode.
143    Fullscreen,
144}
145
146/// Configuration structure for the integrated [`TaskRunner`](crate::tasks::TaskRunner).
147///
148/// The task runner isn't used by maycoon internally, but can be used to spawn asynchronous tasks and integrate them with the UI.
149#[derive(Clone, Debug, Eq, PartialEq)]
150pub struct TasksConfig {
151    /// The stack size of each thread of the task runner thread pool. Defaults to 1 MB.
152    pub stack_size: usize,
153    /// The amount of worker threads of the task runner thread pool. Defaults to half of the available threads.
154    pub workers: NonZeroUsize,
155}
156
157impl Default for TasksConfig {
158    fn default() -> Self {
159        Self {
160            stack_size: 1024 * 1024, // 1 MB
161            workers: NonZeroUsize::new(
162                std::thread::available_parallelism()
163                    .expect("Failed to get available threads")
164                    .get()
165                    / 2,
166            )
167            .unwrap(),
168        }
169    }
170}