Skip to content

Commit

Permalink
[Core] Closing documents work properly
Browse files Browse the repository at this point in the history
  • Loading branch information
BAndysc committed Nov 1, 2018
1 parent be54537 commit d962e0c
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 36 deletions.
2 changes: 1 addition & 1 deletion WDE.Common/Events/EventActiveDocumentChanged.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace WDE.Common.Events
{
public class EventActiveDocumentChanged : PubSubEvent<Document>
public class EventActiveDocumentChanged : PubSubEvent<IDocument>
{
}
}
1 change: 0 additions & 1 deletion WDE.Common/Managers/IDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,4 @@ public interface IDocument

ContentControl Content { get; }
}

}
16 changes: 16 additions & 0 deletions WDE.Common/Managers/ITool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;

namespace WDE.Common.Managers
{
public interface ITool
{
string Title { get; }
ContentControl Content { get; }
}
}
8 changes: 4 additions & 4 deletions WDE.Common/Managers/IWindowManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ namespace WDE.Common.Managers
[UniqueProvider]
public interface IWindowManager
{
Document ActiveDocument { get; set; }
IDocument ActiveDocument { get; set; }

ObservableCollection<Document> OpenedDocuments { get; }
void OpenDocument(Document editor);
ObservableCollection<IDocument> OpenedDocuments { get; }
void OpenDocument(IDocument editor);

ObservableCollection<Document> OpenedTools { get; }
ObservableCollection<ITool> OpenedTools { get; }
void OpenTool(IToolProvider provider);
}
}
1 change: 1 addition & 0 deletions WDE.Common/WDE.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
<Compile Include="IConfigurable.cs" />
<Compile Include="Managers\Document.cs" />
<Compile Include="Managers\IDocument.cs" />
<Compile Include="Managers\ITool.cs" />
<Compile Include="Managers\IWindowManager.cs" />
<Compile Include="Properties\Annotations.cs" />
<Compile Include="Providers\IEntriesProviderService.cs" />
Expand Down
52 changes: 52 additions & 0 deletions WoWDatabaseEditor/Managers/ViewModels/DocumentDecorator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Prism.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;
using WDE.Common.History;
using WDE.Common.Managers;

namespace WoWDatabaseEditor.Managers.ViewModels
{
internal class DocumentDecorator : IDocument
{
private readonly IDocument document;
private readonly Action<DocumentDecorator> closeAction;

public string Title => document.Title;

public ICommand Undo => document.Undo;

public ICommand Redo => document.Redo;

public ICommand Save => document.Save;

public ICommand CloseCommand { get; private set; }

public bool CanClose => document.CanClose;

public IHistoryManager History => document.History;

public ContentControl Content => document.Content;

public System.Windows.Visibility Visibility { get; set; }

public DocumentDecorator(IDocument document, Action<DocumentDecorator> closeAction)
{
this.document = document;
this.closeAction = closeAction;
CloseCommand = new DelegateCommand(Close);

Visibility = System.Windows.Visibility.Visible;
}

private void Close()
{
closeAction(this);
document.CloseCommand?.Execute(null);
}
}
}
35 changes: 35 additions & 0 deletions WoWDatabaseEditor/Managers/ViewModels/ToolWindow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Prism.Commands;
using Prism.Mvvm;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using WDE.Common.Managers;

namespace WoWDatabaseEditor.Managers.ViewModels
{
internal class ToolWindow : BindableBase, ITool
{
public string Title { get; private set; }

public ContentControl Content { get; private set; }

public bool CanClose => false;

public ICommand CloseCommand { get; private set; }

private Visibility _visibility;
public Visibility Visibility
{
get => _visibility;
set => SetProperty(ref _visibility, value);
}

public ToolWindow(string title, ContentControl content)
{
Title = title;
Content = content;
Visibility = System.Windows.Visibility.Visible;
CloseCommand = new DelegateCommand(() => { });
}
}
}
57 changes: 32 additions & 25 deletions WoWDatabaseEditor/Managers/WindowManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
using WDE.Common.Managers;
using WDE.Common.Windows;
using Prism.Ioc;
using Prism.Commands;
using System.Windows.Controls;
using System.Windows.Input;
using WoWDatabaseEditor.Managers.ViewModels;

namespace WoWDatabaseEditor.Managers
{
Expand All @@ -23,48 +27,51 @@ public WindowManager(IEventAggregator eventAggregator)
_eventAggregator = eventAggregator;
}

private Document _activeDocument;
public Document ActiveDocument
private IDocument _activeDocument;
public IDocument ActiveDocument
{
get => _activeDocument;
set { SetProperty(ref _activeDocument, value); _eventAggregator.GetEvent<EventActiveDocumentChanged>().Publish(value); }
set {
if (value == null && OpenedDocuments.Contains(_activeDocument))
return;
SetProperty(ref _activeDocument, value);
_eventAggregator.GetEvent<EventActiveDocumentChanged>().Publish(value);
}
}

public ObservableCollection<Document> OpenedDocuments { get; } = new ObservableCollection<Document>();
public ObservableCollection<Document> OpenedTools { get; } = new ObservableCollection<Document>();

private HashSet<Type> OpenedToolTypes { get; } = new HashSet<Type>();
private Dictionary<Type, Document> Opened { get; } = new Dictionary<Type, Document>();
public void OpenDocument(Document editor)
public ObservableCollection<IDocument> OpenedDocuments { get; } = new ObservableCollection<IDocument>();
public ObservableCollection<ITool> OpenedTools { get; } = new ObservableCollection<ITool>();
private Dictionary<Type, ToolWindow> Opened { get; } = new Dictionary<Type, ToolWindow>();

