Skip to content

Commit

Permalink
Merge pull request #24 from DSaladinCH/release/0.13
Browse files Browse the repository at this point in the history
Release version 0.13
  • Loading branch information
DominicSaladin authored Dec 21, 2023
2 parents 1e256d9 + bad7acd commit 6ebef94
Show file tree
Hide file tree
Showing 22 changed files with 556 additions and 443 deletions.
60 changes: 43 additions & 17 deletions speed-time/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ record AppVersion(Version Version, DateTime ReleaseDate);
internal static readonly IDataService DataService = new FileDataService();

private readonly HotKeyManager hotKeyManager = new();
private HotKey? openQuickTimeTracker;
private HotKey? currentQuickTimeHotKey;

private TrackTime? trackTimeBeforePause;
private TrackTime? trackTimePause;
Expand Down Expand Up @@ -69,30 +69,29 @@ protected override async void OnStartup(StartupEventArgs e)
}
}

openQuickTimeTracker = hotKeyManager.Register(Key.T, ModifierKeys.Control | ModifierKeys.Alt);
hotKeyManager.KeyPressed += HotKeyManagerPressed;

SystemEvents.SessionSwitch += new SessionSwitchEventHandler(SystemEvents_SessionSwitch);
RegisterQuickEntryHotKeyStartup();

base.OnStartup(e);
LiveChartsStartup();

//LiveCharts.Configure(config =>
//{
// config.AddDarkTheme((d) =>
// {
// d.Colors[0] = ResourceToLvcColor("AccentA");
// d.Colors[1] = ResourceToLvcColor("AccentD");
// d.Colors[2] = ResourceToLvcColor("AccentG");
// });
//});
MainWindow mainWindow = new();
mainWindow.Show();
}

private void RegisterQuickEntryHotKeyStartup()
{
hotKeyManager.KeyPressed += HotKeyManagerPressed;
RegisteredHotKey registeredHotKey = SettingsModel.Instance.GetRegisteredHotKey(RegisteredHotKey.HotKeyType.QuickEntry);
RegisterQuickTimeHotkey(registeredHotKey.GetGlobalHotKey());
}

private void LiveChartsStartup()
{
ColorManagement.Instance.CurrThemeChanged += (s, e) => ApplyLiveChartsColors();
ColorManagement.Instance.CurrAccentChanged += (s, e) => ApplyLiveChartsColors();

ApplyLiveChartsColors();

MainWindow mainWindow = new();
mainWindow.Show();
}

private void ApplyLiveChartsColors()
Expand Down Expand Up @@ -127,9 +126,36 @@ private LvcColor ResourceToLvcColor(string resourceName)
return LvcColor.FromArgb(alpha, red, green, blue);
}

internal bool RegisterQuickTimeHotkey(HotKey newHotKey)
{
if (currentQuickTimeHotKey is not null)
hotKeyManager.Unregister(currentQuickTimeHotKey);

if (newHotKey.Key == Key.None || newHotKey.Modifiers == ModifierKeys.None)
{
SettingsModel.Instance.AddRegisteredHotKey(RegisteredHotKey.HotKeyType.QuickEntry, Key.None, ModifierKeys.None);
return true;
}

try
{
currentQuickTimeHotKey = newHotKey;
hotKeyManager.Register(currentQuickTimeHotKey);
SettingsModel.Instance.AddRegisteredHotKey(RegisteredHotKey.HotKeyType.QuickEntry, newHotKey.Key, newHotKey.Modifiers);
return true;
}
catch
{
Debug.WriteLine($"Could not register quick time hotkey {newHotKey.Modifiers} {newHotKey.Key}");
currentQuickTimeHotKey = null;
SettingsModel.Instance.AddRegisteredHotKey(RegisteredHotKey.HotKeyType.QuickEntry, Key.None, ModifierKeys.None);
return false;
}
}

