Project

General

Profile

[Script] Simple window layout manager

Added by Per Amundsen over 6 years ago

Save all open status/channel window sizes and positions to a profile.

Restore saved window sizes and positions from a profile.

Changes

08/17/2020

Fixed a missing parenthese

04/18/2018

Added a check for empty/broken profile names
Fixed allow using spaces in profile names
Fixed restoring and removing layouts doesn't work properly

04/16/2018

Added option to activate windows on restore

Script

on *:START:{
  if ($exists(windowlayout.ini)) { 
    var %s 0, %c $ini(windowlayout.ini, 0)
    while (%s < %c) {
      inc %s
      var %section $ini(windowlayout.ini, %s)
      hload -mi %section windowlayout.ini %section
    }
  }
}

menu * {
  Save layout:savelayout $$input(Enter name, e)
  Remove layout
  .$submenu($removelayoutlist($1)))
  Load layout
  .$submenu($loadlayoutlist($1)))
  Layout options
  .Activate Windows $iif(%windowlayout_activate,$style(1),) :.set %windowlayout_activate $iif(%windowlayout_activate,0,1)
}

alias loadlayoutlist {
  var %tok = $replace($gettok($listlayouts, $1, 44), windowlayout, )
  return $iif(%tok && $1 isnum, %tok :loadlayout %tok,)
}

alias removelayoutlist {
  var %tok = $replace($gettok($listlayouts, $1, 44), windowlayout, )
  return $iif(%tok && $1 isnum, %tok :removelayout %tok,)
}

alias loadlayout {
  var %layout windowlayout $+ $replace($1-, $chr(32), _)

  if (!$hget(%layout)) {
    return
  }

  var %window window $iif(%windowlayout_activate,$chr(32) -a,)

  var %s 0
  while (%s < $scon(0)) {
    inc %s
    scon %s

    var %w 0
    while (%w < $window(*,0)) {
      inc %w

      if ($window(*,%w).type == status) {
        if ($hget(%layout, $network)) {
          %window "Status Window" $hget(%layout, $network)
        }
      }      
      else if ($window(*,%w).type == channel) {
        if ($hget(%layout, $network $+ $window(*, %w))) {
          %window $window(*, %w) $hget(%layout, $network $+ $window(*, %w))          
        }
      }
    }    
  }
}

alias listlayouts {
  var %text
  var %s 0
  while (%s < $hget(0)) {
    inc %s

    if (windowlayout isin $hget(%s) && $hget(%s) != windowlayout) {
       %text = %text $+ $replace($hget(%s),_,$chr(32)) $+ ,
    }
  } 

  return %text
}

alias removelayout {
  var %layout windowlayout $+ $replace($1-,$chr(32),_)

  if ($hget(%layout)) {
    hfree %layout

    if ($exists(windowlayout.ini)) {
      remini windowlayout.ini %layout
    }
  }
}

alias savelayout {
  var %name = $replace($1-, $chr(32), _)

  if (!%name) {
    return
  }

  var %layout windowlayout $+ %name

  if ($hget(%layout)) {
    if (!$input(Overwrite $1- ?,y)) {
      return
    }
    hfree %layout
  }

  hmake %layout

  var %s 0
  while (%s < $scon(0)) {
    inc %s
    scon %s

    var %w 0
    while (%w < $window(*,0)) {
      inc %w

      if ($window(*,%w).type == status) {
        hadd %layout $network $window(*, %w).x $window(*, %w).y $window(*, %w).w $window(*, %w).h
      }      
      else if ($window(*,%w).type == channel) {
        hadd %layout $network $+ $window(*,%w) $window(*, %w).x $window(*, %w).y $window(*, %w).w $window(*, %w).h
      }
    }    
  }

  hsave -i %layout windowlayout.ini %layout
}