public override void NotifySelectionChanged(IEntryModel[] appliedModels)
        {
            base.NotifySelectionChanged(appliedModels);

            _cts.Cancel();
            _cts      = new CancellationTokenSource();
            IsEnabled = appliedModels.Count() == 1 &&
                        appliedModels[0] is FileSystemInfoExModel;
            if (IsEnabled)
            {
                Header = appliedModels[0].IsDirectory ? "Explore" : "Open";
                var appliedModel = appliedModels[0];
                HeaderIconExtractor = ModelIconExtractor <ICommandModel>
                                      .FromTaskFunc(t =>
                                                    GetFromSystemImageList.Instance.GetIconBytesForModelAsync(appliedModel,
                                                                                                              CancellationToken.None));

                Task.Run(() =>
                {
                    List <ICommandModel> subCommands = new List <ICommandModel>();
                    if (appliedModel is FileSystemInfoExModel)
                    {
                        subCommands.AddRange(GetCommands(appliedModel as FileSystemInfoExModel));
                    }
                    return(subCommands);
                }).ContinueWith((prevTsk) =>
                                { SubCommands = (prevTsk as Task <List <ICommandModel> >).Result; }, _cts.Token, TaskContinuationOptions.None,
                                TaskScheduler.FromCurrentSynchronizationContext());
            }
        }
        public static IModelIconExtractor <T> FromFuncCachable(
            Func <T, string> keyFunc, Func <T, byte[]> func)
        {
            return(ModelIconExtractor <T>
                   .FromFunc(
                       t =>
            {
                string key = keyFunc(t);
                if (key == null)
                {
                    return func(t);
                }

                if (!_cacheDic.ContainsKey(key))
                {
                    byte[] val = func(t);
                    if (val != null && val.Length > 0)
                    {
                        _cacheDic[key] = val;
                    }
                    return val;
                }
                return _cacheDic[key] ?? new byte[] {};
            }
                       ));
        }
 public static IModelIconExtractor <IEntryModel> Create(IModelIconExtractor <IEntryModel> baseExtractor,
                                                        Func <IEntryModel, string> keyFunc,
                                                        Func <IEntryModel, string> text2DrawFunc, System.Drawing.Color color)
 {
     return(ModelIconExtractor <IEntryModel> .FromTaskFuncCachable(
                keyFunc,
                em => getIcon(baseExtractor, em, text2DrawFunc, color)
                ));
 }
        public static IModelIconExtractor <IEntryModel> Create(Func <IEntryModel, string> fnameFunc = null)
        {
            fnameFunc = fnameFunc == null ? e => e.Label : fnameFunc;
            Func <IEntryModel, string> keyFunc = (m) =>
            {
                if (m.IsDirectory)
                {
                    return("GetFromSystemImageListUsingExtension - Directory");
                }
                else
                {
                    string fname     = fnameFunc(m);
                    string extension = m.Profile.Path.GetExtension(fname).ToLower();

                    if (String.IsNullOrEmpty(extension))
                    {
                        //Without extension.
                        return("GetFromSystemImageListUsingExtension - File");
                    }
                    else
                    {
                        return("GetFromSystemImageListUsingExtension - " + extension);
                    }
                }
            };
            Func <IEntryModel, byte[]> getIconFunc = em =>
            {
                if (em.IsDirectory)
                {
                    return(ResourceUtils.GetResourceAsByteArray(dummy, "/Themes/Resources/FolderIcon.png"));
                }

                if (em != null)
                {
                    string fname = fnameFunc(em);
                    using (Bitmap bitmap =
                               _iconExtractor.GetBitmap(IconSize.large, fname, em.IsDirectory, false))
                        if (bitmap != null)
                        {
                            return(bitmap.ToByteArray());
                        }
                }
                return(null);
            };

            return(ModelIconExtractor <IEntryModel> .FromFuncCachable(
                       keyFunc,
                       (em) => getIconFunc(em)
                       ));
        }
        public IEnumerable <ICommandModel> GetCommands(FileSystemInfoExModel appliedModel)
        {
            if (!appliedModel.IsDirectory)
            {
                string ext = PathEx.GetExtension(appliedModel.Name);
                foreach (OpenWithInfo info in FileTypeInfoProvider.GetFileTypeInfo(ext).OpenWithList)
                {
                    if (info.OpenCommand != null)
                    {
                        string executePath = OpenWithInfo.GetExecutablePath(info.OpenCommand);
                        string exeName     = Path.GetFileNameWithoutExtension(executePath);

                        if (info.OpenCommand != null && File.Exists(executePath))
                        {
                            IEntryModel exeModel = AsyncUtils.RunSync(() => _profile.ParseAsync(executePath));
                            if (exeModel != null)
                            {
                                yield return new CommandModel(new OpenWithScriptCommand(info))
                                       {
                                           Header              = String.Format("{0} ({1})", exeName, info.KeyName),
                                           ToolTip             = info.Description,
                                           HeaderIconExtractor =
                                               ModelIconExtractor <ICommandModel>
                                               .FromTaskFunc(t =>
                                                             _profile.GetIconExtractSequence(exeModel)
                                                             .Last().GetIconBytesForModelAsync(exeModel,
                                                                                               CancellationToken.None)),
                                           IsEnabled = true
                                       }
                            }
                            ;
                        }
                    }
                }

                yield return(new CommandModel(new OpenWithScriptCommand(OpenWithInfo.OpenAs))
                {
                    Header = "Open with...",
                    IsEnabled = true
                });
            }
        }
    }
        public static IModelIconExtractor <T> FromTaskFuncCachable(string key, Func <Task <byte[]> > task)
        {
            return(ModelIconExtractor <T>
                   .FromTaskFunc(
                       async() =>
            {
                if (key == null)
                {
                    return await task();
                }

                if (!_cacheDic.ContainsKey(key))
                {
                    return _cacheDic[key] = await task();
                }
                return _cacheDic[key] ?? new byte[] {};
            }
                       ));
        }
