Added toolbox panel

This commit is contained in:
Tiberiu Chibici 2014-10-11 10:33:59 +03:00
parent ba0101374e
commit ed320b455c
4 changed files with 294 additions and 37 deletions

View File

@ -143,7 +143,7 @@
<GridSplitter Grid.Row="2" Grid.Column="1"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
<!-- Document panel -->
<!-- Document area -->
<ad:DockingManager
x:Name="dockingManager"
Grid.Row="2">
@ -152,7 +152,9 @@
<adlayout:LayoutRoot.LeftSide>
<adlayout:LayoutAnchorSide>
<adlayout:LayoutAnchorGroup>
<adlayout:LayoutAnchorable Title="Toolbox" />
<adlayout:LayoutAnchorable Title="Toolbox">
<uiPanels:ToolboxPanel x:Name="toolboxPanel" />
</adlayout:LayoutAnchorable>
</adlayout:LayoutAnchorGroup>
</adlayout:LayoutAnchorSide>
</adlayout:LayoutRoot.LeftSide>

View File

@ -14,6 +14,7 @@ using System.Windows.Navigation;
using System.Windows.Shapes;
using RainmeterStudio.Business;
using RainmeterStudio.Core.Editor;
using RainmeterStudio.Core.Editor.Features;
using RainmeterStudio.Core.Model;
using RainmeterStudio.Core.Model.Events;
using RainmeterStudio.UI.Controller;
@ -27,13 +28,30 @@ namespace RainmeterStudio.UI
/// </summary>
public partial class MainWindow : Window
{
/// <summary>
/// Gets or sets the document controller
/// </summary>
public DocumentController DocumentController { get; set; }
/// <summary>
/// Gets or sets the project controller
/// </summary>
public ProjectController ProjectController { get; set; }
/// <summary>
/// Gets the project panel
/// </summary>
public ProjectPanel ProjectPanel { get { return projectPanel; } }
private Dictionary<LayoutDocument, IDocumentEditor> _openedDocuments = new Dictionary<LayoutDocument, IDocumentEditor>();
#region Constructor
/// <summary>
/// Initializes the main window
/// </summary>
/// <param name="projCtrl">The project controller</param>
/// <param name="docCtrl">The document controller</param>
public MainWindow(ProjectController projCtrl, DocumentController docCtrl)
{
InitializeComponent();
@ -52,35 +70,62 @@ namespace RainmeterStudio.UI
this.AddKeyBinding(ProjectController.ProjectOpenCommand);
// Subscribe to events
DocumentController.DocumentOpened += documentController_DocumentOpened;
DocumentController.DocumentOpened += DocumentController_DocumentOpened;
// Initialize panels
projectPanel.ProjectController = ProjectController;
projectPanel.DocumentController = DocumentController;
}
void documentController_DocumentOpened(object sender, DocumentOpenedEventArgs e)
#endregion
#region Document opened event handler
void DocumentController_DocumentOpened(object sender, DocumentOpenedEventArgs e)
{
// Create a new panel
LayoutDocument document = new LayoutDocument();
_openedDocuments.Add(document, e.Editor);
OpenDocument(e.Editor);
}
document.Content = e.Editor.EditorUI;
document.Closing += document_Closing;
document.Closed += document_Closed;
document.Title = GetDocumentTitle(e.Document);
document.IsActiveChanged += new EventHandler((sender2, e2) =>
#endregion
#region Document events
void Document_IsActiveChanged(object sender, EventArgs e)
{
LayoutDocument document = (LayoutDocument)sender;
IDocumentEditor editor = _openedDocuments[document];
if (document.IsActive)
{
if (document.IsActive)
DocumentController.ActiveDocumentEditor = e.Editor;
});
// Set active editor in controller
DocumentController.ActiveDocumentEditor = editor;
documentPane.Children.Add(document);
documentPane.SelectedContentIndex = documentPane.IndexOf(document);
// Set up toolbox
SetUpToolbox(editor);
}
else
{
// Disable drop, so that we don't drop invalid items in an editor
editor.EditorUI.AllowDrop = false;
}
}
e.Document.PropertyChanged += Document_PropertyChanged;
if (e.Document.Reference != null)
e.Document.Reference.PropertyChanged += Reference_PropertyChanged;
void Document_Closed(object sender, EventArgs e)
{
CloseDocument((LayoutDocument)sender);
}
void Document_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// Get editor
var document = (LayoutDocument)sender;
var editor = _openedDocuments[document];
// Try to close active document
if (!DocumentController.Close(editor))
{
e.Cancel = true;
}
}
private void Document_PropertyChanged(object sender, PropertyChangedEventArgs e)
@ -125,6 +170,23 @@ namespace RainmeterStudio.UI
}
}
#endregion
#region Window events
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// Try to close
if (!DocumentController.CloseAll())
{
e.Cancel = true;
}
}
#endregion
#region Helper methods
private string GetDocumentTitle(IDocument document)
{
string documentName;
@ -152,31 +214,52 @@ namespace RainmeterStudio.UI
return documentName;
}
void document_Closed(object sender, EventArgs e)
private LayoutDocument OpenDocument(IDocumentEditor editor)
{
var layoutDocument = (LayoutDocument)sender;
// Create document
LayoutDocument document = new LayoutDocument();
document.Content = editor.EditorUI;
document.Title = GetDocumentTitle(editor.AttachedDocument);
// Set up events
document.Closing += Document_Closing;
document.Closed += Document_Closed;
document.IsActiveChanged += Document_IsActiveChanged;
// Add to dictionary
_openedDocuments.Add(document, editor);
// Add to layout
documentPane.Children.Add(document);
documentPane.SelectedContentIndex = documentPane.IndexOf(document);
// Subscribe to document events
editor.AttachedDocument.PropertyChanged += Document_PropertyChanged;
if (editor.AttachedDocument.Reference != null)
editor.AttachedDocument.Reference.PropertyChanged += Reference_PropertyChanged;
return document;
}
void document_Closing(object sender, System.ComponentModel.CancelEventArgs e)
private void CloseDocument(LayoutDocument document)
{
// Get editor
var document = (LayoutDocument)sender;
var editor = _openedDocuments[document];
_openedDocuments.Remove(document);
}
// Try to close active document
if (!DocumentController.Close(editor))
private void SetUpToolbox(IDocumentEditor editor)
{
var toolboxProvider = editor as IToolboxProvider;
// Set toolbar panel
toolboxPanel.ItemsSource = toolboxProvider;
// Enable 'allow drop'
if (toolboxProvider != null)
{
e.Cancel = true;
editor.EditorUI.AllowDrop = true;
}
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// Try to close
if (!DocumentController.CloseAll())
{
e.Cancel = true;
}
}
#endregion
}
}

