-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
474 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,145 +1,151 @@ | ||
using System; | ||
|
||
namespace Whim; | ||
|
||
/// <summary> | ||
/// Implementation of <see cref="IContext"/>. This is the core of Whim. <br/> | ||
/// | ||
/// <c>Context</c> consists of managers which contain and control Whim's state, and thus | ||
/// functionality. <br/> | ||
/// | ||
/// <c>Context</c> also contains other associated state and functionality, like the | ||
/// <see cref="Logger"/>. | ||
/// </summary> | ||
internal class Context : IContext | ||
{ | ||
private readonly IInternalContext _internalContext; | ||
public IButler Butler { get; } | ||
public IFileManager FileManager { get; } | ||
public IResourceManager ResourceManager { get; } | ||
public Logger Logger { get; } | ||
public UncaughtExceptionHandling UncaughtExceptionHandling { get; set; } = UncaughtExceptionHandling.Log; | ||
public INativeManager NativeManager { get; } | ||
public IWorkspaceManager WorkspaceManager { get; } | ||
public IWindowManager WindowManager { get; } | ||
public IMonitorManager MonitorManager { get; } | ||
public IRouterManager RouterManager { get; } | ||
public IFilterManager FilterManager { get; } | ||
private readonly CommandManager _commandManager; | ||
public ICommandManager CommandManager => _commandManager; | ||
public IPluginManager PluginManager { get; } | ||
public IKeybindManager KeybindManager { get; } | ||
public INotificationManager NotificationManager { get; } | ||
|
||
public event EventHandler<ExitEventArgs>? Exiting; | ||
public event EventHandler<ExitEventArgs>? Exited; | ||
|
||
/// <summary> | ||
/// Create a new <see cref="IContext"/>. | ||
/// </summary> | ||
public Context() | ||
{ | ||
string[] args = Environment.GetCommandLineArgs(); | ||
|
||
FileManager = new FileManager(args); | ||
Logger = new Logger(); | ||
ResourceManager = new ResourceManager(); | ||
_internalContext = new InternalContext(this); | ||
Butler = new Butler(this, _internalContext); | ||
|
||
NativeManager = new NativeManager(this, _internalContext); | ||
|
||
RouterManager = new RouterManager(this); | ||
FilterManager = new FilterManager(); | ||
WindowManager = new WindowManager(this, _internalContext); | ||
MonitorManager = new MonitorManager(this, _internalContext); | ||
WorkspaceManager = new WorkspaceManager(this, _internalContext); | ||
_commandManager = new CommandManager(); | ||
PluginManager = new PluginManager(this, _commandManager); | ||
KeybindManager = new KeybindManager(this); | ||
NotificationManager = new NotificationManager(this); | ||
} | ||
|
||
public void Initialize() | ||
{ | ||
// Load the core commands | ||
CoreCommands coreCommands = new(this); | ||
|
||
foreach (ICommand command in coreCommands.Commands) | ||
{ | ||
_commandManager.AddPluginCommand(command); | ||
} | ||
|
||
foreach ((string name, IKeybind keybind) in coreCommands.Keybinds) | ||
{ | ||
KeybindManager.SetKeybind(name, keybind); | ||
} | ||
|
||
DefaultFilteredWindows.LoadWindowsIgnoredByWhim(FilterManager); | ||
|
||
// Initialize before ResourceManager so user dicts take precedence over the default dict. | ||
ResourceManager.Initialize(); | ||
|
||
// Load the user's config. | ||
ConfigLoader configLoader = new(FileManager); | ||
DoConfig doConfig = configLoader.LoadConfig(); | ||
doConfig(this); | ||
|
||
// Initialize the logger first. | ||
Logger.Initialize(FileManager); | ||
|
||
// Initialize the managers. | ||
Logger.Debug("Initializing..."); | ||
_internalContext.PreInitialize(); | ||
PluginManager.PreInitialize(); | ||
|
||
NotificationManager.Initialize(); | ||
MonitorManager.Initialize(); | ||
WindowManager.Initialize(); | ||
WorkspaceManager.Initialize(); | ||
|
||
Butler.Initialize(); | ||
|
||
WindowManager.PostInitialize(); | ||
PluginManager.PostInitialize(); | ||
_internalContext.PostInitialize(); | ||
|
||
Logger.Debug("Completed initialization"); | ||
} | ||
|
||
public void HandleUncaughtException(string procName, Exception exception) | ||
{ | ||
Logger.Fatal($"Unhandled exception in {procName}: {exception}"); | ||
|
||
switch (UncaughtExceptionHandling) | ||
{ | ||
case UncaughtExceptionHandling.Shutdown: | ||
Exit(new ExitEventArgs() { Reason = ExitReason.Error, Message = exception.ToString() }); | ||
break; | ||
|
||
case UncaughtExceptionHandling.Log: | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
public void Exit(ExitEventArgs? args = null) | ||
{ | ||
Logger.Debug("Exiting context..."); | ||
args ??= new ExitEventArgs() { Reason = ExitReason.User }; | ||
|
||
Exiting?.Invoke(this, args); | ||
|
||
PluginManager.Dispose(); | ||
WorkspaceManager.Dispose(); | ||
WindowManager.Dispose(); | ||
MonitorManager.Dispose(); | ||
NotificationManager.Dispose(); | ||
_internalContext.Dispose(); | ||
|
||
Logger.Debug("Mostly exited..."); | ||
|
||
Logger.Dispose(); | ||
Exited?.Invoke(this, args); | ||
} | ||
} | ||
using System; | ||
|
||
namespace Whim; | ||
|
||
/// <summary> | ||
/// Implementation of <see cref="IContext"/>. This is the core of Whim. <br/> | ||
/// | ||
/// <c>Context</c> consists of managers which contain and control Whim's state, and thus | ||
/// functionality. <br/> | ||
/// | ||
/// <c>Context</c> also contains other associated state and functionality, like the | ||
/// <see cref="Logger"/>. | ||
/// </summary> | ||
internal class Context : IContext | ||
{ | ||
private readonly IInternalContext _internalContext; | ||
public IButler Butler { get; } | ||
public IFileManager FileManager { get; } | ||
public IResourceManager ResourceManager { get; } | ||
public Logger Logger { get; } | ||
public UncaughtExceptionHandling UncaughtExceptionHandling { get; set; } = UncaughtExceptionHandling.Log; | ||
public INativeManager NativeManager { get; } | ||
public IWorkspaceManager WorkspaceManager { get; } | ||
public IWindowManager WindowManager { get; } | ||
public IMonitorManager MonitorManager { get; } | ||
public IRouterManager RouterManager { get; } | ||
public IFilterManager FilterManager { get; } | ||
private readonly CommandManager _commandManager; | ||
public ICommandManager CommandManager => _commandManager; | ||
public IPluginManager PluginManager { get; } | ||
public IKeybindManager KeybindManager { get; } | ||
public INotificationManager NotificationManager { get; } | ||
|
||
public IStore Store { get; } | ||
|
||
public event EventHandler<ExitEventArgs>? Exiting; | ||
public event EventHandler<ExitEventArgs>? Exited; | ||
|
||
/// <summary> | ||
/// Create a new <see cref="IContext"/>. | ||
/// </summary> | ||
public Context() | ||
{ | ||
string[] args = Environment.GetCommandLineArgs(); | ||
|
||
FileManager = new FileManager(args); | ||
Logger = new Logger(); | ||
ResourceManager = new ResourceManager(); | ||
_internalContext = new InternalContext(this); | ||
|
||
Store = new Store(this, _internalContext); | ||
Butler = new Butler(this, _internalContext); | ||
|
||
NativeManager = new NativeManager(this, _internalContext); | ||
|
||
RouterManager = new RouterManager(this); | ||
FilterManager = new FilterManager(); | ||
WindowManager = new WindowManager(this, _internalContext); | ||
MonitorManager = new MonitorManager(this, _internalContext); | ||
WorkspaceManager = new WorkspaceManager(this, _internalContext); | ||
_commandManager = new CommandManager(); | ||
PluginManager = new PluginManager(this, _commandManager); | ||
KeybindManager = new KeybindManager(this); | ||
NotificationManager = new NotificationManager(this); | ||
} | ||
|
||
public void Initialize() | ||
{ | ||
// Load the core commands | ||
CoreCommands coreCommands = new(this); | ||
|
||
foreach (ICommand command in coreCommands.Commands) | ||
{ | ||
_commandManager.AddPluginCommand(command); | ||
} | ||
|
||
foreach ((string name, IKeybind keybind) in coreCommands.Keybinds) | ||
{ | ||
KeybindManager.SetKeybind(name, keybind); | ||
} | ||
|
||
DefaultFilteredWindows.LoadWindowsIgnoredByWhim(FilterManager); | ||
|
||
// Initialize before ResourceManager so user dicts take precedence over the default dict. | ||
ResourceManager.Initialize(); | ||
|
||
// Load the user's config. | ||
ConfigLoader configLoader = new(FileManager); | ||
DoConfig doConfig = configLoader.LoadConfig(); | ||
doConfig(this); | ||
|
||
// Initialize the logger first. | ||
Logger.Initialize(FileManager); | ||
|
||
// Initialize the managers. | ||
Logger.Debug("Initializing..."); | ||
_internalContext.PreInitialize(); | ||
Store.Initialize(); | ||
PluginManager.PreInitialize(); | ||
|
||
NotificationManager.Initialize(); | ||
MonitorManager.Initialize(); | ||
WindowManager.Initialize(); | ||
WorkspaceManager.Initialize(); | ||
|
||
Butler.Initialize(); | ||
|
||
WindowManager.PostInitialize(); | ||
PluginManager.PostInitialize(); | ||
_internalContext.PostInitialize(); | ||
|
||
Logger.Debug("Completed initialization"); | ||
} | ||
|
||
public void HandleUncaughtException(string procName, Exception exception) | ||
{ | ||
Logger.Fatal($"Unhandled exception in {procName}: {exception}"); | ||
|
||
switch (UncaughtExceptionHandling) | ||
{ | ||
case UncaughtExceptionHandling.Shutdown: | ||
Exit(new ExitEventArgs() { Reason = ExitReason.Error, Message = exception.ToString() }); | ||
break; | ||
|
||
case UncaughtExceptionHandling.Log: | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
public void Exit(ExitEventArgs? args = null) | ||
{ | ||
Logger.Debug("Exiting context..."); | ||
args ??= new ExitEventArgs() { Reason = ExitReason.User }; | ||
|
||
Exiting?.Invoke(this, args); | ||
|
||
PluginManager.Dispose(); | ||
WorkspaceManager.Dispose(); | ||
WindowManager.Dispose(); | ||
MonitorManager.Dispose(); | ||
NotificationManager.Dispose(); | ||
Store.Dispose(); | ||
_internalContext.Dispose(); | ||
|
||
Logger.Debug("Mostly exited..."); | ||
|
||
Logger.Dispose(); | ||
Exited?.Invoke(this, args); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System; | ||
using DotNext; | ||
|
||
namespace Whim; | ||
|
||
/// <summary> | ||
/// Whim's store. | ||
/// </summary> | ||
public interface IStore : IDisposable | ||
{ | ||
/// <summary> | ||
/// Initialize the event listeners. | ||
/// </summary> | ||
public void Initialize(); | ||
|
||
/// <summary> | ||
/// Dispatch updates to transform Whim's state. | ||
/// </summary> | ||
/// <typeparam name="TResult"> | ||
/// The result from the transform, if it's successful. | ||
/// </typeparam> | ||
/// <param name="transform"> | ||
/// The record implementing <see cref="Dispatch"/> to update Whim's state. | ||
/// </param> | ||
/// <returns></returns> | ||
public Result<TResult> Dispatch<TResult>(Transform<TResult> transform); | ||
|
||
/// <summary> | ||
/// Entry-point to pick from Whim's state. | ||
/// </summary> | ||
/// <typeparam name="TResult"> | ||
/// The type of the resulting data from the store. | ||
/// </typeparam> | ||
/// <param name="picker"> | ||
/// The record implementing <see cref="Picker{TResult}"/> to fetch from Whim's state. | ||
/// </param> | ||
/// <returns></returns> | ||
public TResult Pick<TResult>(Picker<TResult> picker); | ||
|
||
/// <summary> | ||
/// Entry-point to pick from Whim's state. | ||
/// </summary> | ||
/// <typeparam name="TResult"> | ||
/// The type of the resulting data from the store. | ||
/// </typeparam> | ||
/// <param name="picker"> | ||
/// Pure picker to fetch from Whim's state. | ||
/// </param> | ||
/// <returns></returns> | ||
public TResult Pick<TResult>(PurePicker<TResult> picker); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
namespace Whim; | ||
|
||
/// <summary> | ||
/// Description of how to retrieve data from the <see cref="Store"/>. | ||
/// The implementing record should be populated with the payload. | ||
/// </summary> | ||
/// <typeparam name="TResult">The type of the resulting data from the store.</typeparam> | ||
public abstract record Picker<TResult>() | ||
{ | ||
/// <summary> | ||
/// How to fetch the data from the store. | ||
/// </summary> | ||
/// <param name="ctx">Whim's context.</param> | ||
/// <param name="internalCtx">Internal-only parts of Whim's API.</param> | ||
/// <param name="rootSector"></param> | ||
/// <returns></returns> | ||
internal abstract TResult Execute(IContext ctx, IInternalContext internalCtx, IRootSector rootSector); | ||
} | ||
|
||
/// <summary> | ||
/// Description of how to retrieve data from the <see cref="Store"/>. | ||
/// Pure pickers can be implemented in terms of pure functions. | ||
/// </summary> | ||
/// <typeparam name="TResult"></typeparam> | ||
/// <param name="rootSector"></param> | ||
/// <returns></returns> | ||
public delegate TResult PurePicker<TResult>(IRootSector rootSector); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Whim; | ||
|
||
/// <summary> | ||
/// The root sector of the state. This is read-only. | ||
/// </summary> | ||
public interface IRootSector { } |
Oops, something went wrong.