kwin-scripts / kwin-tiling

Tiling script for kwin
GNU General Public License v2.0
1.1k stars 68 forks source link

Set the floating window to the center of screen #249

Open Piping opened 3 years ago

Piping commented 3 years ago

Hi,

Is it possible to add a keyboard shortcut to center the floating window on the screen?

Currently I can bind a shortcut to make a tiling window into float mode, but I wish I could center the window right after set it to float mode.

Is it possible to accomplish the sequence in setting? If not, is there a way to do it programmatically?

Thanks

Piping commented 3 years ago

in code contents/code/tilingmanager.js I see I can write a new function like this

KWin.registerShortcut("TILING: Toggle Floating",
  "TILING: Toggle Floating and center the Application Window",
  "",
  function() {
      var client = workspace.activeClient;
      if (client == null) {
          print("No active client");
          return;
      }
      // This can be undefined if the client
      // has never been seen before
      if (client.tiling_floating
         || client.tiling_floating == null) {
          client.tiling_floating = false;
          self.tiles.addClient(client);
      } else {
          client.tiling_floating = true;
          self.tiles.untileClient(client);
         ///// maybe center the client here?
      }
  });

But I am not sure which api I can use to center the client

faho commented 3 years ago

The client's shape is stored in client.geometry. You'll have to set that so that the middle of that is the middle of the screenrectangle.

There is no specific api to "center". (Kwin's plugin api is old school, barebones and badly documented)

Piping commented 3 years ago
function() {
    var client = workspace.activeClient;
    if (client == null) {
        print("No active client");
        return;
    }
    // This can be undefined if the client
    // has never been seen before
    if (client.tiling_floating
       || client.tiling_floating == null) {
        client.tiling_floating = false;
        self.tiles.addClient(client);
    } else {
        client.tiling_floating = true;
        self.tiles.untileClient(client);
        var maxArea = workspace.clientArea(KWin.MaximizeArea, client);
        client.geometry = {
            x: maxArea.x + (maxArea.width - client.width) / 2,
            y: maxArea.y + (maxArea.height - client.height) / 2,
            width: client.width,
            height: client.height
        };
    }

I tried one online script, the logic seems to be working in qdbus org.kde.plasmashell /PlasmaShell showInteractiveKWinConsole,

but inside tilingmanager.js, it only toggles the float, and does not move the window to the center of screen. Do you know if there is any state I am missing to configure?