Drag Drop Form
This commit is contained in:
150
Drag-Drop/Form.cs
Normal file
150
Drag-Drop/Form.cs
Normal file
@ -0,0 +1,150 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Phares.Shared;
|
||||
using Serilog;
|
||||
using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
using View_by_Distance.Drag_Drop.Models;
|
||||
using View_by_Distance.Resize.Models;
|
||||
using View_by_Distance.Shared.Models;
|
||||
using View_by_Distance.Shared.Models.Stateless.Methods;
|
||||
|
||||
namespace View_by_Distance.Drag_Drop;
|
||||
|
||||
public partial class Form : System.Windows.Forms.Form
|
||||
{
|
||||
|
||||
private readonly ILogger _Logger;
|
||||
private readonly TextBox _TextBox;
|
||||
private readonly AppSettings _AppSettings;
|
||||
private readonly string _WorkingDirectory;
|
||||
private readonly Configuration _Configuration;
|
||||
private readonly IsEnvironment _IsEnvironment;
|
||||
private readonly string _ResizeFileNameExtension;
|
||||
private readonly IConfigurationRoot _ConfigurationRoot;
|
||||
private readonly Property.Models.Configuration _PropertyConfiguration;
|
||||
private readonly Dictionary<int, MappingFromItem> _IdToMappingFromItem;
|
||||
|
||||
public Form()
|
||||
{
|
||||
InitializeComponent();
|
||||
ILogger logger;
|
||||
AppSettings appSettings;
|
||||
string workingDirectory;
|
||||
Configuration configuration;
|
||||
IsEnvironment isEnvironment;
|
||||
_IdToMappingFromItem = new();
|
||||
IConfigurationRoot configurationRoot;
|
||||
LoggerConfiguration loggerConfiguration = new();
|
||||
Property.Models.Configuration propertyConfiguration;
|
||||
Assembly assembly = Assembly.GetExecutingAssembly();
|
||||
bool debuggerWasAttachedAtLineZero = Debugger.IsAttached || assembly.Location.Contains(@"\bin\Debug");
|
||||
isEnvironment = new(processesCount: null, nullASPNetCoreEnvironmentIsDevelopment: debuggerWasAttachedAtLineZero, nullASPNetCoreEnvironmentIsProduction: !debuggerWasAttachedAtLineZero);
|
||||
IConfigurationBuilder configurationBuilder = new ConfigurationBuilder()
|
||||
.AddEnvironmentVariables()
|
||||
.AddJsonFile(isEnvironment.AppSettingsFileName);
|
||||
configurationRoot = configurationBuilder.Build();
|
||||
appSettings = Models.Binder.AppSettings.Get(configurationRoot);
|
||||
workingDirectory = IWorkingDirectory.GetWorkingDirectory(assembly.GetName().Name, appSettings.WorkingDirectoryName);
|
||||
Environment.SetEnvironmentVariable(nameof(workingDirectory), workingDirectory);
|
||||
_ = ConfigurationLoggerConfigurationExtensions.Configuration(loggerConfiguration.ReadFrom, configurationRoot);
|
||||
Log.Logger = loggerConfiguration.CreateLogger();
|
||||
logger = Log.ForContext<Form>();
|
||||
propertyConfiguration = Property.Models.Binder.Configuration.Get(isEnvironment, configurationRoot);
|
||||
configuration = Models.Binder.Configuration.Get(isEnvironment, configurationRoot, propertyConfiguration);
|
||||
(_, _, string resizeFileNameExtension) = C_Resize.GetTuple(configuration.OutputExtension, configuration.OutputQuality);
|
||||
logger.Information("Complete");
|
||||
_Logger = logger;
|
||||
_AppSettings = appSettings;
|
||||
_Configuration = configuration;
|
||||
_IsEnvironment = isEnvironment;
|
||||
_WorkingDirectory = workingDirectory;
|
||||
_ConfigurationRoot = configurationRoot;
|
||||
_PropertyConfiguration = propertyConfiguration;
|
||||
_ResizeFileNameExtension = resizeFileNameExtension;
|
||||
_TextBox = new() { Location = new(5, 5), Dock = DockStyle.Top };
|
||||
Load += new EventHandler(Form1_Load);
|
||||
Controls.Add(_TextBox);
|
||||
}
|
||||
|
||||
void Form1_Load(object? sender, EventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
int j;
|
||||
int f;
|
||||
int t;
|
||||
AllowDrop = true;
|
||||
bool firstRun = false;
|
||||
Container[] containers;
|
||||
DragDrop += new DragEventHandler(Form1_DragDrop);
|
||||
DragEnter += new DragEventHandler(Form1_DragEnter);
|
||||
Property.Models.A_Property propertyLogic = new(_AppSettings.MaxDegreeOfParallelism, _Configuration.PropertyConfiguration, _ResizeFileNameExtension, _Configuration.Reverse);
|
||||
(j, f, t, containers) = Property.Models.Stateless.Container.GetContainers(_Configuration.PropertyConfiguration, firstRun, propertyLogic);
|
||||
List<MappingFromItem> collection = Program.GetCollection(_Configuration, containers);
|
||||
foreach (MappingFromItem mappingFromItem in collection)
|
||||
{
|
||||
if (_IdToMappingFromItem.ContainsKey(mappingFromItem.Id))
|
||||
continue;
|
||||
_IdToMappingFromItem.Add(mappingFromItem.Id, mappingFromItem);
|
||||
}
|
||||
if (_Logger is null)
|
||||
throw new NullReferenceException(nameof(_Logger));
|
||||
_Logger.Debug((_AppSettings is null).ToString());
|
||||
_Logger.Debug((_Configuration is null).ToString());
|
||||
_Logger.Debug((_IsEnvironment is null).ToString());
|
||||
_Logger.Debug((_WorkingDirectory is null).ToString());
|
||||
_Logger.Debug((_ConfigurationRoot is null).ToString());
|
||||
_Logger.Debug((_PropertyConfiguration is null).ToString());
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
void Form1_DragEnter(object? sender, DragEventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (e.Data is not null && e.Data.GetDataPresent(DataFormats.FileDrop))
|
||||
e.Effect = DragDropEffects.Copy;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
void Form1_DragDrop(object? sender, DragEventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (e.Data is not null)
|
||||
{
|
||||
string fileName;
|
||||
string[] segments;
|
||||
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
|
||||
foreach (string file in files)
|
||||
{
|
||||
fileName = Path.GetFileNameWithoutExtension(file);
|
||||
Text = fileName;
|
||||
segments = fileName.Split('.');
|
||||
if (int.TryParse(segments[0], out int id) && _IdToMappingFromItem.ContainsKey(id))
|
||||
{
|
||||
MappingFromItem mappingFromItem = _IdToMappingFromItem[id];
|
||||
Text = mappingFromItem.ImageFileHolder.Name;
|
||||
_TextBox.Text = mappingFromItem.ImageFileHolder.FullName;
|
||||
_Logger.Information(mappingFromItem.ImageFileHolder.DirectoryName);
|
||||
if (!string.IsNullOrEmpty(mappingFromItem.ImageFileHolder.DirectoryName))
|
||||
_ = Process.Start("explorer.exe", string.Concat("\"", mappingFromItem.ImageFileHolder.DirectoryName, "\""));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user