Use editor string's spans only if main and widget theme are both light or dark

This commit is contained in:
serso 2016-01-06 23:10:31 +01:00
parent 926909a62d
commit 25c83bac94
3 changed files with 33 additions and 12 deletions

View File

@ -34,6 +34,9 @@ import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import org.solovyev.android.UiThreadExecutor;
import org.solovyev.android.Views;
import org.solovyev.android.calculator.ga.Ga;
@ -298,4 +301,11 @@ public final class App {
public static String toColorString(@ColorInt int color) {
return Integer.toHexString(color).substring(2);
}
@NonNull
public static SpannableString colorString(@Nonnull String s, int color) {
final SpannableString spannable = new SpannableString(s);
spannable.setSpan(new ForegroundColorSpan(color), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return spannable;
}
}

View File

@ -160,7 +160,7 @@ public final class Preferences {
@Nullable
private final Gui.Theme appTheme;
private final boolean light;
public final boolean light;
@Nonnull
private final Map<Gui.Theme, SimpleTheme> cache = new EnumMap<>(Gui.Theme.class);
@ -303,22 +303,28 @@ public final class Preferences {
metro_purple_theme(R.style.cpp_metro_purple_theme),
metro_green_theme(R.style.cpp_metro_green_theme),
material_theme(R.style.Cpp_Theme_Material),
material_light_theme(R.style.Cpp_Theme_Material_Light, R.style.Cpp_Theme_Wizard_Light, R.style.Cpp_Theme_Dialog_Material_Light),;
material_light_theme(R.style.Cpp_Theme_Material_Light, R.style.Cpp_Theme_Wizard_Light, R.style.Cpp_Theme_Dialog_Material_Light, true),;
private static final SparseArray<TextColor> textColors = new SparseArray<>();
private final int themeId;
private final int wizardThemeId;
private final int dialogThemeId;
public final boolean light;
Theme(@StyleRes int themeId) {
this(themeId, R.style.Cpp_Theme_Wizard, R.style.Cpp_Theme_Dialog_Material);
}
Theme(@StyleRes int themeId, @StyleRes int wizardThemeId, int dialogThemeId) {
this(themeId, wizardThemeId, dialogThemeId, false);
}
Theme(@StyleRes int themeId, @StyleRes int wizardThemeId, int dialogThemeId, boolean light) {
this.themeId = themeId;
this.wizardThemeId = wizardThemeId;
this.dialogThemeId = dialogThemeId;
this.light = light;
}
public int getThemeId() {
@ -356,7 +362,7 @@ public final class Preferences {
}
}
public static enum Layout {
public enum Layout {
main_calculator(R.layout.main_calculator, R.string.p_layout_calculator, true),
main_calculator_mobile(R.layout.main_calculator_mobile, R.string.p_layout_calculator_mobile, false),

View File

@ -32,9 +32,9 @@ import android.content.Intent;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.text.*;
import android.text.style.ForegroundColorSpan;
import android.widget.RemoteViews;
import org.solovyev.android.Views;
import org.solovyev.android.calculator.*;
@ -76,10 +76,7 @@ public class CalculatorWidget extends AppWidgetProvider {
@Nonnull
private SpannedString initCursorString(@Nonnull Context context) {
if (cursorString == null) {
final SpannableString spannable = new SpannableString("|");
final int cursorColor = ContextCompat.getColor(context, R.color.cpp_widget_cursor);
spannable.setSpan(new ForegroundColorSpan(cursorColor), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
cursorString = new SpannedString(spannable);
cursorString = new SpannedString(App.colorString("|", ContextCompat.getColor(context, R.color.cpp_widget_cursor)));
}
return cursorString;
}
@ -124,7 +121,7 @@ public class CalculatorWidget extends AppWidgetProvider {
}
}
updateEditorState(context, views, editorState);
updateEditorState(context, views, editorState, theme);
updateDisplayState(context, views, displayState, theme);
views.setTextViewText(R.id.cpp_button_multiplication, Locator.getInstance().getEngine().getMultiplicationSign());
@ -198,20 +195,28 @@ public class CalculatorWidget extends AppWidgetProvider {
views.setTextColor(R.id.calculator_display, ContextCompat.getColor(context, theme.getDisplayTextColor(error)));
}
private void updateEditorState(@Nonnull Context context, @Nonnull RemoteViews views, @Nonnull CalculatorEditorViewState editorState) {
private void updateEditorState(@Nonnull Context context, @Nonnull RemoteViews views, @Nonnull CalculatorEditorViewState editorState, @Nonnull SimpleTheme theme) {
final CharSequence text = editorState.getTextAsCharSequence();
final boolean unspan = App.getTheme().light != theme.light;
CharSequence newText = text;
int selection = editorState.getSelection();
if (selection >= 0 && selection <= text.length()) {
// inject cursor
final SpannableStringBuilder result = new SpannableStringBuilder();
result.append(text.subSequence(0, selection));
final CharSequence beforeCursor = text.subSequence(0, selection);
result.append(unspan ? unspan(beforeCursor) : beforeCursor);
result.append(getCursorString(context));
result.append(text.subSequence(selection, text.length()));
final CharSequence afterCursor = text.subSequence(selection, text.length());
result.append(unspan ? unspan(afterCursor) : afterCursor);
newText = result;
}
Locator.getInstance().getNotifier().showDebugMessage(TAG, "New editor state: " + text);
views.setTextViewText(R.id.calculator_editor, newText);
}
@NonNull
private String unspan(@Nonnull CharSequence spannable) {
return spannable.toString();
}
}