Skip to content

Commit

Permalink
Removed concept of InstanceGetter and moves highlighting logic into K…
Browse files Browse the repository at this point in the history
…eyStateService
  • Loading branch information
JuliusSweetland committed Aug 13, 2018
1 parent 98ba185 commit 0e4ece8
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 64 deletions.
1 change: 0 additions & 1 deletion src/JuliusSweetland.OptiKey/JuliusSweetland.OptiKey.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,6 @@
<Compile Include="Services\Audio\SoundPlayerEx.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Services\InstanceGetter.cs" />
<Compile Include="Services\Suggestions\IManagedSuggestions.cs" />
<Compile Include="Services\Suggestions\BasicAutoComplete.cs" />
<Compile Include="Services\Suggestions\ISuggestions.cs" />
Expand Down
17 changes: 8 additions & 9 deletions src/JuliusSweetland.OptiKey/Resources/Themes/Android_Dark.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<SolidColorBrush x:Key="KeyHoverForegroundBrush" Color="{StaticResource Red}" />
<SolidColorBrush x:Key="KeySelectionProgressBrush" Color="{StaticResource Red}" />
<SolidColorBrush x:Key="CapturingMultiKeySelectionBrush" Color="{StaticResource Red}" />
<SolidColorBrush x:Key="FirstMultiKeySelectionBorderBrush" Color="{StaticResource Green}"/>
<SolidColorBrush x:Key="HighlightedKeyBorderBrush" Color="{StaticResource Green}"/>

<StaticResource x:Key="KeySelectionForeground" ResourceKey="Blue"/>
<StaticResource x:Key="KeySelectionBorder" ResourceKey="Grey"/>
Expand Down Expand Up @@ -418,7 +418,7 @@
</Style.Triggers>
</Style>

<Style x:Key="KeyBorderHighlightStyle" TargetType="{x:Type Border}">
<Style x:Key="CurrentKeyBorderHighlightStyle" TargetType="{x:Type Border}">
<Setter Property="BorderThickness" Value="5" />
<Setter Property="BorderBrush" Value="{StaticResource KeyHoverForegroundBrush}" />
<Setter Property="Effect">
Expand All @@ -445,10 +445,9 @@
</Style.Triggers>
</Style>

<!--Style triggers for Green border around first typed MultiKey-->
<Style x:Key="MultiKeyBorderHighlightStyle" TargetType="{x:Type Border}">
<Style x:Key="HighlightedKeyBorderHighlightStyle" TargetType="{x:Type Border}">
<Setter Property="BorderThickness" Value="5" />
<Setter Property="BorderBrush" Value="{StaticResource FirstMultiKeySelectionBorderBrush}" />
<Setter Property="BorderBrush" Value="{StaticResource HighlightedKeyBorderBrush}" />
<Setter Property="Effect">
<Setter.Value>
<BlurEffect Radius="5.0" KernelType="Gaussian"/>
Expand All @@ -466,8 +465,8 @@
<Setter Property="BorderBrush" Value="{StaticResource KeyDisabledForegroundBrush}" />
</DataTrigger>

<!--Make visible when this key is current-->
<DataTrigger Binding="{Binding Path=IsFirstMultiKey, RelativeSource={RelativeSource AncestorType={x:Type controls:Key}}, Mode=OneWay}" Value="True">
<!--Make visible when this key is highlighted-->
<DataTrigger Binding="{Binding Path=IsHighlighted, RelativeSource={RelativeSource AncestorType={x:Type controls:Key}}, Mode=OneWay}" Value="True">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
Expand Down Expand Up @@ -539,9 +538,9 @@
<Path Grid.Row="2" Style="{StaticResource KeyLockIconStyle}" />
</Grid>
</Border>
<Border Style="{StaticResource KeyBorderHighlightStyle}" />
<Border Style="{StaticResource CurrentKeyBorderHighlightStyle}" />
<!--Custom border style for multiKey-->
<Border Style="{StaticResource MultiKeyBorderHighlightStyle}" />
<Border Style="{StaticResource HighlightedKeyBorderHighlightStyle}" />
</Grid>
</ControlTemplate>
</Setter.Value>
Expand Down
17 changes: 8 additions & 9 deletions src/JuliusSweetland.OptiKey/Resources/Themes/Android_Light.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<SolidColorBrush x:Key="KeyHoverForegroundBrush" Color="{StaticResource Red}" />
<SolidColorBrush x:Key="KeySelectionProgressBrush" Color="{StaticResource Red}" />
<SolidColorBrush x:Key="CapturingMultiKeySelectionBrush" Color="{StaticResource Red}" />
<SolidColorBrush x:Key="FirstMultiKeySelectionBorderBrush" Color="{StaticResource Green}"/>
<SolidColorBrush x:Key="HighlightedKeyBorderBrush" Color="{StaticResource Green}"/>

<StaticResource x:Key="KeySelectionForeground" ResourceKey="Blue"/>
<StaticResource x:Key="KeySelectionBorder" ResourceKey="Grey"/>
Expand Down Expand Up @@ -418,7 +418,7 @@
</Style.Triggers>
</Style>