public void OpenDocument(IDocument editor)
{
OpenedDocuments.Add(editor);
ActiveDocument = editor;
var document = new DocumentDecorator(editor, doc =>
{
OpenedDocuments.Remove(doc);
if (ActiveDocument == doc)
ActiveDocument = null;
});
OpenedDocuments.Add(document);
ActiveDocument = document;
}

public void OpenTool(IToolProvider provider)
{
if (!provider.AllowMultiple)
{
if (OpenedToolTypes.Contains(provider.GetType()))
if (Opened.ContainsKey(provider.GetType()))
{
//todo
//Opened[provider.GetType()].Visibilityt = Visibility.Visible;
Opened[provider.GetType()].Visibility = Visibility.Visible;
return;
}

OpenedToolTypes.Add(provider.GetType());
}

var doc = new Document
{
Title = provider.Name,
Content = provider.GetView(),
CloseCommand = new ActionCommand(a => { MessageBox.Show("aaaa"); }),
CanClose = true
};
var toolWindow = new ToolWindow(provider.Name, provider.GetView());

if (!provider.AllowMultiple)
Opened.Add(provider.GetType(), toolWindow);

Opened.Add(provider.GetType(), doc);
OpenedTools.Add(doc);
OpenedTools.Add(toolWindow);
}
}
}
32 changes: 32 additions & 0 deletions WoWDatabaseEditor/Views/Helpers/ActiveDocumentConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using WDE.Common.Managers;

namespace WoWDatabaseEditor.Views.Helpers
{
public class ActiveDocumentConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
if (value is IDocument)
return value;

return null;
}

public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
if (value is IDocument)
return value;

return null;
}
}
}
14 changes: 9 additions & 5 deletions WoWDatabaseEditor/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
prism:ViewModelLocator.AutoWireViewModel="True"
xmlns:avalonDock="http://schemas.xceed.com/wpf/xaml/avalondock"
xmlns:views="clr-namespace:WoWDatabaseEditor.Views"
xmlns:helpers="clr-namespace:WoWDatabaseEditor.Views.Helpers"
xmlns:viewModels="clr-namespace:WoWDatabaseEditor.ViewModels"
xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
Style="{DynamicResource MainWindowStyle}"
Expand All @@ -19,6 +20,9 @@
Icon="/icon.png"
Loaded="Window_Loaded"
Closed="Window_Closed">
<Window.Resources>
<helpers:ActiveDocumentConverter x:Key="ActiveDocumentConverter" />
</Window.Resources>
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="_File">
Expand Down Expand Up @@ -62,7 +66,7 @@
</MenuItem>
</Menu>
<avalonDock:DockingManager AnchorablesSource="{Binding WindowManager.OpenedTools}" x:Name="DockingManager" DocumentClosed="DockingManager_OnDocumentClosed" ActiveContent="{Binding WindowManager.ActiveDocument,Mode=TwoWay,
diag:PresentationTraceSources.TraceLevel=High}" DocumentsSource="{Binding WindowManager.OpenedDocuments}" >
diag:PresentationTraceSources.TraceLevel=High,Converter={StaticResource ActiveDocumentConverter}}" DocumentsSource="{Binding WindowManager.OpenedDocuments}" >

<avalonDock:DockingManager.LayoutItemTemplateSelector>
<views:PaneDocumentTemplateSelector>
Expand All @@ -77,10 +81,10 @@
<avalonDock:DockingManager.LayoutItemContainerStyle>
<!-- you can add additional bindings from the layoutitem to the DockWindowViewModel -->
<Style TargetType="{x:Type avalonDock:LayoutItem}" >
<Setter Property="Title" Value="{Binding Model.Title, Mode=TwoWay}" />
<Setter Property="CloseCommand" Value="{Binding Model.CloseCommand, Mode=TwoWay}" />
<Setter Property="CanClose" Value="{Binding Model.CanClose, Mode=TwoWay}" />
<Setter Property="Visibility" Value="{Binding Model.Visibilityt, Mode=TwoWay}"></Setter>
<Setter Property="Title" Value="{Binding Model.Title}" />
<Setter Property="CloseCommand" Value="{Binding Model.CloseCommand}" />
<Setter Property="CanClose" Value="{Binding Model.CanClose}" />
<Setter Property="Visibility" Value="{Binding Model.Visibility, Mode=TwoWay}"></Setter>
</Style>
</avalonDock:DockingManager.LayoutItemContainerStyle>
</avalonDock:DockingManager>
Expand Down
3 changes: 3 additions & 0 deletions WoWDatabaseEditor/WoWDatabaseEditorCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,16 @@
</ApplicationDefinition>
<Compile Include="Extensions\FocusBehavior.cs" />
<Compile Include="MainModule.cs" />
<Compile Include="Managers\ViewModels\DocumentDecorator.cs" />
<Compile Include="Managers\ViewModels\ToolWindow.cs" />
<Compile Include="ModulesManagement\Configuration\ModulesConfiguration.cs" />
<Compile Include="ModulesManagement\Configuration\ViewModels\ModulesConfigViewModel.cs" />
<Compile Include="ModulesManagement\Configuration\Views\ModulesConfigView.xaml.cs">
<DependentUpon>ModulesConfigView.xaml</DependentUpon>
</Compile>
<Compile Include="ModulesManagement\IModulesManager.cs" />
<Compile Include="ModulesManagement\ModulesManager.cs" />
<Compile Include="Views\Helpers\ActiveDocumentConverter.cs" />
<Compile Include="Views\PaneDocumentTemplateSelector.cs" />
<Compile Include="Views\SplashScreenView.xaml.cs">
<DependentUpon>SplashScreenView.xaml</DependentUpon>
Expand Down

0 comments on commit d962e0c

Please sign in to comment.