Skip to content

Commit

Permalink
Fix language selector layout. Add selected language highlight. (#1201)
Browse files Browse the repository at this point in the history
  • Loading branch information
MortimerGoro committed May 10, 2019
1 parent 77e3d9d commit 333462f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 11 deletions.
Expand Up @@ -11,6 +11,7 @@
import org.mozilla.vrbrowser.R;
import org.mozilla.vrbrowser.ui.widgets.WidgetPlacement;

import java.util.ArrayList;
import java.util.List;


Expand All @@ -32,7 +33,9 @@ public interface Delegate {
private GridLayout mLangRowContainer;
private Delegate mDelegate;
private List<Item> mItems;
private int mItemWidth;
private List<UITextButton> mButtons = new ArrayList<>();
private int mFirstColItemWidth;
private int mSecondColItemWidth;
private static final int kMaxItemsPerColumn = 4;

public LanguageSelectorView(@NonNull Context context) {
Expand All @@ -53,7 +56,8 @@ public LanguageSelectorView(@NonNull Context context, @Nullable AttributeSet att
private void initialize() {
inflate(getContext(), R.layout.language_selection, this);
mLangRowContainer = findViewById(R.id.langRowContainer);
mItemWidth = WidgetPlacement.pixelDimension(getContext(), R.dimen.lang_selector_item_width);
mFirstColItemWidth = WidgetPlacement.pixelDimension(getContext(), R.dimen.lang_selector_first_col_item_width);
mSecondColItemWidth = WidgetPlacement.pixelDimension(getContext(), R.dimen.lang_selector_second_col_item_width);
}

public void setDelegate(Delegate aDelegate) {
Expand All @@ -63,15 +67,27 @@ public void setDelegate(Delegate aDelegate) {
public void setItems(List<Item> aItems) {
mItems = aItems;
mLangRowContainer.removeAllViews();
mButtons.clear();
int columns = (aItems.size() / kMaxItemsPerColumn) + 1;
int rows = aItems.size() < kMaxItemsPerColumn ? aItems.size() : kMaxItemsPerColumn;
mLangRowContainer.setColumnCount(columns);
mLangRowContainer.setRowCount(rows);

int index = 0;
for (Item item: aItems) {
GridLayout.LayoutParams params = new GridLayout.LayoutParams();
params.width = mItemWidth;
mLangRowContainer.addView(createLangButton(item), params);
params.width = index < kMaxItemsPerColumn ? mFirstColItemWidth : mSecondColItemWidth;
UITextButton button = createLangButton(item);
mLangRowContainer.addView(button, params);
mButtons.add(button);
++index;
}
}

public void setSelectedItem(Object aTag) {
for (UITextButton button: mButtons) {
Item item = (Item) button.getTag();
button.setSelected(item.tag == aTag);
}
}

Expand Down
Expand Up @@ -180,8 +180,7 @@ private void initialize(Context aContext) {

mKeyboardView.setVisibility(View.VISIBLE);
mKeyboardNumericView.setKeyboard(mKeyboardNumeric);
mPopupKeyboardview.setVisibility(View.GONE);
mPopupKeyboardLayer.setVisibility(View.GONE);
hideOverlays();

mBackHandler = () -> onDismiss();

Expand Down Expand Up @@ -424,8 +423,7 @@ public void onKey(int primaryCode, int[] keyCodes, boolean hasPopup) {
}

if (!hasPopup) {
mPopupKeyboardview.setVisibility(View.GONE);
mPopupKeyboardLayer.setVisibility(View.GONE);
hideOverlays();
}
break;
}
Expand All @@ -440,8 +438,7 @@ public void onKey(int primaryCode, int[] keyCodes, boolean hasPopup) {

@Override
public void onNoKey() {
mPopupKeyboardview.setVisibility(View.GONE);
mPopupKeyboardLayer.setVisibility(View.GONE);
hideOverlays();
}

@Override
Expand Down Expand Up @@ -593,6 +590,7 @@ private void handleGlobeClick() {
}
mLanguageSelectorView.setItems(items);
}
mLanguageSelectorView.setSelectedItem(mCurrentKeyboard);
mLanguageSelectorView.setVisibility(View.VISIBLE);
mPopupKeyboardLayer.setVisibility(View.VISIBLE);
}
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/drawable/lang_selector_button_background.xml
@@ -1,5 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
<shape android:shape="rectangle">
<corners android:radius="@dimen/keyboard_key_rounded_corner"/>
<solid android:color="@color/white" />
</shape>
</item>>
<item android:state_pressed="true">
<shape android:shape="rectangle">
<corners android:radius="@dimen/keyboard_key_rounded_corner"/>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/drawable/lang_selector_button_color.xml
@@ -1,4 +1,6 @@
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:color="@color/void_color"/>
<item android:state_pressed="true"
android:color="@color/fog"/>
<item android:state_hovered="true"
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/values/dimen.xml
Expand Up @@ -66,7 +66,8 @@
<dimen name="settings_main_button_text_width">125dp</dimen>

<!-- Lang Selector -->
<dimen name="lang_selector_item_width">120dp</dimen>
<dimen name="lang_selector_first_col_item_width">100dp</dimen>
<dimen name="lang_selector_second_col_item_width">130dp</dimen>

<!-- Common values for menu style widgets -->
<dimen name="menu_item_height">88dp</dimen>
Expand Down

0 comments on commit 333462f

Please sign in to comment.