Skip to content

Commit

Permalink
Custom Error Type 🦖
Browse files Browse the repository at this point in the history
	modified:   Cargo.lock
	modified:   README.md
	modified:   examples/basic.rs
	modified:   src/capture.rs
	modified:   src/d3d11.rs
	modified:   src/frame.rs
	modified:   src/graphics_capture_api.rs
	modified:   src/lib.rs
	modified:   src/monitor.rs
	modified:   src/settings.rs
	modified:   src/window.rs
	modified:   windows-capture-python/Cargo.toml
	modified:   windows-capture-python/src/lib.rs
  • Loading branch information
NiiightmareXD committed Dec 6, 2023
1 parent cd77276 commit b565bce
Show file tree
Hide file tree
Showing 13 changed files with 390 additions and 421 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 25 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Windows Capture
![Crates.io](https://img.shields.io/crates/l/windows-capture) ![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/NiiightmareXD/windows-capture/rust.yml) ![Crates.io](https://img.shields.io/crates/v/windows-capture)
# Windows Capture   [![Licence]][repository] [![Build Status]][repository] [![Latest Version]][crates.io]

[Licence]: https://img.shields.io/crates/l/windows-capture
[Licence URL]: https://github.com/NiiightmareXD/windows-capture/blob/main/LICENCE

[Build Status]: https://img.shields.io/github/actions/workflow/status/NiiightmareXD/windows-capture/rust.yml
[repository]: https://github.com/NiiightmareXD/windows-capture

[Latest Version]: https://img.shields.io/crates/v/windows-capture
[crates.io]: https://crates.io/crates/windows-capture

**Windows Capture** is a highly efficient Rust and Python library that enables you to capture the screen using the Graphics Capture API effortlessly. This library allows you to easily capture the screen of your Windows-based computer and use it for various purposes, such as creating instructional videos, taking screenshots, or recording your gameplay. With its intuitive interface and robust functionality, Windows Capture is an excellent choice for anyone looking for a reliable, easy-to-use screen-capturing solution.

Expand Down Expand Up @@ -29,26 +37,28 @@ cargo add windows-capture
## Usage

```rust
use std::error::Error;

use windows_capture::{
capture::WindowsCaptureHandler,
frame::Frame,
graphics_capture_api::InternalCaptureControl,
settings::{ColorFormat, WindowsCaptureSettings},
settings::{ColorFormat, Settings},
window::Window,
};

// Struct To Implement Methods For
// Struct To Implement The Trait For
struct Capture;

impl WindowsCaptureHandler for Capture {
type Flags = String; // To Get The Message From The Settings
// To Get The Message From The Settings
type Flags = String;

// To Redirect To CaptureControl Or Start Method
type Error = Box<dyn std::error::Error + Send + Sync>;

// Function That Will Be Called To Create The Struct The Flags Can Be Passed
// From Settings
fn new(message: Self::Flags) -> Result<Self, Box<dyn Error + Send + Sync>> {
println!("Got The Message: {message}");
// From `WindowsCaptureSettings`
fn new(message: Self::Flags) -> Result<Self, Self::Error> {
println!("Got The Flag: {message}");

Ok(Self {})
}
Expand All @@ -58,7 +68,7 @@ impl WindowsCaptureHandler for Capture {
&mut self,
frame: &mut Frame,
capture_control: InternalCaptureControl,
) -> Result<(), Box<dyn Error + Send + Sync>> {
) -> Result<(), Self::Error> {
println!("New Frame Arrived");

// Save The Frame As An Image To The Specified Path
Expand All @@ -72,7 +82,7 @@ impl WindowsCaptureHandler for Capture {

// Called When The Capture Item Closes Usually When The Window Closes, Capture
// Session Will End After This Function Ends
fn on_closed(&mut self) -> Result<(), Box<dyn Error + Send + Sync>> {
fn on_closed(&mut self) -> Result<(), Self::Error> {
println!("Capture Session Closed");

Ok(())
Expand All @@ -81,9 +91,9 @@ impl WindowsCaptureHandler for Capture {

fn main() {
// Checkout Docs For Other Capture Items
let foreground_window = Window::foreground().unwrap();
let foreground_window = Window::foreground().expect("No Active Window Found");

let settings = WindowsCaptureSettings::new(
let settings = Settings::new(
// Item To Captue
foreground_window,
// Capture Cursor
Expand All @@ -97,7 +107,7 @@ fn main() {
)
.unwrap();

// Every Error From on_closed and on_frame_arrived Will End Up Here
// Every Error From `new`, `on_frame_arrived` and `on_closed` Will End Up Here
Capture::start(settings).unwrap();
}
```
Expand Down
28 changes: 15 additions & 13 deletions examples/basic.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
use std::error::Error;

use windows_capture::{
capture::WindowsCaptureHandler,
frame::Frame,
graphics_capture_api::InternalCaptureControl,
settings::{ColorFormat, WindowsCaptureSettings},
settings::{ColorFormat, Settings},
window::Window,
};

// Struct To Implement Methods For
// Struct To Implement The Trait For
struct Capture;

impl WindowsCaptureHandler for Capture {
type Flags = String; // To Get The Message From The Settings
// To Get The Message From The Settings
type Flags = String;

// To Redirect To CaptureControl Or Start Method
type Error = Box<dyn std::error::Error + Send + Sync>;

// Function That Will Be Called To Create The Struct The Flags Can Be Passed
// From Settings
fn new(message: Self::Flags) -> Result<Self, Box<dyn Error + Send + Sync>> {
println!("Got The Message: {message}");
// From `WindowsCaptureSettings`
fn new(message: Self::Flags) -> Result<Self, Self::Error> {
println!("Got The Flag: {message}");

Ok(Self {})
}
Expand All @@ -27,7 +29,7 @@ impl WindowsCaptureHandler for Capture {
&mut self,
frame: &mut Frame,
capture_control: InternalCaptureControl,
) -> Result<(), Box<dyn Error + Send + Sync>> {
) -> Result<(), Self::Error> {
println!("New Frame Arrived");

// Save The Frame As An Image To The Specified Path
Expand All @@ -41,7 +43,7 @@ impl WindowsCaptureHandler for Capture {

// Called When The Capture Item Closes Usually When The Window Closes, Capture
// Session Will End After This Function Ends
fn on_closed(&mut self) -> Result<(), Box<dyn Error + Send + Sync>> {
fn on_closed(&mut self) -> Result<(), Self::Error> {
println!("Capture Session Closed");

Ok(())
Expand All @@ -50,9 +52,9 @@ impl WindowsCaptureHandler for Capture {

fn main() {
// Checkout Docs For Other Capture Items
let foreground_window = Window::foreground().unwrap();
let foreground_window = Window::foreground().expect("No Active Window Found");

let settings = WindowsCaptureSettings::new(
let settings = Settings::new(
// Item To Captue
foreground_window,
// Capture Cursor
Expand All @@ -66,6 +68,6 @@ fn main() {
)
.unwrap();

// Every Error From on_closed and on_frame_arrived Will End Up Here
// Every Error From `new`, `on_frame_arrived` and `on_closed` Will End Up Here
Capture::start(settings).unwrap();
}
Loading

0 comments on commit b565bce

Please sign in to comment.