Skip to content

Commit

Permalink
Added a directory selector dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
microdee committed Jun 27, 2017
1 parent 61a707a commit 5022d0a
Show file tree
Hide file tree
Showing 11 changed files with 270 additions and 10 deletions.
4 changes: 4 additions & 0 deletions src/vpm/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
<assemblyIdentity name="System.Console" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Core" publicKeyToken="b77a5c561934e089" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
41 changes: 41 additions & 0 deletions src/vpm/ChooseDir.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<controls:MetroWindow x:Class="vpm.ChooseDir"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
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"
xmlns:cefSharp="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
xmlns:local="clr-namespace:vpm"
mc:Ignorable="d" Height="96" Width="399"
BorderThickness="0"
GlowBrush="Black"
ResizeMode="CanResizeWithGrip"
Initialized="ChooseDir_OnInitialized_OnInitialized"
Background="#FF2C2C2C"
NonActiveWindowTitleBrush="#FF2C2C2C"
OverrideDefaultWindowCommandsBrush="White"
TitleForeground="White"
WindowTitleBrush="#FF2C2C2C"
Foreground="White"
WindowStyle="ToolWindow" Closed="OnCancelled">
<controls:MetroWindow.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
<!-- Accent and AppTheme setting -->
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/FlatButton.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</controls:MetroWindow.Resources>
<Grid>
<TextBox x:Name="DirBox" Height="23" TextWrapping="Wrap" Text="TextBox" Margin="0,0,28,0" VerticalAlignment="Top" TabIndex="0" Drop="OnDirDrop"/>
<Button x:Name="PickButton" Content="..." HorizontalAlignment="Right" Width="28" Height="26" VerticalAlignment="Top" Click="OnBrowseDir"/>
<Button x:Name="OkButton" Content="OK" Margin="0,26,0,0" IsDefault="True" TabIndex="1" Click="OnConfirm"/>

</Grid>
</controls:MetroWindow>
83 changes: 83 additions & 0 deletions src/vpm/ChooseDir.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System;
using System.Collections.Generic;
using System.IO;
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 MahApps.Metro.Controls;
using Microsoft.WindowsAPICodePack.Dialogs;
using Path = System.IO.Path;

namespace vpm
{
/// <summary>
/// Interaction logic for ChooseDir.xaml
/// </summary>
public partial class ChooseDir
{
public string PathFromVpm { get; set; }
public string PathResult { get; set; }
public bool Cancelled { get; set; }

public ChooseDir(string pathin)
{
PathFromVpm = pathin;
InitializeComponent();
}

private void ChooseDir_OnInitialized_OnInitialized(object sender, EventArgs e)
{
DirBox.Text = PathFromVpm;
}

private void OnDirDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
var files = (string[])e.Data.GetData(DataFormats.FileDrop);
if (files != null)
{
DirBox.Text = files[0];
}
}
}

private void OnBrowseDir(object sender, RoutedEventArgs e)
{
var dialog = new CommonOpenFileDialog
{
IsFolderPicker = true,
Multiselect = false
};
var result = dialog.ShowDialog();
if (result == CommonFileDialogResult.Ok)
{
DirBox.Text = dialog.FileName;
}
}

private void OnConfirm(object sender, RoutedEventArgs e)
{
Cancelled = false;
PathResult = DirBox.Text;
VpmConfig.Instance.WaitSignal = false;
VpmConfig.Instance.DirWindow.Close();
}

private void OnCancelled(object sender, EventArgs e)
{
Cancelled = true;
VpmConfig.Instance.WaitSignal = false;
VpmConfig.Instance.DirWindow.Close();
}
}
}
54 changes: 47 additions & 7 deletions src/vpm/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using MahApps.Metro.Controls;
using PowerArgs;