private async void HotKeyManagerPressed(object? sender, KeyPressedEventArgs e)
{
if (e.HotKey.Equals(openQuickTimeTracker))
if (e.HotKey.Equals(currentQuickTimeHotKey))
{
TrackTime? trackTime = QuickTimeTracker.Open(await dbContext.TrackedTimes.OrderBy(tt => tt.Id).LastOrDefaultAsync());
if (trackTime is null)
Expand Down
15 changes: 15 additions & 0 deletions speed-time/Components/HotKeySelector.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<potato:DSUserControl x:Class="DSaladin.SpeedTime.Components.HotKeySelector"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:DSaladin.SpeedTime.Components"
xmlns:potato="https://dsaladin.dev/products/fancypotato/wpf/xaml"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance local:HotKeySelector}"
d:DesignHeight="100" d:DesignWidth="250">
<Grid>
<potato:DSButton Content="{Binding SelectedKeysText}" d:Content="Ctrl + Shift + T" Click="HotKey_Click" MouseRightButtonDown="HotKey_MouseRightButtonDown"
KeyUp="HotKey_KeyUp" Padding="0,0,0,0" Style="{DynamicResource AccentButton}" />
</Grid>
</potato:DSUserControl>
182 changes: 182 additions & 0 deletions speed-time/Components/HotKeySelector.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
using DSaladin.FancyPotato.DSUserControls;
using DSaladin.SpeedTime.Model;
using GlobalHotKey;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DSaladin.SpeedTime.Components
{
/// <summary>
/// Interaction logic for HotKeySelector.xaml
/// </summary>
public partial class HotKeySelector : DSUserControl
{
public Key SelectedKey
{
get => (Key)GetValue(SelectedKeyProperty);
set => SetValue(SelectedKeyProperty, value);
}

public ModifierKeys SelectedModifierKeys
{
get => (ModifierKeys)GetValue(SelectedModifierKeysProperty);
set => SetValue(SelectedModifierKeysProperty, value);
}

public string SelectedKeysText
{
get => (string)GetValue(SelectedKeysTextProperty);
set => SetValue(SelectedKeysTextProperty, value);
}

public bool IsSelectingHotkey { get; set; } = false;

public static readonly DependencyProperty SelectedKeyProperty = DependencyProperty.Register(nameof(SelectedKey), typeof(Key), typeof(HotKeySelector), new FrameworkPropertyMetadata(Key.None, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSelectedKeyChanged));
public static readonly DependencyProperty SelectedModifierKeysProperty = DependencyProperty.Register(nameof(SelectedModifierKeys), typeof(ModifierKeys), typeof(HotKeySelector), new FrameworkPropertyMetadata(ModifierKeys.None, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSelectedModifierKeysChanged));
public static readonly DependencyProperty SelectedKeysTextProperty = DependencyProperty.Register(nameof(SelectedKeysText), typeof(string), typeof(HotKeySelector), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

private string selectedHotKey;
public string SelectedHotKey
{
get { return selectedHotKey; }
set
{
selectedHotKey = value;
NotifyPropertyChanged();
}
}

public static readonly RoutedEvent HotKeyChangedEvent = EventManager.RegisterRoutedEvent(
nameof(HotKeyChanged), RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(HotKeySelector));

public event RoutedEventHandler HotKeyChanged
{
add { AddHandler(HotKeyChangedEvent, value); }
remove { RemoveHandler(HotKeyChangedEvent, value); }
}

public static readonly RoutedEvent LoadHotKeyEvent = EventManager.RegisterRoutedEvent(
nameof(LoadHotKey), RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(HotKeySelector));

public event RoutedEventHandler LoadHotKey
{
add { AddHandler(LoadHotKeyEvent, value); }
remove { RemoveHandler(LoadHotKeyEvent, value); }
}

private Key[] modifierKeys = [Key.LeftShift, Key.RightShift, Key.LeftAlt, Key.RightAlt, Key.LeftCtrl, Key.RightCtrl, Key.LWin, Key.RWin, Key.System];

public HotKeySelector()
{
InitializeComponent();
DataContext = this;

Loaded += HotKeySelector_Loaded;
}

private void HotKeySelector_Loaded(object sender, RoutedEventArgs e)
{
HotKeyArgs args = new(LoadHotKeyEvent, this, SelectedKey, SelectedModifierKeys);
RaiseEvent(args);

if (args.IsValid)
{
SetCurrentValue(SelectedKeyProperty, args.SelectedKey);
SetCurrentValue(SelectedModifierKeysProperty, args.SelectedModifierKeys);
ResetKeys(false);
}
}

private static void OnSelectedKeyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
HotKeySelector hotkeySelector = (HotKeySelector)source;
hotkeySelector.SetCurrentValue(SelectedKeyProperty, (Key)e.NewValue);
}

private static void OnSelectedModifierKeysChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
HotKeySelector hotkeySelector = (HotKeySelector)source;
hotkeySelector.SetCurrentValue(SelectedModifierKeysProperty, (ModifierKeys)e.NewValue);
}

private static string GetKeyText(ModifierKeys modifierKeys, Key key)
{
string keyText = "";
keyText += modifierKeys.HasFlag(ModifierKeys.Control) ? "Ctrl + " : "";
keyText += modifierKeys.HasFlag(ModifierKeys.Alt) ? "Alt + " : "";
keyText += modifierKeys.HasFlag(ModifierKeys.Shift) ? "Shift + " : "";
keyText += key.ToString();

return keyText;
}

protected override void OnLostFocus(RoutedEventArgs e)
{
base.OnLostFocus(e);
}

private void HotKey_Click(object sender, RoutedEventArgs e)
{
IsSelectingHotkey = !IsSelectingHotkey;

if (IsSelectingHotkey)
SetCurrentValue(SelectedKeysTextProperty, SpeedTime.Language.SpeedTime.hotkey_press_key);
else
ResetKeys(false);
}

private void HotKey_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
SetCurrentValue(SelectedKeyProperty, Key.None);
SetCurrentValue(SelectedModifierKeysProperty, ModifierKeys.None);
ResetKeys();
}

private void HotKey_KeyUp(object sender, KeyEventArgs e)
{
if (IsSelectingHotkey)
{
Key currentKey = e.Key == Key.System ? e.SystemKey : e.Key;
if (currentKey == Key.Escape)
{
ResetKeys();
return;
}

if (currentKey == Key.None || Keyboard.Modifiers == ModifierKeys.None || modifierKeys.Contains(currentKey))
return;

SetCurrentValue(SelectedKeyProperty, currentKey);
SetCurrentValue(SelectedModifierKeysProperty, Keyboard.Modifiers);
ResetKeys();
}
}

private void ResetKeys(bool raiseChangedEvent = true)
{
IsSelectingHotkey = false;
SetCurrentValue(SelectedKeysTextProperty, GetKeyText(SelectedModifierKeys, SelectedKey));

if (raiseChangedEvent)
RaiseKeyChangedEvent();
}

private void RaiseKeyChangedEvent()
{
HotKeyArgs args = new(HotKeyChangedEvent, this, SelectedKey, SelectedModifierKeys);
RaiseEvent(args);
}
}
}
62 changes: 0 additions & 62 deletions speed-time/Dialogs/TaskLinkEditor.xaml

This file was deleted.

Loading

0 comments on commit 6ebef94

Please sign in to comment.