Double clicking on template item adds it to the template.

This commit is contained in:
Michael Bucari-Tovo 2023-01-07 23:15:38 -07:00
parent 3a48479435
commit 7fafee804d
4 changed files with 33 additions and 21 deletions

View File

@ -22,6 +22,7 @@
<TextBox
Grid.Column="0"
Grid.Row="1"
Name="userEditTbox"
FontFamily="{Binding FontFamily}"
Text="{Binding UserTemplateText, Mode=TwoWay}" />
@ -43,6 +44,7 @@
BorderThickness="1"
GridLinesVisibility="All"
AutoGenerateColumns="False"
DoubleTapped="EditTemplateViewModel_DoubleTapped"
Items="{Binding ListItems}" >
<DataGrid.Columns>

View File

@ -1,12 +1,9 @@
using Avalonia.Data;
using Avalonia.Data.Converters;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
using Dinah.Core;
using LibationFileManager;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
@ -17,22 +14,6 @@ using Avalonia.Controls;
namespace LibationAvalonia.Dialogs
{
class BracketEscapeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is string str && str[0] != '<' && str[^1] != '>')
return $"<{str}>".Replace("->", "-\x200C>").Replace("<-", "<\x200C-");
return new BindingNotification(new InvalidCastException(), BindingErrorType.Error);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is string str && str[0] == '<' && str[^1] == '>')
return str[1..^2].Replace("-\x200C>", "->").Replace("<\x200C-", "<-");
return new BindingNotification(new InvalidCastException(), BindingErrorType.Error);
}
}
public partial class EditTemplateDialog : DialogWindow
{
// final value. post-validity check
@ -43,6 +24,7 @@ namespace LibationAvalonia.Dialogs
public EditTemplateDialog()
{
AvaloniaXamlLoader.Load(this);
userEditTbox = this.FindControl<TextBox>(nameof(userEditTbox));
if (Design.IsDesignMode)
{
AudibleUtilities.AudibleApiStorage.EnsureAccountsSettingsFileExists();
@ -63,6 +45,18 @@ namespace LibationAvalonia.Dialogs
DataContext = _viewModel;
}
public void EditTemplateViewModel_DoubleTapped(object sender, Avalonia.Input.TappedEventArgs e)
{
var dataGrid = sender as DataGrid;
var item = dataGrid.SelectedItem as Tuple<string, string>;
var text = userEditTbox.Text;
userEditTbox.Text = text.Insert(Math.Min(Math.Max(0, userEditTbox.CaretIndex), text.Length), item.Item1);
userEditTbox.CaretIndex += item.Item1.Length;
}
protected override async Task SaveAndCloseAsync()
{
if (!await _viewModel.Validate())
@ -99,6 +93,7 @@ namespace LibationAvalonia.Dialogs
t.Description)
)
);
}
// hold the work-in-progress value. not guaranteed to be valid

View File

@ -103,13 +103,16 @@
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader1,
this.columnHeader2});
this.listView1.HideSelection = false;
this.listView1.FullRowSelect = true;
this.listView1.GridLines = true;
this.listView1.Location = new System.Drawing.Point(12, 56);
this.listView1.MultiSelect = false;
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(328, 283);
this.listView1.TabIndex = 3;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.Details;
this.listView1.DoubleClick += new System.EventHandler(this.listView1_DoubleClick);
//
// columnHeader1
//

View File

@ -5,6 +5,7 @@ using System.IO;
using System.Windows.Forms;
using Dinah.Core;
using LibationFileManager;
using System.Windows.Controls;
namespace LibationWinForms.Dialogs
{
@ -59,7 +60,7 @@ namespace LibationWinForms.Dialogs
// populate list view
foreach (var tag in template.GetTemplateTags())
listView1.Items.Add(new ListViewItem(new[] { $"<{tag.TagName}>", tag.Description }));
listView1.Items.Add(new System.Windows.Forms.ListViewItem(new[] { $"<{tag.TagName}>", tag.Description }));
}
private void resetToDefaultBtn_Click(object sender, EventArgs e) => resetTextBox(template.DefaultTemplate);
@ -197,5 +198,16 @@ namespace LibationWinForms.Dialogs
this.DialogResult = DialogResult.Cancel;
this.Close();
}
private void listView1_DoubleClick(object sender, EventArgs e)
{
var item = listView1.SelectedItems[0];
var text = templateTb.Text;
var selStart = Math.Min(Math.Max(0, templateTb.SelectionStart), text.Length);
templateTb.Text = text.Insert(selStart, item.Text);
templateTb.SelectionStart = selStart + item.Text.Length;
}
}
}