Skip to content

Commit

Permalink
Made the AuthenticatedMenuItem which currently works with Visibility …
Browse files Browse the repository at this point in the history
…and IsEnabled

[release]
  • Loading branch information
Lakritzator committed Nov 18, 2016
1 parent 08f2064 commit b737450
Show file tree
Hide file tree
Showing 20 changed files with 330 additions and 203 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

using System.ComponentModel.Composition;
using Caliburn.Micro;
using Dapplo.CaliburnMicro.Behaviors.Security;
using Dapplo.CaliburnMicro.Configuration;
using Dapplo.CaliburnMicro.Demo.Addon.Languages;
using Dapplo.CaliburnMicro.Demo.UseCases.Configuration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#region Usings

using System.ComponentModel.Composition;
using Dapplo.CaliburnMicro.Behaviors.Security;
using Dapplo.CaliburnMicro.Security;

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
#region Usings

using System.ComponentModel.Composition;
using System.Windows;
using Caliburn.Micro;
using Dapplo.CaliburnMicro.Behaviors.Security;
using Dapplo.CaliburnMicro.Demo.Languages;
using Dapplo.CaliburnMicro.Demo.UseCases.Menu.ViewModels;
using Dapplo.CaliburnMicro.Extensions;
Expand Down Expand Up @@ -62,8 +64,12 @@ public sealed class SomeWindowMenuItems
public IMenuItem SomeWindowMenuItem
{
get {
var menuItem = new MenuItem
var menuItem = new AuthenticatedMenuItem<Visibility>
{
Permission = "Admin",
AuthenticationProperty = AuthenticationProperties.Visibility,
WhenPermission = Visibility.Visible,
WhenPermissionMissing = Visibility.Collapsed,
Icon = new PackIconMaterial
{
Kind = PackIconMaterialKind.ViewList
Expand Down
12 changes: 9 additions & 3 deletions Dapplo.CaliburnMicro.Demo/UseCases/ContextMenu/WizardMenuItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#region Usings

using System.ComponentModel.Composition;
using System.Security.Permissions;
using Caliburn.Micro;
using Dapplo.CaliburnMicro.Behaviors.Security;
using Dapplo.CaliburnMicro.Demo.Languages;
Expand All @@ -43,8 +42,7 @@ namespace Dapplo.CaliburnMicro.Demo.UseCases.ContextMenu
/// This will add an extry for the wizard to the context menu
/// </summary>
[Export("contextmenu", typeof(IMenuItem))]
[UiEnabledPermissions(Permissions = "Admin")]
public sealed class WizardMenuItem : MenuItem
public sealed class WizardMenuItem : AuthenticatedMenuItem<bool>
{
[Import]
public IWindowManager WindowManager { get; set; }
Expand All @@ -55,6 +53,14 @@ public sealed class WizardMenuItem : MenuItem
[Import]
private IContextMenuTranslations ContextMenuTranslations { get; set; }

public WizardMenuItem()
{
Permission = "Admin";
AuthenticationProperty = AuthenticationProperties.IsEnabled;
WhenPermission = true;
WhenPermissionMissing = false;
}

public override void Initialize()
{
// automatically update the DisplayName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:cal="http://www.caliburnproject.org"
xmlns:cmMenu="clr-namespace:Dapplo.CaliburnMicro.Menu;assembly=Dapplo.CaliburnMicro"
xmlns:cmSec="clr-namespace:Dapplo.CaliburnMicro.Behaviors.Security;assembly=Dapplo.CaliburnMicro"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<controls:MetroWindow.Resources>
Expand All @@ -31,6 +32,16 @@
<DataTrigger Binding="{Binding Style}" Value="{x:Static cmMenu:MenuItemStyles.Separator}">
<Setter Property="Template" Value="{StaticResource MenuSeparatorTemplate}" />
</DataTrigger>
<DataTrigger Binding="{Binding AuthenticationProperty}" Value="{x:Static cmSec:AuthenticationProperties.Visibility}">
<Setter Property="cmSec:AuthenticationVisibility.Permission" Value="{Binding Permission}" />
<Setter Property="cmSec:AuthenticationVisibility.WhenPermission" Value="{Binding WhenPermission}" />
<Setter Property="cmSec:AuthenticationVisibility.WhenPermissionMissing" Value="{Binding WhenPermissionMissing}" />
</DataTrigger>
<DataTrigger Binding="{Binding AuthenticationProperty}" Value="{x:Static cmSec:AuthenticationProperties.IsEnabled}">
<Setter Property="cmSec:AuthenticationEnabled.Permission" Value="{Binding Permission}" />
<Setter Property="cmSec:AuthenticationEnabled.WhenPermission" Value="{Binding WhenPermission}" />
<Setter Property="cmSec:AuthenticationEnabled.WhenPermissionMissing" Value="{Binding WhenPermissionMissing}" />
</DataTrigger>
</Style.Triggers>
</Style>
</Menu.Resources>
Expand Down
13 changes: 7 additions & 6 deletions Dapplo.CaliburnMicro.Metro/MetroWindowManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,15 @@ public void AddMahappsStyle(string style)
/// <param name="source">Uri, e.g. /Resources/Icons.xaml or </param>
public void AddResourceDictionary(Uri source)
{
if (Application.Current.Resources.MergedDictionaries.All(x => x.Source != source))
if (Application.Current.Resources.MergedDictionaries.Any(x => x.Source == source))
{
var resourceDictionary = new ResourceDictionary
{
Source = source
};
Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
return;
}
var resourceDictionary = new ResourceDictionary
{
Source = source
};
Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
using System.Windows;
using Caliburn.Micro;
using Dapplo.CaliburnMicro.Behaviors;
using Dapplo.CaliburnMicro.Behaviors.Security;
using Dapplo.CaliburnMicro.Menu;
using Dapplo.CaliburnMicro.Tree;
using Dapplo.Log;
Expand Down Expand Up @@ -126,7 +125,6 @@ protected void ConfigureMenuItems(IEnumerable<IMenuItem> menuItems)

foreach (var contextMenuItem in items.CreateTree())
{
//UiEnabledPermissionsAttribute.ApplyBehaviorWhenAttribute(contextMenuItem);
TrayMenuItems.Add(contextMenuItem);
}
}
Expand Down
11 changes: 11 additions & 0 deletions Dapplo.CaliburnMicro.NotifyIconWpf/Views/TrayIconView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
xmlns:cal="http://www.caliburnproject.org"
xmlns:notifyIconWpf="clr-namespace:Dapplo.CaliburnMicro.NotifyIconWpf"
xmlns:cmMenu="clr-namespace:Dapplo.CaliburnMicro.Menu;assembly=Dapplo.CaliburnMicro"
xmlns:security="clr-namespace:Dapplo.CaliburnMicro.Behaviors.Security;assembly=Dapplo.CaliburnMicro"
cal:Message.Attach="[Event TrayLeftMouseDown] = [Action Click()]"
ToolTipText="{Binding DisplayName}"
Visibility="Collapsed">
Expand Down Expand Up @@ -50,6 +51,16 @@
<Setter Property="IsEnabled" Value="False"/>
<Setter Property="Foreground" Value="{DynamicResource IdealForegroundColorBrush}"/>
</DataTrigger>
<DataTrigger Binding="{Binding AuthenticationProperty}" Value="{x:Static security:AuthenticationProperties.Visibility}">
<Setter Property="security:AuthenticationVisibility.Permission" Value="{Binding Permission}" />
<Setter Property="security:AuthenticationVisibility.WhenPermission" Value="{Binding WhenPermission}" />
<Setter Property="security:AuthenticationVisibility.WhenPermissionMissing" Value="{Binding WhenPermissionMissing}" />
</DataTrigger>
<DataTrigger Binding="{Binding AuthenticationProperty}" Value="{x:Static security:AuthenticationProperties.IsEnabled}">
<Setter Property="security:AuthenticationEnabled.Permission" Value="{Binding Permission}" />
<Setter Property="security:AuthenticationEnabled.WhenPermission" Value="{Binding WhenPermission}" />
<Setter Property="security:AuthenticationEnabled.WhenPermissionMissing" Value="{Binding WhenPermissionMissing}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContextMenu.Resources>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Dapplo.CaliburnMicro.Behaviors.Security
{
/// <summary>
/// This defines the property which is managed by authentication
/// </summary>
public enum AuthenticationProperties
{

/// <summary>
/// No influence
/// </summary>
None,
/// <summary>
/// Changes the IsEnabled
/// </summary>
IsEnabled,
/// <summary>
/// Changes the visibility
/// </summary>
Visibility
}
}
54 changes: 54 additions & 0 deletions Dapplo.CaliburnMicro/Behaviors/Security/INeedAuthentication.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#region Dapplo 2016 - GNU Lesser General Public License

// Dapplo - building blocks for .NET applications
// Copyright (C) 2016 Dapplo
//
// For more information see: http://dapplo.net/
// Dapplo repositories are hosted on GitHub: https://github.com/dapplo
//
// This file is part of Dapplo.CaliburnMicro
//
// Dapplo.CaliburnMicro is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Dapplo.CaliburnMicro is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have a copy of the GNU Lesser General Public License
// along with Dapplo.CaliburnMicro. If not, see <http://www.gnu.org/licenses/lgpl.txt>.

#endregion

namespace Dapplo.CaliburnMicro.Behaviors.Security
{
/// <summary>
/// Base interface which an authenticated thing needs to implement
/// </summary>
/// <typeparam name="TWhen"></typeparam>
public interface INeedAuthentication<out TWhen>
{
/// <summary>
/// This defines the property which is managed by authentication
/// </summary>
AuthenticationProperties AuthenticationProperty { get; }

/// <summary>
/// Permission(s) for which the item is managed
/// </summary>
string Permission { get; }

/// <summary>
/// What should be used when the permission is available
/// </summary>
TWhen WhenPermission { get; }

/// <summary>
/// What should be used when the permission is not available
/// </summary>
TWhen WhenPermissionMissing { get; }
}
}

This file was deleted.

27 changes: 14 additions & 13 deletions Dapplo.CaliburnMicro/Behaviors/ValueChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,24 @@ public void Update(TValue value, string targetValue)
var valueChanged = stringValue != Value;
var targetValueChanged = targetValue != TargetValue;

if (valueChanged || targetValueChanged)
if (!valueChanged && !targetValueChanged)
{
if (valueChanged)
{
Value = stringValue;
}
return;
}
if (valueChanged)
{
Value = stringValue;
}

if (targetValueChanged)
{
TargetValue = targetValue;
}
if (targetValueChanged)
{
TargetValue = targetValue;
}

ParseTargetValues();
ParseTargetValues();

// Update the IsMatch
IsMatch = (Value == null) || Value.Equals("") ? string.IsNullOrEmpty(TargetValue) : _parsedTargetValues.Contains(stringValue);
}
// Update the IsMatch
IsMatch = (Value == null) || Value.Equals("") ? string.IsNullOrEmpty(TargetValue) : _parsedTargetValues.Contains(stringValue);
}

/// <summary>
Expand Down
11 changes: 6 additions & 5 deletions Dapplo.CaliburnMicro/Collections/TrulyObservableCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,13 @@ private void FullObservableCollectionCollectionChanged(object sender, NotifyColl
((INotifyPropertyChanged) item).PropertyChanged += ItemPropertyChanged;
}
}
if (e.OldItems != null)
if (e.OldItems == null)
{
foreach (var item in e.OldItems)
{
((INotifyPropertyChanged) item).PropertyChanged -= ItemPropertyChanged;
}
return;
}
foreach (var item in e.OldItems)
{
((INotifyPropertyChanged) item).PropertyChanged -= ItemPropertyChanged;
}
}

Expand Down
Loading

0 comments on commit b737450

Please sign in to comment.