Exemple #1
0
        private static SettingsProvider CreateSearchSettings()
        {
            var settings = new SettingsProvider(settingsPreferencesKey, SettingsScope.User)
            {
                keywords   = new[] { "quick", "omni", "search" },
                guiHandler = searchContext =>
                {
                    EditorGUIUtility.labelWidth = 500;
                    GUILayout.BeginHorizontal();
                    {
                        GUILayout.Space(10);
                        GUILayout.BeginVertical();
                        {
                            GUILayout.Space(10);
                            EditorGUI.BeginChangeCheck();
                            {
                                trackSelection    = EditorGUILayout.Toggle(Styles.trackSelectionContent, trackSelection);
                                fetchPreview      = EditorGUILayout.Toggle(Styles.fetchPreviewContent, fetchPreview);
                                useDockableWindow = EditorGUILayout.Toggle(Styles.useDockableWindowContent, useDockableWindow);
                                if (useDockableWindow)
                                {
                                    closeWindowByDefault = EditorGUILayout.Toggle(Styles.closeWindowByDefaultContent, closeWindowByDefault);
                                }
                                GUILayout.Space(10);
                                DrawProviderSettings();
                            }
                            if (EditorGUI.EndChangeCheck())
                            {
                                Save();
                                SearchService.Refresh();
                            }
                        }
                        GUILayout.EndVertical();
                    }
                    GUILayout.EndHorizontal();
                }
            };

            return(settings);
        }
        public virtual IEnumerable <string> Search(string query, UnityEditor.SearchService.ISearchContext context, Action <IEnumerable <string> > asyncItemsReceived)
        {
            if (asyncItemsReceived != null)
            {
                m_OnAsyncItemReceived = asyncItemsReceived;
            }

            if (context.requiredTypeNames != null && context.requiredTypeNames.Any())
            {
                this.context.wantsMore  = true;
                this.context.filterType = Utils.GetTypeFromName(context.requiredTypeNames.First());
            }
            else
            {
                this.context.wantsMore  = false;
                this.context.filterType = null;
            }
            this.context.searchText = query;
            var items = SearchService.GetItems(this.context);

            return(items.Select(item => item.id));
        }
Exemple #3
0
        public void FetchItems()
        {
            var ctx = new SearchContext {
                searchText = "test", wantsMore = true
            };

            Assert.AreEqual(0, ctx.searchId);

            var fetchedItems = SearchService.GetItems(ctx);

            Assert.AreEqual(1, ctx.searchId);
            Assert.IsNotEmpty(fetchedItems);
            var foundItem = fetchedItems.Find(item => item.label == Path.GetFileName(k_TestFileName));

            Assert.IsNotNull(foundItem.id);
            Assert.IsNull(foundItem.description);

            Assert.IsNotNull(foundItem.provider);
            Assert.IsNotNull(foundItem.provider.fetchDescription);
            var fetchedDescription = foundItem.provider.fetchDescription(foundItem, ctx);

            Assert.AreEqual("Packages/com.unity.quicksearch/Tests/Editor/Content/test_material_42.mat (2.0 KB)", fetchedDescription);
        }
Exemple #4
0
        public override void BeginSearch(string query, UnityEditor.SearchService.ISearchContext context)
        {
            base.BeginSearch(query, context);
            var searchContext = new SearchContext {
                searchText = query
            };

            searchContext.wantsMore = true;
            if (context.requiredTypeNames != null && context.requiredTypeNames.Any())
            {
                searchContext.filterType = Utils.GetTypeFromName(context.requiredTypeNames.First());
            }
            else
            {
                searchContext.filterType = typeof(GameObject);
            }

            m_SearchItems = new HashSet <int>();
            foreach (var id in SearchService.GetItems(searchContext, provider).Select(item => Convert.ToInt32(item.id)))
            {
                m_SearchItems.Add(id);
            }
        }
Exemple #5
0
 public void EnableService()
 {
     SearchService.Enable(SearchContext.Empty);
     SearchService.Filter.ResetFilter(true);
     SearchService.Providers.First(p => p.name.id == "packages").active = false;
 }
