Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update AntiDuplWPF matser to 0.0.3 from WPF branch #164

Merged
merged 2 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
257 changes: 254 additions & 3 deletions src/AntiDuplWPF/AntiDuplWPF/AntiDuplWPF.csproj

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion src/AntiDuplWPF/AntiDuplWPF/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>

<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Style\Default.xaml" />
<!--<ResourceDictionary Source="pack://application:,,,/AntiDuplWPF;component/Style/Default.xaml"/>-->
<ResourceDictionary Source="/AntiDuplWPF;component/Control/SearchControlTheme.xaml" />
<ResourceDictionary Source="/AntiDuplWPF;component/Resources/lang.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
32 changes: 20 additions & 12 deletions src/AntiDuplWPF/AntiDuplWPF/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using AntiDuplWPF.Core;
using AntiDuplWPF.Model;
using AntiDuplWPF.Properties;
using AntiDuplWPF.Service;
using AntiDuplWPF.View;
using AntiDuplWPF.ViewModel;
using TinyIoC;

namespace AntiDuplWPF
{
/// <summary>
/// Логика взаимодействия для App.xaml
/// </summary>
public partial class App : Application
public partial class App
{
public App()
{
Expand All @@ -34,13 +28,27 @@ private void Application_Startup(object sender, StartupEventArgs e)
// Register config
TinyIoCContainer.Current.Register<IConfigurationModel, ConfigurationModel>(confModel);

LanguageService languageService = new LanguageService(confModel);
TinyIoCContainer.Current.Register<ILanguageService, LanguageService>(languageService);
ILanguageService languageService = new LanguageService(confModel);
TinyIoCContainer.Current.Register<ILanguageService>(languageService);

var viewModel = new MainViewModel();
AntiDuplWPF.Properties.Resources.UserPath = AntiDuplWPF.Properties.Resources.GetDefaultUserPath();
CoreLib core = new CoreLib(AntiDuplWPF.Properties.Resources.UserPath);
TinyIoC.TinyIoCContainer.Current.Register<ICoreLib>(core);

ImageLoader imageLoader = new ImageLoader(core);
TinyIoC.TinyIoCContainer.Current.Register<IImageLoader>(imageLoader);

ThumbnailProvider thumbnailProvider = new ThumbnailProvider(imageLoader);
TinyIoC.TinyIoCContainer.Current.Register<IThumbnailProvider>(thumbnailProvider);

//IWindowService windowService = new WindowService();
MainViewModel viewModel = TinyIoCContainer.Current.Resolve<MainViewModel>();
//var viewModel = new MainViewModel(languageService);

var shell = new MainWindow();
//windowService.MainWindow = shell;
shell.DataContext = viewModel;
shell.Loaded += new RoutedEventHandler(viewModel.OnLoaded);
shell.Closing += new CancelEventHandler(viewModel.OnClosing);
shell.Show();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.Xaml.Behaviors;

namespace AntiDuplWPF.Behavior
{
public class AllowableCharactersTextBoxBehavior : Behavior<TextBox>
{
public static readonly DependencyProperty RegularExpressionProperty =
DependencyProperty.Register("RegularExpression", typeof(string), typeof(AllowableCharactersTextBoxBehavior),
new FrameworkPropertyMetadata(".*"));
public string RegularExpression
{
get
{
return (string)base.GetValue(RegularExpressionProperty);
}
set
{
base.SetValue(RegularExpressionProperty, value);
}
}

public static readonly DependencyProperty MaxLengthProperty =
DependencyProperty.Register("MaxLength", typeof(int), typeof(AllowableCharactersTextBoxBehavior),
new FrameworkPropertyMetadata(int.MinValue));
public int MaxLength
{
get
{
return (int)base.GetValue(MaxLengthProperty);
}
set
{
base.SetValue(MaxLengthProperty, value);
}
}

protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.PreviewTextInput += OnPreviewTextInput;
DataObject.AddPastingHandler(AssociatedObject, OnPaste);
}

private void OnPaste(object sender, DataObjectPastingEventArgs e)
{
if (e.DataObject.GetDataPresent(DataFormats.Text))
{
string text = Convert.ToString(e.DataObject.GetData(DataFormats.Text));

if (!IsValid(text, true))
{
e.CancelCommand();
}
}
else
{
e.CancelCommand();
}
}

void OnPreviewTextInput(object sender, System.Windows.Input.TextCompositionEventArgs e)
{
e.Handled = !IsValid(e.Text, false);
}

protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.PreviewTextInput -= OnPreviewTextInput;
DataObject.RemovePastingHandler(AssociatedObject, OnPaste);
}

private bool IsValid(string newText, bool paste)
{
return !ExceedsMaxLength(newText, paste) && Regex.IsMatch(newText, RegularExpression);
}

private bool ExceedsMaxLength(string newText, bool paste)
{
if (MaxLength == 0) return false;

return LengthOfModifiedText(newText, paste) > MaxLength;
}

private int LengthOfModifiedText(string newText, bool paste)
{
var countOfSelectedChars = this.AssociatedObject.SelectedText.Length;
var caretIndex = this.AssociatedObject.CaretIndex;
string text = this.AssociatedObject.Text;

if (countOfSelectedChars > 0 || paste)
{
text = text.Remove(caretIndex, countOfSelectedChars);
return text.Length + newText.Length;
}
else
{
var insert = Keyboard.IsKeyToggled(Key.Insert);

return insert && caretIndex < text.Length ? text.Length : text.Length + newText.Length;
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using Microsoft.Xaml.Behaviors;
using AntiDuplWPF.Helper;
using AntiDuplWPF.ObjectModel;
using AntiDuplWPF.ViewModel;

Expand All @@ -30,7 +33,7 @@ public Func<DuplPairViewModel, BindableMenuItem[]> MenuGeneratorDuplPair
set { SetValue(MenuGeneratorDuplPairProperty, value); }
}

/* public static readonly DependencyProperty MenuGeneratorDuplGroupProperty =
public static readonly DependencyProperty MenuGeneratorDuplGroupProperty =
DependencyProperty.Register("MenuGeneratorDuplGroup",
typeof(Func<DuplicateGroup, BindableMenuItem[]>),
typeof(DataGridContextMenuItemSourceBindingOnOpenBehavior),
Expand All @@ -40,7 +43,7 @@ public Func<DuplicateGroup, BindableMenuItem[]> MenuGeneratorDuplGroup
{
get { return (Func<DuplicateGroup, BindableMenuItem[]>)GetValue(MenuGeneratorDuplGroupProperty); }
set { SetValue(MenuGeneratorDuplGroupProperty, value); }
}*/
}

public static readonly DependencyProperty MenuGeneratorMultiDuplPairProperty =
DependencyProperty.Register("MenuGeneratorMultiDuplPair",
Expand Down Expand Up @@ -75,42 +78,99 @@ protected override void OnAttached()

void AssociatedObject_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
var grid = sender as MultiSelector;
if (grid != null)
var grid = sender as DataGrid;
if (grid == null)
return;

if (grid.ContextMenu == null)
return;

grid.ContextMenu.ItemsSource = null;
grid.ContextMenu.Items.Clear();

var fe = e.OriginalSource as FrameworkElement;
if (fe != null)
{
if (HandleColumnHeadersMenuOpen(grid, grid.ContextMenu))
return;
}

if (grid.SelectedItems.Count > 1)
{
if (grid.SelectedItems.Count > 1)
if (grid.SelectedItem is DuplPairViewModel)
{
if (grid.SelectedItem is DuplPairViewModel)
{
IList viewResultsOfDupl = (IList)grid.SelectedItems;
if (MenuGeneratorMultiDuplPair != null)
grid.ContextMenu.ItemsSource = MenuGeneratorMultiDuplPair(viewResultsOfDupl);
}
/* else if (grid.SelectedItem is DuplicateGroup)
{
IList viewResults = (IList)grid.SelectedItems;
if (MenuGeneratorMultiDuplGroup != null)
grid.ContextMenu.ItemsSource = MenuGeneratorMultiDuplGroup(viewResults);
}*/
IList viewResultsOfDupl = (IList)grid.SelectedItems;
if (MenuGeneratorMultiDuplPair != null)
grid.ContextMenu.ItemsSource = MenuGeneratorMultiDuplPair(viewResultsOfDupl);
}
else
else if (grid.SelectedItem is DuplicateGroup)
{
IList viewResults = (IList)grid.SelectedItems;
if (MenuGeneratorMultiDuplGroup != null)
grid.ContextMenu.ItemsSource = MenuGeneratorMultiDuplGroup(viewResults);
}
}
else
{
if (grid.SelectedItem is DuplPairViewModel)
{
DuplPairViewModel viewResultOfDupl = (DuplPairViewModel)grid.SelectedItem;
if (MenuGeneratorDuplPair != null)
grid.ContextMenu.ItemsSource = MenuGeneratorDuplPair(viewResultOfDupl);
}
else if (grid.SelectedItem is DuplicateGroup)
{
if (grid.SelectedItem is DuplPairViewModel)
{
DuplPairViewModel viewResultOfDupl = (DuplPairViewModel)grid.SelectedItem;
if (MenuGeneratorDuplPair != null)
grid.ContextMenu.ItemsSource = MenuGeneratorDuplPair(viewResultOfDupl);
}
/*else if (grid.SelectedItem is DuplicateGroup)
{
DuplicateGroup duplicateGroup = (DuplicateGroup)grid.SelectedItem;
if (MenuGeneratorDuplGroup != null)
grid.ContextMenu.ItemsSource = MenuGeneratorDuplGroup(duplicateGroup);
}*/
DuplicateGroup duplicateGroup = (DuplicateGroup)grid.SelectedItem;
if (MenuGeneratorDuplGroup != null)
grid.ContextMenu.ItemsSource = MenuGeneratorDuplGroup(duplicateGroup);
}
}
}

private static bool HandleColumnHeadersMenuOpen(DataGrid grid, ContextMenu menu)
{
var headersPresenter = VisualTreeHelperEx.FindVisualChildByType<DataGridColumnHeadersPresenter>(grid);
if (headersPresenter == null)
return false;

var currentPosition = Mouse.GetPosition(headersPresenter);
if (currentPosition.X < 0 || currentPosition.X > headersPresenter.ActualWidth ||
currentPosition.Y < 0 || currentPosition.Y > headersPresenter.ActualHeight)
return false;

var enabled = DataGridColumnChooserHelper.GetIsEnabled(grid);
if (!enabled)
return true;

foreach (var col in grid.Columns.OrderBy(col => col.DisplayIndex))
{
string title;
if (col.Header is TextBlock)
{
var tblock = (TextBlock) col.Header;
title = !string.IsNullOrEmpty(tblock.Text)
? tblock.Text
: string.Join(string.Empty, tblock.Inlines.OfType<Run>().Select(i => i.Text))
.Replace(Environment.NewLine, string.Empty);
}
else
title = (string) col.Header;

var menuItem = new MenuItem {Header = title, IsCheckable = true};
var binding = new Binding
{
Source = col,
Path = new PropertyPath(DataGridColumn.VisibilityProperty.Name),
Converter = new VisibilityToBoolConverter()
};
BindingOperations.SetBinding(menuItem, MenuItem.IsCheckedProperty, binding);
menuItem.StaysOpenOnClick = true;
menu.Items.Add(menuItem);
}

return true;
}

protected override void OnDetaching()
{
base.OnDetaching();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Xaml.Behaviors;

namespace AntiDuplWPF.Behavior
{
public class DataGridScrollIntoViewBehavior : Behavior<DataGrid>
{
protected override void OnAttached()
{
AssociatedObject.Loaded += OnLoaded;
AssociatedObject.Unloaded += OnUnLoaded;
}

protected override void OnDetaching()
{
AssociatedObject.Loaded -= OnLoaded;
AssociatedObject.Unloaded -= OnUnLoaded;
}

private void OnLoaded(object sender, RoutedEventArgs e)
{
AssociatedObject.SelectionChanged += AssociatedObject_SelectionChanged;
}

void AssociatedObject_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (AssociatedObject is DataGrid)
{
DataGrid grid = (DataGrid)AssociatedObject;
grid.Dispatcher.BeginInvoke((Action)(() =>
{
grid.UpdateLayout();
if (grid.SelectedItem != null)
grid.ScrollIntoView(grid.SelectedItem);
}));
}
}

private void OnUnLoaded(object sender, RoutedEventArgs e)
{
AssociatedObject.SelectionChanged -= AssociatedObject_SelectionChanged;
}


}
}
Loading