diff options
author | Mathias Gumz <akira@fluxbox.org> | 2015-01-28 15:02:59 (GMT) |
---|---|---|
committer | Mathias Gumz <akira@fluxbox.org> | 2015-01-28 15:02:59 (GMT) |
commit | 03ce82a4737b834767c03341db9362ada24c775a (patch) | |
tree | 2444031bbd47d97b1e0af64aa2f1d2a037a08a2e /src/FbTk/TypeAhead.hh | |
parent | fc245408d6975d0813cd4440e7089d987b54d42e (diff) | |
download | fluxbox-03ce82a4737b834767c03341db9362ada24c775a.zip fluxbox-03ce82a4737b834767c03341db9362ada24c775a.tar.bz2 |
Feature: typeahead in menu matches text anywhere
This commit implements a tweak to the typeahead feature already existent in
fluxbox: If the user opens up a menu and starts typing, fluxbox tries to
detect matching menu items and makes them available for quick selection.
The typed pattern is now search also in the middle of the text.
I opted to strip down the code quite a bit and remove complexity by throwing
out FbTk::TypeAhead and FbTk::SearchResult because I do not see the need for a
general solution when the only use case for such a feature is in fluxbox'
menus. FbTk::ITypeAheadable shrunk down to 2 functions; the whole file might
be combined with the code that implements FbTk::Menu::TypeSearch.
FbTk::Menu::setIndex() and related code is also gone: the position of each
menu item is defined by it's position in the items container. This reduces the
mount of book keeping fluxbox has to do. Fewer moving parts is a good thing.
It's possible that users start to complaint because they expect their
typed pattern to match only at the beginning of the text OR that some
demand other tweaks. We will see.
This commit also fixes a regression introduced by 8387742c. The bug made
the menu vanish.
Diffstat (limited to 'src/FbTk/TypeAhead.hh')
-rw-r--r-- | src/FbTk/TypeAhead.hh | 177 |
1 files changed, 0 insertions, 177 deletions
diff --git a/src/FbTk/TypeAhead.hh b/src/FbTk/TypeAhead.hh deleted file mode 100644 index 119e3af..0000000 --- a/src/FbTk/TypeAhead.hh +++ /dev/null | |||
@@ -1,177 +0,0 @@ | |||
1 | // TypeAhead.hh for FbTk - Fluxbox Toolkit | ||
2 | // Copyright (c) 2007 Fluxbox Team (fluxgen at fluxbox dot org) | ||
3 | // | ||
4 | // Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | // copy of this software and associated documentation files (the "Software"), | ||
6 | // to deal in the Software without restriction, including without limitation | ||
7 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | // and/or sell copies of the Software, and to permit persons to whom the | ||
9 | // Software is furnished to do so, subject to the following conditions: | ||
10 | // | ||
11 | // The above copyright notice and this permission notice shall be included in | ||
12 | // all copies or substantial portions of the Software. | ||
13 | // | ||
14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | // DEALINGS IN THE SOFTWARE. | ||
21 | |||
22 | #ifndef FBTK_TYPEAHEAD_HH | ||
23 | #define FBTK_TYPEAHEAD_HH | ||
24 | |||
25 | #include "ITypeAheadable.hh" | ||
26 | #include "SearchResult.hh" | ||
27 | |||
28 | namespace FbTk { | ||
29 | |||
30 | template <typename Items, typename Item_Type> | ||
31 | class TypeAhead { | ||
32 | #if 0 | ||
33 | |||
34 | // a class template can't be split into separate interface + implementation files, an interface summary is given here: | ||
35 | |||
36 | public: | ||
37 | void init(Items const &items); | ||
38 | |||
39 | // accessors: | ||
40 | int stringSize() const { return m_searchstr.size(); } | ||
41 | Items matched() const; | ||
42 | |||
43 | // modifiers: | ||
44 | Items putCharacter(char ch); | ||
45 | void putBackSpace(); | ||
46 | void reset() | ||
47 | |||
48 | private: | ||
49 | SearchResults m_search_results; | ||
50 | std::string m_searchstr; | ||
51 | Items const *m_ref; | ||
52 | |||
53 | // helper | ||
54 | void fillValues(BaseItems const &search, ValueVec &fillin) const; | ||
55 | |||
56 | // reverts to searchstate before current | ||
57 | void revert(); | ||
58 | |||
59 | // search performs iteration and sets state | ||
60 | void search(char char_to_test); | ||
61 | void doSearch(char to_test, | ||
62 | Items const &items, | ||
63 | SearchResult &mySearchResult) const; | ||
64 | void doSearch(char to_test, | ||
65 | BaseItems const &search, | ||
66 | SearchResult &mySearchResult) const; | ||
67 | #endif | ||
68 | |||
69 | public: | ||
70 | typedef std::vector < ITypeAheadable* > BaseItems; | ||
71 | typedef BaseItems::const_iterator BaseItemscIt; | ||
72 | typedef std::vector < SearchResult > SearchResults; | ||
73 | typedef typename Items::const_iterator ItemscIt; | ||
74 | |||
75 | TypeAhead() : m_ref(0) { } | ||
76 | |||
77 | void init(Items const &items) { m_ref = &items; } | ||
78 | |||
79 | size_t stringSize() const { return m_searchstr.size(); } | ||
80 | |||
81 | void seek() { | ||
82 | if (!m_search_results.empty()) | ||
83 | m_searchstr = m_search_results.back().seekedString(); | ||
84 | } | ||
85 | |||
86 | Items putCharacter(char ch) { | ||
87 | if (isprint(ch)) | ||
88 | search(ch); | ||
89 | return matched(); | ||
90 | } | ||
91 | |||
92 | void putBackSpace() { | ||
93 | if (!m_search_results.empty()) | ||
94 | revert(); | ||
95 | } | ||
96 | |||
97 | void reset() { | ||
98 | m_searchstr.clear(); | ||
99 | m_search_results.clear(); | ||
100 | } | ||
101 | |||
102 | Items matched() const { | ||
103 | Items last_matched; | ||
104 | |||
105 | if (!m_search_results.empty()) | ||
106 | fillValues(m_search_results.back().result(), last_matched); | ||
107 | else | ||
108 | return *m_ref; | ||
109 | return last_matched; | ||
110 | } | ||
111 | |||
112 | private: | ||
113 | SearchResults m_search_results; | ||
114 | std::string m_searchstr; | ||
115 | Items const *m_ref; // reference to vector we are operating on | ||
116 | |||
117 | void fillValues(BaseItems const &search, Items &fillin) const { | ||
118 | for (BaseItemscIt it = search.begin(); it != search.end(); ++it) { | ||
119 | Item_Type tmp = dynamic_cast<Item_Type>(*it); | ||
120 | if (tmp) | ||
121 | fillin.push_back(tmp); | ||
122 | } | ||
123 | } | ||
124 | |||
125 | void revert() { | ||
126 | m_search_results.pop_back(); | ||
127 | if (m_search_results.empty()) | ||
128 | m_searchstr.clear(); | ||
129 | else | ||
130 | m_searchstr = m_search_results.back().seekedString(); | ||
131 | } | ||
132 | |||
133 | void search(char char_to_test) { | ||
134 | SearchResult mySearchResult(m_searchstr + char_to_test); | ||
135 | size_t num_items = m_ref->size(); | ||
136 | |||
137 | // check if we have already a searched set | ||
138 | if (m_search_results.empty()) | ||
139 | doSearch(char_to_test, *m_ref, mySearchResult); | ||
140 | else { | ||
141 | num_items = m_search_results.back().size(); | ||
142 | doSearch(char_to_test, m_search_results.back().result(), | ||
143 | mySearchResult); | ||
144 | } | ||
145 | |||
146 | if (mySearchResult.size() > 0 ) { | ||
147 | if (mySearchResult.size() < num_items) { | ||
148 | mySearchResult.seek(); | ||
149 | m_search_results.push_back(mySearchResult); | ||
150 | } | ||
151 | m_searchstr += char_to_test; | ||
152 | } | ||
153 | } | ||
154 | |||
155 | // iteration based on original list of items | ||
156 | void doSearch(char to_test, Items const &items, | ||
157 | SearchResult &mySearchResult) const { | ||
158 | for (ItemscIt it = items.begin(); it != items.end(); ++it) { | ||
159 | if ((*it)->iTypeCompareChar(to_test, stringSize()) && (*it)->isEnabled()) | ||
160 | mySearchResult.add(*it); | ||
161 | } | ||
162 | } | ||
163 | |||
164 | // iteration based on last SearchResult | ||
165 | void doSearch(char to_test, BaseItems const &search, | ||
166 | SearchResult &mySearchResult) const { | ||
167 | for (BaseItemscIt it = search.begin(); it != search.end(); ++it) { | ||
168 | if ((*it)->iTypeCompareChar(to_test, stringSize()) && (*it)->isEnabled()) | ||
169 | mySearchResult.add(*it); | ||
170 | } | ||
171 | } | ||
172 | |||
173 | }; // end Class TypeAhead | ||
174 | |||
175 | } // end namespace FbTk | ||
176 | |||
177 | #endif // FBTK_TYPEAHEAD_HH | ||