Skip to content

Module panels

Roman Kuzmin edited this page Dec 28, 2024 · 1 revision

Module panels tips and tricks


How to set panel title

Constructor or default title

If possible, simple, and cheap, then set the title in the panel constructor or override Panel.DefaultTitle.

class MyPanel : Panel
{
    public MyPanel(MyExplorer explorer) : base(explorer)
    {
        Title = ...
class MyPanel : Panel
{
    protected override string DefaultTitle => ...

When explorer enters panel

When explorers of the same type reuse the same panel on module file system navigation, use Explorer.EnterPanel in order to update the panel title to the new just connected explorer.

class MyExplorer : Explorer
{
    public override void EnterPanel(Panel panel)
    {
        panel.Title = ...

When explorer gets files

Explorer.GetFiles is the suitable place for setting not trivial panel titles.

Making a title may need some resources like temporary opened data sources. Such resources are very likely available on getting files.

A title may reflect panel files aggregated data. Updating the title in the end of getting files with collected results looks natural.

class MyExplorer : Explorer
{
    public override IEnumerable<FarFile> GetFiles(GetFilesEventArgs args)
    {
        // get files and collect some aggregates
        ...

        // set the title using collected results
        if (args.Panel is { } panel)
            panel.Title = ...

If a title is expensive and the same for varying files then it makes sense to make and set it once. On getting files, set the title only when it is null.

class MyExplorer : Explorer
{
    public override IEnumerable<FarFile> GetFiles(GetFilesEventArgs args)
    {
        // make and set the title once
        if (args.Panel is { } panel && panel.Title is null)
            panel.Title = ...

Alternative delete files

Pressing F8 or Del in module panels is supposed to delete files. This calls Panel.UIDeleteFiles and Explorer.DeleteFiles with DeleteFilesEventArgs.

Pressing ShiftF8 or ShiftDel also calls the above methods with one difference, the flag DeleteFilesEventArgs.Force is set to true.

How to process this alternative action is up to the module. The action may be the same as delete. The action may be different (delete permanently vs moving to a bin) but still about removing files from the panel. And the alternative action may be not about removing files at all.

Implement the last case properly in order not to confuse the core. The core expects that selected files are usually removed.

Example

FarNet.PowerShellFar list panels are used in order to show object properties (MemberPanel and PropertyPanel are derived from ListPanel). The alternative delete is used to set nulls to properties, kind of deleting values, not deleting properties.

ListPanel overrides UIDeleteFiles like this

    public override void UIDeleteFiles(DeleteFilesEventArgs args)
    {
        if (args.Force)
        {
            SetNulls(args.Files);
            args.Result = JobResult.Ignore;
        }
        else
        {
            base.UIDeleteFiles(args);
        }
    }

If args.Force is true (alternative delete) then a separate method SetNulls is called. Note that we also tell the core to ignore results and do nothing. SetNulls is doing something different than removing files.

If args.Force is false (normal delete) the we simply call the base method for deleting files normally.