Skip to main content

windows_capture/
settings.rs

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