The window.external.window.center() is a handy function in the AIM Modules API that you can use when all you want to do is center a window without regard for the buddy list. If you want to center a plugin window in a way that accounts for the buddy list, then you will need to write your own function. A solution I actually needed in one of my plugins.
In order to center a window, we need to know what monitor it’s in. Here is a function that returns the monitor for a given window:
function findMonitorForWindow(tWnd) { // loop over all monitors until we find the one where // the top left corner of the target window is in for (var i=0; true; i++) { var m = client.findWindow('_monitor'+i); if (m.exists) { if ( tWnd.x >= m.x && tWnd.x < (m.x + m.width) && tWnd.y >= m.y && tWnd.y < (m.y + m.height) ) return m; } else break; } // default to first monitor return client.findWindow('_monitor0'); }
NOTE: ‘client’ above is the window.external.client object that has been assigned to a global variable.
Now that we can find the monitor for a given window, we can center the window any way we want in this monitor. Here is a function that centers the window over the region not occupied by a docked buddy list.
function BuddyListAwareCenterWindow(tWnd) { // The standard center function: var center = function(o,w,n) { return (o + ((w-n)/2) ); } var tWndMonitor = findMonitorForWindow(tWnd); var blWnd = client.findWindow('_buddylist'); var blWndMonitor = findMonitorForWindow(blWnd); var blWidth = 0, blOffset = 0; // are both windows in the same monitor? if (tWndMonitor.monitorIndex == blWndMonitor.monitorIndex) { // adjust centering width if bl is docked on either left or right if (blWnd.x == 0 || (blWndMonitor.width - blWnd.width) == blWnd.x ) blWidth = blWnd.width; // adjust centering offset if docked on left. if (blWnd.x == 0) blOffset = blWnd.width; } tWnd.move( center(tWndMonitor.x + blOffset, tWndMonitor.width - blWidth, tWnd.width), center(tWndMonitor.y, tWndMonitor.height, tWnd.height) ); }
There are some edge cases like when a window is bigger than the space it’s centered on, but this solution should help you get started.
Tags: AMO, JavaScript, Open AIM, Sample Code, Tips