<Style x:Key="KeyBorderHighlightStyle" TargetType="{x:Type Border}">
<Style x:Key="CurrentKeyBorderHighlightStyle" TargetType="{x:Type Border}">
<Setter Property="BorderThickness" Value="5" />
<Setter Property="BorderBrush" Value="{StaticResource KeyHoverForegroundBrush}" />
<Setter Property="Effect">
Expand All @@ -445,10 +445,9 @@
</Style.Triggers>
</Style>

<!--Style triggers for Green border around first typed MultiKey-->
<Style x:Key="MultiKeyBorderHighlightStyle" TargetType="{x:Type Border}">
<Style x:Key="HighlightedKeyBorderHighlightStyle" TargetType="{x:Type Border}">
<Setter Property="BorderThickness" Value="5" />
<Setter Property="BorderBrush" Value="{StaticResource FirstMultiKeySelectionBorderBrush}" />
<Setter Property="BorderBrush" Value="{StaticResource HighlightedKeyBorderBrush}" />
<Setter Property="Effect">
<Setter.Value>
<BlurEffect Radius="5.0" KernelType="Gaussian"/>
Expand All @@ -466,8 +465,8 @@
<Setter Property="BorderBrush" Value="{StaticResource KeyDisabledForegroundBrush}" />
</DataTrigger>

<!--Make visible when this key is current-->
<DataTrigger Binding="{Binding Path=IsFirstMultiKey, RelativeSource={RelativeSource AncestorType={x:Type controls:Key}}, Mode=OneWay}" Value="True">
<!--Make visible when this key is highlighted-->
<DataTrigger Binding="{Binding Path=IsHighlighted, RelativeSource={RelativeSource AncestorType={x:Type controls:Key}}, Mode=OneWay}" Value="True">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
Expand Down Expand Up @@ -539,9 +538,9 @@
<Path Grid.Row="2" Style="{StaticResource KeyLockIconStyle}" />
</Grid>
</Border>
<Border Style="{StaticResource KeyBorderHighlightStyle}" />
<Border Style="{StaticResource CurrentKeyBorderHighlightStyle}" />
<!--Custom border style for multiKey-->
<Border Style="{StaticResource MultiKeyBorderHighlightStyle}" />
<Border Style="{StaticResource HighlightedKeyBorderHighlightStyle}" />
</Grid>
</ControlTemplate>
</Setter.Value>
Expand Down
2 changes: 2 additions & 0 deletions src/JuliusSweetland.OptiKey/Services/IKeyStateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ public interface IKeyStateService : INotifyPropertyChanged
bool SimulateKeyStrokes { get; set; }
NotifyingConcurrentDictionary<KeyValue, double> KeySelectionProgress { get; }
NotifyingConcurrentDictionary<KeyValue, KeyDownStates> KeyDownStates { get; }
NotifyingConcurrentDictionary<KeyValue, bool> KeyHighlightStates { get; }
KeyEnabledStates KeyEnabledStates { get; }

void SetKeyHighlightState(KeyValue keyValue, bool highlight);
void ProgressKeyDownState(KeyValue keyValue);
}
}
17 changes: 5 additions & 12 deletions src/JuliusSweetland.OptiKey/Services/InputService.subscriptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public partial class InputService

private TriggerSignal? startMultiKeySelectionTriggerSignal;
private TriggerSignal? stopMultiKeySelectionTriggerSignal;
private UI.Controls.Key firstMultiKeyTyped;

#endregion

Expand Down Expand Up @@ -178,14 +177,9 @@ private void ProcessSelectionTrigger(TriggerSignal triggerSignal)

PublishSelection(triggerSignal.PointAndKeyValue);

//Identifying the Controls.Key that is pressed, and setting the IsFirstMultiKey property to true in order to show the green border:
UI.Controls.Key firstKeyInMultiKeySelection = InstanceGetter.Instance.allKeys.Find(k => k.ShiftUpText == triggerSignal.PointAndKeyValue.KeyValue.String);
if (firstKeyInMultiKeySelection != null)
{
firstKeyInMultiKeySelection.IsFirstMultiKey = true;
firstMultiKeyTyped = firstKeyInMultiKeySelection;
}

//Set the key's IsHighlighted property in order to show the green border
keyStateService.KeyHighlightStates[triggerSignal.PointAndKeyValue.KeyValue].Value = true;

multiKeySelectionSubscription =
CreateMultiKeySelectionSubscription()
.ObserveOnDispatcher()
Expand All @@ -205,9 +199,8 @@ private void ProcessSelectionTrigger(TriggerSignal triggerSignal)
stopMultiKeySelectionTriggerSignal = null;
CapturingMultiKeySelection = false;
//Set IsFirstMultiKey to false in order to remove the green border:
firstMultiKeyTyped.IsFirstMultiKey = false;
firstMultiKeyTyped = null;
//Set the key's IsHighlighted to false in order to remove the green border
keyStateService.KeyHighlightStates[triggerSignal.PointAndKeyValue.KeyValue].Value = false;
});
}
else
Expand Down
25 changes: 0 additions & 25 deletions src/JuliusSweetland.OptiKey/Services/InstanceGetter.cs

