From ed320b455cd9b6db3df54ea5f8ba04169c0f9e5d Mon Sep 17 00:00:00 2001 From: Tiberiu Chibici Date: Sat, 11 Oct 2014 10:33:59 +0300 Subject: [PATCH] Added toolbox panel --- RainmeterStudio/UI/MainWindow.xaml | 6 +- RainmeterStudio/UI/MainWindow.xaml.cs | 153 ++++++++++++++---- RainmeterStudio/UI/Panels/ToolboxPanel.xaml | 63 ++++++++ .../UI/Panels/ToolboxPanel.xaml.cs | 109 +++++++++++++ 4 files changed, 294 insertions(+), 37 deletions(-) create mode 100644 RainmeterStudio/UI/Panels/ToolboxPanel.xaml create mode 100644 RainmeterStudio/UI/Panels/ToolboxPanel.xaml.cs diff --git a/RainmeterStudio/UI/MainWindow.xaml b/RainmeterStudio/UI/MainWindow.xaml index dcbe1288..842cdab2 100644 --- a/RainmeterStudio/UI/MainWindow.xaml +++ b/RainmeterStudio/UI/MainWindow.xaml @@ -143,7 +143,7 @@ - + @@ -152,7 +152,9 @@ - + + + diff --git a/RainmeterStudio/UI/MainWindow.xaml.cs b/RainmeterStudio/UI/MainWindow.xaml.cs index 82d1c13b..d76fe173 100644 --- a/RainmeterStudio/UI/MainWindow.xaml.cs +++ b/RainmeterStudio/UI/MainWindow.xaml.cs @@ -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 /// public partial class MainWindow : Window { + /// + /// Gets or sets the document controller + /// public DocumentController DocumentController { get; set; } + + /// + /// Gets or sets the project controller + /// public ProjectController ProjectController { get; set; } + /// + /// Gets the project panel + /// public ProjectPanel ProjectPanel { get { return projectPanel; } } private Dictionary _openedDocuments = new Dictionary(); + #region Constructor + + /// + /// Initializes the main window + /// + /// The project controller + /// The document controller 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 } } \ No newline at end of file diff --git a/RainmeterStudio/UI/Panels/ToolboxPanel.xaml b/RainmeterStudio/UI/Panels/ToolboxPanel.xaml new file mode 100644 index 00000000..03fa4a7b --- /dev/null +++ b/RainmeterStudio/UI/Panels/ToolboxPanel.xaml @@ -0,0 +1,63 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/RainmeterStudio/UI/Panels/ToolboxPanel.xaml.cs b/RainmeterStudio/UI/Panels/ToolboxPanel.xaml.cs new file mode 100644 index 00000000..4d08f9a6 --- /dev/null +++ b/RainmeterStudio/UI/Panels/ToolboxPanel.xaml.cs @@ -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 +{ + /// + /// Interaction logic for ToolboxPanel.xaml + /// + public partial class ToolboxPanel : UserControl + { + private IToolboxProvider _itemsSource = null; + + /// + /// Gets or sets the items source + /// + 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(); + } + } + + /// + /// Initializes the toolbox panel + /// + public ToolboxPanel() + { + InitializeComponent(); + } + + private void RefreshItems() + { + if (_itemsSource == null) + { + listItems.ItemsSource = Enumerable.Empty(); + } + 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 + } +}