View File

@ -0,0 +1,63 @@
<UserControl x:Class="RainmeterStudio.UI.Panels.ToolboxPanel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<Style x:Key="ToolboxListViewStyle" TargetType="ListView">
<Style.Triggers>
<Trigger Property="HasItems" Value="False">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListView">
<Grid>
<TextBlock TextAlignment="Center"
HorizontalAlignment="Center"
VerticalAlignment="Center"
TextWrapping="Wrap"
Text="There are no toolbox items available for the active document." />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<DockPanel Grid.Row="0" LastChildFill="True">
<Button DockPanel.Dock="Right">Search</Button>
<TextBox Name="searchTextBox"/>
</DockPanel>
<ListView Name="listItems"
Style="{StaticResource ToolboxListViewStyle}"
Grid.Row="2">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<EventSetter Event="PreviewMouseMove" Handler="Item_PreviewMouseMove" />
<EventSetter Event="PreviewMouseDoubleClick" Handler="Item_PreviewMouseDoubleClick" />
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<DockPanel LastChildFill="True" ToolTip="{Binding ToolTip}">
<Image Width="16" Height="16" Source="{Binding Icon}" DockPanel.Dock="Left" />
<TextBlock Text="{Binding DisplayText}" />
</DockPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</UserControl>

View File

@ -0,0 +1,109 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using RainmeterStudio.Core.Editor.Features;
using RainmeterStudio.UI.ViewModel;
namespace RainmeterStudio.UI.Panels
{
/// <summary>
/// Interaction logic for ToolboxPanel.xaml
/// </summary>
public partial class ToolboxPanel : UserControl
{
private IToolboxProvider _itemsSource = null;
/// <summary>
/// Gets or sets the items source
/// </summary>
public IToolboxProvider ItemsSource
{
get
{
return _itemsSource;
}
set
{
// Unsubscribe from old items source
if (_itemsSource != null)
{
_itemsSource.ToolboxItemsChanged -= ItemsSource_ToolboxItemsChanged;
}
// Change items source
_itemsSource = value;
// Subscribe to new items source
if (_itemsSource != null)
{
value.ToolboxItemsChanged += ItemsSource_ToolboxItemsChanged;
}
// Refresh items
RefreshItems();
}
}
/// <summary>
/// Initializes the toolbox panel
/// </summary>
public ToolboxPanel()
{
InitializeComponent();
}
private void RefreshItems()
{
if (_itemsSource == null)
{
listItems.ItemsSource = Enumerable.Empty<ToolboxItemViewModel>();
}
else
{
listItems.ItemsSource = _itemsSource.ToolboxItems.Select(item => new ToolboxItemViewModel(item));
}
}
void ItemsSource_ToolboxItemsChanged(object sender, EventArgs e)
{
RefreshItems();
}
#region Adding to editor (drag and double clicking)
void Item_PreviewMouseMove(object sender, MouseEventArgs e)
{
var treeViewItem = sender as TreeViewItem;
var item = treeViewItem.Header as ToolboxItem;
if (item != null && e.LeftButton == MouseButtonState.Pressed)
{
DragDrop.DoDragDrop(this, item, DragDropEffects.Move);
}
}
void Item_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var treeViewItem = sender as TreeViewItem;
var item = treeViewItem.Header as ToolboxItem;
if (item != null)
{
ItemsSource.ToolboxItemDrop(item);
}
}
#endregion
}
}