Fixed issues with selection

This commit is contained in:
serso 2016-02-22 17:37:17 +01:00
parent 71c4f66300
commit 16ec24e1b8
3 changed files with 21 additions and 11 deletions

View File

@ -22,6 +22,8 @@
package org.solovyev.android.calculator;
import static java.lang.Math.min;
import android.content.SharedPreferences;
import com.squareup.otto.Bus;
@ -36,8 +38,6 @@ import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Singleton;
import static java.lang.Math.min;
@Singleton
public class Editor {
@ -167,7 +167,8 @@ public class Editor {
if (selection <= 0 || text.length() <= 0 || selection > text.length()) {
return state;
}
final String newText = text.substring(0, selection - 1) + text.substring(selection, text.length());
final String newText = text.substring(0, selection - 1) + text.substring(selection,
text.length());
return onTextChanged(EditorState.create(newText, selection - 1));
}
@ -215,6 +216,9 @@ public class Editor {
@Nonnull
public EditorState setSelection(int selection) {
Check.isMainThread();
if (state.selection == selection) {
return state;
}
return onSelectionChanged(EditorState.forNewSelection(state, clamp(selection, state.text)));
}

View File

@ -26,6 +26,8 @@ import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.text.Editable;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.ContextMenu;
@ -95,8 +97,8 @@ public class EditorView extends EditTextCompat {
} else {
setText(state.text, BufferType.EDITABLE);
}
setSelection(Editor.clamp(state.selection, length()));
reportChanges = true;
setSelection(Editor.clamp(state.selection, length()));
}
private boolean isFloatingCalculator() {
@ -117,7 +119,6 @@ public class EditorView extends EditTextCompat {
return;
}
if (editor == null) {
Check.shouldNotHappen();
return;
}
editor.setSelection(start);

View File

@ -5,6 +5,7 @@ import static android.graphics.Matrix.MSCALE_Y;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewTreeObserver;
@ -52,7 +53,6 @@ public class Adjuster {
private final TextView view;
private final float percentage;
private final float minTextSizePxs;
private int lastHeight;
public TextViewAdjuster(@NonNull TextView view, float percentage, float minTextSizePxs) {
this.view = view;
@ -63,10 +63,13 @@ public class Adjuster {
@Override
public boolean onPreDraw() {
final int height = view.getHeight();
if (lastHeight == height || height <= 0) {
if (!ViewCompat.isLaidOut(view) || height <= 0) {
return true;
}
lastHeight = height;
final ViewTreeObserver treeObserver = getTreeObserver(view);
if (treeObserver != null) {
treeObserver.removeOnPreDrawListener(this);
}
final float oldTextSize = Math.round(view.getTextSize());
final float newTextSize = Math.max(minTextSizePxs, Math.round(height * percentage));
if (oldTextSize == newTextSize) {
@ -81,7 +84,6 @@ public class Adjuster {
@NonNull
private final ImageView view;
private final float percentage;
private int lastHeight;
public ImageViewAdjuster(@NonNull ImageView view, float percentage) {
this.view = view;
@ -91,10 +93,13 @@ public class Adjuster {
@Override
public boolean onPreDraw() {
final int height = view.getHeight();
if (lastHeight == height || height <= 0) {
if (!ViewCompat.isLaidOut(view) || height <= 0) {
return true;
}
lastHeight = height;
final ViewTreeObserver treeObserver = getTreeObserver(view);
if (treeObserver != null) {
treeObserver.removeOnPreDrawListener(this);
}
final Drawable d = view.getDrawable();
if (d == null) {
return true;