Skip to content

Commit

Permalink
Some refactorings.
Browse files Browse the repository at this point in the history
  • Loading branch information
leegould committed Apr 7, 2016
1 parent a854c1b commit 04a25ae
Show file tree
Hide file tree
Showing 10 changed files with 307 additions and 328 deletions.
1 change: 0 additions & 1 deletion RedisExplorer/AppViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Windows;

using Caliburn.Micro;
using MahApps.Metro;
using RedisExplorer.Controls;
using RedisExplorer.Interface;
using RedisExplorer.Messages;
Expand Down
208 changes: 100 additions & 108 deletions RedisExplorer/Controls/GreyableImage.cs
Original file line number Diff line number Diff line change
@@ -1,111 +1,103 @@
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace RedisExplorer.Controls
{
/// <summary>
/// Class used to have an image that is able to be gray when the control is not enabled.
/// Based on the version by Thomas LEBRUN (http://blogs.developpeur.org/tom)
/// </summary>
/// <seealso cref="http://stackoverflow.com/questions/11305577/grey-out-image-on-button-when-button-is-disabled-simple-and-beautiful-way"/>
public class GreyableImage : Image
{
/// <summary>
/// Initializes a new instance of the <see cref="GreyableImage"/> class.
/// </summary>
static GreyableImage()
{
// Override the metadata of the IsEnabled and Source property.
IsEnabledProperty.OverrideMetadata(typeof(GreyableImage), new FrameworkPropertyMetadata(true, OnAutoGreyScaleImageIsEnabledPropertyChanged));
SourceProperty.OverrideMetadata(typeof(GreyableImage), new FrameworkPropertyMetadata(null, OnAutoGreyScaleImageSourcePropertyChanged));
}

protected static GreyableImage GetImageWithSource(DependencyObject source)
{
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace RedisExplorer.Controls
{
/// <summary>
/// Class used to have an image that is able to be gray when the control is not enabled.
/// Based on the version by Thomas LEBRUN (http://blogs.developpeur.org/tom)
/// </summary>
/// <seealso cref="http://stackoverflow.com/questions/11305577/grey-out-image-on-button-when-button-is-disabled-simple-and-beautiful-way"/>
public class GreyableImage : Image
{
/// <summary>
/// Initializes a new instance of the <see cref="GreyableImage"/> class.
/// </summary>
static GreyableImage()
{
// Override the metadata of the IsEnabled and Source property.
IsEnabledProperty.OverrideMetadata(typeof(GreyableImage), new FrameworkPropertyMetadata(true, OnAutoGreyScaleImageIsEnabledPropertyChanged));
SourceProperty.OverrideMetadata(typeof(GreyableImage), new FrameworkPropertyMetadata(null, OnAutoGreyScaleImageSourcePropertyChanged));
}

protected static GreyableImage GetImageWithSource(DependencyObject source)
{
var image = source as GreyableImage;

return image?.Source == null ? null : image;
}

/// <summary>
/// Called when [auto grey scale image source property changed].
/// </summary>
/// <param name="source">The source.</param>
/// <param name="args">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
protected static void OnAutoGreyScaleImageSourcePropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs args)
{
var image = GetImageWithSource(source);
if (image != null)
{
ApplyGreyScaleImage(image, image.IsEnabled);
}
}

/// <summary>
/// Called when [auto grey scale image is enabled property changed].
/// </summary>
/// <param name="source">The source.</param>
/// <param name="args">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
protected static void OnAutoGreyScaleImageIsEnabledPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs args)
{
var image = GetImageWithSource(source);
if (image == null) { return; }
var isEnabled = Convert.ToBoolean(args.NewValue);
ApplyGreyScaleImage(image, isEnabled);
}

protected static void ApplyGreyScaleImage(GreyableImage greyScaleImg, Boolean isEnabled)
{
try
{
if (!isEnabled)
{
BitmapSource bitmapImage;

if (greyScaleImg.Source is FormatConvertedBitmap)
{
// Already grey !
return;
}
if (greyScaleImg.Source is BitmapSource)
{
bitmapImage = (BitmapSource)greyScaleImg.Source;
}
else // trying string
{
bitmapImage = new BitmapImage(new Uri(greyScaleImg.Source.ToString()));
}
var conv = new FormatConvertedBitmap(bitmapImage, PixelFormats.Gray32Float, null, 0);
greyScaleImg.Source = conv;

// Create Opacity Mask for greyscale image as FormatConvertedBitmap does not keep transparency info
greyScaleImg.OpacityMask = new ImageBrush(((FormatConvertedBitmap)greyScaleImg.Source).Source); //equivalent to new ImageBrush(bitmapImage)
}
else
{
if (greyScaleImg.Source is FormatConvertedBitmap)
{
greyScaleImg.Source = ((FormatConvertedBitmap)greyScaleImg.Source).Source;
}
else if (greyScaleImg.Source is BitmapSource)
{
// Should be full color already.
return;
}

// Reset the Opcity Mask
greyScaleImg.OpacityMask = null;
}
}
catch (Exception)
{
// nothin'
}

}

}
return image?.Source == null ? null : image;
}

/// <summary>
/// Called when [auto grey scale image source property changed].
/// </summary>
/// <param name="source">The source.</param>
/// <param name="args">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
protected static void OnAutoGreyScaleImageSourcePropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs args)
{
var image = GetImageWithSource(source);
if (image != null)
{
ApplyGreyScaleImage(image, image.IsEnabled);
}
}