This file was deleted.

8 changes: 8 additions & 0 deletions src/JuliusSweetland.OptiKey/Services/KeyStateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class KeyStateService : BindableBase, IKeyStateService

private readonly NotifyingConcurrentDictionary<KeyValue, double> keySelectionProgress;
private readonly NotifyingConcurrentDictionary<KeyValue, KeyDownStates> keyDownStates;
private readonly NotifyingConcurrentDictionary<KeyValue, bool> keyHighlightStates;
private readonly KeyEnabledStates keyEnabledStates;
private readonly Action<KeyValue> fireKeySelectionEvent;
private readonly Dictionary<bool, KeyStateServiceState> state = new Dictionary<bool, KeyStateServiceState>();
Expand All @@ -41,6 +42,7 @@ public KeyStateService(
this.fireKeySelectionEvent = fireKeySelectionEvent;
this.keySelectionProgress = new NotifyingConcurrentDictionary<KeyValue, double>();
this.keyDownStates = new NotifyingConcurrentDictionary<KeyValue, KeyDownStates>();
this.keyHighlightStates = new NotifyingConcurrentDictionary<KeyValue, bool>();
this.keyEnabledStates = new KeyEnabledStates(this, suggestionService, capturingStateManager, lastMouseActionStateManager, calibrationService);

InitialiseKeyDownStates();
Expand All @@ -60,6 +62,7 @@ public bool SimulateKeyStrokes
}
public NotifyingConcurrentDictionary<KeyValue, double> KeySelectionProgress { get { return keySelectionProgress; } }
public NotifyingConcurrentDictionary<KeyValue, KeyDownStates> KeyDownStates { get { return keyDownStates; } }
public NotifyingConcurrentDictionary<KeyValue, bool> KeyHighlightStates { get { return keyHighlightStates; } }
public KeyEnabledStates KeyEnabledStates { get { return keyEnabledStates; } }

#endregion
Expand Down Expand Up @@ -98,6 +101,11 @@ public void ProgressKeyDownState(KeyValue keyValue)
}
}

public void SetKeyHighlightState(KeyValue keyValue, bool highlight)
{
keyHighlightStates[keyValue].Value = highlight;
}

#endregion

#region Private Methods
Expand Down
20 changes: 15 additions & 5 deletions src/JuliusSweetland.OptiKey/UI/Controls/Key.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
onUnloaded.Add(currentPositionSubscription);
calculateIsCurrent(mainViewModel.CurrentPositionKey);

//Calculate IsHighlighted
if (Value != null)
{
var keyHighlightedSubscription = keyStateService.KeyHighlightStates[Value]
.OnPropertyChanges(ksp => ksp.Value)
.Subscribe(value => IsHighlighted = value);
onUnloaded.Add(keyHighlightedSubscription);
}
IsHighlighted = Value != null && keyStateService.KeyHighlightStates[Value].Value;

//Calculate DisplayShiftDownText
//Display shift down text (upper case text) if shift is locked down, or down (but NOT when we are capturing a multi key selection)
Action<KeyDownStates, bool> calculateDisplayShiftDownText = (shiftDownState, capturingMultiKeySelection) =>
Expand Down Expand Up @@ -175,13 +185,13 @@ public bool IsCurrent
}

//Should be true when key is the first selected key in multiKey sequence, and false otherwise.
public static readonly DependencyProperty IsFirstMultiKeyProperty =
DependencyProperty.Register("IsFirstMultiKey", typeof(bool), typeof(Key), new PropertyMetadata(default(bool)));
public static readonly DependencyProperty IsHighlightedProperty =
DependencyProperty.Register("IsHighlighted", typeof(bool), typeof(Key), new PropertyMetadata(default(bool)));

public bool IsFirstMultiKey
public bool IsHighlighted
{
get { return (bool)GetValue(IsFirstMultiKeyProperty); }
set { SetValue(IsFirstMultiKeyProperty, value); }
get { return (bool)GetValue(IsHighlightedProperty); }
set { SetValue(IsHighlightedProperty, value); }
}

public static readonly DependencyProperty SelectionProgressProperty =
Expand Down
3 changes: 0 additions & 3 deletions src/JuliusSweetland.OptiKey/UI/Controls/KeyboardHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -511,10 +511,7 @@ private void BuildPointToKeyMap()
private void TraverseAllKeysAndBuildPointToKeyValueMap()
{
var allKeys = VisualAndLogicalTreeHelper.FindVisualChildren<Key>(this).ToList();
InstanceGetter.Instance.allKeys = allKeys;

var pointToKeyValueMap = new Dictionary<Rect, KeyValue>();

var topLeftPoint = new Point(0, 0);

foreach (var key in allKeys)
Expand Down

0 comments on commit 0e4ece8

Please sign in to comment.