using System.Globalization;
using System.IO.Abstractions;
using Terminal.Gui.Resources;
using static System.Environment;
namespace Terminal.Gui;
/// Stores style settings for .
public class FileDialogStyle
{
private readonly IFileSystem _fileSystem;
/// Creates a new instance of the class.
public FileDialogStyle (IFileSystem fileSystem)
{
_fileSystem = fileSystem;
TreeRootGetter = DefaultTreeRootGetter;
DateFormat = CultureInfo.CurrentCulture.DateTimeFormat.SortableDateTimePattern;
}
/// Gets or sets the text on the 'Cancel' button.
public string CancelButtonText { get; set; } = Strings.btnCancel;
///
/// Gets or sets the class thatis responsible for determining which color to use to represent files and
/// directories when is .
///
public FileSystemColorProvider ColorProvider { get; set; } = new ();
///
/// Gets or sets the culture to use (e.g. for number formatting). Defaults to
/// .
///
public CultureInfo Culture { get; set; } = CultureInfo.CurrentUICulture;
///
/// Gets or sets the format to use for date/times in the Modified column. Defaults to
/// of the
///
public string DateFormat { get; set; }
///
/// Gets or sets the default value to use for . This can be populated from .tui config
/// files via
///
[SerializableConfigurationProperty (Scope = typeof (SettingsScope))]
public static bool DefaultUseColors { get; set; }
///
/// Gets or sets the default value to use for . This can be populated from .tui
/// config files via
///
[SerializableConfigurationProperty (Scope = typeof (SettingsScope))]
public static bool DefaultUseUnicodeCharacters { get; set; }
///
/// Gets or sets error message when user is and user enters the
/// name of an existing directory (File system cannot have a folder with the same name as a file).
///
public string DirectoryAlreadyExistsFeedback { get; set; } = Strings.fdDirectoryAlreadyExistsFeedback;
///
/// Gets or sets error message when user selects a directory that does not exist and is
/// and is .
///
public string DirectoryMustExistFeedback { get; set; } = Strings.fdDirectoryMustExistFeedback;
///
/// Gets or sets error message when user is and user
/// enters the name of an existing file (File system cannot have a folder with the same name as a file).
///
public string FileAlreadyExistsFeedback { get; set; } = Strings.fdFileAlreadyExistsFeedback;
///
/// Gets or sets error message when user selects a file that does not exist and is
/// and is .
///
public string FileMustExistFeedback { get; set; } = Strings.fdFileMustExistFeedback;
/// Gets or sets the header text displayed in the Filename column of the files table.
public string FilenameColumnName { get; set; } = Strings.fdFilename;
///
/// Gets or sets error message when user selects a file/dir that does not exist and is
/// and is .
///
public string FileOrDirectoryMustExistFeedback { get; set; } = Strings.fdFileOrDirectoryMustExistFeedback;
///
/// Gets or sets whether to flip the order of the Ok and Cancel buttons. Defaults to false (Ok button then Cancel
/// button). Set to true to show Cancel button on left then Ok button instead.
///
public bool FlipOkCancelButtonLayoutOrder { get; set; }
/// Gets or sets the class responsible for determining which symbol to use to represent files and directories.
public FileSystemIconProvider IconProvider { get; set; } = new ();
/// Gets or sets the header text displayed in the Modified column of the files table.
public string ModifiedColumnName { get; set; } = Strings.fdModified;
/// Gets or sets the text on the 'Ok' button. Typically you may want to change this to "Open" or "Save" etc.
public string OkButtonText { get; set; } = Strings.btnOk;
/// Gets or sets the text displayed in the 'Path' text box when user has not supplied any input yet.
public string PathCaption { get; set; } = Strings.fdPathCaption;
/// Gets or sets the text displayed in the 'Search' text box when user has not supplied any input yet.
public string SearchCaption { get; set; } = Strings.fdSearchCaption;
/// Gets or sets the header text displayed in the Size column of the files table.
public string SizeColumnName { get; set; } = Strings.fdSize;
/// Gets the style settings for the table of files (in currently selected directory).
public TableStyle TableStyle { get; internal set; }
///
/// Gets or Sets the method for getting the root tree objects that are displayed in the collapse-able tree in the
/// . Defaults to all accessible and unique
/// .
///
/// Must be configured before showing the dialog.
public Func> TreeRootGetter { get; set; }
/// Gets the style settings for the collapse-able directory/places tree
public TreeStyle TreeStyle { get; internal set; }
/// Gets or sets the header text displayed in the Type column of the files table.
public string TypeColumnName { get; set; } = Strings.fdType;
///
/// Gets or Sets a value indicating whether different colors should be used for different file types/directories.
/// Defaults to false.
///
public bool UseColors { get; set; } = DefaultUseColors;
/// Gets or sets whether to use advanced unicode characters which might not be installed on all users computers.
public bool UseUnicodeCharacters { get; set; } = DefaultUseUnicodeCharacters;
///
/// Gets or sets error message when user attempts to select a file type that is not one of
///
///
public string WrongFileTypeFeedback { get; set; } = Strings.fdWrongFileTypeFeedback;
private Dictionary DefaultTreeRootGetter ()
{
Dictionary roots = new ();
try
{
foreach (string d in GetLogicalDrives ())
{
IDirectoryInfo dir = _fileSystem.DirectoryInfo.New (d);
if (!roots.ContainsKey (dir))
{
roots.Add (dir, d);
}
}
}
catch (Exception)
{
// Cannot get the system disks thats fine
}
try
{
foreach (SpecialFolder special in Enum.GetValues (typeof (SpecialFolder)).Cast ())
{
try
{
string path = GetFolderPath (special);
if (string.IsNullOrWhiteSpace (path))
{
continue;
}
IDirectoryInfo dir = _fileSystem.DirectoryInfo.New (path);
if (!roots.ContainsKey (dir) && dir.Exists)
{
roots.Add (dir, special.ToString ());
}
}
catch (Exception)
{
// Special file exists but contents are unreadable (permissions?)
// skip it anyway
}
}
}
catch (Exception)
{
// Cannot get the special files for this OS oh well
}
return roots;
}
}