From 9c73c5df237b5b94bfd43edf071c3e39b78e1217 Mon Sep 17 00:00:00 2001 From: serso Date: Sat, 23 Jan 2016 13:48:25 +0100 Subject: [PATCH] Landscape keyboard UI --- .../android/calculator/KeyboardUi.java | 60 ++++++++++++++- .../android/calculator/KeyboardWindow.java | 20 +++-- .../function/EditFunctionFragment.java | 73 +++++++++++++------ app/src/main/res/values/ids.xml | 1 + 4 files changed, 125 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/org/solovyev/android/calculator/KeyboardUi.java b/app/src/main/java/org/solovyev/android/calculator/KeyboardUi.java index 95057a13..227b7408 100644 --- a/app/src/main/java/org/solovyev/android/calculator/KeyboardUi.java +++ b/app/src/main/java/org/solovyev/android/calculator/KeyboardUi.java @@ -62,7 +62,50 @@ public class KeyboardUi { buttonBackground = App.getTheme().light ? R.drawable.material_button_light : R.drawable.material_button_dark; } - public void makeView() { + public void makeView(boolean landscape) { + if (landscape) { + makeViewLand(); + } else { + makeViewPort(); + } + } + + private void makeViewLand() { + final int parametersCount = parameterNames.size(); + + LinearLayout row = makeRow(); + addImageButton(row, R.id.cpp_kb_button_keyboard, R.drawable.ic_keyboard_white_24dp); + addButton(row, 0, parametersCount > 0 ? parameterNames.get(0) : "x"); + addButton(row, 0, "7"); + addButton(row, 0, "8"); + addButton(row, 0, "9").setText("π", up).setText("e", down); + addOperationButton(row, R.id.cpp_kb_button_multiply, Locator.getInstance().getEngine().getMultiplicationSign()).setText("^n", up).setText("^2", down); + addOperationButton(row, R.id.cpp_kb_button_plus, "+"); + addButton(row, R.id.cpp_kb_button_clear, "C"); + + row = makeRow(); + addButton(row, R.id.cpp_kb_button_brackets, "( )").setText("(", up).setText(")", down); + addButton(row, 0, parametersCount > 1 ? parameterNames.get(1) : "y"); + addButton(row, 0, "4"); + addButton(row, 0, "5"); + addButton(row, 0, "6"); + addOperationButton(row, R.id.cpp_kb_button_divide, "/").setText("%", up).setText("sqrt", down); + addOperationButton(row, R.id.cpp_kb_button_minus, "−"); + final View backspace = addImageButton(row, R.id.cpp_kb_button_backspace, R.drawable.ic_backspace_white_24dp); + EditTextLongClickEraser.attachTo(backspace, user.getEditor()); + + row = makeRow(); + addButton(row, R.id.cpp_kb_button_functions_constants, "f/π"); + addButton(row, 0, ".").setText(",", up); + addButton(row, 0, "1"); + addButton(row, 0, "2"); + addButton(row, 0, "3"); + addButton(row, 0, "0").setText("00", up).setText("000", down); + addImageButton(row, R.id.cpp_kb_button_space, R.drawable.ic_space_bar_white_24dp); + addImageButton(row, R.id.cpp_kb_button_close, R.drawable.ic_done_white_24dp); + } + + private void makeViewPort() { LinearLayout row = makeRow(); addButton(row, 0, "7"); addButton(row, 0, "8"); @@ -96,7 +139,7 @@ public class KeyboardUi { final int parametersCount = parameterNames.size(); addButton(row, 0, parametersCount > 0 ? parameterNames.get(0) : "x"); addButton(row, 0, parametersCount > 1 ? parameterNames.get(1) : "y"); - addButton(row, R.id.cpp_kb_button_functions, "f(x)"); + addButton(row, R.id.cpp_kb_button_functions, "f"); addButton(row, R.id.cpp_kb_button_constants, "π"); addImageButton(row, R.id.cpp_kb_button_close, R.drawable.ic_done_white_24dp); } @@ -169,6 +212,14 @@ public class KeyboardUi { return button; } + public int getRowsCount(boolean landscape) { + return landscape ? 3 : 5; + } + + public int getColumnsCount(boolean landscape) { + return landscape ? 8 : 5; + } + public interface User { @NonNull Context getContext(); @@ -195,6 +246,8 @@ public class KeyboardUi { void done(); void showIme(); + + void showFunctionsConstants(@NonNull View v); } private class ButtonHandler implements View.OnClickListener, SimpleDragListener.DragProcessor { @@ -213,6 +266,9 @@ public class KeyboardUi { case R.id.cpp_kb_button_multiply: user.insertOperator('*'); break; + case R.id.cpp_kb_button_functions_constants: + user.showFunctionsConstants(v); + break; case R.id.cpp_kb_button_functions: user.showFunctions(v); break; diff --git a/app/src/main/java/org/solovyev/android/calculator/KeyboardWindow.java b/app/src/main/java/org/solovyev/android/calculator/KeyboardWindow.java index eb8178f5..e622ccd1 100644 --- a/app/src/main/java/org/solovyev/android/calculator/KeyboardWindow.java +++ b/app/src/main/java/org/solovyev/android/calculator/KeyboardWindow.java @@ -2,6 +2,8 @@ package org.solovyev.android.calculator; import android.app.Dialog; import android.content.Context; +import android.content.res.Configuration; +import android.content.res.Resources; import android.os.IBinder; import android.support.annotation.NonNull; import android.support.annotation.Nullable; @@ -53,9 +55,15 @@ public class KeyboardWindow { final Context context = editor.getContext(); final LinearLayout view = new LinearLayout(context); view.setOrientation(LinearLayout.VERTICAL); - final int buttonSize = context.getResources().getDimensionPixelSize(R.dimen.cpp_clickable_area_size); - final int keyboardSize = 5 * buttonSize; - window = new PopupWindow(view, keyboardSize, keyboardSize); + + final Resources resources = context.getResources(); + final int buttonSize = resources.getDimensionPixelSize(R.dimen.cpp_clickable_area_size); + final KeyboardUi keyboardUi = new KeyboardUi(user, parameterNames); + final boolean landscape = resources.getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE; + final int keyboardWidth = keyboardUi.getColumnsCount(landscape) * buttonSize; + final int keyboardHeight = keyboardUi.getRowsCount(landscape) * buttonSize; + + window = new PopupWindow(view, keyboardWidth, keyboardHeight); window.setClippingEnabled(false); window.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override @@ -73,15 +81,15 @@ public class KeyboardWindow { if (editor.getWindowToken() != null) { hideIme(editor); final int inputWidth = editor.getWidth(); - final int xOff = (inputWidth - keyboardSize) / 2; - window.setWidth(keyboardSize); + final int xOff = (inputWidth - keyboardWidth) / 2; + window.setWidth(keyboardWidth); window.showAsDropDown(editor, xOff, 0); } else { editor.postDelayed(this, 50); } } }); - new KeyboardUi(user, parameterNames).makeView(); + keyboardUi.makeView(landscape); } public boolean isShown() { diff --git a/app/src/main/java/org/solovyev/android/calculator/function/EditFunctionFragment.java b/app/src/main/java/org/solovyev/android/calculator/function/EditFunctionFragment.java index 9fe4d587..513ff0aa 100644 --- a/app/src/main/java/org/solovyev/android/calculator/function/EditFunctionFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/function/EditFunctionFragment.java @@ -64,6 +64,7 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC private static final String ARG_FUNCTION = "function"; private static final int MENU_FUNCTION = Menu.FIRST; private static final int MENU_CONSTANT = Menu.FIRST + 1; + private static final int MENU_CATEGORY = Menu.FIRST + 2; @NonNull private final MathRegistry functionsRegistry = Locator.getInstance().getEngine().getFunctionsRegistry(); @@ -456,10 +457,7 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { final int id = v.getId(); if (id == R.id.function_body) { - menu.clear(); - for (String constant : getNamesSorted(constantsRegistry)) { - menu.add(MENU_CONSTANT, Menu.NONE, Menu.NONE, constant).setOnMenuItemClickListener(KeyboardUser.this); - } + addEntities(menu, getNamesSorted(constantsRegistry), MENU_CONSTANT); unregisterForContextMenu(bodyView); } } @@ -481,10 +479,30 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { final int id = v.getId(); if (id == R.id.function_body) { - menu.clear(); - for (String function : getNamesSorted(functionsRegistry)) { - menu.add(MENU_FUNCTION, Menu.NONE, Menu.NONE, function).setOnMenuItemClickListener(KeyboardUser.this); - } + addEntities(menu, getNamesSorted(functionsRegistry), MENU_FUNCTION); + unregisterForContextMenu(bodyView); + } + } + }); + bodyView.showContextMenu(); + } + + private void addEntities(@NonNull Menu menu, @NonNull List entities, int groupId) { + for (String entity : entities) { + menu.add(groupId, Menu.NONE, Menu.NONE, entity).setOnMenuItemClickListener(KeyboardUser.this); + } + } + + @Override + public void showFunctionsConstants(@NonNull View v) { + bodyView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() { + @Override + public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { + final int id = v.getId(); + if (id == R.id.function_body) { + // can't use sub-menus as AlertDialog doesn't support them + menu.add(MENU_CATEGORY, MENU_CONSTANT, Menu.NONE, R.string.c_vars_and_constants).setOnMenuItemClickListener(KeyboardUser.this); + menu.add(MENU_CATEGORY, MENU_FUNCTION, Menu.NONE, R.string.c_functions).setOnMenuItemClickListener(KeyboardUser.this); unregisterForContextMenu(bodyView); } } @@ -522,22 +540,35 @@ public class EditFunctionFragment extends BaseDialogFragment implements View.OnC } @Override - public boolean onMenuItemClick(MenuItem item) { + public boolean onMenuItemClick(final MenuItem item) { final int groupId = item.getGroupId(); final CharSequence title = item.getTitle(); - if (groupId == MENU_FUNCTION) { - final int argsListIndex = title.toString().indexOf("("); - if (argsListIndex < 0) { - keyboardUser.insertText(title + "()", -1); - } else { - keyboardUser.insertText(title.subSequence(0, argsListIndex) + "()", -1); - } - } else if (groupId == MENU_CONSTANT) { - keyboardUser.insertText(title.toString(), 0); - } else { - return false; + switch (groupId) { + case MENU_FUNCTION: + final int argsListIndex = title.toString().indexOf("("); + if (argsListIndex < 0) { + keyboardUser.insertText(title + "()", -1); + } else { + keyboardUser.insertText(title.subSequence(0, argsListIndex) + "()", -1); + } + return true; + case MENU_CONSTANT: + keyboardUser.insertText(title.toString(), 0); + return true; + case MENU_CATEGORY: + bodyView.post(new Runnable() { + @Override + public void run() { + if (item.getItemId() == MENU_FUNCTION) { + showFunctions(bodyView); + } else { + showConstants(bodyView); + } + } + }); + return true; } - return true; + return false; } } } diff --git a/app/src/main/res/values/ids.xml b/app/src/main/res/values/ids.xml index 00356f42..394b51c6 100644 --- a/app/src/main/res/values/ids.xml +++ b/app/src/main/res/values/ids.xml @@ -70,6 +70,7 @@ +