kasper / phoenix

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

Incorrect window TopLeft coordinates (out of screen) after moving it at another space. #351

Closed AlaxJI closed 1 month ago

AlaxJI commented 2 months ago

Incorrect (out of screen) window TopLeft coordinates after moving it at another space.

Screen index: 0
visibleFrame:
    'y' => "0"
    'x' => "0"
    'width' => "1920"
    'height' => "1055"

Screen index: 1
visibleFrame: 
    'y' => "-848"
    'x' => "1920"
    'width' => "1312"
    'height' => "818"

Screen index: 2
visibleFrame: 
    'y' => "0"
    'x' => "1920"
    'width' => "1920"
    'height' => "1055"

Moving window to space 11

space index: 11
visibleFrame: 
    'y' => "-848"
    'x' => "1920"
    'width' => "1312"
    'height' => "818"
window topLeft:
    'x' => "1920"
    'y' => "25"
kasper commented 1 month ago

@AlaxJI Hey, thanks for reporting! I’m going to presume you get the coordinates for the window again after moving to the new space?

Sounds like it’s a regression created by #350 for multi-monitor setups. Also reported in: https://github.com/kasper/phoenix/issues/348#issuecomment-2182162242

Unfortunately, I’m unsure how to fix it at the moment. @metakirby5 would you have any thoughts?

metakirby5 commented 1 month ago

Not really sure, I haven't experienced the issue with my multi-monitor setup 🤔

In System Settings > Desktop & Dock > Mission Control, do you have Displays have separate spaces on or off? I wonder if a difference in settings there is related.

Otherwise, it would help to have a minimal config to reproduce the issue.

(To be honest, I don't fully understand how the API I used in #350 works, since it's totally undocumented. I mostly just observed how other tools were using it and verified it worked on my end.)

AlaxJI commented 1 month ago

In System Settings > Desktop & Dock > Mission Control, do you have Displays have separate spaces on or off? I wonder if a difference in settings there is related.

I have it on.

Otherwise, it would help to have a minimal config to reproduce the issue.

My javascript is poor, but this is my config:

const applicationMapToSpace = {
  "NetBeans": 6,
  "DBeaver": 7,
  "Telegram": 11,
  "Slack": 11,
  "WhatsApp": 11,
};

var getMainWindow = async function (app) {
  return new Promise((resolve, reject) => {
    if (typeof app.mainWindow() !== "undefined") {
      resolve(app.mainWindow());
    } else {
      setTimeout(() => {
        getMainWindow(app).then((resolveApp) => {resolve(resolveApp);});
      }, 1000);
    }
  });
};

var moveAppToSpace = function (app) {
  var entry = Object.entries(applicationMapToSpace).find(([key, value]) => app.name().indexOf(key) >= 0);
  if (typeof entry !== 'undefined') {
    var name = entry[0];
    var spaceNumber = entry[1];
    getMainWindow(app).then((window) => {
      var spaces = Space.all();
      var i = 0;
      for (let index = 0; index < spaces.length; index++) {
        if (!spaces[index].isFullScreen()) {
          i++;
        }
        if (i === spaceNumber) {
          var space = spaces[index];
          if (!window.spaces()[0].isEqual(space)) {
            space.moveWindows([window]);
            setTimeout(() => {
              window.focus();
              window.maximize();
            }, 500)
          }
        }
      }
      });
  }
}

const moverOnLaunch = Event.on('appDidLaunch', (app) => {
  moveAppToSpace(app);
});

const moverOnActivate = Event.on('appDidActivate', (app) => {
  moveAppToSpace(app);
});

This is my "Arrange Displays". Each display have five spaces. 2024-07-10_07-03-31

metakirby5 commented 1 month ago

@AlaxJI Thanks! I found that in my config, I had been updating the window via setTopLeft and setSize after moving it to a new screen, using values from the new screen's flippedVisibleFrame. Would you mind giving it a try too? It would probably go right after the call to space.moveWindows([window]).

AlaxJI commented 1 month ago

@metakirby5 Thanks!

updating the window via setTopLeft and setSize after moving it

It's work!!!

kasper commented 1 month ago

Glad to hear! Thanks @metakirby5 for assisting on this one. ❤️

kasper commented 1 month ago

@dan-phantom Hey! Could you also confirm your code and post a snippet of the logic that was not working for you?

dan-phantom commented 1 month ago

I'm on holiday, will reply next week 🤗 On Jul 21, 2024, at 8:21 AM, Kasper Hirvikoski @.***> wrote: @dan-phantom Hey! Could you also confirm your code and post a snippet of the logic that was not working for you?

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you were mentioned.Message ID: @.***>

dan-phantom commented 1 month ago

More details, sorry for the delay @kasper

I have 3 screens, MBP + 2 externals, each of them has 4 spaces on it

This is the code

const loadConfig = () => {
  _.each(screens, (screen) => {
    const screenConfig = screenMap[screen.id];

    if (screenConfig && screen.raw.identifier() === screen.id) {

      _.each(screen.raw.spaces(), (space, idx) => {
        wait(1000);
        _.each(
          screenConfig.config?.[screens.length].spaces[idx]?.apps,
          (app) => {
            const appWins = App.get(app)?.windows() || [];
            console.log(
              `app space: ${app}  ${idx} ${space.hash()} ${appWins?.length}`
            );
            space.moveWindows(appWins);

            _.each(appWins, (win) => {
              wait(1000);

              win.maximize();
            });
          }
        );
      });
    }
  });

};

and screenMap is something like

const screenMap = {
  "37D8832A-2D66-02CA-blah": {
    name: "Macbook",
    config: {
      3: {
        spaces: [{ apps: ["Warp"] }],
      },
    },
  },
  "B19F092B-DEA3-422F-blah": {
    name: "Left",
    config: {
      3: {
        spaces: [
          { apps: ["Code"] },
          { apps: ["Linear"] },
          { apps: ["Notion"] },
          { apps: ["Rive", "ForkLift"] },
        ],
      },
    },
  },
  "CBFD144E-D4D8-4FF5-blah": {
    name: "Right",
    config: {
      3: {
        spaces: [
          {
            apps: [
              "Microsoft Edge",
              "Google Chrome",
              "Firefox",
              "Safari",
              "Brave Browser",
            ],
          },
          {
            apps: ["com.apple.iphonesimulator", "Vysor"],
          },
          { apps: ["Slack", "Gmail", "Google Calendar"] },
          { apps: ["Spotify"] },
        ],
      },
    },
  },
};
dan-phantom commented 1 month ago

@metakirby5 @kasper I can also confirm the setTopLeft works for me too , I call it before maximise ❤️

kasper commented 1 month ago

@dan-phantom Perfect, great to hear! Thanks!