From 2ecf93d9904544ee73159e9f0ee74b49057bca6c Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Sun, 1 Feb 2026 23:25:38 +0100 Subject: Candidates view improvements (#1168) * Refactor: Create subpackage 'suggestions' * Candidates view: Status message when no dictionary installed Show a message on the candidates view instead of leaving it empty. A button points to the dictionary installation activity. * Add an option to disable the suggestions * Refactor: Remove Config.should_show_candidates_view This was moved to EditorConfig. * Don't disable text suggestions in some text boxes * Suggestion text size matching settings The candidates view height is based on the height of keyboard rows and the suggestion text size is based on the size of labels on the keys. This is influenced by symbol size and keyboard height options.--- .../suggestions/CandidatesView.java | 161 +++++++++++++++++++++ srcs/juloo.keyboard2/suggestions/Suggestions.java | 36 +++++ 2 files changed, 197 insertions(+) create mode 100644 srcs/juloo.keyboard2/suggestions/CandidatesView.java create mode 100644 srcs/juloo.keyboard2/suggestions/Suggestions.java (limited to 'srcs/juloo.keyboard2/suggestions') diff --git a/srcs/juloo.keyboard2/suggestions/CandidatesView.java b/srcs/juloo.keyboard2/suggestions/CandidatesView.java new file mode 100644 index 0000000..259db35 --- /dev/null +++ b/srcs/juloo.keyboard2/suggestions/CandidatesView.java @@ -0,0 +1,161 @@ +package juloo.keyboard2.suggestions; + +import android.content.Context; +import android.text.InputType; +import android.util.AttributeSet; +import android.util.TypedValue; +import android.view.View; +import android.view.ViewGroup; +import android.view.inputmethod.EditorInfo; +import android.widget.LinearLayout; +import android.widget.TextView; +import java.util.ArrayList; +import java.util.List; +import juloo.keyboard2.Config; +import juloo.keyboard2.R; + +public class CandidatesView extends LinearLayout +{ + static final int NUM_CANDIDATES = 3; + + /** Candidates currently visible. Entries can be [null] when there are less + than [NUM_CANDIDATES] suggestions. */ + String[] _items = new String[NUM_CANDIDATES]; + + /** Text views showing the candidates in [_items]. Text views visibility is + set to [GONE] when there are less than [NUM_CANDIDATES] suggestions. */ + TextView[] _item_views = new TextView[NUM_CANDIDATES]; + + /** Optional view showing a message to the user. Visible when no candidates + are shown. Might be [null]. */ + View _status_no_dict = null; // Dictionary not installed + + public CandidatesView(Context context, AttributeSet attrs) + { + super(context, attrs); + } + + @Override + protected void onFinishInflate() + { + super.onFinishInflate(); + setup_item_view(0, R.id.candidates_middle); + setup_item_view(1, R.id.candidates_right); + setup_item_view(2, R.id.candidates_left); + } + + public void set_candidates(List suggestions) + { + int s_count = suggestions.size(); + // 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) + { + String it = suggestions.get(i); + _items[i] = it; + v.setText(it); + v.setVisibility(View.VISIBLE); + } + else + { + _items[i] = null; + v.setVisibility(View.GONE); + } + } + } + + public void refresh_config(Config config) + { + set_candidates(Suggestions.NO_SUGGESTIONS); + // The status message indicates whether the dictionaries should be + // installed. + _status_no_dict = inflate_and_show(_status_no_dict, + true, + R.layout.candidates_status_no_dict); + set_height(config); + } + + void set_height(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)); + // Match the size of labels on the keyboard, increased by 15%. + float text_size = height * config.characterSize * config.labelTextSize * 1.15f; + for (int i = 0; i < NUM_CANDIDATES; i++) + { + TextView v = _item_views[i]; + ViewGroup.MarginLayoutParams p = + (ViewGroup.MarginLayoutParams)v.getLayoutParams(); + p.height = height; + v.setLayoutParams(p); + v.setTextSize(TypedValue.COMPLEX_UNIT_PX, text_size); + } + } + + /** Show or hide a status view and inflate it if needed. */ + View inflate_and_show(View v, boolean show, int layout_id) + { + if (!show) + { + if (v != null) + v.setVisibility(View.GONE); + } + else + { + if (v == null) + { + v = View.inflate(getContext(), layout_id, null); + addView(v); + } + v.setVisibility(View.VISIBLE); + } + return v; + } + + private void setup_item_view(final int item_index, int item_id) + { + TextView v = (TextView)findViewById(item_id); + v.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View _v) + { + String it = _items[item_index]; + if (it != null) + Config.globalConfig().handler.suggestion_entered(it); + } + }); + v.setVisibility(View.GONE); + _item_views[item_index] = v; + } + + /** Whether the candidates view should be shown for a given editor. */ + public static boolean should_show(EditorInfo info) + { + int variation = info.inputType & InputType.TYPE_MASK_VARIATION; + int flags = info.inputType & InputType.TYPE_MASK_FLAGS; + switch (info.inputType & InputType.TYPE_MASK_CLASS) + { + case InputType.TYPE_CLASS_TEXT: + switch (variation) + { + case InputType.TYPE_TEXT_VARIATION_PASSWORD: + case InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD: + case InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD: + return false; + default: + if ((flags & InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS) != 0) + return false; // Editor requested that we don't show suggestions + return true; + } + case InputType.TYPE_CLASS_NUMBER: + // Beware of TYPE_NUMBER_VARIATION_PASSWORD + return false; + default: return false; + } + } +} diff --git a/srcs/juloo.keyboard2/suggestions/Suggestions.java b/srcs/juloo.keyboard2/suggestions/Suggestions.java new file mode 100644 index 0000000..50c64e0 --- /dev/null +++ b/srcs/juloo.keyboard2/suggestions/Suggestions.java @@ -0,0 +1,36 @@ +package juloo.keyboard2.suggestions; + +import java.util.Arrays; +import java.util.List; + +/** Keep track of the word being typed and provide suggestions for + [CandidatesView]. */ +public final class Suggestions +{ + Callback _callback; + + public Suggestions(Callback c) + { + _callback = c; + } + + public void currently_typed_word(String word) + { + if (word.equals("")) + { + _callback.set_suggestions(NO_SUGGESTIONS); + } + else + { + // TODO + _callback.set_suggestions(Arrays.asList(word)); + } + } + + static final List NO_SUGGESTIONS = Arrays.asList(); + + public static interface Callback + { + public void set_suggestions(List suggestions); + } +} -- cgit v1.2.3