-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
307 additions
and
328 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} | ||
|
||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Oops, something went wrong.