namespace vpm
Expand All @@ -19,7 +21,7 @@ static void Main(string[] args)
Console.ResetColor();
try
{
Args.Parse<VpmArgs>(args);
VpmConfig.Instance.Arguments = Args.Parse<VpmArgs>(args);
}
catch (ArgException ex)
{
Expand All @@ -36,7 +38,7 @@ static void Main(string[] args)
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("Parsing input Pack");
Console.ResetColor();
var vpxml = VpmUtils.ParseAndValidateXmlFile(Args.GetAmbientArgs<VpmArgs>().VPackFile);
var vpxml = VpmUtils.ParseAndValidateXmlFile(VpmConfig.Instance.Arguments.VPackFile);

var namenode = vpxml.SelectSingleNode("/vpack/meta/name");
var srcnode = vpxml.SelectSingleNode("/vpack/meta/source");
Expand All @@ -53,6 +55,37 @@ static void Main(string[] args)
Console.WriteLine("Input pack is " + name);
Console.ResetColor();

VpmConfig.Instance.WaitSignal = true;
VpmConfig.Instance.WinApp.BeginInvoke(() =>
{
var winapp = VpmConfig.Instance.WinApp;
var window = VpmConfig.Instance.DirWindow = new ChooseDir(VpmConfig.Instance.Arguments.VVVVDir);
winapp.MainWindow = window;
window.Show();
});
VpmUtils.Wait();
var dirwindow = (ChooseDir)VpmConfig.Instance.DirWindow;
if (dirwindow.Cancelled)
{
//VpmUtils.CleanUp();
Environment.Exit(0);
}
VpmConfig.Instance.Arguments.VVVVDir = dirwindow.PathResult;

if (!Directory.Exists(VpmConfig.Instance.Arguments.VVVVDir))
{
Console.WriteLine("Destination directory doesn't exist. Creating it.");
try
{
Directory.CreateDirectory(VpmConfig.Instance.Arguments.VVVVDir);
}
catch (Exception)
{
Console.WriteLine("Couldn't do that.");
throw;
}
}

List<string> aliaslist = null;
if (aliasesnode != null)
{
Expand All @@ -69,7 +102,7 @@ static void Main(string[] args)
Console.WriteLine("Input pack seems to be already existing as " + matchalias);
if (VpmUtils.PromptYayOrNay("Do you want to overwrite?"))
{
var packdir = Path.Combine(Args.GetAmbientArgs<VpmArgs>().VVVVDir, "packs", matchalias);
var packdir = Path.Combine(VpmConfig.Instance.Arguments.VVVVDir, "packs", matchalias);
VpmUtils.DeleteDirectory(packdir, true);
}
else
Expand All @@ -83,11 +116,18 @@ static void Main(string[] args)
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Initialization complete.");
Console.ResetColor();

var winapp = VpmConfig.Instance.WinApp = new Application();
var window = VpmConfig.Instance.AgreeWindow = new UserAgree();

winapp.Run(window);
VpmConfig.Instance.WinApp.BeginInvoke(() =>
{
var winapp = VpmConfig.Instance.WinApp;
var window = VpmConfig.Instance.AgreeWindow = new UserAgree();
winapp.MainWindow = window;
window.Show();
});
while (!VpmConfig.Instance.AgreementsAgreed)
{
Thread.Sleep(10);
}

if (VpmConfig.Instance.InstallationCancelled)
{
Expand Down
2 changes: 1 addition & 1 deletion src/vpm/UserAgree.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
BorderThickness="0"
GlowBrush="Black"
ResizeMode="CanResizeWithGrip"
Initialized="UserAgree_OnInitialized" Background="#FF2C2C2C" NonActiveWindowTitleBrush="#FF2C2C2C" OverrideDefaultWindowCommandsBrush="White" TitleForeground="White" WindowTitleBrush="#FF2C2C2C" Foreground="White">
Initialized="UserAgree_OnInitialized" Background="#FF2C2C2C" NonActiveWindowTitleBrush="#FF2C2C2C" OverrideDefaultWindowCommandsBrush="White" TitleForeground="White" WindowTitleBrush="#FF2C2C2C" Foreground="White" Closed="OnCancelled">
<Controls:MetroWindow.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
Expand Down
11 changes: 10 additions & 1 deletion src/vpm/UserAgree.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ public void ContinueFromJS()
private void ContinueInstall_Click(object sender, RoutedEventArgs e)
{
VpmConfig.Instance.InstallationCancelled = false;
VpmConfig.Instance.WaitSignal = false;
VpmConfig.Instance.AgreementsAgreed = true;
VpmConfig.Instance.AgreeWindow.Close();
VpmConfig.Instance.WinApp.Shutdown();
}

private void UserAgree_OnInitialized(object sender, EventArgs e)
Expand Down Expand Up @@ -160,5 +161,13 @@ private void AgreeAndInstall_OnIsEnabledChanged(object sender, DependencyPropert
if ((bool)e.NewValue) AgreeAndInstall.Opacity = 1;
else AgreeAndInstall.Opacity = 0.5;
}

private void OnCancelled(object sender, EventArgs e)
{
VpmConfig.Instance.InstallationCancelled = true;
VpmConfig.Instance.WaitSignal = false;
VpmConfig.Instance.AgreementsAgreed = true;
VpmConfig.Instance.DirWindow.Close();
}
}
}
2 changes: 2 additions & 0 deletions src/vpm/args.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
using System.IO;
using System.Reflection;
using System.Threading;
using System.Windows;
using BrendanGrant.Helpers.FileAssociation;
using MahApps.Metro.Controls;
using Microsoft.Win32;
using PowerArgs;

Expand Down
38 changes: 37 additions & 1 deletion src/vpm/config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Xml;
using MahApps.Metro.Controls;
using NuGet;
using PowerArgs;

Expand All @@ -18,6 +21,8 @@ public class VpmConfig
public IPackageRepository DefaultNugetRepository => _defaultNugetRepository ??
(_defaultNugetRepository = PackageRepositoryFactory.Default.CreateRepository("https://packages.nuget.org/api/v2"));

public VpmArgs Arguments { get; set; }

private string _vvvvarch;
public string VVVVArcitecture
{
Expand Down Expand Up @@ -84,6 +89,8 @@ public string VpmTempDir
}

private XmlDocument _openedpack;
public bool WaitSignal {get; set; }
public bool AgreementsAgreed { get; set; }

public XmlDocument OpenedPackXml
{
Expand All @@ -97,8 +104,37 @@ public XmlDocument OpenedPackXml
return _openedpack;
}
}
public Application WinApp { get; set; }
public Application WinApp
{
get
{
if (_winApp == null)
{
VpmConfig.Instance.WaitSignal = true;
ApplicationTask = VpmUtils.StartSTATask<bool>(() =>
{
var winapp = _winApp = new Application();
winapp.ShutdownMode = ShutdownMode.OnExplicitShutdown;
winapp.Run();
return true;
});
while (_winApp == null)
{
Thread.Sleep(10);
}
}
return _winApp;
}
//set => _winApp = value;
}

