From d746bfb344455bba15c28447b0a1d3db77b269fa Mon Sep 17 00:00:00 2001 From: Andrei Daniel Petrica Date: Thu, 26 Sep 2024 23:59:50 +0200 Subject: [PATCH 1/8] added getCenterOfActiveScreen in Screen class Added function that Returns the center of the screen where the mouse pointer is placed. Useful if you want to initialise a smaller window and place it exactly in the center of screen. --- src/Screen.php | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/Screen.php b/src/Screen.php index dfd98bb..4ea3a9f 100644 --- a/src/Screen.php +++ b/src/Screen.php @@ -17,4 +17,40 @@ public function displays(): array { return $this->client->get('screen/displays')->json('displays'); } + + /** + * Returns the center of the screen where the mouse pointer is placed. + * + * @return array + */ + public function getCenterOfActiveScreen(): array + { + //get the screen size and current cursor position. + $cursor = $this->cursorPosition(); + $screen = $this->displays(); + + /* Navigate every screen and check for cursor position against the bounds of the screen. */ + foreach ($screen as $s) { + /** @var array $bounds */ + $bounds = $s['bounds']; + if ( + $cursor->x >= $bounds['x'] + && $cursor->x <= ($bounds['x'] + $bounds['width']) + && $cursor->y >= $bounds['y'] + && $cursor->y <= ($bounds['y'] + $bounds['height']) + ) { + // I'm inside the display + return [ + 'x' => $bounds['x'] + $bounds['width'] / 2, + 'y' => $bounds['y'] + $bounds['height'] / 2, + ]; + } + } + $bounds = $screen[0]['bounds']; + + return [ + 'x' => $bounds['x'] + $bounds['width'] / 2, + 'y' => $bounds['y'] + $bounds['height'] / 2, + ]; + } } From ab5c8fe1afd5c7f0d1c525153f390d079eed2bd2 Mon Sep 17 00:00:00 2001 From: Simon Hamp Date: Fri, 18 Oct 2024 14:08:10 +0100 Subject: [PATCH 2/8] Add primary and active methods --- src/Screen.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Screen.php b/src/Screen.php index 4ea3a9f..a8b2ca3 100644 --- a/src/Screen.php +++ b/src/Screen.php @@ -18,6 +18,16 @@ public function displays(): array return $this->client->get('screen/displays')->json('displays'); } + public function primary(): object + { + return $this->client->get('screen/primary-display')->json('primaryDisplay'); + } + + public function active(): object + { + return $this->client->get('screen/active')->json(); + } + /** * Returns the center of the screen where the mouse pointer is placed. * From fdfec9dffe60478851b99aaee7374897e95e2578 Mon Sep 17 00:00:00 2001 From: Andrei Daniel Petrica Date: Sat, 19 Oct 2024 14:17:49 +0200 Subject: [PATCH 3/8] wip --- src/Facades/Screen.php | 1 + src/Screen.php | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Facades/Screen.php b/src/Facades/Screen.php index fe917f1..4e15092 100644 --- a/src/Facades/Screen.php +++ b/src/Facades/Screen.php @@ -7,6 +7,7 @@ /** * @method static object cursorPosition() * @method static array displays() + * @method static array getCenterOfActiveScreen() */ class Screen extends Facade { diff --git a/src/Screen.php b/src/Screen.php index a8b2ca3..7f3e6c6 100644 --- a/src/Screen.php +++ b/src/Screen.php @@ -3,11 +3,16 @@ namespace Native\Laravel; use Native\Laravel\Client\Client; +use Illuminate\Support\Facades\Log; class Screen { public function __construct(protected Client $client) {} + public function getClient() { + return $this->client; + } + public function cursorPosition(): object { return (object) $this->client->get('screen/cursor-position')->json(); @@ -23,7 +28,7 @@ public function primary(): object return $this->client->get('screen/primary-display')->json('primaryDisplay'); } - public function active(): object + public function active(): object|null { return $this->client->get('screen/active')->json(); } @@ -40,6 +45,10 @@ public function getCenterOfActiveScreen(): array $screen = $this->displays(); /* Navigate every screen and check for cursor position against the bounds of the screen. */ + $activeScreen = $this->active(); + $resp = $this->client->get('screen/active'); + Log::debug("activeScreen", [$activeScreen, $resp->body(), $resp->headers(), $resp->status()]); + foreach ($screen as $s) { /** @var array $bounds */ $bounds = $s['bounds']; From cca1868c7d9d49509e31446643eb9bfd167e4f82 Mon Sep 17 00:00:00 2001 From: Andrei Daniel Petrica Date: Sat, 19 Oct 2024 14:34:02 +0200 Subject: [PATCH 4/8] tests --- src/Screen.php | 116 ++++++++++++++++++++++++++----------------------- 1 file changed, 62 insertions(+), 54 deletions(-) diff --git a/src/Screen.php b/src/Screen.php index 7f3e6c6..499ae61 100644 --- a/src/Screen.php +++ b/src/Screen.php @@ -7,69 +7,77 @@ class Screen { - public function __construct(protected Client $client) {} + public function __construct(protected Client $client) + { + } - public function getClient() { - return $this->client; - } + public function getClient() + { + return $this->client; + } - public function cursorPosition(): object - { - return (object) $this->client->get('screen/cursor-position')->json(); - } + public function cursorPosition(): object + { + return (object) $this->client->get("screen/cursor-position")->json(); + } - public function displays(): array - { - return $this->client->get('screen/displays')->json('displays'); - } + public function displays(): array + { + return $this->client->get("screen/displays")->json("displays"); + } - public function primary(): object - { - return $this->client->get('screen/primary-display')->json('primaryDisplay'); - } + public function primary(): object + { + return $this->client->get("screen/primary-display")->json("primaryDisplay"); + } - public function active(): object|null - { - return $this->client->get('screen/active')->json(); - } - - /** - * Returns the center of the screen where the mouse pointer is placed. - * - * @return array - */ - public function getCenterOfActiveScreen(): array - { - //get the screen size and current cursor position. - $cursor = $this->cursorPosition(); - $screen = $this->displays(); + public function active(): object|null|array + { + return $this->client->get("screen/active")->json(); + } - /* Navigate every screen and check for cursor position against the bounds of the screen. */ - $activeScreen = $this->active(); - $resp = $this->client->get('screen/active'); - Log::debug("activeScreen", [$activeScreen, $resp->body(), $resp->headers(), $resp->status()]); + /** + * Returns the center of the screen where the mouse pointer is placed. + * + * @return array + */ + public function getCenterOfActiveScreen(): array + { + //get the screen size and current cursor position. + $cursor = $this->cursorPosition(); + $screen = $this->displays(); - foreach ($screen as $s) { - /** @var array $bounds */ - $bounds = $s['bounds']; - if ( - $cursor->x >= $bounds['x'] - && $cursor->x <= ($bounds['x'] + $bounds['width']) - && $cursor->y >= $bounds['y'] - && $cursor->y <= ($bounds['y'] + $bounds['height']) - ) { - // I'm inside the display - return [ - 'x' => $bounds['x'] + $bounds['width'] / 2, - 'y' => $bounds['y'] + $bounds['height'] / 2, - ]; - } - } - $bounds = $screen[0]['bounds']; + /* Navigate every screen and check for cursor position against the bounds of the screen. */ + $activeScreen = $this->active(); + $resp = $this->client->get("screen/active"); + Log::debug("activeScreen", [ + $activeScreen, + $resp->body(), + $resp->headers(), + $resp->status(), + ]); + foreach ($screen as $s) { + /** @var array $bounds */ + $bounds = $s["bounds"]; + if ( + $cursor->x >= $bounds["x"] && + $cursor->x <= $bounds["x"] + $bounds["width"] && + $cursor->y >= $bounds["y"] && + $cursor->y <= $bounds["y"] + $bounds["height"] + ) { + // I'm inside the display return [ - 'x' => $bounds['x'] + $bounds['width'] / 2, - 'y' => $bounds['y'] + $bounds['height'] / 2, + "x" => $bounds["x"] + $bounds["width"] / 2, + "y" => $bounds["y"] + $bounds["height"] / 2, ]; + } } + $bounds = $screen[0]["bounds"]; + + return [ + "x" => $bounds["x"] + $bounds["width"] / 2, + "y" => $bounds["y"] + $bounds["height"] / 2, + ]; + } } From 7839fcf53b11792d7fb5479754bd17e6a86988f6 Mon Sep 17 00:00:00 2001 From: Andrei Daniel Petrica Date: Sat, 19 Oct 2024 14:38:16 +0200 Subject: [PATCH 5/8] cleaned and finsihed --- src/Screen.php | 29 +---------------------------- 1 file changed, 1 insertion(+), 28 deletions(-) diff --git a/src/Screen.php b/src/Screen.php index 499ae61..db24752 100644 --- a/src/Screen.php +++ b/src/Screen.php @@ -43,37 +43,10 @@ public function active(): object|null|array */ public function getCenterOfActiveScreen(): array { - //get the screen size and current cursor position. - $cursor = $this->cursorPosition(); - $screen = $this->displays(); - /* Navigate every screen and check for cursor position against the bounds of the screen. */ $activeScreen = $this->active(); - $resp = $this->client->get("screen/active"); - Log::debug("activeScreen", [ - $activeScreen, - $resp->body(), - $resp->headers(), - $resp->status(), - ]); - foreach ($screen as $s) { - /** @var array $bounds */ - $bounds = $s["bounds"]; - if ( - $cursor->x >= $bounds["x"] && - $cursor->x <= $bounds["x"] + $bounds["width"] && - $cursor->y >= $bounds["y"] && - $cursor->y <= $bounds["y"] + $bounds["height"] - ) { - // I'm inside the display - return [ - "x" => $bounds["x"] + $bounds["width"] / 2, - "y" => $bounds["y"] + $bounds["height"] / 2, - ]; - } - } - $bounds = $screen[0]["bounds"]; + $bounds = $activeScreen["bounds"]; return [ "x" => $bounds["x"] + $bounds["width"] / 2, From fc7a8595b3d0066138f7a8a41a47df970f2c936a Mon Sep 17 00:00:00 2001 From: Andrei Daniel Petrica Date: Sat, 19 Oct 2024 14:40:14 +0200 Subject: [PATCH 6/8] type fix --- src/Screen.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Screen.php b/src/Screen.php index db24752..bf018d7 100644 --- a/src/Screen.php +++ b/src/Screen.php @@ -31,7 +31,7 @@ public function primary(): object return $this->client->get("screen/primary-display")->json("primaryDisplay"); } - public function active(): object|null|array + public function active(): object { return $this->client->get("screen/active")->json(); } From 405dc511da9bc0441e252d87ac46db47fdb6aca4 Mon Sep 17 00:00:00 2001 From: Andrei Daniel Petrica Date: Sat, 19 Oct 2024 18:22:13 +0200 Subject: [PATCH 7/8] added methods, cleaned imports, restored 4 spaces indent and removed useless method. --- src/Facades/Screen.php | 2 + src/Screen.php | 88 ++++++++++++++++++++---------------------- 2 files changed, 43 insertions(+), 47 deletions(-) diff --git a/src/Facades/Screen.php b/src/Facades/Screen.php index 4e15092..82dfcfa 100644 --- a/src/Facades/Screen.php +++ b/src/Facades/Screen.php @@ -8,6 +8,8 @@ * @method static object cursorPosition() * @method static array displays() * @method static array getCenterOfActiveScreen() + * @method static array active() + * @method static array primary() */ class Screen extends Facade { diff --git a/src/Screen.php b/src/Screen.php index bf018d7..c2d0b2b 100644 --- a/src/Screen.php +++ b/src/Screen.php @@ -3,54 +3,48 @@ namespace Native\Laravel; use Native\Laravel\Client\Client; -use Illuminate\Support\Facades\Log; class Screen { - public function __construct(protected Client $client) - { - } - - public function getClient() - { - return $this->client; - } - - public function cursorPosition(): object - { - return (object) $this->client->get("screen/cursor-position")->json(); - } - - public function displays(): array - { - return $this->client->get("screen/displays")->json("displays"); - } - - public function primary(): object - { - return $this->client->get("screen/primary-display")->json("primaryDisplay"); - } - - public function active(): object - { - return $this->client->get("screen/active")->json(); - } - - /** - * Returns the center of the screen where the mouse pointer is placed. - * - * @return array - */ - public function getCenterOfActiveScreen(): array - { - /* Navigate every screen and check for cursor position against the bounds of the screen. */ - $activeScreen = $this->active(); - - $bounds = $activeScreen["bounds"]; - - return [ - "x" => $bounds["x"] + $bounds["width"] / 2, - "y" => $bounds["y"] + $bounds["height"] / 2, - ]; - } + public function __construct(protected Client $client) + { + } + + public function cursorPosition(): object + { + return (object) $this->client->get("screen/cursor-position")->json(); + } + + public function displays(): array + { + return $this->client->get("screen/displays")->json("displays"); + } + + public function primary(): object + { + return $this->client->get("screen/primary-display")->json("primaryDisplay"); + } + + public function active(): object + { + return $this->client->get("screen/active")->json(); + } + + /** + * Returns the center of the screen where the mouse pointer is placed. + * + * @return array + */ + public function getCenterOfActiveScreen(): array + { + /* Navigate every screen and check for cursor position against the bounds of the screen. */ + $activeScreen = $this->active(); + + $bounds = $activeScreen["bounds"]; + + return [ + "x" => $bounds["x"] + $bounds["width"] / 2, + "y" => $bounds["y"] + $bounds["height"] / 2, + ]; + } } From 348e3cafeabb15e50dfc37666a9fc9ec61fd7060 Mon Sep 17 00:00:00 2001 From: Andrei Daniel Petrica Date: Sun, 20 Oct 2024 16:23:23 +0200 Subject: [PATCH 8/8] turn " to ' --- src/Screen.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Screen.php b/src/Screen.php index c2d0b2b..ab8d61b 100644 --- a/src/Screen.php +++ b/src/Screen.php @@ -12,22 +12,22 @@ public function __construct(protected Client $client) public function cursorPosition(): object { - return (object) $this->client->get("screen/cursor-position")->json(); + return (object) $this->client->get('screen/cursor-position')->json(); } public function displays(): array { - return $this->client->get("screen/displays")->json("displays"); + return $this->client->get('screen/displays')->json('displays'); } public function primary(): object { - return $this->client->get("screen/primary-display")->json("primaryDisplay"); + return $this->client->get('screen/primary-display')->json('primaryDisplay'); } public function active(): object { - return $this->client->get("screen/active")->json(); + return $this->client->get('screen/active')->json(); } /** @@ -40,11 +40,11 @@ public function getCenterOfActiveScreen(): array /* Navigate every screen and check for cursor position against the bounds of the screen. */ $activeScreen = $this->active(); - $bounds = $activeScreen["bounds"]; + $bounds = $activeScreen['bounds']; return [ - "x" => $bounds["x"] + $bounds["width"] / 2, - "y" => $bounds["y"] + $bounds["height"] / 2, + 'x' => $bounds['x'] + $bounds['width'] / 2, + 'y' => $bounds['y'] + $bounds['height'] / 2, ]; } }