98 lines
3.4 KiB
C#
98 lines
3.4 KiB
C#
using Microsoft.Win32;
|
||
using System.IO;
|
||
using System.Windows;
|
||
|
||
namespace CodeContextGenerator.Services;
|
||
|
||
public interface IUIService
|
||
{
|
||
string ShowFolderBrowserDialog(string initialDirectory = null);
|
||
bool ShowOpenProjectFileDialog(out string selectedPath);
|
||
bool ShowSaveFileDialog(string defaultFileName, string initialDirectory, out string savePath);
|
||
void ShowMessage(string message, string title, MessageBoxImage icon = MessageBoxImage.Information);
|
||
}
|
||
|
||
public class UIService : IUIService
|
||
{
|
||
public string ShowFolderBrowserDialog(string initialDirectory = null)
|
||
{
|
||
// Используем OpenFileDialog для выбора папки - это стандартный способ в WPF
|
||
var dialog = new OpenFileDialog
|
||
{
|
||
Title = "Выберите папку с проектом",
|
||
Filter = "Папки|*.folder", // Фильтр для отображения только папок
|
||
FileName = "select_folder", // Имя файла для обхода проверки существования файла
|
||
CheckFileExists = false,
|
||
CheckPathExists = true,
|
||
ValidateNames = false, // Отключаем валидацию имен для выбора папок
|
||
DereferenceLinks = true
|
||
};
|
||
|
||
if (!string.IsNullOrEmpty(initialDirectory) && Directory.Exists(initialDirectory))
|
||
{
|
||
dialog.InitialDirectory = initialDirectory;
|
||
}
|
||
|
||
var result = dialog.ShowDialog();
|
||
if (result == true)
|
||
{
|
||
// Возвращаем папку, а не файл
|
||
return Path.GetDirectoryName(dialog.FileName);
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
public bool ShowOpenProjectFileDialog(out string selectedPath)
|
||
{
|
||
selectedPath = null;
|
||
|
||
var dialog = new OpenFileDialog
|
||
{
|
||
Title = "Выберите файл решения, проекта или папку",
|
||
Filter = "Все поддерживаемые файлы (*.sln;*.csproj;*.razor)|*.sln;*.csproj;*.razor|Файлы решений (*.sln)|*.sln|Файлы проектов (*.csproj)|*.csproj|Файлы Razor (*.razor)|*.razor|Все файлы (*.*)|*.*",
|
||
Multiselect = false
|
||
};
|
||
|
||
bool? result = dialog.ShowDialog();
|
||
if (result == true)
|
||
{
|
||
selectedPath = dialog.FileName;
|
||
return true;
|
||
}
|
||
|
||
return false;
|
||
}
|
||
|
||
public bool ShowSaveFileDialog(string defaultFileName, string initialDirectory, out string savePath)
|
||
{
|
||
savePath = null;
|
||
|
||
var dialog = new SaveFileDialog
|
||
{
|
||
Title = "Сохранить контекстный файл",
|
||
Filter = "Текстовые файлы (*.txt)|*.txt|Все файлы (*.*)|*.*",
|
||
FileName = defaultFileName,
|
||
DefaultExt = ".txt"
|
||
};
|
||
|
||
if (!string.IsNullOrEmpty(initialDirectory) && Directory.Exists(initialDirectory))
|
||
{
|
||
dialog.InitialDirectory = initialDirectory;
|
||
}
|
||
|
||
bool? result = dialog.ShowDialog();
|
||
if (result == true)
|
||
{
|
||
savePath = dialog.FileName;
|
||
return true;
|
||
}
|
||
|
||
return false;
|
||
}
|
||
|
||
public void ShowMessage(string message, string title, MessageBoxImage icon = MessageBoxImage.Information)
|
||
{
|
||
MessageBox.Show(message, title, MessageBoxButton.OK, icon);
|
||
}
|
||
} |