diff --git a/Text-Grab/App.config b/Text-Grab/App.config index f852fa6e..646b884d 100644 --- a/Text-Grab/App.config +++ b/Text-Grab/App.config @@ -145,6 +145,9 @@ Resize + + True + \ No newline at end of file diff --git a/Text-Grab/Controls/CollapsibleButton.xaml b/Text-Grab/Controls/CollapsibleButton.xaml index 0f0059b5..a29f0dcc 100644 --- a/Text-Grab/Controls/CollapsibleButton.xaml +++ b/Text-Grab/Controls/CollapsibleButton.xaml @@ -24,7 +24,9 @@ + Symbol="{Binding Path=ButtonSymbol, + Mode=TwoWay, + ElementName=CollapsibleButtonUserControl}" /> + Text="{Binding ElementName=CollapsibleButtonUserControl, + Path=ButtonText, + Mode=TwoWay}" /> @@ -40,7 +44,8 @@ Name="ChangeButtonLayout" Click="ChangeButtonLayout_Click" Header="Change Style" - IsEnabled="{Binding ElementName=CollapsibleButtonUserControl, Path=CanChangeStyle}" /> + IsEnabled="{Binding ElementName=CollapsibleButtonUserControl, + Path=CanChangeStyle}" /> diff --git a/Text-Grab/Models/LookupItem.cs b/Text-Grab/Models/LookupItem.cs index 3beebb5b..64102818 100644 --- a/Text-Grab/Models/LookupItem.cs +++ b/Text-Grab/Models/LookupItem.cs @@ -1,11 +1,37 @@ -using System; +using Humanizer; +using System; namespace Text_Grab.Models; +public enum LookupItemKind +{ + Simple = 0, + EditWindow = 1, + GrabFrame = 2, + Link = 3, +} + public class LookupItem : IEquatable { - public string shortValue { get; set; } = string.Empty; - public string longValue { get; set; } = string.Empty; + public string ShortValue { get; set; } = string.Empty; + public string LongValue { get; set; } = string.Empty; + + public Wpf.Ui.Controls.SymbolRegular UiSymbol + { + get + { + return Kind switch + { + LookupItemKind.Simple => Wpf.Ui.Controls.SymbolRegular.Copy20, + LookupItemKind.EditWindow => Wpf.Ui.Controls.SymbolRegular.Window24, + LookupItemKind.GrabFrame => Wpf.Ui.Controls.SymbolRegular.PanelBottom20, + LookupItemKind.Link => Wpf.Ui.Controls.SymbolRegular.Link24, + _ => Wpf.Ui.Controls.SymbolRegular.Copy20, + }; + } + } + + public LookupItemKind Kind { get; set; } = LookupItemKind.Simple; public LookupItem() { @@ -14,13 +40,34 @@ public LookupItem() public LookupItem(string sv, string lv) { - shortValue = sv; - longValue = lv; + ShortValue = sv; + LongValue = lv; } - public override string ToString() => $"{shortValue} {longValue}"; + public LookupItem(HistoryInfo historyInfo) + { + ShortValue = historyInfo.CaptureDateTime.Humanize() + Environment.NewLine + historyInfo.CaptureDateTime.ToString("F"); + LongValue = historyInfo.TextContent.Length > 100 ? historyInfo.TextContent[..100].Trim() + "…" : historyInfo.TextContent.Trim(); + + HistoryItem = historyInfo; + + if (string.IsNullOrEmpty(historyInfo.ImagePath)) + Kind = LookupItemKind.EditWindow; + else + Kind = LookupItemKind.GrabFrame; + } + + public HistoryInfo? HistoryItem { get; set; } + + public override string ToString() + { + if (HistoryItem is not null) + return $"{HistoryItem.CaptureDateTime:F} {HistoryItem.TextContent}"; + + return $"{ShortValue} {LongValue}"; + } - public string ToCSVString() => $"{shortValue},{longValue}"; + public string ToCSVString() => $"{ShortValue},{LongValue}"; public bool Equals(LookupItem? other) { @@ -32,4 +79,4 @@ public bool Equals(LookupItem? other) return false; } -} \ No newline at end of file +} diff --git a/Text-Grab/Properties/Settings.Designer.cs b/Text-Grab/Properties/Settings.Designer.cs index 95b517f6..2e79afb9 100644 --- a/Text-Grab/Properties/Settings.Designer.cs +++ b/Text-Grab/Properties/Settings.Designer.cs @@ -12,7 +12,7 @@ namespace Text_Grab.Properties { [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.10.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.12.0.0")] internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); @@ -574,5 +574,17 @@ public string GrabFrameScrollBehavior { this["GrabFrameScrollBehavior"] = value; } } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("True")] + public bool LookupSearchHistory { + get { + return ((bool)(this["LookupSearchHistory"])); + } + set { + this["LookupSearchHistory"] = value; + } + } } } diff --git a/Text-Grab/Properties/Settings.settings b/Text-Grab/Properties/Settings.settings index fc262f6b..99ca662d 100644 --- a/Text-Grab/Properties/Settings.settings +++ b/Text-Grab/Properties/Settings.settings @@ -140,5 +140,8 @@ Resize + + True + \ No newline at end of file diff --git a/Text-Grab/Services/HistoryService.cs b/Text-Grab/Services/HistoryService.cs index 5271b12d..fafcd154 100644 --- a/Text-Grab/Services/HistoryService.cs +++ b/Text-Grab/Services/HistoryService.cs @@ -23,9 +23,9 @@ public class HistoryService private static readonly int maxHistoryTextOnly = 100; private static readonly int maxHistoryWithImages = 10; - private List HistoryTextOnly = new(); - private List HistoryWithImage = new(); - private DispatcherTimer saveTimer = new(); + private List HistoryTextOnly = []; + private List HistoryWithImage = []; + private readonly DispatcherTimer saveTimer = new(); private readonly Settings DefaultSettings = AppUtilities.TextGrabSettings; #endregion Fields @@ -119,7 +119,7 @@ public async Task LoadHistories() public async Task PopulateMenuItemWithRecentGrabs(MenuItem recentGrabsMenuItem) { List grabsHistory = GetRecentGrabs(); - grabsHistory = grabsHistory.OrderByDescending(x => x.CaptureDateTime).ToList(); + grabsHistory = [.. grabsHistory.OrderByDescending(x => x.CaptureDateTime)]; recentGrabsMenuItem.Items.Clear(); @@ -244,6 +244,23 @@ public void WriteHistory() WriteHistoryFiles(HistoryWithImage, nameof(HistoryWithImage), maxHistoryWithImages); } } + + public void RemoveTextHistoryItem(HistoryInfo historyItem) + { + HistoryTextOnly.Remove(historyItem); + + saveTimer.Stop(); + saveTimer.Start(); + } + + public void RemoveImageHistoryItem(HistoryInfo historyItem) + { + HistoryWithImage.Remove(historyItem); + + saveTimer.Stop(); + saveTimer.Start(); + } + #endregion Public Methods #region Private Methods @@ -252,14 +269,14 @@ private static async Task> LoadHistory(string fileName) { string rawText = await FileUtilities.GetTextFileAsync($"{fileName}.json", FileStorageKind.WithHistory); - if (string.IsNullOrWhiteSpace(rawText)) return new List(); + if (string.IsNullOrWhiteSpace(rawText)) return []; List? tempHistory = JsonSerializer.Deserialize>(rawText); if (tempHistory is List jsonList && jsonList.Count > 0) return tempHistory; - return new List(); + return []; } private static void WriteHistoryFiles(List history, string fileName, int maxNumberToSave) @@ -310,5 +327,6 @@ private void SaveTimer_Tick(object? sender, EventArgs e) WriteHistory(); CachedBitmap = null; } + #endregion Private Methods } diff --git a/Text-Grab/Styles/ButtonStyles.xaml b/Text-Grab/Styles/ButtonStyles.xaml index 184df4d8..4edc2705 100644 --- a/Text-Grab/Styles/ButtonStyles.xaml +++ b/Text-Grab/Styles/ButtonStyles.xaml @@ -218,7 +218,8 @@ x:Name="PART_Popup" AllowsTransparency="true" Focusable="false" - IsOpen="{Binding IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" + IsOpen="{Binding IsSubmenuOpen, + RelativeSource={RelativeSource TemplatedParent}}" Placement="Bottom" PlacementTarget="{Binding ElementName=templateRoot}" PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}"> @@ -237,9 +238,12 @@ VerticalAlignment="Top"> + Width="{Binding ActualWidth, + ElementName=SubMenuBorder}" + Height="{Binding ActualHeight, + ElementName=SubMenuBorder}" + Fill="{Binding Background, + ElementName=SubMenuBorder}" /> @@ -469,9 +474,12 @@ VerticalAlignment="Top"> + Width="{Binding ActualWidth, + ElementName=SubMenuBorder}" + Height="{Binding ActualHeight, + ElementName=SubMenuBorder}" + Fill="{Binding Background, + ElementName=SubMenuBorder}" /> + Visibility="{Binding HeadersVisibility, + ConverterParameter={x:Static DataGridHeadersVisibility.Row}, + Converter={x:Static DataGrid.HeadersVisibilityConverter}, + RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" /> @@ -155,4 +161,4 @@ - \ No newline at end of file + diff --git a/Text-Grab/Styles/TextBoxStyles.xaml b/Text-Grab/Styles/TextBoxStyles.xaml index c2555038..16bd0c4b 100644 --- a/Text-Grab/Styles/TextBoxStyles.xaml +++ b/Text-Grab/Styles/TextBoxStyles.xaml @@ -169,7 +169,8 @@ x:Name="Arrow" HorizontalAlignment="Center" VerticalAlignment="Center" - Data="{Binding Content, RelativeSource={RelativeSource TemplatedParent}}"> + Data="{Binding Content, + RelativeSource={RelativeSource TemplatedParent}}"> @@ -359,4 +360,4 @@ - \ No newline at end of file + diff --git a/Text-Grab/Views/FullscreenGrab.xaml.cs b/Text-Grab/Views/FullscreenGrab.xaml.cs index 72bc321c..877dc742 100644 --- a/Text-Grab/Views/FullscreenGrab.xaml.cs +++ b/Text-Grab/Views/FullscreenGrab.xaml.cs @@ -40,7 +40,7 @@ public partial class FullscreenGrab : Window private double xShiftDelta; private double yShiftDelta; private HistoryInfo? historyInfo; - private bool usingTesseract; + private readonly bool usingTesseract; private static readonly Settings DefaultSettings = AppUtilities.TextGrabSettings; #endregion Fields diff --git a/Text-Grab/Views/QuickSimpleLookup.xaml b/Text-Grab/Views/QuickSimpleLookup.xaml index c81435af..2c11ca67 100644 --- a/Text-Grab/Views/QuickSimpleLookup.xaml +++ b/Text-Grab/Views/QuickSimpleLookup.xaml @@ -11,6 +11,7 @@ Width="800" Height="400" Closed="FluentWindow_Closed" + Closing="FluentWindow_Closing" Loaded="Window_Loaded" PreviewKeyDown="QuickSimpleLookup_PreviewKeyDown" Topmost="True" @@ -23,7 +24,7 @@ @@ -151,6 +152,11 @@ x:Name="ParseCSVFileMenuItem" Click="ParseCSVFileMenuItem_Click" Header="Add Rows from CSV File..." /> + + + + + + + + + + + + + + + + - + - + @@ -225,7 +255,7 @@ - + @@ -233,11 +263,31 @@ + Text="{Binding LongValue}" /> + + + + + + + + + + + + + + diff --git a/Text-Grab/Views/QuickSimpleLookup.xaml.cs b/Text-Grab/Views/QuickSimpleLookup.xaml.cs index 0b85f879..c072593b 100644 --- a/Text-Grab/Views/QuickSimpleLookup.xaml.cs +++ b/Text-Grab/Views/QuickSimpleLookup.xaml.cs @@ -13,24 +13,24 @@ using System.Windows.Media; using Text_Grab.Models; using Text_Grab.Properties; +using Text_Grab.Services; using Text_Grab.Utilities; namespace Text_Grab.Views; -/// -/// Interaction logic for QuickSimpleLookup.xaml -/// public partial class QuickSimpleLookup : Wpf.Ui.Controls.FluentWindow { #region Fields public TextBox? DestinationTextBox; - private string cacheFilename = "QuickSimpleLookupCache.csv"; + private readonly string cacheFilename = "QuickSimpleLookupCache.csv"; private bool isPuttingValueIn = false; private LookupItem? lastSelection; private int rowCount = 0; private string valueUnderEdit = string.Empty; + private LookupItem? itemUnderEdit; private static readonly Settings DefaultSettings = AppUtilities.TextGrabSettings; + private List lookupItems = []; #endregion Fields @@ -48,7 +48,7 @@ public QuickSimpleLookup() public bool IsEditingDataGrid { get; set; } = false; public bool IsFromETW { get; set; } = false; - public List ItemsDictionary { get; set; } = new(); + public List ItemsDictionary { get; set; } = []; #endregion Properties @@ -56,20 +56,20 @@ public QuickSimpleLookup() private static LookupItem ParseStringToLookupItem(char splitChar, string row) { - List cells = row.Split(splitChar).ToList(); + List cells = [.. row.Split(splitChar)]; LookupItem newRow = new(); - if (cells.FirstOrDefault() is String firstCell) - newRow.shortValue = firstCell; + if (cells.FirstOrDefault() is string firstCell) + newRow.ShortValue = firstCell; - newRow.longValue = ""; + newRow.LongValue = ""; if (cells.Count > 1 && cells[1] is not null) - newRow.longValue = String.Join(" ", cells.Skip(1).ToArray()); + newRow.LongValue = string.Join(" ", cells.Skip(1).ToArray()); return newRow; } private static IEnumerable ParseStringToRows(string clipboardContent, bool isCSV = false) { - List rows = clipboardContent.Split(Environment.NewLine).ToList(); + List rows = [.. clipboardContent.Split(Environment.NewLine)]; char splitChar = isCSV ? ',' : '\t'; @@ -98,7 +98,7 @@ private void AddToLookUpResults(char splitChar, string text) MainDataGrid.ItemsSource = null; ItemsDictionary.Add(newItem); - MainDataGrid.ItemsSource = ItemsDictionary; + lookupItems.Add(newItem); UpdateRowCount(); MainDataGrid.ScrollIntoView(ItemsDictionary.LastOrDefault()); @@ -186,9 +186,10 @@ private void FluentWindow_Closed(object sender, EventArgs e) private List GetMainDataGridSelection() { - if (MainDataGrid.SelectedItems is not List selectedItems || selectedItems.Count == 0) + if (MainDataGrid.SelectedItems is not List selectedItems + || selectedItems.Count == 0) { - selectedItems = new List(); + selectedItems = []; if (MainDataGrid.SelectedItem is not LookupItem selectedLookupItem) return selectedItems; @@ -225,6 +226,7 @@ private void GoToEndOfMainDataGrid() private void MainDataGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e) { IsEditingDataGrid = true; + itemUnderEdit = e.Row.Item as LookupItem; } private void MainDataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) @@ -232,7 +234,27 @@ private void MainDataGrid_CellEditEnding(object sender, DataGridCellEditEndingEv IsEditingDataGrid = false; if (e.EditAction == DataGridEditAction.Cancel) + { + itemUnderEdit = null; return; + } + + LookupItem? editedRow = e.Row.Item as LookupItem; + int prevIndex = -1; + + if (itemUnderEdit is not null) + { + prevIndex = lookupItems.IndexOf(itemUnderEdit); + lookupItems.Remove(itemUnderEdit); + } + + if (editedRow is not null) + { + if (prevIndex > -1) + lookupItems.Insert(prevIndex, editedRow); + else + lookupItems.Add(editedRow); + } DependencyObject child = VisualTreeHelper.GetChild(e.EditingElement, 0); if (child is TextBox editedBox @@ -281,12 +303,13 @@ private void ParseBTN_Click(object sender, RoutedEventArgs e) private async void ParseCSVFileMenuItem_Click(object sender, RoutedEventArgs e) { // Create OpenFileDialog - Microsoft.Win32.OpenFileDialog dlg = new(); - - // Set filter for file extension and default file extension - dlg.DefaultExt = ".csv"; - dlg.Filter = "Comma Separated Values File (.csv)|*.csv"; - dlg.CheckFileExists = true; + OpenFileDialog dlg = new() + { + // Set filter for file extension and default file extension + DefaultExt = ".csv", + Filter = "Comma Separated Values File (.csv)|*.csv", + CheckFileExists = true + }; bool? result = dlg.ShowDialog(); @@ -295,7 +318,7 @@ private async void ParseCSVFileMenuItem_Click(object sender, RoutedEventArgs e) string csvToOpenPath = dlg.FileName; - await ReadCsvFileIntoQuickSimpleLookup(csvToOpenPath); + await LoadDataGridContent(csvToOpenPath); SaveBTN.Visibility = Visibility.Visible; } @@ -308,17 +331,18 @@ private void PasteToggleButton_Checked(object sender, RoutedEventArgs e) private async void PickSaveLocation_Click(object sender, RoutedEventArgs e) { - SaveFileDialog dlg = new(); - - dlg.AddExtension = true; - dlg.DefaultExt = ".csv"; - dlg.InitialDirectory = "C:\\"; - dlg.FileName = "QuickSimpleLookupDataFile.csv"; - dlg.OverwritePrompt = false; + SaveFileDialog dlg = new() + { + AddExtension = true, + DefaultExt = ".csv", + InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), + FileName = "QuickSimpleLookupDataFile.csv", + OverwritePrompt = false + }; if (!string.IsNullOrEmpty(DefaultSettings.LookupFileLocation)) { - dlg.InitialDirectory = DefaultSettings.LookupFileLocation; + dlg.InitialDirectory = Path.GetDirectoryName(DefaultSettings.LookupFileLocation); dlg.FileName = Path.GetFileName(DefaultSettings.LookupFileLocation); } @@ -334,7 +358,7 @@ private async void PickSaveLocation_Click(object sender, RoutedEventArgs e) { // clear and load the new file ItemsDictionary.Clear(); - await ReadCsvFileIntoQuickSimpleLookup(dlg.FileName); + await LoadDataGridContent(dlg.FileName); } else await WriteDataToCSV(); @@ -342,16 +366,28 @@ private async void PickSaveLocation_Click(object sender, RoutedEventArgs e) private void PopulateSampleData() { - LookupItem sampleItem1 = new("This is the key", "This is the value you want to copy quickly"); + LookupItem sampleItem1 = new("This is the key", "This is the value you want to copy quickly") + { + Kind = LookupItemKind.Simple + }; ItemsDictionary.Add(sampleItem1); - LookupItem sampleItem2 = new("Import data", "From a copied Excel table, or import from a CSV File"); + LookupItem sampleItem2 = new("Import data", "From a copied Excel table, or import from a CSV File") + { + Kind = LookupItemKind.Simple + }; ItemsDictionary.Add(sampleItem2); - LookupItem sampleItem3 = new("You can change save location", "Putting the data store location in OneDrive it will sync across devices"); + LookupItem sampleItem3 = new("You can change save location", "Putting the data store location in OneDrive it will sync across devices") + { + Kind = LookupItemKind.Simple + }; ItemsDictionary.Add(sampleItem3); - LookupItem sampleItem4 = new("Delete these initial rows", "and add your own manually if you like."); + LookupItem sampleItem4 = new("Delete these initial rows", "and add your own manually if you like.") + { + Kind = LookupItemKind.Simple + }; ItemsDictionary.Add(sampleItem4); MainDataGrid.ItemsSource = null; @@ -362,11 +398,17 @@ private async void PutValueIntoClipboard(KeyboardModifiersDown? keysDown = null) { if (MainDataGrid.ItemsSource is not List lookUpList || lookUpList.FirstOrDefault() is not LookupItem firstLookupItem) + { + EditTextWindow etw = new(SearchBox.Text, false); + etw.Show(); + this.Close(); + WindowUtilities.ShouldShutDown(); return; + } isPuttingValueIn = true; - List selectedLookupItems = new(); + List selectedLookupItems = []; foreach (object item in MainDataGrid.SelectedItems) if (item is LookupItem selectedLookupItem) @@ -375,10 +417,10 @@ private async void PutValueIntoClipboard(KeyboardModifiersDown? keysDown = null) if (selectedLookupItems.Count == 0) selectedLookupItems.Add(firstLookupItem); + bool openedHistoryItemOrLink = false; StringBuilder stringBuilder = new(); - if (keysDown is null) - keysDown = KeyboardExtensions.GetKeyboardModifiersDown(); + keysDown ??= KeyboardExtensions.GetKeyboardModifiersDown(); switch (keysDown) { @@ -397,16 +439,16 @@ private async void PutValueIntoClipboard(KeyboardModifiersDown? keysDown = null) if (selectedLookupItems.FirstOrDefault() is not LookupItem lookupItem) return; - if (Uri.TryCreate(lookupItem.longValue, UriKind.Absolute, out Uri? uri)) + if (Uri.TryCreate(lookupItem.LongValue, UriKind.Absolute, out Uri? uri)) { - Process.Start(new ProcessStartInfo(lookupItem.longValue) { UseShellExecute = true }); + Process.Start(new ProcessStartInfo(lookupItem.LongValue) { UseShellExecute = true }); this.Close(); return; } break; case KeyboardModifiersDown.Ctrl: foreach (LookupItem lItem in selectedLookupItems) - stringBuilder.AppendLine(lItem.shortValue); + stringBuilder.AppendLine(lItem.ShortValue); break; case KeyboardModifiersDown.Shift: foreach (LookupItem lItem in selectedLookupItems) @@ -414,10 +456,42 @@ private async void PutValueIntoClipboard(KeyboardModifiersDown? keysDown = null) break; default: foreach (LookupItem lItem in selectedLookupItems) - stringBuilder.AppendLine(lItem.longValue); + { + switch (lItem.Kind) + { + case LookupItemKind.EditWindow when lItem.HistoryItem is not null: + { + EditTextWindow editTextWindow = new(lItem.HistoryItem); + editTextWindow.Show(); + openedHistoryItemOrLink = true; + break; + } + case LookupItemKind.GrabFrame when lItem.HistoryItem is not null: + { + GrabFrame gf = new(lItem.HistoryItem); + gf.Show(); + openedHistoryItemOrLink = true; + break; + } + case LookupItemKind.Link: + Process.Start(new ProcessStartInfo(lItem.LongValue) { UseShellExecute = true }); + openedHistoryItemOrLink = true; + break; + default: + stringBuilder.AppendLine(lItem.LongValue); + break; + } + } break; } + if (openedHistoryItemOrLink) + { + this.Close(); + WindowUtilities.ShouldShutDown(); + return; + } + if (string.IsNullOrEmpty(stringBuilder.ToString())) return; @@ -543,7 +617,7 @@ private async void QuickSimpleLookup_PreviewKeyDown(object sender, KeyEventArgs } } - private async Task ReadCsvFileIntoQuickSimpleLookup(string csvToOpenPath) + private async Task LoadDataGridContent(string csvToOpenPath) { string contentToParse = string.Empty; @@ -555,9 +629,17 @@ private async Task ReadCsvFileIntoQuickSimpleLookup(string csvToOpenPath) if (string.IsNullOrWhiteSpace(contentToParse)) PopulateSampleData(); + MainDataGrid.ItemsSource = null; + ItemsDictionary.AddRange(ParseStringToRows(contentToParse, true)); + lookupItems = [.. ItemsDictionary]; + + if (DefaultSettings.LookupSearchHistory) + { + AddHistoryItemsToItemsDictionary(); + SearchHistory.IsChecked = true; + } - MainDataGrid.ItemsSource = null; MainDataGrid.ItemsSource = ItemsDictionary; UpdateRowCount(); @@ -578,8 +660,19 @@ private void RowDeleted() if (item is LookupItem selectedLookupItem) { filteredLookupList.Remove(selectedLookupItem); + lookupItems.Remove(selectedLookupItem); ItemsDictionary.Remove(selectedLookupItem); - SaveBTN.Visibility = Visibility.Visible; + + if (selectedLookupItem.HistoryItem is null) + { + SaveBTN.Visibility = Visibility.Visible; + continue; + } + + if (selectedLookupItem.Kind is LookupItemKind.EditWindow) + Singleton.Instance.RemoveTextHistoryItem(selectedLookupItem.HistoryItem); + else if (selectedLookupItem.Kind is LookupItemKind.GrabFrame) + Singleton.Instance.RemoveImageHistoryItem(selectedLookupItem.HistoryItem); } } @@ -647,10 +740,10 @@ private async void SearchBox_TextChanged(object sender, TextChangedEventArgs e) else MainDataGrid.CanUserAddRows = false; - List searchArray = SearchBox.Text.ToLower().Split().ToList(); + List searchArray = [.. SearchBox.Text.ToLower().Split()]; searchArray.Sort(); - List filteredList = new(); + List filteredList = []; foreach (LookupItem lItem in ItemsDictionary) { @@ -691,7 +784,7 @@ private void UpdateRowCount() private async void Window_Loaded(object sender, RoutedEventArgs e) { - await ReadCsvFileIntoQuickSimpleLookup(DefaultSettings.LookupFileLocation); + await LoadDataGridContent(DefaultSettings.LookupFileLocation); if (DefaultSettings.TryInsert && !IsFromETW) PasteToggleButton.IsChecked = true; @@ -703,6 +796,26 @@ private async void Window_Loaded(object sender, RoutedEventArgs e) Activate(); SearchBox.Focus(); } + + private void AddHistoryItemsToItemsDictionary() + { + List textHistoryItems = Singleton.Instance.GetEditWindows(); + + foreach (HistoryInfo historyItem in textHistoryItems) + { + LookupItem newItem = new(historyItem); + ItemsDictionary.Add(newItem); + } + + List grabFrameHistoryItems = Singleton.Instance.GetRecentGrabs(); + + foreach (HistoryInfo historyItem in grabFrameHistoryItems) + { + LookupItem newItem = new(historyItem); + ItemsDictionary.Add(newItem); + } + } + private async Task WriteDataToCSV() { if (!string.IsNullOrWhiteSpace(SearchBox.Text)) @@ -710,10 +823,7 @@ private async Task WriteDataToCSV() StringBuilder csvContents = new(); - if (MainDataGrid.ItemsSource is not List itemsToSave) - return; - - foreach (LookupItem lookupItem in itemsToSave) + foreach (LookupItem lookupItem in lookupItems) csvContents.AppendLine(lookupItem.ToCSVString()); try @@ -730,5 +840,80 @@ private async Task WriteDataToCSV() System.Windows.Forms.MessageBox.Show($"Failed to save csv file. {ex.Message}"); } } + + private void FluentWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e) + { + // Can't do this here because it will close the app before the text can be inserted + // WindowUtilities.ShouldShutDown(); + } + + private async void SearchHistory_Click(object sender, RoutedEventArgs e) + { + DefaultSettings.LookupSearchHistory = SearchHistory.IsChecked is true; + + lookupItems.Clear(); + ItemsDictionary.Clear(); + MainDataGrid.ItemsSource = null; + + await LoadDataGridContent(DefaultSettings.LookupFileLocation); + } + + private void DeleteItem_Click(object sender, RoutedEventArgs e) + { + if (MainDataGrid.SelectedItem is not LookupItem lookupItem) + return; + + RowDeleted(); + } + + private void OpenInETWMenuItem_Click(object sender, RoutedEventArgs e) + { + if (MainDataGrid.SelectedItem is not LookupItem lookupItem) + return; + + switch (lookupItem.Kind) + { + case LookupItemKind.Simple: + StringBuilder sb = new(); + sb.Append(lookupItem.ShortValue); + sb.Append(Environment.NewLine); + sb.AppendLine(lookupItem.LongValue); + EditTextWindow etw = new(sb.ToString(), false); + etw.Show(); + break; + case LookupItemKind.EditWindow: + if (lookupItem.HistoryItem is not null) + { + EditTextWindow editTextWindow = new(lookupItem.HistoryItem); + editTextWindow.Show(); + return; + } + + EditTextWindow etw2 = new(lookupItem.LongValue, false); + etw2.Show(); + break; + case LookupItemKind.GrabFrame: + if (lookupItem.HistoryItem is not null) + { + EditTextWindow editTextWindow = new(lookupItem.HistoryItem); + editTextWindow.Show(); + return; + } + + EditTextWindow etw3 = new(lookupItem.LongValue, false); + etw3.Show(); + break; + case LookupItemKind.Link: + StringBuilder sb2 = new(); + sb2.Append(lookupItem.ShortValue); + sb2.Append(Environment.NewLine); + sb2.AppendLine(lookupItem.LongValue); + EditTextWindow etw4 = new(sb2.ToString(), false); + etw4.Show(); + break; + default: + break; + } + } #endregion Methods }