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; package org.solovyev.android.calculator;
import static java.lang.Math.min;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import com.squareup.otto.Bus; import com.squareup.otto.Bus;
@ -36,8 +38,6 @@ import javax.annotation.Nullable;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import static java.lang.Math.min;
@Singleton @Singleton
public class Editor { public class Editor {
@ -167,7 +167,8 @@ public class Editor {
if (selection <= 0 || text.length() <= 0 || selection > text.length()) { if (selection <= 0 || text.length() <= 0 || selection > text.length()) {
return state; 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)); return onTextChanged(EditorState.create(newText, selection - 1));
} }
@ -215,6 +216,9 @@ public class Editor {
@Nonnull @Nonnull
public EditorState setSelection(int selection) { public EditorState setSelection(int selection) {
Check.isMainThread(); Check.isMainThread();
if (state.selection == selection) {
return state;
}
return onSelectionChanged(EditorState.forNewSelection(state, clamp(selection, state.text))); 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.content.Context;
import android.os.Build; import android.os.Build;
import android.text.Editable; import android.text.Editable;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.ContextMenu; import android.view.ContextMenu;
@ -95,8 +97,8 @@ public class EditorView extends EditTextCompat {
} else { } else {
setText(state.text, BufferType.EDITABLE); setText(state.text, BufferType.EDITABLE);
} }
setSelection(Editor.clamp(state.selection, length()));
reportChanges = true; reportChanges = true;
setSelection(Editor.clamp(state.selection, length()));
} }
private boolean isFloatingCalculator() { private boolean isFloatingCalculator() {
@ -117,7 +119,6 @@ public class EditorView extends EditTextCompat {
return; return;
} }
if (editor == null) { if (editor == null) {
Check.shouldNotHappen();
return; return;
} }
editor.setSelection(start); editor.setSelection(start);

View File

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