Work on project, document managers, controllers and UI

This commit is contained in:
2014-09-08 21:31:47 +03:00
parent e338ae31ca
commit fd166fe814
19 changed files with 698 additions and 148 deletions

View File

@ -9,6 +9,8 @@ using RainmeterStudio.UI.Dialogs;
using RainmeterStudio.UI.ViewModel;
using RainmeterStudio.Core.Model;
using System.IO;
using Microsoft.Win32;
using RainmeterStudio.Core.Utils;
namespace RainmeterStudio.UI.Controller
{
@ -34,10 +36,20 @@ namespace RainmeterStudio.UI.Controller
public Command DocumentOpenCommand { get; private set; }
public Command DocumentSaveCommand { get; private set; }
public Command DocumentSaveAsCommand { get; private set; }
public Command DocumentSaveACopyCommand { get; private set; }
public Command DocumentSaveAllCommand { get; private set; }
public Command DocumentCloseCommand { get; private set; }
#endregion
#region Events
/// <summary>
/// Triggered when a document is opened
/// </summary>
@ -56,15 +68,77 @@ namespace RainmeterStudio.UI.Controller
remove { DocumentManager.DocumentClosed -= value; }
}
/// <summary>
/// Triggered when the active document editor changes.
/// </summary>
public event EventHandler ActiveDocumentEditorChanged;
#endregion
#region Properties
private IDocumentEditor _activeDocumentEditor = null;
/// <summary>
/// Gets or sets the active document editor.
/// This must be set by the main window when active document changes.
/// </summary>
public IDocumentEditor ActiveDocumentEditor
{
get
{
return _activeDocumentEditor;
}
set
{
_activeDocumentEditor = value;
if (ActiveDocumentEditorChanged != null)
ActiveDocumentEditorChanged(this, new EventArgs());
}
}
public MainWindow OwnerWindow { get; set; }
#endregion
/// <summary>
/// Initializes a document controller
/// </summary>
/// <param name="documentManager"></param>
/// <param name="projectManager"></param>
public DocumentController(DocumentManager documentManager, ProjectManager projectManager)
{
DocumentManager = documentManager;
ProjectManager = projectManager;
DocumentCreateCommand = new Command("DocumentCreateCommand", () => Create(), () => ProjectManager.ActiveProject != null);
ProjectManager.ActiveProjectChanged += new EventHandler((obj, e) => DocumentCreateCommand.NotifyCanExecuteChanged());
DocumentCreateCommand = new Command("DocumentCreate", Create, () => ProjectManager.ActiveProject != null);
DocumentOpenCommand = new Command("DocumentOpen", Open);
DocumentSaveCommand = new Command("DocumentSave", () => Save(), HasActiveDocumentEditor);
DocumentSaveAsCommand = new Command("DocumentSaveAs", () => SaveAs(), HasActiveDocumentEditor);
DocumentSaveACopyCommand = new Command("DocumentSaveACopy", () => SaveACopy(), HasActiveDocumentEditor);
DocumentSaveAllCommand = new Command("DocumentSaveAll", SaveAll, () => ProjectManager.ActiveProject != null);
DocumentCloseCommand = new Command("DocumentClose", () => Close(), HasActiveDocumentEditor);
ProjectManager.ActiveProjectChanged += new EventHandler((obj, e) =>
{
DocumentCreateCommand.NotifyCanExecuteChanged();
DocumentSaveAllCommand.NotifyCanExecuteChanged();
});
ActiveDocumentEditorChanged += new EventHandler((obj, e) =>
{
DocumentSaveCommand.NotifyCanExecuteChanged();
DocumentSaveAsCommand.NotifyCanExecuteChanged();
DocumentSaveACopyCommand.NotifyCanExecuteChanged();
DocumentCloseCommand.NotifyCanExecuteChanged();
});
}
private bool HasActiveDocumentEditor()
{
return ActiveDocumentEditor != null;
}
#region Document operations
@ -83,13 +157,13 @@ namespace RainmeterStudio.UI.Controller
return;
var format = dialog.SelectedTemplate;
// Call manager
var editor = DocumentManager.Create(format.Template);
// Set the reference
var name = dialog.SelectedName;
string folder = OwnerWindow.ProjectPanel.ActiveItem.StoragePath;
if (!Directory.Exists(folder))
folder = Path.GetDirectoryName(folder);
@ -105,23 +179,105 @@ namespace RainmeterStudio.UI.Controller
}
/// <summary>
/// Saves the document opened in specified editor
/// Shows an 'open document' dialog, and opens a document
/// </summary>
/// <param name="editor">Editor</param>
public void Save(IDocumentEditor editor)
public void Open()
{
if (!editor.AttachedDocument.Reference.IsOnStorage())
{
SaveAs(editor);
return;
}
// Show open dialog
OpenFileDialog dialog = new OpenFileDialog();
dialog.Title = Resources.Strings.Dialog_OpenDocument_Title;
dialog.Filter = Resources.Strings.Dialog_FileType_AllFiles + "|*.*";
dialog.InitialDirectory = Properties.Settings.Default.Project_SavedLocation;
// TODO
bool? res = dialog.ShowDialog();
if (res.HasValue && res.Value)
{
// Open file
DocumentManager.Open(dialog.FileName);
}
}
public void SaveAs(IDocumentEditor editor)
/// <summary>
/// Saves the active document
/// </summary>
public bool Save()
{
// TODO
return Save(ActiveDocumentEditor);
}
/// <summary>
/// Saves the active document
/// </summary>
public bool Save(IDocumentEditor editor)
{
if (editor.AttachedDocument.Reference.IsOnStorage())
{
DocumentManager.Save(editor.AttachedDocument);
return true;
}
else
{
return SaveAs(editor);
}
}
/// <summary>
/// Displays a 'save as' dialog, and saves active document
/// </summary>
public bool SaveAs()
{
return SaveAs(ActiveDocumentEditor);
}
/// <summary>
/// Displays a 'save as' dialog, and saves active document
/// </summary>
public bool SaveAs(IDocumentEditor editor)
{
// Show save dialog
SaveFileDialog dialog = new SaveFileDialog();
dialog.Title = Resources.Strings.Dialog_SaveDocument_Title;
dialog.Filter = Resources.Strings.Dialog_FileType_AllFiles + "|*.*";
dialog.FileName = editor.AttachedDocument.Reference.StoragePath;
bool? res = dialog.ShowDialog();
if (res.HasValue && res.Value)
{
DocumentManager.SaveAs(dialog.FileName, editor.AttachedDocument);
return true;
}
return false;
}
/// <summary>
/// Displays a 'save' dialog, and saves a copy of the active document
/// </summary>
public void SaveACopy()
{
// Show save dialog
SaveFileDialog dialog = new SaveFileDialog();
dialog.Title = Resources.Strings.Dialog_SaveDocument_Title;
dialog.Filter = Resources.Strings.Dialog_FileType_AllFiles + "|*.*";
dialog.FileName = ActiveDocumentEditor.AttachedDocument.Reference.StoragePath;
bool? res = dialog.ShowDialog();
if (res.HasValue && res.Value)
{
DocumentManager.SaveACopy(dialog.FileName, ActiveDocumentEditor.AttachedDocument);
}
}
/// <summary>
/// Saves all opened documents
/// </summary>
public void SaveAll()
{
foreach (var editor in DocumentManager.Editors)
{
if (!Save(editor))
return;
}
}
/// <summary>
@ -135,18 +291,14 @@ namespace RainmeterStudio.UI.Controller
// Show the 'are you sure' prompt if necesary
if (editor.AttachedDocument.IsDirty)
{
bool? res = CloseUnsavedDialog.ShowDialog(OwnerWindow, editor.AttachedDocument);
if (res.HasValue)
switch(CloseUnsavedDialog.ShowDialog(OwnerWindow, editor.AttachedDocument))
{
// Save
if (res.Value)
{
Save(editor);
}
}
else
{
return false;
case CloseUnsavedDialogResult.Save:
Save();
break;
case CloseUnsavedDialogResult.Cancel:
return false;
}
}
@ -155,6 +307,56 @@ namespace RainmeterStudio.UI.Controller
return true;
}
/// <summary>
/// Closes the active document.
/// </summary>
/// <returns>True if closed successfully</returns>
/// <remarks>Shows the 'are you sure' prompt if there are unsaved edits.</remarks>
public bool Close()
{
// Show the 'are you sure' prompt if necesary
if (Close(ActiveDocumentEditor))
{
ActiveDocumentEditor = null;
return true;
}
return false;
}
/// <summary>
/// Closes all the opened documents
/// </summary>
/// <returns>True if closed successfully, false if user hit 'cancel'.</returns>
public bool CloseAll()
{
// Get dirty documents
var unsaved = DocumentManager.Editors
.Select(editor => editor.AttachedDocument)
.Where(document => document.IsDirty);
// There are unsaved documents? Display save dialog
if (unsaved.Any())
{
switch (CloseUnsavedDialog.ShowDialog(OwnerWindow, unsaved))
{
case CloseUnsavedDialogResult.Save:
SaveAll();
break;
case CloseUnsavedDialogResult.Cancel:
return false;
}
}
// Close all documents
// To array is used because DocumentManager.Editors is modified when closing a document.
DocumentManager.Editors.ToArray().ForEach(DocumentManager.Close);
// Done
return true;
}
#endregion
/// <summary>

