272 lines
7.0 KiB
C#
272 lines
7.0 KiB
C#
using BeWo.ViewModel;
|
|
using DevExpress.Xpf.Editors;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Input;
|
|
using System.Windows.Media;
|
|
|
|
namespace BeWo.View.Controls.AI
|
|
{
|
|
/// <summary>
|
|
/// Gemeinsame Promptbaustein-Bibliothek (TreeView + GridSplitter + TabControl),
|
|
/// genutzt von AiPromptbausteinComplexTreeView und AiPromptbausteinComplexSelectionView.
|
|
/// Der DataContext wird vom Host geerbt und muss IAiPromptbausteinComplexViewModel implementieren.
|
|
/// </summary>
|
|
public partial class AiPromptbausteinLibraryControl : UserControl
|
|
{
|
|
public AiPromptbausteinLibraryControl()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
private IAiPromptbausteinComplexViewModel ViewModel => DataContext as IAiPromptbausteinComplexViewModel;
|
|
|
|
public static readonly DependencyProperty PromptActivatedCommandProperty =
|
|
DependencyProperty.Register("PromptActivatedCommand", typeof(ICommand),
|
|
typeof(AiPromptbausteinLibraryControl), new PropertyMetadata(null));
|
|
|
|
public static readonly DependencyProperty RoutineActivatedCommandProperty =
|
|
DependencyProperty.Register("RoutineActivatedCommand", typeof(ICommand),
|
|
typeof(AiPromptbausteinLibraryControl), new PropertyMetadata(null));
|
|
|
|
/// <summary>
|
|
/// Wird bei Doppelklick auf einen Prompt ausgeführt (Verwaltung: Bearbeiten, Auswahl: Senden).
|
|
/// </summary>
|
|
public ICommand PromptActivatedCommand
|
|
{
|
|
get { return (ICommand)GetValue(PromptActivatedCommandProperty); }
|
|
set { SetValue(PromptActivatedCommandProperty, value); }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Wird bei Doppelklick auf eine Routine ausgeführt (Verwaltung: Bearbeiten, Auswahl: Senden).
|
|
/// </summary>
|
|
public ICommand RoutineActivatedCommand
|
|
{
|
|
get { return (ICommand)GetValue(RoutineActivatedCommandProperty); }
|
|
set { SetValue(RoutineActivatedCommandProperty, value); }
|
|
}
|
|
|
|
public static readonly DependencyProperty IsEditModeProperty =
|
|
DependencyProperty.Register("IsEditMode", typeof(bool),
|
|
typeof(AiPromptbausteinLibraryControl), new PropertyMetadata(true, OnIsEditModeChanged));
|
|
|
|
/// <summary>
|
|
/// Schaltet den Bearbeitungsmodus (Kontextmenüs, Edit-Shortcuts, Anlegen-Buttons,
|
|
/// Ordner-Doppelklick-Bearbeiten). Default: eingeschaltet.
|
|
/// </summary>
|
|
public bool IsEditMode
|
|
{
|
|
get { return (bool)GetValue(IsEditModeProperty); }
|
|
set { SetValue(IsEditModeProperty, value); }
|
|
}
|
|
|
|
static void OnIsEditModeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
|
{
|
|
var control = (AiPromptbausteinLibraryControl)d;
|
|
control.UpdateEditMode((bool)e.NewValue);
|
|
}
|
|
|
|
private Dictionary<FrameworkElement, InputBinding[]> _DisabledInputBindings;
|
|
|
|
void UpdateEditMode(bool isEditMode)
|
|
{
|
|
if (!isEditMode)
|
|
{
|
|
if (_DisabledInputBindings != null)
|
|
return;
|
|
|
|
_DisabledInputBindings = new Dictionary<FrameworkElement, InputBinding[]>();
|
|
|
|
foreach (var element in new FrameworkElement[] { treeView, tabItemAlle, tabItemPrompts, tabItemRoutinen })
|
|
{
|
|
_DisabledInputBindings[element] = element.InputBindings.Cast<InputBinding>().ToArray();
|
|
element.InputBindings.Clear();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (_DisabledInputBindings == null)
|
|
return;
|
|
|
|
foreach (var entry in _DisabledInputBindings)
|
|
foreach (var binding in entry.Value)
|
|
entry.Key.InputBindings.Add(binding);
|
|
|
|
_DisabledInputBindings = null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Setzt die initiale Selektion zurück (erstes TreeViewItem an- und wieder abwählen),
|
|
/// damit beim Öffnen kein Ordner vorselektiert ist.
|
|
/// </summary>
|
|
public void ResetInitialSelection()
|
|
{
|
|
if (treeView.ItemContainerGenerator.ContainerFromIndex(0) is TreeViewItem item)
|
|
{
|
|
item.IsSelected = true;
|
|
item.IsSelected = false;
|
|
}
|
|
|
|
listBoxEdit1.UnSelectAll();
|
|
}
|
|
|
|
private void OnPreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
|
|
{
|
|
TreeViewItem treeViewItem = VisualUpwardSearch<TreeViewItem>(e.OriginalSource as DependencyObject);
|
|
|
|
if (treeViewItem != null)
|
|
{
|
|
treeViewItem.Focus();
|
|
e.Handled = true;
|
|
}
|
|
else
|
|
{
|
|
e.Handled = true;
|
|
|
|
if (ViewModel?.SelectedFolder == null)
|
|
return;
|
|
|
|
_LastTreeViewItem.IsSelected = false;
|
|
|
|
treeView.Focus();
|
|
}
|
|
}
|
|
|
|
static T VisualUpwardSearch<T>(DependencyObject source) where T : DependencyObject
|
|
{
|
|
while (source != null && !(source is T))
|
|
source = VisualTreeHelper.GetParent(source);
|
|
|
|
return source as T;
|
|
}
|
|
|
|
private void treeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
|
|
{
|
|
if (ViewModel is null)
|
|
return;
|
|
|
|
ViewModel.SelectedFolder = treeView.SelectedValue as AiPromptbausteinFolderVM;
|
|
}
|
|
|
|
private void Grid_PreviewMouseDown(object sender, MouseButtonEventArgs e)
|
|
{
|
|
TreeViewItem treeViewItem = VisualUpwardSearch<TreeViewItem>(e.OriginalSource as DependencyObject);
|
|
|
|
if (treeViewItem is null)
|
|
{
|
|
treeView.Focus();
|
|
e.Handled = true;
|
|
return;
|
|
}
|
|
|
|
if (e.ClickCount == 2)
|
|
{
|
|
if (!IsEditMode)
|
|
return;
|
|
|
|
if (!ViewModel.OpenEditFolderDialogCommand.CanExecute(null))
|
|
return;
|
|
|
|
ViewModel.OpenEditFolderDialogCommand.Execute(null);
|
|
e.Handled = true;
|
|
}
|
|
}
|
|
|
|
private TreeViewItem _LastTreeViewItem;
|
|
private void treeView_Selected(object sender, RoutedEventArgs e)
|
|
{
|
|
_LastTreeViewItem = e.OriginalSource as TreeViewItem;
|
|
}
|
|
|
|
private void treeView_ContextMenuOpening(object sender, ContextMenuEventArgs e)
|
|
{
|
|
if (!IsEditMode)
|
|
{
|
|
e.Handled = true;
|
|
return;
|
|
}
|
|
|
|
ViewModel?.UpdateFolderCommands();
|
|
}
|
|
|
|
private void ListBox_ContextMenuOpening(object sender, ContextMenuEventArgs e)
|
|
{
|
|
if (!IsEditMode)
|
|
{
|
|
e.Handled = true;
|
|
return;
|
|
}
|
|
|
|
ViewModel?.UpdatePromptCommands();
|
|
}
|
|
|
|
private void Hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
|
|
{
|
|
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
|
|
e.Handled = true;
|
|
}
|
|
|
|
private void Grid_PreviewMouseDown_1(object sender, MouseButtonEventArgs e)
|
|
{
|
|
var cmd = PromptActivatedCommand;
|
|
|
|
if (cmd == null || !cmd.CanExecute(null))
|
|
return;
|
|
|
|
if (e.ClickCount == 2)
|
|
{
|
|
cmd.Execute(null);
|
|
e.Handled = true;
|
|
}
|
|
}
|
|
|
|
private void Grid_PreviewMouseDown_2(object sender, MouseButtonEventArgs e)
|
|
{
|
|
var cmd = RoutineActivatedCommand;
|
|
|
|
if (cmd == null || !cmd.CanExecute(null))
|
|
return;
|
|
|
|
if (e.ClickCount == 2)
|
|
{
|
|
cmd.Execute(null);
|
|
e.Handled = true;
|
|
}
|
|
}
|
|
|
|
private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
|
{
|
|
if (e.OriginalSource is TabControl)
|
|
{
|
|
listBoxEdit1.UnSelectAll();
|
|
listBoxEdit2.UnSelectAll();
|
|
listBoxEdit3.UnSelectAll();
|
|
}
|
|
}
|
|
|
|
private void listBoxEdit3_PreviewMouseDown(object sender, MouseButtonEventArgs e)
|
|
{
|
|
return;
|
|
|
|
if (!(e.OriginalSource is UIElement))
|
|
{
|
|
return;
|
|
}
|
|
|
|
ListBoxItem listBoxItem = VisualUpwardSearch<ListBoxItem>(e.OriginalSource as DependencyObject);
|
|
|
|
if (listBoxItem is null)
|
|
{
|
|
((ListBoxEdit)sender).Focus();
|
|
e.Handled = true;
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|