static void FetchAdditionalItemInfo(SearchItem item, Dictionary <string, object> data, FetchOptions fetchOptions, SearchContext context)
        {
            if ((fetchOptions & FetchOptions.Label) == FetchOptions.Label)
            {
                data["label"] = item.GetLabel(context);
            }

            if ((fetchOptions & FetchOptions.Description) == FetchOptions.Description)
            {
                data["description"] = item.GetDescription(context);
            }
        }
        public static GUIContent FormatDescription(SearchItem item, SearchContext context, float availableSpace, bool useColor = true)
        {
            var desc = item.GetDescription(context);

            if (String.IsNullOrEmpty(desc))
            {
                return(Styles.emptyContent);
            }
            var content = Take(desc);

            if (item.descriptionFormat == SearchItemDescriptionFormat.None || Event.current.type != EventType.Repaint)
            {
                return(content);
            }

            var truncatedDesc = desc;
            var truncated     = false;

            if (useColor)
            {
                if (item.descriptionFormat.HasFlag(SearchItemDescriptionFormat.Ellipsis))
                {
                    int maxCharLength = Utils.GetNumCharactersThatFitWithinWidth(Styles.itemDescription, truncatedDesc + "...", availableSpace);
                    if (maxCharLength < 0)
                    {
                        maxCharLength = truncatedDesc.Length;
                    }
                    truncated = desc.Length > maxCharLength;
                    if (truncated)
                    {
                        if (item.descriptionFormat.HasFlag(SearchItemDescriptionFormat.RightToLeft))
                        {
                            truncatedDesc = "..." + desc.Replace("<b>", "").Replace("</b>", "");
                            truncatedDesc = truncatedDesc.Substring(Math.Max(0, truncatedDesc.Length - maxCharLength));
                        }
                        else
                        {
                            truncatedDesc = desc.Substring(0, Math.Min(maxCharLength, desc.Length)) + "...";
                        }
                    }
                }

                if (item.descriptionFormat.HasFlag(SearchItemDescriptionFormat.Highlight))
                {
                    var parts = context.searchQuery.Split('*', ' ', '.').Where(p => p.Length > 2);
                    foreach (var p in parts)
                    {
                        truncatedDesc = Regex.Replace(truncatedDesc, Regex.Escape(p), string.Format(Styles.highlightedTextColorFormat, "$0"), RegexOptions.IgnoreCase);
                    }
                }
                else if (item.descriptionFormat.HasFlag(SearchItemDescriptionFormat.FuzzyHighlight))
                {
                    long score   = 1;
                    var  matches = new List <int>();
                    var  sq      = Utils.CleanString(context.searchQuery.ToLowerInvariant());
                    if (FuzzySearch.FuzzyMatch(sq, Utils.CleanString(truncatedDesc), ref score, matches))
                    {
                        truncatedDesc = RichTextFormatter.FormatSuggestionTitle(truncatedDesc, matches);
                    }
                }
            }

            content.text = truncatedDesc;
            if (truncated)
            {
                content.tooltip = Utils.StripHTML(desc);
            }

            return(content);
        }
Example #3
0
        private void DrawGridItem(int index, SearchItem item, Rect itemRect, bool canHover, ICollection <int> selection, Event evt)
        {
            var backgroundRect = new Rect(itemRect.x + 1, itemRect.y + 1, itemRect.width - 2, itemRect.height - 2);
            var itemContent    = canHover ? new GUIContent("", item.GetDescription(context, true)) : GUIContent.none;

            if (selection.Contains(index))
            {
                GUI.Label(backgroundRect, itemContent, Styles.selectedGridItemBackground);
            }
            else if (canHover)
            {
                GUI.Label(backgroundRect, itemContent, itemRect.Contains(evt.mousePosition) ? Styles.itemGridBackground2 : Styles.itemGridBackground1);
            }

            Texture2D thumbnail          = null;
            var       shouldFetchPreview = SearchSettings.fetchPreview && itemSize > 64;

            if (SearchSettings.fetchPreview && itemSize > 64)
            {
                thumbnail          = item.preview;
                shouldFetchPreview = !thumbnail && item.provider.fetchPreview != null;
                if (shouldFetchPreview)
                {
                    var previewSize = new Vector2(itemSize, itemSize);
                    thumbnail = item.provider.fetchPreview(item, context, previewSize, FetchPreviewOptions.Preview2D | FetchPreviewOptions.Normal);
                    if (thumbnail)
                    {
                        item.preview = thumbnail;
                    }
                }
            }

            if (!thumbnail)
            {
                thumbnail = item.thumbnail;
                if (!thumbnail && item.provider.fetchThumbnail != null)
                {
                    thumbnail = item.provider.fetchThumbnail(item, context);
                    if (thumbnail && !shouldFetchPreview)
                    {
                        item.thumbnail = thumbnail;
                    }
                }
            }

            if (thumbnail)
            {
                var thumbnailRect = new Rect(itemRect.x + itemPadding, itemRect.y + itemPadding, itemSize, itemSize);
                var dw            = thumbnailRect.width - thumbnail.width;
                var dh            = thumbnailRect.height - thumbnail.height;
                if (dw > 0 || dh > 0)
                {
                    var scaledWidth  = Mathf.Min(thumbnailRect.width, thumbnail.width);
                    var scaledHeight = Mathf.Min(thumbnailRect.height, thumbnail.height);
                    thumbnailRect = new Rect(
                        thumbnailRect.center.x - scaledWidth / 2f,
                        thumbnailRect.center.y - scaledHeight / 2f,
                        scaledWidth, scaledHeight);
                }
                GUI.DrawTexture(thumbnailRect, thumbnail, ScaleMode.ScaleToFit, true, 0f, Color.white, 0f, 4f);
            }

            var labelRect = new Rect(
                itemRect.x + itemPadding, itemRect.yMax - itemLabelHeight - itemPadding,
                itemRect.width - itemPadding * 2f, itemLabelHeight - itemPadding);
            var maxCharLength = Utils.GetNumCharactersThatFitWithinWidth(Styles.itemLabelGrid, item.GetLabel(context, true), itemRect.width * 2f);
            var itemLabel     = item.GetLabel(context);

            if (itemLabel.Length > maxCharLength)
            {
                maxCharLength = Math.Max(0, maxCharLength - 3);
                itemLabel     = Utils.StripHTML(itemLabel);
                itemLabel     = itemLabel.Substring(0, maxCharLength / 2) + "\u2026" + itemLabel.Substring(itemLabel.Length - maxCharLength / 2);
            }
            GUI.Label(labelRect, itemLabel, Styles.itemLabelGrid);
        }