Skip to content

Commit

Permalink
Allow multiple character selection
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie D committed Feb 5, 2022
1 parent a74e8ed commit 6a1726b
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions SmartThingsTerminal/Scenario.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Newtonsoft.Json;
using NStack;
using SmartThingsNet.Model;
using SmartThingsTerminal.Scenarios;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Terminal.Gui;
Expand Down Expand Up @@ -44,6 +44,10 @@ public class Scenario : IDisposable

public MenuBar MenuBar { get; set; }

public string SearchText { get; set; }

public System.Timers.Timer SearchTimer { get; set; }

public string FormatJson(string json)
{
return json?.Replace("\r", "");
Expand Down Expand Up @@ -226,25 +230,42 @@ public ListView GetClassListView<T>(Dictionary<string, string> displayItemList)

classListView.KeyDown += (args) =>
{
var s = displayItemList.Values.ToList()
.FindIndex(0, displayItemList.Values.Count(), d => d.StartsWith(args.KeyEvent.ToString(), StringComparison.InvariantCultureIgnoreCase));
if (!char.IsControl((char)args.KeyEvent.Key))
{
SearchText += args.KeyEvent.ToString();
}

if (s >= 0)
if (SearchTimer == null)
{
ClassListView.SelectedItem = s;
classListView.SelectedItem = s;
SelectedItemIndex = ClassListView.SelectedItem;
SelectedItem = _dataItemList.Values.ToArray()[SelectedItemIndex];
UpdateJsonView(SelectedItem.ToJson());
HostPane.Title = displayItemList.Keys.ToArray()[classListView.SelectedItem];
UpdateSettings<T>(SelectedItem);
SearchTimer = new System.Timers.Timer(500);
SearchTimer.Elapsed += SearchTimer_Elapsed;
}
if (!SearchTimer.Enabled)
{
SearchTimer.Start();
}
};
}
ClassListView = classListView;
return classListView;
}

private void SearchTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
SearchTimer.Stop();

if (!string.IsNullOrEmpty(SearchText))
{
var s = (List<string>)(ClassListView.Source.ToList());
var i = s.FindIndex(0, s.Count, d => d.StartsWith(SearchText, StringComparison.InvariantCultureIgnoreCase));
if (i >= 0)
{
Application.MainLoop.Invoke(() => { ClassListView.SelectedItem = i; });
}
SearchText = null;
}
}

public virtual void ExportItem()
{
if (SelectedItem != null)
Expand Down

0 comments on commit 6a1726b

Please sign in to comment.