Skip to content

Commit

Permalink
Fix Issue #189
Browse files Browse the repository at this point in the history
Rolling back removal of DctionaryTheme class with additional documentation for its usage.
  • Loading branch information
Dirkster99 committed Aug 30, 2020
1 parent 4e9c310 commit ec3c74f
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 38 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ to also theme standard elements, such as, button and textblock etc.


- Removed the additional [ToolTip](https://github.com/Dirkster99/AvalonDock/commit/5554de5c4bfadc37f974ba29803dc792b54f00d0) and [ContextMenu](https://github.com/Dirkster99/AvalonDock/commit/103e1068bc9f5bae8fef275a0e785393b4115764) styles from the Generic.xaml in VS2013 [more details here](https://github.com/Dirkster99/AvalonDock/pull/170#issuecomment-674253874)
- [#189 Removal of DictionaryTheme breaks my application](https://github.com/Dirkster99/AvalonDock/issues/189) (thanx to [hamohn](https://github.com/hamohn))

## Fixes & Features added in Version 4.3

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/************************************************************************
/************************************************************************
AvalonDock
Copyright (C) 2007-2013 Xceed Software Inc.
Expand Down Expand Up @@ -36,7 +36,7 @@ namespace AvalonDock.Controls
public abstract class LayoutFloatingWindowControl : Window, ILayoutControl
{
#region fields

private ResourceDictionary currentThemeResourceDictionary; // = null
private bool _isInternalChange; //false
private readonly ILayoutElement _model;
private bool _attachDrag = false;
Expand Down Expand Up @@ -246,22 +246,40 @@ public double ContentMinWidth
#endregion Properties

#region Internal Methods

/// <summary>Is Invoked when AvalonDock's WPF Theme changes via the <see cref="DockingManager.OnThemeChanged()"/> method.</summary>
/// <param name="oldTheme"></param>
internal virtual void UpdateThemeResources(Theme oldTheme = null)
{
if (oldTheme != null)
if (oldTheme != null) // Remove the old theme if present
{
var resourceDictionaryToRemove =
Resources.MergedDictionaries.FirstOrDefault(r => r.Source == oldTheme.GetResourceUri());
if (resourceDictionaryToRemove != null)
Resources.MergedDictionaries.Remove(
resourceDictionaryToRemove);
if (oldTheme is DictionaryTheme)
{
if (currentThemeResourceDictionary != null)
{
Resources.MergedDictionaries.Remove(currentThemeResourceDictionary);
currentThemeResourceDictionary = null;
}
}
else
{
var resourceDictionaryToRemove =
Resources.MergedDictionaries.FirstOrDefault(r => r.Source == oldTheme.GetResourceUri());
if (resourceDictionaryToRemove != null)
Resources.MergedDictionaries.Remove(
resourceDictionaryToRemove);
}
}

// Implicit parameter to this method is the new theme already set here
var manager = _model.Root?.Manager;
if (manager?.Theme == null) return;

Resources.MergedDictionaries.Add(new ResourceDictionary { Source = manager.Theme.GetResourceUri() });
if (manager.Theme is DictionaryTheme dictionaryTheme)
{
currentThemeResourceDictionary = dictionaryTheme.ThemeResourceDictionary;
Resources.MergedDictionaries.Add(currentThemeResourceDictionary);
}
else
Resources.MergedDictionaries.Add(new ResourceDictionary { Source = manager.Theme.GetResourceUri() });
}

internal void AttachDrag(bool onActivated = true)
Expand Down
32 changes: 26 additions & 6 deletions source/Components/AvalonDock/Controls/NavigatorWindow.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/************************************************************************
/************************************************************************
AvalonDock
Copyright (C) 2007-2013 Xceed Software Inc.
Expand Down Expand Up @@ -28,6 +28,7 @@ namespace AvalonDock.Controls
public class NavigatorWindow : Window
{
#region fields
private ResourceDictionary currentThemeResourceDictionary; // = null

private const string PART_AnchorableListBox = "PART_AnchorableListBox";
private const string PART_DocumentListBox = "PART_DocumentListBox";
Expand Down Expand Up @@ -286,17 +287,36 @@ protected override void OnPreviewKeyUp(System.Windows.Input.KeyEventArgs e)
/// <param name="value">The new value for the property.</param>
protected void SetDocuments(LayoutDocumentItem[] value) => SetValue(DocumentsPropertyKey, value);

/// <summary>Is Invoked when AvalonDock's WPF Theme changes via the <see cref="DockingManager.OnThemeChanged()"/> method.</summary>
/// <param name="oldTheme"></param>
internal void UpdateThemeResources(Theme oldTheme = null)
{
if (oldTheme != null)
if (oldTheme != null) // Remove the old theme if present
{
var resourceDictionaryToRemove = Resources.MergedDictionaries.FirstOrDefault(r => r.Source == oldTheme.GetResourceUri());
if (resourceDictionaryToRemove != null) Resources.MergedDictionaries.Remove(resourceDictionaryToRemove);
if (oldTheme is DictionaryTheme)
{
if (currentThemeResourceDictionary != null)
{
Resources.MergedDictionaries.Remove(currentThemeResourceDictionary);
currentThemeResourceDictionary = null;
}
}
else
{
var resourceDictionaryToRemove = Resources.MergedDictionaries.FirstOrDefault(r => r.Source == oldTheme.GetResourceUri());
if (resourceDictionaryToRemove != null) Resources.MergedDictionaries.Remove(resourceDictionaryToRemove);
}
}

// Implicit parameter to this method is the new theme already set here
if (_manager.Theme == null) return;

Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = _manager.Theme.GetResourceUri() });
if (_manager.Theme is DictionaryTheme dictionaryTheme)
{
currentThemeResourceDictionary = dictionaryTheme.ThemeResourceDictionary;
Resources.MergedDictionaries.Add(currentThemeResourceDictionary);
}
else
Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = _manager.Theme.GetResourceUri() });
}

internal void SelectNextDocument()
Expand Down
41 changes: 31 additions & 10 deletions source/Components/AvalonDock/Controls/OverlayWindow.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/************************************************************************
/************************************************************************
AvalonDock
Copyright (C) 2007-2013 Xceed Software Inc.
Expand All @@ -25,6 +25,7 @@ namespace AvalonDock.Controls
public class OverlayWindow : Window, IOverlayWindow
{
#region fields
private ResourceDictionary currentThemeResourceDictionary; // = null

private Canvas _mainCanvasPanel;
private Grid _gridDockingManagerDropTargets; // Showing and activating 4 outer drop taget buttons over DockingManager
Expand Down Expand Up @@ -169,21 +170,41 @@ protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
#endregion Overrides

#region Internal Methods

/// <summary>Is Invoked when AvalonDock's WPF Theme changes via the <see cref="DockingManager.OnThemeChanged()"/> method.</summary>
/// <param name="oldTheme"></param>
internal void UpdateThemeResources(Theme oldTheme = null)
{
if (oldTheme != null)
if (oldTheme != null) // Remove the old theme if present
{
var resourceDictionaryToRemove =
Resources.MergedDictionaries.FirstOrDefault(r => r.Source == oldTheme.GetResourceUri());
if (resourceDictionaryToRemove != null)
Resources.MergedDictionaries.Remove(
resourceDictionaryToRemove);
if (oldTheme is DictionaryTheme)
{
if (currentThemeResourceDictionary != null)
{
Resources.MergedDictionaries.Remove(currentThemeResourceDictionary);
currentThemeResourceDictionary = null;
}
}
else
{
var resourceDictionaryToRemove =
Resources.MergedDictionaries.FirstOrDefault(r => r.Source == oldTheme.GetResourceUri());
if (resourceDictionaryToRemove != null)
Resources.MergedDictionaries.Remove(
resourceDictionaryToRemove);
}
}

if (_host.Manager.Theme != null)
if (_host.Manager.Theme != null) // Implicit parameter to this method is the new theme already set here
{
Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = _host.Manager.Theme.GetResourceUri() });
if (_host.Manager.Theme is DictionaryTheme theme)
{
currentThemeResourceDictionary = theme.ThemeResourceDictionary;
Resources.MergedDictionaries.Add(currentThemeResourceDictionary);
}
else
{
Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = _host.Manager.Theme.GetResourceUri() });
}
}
}

Expand Down
42 changes: 31 additions & 11 deletions source/Components/AvalonDock/DockingManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/************************************************************************
/************************************************************************
AvalonDock
Copyright (C) 2007-2013 Xceed Software Inc.
Expand Down Expand Up @@ -42,6 +42,9 @@ namespace AvalonDock
public class DockingManager : Control, IOverlayWindowHost//, ILogicalChildrenContainer
{
#region fields
// ShortCut to current AvalonDock theme if OnThemeChanged() is invoked with DictionaryTheme instance
// in e.OldValue and e.NewValue of the passed event
private ResourceDictionary currentThemeResourceDictionary;

private AutoHideWindowManager _autoHideWindowManager;
private FrameworkElement _autohideArea;
Expand Down Expand Up @@ -1029,24 +1032,41 @@ protected virtual void OnThemeChanged(DependencyPropertyChangedEventArgs e)
{
var oldTheme = e.OldValue as Theme;
var resources = Resources;
if (oldTheme != null)
if (oldTheme != null) // remove old theme from resource dictionary if present
{
var resourceDictionaryToRemove =
resources.MergedDictionaries.FirstOrDefault(r => r.Source == oldTheme.GetResourceUri());
if (resourceDictionaryToRemove != null)
resources.MergedDictionaries.Remove(
resourceDictionaryToRemove);
if (oldTheme is DictionaryTheme) // We are using AvalonDock's own DictionaryTheme class
{
if (currentThemeResourceDictionary != null)
{
resources.MergedDictionaries.Remove(currentThemeResourceDictionary);
currentThemeResourceDictionary = null;
}
}
else // We are using standard ResourceDictionaries
{ // Lockup the old theme and remove it from resource dictionary
var resourceDictionaryToRemove =
resources.MergedDictionaries.FirstOrDefault(r => r.Source == oldTheme.GetResourceUri());
if (resourceDictionaryToRemove != null)
resources.MergedDictionaries.Remove(
resourceDictionaryToRemove);
}
}

if (e.NewValue as Theme != null)
if (e.NewValue as Theme != null) // Add new theme into resource dictionary if present
{
resources.MergedDictionaries.Add(new ResourceDictionary { Source = (e.NewValue as Theme).GetResourceUri() });
if (e.NewValue as Theme is DictionaryTheme theme)
{
currentThemeResourceDictionary = theme.ThemeResourceDictionary;
resources.MergedDictionaries.Add(currentThemeResourceDictionary);
}
else // We are using standard ResourceDictionaries -> Add new theme resource
resources.MergedDictionaries.Add(new ResourceDictionary { Source = (e.NewValue as Theme).GetResourceUri() });
}

foreach (var fwc in _fwList)
foreach (var fwc in _fwList) // Update theme resources in floating window controls
fwc.UpdateThemeResources(oldTheme);

_navigatorWindow?.UpdateThemeResources();
_navigatorWindow?.UpdateThemeResources(); // Update theme resources in related AvalonDock controls
_overlayWindow?.UpdateThemeResources();
}

Expand Down
50 changes: 50 additions & 0 deletions source/Components/AvalonDock/Themes/DictionaryTheme.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/************************************************************************
AvalonDock
Copyright (C) 2007-2013 Xceed Software Inc.
This program is provided to you under the terms of the Microsoft Public
License (Ms-PL) as published at https://opensource.org/licenses/MS-PL
************************************************************************/

using System;
using System.Windows;

namespace AvalonDock.Themes
{
/// <summary>Defines a base class to implement a method for storing the current AvalonDock theme
/// that provides a XAML Uri pointing to a <see cref="ResourceDictionary"/>.
///
/// This class can be used to create customized themes by loading a <see cref="ResourceDictionary"/>
/// from an existing theme (by using theme.GetResourceUri()), and then replacing some key colors
/// (typically the "Accent" colors).
///
/// See Issue https://github.com/Dirkster99/AvalonDock/issues/189 for more details.</summary>
public abstract class DictionaryTheme : Theme
{
#region Constructors
/// <summary>Class constructor</summary>
public DictionaryTheme()
{
}

/// <summary>Class constructor from theme specific resource dictionary.</summary>
/// <param name="themeResourceDictionary"></param>
public DictionaryTheme(ResourceDictionary themeResourceDictionary)
{
this.ThemeResourceDictionary = themeResourceDictionary;
}

#endregion Constructors

/// <summary>Gets the resource dictionary that is associated with this AvalonDock theme.</summary>
public ResourceDictionary ThemeResourceDictionary { get; private set; }

/// <summary>Gets the <see cref="Uri"/> of the XAML that contains the definition for this AvalonDock theme.</summary>
/// <returns><see cref="Uri"/> of the XAML that contains the definition for this custom AvalonDock theme</returns>
public override Uri GetResourceUri()
{
return null;
}
}
}

0 comments on commit ec3c74f

Please sign in to comment.