/// <summary>
/// Called when [auto grey scale image is enabled property changed].
/// </summary>
/// <param name="source">The source.</param>
/// <param name="args">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
protected static void OnAutoGreyScaleImageIsEnabledPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs args)
{
var image = GetImageWithSource(source);
if (image == null) { return; }
var isEnabled = Convert.ToBoolean(args.NewValue);
ApplyGreyScaleImage(image, isEnabled);
}

protected static void ApplyGreyScaleImage(GreyableImage greyScaleImg, Boolean isEnabled)
{
try
{
if (!isEnabled)
{
if (greyScaleImg.Source is FormatConvertedBitmap)
{
// Already grey !
return;
}
var image = greyScaleImg.Source as BitmapSource;
var bitmapImage = image ?? new BitmapImage(new Uri(greyScaleImg.Source.ToString()));
var conv = new FormatConvertedBitmap(bitmapImage, PixelFormats.Gray32Float, null, 0);
greyScaleImg.Source = conv;

// Create Opacity Mask for greyscale image as FormatConvertedBitmap does not keep transparency info
greyScaleImg.OpacityMask = new ImageBrush(((FormatConvertedBitmap)greyScaleImg.Source).Source); //equivalent to new ImageBrush(bitmapImage)
}
else
{
if (greyScaleImg.Source is FormatConvertedBitmap)
{
greyScaleImg.Source = ((FormatConvertedBitmap)greyScaleImg.Source).Source;
}
else if (greyScaleImg.Source is BitmapSource)
{
// Should be full color already.
return;
}

// Reset the Opcity Mask
greyScaleImg.OpacityMask = null;
}
}
catch (Exception)
{
// nothin'
}

}

}
}
148 changes: 74 additions & 74 deletions RedisExplorer/Controls/KeyStringViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,82 +1,82 @@
using Caliburn.Micro;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

using RedisExplorer.Interface;
using RedisExplorer.Messages;
using RedisExplorer.Models;

namespace RedisExplorer.Controls
{
public class KeyStringViewModel : Screen, IHandle<TreeItemSelectedMessage>, IHandle<AddKeyMessage>, IValueItem, IKeyValue<string>, IHandle<RedisKeyReloadMessage>
{
private string keyValue;

public string KeyValue
{
get
{
return keyValue;
}
set
{
keyValue = value;
NotifyOfPropertyChange(() => KeyValue);
}
}

public KeyStringViewModel(IEventAggregator eventAggregator)
{
eventAggregator.Subscribe(this);
}

#region Message Handlers

public void Handle(TreeItemSelectedMessage message)
{
if (message?.SelectedItem is RedisKeyString && !message.SelectedItem.HasChildren)
{
DisplayStringValue((RedisKeyString) message.SelectedItem);
}
}

public void Handle(AddKeyMessage message)
{
KeyValue = string.Empty;
}

public void Handle(RedisKeyReloadMessage message)
{
var redisKeyString = message.Item as RedisKeyString;
if (redisKeyString != null)
{
using Caliburn.Micro;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

using RedisExplorer.Interface;
using RedisExplorer.Messages;
using RedisExplorer.Models;

namespace RedisExplorer.Controls
{
public class KeyStringViewModel : Screen, IHandle<TreeItemSelectedMessage>, IHandle<AddKeyMessage>, IValueItem, IKeyValue<string>, IHandle<RedisKeyReloadMessage>
{
private string keyValue;

public string KeyValue
{
get
{
return keyValue;
}
set
{
keyValue = value;
NotifyOfPropertyChange(() => KeyValue);
}
}

public KeyStringViewModel(IEventAggregator eventAggregator)
{
eventAggregator.Subscribe(this);
}

#region Message Handlers

public void Handle(TreeItemSelectedMessage message)
{
if (message?.SelectedItem is RedisKeyString && !message.SelectedItem.HasChildren)
{
DisplayStringValue((RedisKeyString) message.SelectedItem);
}
}

public void Handle(AddKeyMessage message)
{
KeyValue = string.Empty;
}

public void Handle(RedisKeyReloadMessage message)
{
var redisKeyString = message.Item as RedisKeyString;
if (redisKeyString != null)
{
DisplayStringValue(redisKeyString);
}
}

#endregion

#region Private

private void DisplayStringValue(RedisKeyString item)
}
}

#endregion

#region Private

private void DisplayStringValue(RedisKeyString item)
{
if (item == null)
{
return;
}

var value = item.KeyValue;

try
{
KeyValue = JObject.Parse(value).ToString(Formatting.Indented);
}
catch (JsonReaderException)
{
KeyValue = value;
var value = item.KeyValue;

try
{
KeyValue = JObject.Parse(value).ToString(Formatting.Indented);
}
}

#endregion
}
}
catch (JsonReaderException)
{
KeyValue = value;
}
}

#endregion
}
}
Loading

0 comments on commit 04a25ae

Please sign in to comment.