windows_capture/
settings.rs

1use std::time::Duration;
2
3use windows::Graphics::Capture::GraphicsCaptureItem;
4
5use crate::monitor::Monitor;
6use crate::window::Window;
7
8/// An enumeration of the types of items that can be captured.
9pub enum CaptureItemTypes {
10    /// A display monitor.
11    Monitor(Monitor),
12    /// An application window.
13    Window(Window),
14}
15
16/// A trait for types that can be converted into a `GraphicsCaptureItem`.
17pub trait TryIntoCaptureItemWithType {
18    /// Attempts to convert the object into a `GraphicsCaptureItem` and its corresponding `CaptureItemTypes` variant.
19    fn try_into_capture_item(
20        self,
21    ) -> Result<(GraphicsCaptureItem, CaptureItemTypes), windows::core::Error>;
22}
23
24/// Specifies the pixel format for the captured frame.
25#[derive(Eq, PartialEq, Clone, Copy, Debug)]
26pub enum ColorFormat {
27    /// 16-bit floating-point RGBA format.
28    Rgba16F = 10,
29    /// 8-bit unsigned integer RGBA format.
30    Rgba8 = 28,
31    /// 8-bit unsigned integer BGRA format.
32    Bgra8 = 87,
33}
34
35impl Default for ColorFormat {
36    /// The default color format is `Rgba8`.
37    #[inline]
38    fn default() -> Self {
39        Self::Rgba8
40    }
41}
42
43/// Defines whether the cursor should be visible in the captured output.
44#[derive(Eq, PartialEq, Clone, Copy, Debug)]
45pub enum CursorCaptureSettings {
46    /// Use the system's default behavior for cursor visibility.
47    Default,
48    /// Ensure the cursor is always visible in the capture.
49    WithCursor,
50    /// Ensure the cursor is never visible in the capture.
51    WithoutCursor,
52}
53
54/// Defines whether a border should be drawn around the captured item.
55#[derive(Eq, PartialEq, Clone, Copy, Debug)]
56pub enum DrawBorderSettings {
57    /// Use the system's default behavior for the capture border.
58    Default,
59    /// Draw a border around the captured item.
60    WithBorder,
61    /// Do not draw a border around the captured item.
62    WithoutBorder,
63}
64
65/// Defines whether to include or exclude secondary windows in the capture.
66#[derive(Eq, PartialEq, Clone, Copy, Debug)]
67pub enum SecondaryWindowSettings {
68    /// Use the system's default behavior for capturing secondary windows.
69    Default,
70    /// Include secondary windows in the capture.
71    Include,
72    /// Exclude secondary windows from the capture.
73    Exclude,
74}
75
76/// Defines the minimum interval between frame updates.
77#[derive(Eq, PartialEq, Clone, Copy, Debug)]
78pub enum MinimumUpdateIntervalSettings {
79    /// Use the system's default update interval.
80    Default,
81    /// Specify a custom minimum update interval.
82    Custom(Duration),
83}
84
85/// Defines how the system should handle dirty regions, which are areas of the screen that have changed.
86#[derive(Eq, PartialEq, Clone, Copy, Debug)]
87pub enum DirtyRegionSettings {
88    /// Use the system's default behavior for dirty regions.
89    Default,
90    /// Only report the dirty regions without rendering them separately.
91    ReportOnly,
92    /// Report and render the dirty regions.
93    ReportAndRender,
94}
95
96/// Represents the settings for a screen capture session.
97#[derive(Eq, PartialEq, Clone, Debug)]
98pub struct Settings<Flags, T: TryIntoCaptureItemWithType> {
99    /// The item to be captured (e.g., a `Window` or `Monitor`).
100    pub(crate) item: T,
101    /// Specifies whether the cursor should be captured.
102    pub(crate) cursor_capture_settings: CursorCaptureSettings,
103    /// Specifies whether a border should be drawn around the captured item.
104    pub(crate) draw_border_settings: DrawBorderSettings,
105    /// Specifies whether to include secondary windows in the capture.
106    pub(crate) secondary_window_settings: SecondaryWindowSettings,
107    /// Specifies the minimum time between frame updates.
108    pub(crate) minimum_update_interval_settings: MinimumUpdateIntervalSettings,
109    /// Specifies how to handle dirty regions.
110    pub(crate) dirty_region_settings: DirtyRegionSettings,
111    /// The pixel format for the captured frames.
112    pub(crate) color_format: ColorFormat,
113    /// User-defined flags that can be passed to the capture implementation.
114    pub(crate) flags: Flags,
115}
116
117impl<Flags, T: TryIntoCaptureItemWithType> Settings<Flags, T> {
118    /// Creates a new `Settings` configuration.
119    ///
120    /// # Arguments
121    ///
122    /// * `item` - The item to be captured (a `Window` or `Monitor`).
123    /// * `cursor_capture_settings` - The desired cursor capture behavior.
124    /// * `draw_border_settings` - The desired border drawing behavior.
125    /// * `secondary_window_settings` - The desired behavior for capturing secondary windows.
126    /// * `minimum_update_interval_settings` - The desired minimum time between frame updates.
127    /// * `dirty_region_settings` - The desired behavior for handling dirty regions.
128    /// * `color_format` - The desired pixel format for the captured frames.
129    /// * `flags` - Custom flags to be passed to the capture implementation's `new` function.
130    #[must_use]
131    #[inline]
132    #[allow(clippy::too_many_arguments)]
133    pub const fn new(
134        item: T,
135        cursor_capture_settings: CursorCaptureSettings,
136        draw_border_settings: DrawBorderSettings,
137        secondary_window_settings: SecondaryWindowSettings,
138        minimum_update_interval_settings: MinimumUpdateIntervalSettings,
139        dirty_region_settings: DirtyRegionSettings,
140        color_format: ColorFormat,
141        flags: Flags,
142    ) -> Self {
143        Self {
144            item,
145            cursor_capture_settings,
146            draw_border_settings,
147            secondary_window_settings,
148            minimum_update_interval_settings,
149            dirty_region_settings,
150            color_format,
151            flags,
152        }
153    }
154
155    /// Returns a reference to the capture item.
156    ///
157    /// # Returns
158    ///
159    /// A reference to the item to be captured.
160    #[must_use]
161    #[inline]
162    pub const fn item(&self) -> &T {
163        &self.item
164    }
165
166    /// Returns the cursor capture settings.
167    ///
168    /// # Returns
169    ///
170    /// The current `CursorCaptureSettings`.
171    #[must_use]
172    #[inline]
173    pub const fn cursor_capture(&self) -> CursorCaptureSettings {
174        self.cursor_capture_settings
175    }
176
177    /// Returns the draw border settings.
178    ///
179    /// # Returns
180    ///
181    /// The current `DrawBorderSettings`.
182    #[must_use]
183    #[inline]
184    pub const fn draw_border(&self) -> DrawBorderSettings {
185        self.draw_border_settings
186    }
187
188    /// Returns the color format.
189    ///
190    /// # Returns
191    ///
192    /// The current `ColorFormat`.
193    #[must_use]
194    #[inline]
195    pub const fn color_format(&self) -> ColorFormat {
196        self.color_format
197    }
198
199    /// Returns a reference to the flags.
200    ///
201    /// # Returns
202    ///
203    /// A reference to the user-defined flags.
204    #[must_use]
205    #[inline]
206    pub const fn flags(&self) -> &Flags {
207        &self.flags
208    }
209}