Skip to content

Commit

Permalink
wip(split): Fix lint and code for darwin system
Browse files Browse the repository at this point in the history
  • Loading branch information
miniben-90 committed Aug 5, 2024
1 parent 9b403a1 commit a9999d0
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 73 deletions.
22 changes: 11 additions & 11 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
/* auto-generated by NAPI-RS */

/**
* Struct to store usage data of the window
* Struct to store process information of the window
*/
export interface UsageInfo {
memory: number
export interface IconInfo {
data: string
height: number
width: number
}
/**
* Struct to store process information of the window
Expand All @@ -18,6 +20,12 @@ export interface ProcessInfo {
name: string
execName: string
}
/**
* Struct to store usage data of the window
*/
export interface UsageInfo {
memory: number
}
/**
* Struct to store position and size of the window
*/
Expand All @@ -28,14 +36,6 @@ export interface WindowPosition {
height: number
isFullScreen: boolean
}
/**
* Struct to store process information of the window
*/
export interface IconInfo {
data: string
height: number
width: number
}
/**
* Retrieve information the about currently active window.
* Returns an object of `WindowInfo`.
Expand Down
8 changes: 0 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@
//#![deny(clippy::all)]
//#![allow(unused_imports)]

#[cfg(target_os = "macos")]
#[macro_use]
extern crate objc;

#[cfg(target_os = "macos")]
#[macro_use]
extern crate core;

mod common;

use common::{
Expand Down
2 changes: 1 addition & 1 deletion x-win-rs/src/common/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub fn os_name() -> String {
pub fn empty_entity() -> WindowInfo {
WindowInfo {
id: 0,
os: os_name().to_owned(),
os: os_name(),
title: "".to_string(),
position: WindowPosition {
x: 0,
Expand Down
4 changes: 2 additions & 2 deletions x-win-rs/src/linux/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#![deny(unused_imports)]

mod api;
use crate::common::api::API;
use crate::common::api::Api;

use api::LinuxAPI;

use self::api::APIGnome;

pub fn init_platform_api() -> impl API {
pub fn init_platform_api() -> impl Api {
LinuxAPI {}
}

Expand Down
95 changes: 46 additions & 49 deletions x-win-rs/src/macos/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use core_graphics::window::{

use crate::common::x_win_struct::icon_info::IconInfo;
use crate::common::{
api::{empty_entity, os_name, API},
api::{empty_entity, os_name, Api},
x_win_struct::{
process_info::ProcessInfo, usage_info::UsageInfo, window_info::WindowInfo,
window_position::WindowPosition,
Expand All @@ -40,10 +40,10 @@ pub struct MacosAPI {}
/**
* Impl. for Darwin system
*/
impl API for MacosAPI {
impl Api for MacosAPI {
fn get_active_window(&self) -> WindowInfo {
let windows: Vec<WindowInfo> = get_windows_informations(true);
if windows.len() > 0 {
if !windows.is_empty() {
let t: &WindowInfo = windows.first().unwrap();
t.clone() as WindowInfo
} else {
Expand Down Expand Up @@ -79,7 +79,7 @@ impl API for MacosAPI {
let byte_slice: &[u8] = std::slice::from_raw_parts(bytes, length);
let data = base64::prelude::BASE64_STANDARD.encode(byte_slice);
return IconInfo {
data: format!("data:image/png;base64,{}", data).to_owned(),
data: format!("data:image/png;base64,{}", data),
width: imagesize.0 as u32,
height: imagesize.1 as u32,
};
Expand Down Expand Up @@ -195,12 +195,12 @@ fn get_windows_informations(only_active: bool) -> Vec<WindowInfo> {

let mut url: String = String::new();

if is_browser_bundle_id(&bundle_identifier) {
if is_browser_bundle_id(bundle_identifier) {
let mut command = format!(
"tell app id \"{}\" to get URL of active tab of front window",
bundle_identifier
);
if is_from_document(&bundle_identifier) {
if is_from_document(bundle_identifier) {
command = format!(
"tell app id \"{}\" to get URL of front document",
bundle_identifier
Expand Down Expand Up @@ -241,48 +241,48 @@ fn get_windows_informations(only_active: bool) -> Vec<WindowInfo> {
}
}

return windows;
windows
}

fn is_browser_bundle_id(bundle_id: &str) -> bool {
match bundle_id {
matches!(
bundle_id,
"com.apple.Safari"
| "com.apple.SafariTechnologyPreview"
| "com.google.Chrome"
| "com.google.Chrome.beta"
| "com.google.Chrome.dev"
| "com.google.Chrome.canary"
| "org.mozilla.firefox"
| "org.mozilla.firefoxdeveloperedition"
| "com.brave.Browser"
| "com.brave.Browser.beta"
| "com.brave.Browser.nightly"
| "com.microsoft.edgemac"
| "com.microsoft.edgemac.Beta"
| "com.microsoft.edgemac.Dev"
| "com.microsoft.edgemac.Canary"
| "com.mighty.app"
| "com.ghostbrowser.gb1"
| "com.bookry.wavebox"
| "com.pushplaylabs.sidekick"
| "com.operasoftware.Opera"
| "com.operasoftware.OperaNext"
| "com.operasoftware.OperaDeveloper"
| "com.operasoftware.OperaGX"
| "com.vivaldi.Vivaldi"
| "com.kagi.kagimacOS"
| "company.thebrowser.Browser"
| "com.sigmaos.sigmaos.macos"
| "com.SigmaOS.SigmaOS" => true,
_ => false,
}
| "com.apple.SafariTechnologyPreview"
| "com.google.Chrome"
| "com.google.Chrome.beta"
| "com.google.Chrome.dev"
| "com.google.Chrome.canary"
| "org.mozilla.firefox"
| "org.mozilla.firefoxdeveloperedition"
| "com.brave.Browser"
| "com.brave.Browser.beta"
| "com.brave.Browser.nightly"
| "com.microsoft.edgemac"
| "com.microsoft.edgemac.Beta"
| "com.microsoft.edgemac.Dev"
| "com.microsoft.edgemac.Canary"
| "com.mighty.app"
| "com.ghostbrowser.gb1"
| "com.bookry.wavebox"
| "com.pushplaylabs.sidekick"
| "com.operasoftware.Opera"
| "com.operasoftware.OperaNext"
| "com.operasoftware.OperaDeveloper"
| "com.operasoftware.OperaGX"
| "com.vivaldi.Vivaldi"
| "com.kagi.kagimacOS"
| "company.thebrowser.Browser"
| "com.sigmaos.sigmaos.macos"
| "com.SigmaOS.SigmaOS"
)
}

fn is_from_document(bundle_id: &str) -> bool {
match bundle_id {
"com.apple.Safari" | "com.apple.SafariTechnologyPreview" | "com.kagi.kagimacOS" => true,
_ => false,
}
matches!(
bundle_id,
"com.apple.Safari" | "com.apple.SafariTechnologyPreview" | "com.kagi.kagimacOS"
)
}

// fn is_firefox_browser(bundle_id: &str) -> bool {
Expand All @@ -294,19 +294,16 @@ fn is_from_document(bundle_id: &str) -> bool {
// }

fn execute_applescript(script: &str) -> String {
let output = Command::new("osascript").args(&["-e", script]).output();
if output.is_ok() {
return String::from_utf8_lossy(&output.unwrap().stdout)
.trim()
.to_owned();
let output = Command::new("osascript").args(["-e", script]).output();
if let Ok(output) = output {
return String::from_utf8_lossy(&output.stdout).trim().to_owned();
}
return "".to_owned();
"".into()
}

fn get_screen_rect() -> NSRect {
let screen = unsafe { NSScreen::mainScreen(nil) };
let frame = unsafe { NSScreen::frame(screen) };
frame
unsafe { NSScreen::frame(screen) }
}

fn is_full_screen(window_rect: CGRect, screen_rect: NSRect) -> bool {
Expand Down
4 changes: 2 additions & 2 deletions x-win-rs/src/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

mod api;

use crate::common::api::API;
use crate::common::api::Api;
use api::MacosAPI;

pub fn init_platform_api() -> impl API {
pub fn init_platform_api() -> impl Api {
MacosAPI {}
}

0 comments on commit a9999d0

Please sign in to comment.