Exemple #6
0
        private void HandleKeyboardNavigation(SearchContext context)
        {
            // TODO: support page down and page up

            var evt = Event.current;

            if (evt.type == EventType.KeyDown)
            {
                var prev = m_SelectedIndex;
                if (evt.keyCode == KeyCode.DownArrow)
                {
                    if (m_SelectedIndex == -1 && evt.modifiers.HasFlag(EventModifiers.Alt))
                    {
                        m_CycledSearch = SearchService.CyclePreviousSearch(-1);
                        GUI.FocusControl(null);
                    }
                    else
                    {
                        m_SelectedIndex = Math.Min(m_SelectedIndex + 1, m_FilteredItems.Count - 1);
                        Event.current.Use();
                    }
                }
                else if (evt.keyCode == KeyCode.UpArrow)
                {
                    if (m_SelectedIndex >= 0)
                    {
                        m_SelectedIndex = Math.Max(-1, m_SelectedIndex - 1);
                        if (m_SelectedIndex == -1)
                        {
                            m_SearchBoxFocus = true;
                        }
                        Event.current.Use();
                    }
                    else if (evt.modifiers.HasFlag(EventModifiers.Alt))
                    {
                        m_CycledSearch = SearchService.CyclePreviousSearch(+1);
                        GUI.FocusControl(null);
                    }
                }
                else if (evt.keyCode == KeyCode.PageDown)
                {
                    m_SelectedIndex = Math.Min(m_SelectedIndex + GetDisplayItemCount() - 1, m_FilteredItems.Count - 1);
                    Event.current.Use();
                }
                else if (evt.keyCode == KeyCode.PageUp)
                {
                    m_SelectedIndex = Math.Max(0, m_SelectedIndex - GetDisplayItemCount());
                    Event.current.Use();
                }
                else if (evt.keyCode == KeyCode.RightArrow && evt.modifiers.HasFlag(EventModifiers.Alt))
                {
                    if (m_SelectedIndex != -1)
                    {
                        var item          = m_FilteredItems.ElementAt(m_SelectedIndex);
                        var menuPositionY = (m_SelectedIndex + 1) * Styles.itemRowHeight - m_ScrollPosition.y + Styles.itemRowHeight / 2.0f;
                        ShowItemContextualMenu(item, context, new Rect(position.width - Styles.actionButtonSize, menuPositionY, 1, 1));
                        Event.current.Use();
                    }
                }
                else if (evt.keyCode == KeyCode.LeftArrow && evt.modifiers.HasFlag(EventModifiers.Alt))
                {
                    m_ShowFilterWindow = true;
                    Event.current.Use();
                }
                else if (evt.keyCode == KeyCode.KeypadEnter || evt.keyCode == KeyCode.Return)
                {
                    var selectedIndex = m_SelectedIndex;
                    if (selectedIndex == -1 && m_FilteredItems != null && m_FilteredItems.Count > 0)
                    {
                        selectedIndex = 0;
                    }

                    if (selectedIndex != -1 && m_FilteredItems != null)
                    {
                        int actionIndex = 0;
                        if (evt.modifiers.HasFlag(EventModifiers.Alt))
                        {
                            actionIndex = 1;
                            if (evt.modifiers.HasFlag(EventModifiers.Control))
                            {
                                actionIndex = 2;
                                if (evt.modifiers.HasFlag(EventModifiers.Shift))
                                {
                                    actionIndex = 3;
                                }
                            }
                        }
                        var item = m_FilteredItems.ElementAt(selectedIndex);
                        if (item.provider.actions.Any())
                        {
                            Event.current.Use();
                            actionIndex = Math.Max(0, Math.Min(actionIndex, item.provider.actions.Count - 1));

                            ExecuteAction(item.provider.actions[actionIndex], item, context);
                            GUIUtility.ExitGUI();
                        }
                    }
                }
                else if (evt.keyCode == KeyCode.Escape)
                {
                    CloseSearchWindow();
                    Event.current.Use();
                }
                else
                {
                    GUI.FocusControl(k_QuickSearchBoxName);
                }

                if (prev != m_SelectedIndex)
                {
                    m_FocusSelectedItem = true;
                    Repaint();
                }
            }

            if (m_FilteredItems == null || m_FilteredItems.Count == 0)
            {
                m_SearchBoxFocus = true;
            }
        }
