Skip to content

Tasks Service

Sam Johnson edited this page Jan 3, 2021 · 3 revisions

The tasks service provides information regarding currently open windows, and allows basic window management.

There are a few objects involved with using the tasks service:

  • Tasks provides the GroupedWindows collection view and methods to manage the tasks service lifecycle.
  • TasksService provides the Windows hook functionality to populate window data.
  • ApplicationWindow objects represent each individual window.

Lifecycle

Using ShellManager

If you are using the ShellManager to instantiate ManagedShell, the lifecycle of the tasks service is managed for you. However, you can customize this behavior by passing a ShellConfig object to the ShellManager constructor to modify settings:

// Initialize the default configuration.
ShellConfig config = ShellManager.DefaultShellConfig;

// Customize tasks service options.
config.EnableTasksService = true; // controls whether the tasks objects are instantiated in ShellManager, true by default
config.AutoStartTasksService = false; // controls whether the tasks service is started as part of ShellManager instantiation, true by default
config.TaskIconSize = IconSize.Medium; // sets the size of window icon to fetch, small (16pt) by default

// Initialize the shell manager.
ShellManager _shellManager = new ShellManager(config);

// Initialize the tasks service, since we disabled auto-start above.
_shellManager.Tasks.Initialize();

Custom

The tasks service can be used without ShellManager management, if desired.

// Instantiate service.
TasksService tasksService = new TasksService(IconSize.Small);
Tasks tasks = new Tasks(tasksService);

// Start the service.
tasks.Initialize();

// Access the window listing.
MyTaskbarItemsControl.ItemsSource = tasks.GroupedWindows;

// Dispose of the service and icons.
IconHelper.DisposeIml();
tasks.Dispose();

Task Categorization

The tasks service supports defining custom groupings for windows, by implementing the ITaskCategoryProvider interface. This interface defines two methods:

  • string GetCategory(ApplicationWindow window) - The implementation should return a string value representing the category of the passed window.
  • void SetCategoryChangeDelegate(TaskCategoryChangeDelegate changeDelegate) - The implementation should maintain a reference to changeDelegate and call it whenever task category data changes. When called, the tasks service will query for each window's category using the above GetCategory method.

To enable task categorization, perform a custom initialization of the Tasks object, passing in your custom ITaskCategoryProvider implementation:

// Instantiate service.
TasksService tasksService = new TasksService(IconSize.Small);
Tasks tasks = new Tasks(tasksService);

// Instantiate the task category provider.
MyTaskCategoryProvider categoryProvider = new MyTaskCategoryProvider();

// Start the service with the custom category provider.
tasks.Initialize(categoryProvider);

Task Management

The tasks service provides window management functionality essential for shell replacements, via methods on each ApplicationWindow object in the GroupedWindows collection:

private void DoStuffToWindow(ApplicationWindow window)
{
    // Select window
    window.BringToFront();
    
    // Minimize window
    window.Minimize();
    
    // Maximize window
    window.Maximize();
    
    // Restore window
    window.Restore();
    
    // Close window
    window.Close();
}
Clone this wiki locally