Skip to content

Commit

Permalink
Enabled FileDialog IDesignable
Browse files Browse the repository at this point in the history
  • Loading branch information
tig committed Oct 28, 2024
1 parent cbb7ddc commit 262bc01
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 20 deletions.
63 changes: 44 additions & 19 deletions Terminal.Gui/Views/FileDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Terminal.Gui;
/// Modal dialog for selecting files/directories. Has auto-complete and expandable navigation pane (Recent, Root
/// drives etc).
/// </summary>
public class FileDialog : Dialog
public class FileDialog : Dialog, IDesignable
{
private const int alignmentGroupInput = 32;
private const int alignmentGroupComplete = 55;
Expand Down Expand Up @@ -78,20 +78,34 @@ internal FileDialog (IFileSystem fileSystem)
Y = Pos.AnchorEnd (),
IsDefault = true, Text = Style.OkButtonText
};
_btnOk.Accepting += (s, e) => Accept (true);
_btnOk.Accepting += (s, e) =>
{
if (e.Cancel)
{
return;
}
Accept (true);
};


_btnCancel = new Button
{
X = Pos.Align (Alignment.End, AlignmentModes.AddSpaceBetweenItems, alignmentGroupComplete),
Y = Pos.AnchorEnd(),
Y = Pos.AnchorEnd (),
Text = Strings.btnCancel
};

_btnCancel.Accepting += (s, e) =>
{
Canceled = true;
Application.RequestStop ();
if (e.Cancel)
{
return;
}
if (Modal)
{
Application.RequestStop ();
}
};

_btnUp = new Button { X = 0, Y = 1, NoPadding = true };
Expand Down Expand Up @@ -163,7 +177,7 @@ internal FileDialog (IFileSystem fileSystem)
ColumnStyle typeStyle = Style.TableStyle.GetOrCreateColumnStyle (3);
typeStyle.MinWidth = 6;
typeStyle.ColorGetter = ColorGetter;

_treeView = new TreeView<IFileSystemInfo> { Width = Dim.Fill (), Height = Dim.Fill () };

var fileDialogTreeBuilder = new FileSystemTreeBuilder ();
Expand All @@ -189,12 +203,12 @@ internal FileDialog (IFileSystem fileSystem)
bool newState = !tile.ContentView.Visible;
tile.ContentView.Visible = newState;
_btnToggleSplitterCollapse.Text = GetToggleSplitterText (newState);
SetNeedsLayout();
SetNeedsLayout ();
};

_tbFind = new TextField
{
X = Pos.Align (Alignment.Start,AlignmentModes.AddSpaceBetweenItems, alignmentGroupInput),
X = Pos.Align (Alignment.Start, AlignmentModes.AddSpaceBetweenItems, alignmentGroupInput),
CaptionColor = new Color (Color.Black),
Width = 30,
Y = Pos.Top (_btnToggleSplitterCollapse),
Expand Down Expand Up @@ -240,7 +254,7 @@ internal FileDialog (IFileSystem fileSystem)
_tableView.KeyBindings.ReplaceCommands (Key.End, Command.End);
_tableView.KeyBindings.ReplaceCommands (Key.Home.WithShift, Command.StartExtend);
_tableView.KeyBindings.ReplaceCommands (Key.End.WithShift, Command.EndExtend);

AllowsMultipleSelection = false;

UpdateNavigationVisibility ();
Expand All @@ -254,8 +268,8 @@ internal FileDialog (IFileSystem fileSystem)
Add (_tbFind);
Add (_spinnerView);

Add(_btnOk);
Add(_btnCancel);
Add (_btnOk);
Add (_btnCancel);
}

/// <summary>
Expand Down Expand Up @@ -494,8 +508,8 @@ public override void OnLoaded ()
MoveSubviewTowardsStart (_btnCancel);
}

SetNeedsDisplay();
SetNeedsLayout();
SetNeedsDraw ();
SetNeedsLayout ();
}

/// <inheritdoc/>
Expand Down Expand Up @@ -636,7 +650,7 @@ private void Accept (IFileInfo f)
if (!IsCompatibleWithOpenMode (f.FullName, out string reason))
{
_feedback = reason;
SetNeedsDisplay ();
SetNeedsDraw ();

return;
}
Expand All @@ -663,7 +677,7 @@ private void Accept (bool allowMulti)
if (reason is { })
{
_feedback = reason;
SetNeedsDisplay ();
SetNeedsDraw ();
}

return;
Expand Down Expand Up @@ -829,7 +843,11 @@ private void FinishAccept ()
}

Canceled = false;
Application.RequestStop ();

if (Modal)
{
Application.RequestStop ();
}
}

private string GetBackButtonText () { return Glyphs.LeftArrow + "-"; }
Expand Down Expand Up @@ -1117,7 +1135,7 @@ private void PushState (
_tableView.RowOffset = 0;
_tableView.SelectedRow = 0;

SetNeedsDisplay ();
SetNeedsDraw ();
UpdateNavigationVisibility ();
}
finally
Expand Down Expand Up @@ -1402,7 +1420,7 @@ out reason
if (reason is { })
{
_feedback = reason;
SetNeedsDisplay ();
SetNeedsDraw ();
}

return false;
Expand Down Expand Up @@ -1590,9 +1608,16 @@ private void UpdateChildrenToFound ()
Parent.WriteStateToTableView ();
Parent._spinnerView.Visible = true;
Parent._spinnerView.SetNeedsDisplay ();
Parent._spinnerView.SetNeedsDraw ();
}
);
}
}

bool IDesignable.EnableForDesign ()
{
Modal = false;
OnLoaded ();
return true;
}
}
2 changes: 1 addition & 1 deletion UICatalog/Scenarios/AllViewsTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ private void CreateCurrentView (Type type)

// Instantiate view
var view = (View)Activator.CreateInstance (type)!;
_eventLog!.ViewToLog = _curView;

if (view is IDesignable designable)
{
Expand Down Expand Up @@ -281,7 +282,6 @@ private void CreateCurrentView (Type type)
view.Id = "_curView";
_curView = view;

_eventLog!.ViewToLog = _curView;
_hostPane!.Add (_curView);
_layoutEditor!.ViewToEdit = _curView;
_curView.SetNeedsLayout ();
Expand Down

0 comments on commit 262bc01

Please sign in to comment.