View File

@ -82,10 +82,21 @@ namespace RainmeterStudio.UI.Controller
#region Commands
/// <summary>
/// Create project command
/// </summary>
public Command ProjectCreateCommand { get; private set; }
/// <summary>
/// Open project command
/// </summary>
public Command ProjectOpenCommand { get; private set; }
/// <summary>
/// Close project command
/// </summary>
public Command ProjectCloseCommand { get; private set; }
#endregion
/// <summary>
@ -96,8 +107,11 @@ namespace RainmeterStudio.UI.Controller
{
Manager = manager;
ProjectCreateCommand = new Command("ProjectCreateCommand", () => CreateProject());
ProjectOpenCommand = new Command("ProjectOpenCommand", () => OpenProject());
// Initialize commands
ProjectCreateCommand = new Command("ProjectCreate", CreateProject);
ProjectOpenCommand = new Command("ProjectOpen", OpenProject);
ProjectCloseCommand = new Command("ProjectClose", CloseProject, () => ActiveProject != null);
ActiveProjectChanged += new EventHandler((sender, e) => ProjectCloseCommand.NotifyCanExecuteChanged());
}
/// <summary>
@ -126,7 +140,7 @@ namespace RainmeterStudio.UI.Controller
/// Displays an 'open file' dialog and opens an existing project
/// </summary>
/// <param name="path"></param>
public void OpenProject(string path = null)
public void OpenProject()
{
// Open dialog
OpenFileDialog dialog = new OpenFileDialog();
@ -151,6 +165,7 @@ namespace RainmeterStudio.UI.Controller
/// </summary>
public void CloseProject()
{
Manager.Close();
}
}
}

