Move IconGetter to Style and provide default implementation

- Depends on `UseUnicodeCharacters`
- Also updated up/back/collapse/expand tree to use spicier icons
This commit is contained in:
tznind
2023-04-02 20:07:23 +01:00
parent c2db95461e
commit 20f773d5df
4 changed files with 38 additions and 52 deletions

View File

@@ -101,10 +101,8 @@ namespace Terminal.Gui {
return;
}
// TODO: Not Cyan!
// draw it like its selected even though its not
Application.Driver.SetAttribute (new Attribute (Color.Cyan, textField.ColorScheme.Focus.Background));
Application.Driver.SetAttribute (new Attribute (ColorScheme.Normal.Foreground, textField.ColorScheme.Focus.Background));
textField.Move (textField.Text.Length, 0);
var suggestion = this.Suggestions.ElementAt (this.SelectedIdx);

View File

@@ -32,12 +32,6 @@ namespace Terminal.Gui.FileServices {
/// </summary>
public ColorScheme ColorSchemeDirectory { get; set; }
/// <summary>
/// Sets a <see cref="ColorScheme"/> to use for regular file rows of
/// the <see cref="TableView"/>. Defaults to White text on Black background.
/// </summary>
public ColorScheme ColorSchemeDefault { get; set; }
/// <summary>
/// Sets a <see cref="ColorScheme"/> to use for file rows with an image extension
/// of the <see cref="TableView"/>. Defaults to White text on Black background.
@@ -140,12 +134,27 @@ namespace Terminal.Gui.FileServices {
/// </summary>
/// <remarks>Must be configured before showing the dialog.</remarks>
public FileDialogTreeRootGetter TreeRootGetter { get; set; } = DefaultTreeRootGetter;
/// <summary>
/// Gets or sets whether to use advanced unicode characters which might not be installed
/// on all users computers.
/// </summary>
public bool UseUnicodeCharacters { get; set; } = false;
/// <summary>
/// User defined delegate for picking which character(s)/unicode
/// symbol(s) to use as an 'icon' for files/folders.
/// </summary>
public Func<FileSystemInfo, string> IconGetter { get; set; }
/// <summary>
/// Creates a new instance of the <see cref="FileDialogStyle"/> class.
/// </summary>
public FileDialogStyle ()
{
IconGetter = DefaultIconGetter;
ColorSchemeDirectory = new ColorScheme {
Normal = Application.Driver.MakeAttribute (Color.Blue, Color.Black),
@@ -155,12 +164,6 @@ namespace Terminal.Gui.FileServices {
};
ColorSchemeDefault = new ColorScheme {
Normal = Application.Driver.MakeAttribute (Color.White, Color.Black),
HotNormal = Application.Driver.MakeAttribute (Color.White, Color.Black),
Focus = Application.Driver.MakeAttribute (Color.Black, Color.White),
HotFocus = Application.Driver.MakeAttribute (Color.Black, Color.White),
};
ColorSchemeImage = new ColorScheme {
Normal = Application.Driver.MakeAttribute (Color.Magenta, Color.Black),
HotNormal = Application.Driver.MakeAttribute (Color.Magenta, Color.Black),
@@ -176,6 +179,16 @@ namespace Terminal.Gui.FileServices {
}
private string DefaultIconGetter (FileSystemInfo arg)
{
if (arg is DirectoryInfo) {
return UseUnicodeCharacters ? "\ua909 " : "\\";
}
return UseUnicodeCharacters ? "\u2630 " : "";
}
private static IEnumerable<FileDialogRootTreeNode> DefaultTreeRootGetter ()
{
var roots = new List<FileDialogRootTreeNode> ();

View File

@@ -168,7 +168,7 @@ namespace Terminal.Gui {
this.btnUp = new Button ()
{ X = 0, Y = 1, NoPadding=true };
btnUp.Text = "◭";
btnUp.Text = Style.UseUnicodeCharacters ? "◭" : "▲";
this.btnUp.Clicked += (s, e) => this.history.Up ();
this.btnBack = new Button ()
@@ -253,7 +253,7 @@ namespace Terminal.Gui {
this.splitContainer.Tiles.ElementAt (0).ContentView.Add (this.treeView);
this.splitContainer.Tiles.ElementAt (1).ContentView.Add (this.tableView);
this.btnToggleSplitterCollapse = new Button (">>") {
this.btnToggleSplitterCollapse = new Button ("▶▶") {
Y = Pos.AnchorEnd (1),
};
this.btnToggleSplitterCollapse.Clicked += (s, e) => {
@@ -261,7 +261,7 @@ namespace Terminal.Gui {
var newState = !tile.ContentView.Visible;
tile.ContentView.Visible = newState;
this.btnToggleSplitterCollapse.Text = newState ? "<<" : ">>";
this.btnToggleSplitterCollapse.Text = newState ? "◀◀" : "▶▶";
};
@@ -302,15 +302,12 @@ namespace Terminal.Gui {
this.tableView.CellActivated += this.CellActivate;
this.tableView.KeyUp += (s, k) => k.Handled = this.TableView_KeyUp (k.KeyEvent);
this.tableView.SelectedCellChanged += this.TableView_SelectedCellChanged;
this.tableView.ColorScheme = Style.ColorSchemeDefault;
this.tableView.AddKeyBinding (Key.Home, Command.TopHome);
this.tableView.AddKeyBinding (Key.End, Command.BottomEnd);
this.tableView.AddKeyBinding (Key.Home | Key.ShiftMask, Command.TopHomeExtend);
this.tableView.AddKeyBinding (Key.End | Key.ShiftMask, Command.BottomEndExtend);
this.treeView.ColorScheme = Style.ColorSchemeDefault;
this.treeView.KeyDown += (s, k) => {
@@ -519,13 +516,6 @@ namespace Terminal.Gui {
}
}
/// <summary>
/// User defined delegate for picking which character(s)/unicode
/// symbol(s) to use as an 'icon' for files/folders. Defaults to
/// null (i.e. no icons).
/// </summary>
public Func<FileSystemInfo, string> IconGetter { get; set; } = null;
/// <summary>
/// Defines how the dialog matches files/folders when using the search
/// box. Provide a custom implementation if you want to tailor how matching
@@ -645,6 +635,8 @@ namespace Terminal.Gui {
// May have been updated after instance was constructed
this.btnOk.Text = Style.OkButtonText;
tbPath.Autocomplete.ColorScheme.Normal = Attribute.Make (Color.Gray, tbPath.ColorScheme.Normal.Background);
treeView.AddObjects (Style.TreeRootGetter ());
// if filtering on file type is configured then create the ComboBox and establish
@@ -945,7 +937,7 @@ namespace Terminal.Gui {
return string.Empty;
}
var icon = stats.IsParent ? null : IconGetter?.Invoke (stats.FileSystemInfo);
var icon = stats.IsParent ? null : Style.IconGetter?.Invoke (stats.FileSystemInfo);
if (icon != null) {
return icon + stats.Name;
@@ -1177,7 +1169,7 @@ namespace Terminal.Gui {
var stats = this.RowToStats (args.RowIndex);
if (!Style.UseColors) {
return Style.ColorSchemeDefault;
return tableView.ColorScheme;
}
if (stats.IsDir ()) {
@@ -1193,7 +1185,7 @@ namespace Terminal.Gui {
return Style.ColorSchemeExeOrRecommended;
}
return Style.ColorSchemeDefault;
return tableView.ColorScheme;
}
/// <summary>

View File

@@ -14,7 +14,7 @@ namespace UICatalog.Scenarios {
[ScenarioCategory ("Files and IO")]
public class FileDialogExamples : Scenario {
private CheckBox cbMustExist;
private CheckBox cbIcons;
private CheckBox cbUnicode;
private CheckBox cbUseColors;
private CheckBox cbCaseSensitive;
private CheckBox cbAllowMultipleSelection;
@@ -35,8 +35,8 @@ namespace UICatalog.Scenarios {
Win.Add (cbMustExist);
cbIcons = new CheckBox ("Icons") { Checked = true, Y = y++, X = x };
Win.Add (cbIcons);
cbUnicode = new CheckBox ("UseUnicode") { Checked = true, Y = y++, X = x };
Win.Add (cbUnicode);
cbUseColors = new CheckBox ("Use Colors") { Checked = false, Y = y++, X = x };
Win.Add (cbUseColors);
@@ -139,9 +139,7 @@ namespace UICatalog.Scenarios {
fd.FilesSelected += ConfirmOverwrite;
}
if (cbIcons.Checked ?? false) {
fd.IconGetter = GetIcon;
}
fd.Style.UseUnicodeCharacters = cbUnicode.Checked ?? false;
if (cbCaseSensitive.Checked ?? false) {
@@ -213,20 +211,5 @@ namespace UICatalog.Scenarios {
return f.Name.Contains (terms, StringComparison.CurrentCulture);
}
}
private string GetIcon (FileSystemInfo arg)
{
// Typically most windows terminals will not have these unicode characters installed
// so for the demo lets not bother having any icons on windows
if (RuntimeInformation.IsOSPlatform (OSPlatform.Windows)) {
return arg is DirectoryInfo ? "\\" : null;
}
if (arg is DirectoryInfo) {
return "\ua909 ";
}
return "\u2630 ";
}
}
}