Skip to content

Commit

Permalink
Merge pull request #16 from ahkohd/feat/monitor
Browse files Browse the repository at this point in the history
feat: docs
  • Loading branch information
ahkohd authored Feb 16, 2024
2 parents 949d4ed + 1e0f617 commit 5105a7f
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 4 deletions.
2 changes: 1 addition & 1 deletion libs/monitor/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "monitor"
version = "0.1.0"
description = "Utilies for monitors"
description = "A library to get information about monitors"
authors.workspace = true
license.workspace = true
edition.workspace = true
Expand Down
134 changes: 134 additions & 0 deletions libs/monitor/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
A library to get information about monitors.

### Install
_This lib requires a Rust version of at least **1.64**_

There are three general methods of installation that we can recommend.

1. Use crates.io and npm (easiest, and requires you to trust that our publishing pipeline worked)
2. Pull sources directly from Github using git tags / revision hashes (most secure)
3. Git submodule install this repo in your tauri project and then use file protocol to ingest the source (most secure, but inconvenient to use)

Install the Core plugin by adding the following to your `Cargo.toml` file:

`src-tauri/Cargo.toml`
```toml
[dependencies]
monitor = { git = "https://github.com/ahkohd/tauri-toolkit", branch = "main" }
```

## Usage
```rust
use monitor::{get_monitors, get_monitor_with_cursor};

fn main() {
let monitors = get_monitors();

let monitor_with_cursor = get_monitor_with_cursor();
}
```

## Functions

- `get_monitor_with_cursor() -> Option<Monitor>`:
Returns the monitor which currently hosts the system pointer, if any.

- `get_monitors() -> Vec<Monitor>`:
Returns a vector of all connected monitors.

### Monitor
The struct Monitor provides properties of a single display monitor, defined as follows:
```rust
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Monitor {
id: u32,
uuid: Option<String>,
name: Option<String>,
size: PhysicalSize<f64>,
position: PhysicalPosition<f64>,
scale_factor: f64,
has_cursor: bool,
is_primary: bool,
visible_area: VisibleArea,
}
```
It includes the following fields:
- `id`: a unique identifier for the monitor
- `uuid`: the UUID of the monitor, if any
- `name`: the name of the monitor, if available
- `size`: the size of the monitor, specified as a PhysicalSize struct
- `position`: the position of the monitor, specified as a PhysicalPosition struct
- `scale_factor`: the scaling factor of the monitor's resolution
- `has_cursor`: a Boolean flag indicating if the monitor currently has a cursor
- `is_primary`: a Boolean flag indicating if the monitor is the primary monitor
- `visible_area`: the visible area of the monitor

#### Monitor Methods

`Monitor` struct provides the following methods to fetch its attributes:

- `id(&self) -> u32`: This method returns the identifier of the monitor which is of type `u32`.

- `uuid(&self) -> Option<&String>`: This method returns an `Option` containing a reference to the UUID of the monitor, if it is assigned one.

- `name(&self) -> Option<&String>`: This method returns an `Option` containing a reference to the name of the monitor, if it is assigned one.

- `size(&self) -> PhysicalSize<f64>`: This method returns the size of the monitor as an instance of `PhysicalSize<f64>`.

- `visible_area(&self) -> VisibleArea`: This method returns the visible area of the monitor as a `VisibleArea` struct.

- `position(&self) -> PhysicalPosition<f64>`: This method returns the position of the monitor as an instance of `PhysicalPosition<f64>`.

- `scale_factor(&self) -> f64`: This method returns the scale factor of the monitor.

- `has_cursor(&self) -> bool`: This method returns a boolean value indicating whether the monitor currently has a cursor.

- `is_primary(&self) -> bool`: This method returns a boolean value indicating whether or not the monitor is the primary monitor.

To use any of these methods, you need to have an instance of a `Monitor`.

For example:
```rust
let monitor_id = monitor.id();
let monitor_uuid = monitor.uuid();
```

### VisibleArea

This visible area is represented by the struct `VisibleArea` defined as follows:

```rust
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct VisibleArea {
size: PhysicalSize<f64>,
position: PhysicalPosition<f64>,
}
```
It includes fields:
- `size`: the size of the visible area specified as a PhysicalSize struct containing width and height as f64.
- `position`: the position of the visible area on screen specified as a PhysicalPosition struct containing x and y as f64.

#### VisibleArea Methods

`VisibleArea` struct provides the following methods:

- `size(&self) -> PhysicalSize<f64>`: This method returns the size of the visible area as an instance of `PhysicalSize<f64>`.

- `position(&self) -> PhysicalPosition<f64>`: This method returns the position of the visible area on the screen as an instance of `PhysicalPosition<f64>`.

Each of these methods allow you to access specific attributes of the `VisibleArea`.

For example:
```rust
let visible_area_size = visible_area.size();
let visible_area_position = visible_area.position();
```

These methods are particularly useful when you need to query or manipulate the visible area of a screen. Just instantiate a `VisibleArea` and call the appropriate methods.

## Contributing

PRs accepted. Please make sure to read the Contributing Guide before making a pull request.

## License
MIT or MIT/Apache 2.0 where applicable
5 changes: 2 additions & 3 deletions libs/monitor/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use core_graphics::display::CGDirectDisplayID;
use serde::{Deserialize, Serialize};
use tauri::{PhysicalPosition, PhysicalSize};

Expand All @@ -23,7 +22,7 @@ impl VisibleArea {

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Monitor {
id: CGDirectDisplayID,
id: u32,
uuid: Option<String>,
name: Option<String>,
size: PhysicalSize<f64>,
Expand All @@ -35,7 +34,7 @@ pub struct Monitor {
}

impl Monitor {
pub fn id(&self) -> CGDirectDisplayID {
pub fn id(&self) -> u32 {
self.id
}

Expand Down

0 comments on commit 5105a7f

Please sign in to comment.