View File

@ -17,15 +17,14 @@
<TextBlock Grid.Row="0" Text="{x:Static r:Strings.CloseUnsavedDialog_Message }"
Margin="4"/>
<ScrollViewer Grid.Row="1"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Auto">
<TextBlock Name="textFiles"
Margin="4" Padding="4"
Background="White"/>
</ScrollViewer>
<ListBox Name="listUnsavedDocuments" Grid.Row="1" Padding="3"
IsEnabled="False" >
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Reference.Name}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBlock Grid.Row="2" Text="{x:Static r:Strings.CloseUnsavedDialog_Question }"
Margin="4"/>

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
@ -11,9 +12,18 @@ using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using RainmeterStudio.Core.Model;
using RainmeterStudio.Core.Utils;
namespace RainmeterStudio.UI.Dialogs
{
public enum CloseUnsavedDialogResult
{
Unset,
Save,
DoNotSave,
Cancel
}
/// <summary>
/// Interaction logic for CloseUnsavedDialog.xaml
/// </summary>
@ -24,10 +34,11 @@ namespace RainmeterStudio.UI.Dialogs
/// </summary>
/// <param name="unsavedDocuments">List of unsaved documents</param>
/// <returns>Dialog result</returns>
public static bool? ShowDialog(IEnumerable<IDocument> unsavedDocuments)
public static CloseUnsavedDialogResult ShowDialog(IEnumerable<IDocument> unsavedDocuments)
{
var dialog = new CloseUnsavedDialog(unsavedDocuments);
return dialog.ShowDialog();
dialog.ShowDialog();
return dialog.SaveDialogResult;
}
/// <summary>
@ -36,11 +47,11 @@ namespace RainmeterStudio.UI.Dialogs
/// <param name="owner">Owner window</param>
/// <param name="unsavedDocuments">List of unsaved documents</param>
/// <returns>Dialog result</returns>
public static bool? ShowDialog(Window owner, IEnumerable<IDocument> unsavedDocuments)
public static CloseUnsavedDialogResult ShowDialog(Window owner, IEnumerable<IDocument> unsavedDocuments)
{
var dialog = new CloseUnsavedDialog(unsavedDocuments);
dialog.Owner = owner;
return dialog.ShowDialog();
dialog.ShowDialog();
return dialog.SaveDialogResult;
}
/// <summary>
@ -49,13 +60,18 @@ namespace RainmeterStudio.UI.Dialogs
/// <param name="owner">Owner window</param>
/// <param name="unsavedDocuments">List of unsaved documents</param>
/// <returns>Dialog result</returns>
public static bool? ShowDialog(Window owner, params IDocument[] unsavedDocuments)
public static CloseUnsavedDialogResult ShowDialog(Window owner, params IDocument[] unsavedDocuments)
{
var dialog = new CloseUnsavedDialog(unsavedDocuments);
dialog.Owner = owner;
return dialog.ShowDialog();
dialog.ShowDialog();
return dialog.SaveDialogResult;
}
/// <summary>
/// Gets the 'close unsaved' dialog result
/// </summary>
public CloseUnsavedDialogResult SaveDialogResult { get; private set; }
/// <summary>
/// Initializes the dialog
/// </summary>
@ -64,39 +80,25 @@ namespace RainmeterStudio.UI.Dialogs
{
InitializeComponent();
textFiles.Inlines.AddRange(unsavedDocuments.SelectMany(GetInlines));
}
private IEnumerable<Inline> GetInlines(IDocument doc)
{
var folder = System.IO.Path.GetDirectoryName(doc.Reference.StoragePath);
yield return new Run(folder)
{
Foreground = Brushes.DarkGray
};
yield return new Run(doc.Reference.Name)
{
FontWeight = FontWeights.Bold
};
SaveDialogResult = CloseUnsavedDialogResult.Unset;
unsavedDocuments.ForEach(d => listUnsavedDocuments.Items.Add(d));
}
private void buttonSave_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
SaveDialogResult = CloseUnsavedDialogResult.Save;
Close();
}
private void buttonDoNotSave_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
SaveDialogResult = CloseUnsavedDialogResult.DoNotSave;
Close();
}
private void buttonCancel_Click(object sender, RoutedEventArgs e)
{
DialogResult = null;
SaveDialogResult = CloseUnsavedDialogResult.Cancel;
Close();
}
}