Exemple #7
0
        private void DrawToolbar(SearchContext context)
        {
            if (context == null)
            {
                return;
            }

            GUILayout.BeginHorizontal(Styles.toolbar);
            {
                var rightRect = EditorGUILayout.GetControlRect(GUILayout.MaxWidth(32f), GUILayout.ExpandHeight(true));
                if (EditorGUI.DropdownButton(rightRect, Styles.filterButtonContent, FocusType.Passive, Styles.filterButton) || m_ShowFilterWindow)
                {
                    if (FilterWindow.canShow)
                    {
                        rightRect.x += 12f; rightRect.y -= 3f;
                        if (m_ShowFilterWindow)
                        {
                            rightRect.y += 30f;
                        }

                        m_ShowFilterWindow = false;
                        if (FilterWindow.ShowAtPosition(this, rightRect))
                        {
                            GUIUtility.ExitGUI();
                        }
                    }
                }

                EditorGUI.BeginChangeCheck();

                using (new BlinkCursorScope(m_CursorBlinking, new Color(0, 0, 0, 0.01f)))
                {
                    var userSearchQuery = context.searchBoxText;
                    if (!String.IsNullOrEmpty(m_CycledSearch) && (Event.current.type == EventType.Repaint || Event.current.type == EventType.Layout))
                    {
                        userSearchQuery  = m_CycledSearch;
                        m_CycledSearch   = null;
                        m_SearchBoxFocus = true;
                        GUI.changed      = true;
                    }

                    GUI.SetNextControlName(k_QuickSearchBoxName);
                    context.searchBoxText = EditorGUILayout.TextField(userSearchQuery, Styles.searchField, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
                }

                if (String.IsNullOrEmpty(context.searchBoxText))
                {
                    EditorGUI.BeginDisabledGroup(true);
                    EditorGUI.TextArea(GUILayoutUtility.GetLastRect(), "search anything...", Styles.placeholderTextStyle);
                    EditorGUI.EndDisabledGroup();
                }
                if (!String.IsNullOrEmpty(context.searchBoxText))
                {
                    if (GUILayout.Button(Icons.clear, Styles.searchFieldClear, GUILayout.Width(24), GUILayout.Height(24)))
                    {
                        context.searchBoxText = "";
                        GUI.changed           = true;
                        GUI.FocusControl(null);
                    }
                }

                if (EditorGUI.EndChangeCheck() || m_FilteredItems == null)
                {
                    m_SelectedIndex    = -1;
                    m_ScrollPosition.y = 0;
                    SearchService.SearchTextChanged(context);
                    m_FilteredItems = SearchService.GetItems(context);
                    UpdateWindowTitle();
                }

                #if QUICKSEARCH_DEBUG
                DrawDebugTools();
                #endif
            }
            GUILayout.EndHorizontal();
        }
Exemple #8
0
        public static void AutoCompletion(Rect rect, SearchContext context, ISearchView view)
        {
            if (s_DiscardAutoComplete || controlID <= 0)
            {
                return;
            }

            if (s_AutoCompleting && Event.current.type == EventType.MouseDown && !s_AutoCompleteRect.Contains(Event.current.mousePosition))
            {
                s_DiscardAutoComplete = true;
                s_AutoCompleting      = false;
                return;
            }

            var te             = GetTextEditor();
            var cursorPosition = te.cursorIndex;

            if (cursorPosition == 0)
            {
                return;
            }

            var searchText        = context.searchText;
            var lastTokenStartPos = searchText.LastIndexOf(' ', Math.Max(0, te.cursorIndex - 1));
            var lastToken         = lastTokenStartPos == -1 ? searchText : searchText.Substring(lastTokenStartPos + 1);
            var keywords          = SearchService.GetKeywords(context, lastToken).Where(k => !k.Equals(lastToken, StringComparison.OrdinalIgnoreCase)).ToArray();

            if (keywords.Length > 0)
            {
                const int maxAutoCompleteCount = 16;
                s_AutoCompleteMaxIndex = Math.Min(keywords.Length, maxAutoCompleteCount);
                if (!s_AutoCompleting)
                {
                    s_AutoCompleteIndex = 0;
                }

                if (Event.current.type == EventType.Repaint)
                {
                    var content = new GUIContent(context.searchText.Substring(0, context.searchText.Length - lastToken.Length));
                    var offset  = Styles.searchField.CalcSize(content).x;
                    s_AutoCompleteRect       = rect;
                    s_AutoCompleteRect.x    += offset;
                    s_AutoCompleteRect.y     = rect.yMax;
                    s_AutoCompleteRect.width = 250;
                    s_AutoCompleteRect.x     = Math.Min(rect.width - s_AutoCompleteRect.width - 25, s_AutoCompleteRect.x);
                }

                var lt = lastToken;

                var autoFill = DoAutoComplete(lastToken, keywords, maxAutoCompleteCount, 0.1f);
                if (autoFill == null)
                {
                    // No more results
                    s_AutoCompleting    = false;
                    s_AutoCompleteIndex = -1;
                }
                else if (autoFill != lastToken)
                {
                    var regex = new Regex(Regex.Escape(lastToken), RegexOptions.IgnoreCase);
                    autoFill = regex.Replace(autoFill, "");
                    view.SetSearchText(context.searchText.Insert(cursorPosition, autoFill));
                }
                else
                {
                    s_AutoCompleting = true;
                }
            }
            else
            {
                s_AutoCompleting    = false;
                s_AutoCompleteIndex = -1;
            }
        }
        private static void DrawProviderSettings()
        {
            EditorGUILayout.LabelField("Provider Settings", EditorStyles.largeLabel);
            foreach (var p in SearchService.OrderedProviders)
            {
                GUILayout.BeginHorizontal();
                GUILayout.Space(20);

                var wasActive = p.active;
                p.active = GUILayout.Toggle(wasActive, Styles.toggleActiveContent);
                if (p.active != wasActive)
                {
                    EditorPrefs.SetBool($"{k_KeyPrefix}.{p.name.id}.active", p.active);
                }

                using (new EditorGUI.DisabledGroupScope(!p.active))
                {
                    GUILayout.Label(new GUIContent(p.name.displayName, $"{p.name.id} ({p.priority})"), GUILayout.Width(175));
                }

                if (!p.isExplicitProvider)
                {
                    if (GUILayout.Button(Styles.increasePriorityContent, Styles.priorityButton))
                    {
                        LowerProviderPriority(p);
                    }
                    if (GUILayout.Button(Styles.decreasePriorityContent, Styles.priorityButton))
                    {
                        UpperProviderPriority(p);
                    }
                }
                else
                {
                    GUILayoutUtility.GetRect(Styles.increasePriorityContent, Styles.priorityButton);
                    GUILayoutUtility.GetRect(Styles.increasePriorityContent, Styles.priorityButton);
                }

                GUILayout.Space(20);

                using (new EditorGUI.DisabledScope(p.actions.Count < 2))
                {
                    EditorGUI.BeginChangeCheck();
                    var items = p.actions.Select(a => new GUIContent(a.DisplayName, a.content.image,
                                                                     p.actions.Count == 1 ?
                                                                     $"Default action for {p.name.displayName} (Enter)" :
                                                                     $"Set default action for {p.name.displayName} (Enter)")).ToArray();
                    var newDefaultAction = EditorGUILayout.Popup(0, items, GUILayout.ExpandWidth(true));
                    if (EditorGUI.EndChangeCheck())
                    {
                        SearchService.SetDefaultAction(p.name.id, p.actions[newDefaultAction].Id);
                        GUI.changed = true;
                    }
                    GUILayout.Space(10);
                }

                GUILayout.EndHorizontal();
            }

            GUILayout.BeginHorizontal();
            GUILayout.Space(20);
            if (GUILayout.Button(Styles.resetPrioritiesContent, GUILayout.MaxWidth(100)))
            {
                ResetProviderPriorities();
            }
            GUILayout.EndHorizontal();
        }
Exemple #10
0
 public void DisableService()
 {
     SearchService.Disable(SearchContext.Empty);
     SearchService.LoadFilters();
     SearchSettings.useUberIndexing = m_WasUsingNewIndex;
 }
 public void DisableService()
 {
     SearchService.Disable(SearchContext.Empty);
     SearchService.LoadFilters();
 }
 internal void RecordFetchTime(double t)
 {
     fetchTimes[fetchTimeWriteIndex] = t;
     fetchTimeWriteIndex             = SearchService.Wrap(fetchTimeWriteIndex + 1, fetchTimes.Length);
 }
        private SearchRequest BuildSearchRequest(SearchExpressionNode node)
        {
            var searchQuery = Convert.ToString(node.value);

            if (node.variables != null)
            {
                // Replace constants
                foreach (var v in node.variables)
                {
                    if (v.type != ExpressionType.Value)
                    {
                        continue;
                    }

                    var constantValue = Convert.ToString(v.source.value);
                    if (String.IsNullOrEmpty(constantValue))
                    {
                        UnityEngine.Debug.LogWarning($"Constant value is null for {v.source.id}");
                    }

                    searchQuery = searchQuery.Replace($"${v.name}", constantValue);
                }

                foreach (var v in node.variables)
                {
                    if (v.type == ExpressionType.Value)
                    {
                        continue;
                    }

                    switch (v.type)
                    {
                    case ExpressionType.Search:
                    case ExpressionType.Select:
                    {
                        var varName = v.name;
                        return(BuildRequest(v.source).Join(varValue =>
                            {
                                var selectNode = new SearchExpressionNode(node.type, node.source, searchQuery.Replace($"${varName}", varValue));
                                return BuildRequest(selectNode);
                            }));
                    }

                    case ExpressionType.Undefined:
                        break;

                    default:
                        throw new NotSupportedException($"Evaluation of variable {v.name} of type {v.type} not supported");
                    }
                }
            }

            var providers = new List <SearchProvider>();

            if (node.source?.type == ExpressionType.Provider)
            {
                var selectedProviderName = Convert.ToString(node.source.value);
                if (!m_AdditionalProviders.TryGetValue(selectedProviderName, out var selectedProvider))
                {
                    selectedProvider = SearchService.GetProvider(selectedProviderName);
                }
                if (selectedProvider != null)
                {
                    providers.Add(selectedProvider);
                }
            }
            else if (node.source == null)
            {
                providers.AddRange(SearchService.Providers.Where(p => p.active));
            }
            else
            {
                throw new NotSupportedException($"Evaluation of source node {node.source.id} of type {node.source.type} is not supported.");
            }

            requestCount++;
            return(new SearchRequest(node.type, SearchService.CreateContext(providers, searchQuery, m_SearchOptions)));
        }
Exemple #14
0
 public void EnableService()
 {
     SearchService.Enable(SearchContext.Empty);
     SearchService.Filter.ResetFilter(true);
     m_Provider = new AsyncSearchProvider(Guid.NewGuid().ToString());
 }
Exemple #15
0
 public void EnableService()
 {
     SearchService.Enable(SearchContext.Empty);
     SearchService.Filter.ResetFilter(true);
 }
Exemple #16
0
 public void DisableService()
 {
     SearchService.Disable(SearchContext.Empty);
 }
Exemple #17
0
 public void ReportProgress(float progress = 0f, string status = null)
 {
     SearchService.ReportProgress(this, progress, status);
 }