Skip to content

Commit

Permalink
Better Error Handling 💥
Browse files Browse the repository at this point in the history
	modified:   Cargo.lock
	modified:   Cargo.toml
	modified:   src/capture.rs
	modified:   src/window.rs
  • Loading branch information
NiiightmareXD committed Sep 22, 2023
1 parent 7877b60 commit 24fdd99
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "windows-capture"
version = "1.0.14"
version = "1.0.15"
authors = ["NiiightmareXD"]
edition = "2021"
description = "Windows Capture Simple Screen Capture for Windows 🔥"
Expand Down
3 changes: 0 additions & 3 deletions src/capture.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::sync::Arc;

use log::error;
use parking_lot::Mutex;
use thiserror::Error;
use windows::{
Expand Down Expand Up @@ -110,8 +109,6 @@ impl WindowsCapture {
_item.Closed(
&TypedEventHandler::<GraphicsCaptureItem, IInspectable>::new({
move |_, _| {
error!("Graphics Capture Item Closed Impossible to Continue Capturing");

trigger_item.lock().on_closed();

unsafe { PostQuitMessage(0) };
Expand Down
18 changes: 16 additions & 2 deletions src/window.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use log::warn;
use thiserror::Error;
use windows::{
core::HSTRING,
Graphics::Capture::GraphicsCaptureItem,
Expand All @@ -15,6 +16,15 @@ use windows::{
},
};

/// Used To Handle Internal Errors
#[derive(Error, Debug)]
pub enum WindowErrors {
#[error("Failed To Find Window")]
NotFound,
#[error("Unknown Error")]
Unknown,
}

/// Represents A Windows
pub struct Window {
window: HWND,
Expand All @@ -33,11 +43,15 @@ impl Window {
}

/// Create From A Window Name
pub fn from_window_name(title: &str) -> Self {
pub fn from_window_name(title: &str) -> Result<Self, Box<dyn std::error::Error>> {
let title = HSTRING::from(title);
let window = unsafe { FindWindowW(None, &title) };

Self { window }
if window.0 == 0 {
return Err(Box::new(WindowErrors::NotFound));
}

Ok(Self { window })
}

/// Get Window Title
Expand Down

0 comments on commit 24fdd99

Please sign in to comment.