Skip to content

Commit

Permalink
Merge pull request #109 from valenotary/106
Browse files Browse the repository at this point in the history
Replace Capturer::new with Capturer::build
  • Loading branch information
Brendonovich authored Nov 5, 2024
2 parents 691bd88 + 12cb7b8 commit 7339e29
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target
.DS_Store
/test
/.idea
26 changes: 26 additions & 0 deletions src/capturer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::sync::mpsc;

use crate::{
frame::{Frame, FrameType},
has_permission, is_supported, request_permission,
targets::Target,
};

Expand Down Expand Up @@ -73,15 +74,40 @@ pub struct Capturer {
rx: mpsc::Receiver<Frame>,
}

pub enum CapturerBuildError {
NotSupported,
PermissionNotGranted,
}

impl Capturer {
/// Create a new capturer instance with the provided options
#[deprecated(
since = "0.0.6",
note = "Use `build` instead of `new` to create a new capturer instance."
)]
pub fn new(options: Options) -> Capturer {
let (tx, rx) = mpsc::channel::<Frame>();
let engine = engine::Engine::new(&options, tx);

Capturer { engine, rx }
}

/// Build a new [Capturer] instance with the provided options
pub fn build(options: Options) -> Result<Capturer, CapturerBuildError> {
if !is_supported() {
return Err(CapturerBuildError::NotSupported);
}

if !has_permission() {
return Err(CapturerBuildError::PermissionNotGranted);
}

let (tx, rx) = mpsc::channel::<Frame>();
let engine = engine::Engine::new(&options, tx);

Ok(Capturer { engine, rx })
}

// TODO
// Prevent starting capture if already started
/// Start capturing the frames
Expand Down
10 changes: 7 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

use scap::{
capturer::{Area, Capturer, Options, Point, Size},
frame::Frame,
Target,
frame::Frame
,
};
use std::process;

fn main() {
// Check if the platform is supported
Expand Down Expand Up @@ -46,7 +47,10 @@ fn main() {
};

// Create Recorder with options
let mut recorder = Capturer::new(options);
let mut recorder = Capturer::build(options).unwrap_or_else(|err| {
println!("Problem with building Capturer: {err}");
process::exit(1);
});

// Start Capture
recorder.start_capture();
Expand Down

0 comments on commit 7339e29

Please sign in to comment.