maycoon_core/
config.rs

1use nalgebra::{Point2, Vector2};
2use std::num::NonZeroUsize;
3use vello::util::DeviceHandle;
4pub use vello::AaConfig;
5pub use wgpu_types::PresentMode;
6pub use winit::window::{
7    BadIcon, Cursor, CursorIcon, CustomCursor, Icon as WindowIcon, WindowButtons, WindowLevel,
8};
9
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<T: Default + Theme> Default for MayConfig<T> {
26    fn default() -> Self {
27        Self {
28            window: WindowConfig::default(),
29            render: RenderConfig::default(),
30            tasks: None,
31            theme: T::default(),
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    /// The selector function to determine which device to use for rendering. Defaults to using the first device found.
122    pub device_selector: fn(&Vec<DeviceHandle>) -> &DeviceHandle,
123}
124
125impl Default for RenderConfig {
126    fn default() -> Self {
127        Self {
128            antialiasing: AaConfig::Area,
129            cpu: false,
130            present_mode: PresentMode::AutoNoVsync,
131            init_threads: None,
132            device_selector: |devices| devices.first().expect("No devices found"),
133        }
134    }
135}
136
137/// The window mode.
138#[derive(Clone, Debug, Default)]
139pub enum WindowMode {
140    /// The default windowed mode.
141    #[default]
142    Windowed,
143    /// Size the window to fill the screen and remove borders. This is more modern, than default Fullscreen.
144    Borderless,
145    /// Legacy Fullscreen mode.
146    Fullscreen,
147}
148
149/// Configuration structure for the integrated [TaskRunner](crate::tasks::TaskRunner).
150///
151/// The task runner isn't used by maycoon internally, but can be used to spawn asynchronous tasks and integrate them with the UI.
152#[derive(Clone, Debug, Eq, PartialEq)]
153pub struct TasksConfig {
154    /// The stack size of each thread of the task runner thread pool. Defaults to 1 MB.
155    pub stack_size: usize,
156    /// The amount of worker threads of the task runner thread pool. Defaults to half of the available threads.
157    pub workers: NonZeroUsize,
158}
159
160impl Default for TasksConfig {
161    fn default() -> Self {
162        Self {
163            stack_size: 1024 * 1024, // 1 MB
164            workers: NonZeroUsize::new(
165                std::thread::available_parallelism()
166                    .expect("Failed to get available threads")
167                    .get()
168                    / 2,
169            )
170            .unwrap(),
171        }
172    }
173}