View File

@ -14,7 +14,8 @@
Left="{Binding Source={x:Static p:Settings.Default}, Path=MainWindow_Left, Mode=TwoWay}"
Top="{Binding Source={x:Static p:Settings.Default}, Path=MainWindow_Top, Mode=TwoWay}"
ResizeMode="CanResizeWithGrip" >
ResizeMode="CanResizeWithGrip"
Closing="Window_Closing">
<Grid>
<Grid.RowDefinitions>
@ -47,16 +48,58 @@
</MenuItem>
<Separator />
<MenuItem Header="{x:Static r:Strings.MainWindow_File_Open}">
<MenuItem DataContext="{Binding DocumentController.DocumentOpenCommand}"
Style="{StaticResource CommandMenuItemStyle}">
<MenuItem.Icon>
<Image Source="{Binding Icon}" />
</MenuItem.Icon>
</MenuItem>
<MenuItem DataContext="{Binding ProjectController.ProjectOpenCommand}"
Style="{StaticResource CommandMenuItemStyle}">
<MenuItem.Icon>
<Image Source="{Binding Icon}" />
</MenuItem.Icon>
</MenuItem>
<Separator />
</MenuItem>
<Separator />
<MenuItem Header="_Close" />
<MenuItem DataContext="{Binding DocumentController.DocumentSaveCommand}"
Style="{StaticResource CommandMenuItemStyle}" >
<MenuItem.Icon>
<Image Source="{Binding Icon}" />
</MenuItem.Icon>
</MenuItem>
<MenuItem DataContext="{Binding DocumentController.DocumentSaveAsCommand}"
Style="{StaticResource CommandMenuItemStyle}" >
<MenuItem.Icon>
<Image Source="{Binding Icon}" />
</MenuItem.Icon>
</MenuItem>
<MenuItem DataContext="{Binding DocumentController.DocumentSaveACopyCommand}"
Style="{StaticResource CommandMenuItemStyle}" >
<MenuItem.Icon>
<Image Source="{Binding Icon}" />
</MenuItem.Icon>
</MenuItem>
<MenuItem DataContext="{Binding DocumentController.DocumentSaveAllCommand}"
Style="{StaticResource CommandMenuItemStyle}" >
<MenuItem.Icon>
<Image Source="{Binding Icon}" />
</MenuItem.Icon>
</MenuItem>
<Separator />
<MenuItem DataContext="{Binding DocumentController.DocumentCloseCommand}"
Style="{StaticResource CommandMenuItemStyle}" >
<MenuItem.Icon>
<Image Source="{Binding Icon}" />
</MenuItem.Icon>
</MenuItem>
<MenuItem DataContext="{Binding ProjectController.ProjectCloseCommand}"
Style="{StaticResource CommandMenuItemStyle}" >
<MenuItem.Icon>
<Image Source="{Binding Icon}" />
</MenuItem.Icon>
</MenuItem>
<Separator />
<MenuItem Header="E_xit" />
</MenuItem>
<MenuItem Header="_Edit" />