示例#7
0
        public static IModelIconExtractor <IEntryModel> Create()
        {
            Func <IEntryModel, string> keyFunc = (m) =>
            {
                if (m.IsDirectory)
                {
                    //if (model.FullPath.StartsWith("::"))
                    return("GetFromSystemImageList - " + m.FullPath);
                }
                //else return "GetFromSystemImageList - Directory";
                else
                {
                    string extension = m.GetExtension().ToLower();

                    if (String.IsNullOrEmpty(extension))
                    {
                        //Without extension.
                        if (m.FullPath.StartsWith("::"))
                        {
                            return("GetFromSystemImageList - " + m.FullPath);
                        }
                        else
                        {
                            return("GetFromSystemImageList - File");
                        }
                    }
                    else
                    {
                        if (excludedExtensions.Contains(extension))
                        {
                            return("GetFromSystemImageList - " + m.FullPath);
                        }
                        return("GetFromSystemImageList - " + extension);
                    }
                }
            };
            Func <IEntryModel, byte[]> getIconFunc = em =>
            {
                if (em != null && !String.IsNullOrEmpty(em.FullPath))
                {
                    using (FileSystemInfoEx fsi = FileSystemInfoEx.FromString(em.FullPath))
                    {
                        Bitmap bitmap = null;
                        if (fsi != null && fsi.Exists)
                        {
                            return(fsi.RequestPIDL(pidl =>
                            {
                                if (pidl != null)
                                {
                                    bitmap = _iconExtractor.GetBitmap(QuickZip.Converters.IconSize.extraLarge,
                                                                      pidl.Ptr, em.IsDirectory, false);

                                    if (bitmap != null)
                                    {
                                        return
                                        bitmap.ToByteArray();
                                    }
                                }
                                return new byte[] {};
                            }));
                        }
                    }
                }
                return(null);
            };

            return(ModelIconExtractor <IEntryModel> .FromFuncCachable(
                       keyFunc,
                       (em) => getIconFunc(em)
                       ));
        }