Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Changing Keyboard widget view based on layout size.
Browse files Browse the repository at this point in the history
  • Loading branch information
daoshengmu committed Feb 20, 2020
1 parent ba99fb1 commit 95fd471
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,21 @@ public String getModeChangeKeyText() {
return mContext.getString(R.string.keyboard_mode_change);
}

@Override
public float getKeyboardTranslateYInWorld() {
return WidgetPlacement.unitFromMeters(mContext, R.dimen.keyboard_y);
}

@Override
public float getAlphabeticKeyboardWidth() {
return WidgetPlacement.dpDimension(mContext, R.dimen.keyboard_alphabetic_width);
}

@Override
public float getAlphabeticKeyboardHeight() {
return WidgetPlacement.dpDimension(mContext, R.dimen.keyboard_height);
}

@Override
public String[] getDomains(String... domains) {
return Stream.of(new String[]{".com", ".net", ".org", ".co"}, domains).flatMap(Stream::of)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.mozilla.vrbrowser.R;
import org.mozilla.vrbrowser.input.CustomKeyboard;
import org.mozilla.vrbrowser.ui.widgets.WidgetPlacement;
import org.mozilla.vrbrowser.utils.StringUtils;
import org.mozilla.vrbrowser.utils.SystemUtils;

Expand Down Expand Up @@ -44,7 +45,7 @@ public ChineseZhuyinKeyboard(Context aContext) {
@Override
public CustomKeyboard getAlphabeticKeyboard() {
if (mKeyboard == null) {
mKeyboard = new CustomKeyboard(mContext.getApplicationContext(), R.xml.keyboard_zhuyin);
mKeyboard = new CustomKeyboard(mContext.getApplicationContext(), R.xml.keyboard_qwerty_zhuyin);
loadDatabase();
}
return mKeyboard;
Expand Down Expand Up @@ -140,6 +141,21 @@ public boolean usesComposingText() {
return true;
}

@Override
public float getKeyboardTranslateYInWorld() {
return WidgetPlacement.unitFromMeters(mContext, R.dimen.zhuyin_keyboard_y);
}

@Override
public float getAlphabeticKeyboardWidth() {
return WidgetPlacement.dpDimension(mContext, R.dimen.keyboard_alphabetic_width_zhuyin);
}

@Override
public float getAlphabeticKeyboardHeight() {
return WidgetPlacement.dpDimension(mContext, R.dimen.keyboard_zhuyin_height);
}

@Override
public String getKeyboardTitle() {
return StringUtils.getStringByLocale(mContext, R.string.settings_language_traditional_chinese, getLocale());
Expand Down Expand Up @@ -218,7 +234,6 @@ private String findLabelFromKey(int primaryCode) {
}

private void addExtraKeyMaps() {
// List<Key> keys = mKeyboard.getKeys();
String s = Character.toString((char)0x3105);
String aa = findLabelFromKey(0x3105);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public enum Action {
public String composing;
}
@NonNull CustomKeyboard getAlphabeticKeyboard();
float getKeyboardTranslateYInWorld();
float getAlphabeticKeyboardWidth();
float getAlphabeticKeyboardHeight();
default @Nullable CustomKeyboard getSymbolsKeyboard() { return null; }
default @Nullable CandidatesResult getCandidates(String aComposingText) { return null; }
default @Nullable String overrideAddText(String aTextBeforeCursor, String aNextText) { return null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,7 @@ public void releaseWidget() {
protected void initializeWidgetPlacement(WidgetPlacement aPlacement) {
Context context = getContext();
aPlacement.width = getKeyboardWidth(WidgetPlacement.dpDimension(context, R.dimen.keyboard_alphabetic_width));
aPlacement.height = WidgetPlacement.dpDimension(context, R.dimen.keyboard_height);
aPlacement.height += WidgetPlacement.dpDimension(context, R.dimen.autocompletion_widget_line_height);
aPlacement.height += WidgetPlacement.dpDimension(context, R.dimen.keyboard_layout_padding);
aPlacement.height = getKeyboardHeight(WidgetPlacement.dpDimension(context, R.dimen.keyboard_height));
aPlacement.anchorX = 0.5f;
aPlacement.anchorY = 0.0f;
aPlacement.parentAnchorY = 0.0f;
Expand Down Expand Up @@ -331,6 +329,13 @@ private int getKeyboardWidth(float aAlphabeticWidth) {
return (int) width;
}

private int getKeyboardHeight(float aAlphabeticHeight) {
float height = aAlphabeticHeight;
height += WidgetPlacement.dpDimension(getContext(), R.dimen.autocompletion_widget_line_height);
height += WidgetPlacement.dpDimension(getContext(), R.dimen.keyboard_layout_padding);
return (int) height;
}

private void resetKeyboardLayout() {
if ((mEditorInfo.inputType & EditorInfo.TYPE_CLASS_NUMBER) == EditorInfo.TYPE_CLASS_NUMBER) {
mKeyboardView.setKeyboard(getSymbolsKeyboard());
Expand Down Expand Up @@ -723,19 +728,27 @@ private void handleLanguageChange(KeyboardInterface aKeyboard) {

mCurrentKeyboard = aKeyboard;
final int width = getKeyboardWidth(mCurrentKeyboard.getAlphabeticKeyboardWidth());
if (width != mWidgetPlacement.width) {
final int height = getKeyboardHeight(mCurrentKeyboard.getAlphabeticKeyboardHeight());

if (width != mWidgetPlacement.width || height != mWidgetPlacement.height) {
mWidgetPlacement.translationY = mCurrentKeyboard.getKeyboardTranslateYInWorld() - WidgetPlacement.unitFromMeters(getContext(), R.dimen.window_world_y);
mWidgetPlacement.width = width;
float defaultWorldWidth = WidgetPlacement.floatDimension(getContext(), R.dimen.keyboard_world_width);
int defaultKeyboardWidth = getKeyboardWidth(mKeyboards.get(0).getAlphabeticKeyboardWidth());
mWidgetPlacement.height = height;
final float defaultWorldWidth = WidgetPlacement.floatDimension(getContext(), R.dimen.keyboard_world_width);
final int defaultKeyboardWidth = getKeyboardWidth(mKeyboards.get(0).getAlphabeticKeyboardWidth());
mWidgetPlacement.worldWidth = defaultWorldWidth * ((float) width / (float) defaultKeyboardWidth);
mWidgetManager.updateWidget(this);
ViewGroup.LayoutParams params = mKeyboardContainer.getLayoutParams();
params.width = WidgetPlacement.convertDpToPixel(getContext(), mCurrentKeyboard.getAlphabeticKeyboardWidth());
params.height = WidgetPlacement.convertDpToPixel(getContext(), mCurrentKeyboard.getAlphabeticKeyboardHeight());
mKeyboardContainer.setLayoutParams(params);
}

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)mKeyboardLayout.getLayoutParams();
params.topMargin = mCurrentKeyboard.supportsAutoCompletion() ? WidgetPlacement.pixelDimension(getContext(), R.dimen.keyboard_margin_top_without_autocompletion) : 0;
if (height != params.height) {
params.height = WidgetPlacement.convertDpToPixel(getContext(), mCurrentKeyboard.getAlphabeticKeyboardHeight());
}
mKeyboardLayout.setLayoutParams(params);

SettingsStore.getInstance(getContext()).setSelectedKeyboard(aKeyboard.getLocale());
Expand Down Expand Up @@ -811,6 +824,25 @@ private void handleModeChange() {
Keyboard alphabetic = mCurrentKeyboard.getAlphabeticKeyboard();
mKeyboardView.setKeyboard(current == alphabetic ? getSymbolsKeyboard() : alphabetic);
mKeyboardView.setLayoutParams(mKeyboardView.getLayoutParams());

// Due to zh-tw keyboard uses the default keyboard layout as its symbol keyboard's layout,
// and their layout are different (4 rows vs. 5 rows). We need to do some adjustment here.
if (mCurrentKeyboard.getLocale() == Locale.TRADITIONAL_CHINESE) {
KeyboardInterface nextKeyboard = (current == alphabetic) ? mKeyboards.get(0) : mCurrentKeyboard;
final int width = getKeyboardWidth(nextKeyboard.getAlphabeticKeyboardWidth());
final int height = getKeyboardHeight(nextKeyboard.getAlphabeticKeyboardHeight());
mWidgetPlacement.width = width;
mWidgetPlacement.height = height;

final float defaultWorldWidth = WidgetPlacement.floatDimension(getContext(), R.dimen.keyboard_world_width);
final int defaultKeyboardWidth = getKeyboardWidth(mKeyboards.get(0).getAlphabeticKeyboardWidth());
mWidgetPlacement.worldWidth = defaultWorldWidth * ((float) width / (float) defaultKeyboardWidth);
mWidgetManager.updateWidget(this);
ViewGroup.LayoutParams params = mKeyboardContainer.getLayoutParams();
params.width = WidgetPlacement.convertDpToPixel(getContext(), nextKeyboard.getAlphabeticKeyboardWidth());
params.height = WidgetPlacement.convertDpToPixel(getContext(), nextKeyboard.getAlphabeticKeyboardHeight());
mKeyboardContainer.setLayoutParams(params);
}
if (current == alphabetic) {
mCurrentKeyboard.getAlphabeticKeyboard().setSpaceKeyLabel("");
}
Expand Down

0 comments on commit 95fd471

Please sign in to comment.