Skip to content

Commit

Permalink
moving API doc from the README to the source code (#83)
Browse files Browse the repository at this point in the history
* moving API doc from the README to the source code

* fixing typo in code
  • Loading branch information
TornaxO7 authored Nov 9, 2023
1 parent 564b387 commit 2027514
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 16 deletions.
12 changes: 0 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,6 @@ fn main() {
}
```

## API

### `Screen`

The `Screen` struct represents a screen capturer and provides the following methods:

- `Screen::new(display_info)`: Get a screen from the [display info](https://docs.rs/display-info/latest/display_info/struct.DisplayInfo.html), returns a `Screen`.
- `Screen::all()`: Get all screens, returns `Result<Vec<Screen>>`.
- `Screen::from_point(x, y)`: Get a screen from a point, returns `Result<Screen>`.
- `screen.capture()`: Capture a screenshot of the screen, returns a [image](https://docs.rs/image/latest/image/type.RgbaImage.html) as `Result<RgbaImage>`.
- `screen.capture_area(x, y, width, height)`: Capture a screenshot of the designated area of the screen, returns the same as `capture()`.

## Linux Requirements

On Linux, you need to install `libxcb`, `libxrandr`, and `dbus`.
Expand Down
12 changes: 8 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,40 @@ mod linux;
#[cfg(target_os = "linux")]
use linux::*;

/// This struct represents a screen capturer.
#[derive(Debug, Clone, Copy)]
pub struct Screen {
pub display_info: DisplayInfo,
}

impl Screen {
/// Get a screen from the [display_info].
///
/// [display_info]: https://docs.rs/display-info/latest/display_info/struct.DisplayInfo.html
pub fn new(display_info: &DisplayInfo) -> Self {
Screen {
display_info: *display_info,
}
}

/// Return all available screens.
pub fn all() -> Result<Vec<Screen>> {
let screens = DisplayInfo::all()?.iter().map(Screen::new).collect();
Ok(screens)
}

/// Get a screen which includes the point with the given coordinates.
pub fn from_point(x: i32, y: i32) -> Result<Screen> {
let display_info = DisplayInfo::from_point(x, y)?;
Ok(Screen::new(&display_info))
}

/// Capture a screenshot of the screen.
pub fn capture(&self) -> Result<RgbaImage> {
capture_screen(&self.display_info)
}

/**
* 截取指定区域
* 区域x,y为相对于当前屏幕的x,y坐标
*/
/// Captures a screenshot of the designated area of the screen.
pub fn capture_area(&self, x: i32, y: i32, width: u32, height: u32) -> Result<RgbaImage> {
let display_info = self.display_info;
let screen_x2 = display_info.x + display_info.width as i32;
Expand Down

0 comments on commit 2027514

Please sign in to comment.