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 | |
| 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')
| -rw-r--r-- | srcs/juloo.keyboard2/suggestions/CandidatesView.java | 46 | ||||
| -rw-r--r-- | srcs/juloo.keyboard2/suggestions/Suggestions.java | 88 |
2 files changed, 88 insertions, 46 deletions
diff --git a/srcs/juloo.keyboard2/suggestions/CandidatesView.java b/srcs/juloo.keyboard2/suggestions/CandidatesView.java index 1768a52..5485ccf 100644 --- a/srcs/juloo.keyboard2/suggestions/CandidatesView.java +++ b/srcs/juloo.keyboard2/suggestions/CandidatesView.java @@ -1,6 +1,7 @@ package juloo.keyboard2.suggestions; import android.content.Context; +import android.os.Build.VERSION; import android.text.InputType; import android.util.AttributeSet; import android.util.TypedValue; @@ -16,10 +17,12 @@ import juloo.keyboard2.R; public class CandidatesView extends LinearLayout { - static final int NUM_CANDIDATES = 3; + static final int NUM_CANDIDATES = 4; /** Candidates currently visible. Entries can be [null] when there are less - than [NUM_CANDIDATES] suggestions. */ + than [NUM_CANDIDATES] suggestions. + - Entries at indexes [0] to [2] are word suggestions. + - Entry at index [3] is the emoji suggestion. */ String[] _items = new String[NUM_CANDIDATES]; /** Text views showing the candidates in [_items]. Text views visibility is @@ -42,44 +45,55 @@ public class CandidatesView extends LinearLayout setup_item_view(0, R.id.candidates_middle); setup_item_view(1, R.id.candidates_right); setup_item_view(2, R.id.candidates_left); + setup_item_view(3, R.id.candidates_emoji); } - public void set_candidates(List<String> suggestions) + public void set_candidates(Suggestions s) { - int s_count = suggestions.size(); + int s_count = s.count; + for (int i = 0; i < Suggestions.MAX_COUNT; i++) + _items[i] = (i < s_count) ? s.suggestions[i] : null; + _items[3] = s.emoji_suggestion; // Hide the status message when showing candidates. if (s_count != 0 && _status_no_dict != null) _status_no_dict.setVisibility(View.GONE); for (int i = 0; i < _item_views.length; i++) { TextView v = _item_views[i]; - if (i < s_count) + if (_items[i] != null) { - String it = suggestions.get(i); - _items[i] = it; - v.setText(it); + v.setText(_items[i]); v.setVisibility(View.VISIBLE); } else { - _items[i] = null; v.setVisibility(View.GONE); } } } + public void clear_candidates() + { + for (int i = 0; i < _item_views.length; i++) + { + _items[i] = null; + _item_views[i].setVisibility(View.GONE); + } + } + public void refresh_config(Config config) { - set_candidates(Suggestions.NO_SUGGESTIONS); + clear_candidates(); // The status message indicates whether the dictionaries should be // installed. _status_no_dict = inflate_and_show(_status_no_dict, (config.current_dictionary == null), R.layout.candidates_status_no_dict); - set_height(config); + set_sizes(config); } - void set_height(Config config) + /** Set the height of the suggestion row and the text size. */ + void set_sizes(Config config) { // Make the candidates view about as high as a keyboard row. int height = (int)(config.keyboard_rows_height_pixels * (1 - config.key_vertical_margin)); @@ -88,11 +102,17 @@ public class CandidatesView extends LinearLayout for (int i = 0; i < NUM_CANDIDATES; i++) { TextView v = _item_views[i]; + // Set view height ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams)v.getLayoutParams(); p.height = height; v.setLayoutParams(p); - v.setTextSize(TypedValue.COMPLEX_UNIT_PX, text_size); + // Set text size and enable auto size if supported. + if (VERSION.SDK_INT < 26) + v.setTextSize(TypedValue.COMPLEX_UNIT_PX, text_size); + else + v.setAutoSizeTextTypeUniformWithConfiguration( + (int)(text_size / 2.), (int)text_size, 1, TypedValue.COMPLEX_UNIT_PX); } } 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); } } |
