Skip to content

Commit

Permalink
Merge pull request #23 from LegendaryB/develop
Browse files Browse the repository at this point in the history
Release v1.3
  • Loading branch information
LegendaryB authored Sep 5, 2019
2 parents cf24569 + e3579dc commit f00a3c9
Show file tree
Hide file tree
Showing 19 changed files with 180 additions and 56 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ The `appsettings.json` file resides in the same folder as the application.
"InputPollingRate": 0
},
"Actions": {
"PreventSleep": true,
"HideDesktopIcons": true,
"HideTaskBar": true,
"HideApplicationWindows": true
Expand Down
11 changes: 0 additions & 11 deletions src/Kirei.Application/IInputHandler.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Kirei.Application
namespace Kirei.Application.System.Desktop
{
public interface IDesktop
public interface IDesktopService
{
void ToggleWindows();
void ToggleIcons();
Expand Down
7 changes: 7 additions & 0 deletions src/Kirei.Application/System/IHibernationService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Kirei.Application.System
{
public interface IHibernationService
{
void PreventSleep();
}
}
10 changes: 10 additions & 0 deletions src/Kirei.Application/System/Input/IInputActionMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace Kirei.Application.System.Input
{
public interface IInputActionMapper
{
void HandleInput();
void RegisterAction(Action action, Func<bool> condition);
}
}
9 changes: 9 additions & 0 deletions src/Kirei.Application/System/Input/IInputListener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace Kirei.Application.System.Input
{
public interface IInputListener
{
void Listen(IInputActionMapper inputActionMapper);
}
}
1 change: 1 addition & 0 deletions src/Kirei.Domain/Configuration/ActionsSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
public class ActionsSection
{
public bool PreventSleep { get; set; }
public bool HideDesktopIcons { get; set; }
public bool HideTaskBar { get; set; }
public bool HideApplicationWindows { get; set; }
Expand Down
11 changes: 11 additions & 0 deletions src/Kirei.Domain/Native/Enums/ExecutionStates.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace Kirei.Domain.Native.Enums
{
[Flags]
public enum ExecutionStates : uint
{
ES_DISPLAY_REQUIRED = 0x00000002,
ES_CONTINUOUS = 0x80000000
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

using System;

namespace Kirei.Domain.DesktopAPI
namespace Kirei.Domain.System.Desktop
{
public class TaskBarDescriptor
{
Expand Down
12 changes: 12 additions & 0 deletions src/Kirei.Infrastructure/Native/Kernel32.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Kirei.Domain.Native.Enums;

using System.Runtime.InteropServices;

namespace Kirei.Infrastructure.Native
{
internal static class Kernel32
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern ExecutionStates SetThreadExecutionState(ExecutionStates esFlags);
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
using Kirei.Application;
using Kirei.Application.System.Desktop;

namespace Kirei.Infrastructure.DesktopAPI
namespace Kirei.Infrastructure.System.Desktop
{
public class Desktop :
IDesktop
public class DesktopService :
IDesktopService
{
private readonly Shell _shell;
private readonly TaskBar _taskBar;

private bool windowsMinimized = false;
private bool taskBarHidden = false;

public Desktop()
public DesktopService()
{
_shell = new Shell();
_taskBar = new TaskBar();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using System;

namespace Kirei.Infrastructure.DesktopAPI
namespace Kirei.Infrastructure.System.Desktop
{
internal class Shell
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using Kirei.Infrastructure.Native;
using Kirei.Domain.Native.Enums;
using Kirei.Domain.Native.Structures;
using Kirei.Domain.DesktopAPI;
using Kirei.Domain.System.Desktop;

using System;
using System.Collections.Generic;

namespace Kirei.Infrastructure.DesktopAPI

namespace Kirei.Infrastructure.System.Desktop
{
internal class TaskBar
{
Expand Down
27 changes: 27 additions & 0 deletions src/Kirei.Infrastructure/System/HibernationService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Kirei.Application.System;
using Kirei.Domain.Native.Enums;
using Kirei.Infrastructure.Native;

using System.Threading.Tasks;

namespace Kirei.Infrastructure.System
{
public class HibernationService : IHibernationService
{
public void PreventSleep()
{
Task.Run(async () =>
{
while (true)
{
Kernel32.SetThreadExecutionState(
ExecutionStates.ES_CONTINUOUS |
ExecutionStates.ES_DISPLAY_REQUIRED);
await Task.Delay(5000);
}
});
}

}
}
33 changes: 33 additions & 0 deletions src/Kirei.Infrastructure/System/Input/InputActionMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Kirei.Application.System.Input;

using System;
using System.Collections.Generic;

namespace Kirei.Infrastructure.System.Input
{
public class InputActionMapper : IInputActionMapper
{
private readonly List<Action> _actionsOnInput;

public InputActionMapper()
{
_actionsOnInput = new List<Action>();
}

public void HandleInput()
{
foreach (var action in _actionsOnInput)
action?.Invoke();
}

public void RegisterAction(
Action action,
Func<bool> condition)
{
if (action == null || condition?.Invoke() != true)
return;

_actionsOnInput.Add(action);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
using Kirei.Application;
using Kirei.Application.System.Input;
using Kirei.Infrastructure.Configuration;
using Kirei.Infrastructure.Native;

using System;
using System.Threading;

namespace Kirei.Infrastructure
namespace Kirei.Infrastructure.System.Input
{
public class InputHandler :
IInputHandler
public class InputListener :
IInputListener
{
private IInputActionMapper _inputActionMapper;
private bool hasIconsBeenHidden = false;

public Action Handler { get; set; }

public void Handle()
public void Listen(IInputActionMapper inputActionMapper)
{
_inputActionMapper = inputActionMapper;

while (true)
{
var lastInputInMilliseconds = User32.GetUserIdleTime();
var inactiveAfterMs = ConfigurationProvider.Configuration.Application.InactiveAfterMs;

if (lastInputInMilliseconds >= inactiveAfterMs && !hasIconsBeenHidden)
{
Handler?.Invoke();
_inputActionMapper.HandleInput();
hasIconsBeenHidden = true;
}
else if (lastInputInMilliseconds < inactiveAfterMs && hasIconsBeenHidden)
{
Handler?.Invoke();
_inputActionMapper.HandleInput();
hasIconsBeenHidden = false;
}
}

Thread.Sleep(ConfigurationProvider.Configuration.Application.InputPollingRate);
}
Expand Down
53 changes: 34 additions & 19 deletions src/Kirei/App.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,57 @@
using Kirei.Application;
using Kirei.Application.System;
using Kirei.Application.System.Desktop;
using Kirei.Application.System.Input;
using Kirei.Infrastructure.Configuration;

namespace Kirei
{
internal class App
{
private readonly IInstallWizard _installWizard;
private readonly IDesktop _desktopAPI;
private readonly IInputHandler _inputHandler;
private readonly IInstallWizard _installWizard;
private readonly IInputListener _inputListener;
private readonly IInputActionMapper _inputActionMapper;
private readonly IDesktopService _desktopService;
private readonly IHibernationService _hibernationService;

public App(IInstallWizard installWizard,
IDesktop desktopAPI,
IInputHandler inputHandler)
IInputListener inputListener,
IInputActionMapper inputActionMapper,
IDesktopService desktopService,
IHibernationService hibernationService)
{
_installWizard = installWizard;
_desktopAPI = desktopAPI;
_inputHandler = inputHandler;
_installWizard = installWizard;
_inputListener = inputListener;
_inputActionMapper = inputActionMapper;
_desktopService = desktopService;
_hibernationService = hibernationService;
}

internal void Run()
{
if (ConfigurationProvider.Configuration.Application.ShouldRunOnStartup)
_installWizard.RunOnStartup();

_inputHandler.Handler = OnUserActiveOrInactive;
_inputHandler.Handle();
}
var cfg = ConfigurationProvider
.Configuration
.Actions;

private void OnUserActiveOrInactive()
{
if (ConfigurationProvider.Configuration.Actions.HideDesktopIcons)
_desktopAPI.ToggleIcons();
_inputActionMapper.RegisterAction(
_desktopService.ToggleIcons,
() => cfg.HideDesktopIcons);

_inputActionMapper.RegisterAction(
_desktopService.ToggleTaskBar,
() => cfg.HideTaskBar);

_inputActionMapper.RegisterAction(
_desktopService.ToggleWindows,
() => cfg.HideApplicationWindows);

if (ConfigurationProvider.Configuration.Actions.HideTaskBar)
_desktopAPI.ToggleTaskBar();
if (cfg.PreventSleep)
_hibernationService.PreventSleep();

if (ConfigurationProvider.Configuration.Actions.HideApplicationWindows)
_desktopAPI.ToggleWindows();
_inputListener.Listen(_inputActionMapper);
}
}
}
13 changes: 10 additions & 3 deletions src/Kirei/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
using Kirei.Application;
using Kirei.Application.System;
using Kirei.Application.System.Desktop;
using Kirei.Application.System.Input;
using Kirei.Infrastructure;
using Kirei.Infrastructure.DesktopAPI;
using Kirei.Infrastructure.System;
using Kirei.Infrastructure.System.Desktop;
using Kirei.Infrastructure.System.Input;

using Microsoft.Extensions.DependencyInjection;

Expand All @@ -20,8 +25,10 @@ private static ServiceProvider ConfigureServices()
{
return new ServiceCollection()
.AddSingleton<IInstallWizard, InstallWizard>()
.AddSingleton<IDesktop, Desktop>()
.AddSingleton<IInputHandler, InputHandler>()
.AddSingleton<IDesktopService, DesktopService>()
.AddSingleton<IInputActionMapper, InputActionMapper>()
.AddSingleton<IInputListener, InputListener>()
.AddSingleton<IHibernationService, HibernationService>()
.AddSingleton<App>()
.BuildServiceProvider();
}
Expand Down
3 changes: 2 additions & 1 deletion src/Kirei/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"Application": {
"ShouldRunOnStartup": true,
"InactiveAfter": 0,
"InactiveAfter": 5,
"InputPollingRate": 0
},
"Actions": {
"PreventSleep": true,
"HideDesktopIcons": true,
"HideTaskBar": true,
"HideApplicationWindows": true
Expand Down

0 comments on commit f00a3c9

Please sign in to comment.