kasper / phoenix

A lightweight macOS window and app manager scriptable with JavaScript
https://kasper.github.io/phoenix/
Other
4.36k stars 129 forks source link

Some windows are ignored while looking at Window.at() #260

Open gaabora opened 4 years ago

gaabora commented 4 years ago

Hello. I want to make a script for moving and sizing windows with a mouse over any window's area with some modificator key pressed (this behaviour you can set up eazily in KDE)

I've got two issues:

  1. Some windows are ignored, for example latest Firefox and Telegram desktop apps.
  2. (more like a question) How do I store a previous position of window\cursor? (I want to get cursor's offset after last event's tick ang get rid of hardcoded 400x300 values below). You ca run the code below to clarify what I mean.

Thanks!

My code is:

var handler = new Event('mouseDidLeftDrag', function (event) {
  if (_.includes(event.modifiers, 'alt')) {
    let mPos = Mouse.location();
    const window = Window.at(mPos);

    if (window) {

      mPos = Mouse.location();

      let wPos = window.topLeft();

      const diff = {
        x: mPos.x - wPos.x,
        y: mPos.y - wPos.y
      };
      const nPos = {
        x: mPos.x - 400, // hardcoded x cursor offset
        y: mPos.y - 300 // hardcoded x cursor offset
      };
      console.log(`move ${window.title()} m:${mPos.x}x${mPos.y} w:${wPos.x}x${wPos.y} d:${diff.x}x${diff.y} n:d:${nPos.x}x${nPos.y}`);
      window.setTopLeft(nPos)
    }
  }
});
kasper commented 4 years ago

Hello!

For 1., are you getting any errors in the logs starting with “Error: Could not get accessibility element at position”. This is a native macOS API, so it might be that the apps have a broken accessibility integration.

For 2., what do you mean by the previous position? You could store the position in a variable. Currently you have the variable within the function scope so you can hoist the variable outside of the function to be able to store it across event callbacks. Or if you need to store the values across system boots you could use the Storage feature.

gaabora commented 4 years ago
  1. no
  2. Thanks for a tip, I kinda figure out a wip buggy solution You can now move and size windows on mac with mouse like in kde by holding shift and dragging with left button to move or right button to size)

var lastPos = Mouse.location();

var mouseDidMove_lastPos = new Event('mouseDidMove', function (event) {
    lastPos = Mouse.location();
});

var mouseDidLeftDrag_winMove = new Event('mouseDidLeftDrag', function (event) {
    if (_.includes(event.modifiers, 'shift')) {
        let currPos = Mouse.location();
        const window = Window.at(currPos);

        if (window != undefined) {
            let winPos = window.topLeft();

            const diff = {
                x: lastPos.x - winPos.x,
                y: lastPos.y - winPos.y
            };
            const newPos = {
                x: currPos.x - diff.x,
                y: currPos.y - diff.y
            };
            window.setTopLeft(newPos)
        }
        lastPos = Mouse.location();
    }
});
var mouseDidRightDrag_winSize = new Event('mouseDidRightDrag', function (event) {
    if (_.includes(event.modifiers, 'shift')) {
        let currPos = Mouse.location();
        const window = Window.at(currPos);

        if (window != undefined) {
            let winSize = window.size();
            const diff = {
                x: lastPos.x - currPos.x,
                y: lastPos.y - currPos.y
            };
            const newSize = {
                width: winSize.width - diff.x,
                height: winSize.height - diff.y
            };
            window.setSize(newSize)
        }
        lastPos = Mouse.location();
    }
});