diff options
| author | Patrick | 2026-05-07 20:16:23 +0200 |
|---|---|---|
| committer | Patrick | 2026-05-07 20:16:23 +0200 |
| commit | b866f59e400973c2f7ba0e97517fdddeb3efbb33 (patch) | |
| tree | 20c702893298303445e4513de78a9fd83ca5bf94 /srcs/juloo.keyboard2/suggestions/Suggestions.java | |
| parent | 5cfb4e328e4985f9ea7c145a76938f48e9df6fef (diff) | |
| parent | b807f217a06d3312e05a25c23342f41d339e76c6 (diff) | |
| download | unexpected-keyboard-b866f59e400973c2f7ba0e97517fdddeb3efbb33.tar.gz unexpected-keyboard-b866f59e400973c2f7ba0e97517fdddeb3efbb33.zip | |
Merge branch 'master' into skintones
Diffstat (limited to 'srcs/juloo.keyboard2/suggestions/Suggestions.java')
| -rw-r--r-- | srcs/juloo.keyboard2/suggestions/Suggestions.java | 88 |
1 files changed, 55 insertions, 33 deletions
diff --git a/srcs/juloo.keyboard2/suggestions/Suggestions.java b/srcs/juloo.keyboard2/suggestions/Suggestions.java index 41a7941..f72257b 100644 --- a/srcs/juloo.keyboard2/suggestions/Suggestions.java +++ b/srcs/juloo.keyboard2/suggestions/Suggestions.java @@ -16,9 +16,14 @@ public final class Suggestions Config _config; boolean _enabled; - /** The suggestion displayed at the center of the candidates view and entered - by the space bar. */ - public String best_suggestion = null; + /** Current suggestions. The best suggestion is at index [0]. */ + public String[] suggestions = new String[MAX_COUNT]; + /** Number of suggestions at the beginning of the [suggestions] array that + are not [null]. */ + public int count = 0; + public String emoji_suggestion = null; + /** Number of suggestions in [suggestions]. */ + public static final int MAX_COUNT = 3; public Suggestions(Callback c, Config conf) { @@ -29,55 +34,74 @@ public final class Suggestions public void started() { _enabled = _config.editor_config.should_show_candidates_view; - best_suggestion = null; + clear(); } public void currently_typed_word(String word) { if (!_enabled) return; - Cdict dict = _config.current_dictionary; - if (word.length() < 2 || dict == null) - { - set_suggestions(NO_SUGGESTIONS); - } + if (word.length() < 2 || _config.current_dictionary == null) + clear(); else - { - String[] dst = new String[3]; - query_suggestions(dict, word, dst, 3); - set_suggestions(Arrays.asList(dst)); - } + query_suggestions(word); + set_suggestions(); + } + + void clear() + { + count = 0; + suggestions[0] = null; + emoji_suggestion = null; } - int query_suggestions(Cdict dict, String word, String[] dst, int max_count) + int query_suggestions(String word) { + Cdict dict = _config.current_dictionary; boolean first_char_upper = Character.isUpperCase(word.charAt(0)); word = apply_substitutions(word); Cdict.Result r = dict.find(word); int i = 0; if (r.found) - dst[i++] = dict.word(r.index); - int[] suffixes = dict.suffixes(r, max_count); + suggestions[i++] = dict.word(r.index); + int[] suffixes = dict.suffixes(r, MAX_COUNT); // Disable distance search for small words - int[] dist = (word.length() < 3 || i + 1 >= max_count) ? NO_RESULTS : - dict.distance(word, 1, max_count); - for (int j = 0; j < max_count && i < max_count; j++) + int[] dist = (word.length() < 3 || i + 1 >= MAX_COUNT) ? NO_RESULTS : + dict.distance(word, 1, MAX_COUNT); + for (int j = 0; j < MAX_COUNT && i < MAX_COUNT; j++) { if (suffixes.length > j) - dst[i++] = dict.word(suffixes[j]); - if (dist.length > j && i < max_count) - dst[i++] = dict.word(dist[j]); + suggestions[i++] = dict.word(suffixes[j]); + if (dist.length > j && i < MAX_COUNT) + suggestions[i++] = dict.word(dist[j]); } if (first_char_upper) - capitalize_results(dst); + capitalize_results(); + emoji_suggestion = query_emoji(word); // word with substitutions applied + count = i; return i; } - void capitalize_results(String[] rs) + void capitalize_results() { - for (int i = 0; i < rs.length; i++) - if (rs[i] != null) - rs[i] = rs[i].substring(0, 1).toUpperCase() + rs[i].substring(1); + for (int i = 0; i < count; i++) + suggestions[i] = suggestions[i].substring(0, 1).toUpperCase() + + suggestions[i].substring(1); + } + + String query_emoji(String word) + { + Cdict dict = _config.emoji_dictionary; + // Disable emoji suggestion for short words + if (dict == null || word.length() < 3) + return null; + Cdict.Result r = dict.find(word); + if (r.found) + return dict.word(r.index); + int[] s = dict.suffixes(r, 1); + if (s.length > 0) + return dict.word(s[0]); + return null; } /** Apply the same substitutions that were used when building the @@ -96,17 +120,15 @@ public final class Suggestions return b.toString(); } - void set_suggestions(List<String> ws) + void set_suggestions() { - _callback.set_suggestions(ws); - best_suggestion = (ws.size() > 0) ? ws.get(0) : null; + _callback.set_suggestions(this); } - static final List<String> NO_SUGGESTIONS = Arrays.asList(); static final int[] NO_RESULTS = new int[0]; public static interface Callback { - public void set_suggestions(List<String> suggestions); + public void set_suggestions(Suggestions suggestions); } } |
