Skip to content

Commit

Permalink
Resolves #43
Browse files Browse the repository at this point in the history
  • Loading branch information
luttje committed Oct 22, 2023
1 parent 6b38fe9 commit a0aa208
Show file tree
Hide file tree
Showing 13 changed files with 185 additions and 37 deletions.
49 changes: 49 additions & 0 deletions Core/Key2Joy.Contracts/Util/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ namespace Key2Joy.Contracts.Util;

public static class StringExtensions
{
/// <summary>
/// Ellipsizes the string to the specified length, adding ... at the end
/// </summary>
/// <param name="input"></param>
/// <param name="maxLength"></param>
/// <returns></returns>
/// <exception cref="System.ArgumentOutOfRangeException"></exception>
public static string Ellipsize(this string input, int maxLength)
{
if (input == null)
Expand All @@ -26,4 +33,46 @@ public static string Ellipsize(this string input, int maxLength)

return input.Substring(0, maxLength - 3) + "...";
}

/// <summary>
/// Places newlines in the string to make it fit the specified width
/// </summary>
/// <param name="input"></param>
/// <param name="maxCharLength"></param>
/// <returns></returns>
public static string Wrap(this string input, int maxCharLength)
{
if (input == null)
{
return null;
}

if (maxCharLength <= 0)
{
throw new System.ArgumentOutOfRangeException(nameof(maxCharLength));
}

if (input.Length <= maxCharLength)
{
return input;
}

var sb = new System.Text.StringBuilder();
var words = input.Split(' ');
var lineLength = 0;
foreach (var word in words)
{
if (lineLength + word.Length > maxCharLength)
{
sb.AppendLine();
lineLength = 0;
}

sb.Append(word);
sb.Append(' ');
lineLength += word.Length + 1;
}

return sb.ToString().TrimEnd();
}
}
1 change: 1 addition & 0 deletions Core/Key2Joy.Core/Config/ConfigControlAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace Key2Joy.Config;
public abstract class ConfigControlAttribute : Attribute
{
public string Text { get; set; }
public string Hint { get; set; } = null;

/// <summary>
/// Gets all configs and their property
Expand Down
59 changes: 47 additions & 12 deletions Core/Key2Joy.Core/Config/ConfigState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public string LastInstallPath
private string lastInstallPath;

[EnumConfigControl(
Text = "Style mapped options are grouped by",
Text = "Mapped options grouping style",
EnumType = typeof(ViewMappingGroupType)
)]
public ViewMappingGroupType SelectedViewMappingGroupType
Expand All @@ -45,17 +45,6 @@ public bool ShouldCloseButtonMinimize

private bool shouldCloseButtonMinimize;

[BooleanConfigControl(
Text = "Override default behaviour when trigger action is executed"
)]
public bool OverrideDefaultTriggerBehaviour
{
get => this.overrideDefaultTriggerBehaviour;
set => this.SaveIfInitialized(this.overrideDefaultTriggerBehaviour = value);
}

private bool overrideDefaultTriggerBehaviour = true;

[TextConfigControl(
Text = "Last loaded mapping profile file location"
)]
Expand All @@ -67,6 +56,52 @@ public string LastLoadedProfile

private string lastLoadedProfile;

[BooleanConfigControl(
Text = "While armed override the default Keyboard behaviour for mapped keys"
)]
public bool ListenerOverrideDefaultKeyboard
{
get => this.listenerOverrideDefaultKeyboard;
set => this.SaveIfInitialized(this.listenerOverrideDefaultKeyboard = value);
}

private bool listenerOverrideDefaultKeyboard = true;

[BooleanConfigControl(
Text = "While armed override the default Keyboard behaviour for all keys",
Hint = "Make sure you map an 'Abort' action to a key, so you can disarm the mappings."
)]
public bool ListenerOverrideDefaultKeyboardAll
{
get => this.listenerOverrideDefaultKeyboardAll;
set => this.SaveIfInitialized(this.listenerOverrideDefaultKeyboardAll = value);
}

private bool listenerOverrideDefaultKeyboardAll = false;

[BooleanConfigControl(
Text = "While armed override the default Mouse behaviour for mapped buttons"
)]
public bool ListenerOverrideDefaultMouse
{
get => this.listenerOverrideDefaultMouse;
set => this.SaveIfInitialized(this.listenerOverrideDefaultMouse = value);
}

private bool listenerOverrideDefaultMouse = true;

[BooleanConfigControl(
Text = "While armed override the default Mouse behaviour for all buttons",
Hint = "Make sure you map an 'Abort' action to a key, otherwise you can't click the disarm checkbox!"
)]
public bool ListenerOverrideDefaultMouseAll
{
get => this.listenerOverrideDefaultMouseAll;
set => this.SaveIfInitialized(this.listenerOverrideDefaultMouseAll = value);
}

private bool listenerOverrideDefaultMouseAll = false;

public Dictionary<string, string> EnabledPlugins { get; set; } = new Dictionary<string, string>();

private void SaveIfInitialized(object changedValue = null)
Expand Down
16 changes: 8 additions & 8 deletions Core/Key2Joy.Core/IAcceptAppCommands.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Key2Joy.Mapping.Actions.Logic;

namespace Key2Joy;

public interface IAcceptAppCommands
{
bool RunAppCommand(AppCommand command);
}
using Key2Joy.Mapping.Actions.Logic;

namespace Key2Joy;

public interface IAcceptAppCommands
{
bool RunAppCommand(AppCommand command);
}
11 changes: 5 additions & 6 deletions Core/Key2Joy.Core/Mapping/Triggers/CoreTriggerListener.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using CommonServiceLocator;
using Key2Joy.Config;
using Key2Joy.Contracts.Mapping;
using Key2Joy.Contracts.Mapping.Triggers;

Expand Down Expand Up @@ -49,10 +47,11 @@ protected override bool DoExecuteTrigger(
{
var executedAny = base.DoExecuteTrigger(mappedOptions, inputBag, optionCandidateFilter);

var configState = ServiceLocator.Current
.GetInstance<IConfigManager>()
.GetConfigState();
if (this is IOverrideDefaultBehavior overridesDefault)
{
return overridesDefault.ShouldListenerOverrideDefault(executedAny);
}

return configState.OverrideDefaultTriggerBehaviour && executedAny;
return false;
}
}
11 changes: 11 additions & 0 deletions Core/Key2Joy.Core/Mapping/Triggers/IOverrideDefaultBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Key2Joy.Mapping.Triggers;

public interface IOverrideDefaultBehavior
{
/// <summary>
/// Used by the trigger listener to determine if it should override default behavior.
/// </summary>
/// <param name="executedAny"></param>
/// <returns></returns>
bool ShouldListenerOverrideDefault(bool executedAny);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using System.Collections.Generic;
using System.Windows.Forms;
using CommonServiceLocator;
using Key2Joy.Config;
using Key2Joy.Contracts.Mapping;
using Key2Joy.Contracts.Mapping.Triggers;
using Key2Joy.LowLevelInput;

namespace Key2Joy.Mapping.Triggers.Keyboard;

public class KeyboardTriggerListener : PressReleaseTriggerListener<KeyboardTrigger>
public class KeyboardTriggerListener : PressReleaseTriggerListener<KeyboardTrigger>, IOverrideDefaultBehavior
{
private static KeyboardTriggerListener instance;

Expand All @@ -26,6 +28,23 @@ public static KeyboardTriggerListener Instance

public bool GetKeyDown(Keys key) => this.currentKeysPressed.ContainsKey(key);

/// <inheritdoc/>
public bool ShouldListenerOverrideDefault(bool executedAny)
{
var configManager = ServiceLocator.Current.GetInstance<IConfigManager>();
var config = configManager.GetConfigState();
var listenerOverrideDefaultAll = config.ListenerOverrideDefaultKeyboardAll;

if (listenerOverrideDefaultAll)
{
return true;
}

var listenerOverrideDefault = config.ListenerOverrideDefaultKeyboard;

return listenerOverrideDefault && executedAny;
}

/// <inheritdoc/>
protected override void Start()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System.Collections.Generic;
using CommonServiceLocator;
using Key2Joy.Config;
using Key2Joy.Contracts.Mapping.Triggers;
using Key2Joy.LowLevelInput;

namespace Key2Joy.Mapping.Triggers.Mouse;

public class MouseButtonTriggerListener : PressReleaseTriggerListener<MouseButtonTrigger>
public class MouseButtonTriggerListener : PressReleaseTriggerListener<MouseButtonTrigger>, IOverrideDefaultBehavior
{
public static MouseButtonTriggerListener instance;

Expand All @@ -21,7 +23,25 @@ public static MouseButtonTriggerListener Instance
private GlobalInputHook globalMouseButtonHook;
private readonly Dictionary<LowLevelInput.Mouse.Buttons, bool> currentButtonsDown = new();

public bool GetButtonsDown(LowLevelInput.Mouse.Buttons buttons) => this.currentButtonsDown.ContainsKey(buttons);
/// <inheritdoc/>
public bool ShouldListenerOverrideDefault(bool executedAny)
{
var configManager = ServiceLocator.Current.GetInstance<IConfigManager>();
var config = configManager.GetConfigState();
var listenerOverrideDefaultAll = config.ListenerOverrideDefaultMouseAll;

if (listenerOverrideDefaultAll)
{
return true;
}

var listenerOverrideDefault = config.ListenerOverrideDefaultMouse;

return listenerOverrideDefault && executedAny;
}

public bool GetButtonsDown(LowLevelInput.Mouse.Buttons buttons)
=> this.currentButtonsDown.ContainsKey(buttons);

/// <inheritdoc/>
protected override void Start()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public static MouseMoveTriggerListener Instance
private List<int> lastDirectionHashes;
private DateTime lastMoveTime;

private MouseMoveTriggerListener() => this.lookupAxis = new Dictionary<int, List<AbstractMappedOption>>();
private MouseMoveTriggerListener()
=> this.lookupAxis = new Dictionary<int, List<AbstractMappedOption>>();

/// <inheritdoc/>
protected override void Start()
Expand Down
1 change: 1 addition & 0 deletions Key2Joy.Gui/ConfigForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions Key2Joy.Gui/ConfigForm.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using CommonServiceLocator;
using Key2Joy.Config;
using Key2Joy.Contracts.Util;

namespace Key2Joy.Gui;

Expand Down Expand Up @@ -38,6 +40,21 @@ public ConfigForm()

this.pnlConfigurations.Controls.Add(controlParent);
controlParent.Dock = DockStyle.Bottom;

if (!string.IsNullOrWhiteSpace(attribute.Hint))
{
var wrapped = attribute.Hint.Wrap(80);

Label label = new()
{
AutoSize = true,
Dock = DockStyle.Bottom,
Text = wrapped,
ForeColor = Color.Gray,
Font = new Font(this.Font, FontStyle.Italic),
};
controlParent.Controls.Add(label);
}
}

this.PerformLayout();
Expand Down
6 changes: 0 additions & 6 deletions Support/Key2Joy.Tests/Core/Config/AttributeProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ public class TestConfigState
)]
public bool MuteCloseExitMessage { get; set; }

[BooleanConfigControl(
Text = "Override default behaviour when trigger action is executed"
)]
public bool OverrideDefaultTriggerBehaviour { get; set; }

[TextConfigControl(
Text = "Last loaded mapping profile file location"
)]
Expand All @@ -36,7 +31,6 @@ public void GetProperties_ReturnsAllPropertiesOfType()
var properties = this.attributeProvider.GetProperties(typeof(TestConfigState));

Assert.IsTrue(properties.Any(p => p.Name == "MuteCloseExitMessage"));
Assert.IsTrue(properties.Any(p => p.Name == "OverrideDefaultTriggerBehaviour"));
Assert.IsTrue(properties.Any(p => p.Name == "LastLoadedProfile"));
}

Expand Down
3 changes: 2 additions & 1 deletion Support/Key2Joy.Tests/Core/Config/Stubs/current-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
"LastInstallPath": "%TEST_ASSEMBLY_PATH%\\Key2Joy.exe",
"SelectedViewMappingGroupType": 1,
"ShouldCloseButtonMinimize": true,
"OverrideDefaultTriggerBehaviour": true,
"LastLoadedProfile": "%TEST_APP_DATA_PATH%\\Profiles\\default-profile.k2j.json",
"ListenerOverrideDefaultKeyboard": true,
"ListenerOverrideDefaultKeyboardAll": false,
"EnabledPlugins": {
"Plugins\\Key2Joy.Plugin.Ffmpeg\\Key2Joy.Plugin.Ffmpeg.dll": "E4AADC70FE37D2D4D8603E2EC2ACB301",
"Plugins\\Key2Joy.Plugin.HelloWorld\\Key2Joy.Plugin.HelloWorld.dll": "E4AADC70FE37D2D4D8603E2EC2ACB301",
Expand Down

0 comments on commit a0aa208

Please sign in to comment.