Skip to content

Commit

Permalink
Exit support (#155)
Browse files Browse the repository at this point in the history
- `IConfigContext` and `ConfigContext` can now be shut down.
- Added the events `IConfigContext.Exit` and `IConfigContext.Exiting`.
- Added a command to `Exit` from the `IConfigContext`.
- Renamed all `disposedValue` variables to `_disposedValue`.
- Updated the following to be `IDisposable`:
  - `CommandPalettePlugin`
  - `FocusIndicatorPlugin`
  - `IWorkspace`
  - `IWorkspaceManager`
  - `Logger`
  - `SwitchWorkspaceCommand`
  - `TreeLayoutEngineWidget`
  - `WorkspaceWidget`
- Unsubscribed to `IConfigContext` and `IWindow` events in `Dispose` methods.
- `Whim.Runner.App` now starts the `Whim` engine, instead of taking it as an argument from `Program.Main`.
- `Whim.Runner.App` now responds to the `IConfigContext` having completed exiting.
- Moved the loading and provisioning of the config from `Whim.Runner` to `Whim.ConfigContext.Initialize`.
- `DoConfig` no longer returns `IConfigContext`.
  • Loading branch information
dalyIsaac authored Jul 22, 2022
1 parent 9ba2dd2 commit 6d7c39f
Show file tree
Hide file tree
Showing 44 changed files with 634 additions and 324 deletions.
2 changes: 1 addition & 1 deletion omnisharp.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"FileOptions": {
"ExcludeSearchPatterns": [
"src/Whim.Runner/Template/whim.config.template.csx"
"src/Whim.Runner/Template/whim.config.csx"
]
},
"RoslynExtensionsOptions": {
Expand Down
6 changes: 3 additions & 3 deletions src/Whim.Bar/ActiveLayout/ActiveLayoutWidgetViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class ActiveLayoutWidgetViewModel : INotifyPropertyChanged, IDisposable
/// </summary>
public IMonitor Monitor { get; }

private bool disposedValue;
private bool _disposedValue;

private readonly HashSet<IWorkspace> _workspaces = new();

Expand Down Expand Up @@ -62,7 +62,7 @@ public ActiveLayoutWidgetViewModel(IConfigContext configContext, IMonitor monito
/// <inheritdoc/>
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
if (!_disposedValue)
{
if (disposing)
{
Expand All @@ -74,7 +74,7 @@ protected virtual void Dispose(bool disposing)

// free unmanaged resources (unmanaged objects) and override finalizer
// set large fields to null
disposedValue = true;
_disposedValue = true;
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/Whim.Bar/BarPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class BarPlugin : IPlugin, IDisposable
private readonly BarConfig _barConfig;

private readonly Dictionary<IMonitor, BarWindow> _monitorBarMap = new();
private bool disposedValue;
private bool _disposedValue;

/// <summary>
/// Create the bar plugin.
Expand Down Expand Up @@ -81,7 +81,7 @@ private void ShowAll()
/// <inheritdoc />
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
if (!_disposedValue)
{
if (disposing)
{
Expand All @@ -91,11 +91,12 @@ protected virtual void Dispose(bool disposing)
}

_monitorBarMap.Clear();
_configContext.MonitorManager.MonitorsChanged -= MonitorManager_MonitorsChanged;
}

// free unmanaged resources (unmanaged objects) and override finalizer
// set large fields to null
disposedValue = true;
_disposedValue = true;
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/Whim.Bar/BarWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,4 @@ public BarWindow(IConfigContext configContext, BarConfig barConfig, IMonitor mon
CenterPanel.Children.AddRange(_barConfig.CenterComponents.Select(c => c(_configContext, _monitor, this)));
RightPanel.Children.AddRange(_barConfig.RightComponents.Select(c => c(_configContext, _monitor, this)));
}


}
7 changes: 4 additions & 3 deletions src/Whim.Bar/DateTime/DateTimeWidgetViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class DateTimeWidgetViewModel : INotifyPropertyChanged, IDisposable
{
private readonly DispatcherTimer _timer = new();
private readonly string _format;
private bool disposedValue;
private bool _disposedValue;

/// <summary>
/// The current date and time.
Expand Down Expand Up @@ -48,17 +48,18 @@ private void Timer_Tick(object? sender, object e)
/// <inheritdoc/>
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
if (!_disposedValue)
{
if (disposing)
{
// dispose managed state (managed objects)
_timer.Stop();
_timer.Tick -= Timer_Tick;
}

// free unmanaged resources (unmanaged objects) and override finalizer
// set large fields to null
disposedValue = true;
_disposedValue = true;
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/Whim.Bar/FocusedWindow/FocusedWindowWidgetViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Whim.Bar;
public class FocusedWindowWidgetViewModel : INotifyPropertyChanged, IDisposable
{
private readonly IConfigContext _configContext;
private bool disposedValue;
private bool _disposedValue;

/// <summary>
/// The title of the last focused window.
Expand Down Expand Up @@ -38,7 +38,7 @@ protected virtual void OnPropertyChanged(string propertyName)
/// <inheritdoc/>
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
if (!_disposedValue)
{
if (disposing)
{
Expand All @@ -48,7 +48,7 @@ protected virtual void Dispose(bool disposing)

// free unmanaged resources (unmanaged objects) and override finalizer
// set large fields to null
disposedValue = true;
_disposedValue = true;
}
}

Expand Down
35 changes: 34 additions & 1 deletion src/Whim.Bar/Workspace/SwitchWorkspaceCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ namespace Whim.Bar;
/// <summary>
/// Command for switching workspace.
/// </summary>
public class SwitchWorkspaceCommand : System.Windows.Input.ICommand
public class SwitchWorkspaceCommand : System.Windows.Input.ICommand, IDisposable
{
private readonly IConfigContext _configContext;
private readonly WorkspaceWidgetViewModel _viewModel;
private readonly WorkspaceModel _workspace;
private bool _disposedValue;

/// <inheritdoc/>
public event EventHandler? CanExecuteChanged;
Expand Down Expand Up @@ -50,4 +51,36 @@ public void Execute(object? parameter)
_configContext.WorkspaceManager.Activate(_workspace.Workspace, _viewModel.Monitor);
}
}

/// <inheritdoc/>
protected virtual void Dispose(bool disposing)
{
if (!_disposedValue)
{
if (disposing)
{
// dispose managed state (managed objects)
_workspace.PropertyChanged -= Workspace_PropertyChanged;
}

// free unmanaged resources (unmanaged objects) and override finalizer
// set large fields to null
_disposedValue = true;
}
}

// // override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
// ~SwitchWorkspaceCommand()
// {
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
// Dispose(disposing: false);
// }

/// <inheritdoc/>
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
32 changes: 31 additions & 1 deletion src/Whim.Bar/Workspace/WorkspaceWidget.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
using Microsoft.UI.Xaml.Controls;
using System;

namespace Whim.Bar;

/// <summary>
/// Interaction logic for WorkspaceWidget.xaml
/// </summary>
public partial class WorkspaceWidget : UserControl
public partial class WorkspaceWidget : UserControl, IDisposable
{
private readonly Microsoft.UI.Xaml.Window _window;
private bool _disposedValue;

/// <summary>
/// The workspace view model.
/// </summary>
public WorkspaceWidgetViewModel ViewModel { get; }

internal WorkspaceWidget(IConfigContext configContext, IMonitor monitor, Microsoft.UI.Xaml.Window window)
{
_window = window;
ViewModel = new WorkspaceWidgetViewModel(configContext, monitor);
window.Closed += Window_Closed;
UIElementExtensions.InitializeComponent(this, "Whim.Bar", "Workspace/WorkspaceWidget");
Expand All @@ -31,4 +36,29 @@ public static BarComponent CreateComponent()
{
return new BarComponent((configContext, monitor, window) => new WorkspaceWidget(configContext, monitor, window));
}

/// <inheritdoc/>
protected virtual void Dispose(bool disposing)
{
if (!_disposedValue)
{
if (disposing)
{
// dispose managed state (managed objects)
_window.Closed -= Window_Closed;
}

// free unmanaged resources (unmanaged objects) and override finalizer
// set large fields to null
_disposedValue = true;
}
}

/// <inheritdoc/>
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
6 changes: 3 additions & 3 deletions src/Whim.Bar/Workspace/WorkspaceWidgetViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Whim.Bar;
public class WorkspaceWidgetViewModel : INotifyPropertyChanged, IDisposable
{
private readonly IConfigContext _configContext;
private bool disposedValue;
private bool _disposedValue;

/// <summary>
/// The monitor which contains the workspaces.
Expand Down Expand Up @@ -102,7 +102,7 @@ private void WorkspaceManager_MonitorWorkspaceChanged(object? sender, MonitorWor
/// <inheritdoc/>
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
if (!_disposedValue)
{
if (disposing)
{
Expand All @@ -114,7 +114,7 @@ protected virtual void Dispose(bool disposing)

// free unmanaged resources (unmanaged objects) and override finalizer
// set large fields to null
disposedValue = true;
_disposedValue = true;
}
}

Expand Down
29 changes: 28 additions & 1 deletion src/Whim.CommandPalette/CommandPalettePlugin.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;

namespace Whim.CommandPalette;
Expand All @@ -6,10 +7,11 @@ namespace Whim.CommandPalette;
/// CommandPalettePlugin displays a rudimentary command palette window. It allows the user to
/// interact with the loaded commands of Whim.
/// </summary>
public class CommandPalettePlugin : IPlugin
public class CommandPalettePlugin : IPlugin, IDisposable
{
private readonly IConfigContext _configContext;
private CommandPaletteWindow? _commandPaletteWindow;
private bool _disposedValue;

/// <summary>
/// The configuration for the command palette plugin.
Expand Down Expand Up @@ -67,4 +69,29 @@ public void Toggle()
{
_commandPaletteWindow?.Toggle();
}

/// <inheritdoc/>
protected virtual void Dispose(bool disposing)
{
if (!_disposedValue)
{
if (disposing)
{
// dispose managed state (managed objects)
_commandPaletteWindow?.Close();
}

// free unmanaged resources (unmanaged objects) and override finalizer
// set large fields to null
_disposedValue = true;
}
}

/// <inheritdoc/>
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
36 changes: 35 additions & 1 deletion src/Whim.FocusIndicator/FocusIndicatorPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
using Microsoft.UI.Xaml;
using System;
using System.ComponentModel;

namespace Whim.FocusIndicator;

/// <summary>
/// FocusIndicatorPlugin is the plugin that displays a focus indicator on the focused window.
/// </summary>
public class FocusIndicatorPlugin : IPlugin
public class FocusIndicatorPlugin : IPlugin, IDisposable
{
private readonly IConfigContext _configContext;
private readonly FocusIndicatorConfig _focusIndicatorConfig;
private FocusIndicatorWindow? _focusIndicatorWindow;
private DispatcherTimer? _dispatcherTimer;
private bool _disposedValue;

/// <summary>
/// Creates a new instance of the focus indicator plugin.
Expand Down Expand Up @@ -129,4 +131,36 @@ private void Hide()
_dispatcherTimer.Tick -= DispatcherTimer_Tick;
}
}

/// <inheritdoc/>
protected virtual void Dispose(bool disposing)
{
if (!_disposedValue)
{
if (disposing)
{
// dispose managed state (managed objects)
_configContext.WindowManager.WindowFocused -= WindowManager_WindowFocused;
_configContext.WindowManager.WindowRegistered -= WindowManager_EventSink;
_configContext.WindowManager.WindowUnregistered -= WindowManager_EventSink;
_configContext.WindowManager.WindowMoveStart -= WindowManager_EventSink;
_configContext.WindowManager.WindowMoved -= WindowManager_EventSink;
_configContext.WindowManager.WindowMinimizeStart -= WindowManager_EventSink;
_configContext.WindowManager.WindowMinimizeEnd -= WindowManager_EventSink;
_focusIndicatorWindow?.Close();
}

// free unmanaged resources (unmanaged objects) and override finalizer
// set large fields to null
_disposedValue = true;
}
}

/// <inheritdoc/>
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
Loading

0 comments on commit 6d7c39f

Please sign in to comment.