ScriptCommandInitializer (Detailed)
FileExplorer support a lot of customizations through it's IExplorerViewModel, but because IExplorerViewMode support OpenInNewWindow (or tab), unless one don't require this feature, it's not possible to be initialized the subsequent instance of IExplorerViewModel. To solve this problem, in earlier version, I added IExplorerInitializer, which runs a number of IViewModelInitializer, which does something like (evm) => { evm.Variable1 = x; evm.Variable2 = y; }, e.g.

IExplorerInitializer initializer = new ExplorerInitializer(_windowManager, _events, RootModels.ToArray())
    Initializers = new List<IViewModelInitializer<IExplorerViewModel>>()
            new BasicParamInitalizers(_expandRootDirectories, _enableMultiSelect, _enableDrag, _enableDrop),
            new ColumnInitializers(),
            new ScriptCommandsInitializers(_windowManager, _events, _profiles),
            new ToolbarCommandsInitializers(_windowManager)
ExplorerViewModel evm = new ExplorerViewModel(_windowManager, _events) { Initializer = initializer };

These Initializers are difficult to update, what happen if I add another parameter to BasicParamInitializers? It breaks everyone's code; what if I want to initialize another thing? I have to write another Initializer to be bundled by every user.

So although IExplorerInitializer interface remains, the implement class ExplorerInitializer is replaced by ScriptCommandInitializer (inside UIScriptCommands.ExplorerCreate() IScriptCommand):

IExplorerInitializer initializer = new ScriptCommandInitializer()
    StartupParameters = pm,
    WindowManager = wm,
    Events = events,
    OnModelCreated = ScriptCommands.RunScriptCommand(OnModelCreatedKey),
    OnViewAttached = ScriptCommands.RunScriptCommand(OnViewAttachedKey)

Noticed the OnModelCreated and OnViewAttached runs a ScriptCommand in ParameterDic, in IOScriptCommands.ExplorerNewWindow(), it's defined this way:

public static IScriptCommand ExplorerNewWindow(IProfile[] profiles, IEntryModel[] rootDirectories,
    string explorerVariable = "{Explorer}", IScriptCommand nextCommand = null)
    return ScriptCommands.Assign(new Dictionary<string, object>()
            {"{Profiles}", profiles },
            {"{RootDirectories}", rootDirectories },
            {"{OnModelCreated}", IOInitializeHelpers.Explorer_Initialize_Default }, //Defined the commands.
            {"{OnViewAttached}", UIScriptCommands.ExplorerGotoStartupPathOrFirstRoot() }

        }, false,
            UIScriptCommands.ExplorerNewWindow("{OnModelCreated}", "{OnViewAttached}", "{WindowManager}",
                "{Events}", explorerVariable, nextCommand));

Which initialize the properties and go to startup path when loaded.

So at the end all you need is to call the following to open a window.
ScriptRunner.RunScriptAsync(new ParameterDic() {                   
        { "StartupPath", OpenPath }, //All of these are optional.
        { "GlobalEvents", _events },
        { "WindowManager", _windowManager },
        { "EnableDrag", _enableDrag }, 
        { "EnableDrop", _enableDrop },                     
        { "EnableMultiSelect", _enableMultiSelect}, 
    IOScriptCommands.ExplorerNewWindow(_profiles, RootModels.ToArray()));

Last edited Aug 22, 2014 at 5:54 PM by lycj, version 2


No comments yet.