Skip to content

Commit

Permalink
Bug fix of origin calculation and window indices.
Browse files Browse the repository at this point in the history
  • Loading branch information
SirVer committed Aug 19, 2019
1 parent 80261ce commit 89b0e9e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "move_window"
version = "0.2.0"
version = "0.3.0"
authors = ["Holger Rapp <HolgerRapp@gmx.net>"]
edition = "2018"

Expand Down
57 changes: 40 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use cocoa::foundation::{NSArray, NSDictionary, NSString};
use objc::runtime::Class;
use objc::runtime::Object;
use osascript::JavaScript;
use serde_derive::Serialize;
use serde_derive::{Deserialize, Serialize};
use std::ffi::CStr;

#[derive(Serialize)]
Expand All @@ -18,7 +18,7 @@ struct MoveWindowParams {
r: Rect,
}

#[derive(Debug, Serialize)]
#[derive(Debug, Deserialize, Serialize, Clone)]
struct Rect {
x: i32,
y: i32,
Expand All @@ -45,7 +45,8 @@ struct MoveParameters {
#[derive(Debug)]
struct Screen {
index: u64,
rect: Rect,
visible_frame: Rect,
frame: Rect,
}

fn next_integer(a: &mut ::std::iter::Peekable<impl Iterator<Item = char>>) -> Result<i32, String> {
Expand All @@ -63,7 +64,7 @@ impl MoveParameters {
let screen = {
let c = i.next().ok_or_else(|| "No more items".to_string())?;
match c {
'0'...'9' => ScreenSelector::Index(c.to_digit(10).unwrap() as usize),
'0'..='9' => ScreenSelector::Index(c.to_digit(10).unwrap() as usize),
c => ScreenSelector::Char(c),
}
};
Expand Down Expand Up @@ -123,19 +124,33 @@ fn get_screens() -> Vec<Screen> {
let screens: *mut Object = NSScreen::screens(nil);
for index in 0..NSArray::count(screens) {
let screen: *mut Object = msg_send![screens, objectAtIndex: index];
let s = screen.visibleFrame();
let visible_frame = screen.visibleFrame();
let frame = screen.frame();
rv.push(Screen {
index,
rect: Rect {
x: s.origin.x as i32,
y: s.origin.y as i32,
width: s.size.width as i32,
height: s.size.height as i32,
visible_frame: Rect {
x: visible_frame.origin.x as i32,
y: visible_frame.origin.y as i32,
width: visible_frame.size.width as i32,
height: visible_frame.size.height as i32,
},
frame: Rect {
x: frame.origin.x as i32,
y: frame.origin.y as i32,
width: frame.size.width as i32,
height: frame.size.height as i32,
},
})
}
};
rv.sort_by_key(|s| s.rect.x);
// The window frames have their origins in the bottom left of the screen, y going upwards.
// However, screen bounds have the origin at the top left going down. We need to convert here
// to get them in the screen space.
for idx in 1..rv.len() {
let y = rv[0].frame.height - rv[idx].visible_frame.height - rv[idx].visible_frame.y;
rv[idx].visible_frame.y = y;
}
rv.sort_by_key(|s| s.visible_frame.x);
rv
}

Expand Down Expand Up @@ -167,20 +182,28 @@ fn main() {

let screens = get_screens();
let screen = match params.screen {
ScreenSelector::Index(index) => &screens[index],
ScreenSelector::Index(index) => {
let mut screen = None;
for s in &screens {
if s.index == index as u64 {
screen = Some(s)
}
}
screen.expect("Unknown screen index.")
}
ScreenSelector::Char(c) => match c {
'l' => &screens[0],
'm' | 'c' => &screens[1],
'r' => &screens[screens.len() - 1],
_ => panic!("Unknown character for screen selection: {}", c),
}
},
};

let width = f64::from(screen.rect.width) / f64::from(params.x_ratio);
let height = f64::from(screen.rect.height) / f64::from(params.y_ratio);
let width = f64::from(screen.visible_frame.width) / f64::from(params.x_ratio);
let height = f64::from(screen.visible_frame.height) / f64::from(params.y_ratio);
let frame = Rect {
x: (f64::from(screen.rect.x) + width * f64::from(params.x_start)).round() as i32,
y: (f64::from(screen.rect.y) + height * f64::from(params.y_start)).round() as i32,
x: (f64::from(screen.visible_frame.x) + width * f64::from(params.x_start)).round() as i32,
y: (f64::from(screen.visible_frame.y) + height * f64::from(params.y_start)).round() as i32,
width: (width * f64::from(params.x_end - params.x_start + 1)).round() as i32,
height: (height * f64::from(params.y_end - params.y_start + 1)).round() as i32,
};
Expand Down
8 changes: 4 additions & 4 deletions watcher.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ return {
-- name = "Running cargo check",
-- command = "cargo check --release --color=always",
-- },
{
name = "Running cargo clippy",
command = "cargo clippy --release --color=always",
},
{
name = "Running cargo build",
command = "cargo build --release --color=always",
},
{
name = "Running cargo clippy",
command = "cargo clippy --color=always -- -D warnings",
},
}
},
}

0 comments on commit 89b0e9e

Please sign in to comment.