View File

@ -44,8 +44,11 @@ namespace RainmeterStudio.UI
ProjectController = projCtrl;
// Add key bindings
this.AddKeyBinding(ProjectController.ProjectCreateCommand);
this.AddKeyBinding(DocumentController.DocumentCreateCommand);
this.AddKeyBinding(DocumentController.DocumentOpenCommand);
this.AddKeyBinding(DocumentController.DocumentSaveCommand);
this.AddKeyBinding(DocumentController.DocumentCloseCommand);
this.AddKeyBinding(ProjectController.ProjectCreateCommand);
// Subscribe to events
DocumentController.DocumentOpened += documentController_DocumentOpened;
@ -63,17 +66,21 @@ namespace RainmeterStudio.UI
document.Content = e.Editor.EditorUI;
document.Closing += document_Closing;
document.Closed += document_Closed;
document.IsActiveChanged += new EventHandler((sender2, e2) =>
{
if (document.IsActive)
DocumentController.ActiveDocumentEditor = e.Editor;
});
documentPane.Children.Add(document);
documentPane.SelectedContentIndex = documentPane.IndexOf(document);
e.Document.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler((obj, args) =>
{
string documentName;
// Get title
if (!ProjectController.ActiveProject.Contains(e.Document.Reference))
if (ProjectController.ActiveProject == null || !ProjectController.ActiveProject.Contains(e.Document.Reference))
{
documentName = e.Document.Reference.StoragePath ?? "New document";
}
@ -96,25 +103,27 @@ namespace RainmeterStudio.UI
void document_Closed(object sender, EventArgs e)
{
var layoutDocument = (LayoutDocument)sender;
}
void document_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// Get editor
var document = (LayoutDocument)sender;
var editor = _openedDocuments[document];
switch (MessageBox.Show("Are you sure?", "", MessageBoxButton.YesNoCancel, MessageBoxImage.Question))
// Try to close active document
if (!DocumentController.Close(editor))
{
case MessageBoxResult.Yes:
break;
e.Cancel = true;
}
}
case MessageBoxResult.No:
break;
default:
e.Cancel = true;
return;
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// Try to close
if (!DocumentController.CloseAll())
{
e.Cancel = true;
}
}
}