Skip to content

Commit

Permalink
Merge pull request #371 from hellium/feature-370-priority-shortcut
Browse files Browse the repository at this point in the history
Implement "New task with priority" for #370
  • Loading branch information
benrhughes committed Nov 1, 2018
2 parents 8a47fbe + 938c9b7 commit 3238706
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 3 deletions.
21 changes: 19 additions & 2 deletions Client/Controls/IntellisenseTextBox.cs
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
Expand All @@ -22,6 +23,9 @@ public class IntellisenseTextBox : TextBox
private ListBox IntellisenseList { get; set; }
private int IntelliPos { get; set; } // used to position the Intellisense popup

private readonly Regex StartDateWithPriorityRegex = new Regex(@"[0-9]{4}\-[0-9]{2}\-[0-9]{2}\s\(");
private readonly List<string> Priorities = Enumerable.Range('A', 26).Select(i => $"({Convert.ToChar(i)})").ToList();

public TaskList TaskList
{
get
Expand Down Expand Up @@ -249,7 +253,7 @@ private string FindIntelliWord()
}

/// <summary>
/// Triggers the Intellisense popup to appear when "+" or "@" is pressed in the text box.
/// Triggers the Intellisense popup to appear when "+", "@" or "(" is pressed in the text box.
/// </summary>
/// <param name="sender">Not used</param>
/// <param name="e">Event arguments</param>
Expand All @@ -259,12 +263,17 @@ private void IntellisenseTextBox_TextChanged(object sender, TextChangedEventArgs
{
return;
}

if (this.TaskList == null)
{
return;
}

CheckKeyAndShowPopup();
}

public void CheckKeyAndShowPopup()
{
var lastAddedCharacter = this.Text.Substring(this.CaretIndex - 1, 1);
switch (lastAddedCharacter)
{
Expand All @@ -277,6 +286,14 @@ private void IntellisenseTextBox_TextChanged(object sender, TextChangedEventArgs
this.IntelliPos = this.CaretIndex - 1;
ShowIntellisensePopup(this.TaskList.Contexts, this.GetRectFromCharacterIndex(this.IntelliPos));
break;
case "(":
if (this.CaretIndex == 1 ||
(this.CaretIndex == 12 && StartDateWithPriorityRegex.IsMatch(this.Text.Substring(0, 12))))
{
this.IntelliPos = this.CaretIndex - 1;
ShowIntellisensePopup(Priorities, this.GetRectFromCharacterIndex(this.IntelliPos));
}
break;
}
}

Expand Down
3 changes: 3 additions & 0 deletions Client/Controls/MainWindow.xaml
Expand Up @@ -32,6 +32,7 @@
<RoutedUICommand x:Key="EmulateDownArrow" Text="Down" />
<!-- Task menu commands -->
<RoutedUICommand x:Key="NewTask" Text="New Task" />
<RoutedUICommand x:Key="NewTaskWithPriority" Text="New Task with Priority" />
<RoutedUICommand x:Key="UpdateTask" Text="Update Task" />
<RoutedUICommand x:Key="DeleteTask" Text="Delete Task" />
<RoutedUICommand x:Key="AppendText" Text="Append Text" />
Expand Down Expand Up @@ -120,6 +121,7 @@
<CommandBinding Command="{StaticResource CopySelectedTaskToNewTask}" CanExecute="WhenSingleTaskSelectedCanExecute" Executed="CopySelectedTaskToNewTaskExecuted"/>
<!-- Task menu commands -->
<CommandBinding Command="{StaticResource NewTask}" CanExecute="AlwaysCanExecute" Executed="NewTaskExecuted"/>
<CommandBinding Command="{StaticResource NewTaskWithPriority}" CanExecute="AlwaysCanExecute" Executed="NewTaskWithPriorityExecuted"/>
<CommandBinding Command="{StaticResource UpdateTask}" CanExecute="WhenSingleTaskSelectedCanExecute" Executed="UpdateTaskExecuted"/>
<CommandBinding Command="{StaticResource AppendText}" CanExecute="WhenTasksSelectedCanExecute" Executed="AppendTextExecuted"/>
<CommandBinding Command="{StaticResource DeleteTask}" CanExecute="WhenTasksSelectedCanExecute" Executed="DeleteTaskExecuted"/>
Expand Down Expand Up @@ -303,6 +305,7 @@
<KeyBinding Command="{StaticResource EmulateDownArrow}" Key="J" />
<!-- Task menu commands -->
<KeyBinding Command="{StaticResource NewTask}" Key="N" />
<KeyBinding Command="{StaticResource NewTaskWithPriority}" Key="D9" Modifiers="Shift" />
<KeyBinding Command="{StaticResource PrintFile}" Key="P" />
<KeyBinding Command="{StaticResource UpdateTask}" Key="U" />
<KeyBinding Command="{StaticResource UpdateTask}" Key="F2" />
Expand Down
5 changes: 5 additions & 0 deletions Client/Controls/MainWindow.xaml.cs
Expand Up @@ -306,6 +306,11 @@ private void NewTaskExecuted(object sender, RoutedEventArgs e)
ViewModel.AddNewTask();
}

private void NewTaskWithPriorityExecuted(object sender, RoutedEventArgs e)
{
ViewModel.AddNewTaskWithPriority();
}

private void UpdateTaskExecuted(object sender, RoutedEventArgs e)
{
ViewModel.UpdateTask();
Expand Down
9 changes: 9 additions & 0 deletions Client/MainWindowViewModel.cs
Expand Up @@ -17,6 +17,7 @@
using System.ComponentModel;
using System.Text.RegularExpressions;
using System.Globalization;
using System.Windows.Threading;

namespace Client
{
Expand Down Expand Up @@ -970,6 +971,14 @@ public void AddNewTask()
_window.taskText.Focus();
}

public void AddNewTaskWithPriority()
{
AddNewTask();
_window.taskText.Text = _window.taskText.Text.Length > 0 ? $"( {_window.taskText.Text}" : "(";
_window.taskText.CaretIndex = 1;
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>_window.taskText.CheckKeyAndShowPopup()));
}

public void UpdateTask()
{
// Abort if no task, or more than one task, is selected.
Expand Down
4 changes: 3 additions & 1 deletion Client/Resource.Designer.cs

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

1 change: 1 addition & 0 deletions Client/Resource.resx
Expand Up @@ -196,6 +196,7 @@
- O or Ctrl+O: open todo.txt file
- C or Ctrl+N: new todo.txt file
- N: new task
- ( or Shift+9: new task with priority
- J: next task
- K: prev task
- X: toggle task completion
Expand Down

0 comments on commit 3238706

Please sign in to comment.