kwin-tiling icon indicating copy to clipboard operation
kwin-tiling copied to clipboard

Set the floating window to the center of screen

Open Piping opened this issue 2 years ago • 3 comments

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 avatar Aug 20 '21 18:08 Piping

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

Piping avatar Aug 20 '21 18:08 Piping

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)

faho avatar Aug 20 '21 18:08 faho

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?

Piping avatar Aug 20 '21 19:08 Piping