Processing Ajax...

Title

Message

Confirm

Confirm

Confirm

Confirm

Are you sure you want to delete this item?

Confirm

Are you sure you want to delete this item?

Confirm

Are you sure?

Rollup Inactive Windows To Title Bar

Description
This function will roll up windows to the title bar that haven't been focused for 30 seconds.
Language
C#.net
Minimum Version
Created By
Thomas Malloch (BFS)
Contributors
-
Date Created
Feb 3, 2015
Date Last Modified
Feb 3, 2015

Scripted Function (Macro) Code

using System;
using System.Drawing;
using System.Collections.Generic;

// The 'windowHandle' parameter will contain the window handle for the:
//   - Active window when run by hotkey
//   - Window Location target when run by a Window Location rule
//   - TitleBar Button owner when run by a TitleBar Button
//   - Jump List owner when run from a Taskbar Jump List
//   - Currently focused window if none of these match
public static class DisplayFusionFunction
{
	public static void Run(IntPtr windowHandle)
	{
		//a dictionary type to store the window handle and an approximate time
		Dictionary<IntPtr, int> watchedWindows = new Dictionary<IntPtr, int>();
		while(true)
		{
			//store the currently focused window
			IntPtr focusedWindow = BFS.Window.GetFocusedWindow();
			
			//if the focused window is in the watched windows, reset the timer
			if(watchedWindows.ContainsKey(focusedWindow))
				watchedWindows[focusedWindow] = 0;
			
			//get a list of all of the visible window handles
			List<IntPtr> windows = new List<IntPtr>(BFS.Window.GetVisibleWindowHandles());
			
			//loop though all of the visible windows
			foreach(IntPtr window in windows)
			{			
				//check if this window is already rolled up
				if(BFS.Window.IsWindowRolledUpToTitleBar(window))
					continue;
				
				//if this is a new window, add it to the list and continue
				if(!watchedWindows.ContainsKey(window))
				{
					watchedWindows.Add(window, 0);
					continue;
				}
				
				//increment the time by 1
				watchedWindows[window]++;
				
				//if the time is greater or equal to 30, roll up the window and remove it from the list
				if(watchedWindows[window] >= 30)
				{
					BFS.Window.RollupWindowToTitleBar(window);
					watchedWindows.Remove(window);
				}
			}
			
			//make a list to store windows that have been closed/minimized
			List<IntPtr> windowsToRemove = new List<IntPtr>();
			
			//look for windows that aren't visible anymore
			foreach(IntPtr key in watchedWindows.Keys)
			{
				//sequencial lookup, but the list size shouldn't ever be very big (>500)
				if(!windows.Contains(key))
					windowsToRemove.Add(key);
			}
			
			foreach(IntPtr window in windowsToRemove)
				watchedWindows.Remove(window);
			
			//pause for one second
			BFS.General.Sleep(1000);
		}
	}
}