private Application _winApp;
public Task ApplicationTask { get; set; }

public Window AgreeWindow { get; set; }

public Window DirWindow { get; set; }

public List<VPack> _packlist = new List<VPack>();
public List<VPack> PackList => _packlist;
public bool InstallationCancelled = true;
Expand Down
2 changes: 2 additions & 0 deletions src/vpm/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<package id="Microsoft.CodeAnalysis.CSharp.Scripting" version="2.3.0-beta1" targetFramework="net461" />
<package id="Microsoft.CodeAnalysis.Scripting.Common" version="2.3.0-beta1" targetFramework="net461" />
<package id="Microsoft.Web.Xdt" version="2.1.1" targetFramework="net461" />
<package id="Microsoft.WindowsAPICodePack-Core" version="1.1.0.0" targetFramework="net461" />
<package id="Microsoft.WindowsAPICodePack-Shell" version="1.1.0.0" targetFramework="net461" />
<package id="Newtonsoft.Json" version="10.0.2" targetFramework="net461" />
<package id="NuGet.CommandLine" version="4.1.0" targetFramework="net461" developmentDependency="true" />
<package id="NuGet.Core" version="2.14.0" targetFramework="net461" />
Expand Down
27 changes: 27 additions & 0 deletions src/vpm/utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using LibGit2Sharp;
using LibGit2Sharp.Handlers;
Expand Down Expand Up @@ -77,6 +78,32 @@ public VersionRelation(System.Version curr, System.Version existing)

public static class VpmUtils
{
public static Task<T> StartSTATask<T>(Func<T> func)
{
var tcs = new TaskCompletionSource<T>();
Thread thread = new Thread(() =>
{
try
{
tcs.SetResult(func());
}
catch (Exception e)
{
tcs.SetException(e);
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
return tcs.Task;
}

public static void Wait()
{
while (VpmConfig.Instance.WaitSignal)
{
Thread.Sleep(10);
}
}
public static void ConsoleClearLine()
{
Console.SetCursorPosition(0, Math.Max(Console.CursorTop - 1, 0));
Expand Down
Loading

0 comments on commit 5022d0a

Please sign in to comment.