Skip to content

Commit

Permalink
Navigator code style refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ImoutoChan committed Dec 28, 2023
1 parent 0852c68 commit e02bdea
Show file tree
Hide file tree
Showing 31 changed files with 171 additions and 255 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace ImoutoRebirth.Navigator.Behaviors;

class FrameworkElementDragBehavior : Behavior<FrameworkElement>
internal class FrameworkElementDragBehavior : Behavior<FrameworkElement>
{
protected bool IsMouseClicked { get; set; }

Expand All @@ -17,33 +17,28 @@ protected override void OnAttached()
AssociatedObject.MouseLeave += AssociatedObject_MouseLeave;
}

void AssociatedObject_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
IsMouseClicked = true;
}
private void AssociatedObject_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) => IsMouseClicked = true;

void AssociatedObject_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
IsMouseClicked = false;
}
private void AssociatedObject_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) => IsMouseClicked = false;

protected virtual void AssociatedObject_MouseLeave(object sender, MouseEventArgs e)
{
if (IsMouseClicked)
if (!IsMouseClicked)
return;

// Set the item's DataContext as the data to be transferred
if (AssociatedObject.DataContext is IDragable dragObject)
{
// Set the item's DataContext as the data to be transferred
if (AssociatedObject.DataContext is IDragable dragObject)
try
{
DragDrop.DoDragDrop(AssociatedObject, dragObject.Data, dragObject.AllowDragDropEffects);
}
catch (Exception exception)
{
try
{
DragDrop.DoDragDrop(AssociatedObject, dragObject.Data, dragObject.AllowDragDropEffects);
}
catch (Exception exception)
{
Debug.WriteLine(exception);
}
Debug.WriteLine(exception);
}
}

IsMouseClicked = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@

namespace ImoutoRebirth.Navigator.Behaviors;

class FrameworkElementDropBehavior : Behavior<FrameworkElement>
internal class FrameworkElementDropBehavior : Behavior<FrameworkElement>
{
protected Type DataType; //the type of the data that can be dropped into this control
/// <summary>
/// The type of the data that can be dropped into this control
/// </summary>
protected Type? DataType;

protected override void OnAttached()
{
Expand All @@ -20,67 +23,50 @@ protected override void OnAttached()

protected virtual void AssociatedObject_Drop(object sender, DragEventArgs e)
{
if (DataType != null)
if (DataType != null && CanBeDropped(e))
{
//if the data type can be dropped
if (e.Data.GetDataPresent(DataType))
{
//drop the data
IDropable target = AssociatedObject.DataContext as IDropable;
target?.Drop(e.Data.GetData(DataType));
}
var target = AssociatedObject.DataContext as IDropable;
var data = e.Data.GetData(DataType);

if (data != null)
target?.Drop(data);
}
e.Handled = true;
}

void AssociatedObject_DragLeave(object sender, DragEventArgs e)
private void AssociatedObject_DragLeave(object sender, DragEventArgs e)
{
DataType = null;
e.Handled = true;
}

void AssociatedObject_DragOver(object sender, DragEventArgs e)
private void AssociatedObject_DragOver(object sender, DragEventArgs e)
{
if (DataType != null)
{
//if item can be dropped
if (e.Data.GetDataPresent(DataType))
{
//give mouse effect
SetDragDropEffects(e);
}
}
if (DataType != null && CanBeDropped(e))
SetDragDropEffects(e);

e.Handled = true;
}

protected virtual void AssociatedObject_DragEnter(object sender, DragEventArgs e)
{
//if the DataContext implements IDropable, record the data type that can be dropped
if (DataType == null)
{
IDropable dropObject = AssociatedObject.DataContext as IDropable;
if (dropObject != null)
{
DataType = dropObject.DataType;
}
}
// if the DataContext implements IDropable, record the data type that can be dropped
if (DataType == null && AssociatedObject.DataContext is IDropable dropObject)
DataType = dropObject.DataType;

e.Handled = true;
}

/// <summary>
/// Provides feedback on if the data can be dropped
/// </summary>
/// <param name="e"></param>
private void SetDragDropEffects(DragEventArgs e)
{
e.Effects = DragDropEffects.None; //default to None
e.Effects = DragDropEffects.None;

//if the data type can be dropped
if (e.Data.GetDataPresent(DataType))
{
if (CanBeDropped(e))
e.Effects = DragDropEffects.Copy;
}
}

}
protected bool CanBeDropped(DragEventArgs e) => e.Data.GetDataPresent(DataType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace ImoutoRebirth.Navigator.Behaviors;

interface IDragable
internal interface IDragable
{
/// <summary>
/// The data item.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
namespace ImoutoRebirth.Navigator.Behaviors;

interface IDropable
internal interface IDropable
{
/// <summary>
/// Type of the data item
/// Type of the data item
/// </summary>
Type DataType { get; }

/// <summary>
/// Drop data into the collection.
/// Drop data into the collection.
/// </summary>
/// <param name="data">The data to be dropped</param>
/// <param name="index">optional: The index location to insert the data</param>
void Drop(object data, int index = -1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace ImoutoRebirth.Navigator.Behaviors;

class MultiSelectListBoxDragBehavior : FrameworkElementDragBehavior
internal class MultiSelectListBoxDragBehavior : FrameworkElementDragBehavior
{
protected override void AssociatedObject_MouseLeave(object sender, MouseEventArgs e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,26 @@

namespace ImoutoRebirth.Navigator.Behaviors;

class MultiSelectListBoxDropBehavior : FrameworkElementDropBehavior
internal class MultiSelectListBoxDropBehavior : FrameworkElementDropBehavior
{
protected override void AssociatedObject_DragEnter(object sender, DragEventArgs e)
{
//if the DataContext implements IDropable, record the data type that can be dropped
if (DataType == null)
{
var dropObject = (AssociatedObject as ListBox)?.DataContext as IDropable;
if (dropObject != null)
{
DataType = dropObject.DataType;
}
}
if (DataType == null && AssociatedObject is ListBox { DataContext: IDropable dropObject })
DataType = dropObject.DataType;

e.Handled = true;
}

protected override void AssociatedObject_Drop(object sender, DragEventArgs e)
{
if (DataType != null)
if (DataType != null && CanBeDropped(e))
{
//if the data type can be dropped
if (e.Data.GetDataPresent(DataType))
{
//drop the data
var target = AssociatedObject.DataContext as IDropable;
target?.Drop(e.Data.GetData(DataType));
}
var target = AssociatedObject.DataContext as IDropable;
var data = e.Data.GetData(DataType);

if (data != null && target != null)
target.Drop(data);
}
e.Handled = true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,25 @@ namespace ImoutoRebirth.Navigator.Commands;

internal class RelayCommand : ICommand
{
#region Fields
private readonly Action<object?> _execute;
private readonly Predicate<object?>? _canExecute;

readonly Action<object> _execute;
readonly Predicate<object> _canExecute;

#endregion

#region Constructors

public RelayCommand(Action<object> execute, Predicate<object> canExecute = null)
public RelayCommand(Action<object?> execute, Predicate<object?>? canExecute = null)
{
if (execute == null)
throw new ArgumentNullException("execute");

_execute = execute;
_canExecute = canExecute;
}

#endregion

#region ICommand Members

[DebuggerStepThrough]
public bool CanExecute(object parameter)
{
return _canExecute == null || _canExecute(parameter);
}
public bool CanExecute(object? parameter) => _canExecute == null || _canExecute(parameter);

public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}

public void Execute(object parameter)
public event EventHandler? CanExecuteChanged
{
_execute(parameter);
add => CommandManager.RequerySuggested += value;
remove => CommandManager.RequerySuggested -= value;
}

#endregion
public void Execute(object? parameter) => _execute(parameter);
}

internal class RelayCommand<T> : ICommand
Expand All @@ -68,4 +47,4 @@ public event EventHandler? CanExecuteChanged
add => CommandManager.RequerySuggested += value;
remove => CommandManager.RequerySuggested -= value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,58 +4,54 @@

namespace ImoutoRebirth.Navigator.Converters;

class BooleanResultConverter : IValueConverter
internal class BooleanResultConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (parameter != null)
{
var paramsConv = parameter.ToString();
if (parameter == null)
return false;

return CheckedValue(value, paramsConv);
}
return false;
var paramsConv = parameter.ToString();
return CheckedValue(value, paramsConv);
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
=> throw new NotImplementedException();

public static bool CheckedValue(object value, string param)
public static bool CheckedValue(object? value, string? param)
{
if (string.IsNullOrWhiteSpace(param))
{
return false;
}

var paramToLower = param.Trim().ToLowerInvariant();
switch (paramToLower)
{
case "true":
return (Converts.To<bool?>(value) == true) ? true : false;
return Converts.To<bool?>(value) == true;
case "false":
return (Converts.To<bool?>(value) == false) ? true : false;
return Converts.To<bool?>(value) == false;
case "null":
return (Converts.To<object>(value) == null) ? true : false;
return Converts.To<object>(value) == null;
case "!true":
return (Converts.To<bool?>(value) != true) ? true : false;
return Converts.To<bool?>(value) != true;
case "!false":
return (Converts.To<bool?>(value) != false) ? true : false;
return Converts.To<bool?>(value) != false;
case "!null":
return (Converts.To<object>(value) != null) ? true : false;
return Converts.To<object>(value) != null;
default:
{
var valueStr = Converts.To<string>(value);
if (param.StartsWith("!"))

if (param.StartsWith('!'))
{
param = param.Remove(0, 1);
return (valueStr != param);
return valueStr != param;
}
else
{
return (valueStr == param);
return valueStr == param;
}
}
}
}
}
}
Loading

0 comments on commit e02bdea

Please sign in to comment.