changed project creation save-cancel handles

https://github.com/JaneaSystems/PowerToys-DevProjects/issues/14
This commit is contained in:
seraphima
2024-07-04 16:01:07 +02:00
parent 647ea2db1e
commit deb1680f91
5 changed files with 68 additions and 40 deletions

View File

@@ -6,6 +6,7 @@ using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using ProjectsEditor.Data;
using ProjectsEditor.Models;
using ProjectsEditor.ViewModels;
@@ -37,7 +38,15 @@ namespace ProjectsEditor
private void SaveButtonClicked(object sender, RoutedEventArgs e)
{
Project projectToSave = this.DataContext as Project;
_mainViewModel.SaveProject(projectToSave);
if (projectToSave.EditorWindowTitle == Properties.Resources.CreateProject)
{
_mainViewModel.AddNewProject(projectToSave);
}
else
{
_mainViewModel.SaveProject(projectToSave);
}
_mainViewModel.SwitchToMainView();
}
@@ -49,7 +58,10 @@ namespace ProjectsEditor
private void CancelButtonClicked(object sender, RoutedEventArgs e)
{
_mainViewModel.CancelLastEdit();
// delete the temp file created by the snapshot tool
TempProjectData parser = new TempProjectData();
parser.DeleteTempFile();
_mainViewModel.SwitchToMainView();
}

View File

@@ -150,6 +150,15 @@ namespace ProjectsEditor.Properties {
}
}
/// <summary>
/// Looks up a localized string similar to Project.
/// </summary>
public static string DefaultProjectNamePrefix {
get {
return ResourceManager.GetString("DefaultProjectNamePrefix", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Remove.
/// </summary>

View File

@@ -147,6 +147,9 @@
<data name="DaysAgo" xml:space="preserve">
<value>days ago</value>
</data>
<data name="DefaultProjectNamePrefix" xml:space="preserve">
<value>Project</value>
</data>
<data name="Delete" xml:space="preserve">
<value>Remove</value>
</data>

View File

@@ -2,19 +2,7 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
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.Shapes;
using ProjectsEditor.ViewModels;
namespace ProjectsEditor
@@ -41,7 +29,7 @@ namespace ProjectsEditor
private void SnapshotButtonClicked(object sender, RoutedEventArgs e)
{
Close();
_mainViewModel.AddNewProject();
_mainViewModel.SnapNewProject();
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)

View File

@@ -8,9 +8,9 @@ using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Timers;
using ManagedCommon;
@@ -127,7 +127,6 @@ namespace ProjectsEditor.ViewModels
}
private Project editedProject;
private bool isEditedProjectNewlyCreated;
private string projectNameBeingEdited;
private MainWindow _mainWindow;
private System.Timers.Timer lastUpdatedTimer;
@@ -218,45 +217,62 @@ namespace ProjectsEditor.ViewModels
project.Name = projectNameBeingEdited;
}
public async void AddNewProject()
public async void SnapNewProject()
{
CancelSnapshot();
await Task.Run(() => RunSnapshotTool());
if (_projectsEditorIO.ParseProjects(this).Result == true && Projects.Count != 0)
{
int repeatCounter = 1;
string newName = Projects.Count != 0 ? Projects.Last().Name : "Project 1"; // TODO: localizable project name
while (Projects.Where(x => x.Name.Equals(Projects.Last().Name, StringComparison.Ordinal)).Count() > 1)
{
Projects.Last().Name = $"{newName} ({repeatCounter})";
repeatCounter++;
}
_projectsEditorIO.SerializeProjects(Projects.ToList());
EditProject(Projects.Last(), true);
await Task.Run(() => RunSnapshotTool());
Project project = new Project();
if (_projectsEditorIO.ParseTempProject(out project).Result)
{
EditProject(project, true);
}
}
public void EditProject(Project selectedProject, bool isNewlyCreated = false)
{
isEditedProjectNewlyCreated = isNewlyCreated;
var editPage = new ProjectEditor(this);
SetEditedProject(selectedProject);
Project projectEdited = new Project(selectedProject) { EditorWindowTitle = isNewlyCreated ? Properties.Resources.CreateProject : Properties.Resources.EditProject };
projectEdited.Initialize();
editPage.DataContext = projectEdited;
if (isNewlyCreated)
{
// generate a default name for the new project
string defaultNamePrefix = Properties.Resources.DefaultProjectNamePrefix;
int nextProjectIndex = 0;
foreach (var proj in Projects)
{
if (proj.Name.StartsWith(defaultNamePrefix, StringComparison.CurrentCulture))
{
try
{
int index = int.Parse(proj.Name[(defaultNamePrefix.Length + 1)..], CultureInfo.CurrentCulture);
if (nextProjectIndex < index)
{
nextProjectIndex = index;
}
}
catch (Exception)
{
}
}
}
selectedProject.Name = defaultNamePrefix + " " + (nextProjectIndex + 1).ToString(CultureInfo.CurrentCulture);
}
selectedProject.EditorWindowTitle = isNewlyCreated ? Properties.Resources.CreateProject : Properties.Resources.EditProject;
selectedProject.Initialize();
editPage.DataContext = selectedProject;
_mainWindow.ShowPage(editPage);
lastUpdatedTimer.Stop();
}
public void CancelLastEdit()
public void AddNewProject(Project project)
{
if (isEditedProjectNewlyCreated)
{
Projects.Remove(editedProject);
_projectsEditorIO.SerializeProjects(Projects.ToList());
}
Projects.Add(project);
_projectsEditorIO.SerializeProjects(Projects.ToList());
}
public void DeleteProject(Project selectedProject)