feat: light theme for new editor
This commit is contained in:
@@ -19,6 +19,8 @@ public class Pref {
|
||||
private static final String KEY_FIRST_SHOW_AD = "En, Today is 17.7.7, but, I'm still love you so....";
|
||||
private static final String KEY_LAST_SHOW_AD_MILLIS = "But... it seems that...you will not come back any more...";
|
||||
private static final String KEY_FLOATING_MENU_SHOWN = "17.10.28 I have idea of what you think...maybe...I'm overthinking...";
|
||||
private static final String KEY_EDITOR_THEME = "editor.theme";
|
||||
|
||||
private static SharedPreferences.OnSharedPreferenceChangeListener onSharedPreferenceChangeListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
|
||||
@Override
|
||||
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
||||
@@ -147,4 +149,12 @@ public class Pref {
|
||||
public static void setFloatingMenuShown(boolean checked) {
|
||||
def().edit().putBoolean(KEY_FLOATING_MENU_SHOWN, checked).apply();
|
||||
}
|
||||
|
||||
public static String getCurrentTheme() {
|
||||
return def().getString(KEY_EDITOR_THEME, null);
|
||||
}
|
||||
|
||||
public static void setCurrentTheme(String theme) {
|
||||
def().edit().putString(KEY_EDITOR_THEME, theme).apply();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,8 +12,8 @@ public class EditorColors {
|
||||
private String mEditorForeground;
|
||||
@SerializedName("editor.inactiveSelectionBackground")
|
||||
private String mEditorInactiveSelectionBackground;
|
||||
@SerializedName("editorIndentGuide.background")
|
||||
private String mEditorIndentGuideBackground;
|
||||
@SerializedName("lineNumber.foreground")
|
||||
private String mLineNumberForeground;
|
||||
@SerializedName("editor.selectionHighlightBackground")
|
||||
private String mEditorSelectionHighlightBackground;
|
||||
@SerializedName("imeBar.background")
|
||||
@@ -45,12 +45,12 @@ public class EditorColors {
|
||||
mEditorInactiveSelectionBackground = editorInactiveSelectionBackground;
|
||||
}
|
||||
|
||||
public String getEditorIndentGuideBackground() {
|
||||
return mEditorIndentGuideBackground;
|
||||
public String getLineNumberForeground() {
|
||||
return mLineNumberForeground;
|
||||
}
|
||||
|
||||
public void setEditorIndentGuideBackground(String editorIndentGuideBackground) {
|
||||
mEditorIndentGuideBackground = editorIndentGuideBackground;
|
||||
public void setLineNumberForeground(String lineNumberForeground) {
|
||||
mLineNumberForeground = lineNumberForeground;
|
||||
}
|
||||
|
||||
public String getEditorSelectionHighlightBackground() {
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
|
||||
package com.stardust.scriptdroid.model.editor;
|
||||
|
||||
import android.graphics.Color;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
@@ -11,8 +9,6 @@ import java.io.Reader;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.mozilla.javascript.Token.*;
|
||||
|
||||
public class EditorTheme {
|
||||
|
||||
private static Gson sGson;
|
||||
|
||||
@@ -16,6 +16,7 @@ import android.widget.FrameLayout;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.afollestad.materialdialogs.MaterialDialog;
|
||||
import com.stardust.autojs.engine.JavaScriptEngine;
|
||||
import com.stardust.autojs.execution.ScriptExecution;
|
||||
import com.stardust.pio.PFiles;
|
||||
@@ -34,6 +35,7 @@ import com.stardust.scriptdroid.ui.edit.editor.CodeEditor;
|
||||
import com.stardust.scriptdroid.ui.edit.keyboard.FunctionsKeyboardHelper;
|
||||
import com.stardust.scriptdroid.ui.edit.keyboard.FunctionsKeyboardView;
|
||||
import com.stardust.scriptdroid.ui.edit.theme.Theme;
|
||||
import com.stardust.scriptdroid.ui.edit.theme.Themes;
|
||||
import com.stardust.scriptdroid.ui.log.LogActivity_;
|
||||
import com.stardust.scriptdroid.ui.widget.EWebView;
|
||||
import com.stardust.scriptdroid.ui.widget.ToolbarMenuItem;
|
||||
@@ -46,6 +48,7 @@ import org.androidannotations.annotations.EViewGroup;
|
||||
import org.androidannotations.annotations.ViewById;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
import io.reactivex.Observable;
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
@@ -224,7 +227,9 @@ public class EditorView extends FrameLayout implements CodeCompletionBar.OnHintC
|
||||
setMenuItemStatus(R.id.save, false);
|
||||
mDocsWebView.getWebView().getSettings().setDisplayZoomControls(true);
|
||||
mDocsWebView.getWebView().loadUrl(Pref.getDocumentationUrl() + "index.html");
|
||||
|
||||
Themes.getCurrent(getContext()).
|
||||
observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(this::setTheme);
|
||||
}
|
||||
|
||||
private void setUpFunctionsKeyboard() {
|
||||
@@ -267,6 +272,7 @@ public class EditorView extends FrameLayout implements CodeCompletionBar.OnHintC
|
||||
mCodeCompletionBar.setTextColor(textColor);
|
||||
mSymbolBar.setTextColor(textColor);
|
||||
mShowFunctionsButton.setColorFilter(textColor);
|
||||
invalidate();
|
||||
}
|
||||
|
||||
public boolean onBackPressed() {
|
||||
@@ -371,7 +377,30 @@ public class EditorView extends FrameLayout implements CodeCompletionBar.OnHintC
|
||||
}
|
||||
|
||||
public void selectEditorTheme() {
|
||||
mEditor.setProgress(true);
|
||||
Themes.getAllThemes(getContext())
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(themes -> {
|
||||
mEditor.setProgress(false);
|
||||
selectEditorTheme(themes);
|
||||
});
|
||||
}
|
||||
|
||||
private void selectEditorTheme(List<Theme> themes) {
|
||||
int i = themes.indexOf(mEditorTheme);
|
||||
if (i < 0) {
|
||||
i = 0;
|
||||
}
|
||||
new MaterialDialog.Builder(getContext())
|
||||
.title(R.string.text_editor_theme)
|
||||
.items(themes)
|
||||
.itemsCallbackSingleChoice(i, (dialog, itemView, which, text) -> {
|
||||
setTheme(themes.get(which));
|
||||
Themes.setCurrent(themes.get(which).getName());
|
||||
return true;
|
||||
})
|
||||
.show();
|
||||
}
|
||||
|
||||
public CodeEditor getEditor() {
|
||||
|
||||
@@ -79,12 +79,11 @@ public class CodeEditText extends AppCompatEditText {
|
||||
if (mScrollView == null) {
|
||||
mScrollView = (HVScrollView) getParent();
|
||||
}
|
||||
// 根据行号计算左边距padding
|
||||
// 根据行号计算左边距padding 留出绘制行号的空间
|
||||
String max = Integer.toString(getLineCount());
|
||||
float lineNumberSize = getPaint().measureText(max) + 20;
|
||||
if (getPaddingLeft() != lineNumberSize) {
|
||||
setPadding((int) lineNumberSize, 0, 0, 0);
|
||||
invalidate();
|
||||
float gutterWidth = getPaint().measureText(max) + 20;
|
||||
if (getPaddingLeft() != gutterWidth) {
|
||||
setPadding((int) gutterWidth, 0, 0, 0);
|
||||
}
|
||||
super.onDraw(canvas);
|
||||
// 画文字
|
||||
@@ -92,7 +91,6 @@ public class CodeEditText extends AppCompatEditText {
|
||||
canvas.translate(0, getExtendedPaddingTop());
|
||||
drawText(canvas);
|
||||
canvas.restore();
|
||||
|
||||
}
|
||||
|
||||
// 绘制文本着色
|
||||
|
||||
@@ -138,9 +138,11 @@ public class CodeEditor extends HVScrollView {
|
||||
|
||||
public void setTheme(Theme theme) {
|
||||
mTheme = theme;
|
||||
mJavaScriptHighlighter.setTheme(theme);
|
||||
setBackgroundColor(mTheme.getBackgroundColor());
|
||||
|
||||
mJavaScriptHighlighter.setTheme(theme);
|
||||
mJavaScriptHighlighter.updateTokens(mCodeEditText.getText().toString());
|
||||
mCodeEditText.setTheme(mTheme);
|
||||
invalidate();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -81,7 +81,7 @@ public class JavaScriptHighlighter implements SimpleTextWatcher.AfterTextChanged
|
||||
mTheme = theme;
|
||||
}
|
||||
|
||||
private void updateTokens(String sourceString) {
|
||||
public void updateTokens(String sourceString) {
|
||||
final int id = mRunningHighlighterId.incrementAndGet();
|
||||
mExecutorService.execute(() -> {
|
||||
try {
|
||||
|
||||
@@ -1,22 +1,14 @@
|
||||
package com.stardust.scriptdroid.ui.edit.theme;
|
||||
|
||||
import android.graphics.Color;
|
||||
import android.util.SparseArray;
|
||||
import android.util.SparseIntArray;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.stardust.scriptdroid.model.editor.EditorTheme;
|
||||
import com.stardust.scriptdroid.model.editor.TokenColor;
|
||||
|
||||
import org.mozilla.javascript.Context;
|
||||
import org.mozilla.javascript.Token;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.mozilla.javascript.Token.*;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2018/2/16.
|
||||
@@ -25,19 +17,21 @@ import static org.mozilla.javascript.Token.*;
|
||||
public class Theme {
|
||||
|
||||
|
||||
private int mBackgroundColor;
|
||||
private int mForegroundColor;
|
||||
private int mLineNumberColor;
|
||||
private int mBackgroundColor = Color.WHITE;
|
||||
private int mForegroundColor = Color.BLACK;
|
||||
private int mLineNumberColor = Color.GRAY;
|
||||
private SparseIntArray mTokenColors = new SparseIntArray();
|
||||
private int mImeBarBackgroundColor;
|
||||
private int mImeBarForegroundColor;
|
||||
private int mImeBarBackgroundColor = 0xDDFFFFFF;
|
||||
private int mImeBarForegroundColor = Color.WHITE;
|
||||
private EditorTheme mEditorTheme;
|
||||
|
||||
public Theme(EditorTheme theme) {
|
||||
mBackgroundColor = Color.parseColor(theme.getEditorColors().getEditorBackground());
|
||||
mForegroundColor = Color.parseColor(theme.getEditorColors().getEditorForeground());
|
||||
mLineNumberColor = Color.parseColor(theme.getEditorColors().getEditorIndentGuideBackground());
|
||||
mImeBarBackgroundColor = Color.parseColor(theme.getEditorColors().getImeBackgroundColor());
|
||||
mImeBarForegroundColor = Color.parseColor(theme.getEditorColors().getImeForegroundColor());
|
||||
mEditorTheme = theme;
|
||||
mBackgroundColor = parseColor(theme.getEditorColors().getEditorBackground(), mBackgroundColor);
|
||||
mForegroundColor = parseColor(theme.getEditorColors().getEditorForeground(), mForegroundColor);
|
||||
mLineNumberColor = parseColor(theme.getEditorColors().getLineNumberForeground(), mLineNumberColor);
|
||||
mImeBarBackgroundColor = parseColor(theme.getEditorColors().getImeBackgroundColor(), mImeBarBackgroundColor);
|
||||
mImeBarForegroundColor = parseColor(theme.getEditorColors().getImeForegroundColor(), mImeBarForegroundColor);
|
||||
|
||||
for (TokenColor tokenColor : theme.getTokenColors()) {
|
||||
String foregroundStr = tokenColor.getSettings().getForeground();
|
||||
@@ -50,6 +44,20 @@ public class Theme {
|
||||
}
|
||||
}
|
||||
|
||||
private int parseColor(String color, int defaultValue) {
|
||||
if (color == null)
|
||||
return defaultValue;
|
||||
try {
|
||||
return Color.parseColor(color);
|
||||
} catch (Exception ignored) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return mEditorTheme.getName();
|
||||
}
|
||||
|
||||
private void setTokenColor(String scope, int foreground) {
|
||||
for (int token : TokenMapping.getTokensForScope(scope)) {
|
||||
mTokenColors.put(token, foreground);
|
||||
@@ -111,4 +119,19 @@ public class Theme {
|
||||
public void setImeBarForegroundColor(int imeBarForegroundColor) {
|
||||
mImeBarForegroundColor = imeBarForegroundColor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
Theme theme = (Theme) o;
|
||||
|
||||
return mEditorTheme.getName() != null ? mEditorTheme.getName().equals(theme.mEditorTheme.getName()) : theme.mEditorTheme.getName() == null;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return getName();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
package com.stardust.scriptdroid.ui.edit.theme;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.stardust.pio.UncheckedIOException;
|
||||
import com.stardust.scriptdroid.Pref;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import io.reactivex.Observable;
|
||||
import io.reactivex.Single;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
import io.reactivex.subjects.PublishSubject;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2018/2/22.
|
||||
*/
|
||||
|
||||
public class Themes {
|
||||
|
||||
|
||||
private static final String ASSETS_THEMES_PATH = "editor/theme";
|
||||
private static final String DEFAULT_THEME = "Quiet Light";
|
||||
|
||||
private static List<Theme> sThemes;
|
||||
private static Theme sDefaultTheme;
|
||||
|
||||
public static Observable<List<Theme>> getAllThemes(Context context) {
|
||||
if (sThemes != null) {
|
||||
return Observable.just(sThemes);
|
||||
}
|
||||
PublishSubject<List<Theme>> subject = PublishSubject.create();
|
||||
getAllThemesInner(context)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.subscribe(themes -> {
|
||||
setThemes(themes);
|
||||
subject.onNext(sThemes);
|
||||
subject.onComplete();
|
||||
}, Throwable::printStackTrace);
|
||||
return subject;
|
||||
}
|
||||
|
||||
public static Observable<Theme> getDefault(Context context) {
|
||||
if (sDefaultTheme != null)
|
||||
return Observable.just(sDefaultTheme);
|
||||
return getAllThemes(context)
|
||||
.map(themes -> sDefaultTheme);
|
||||
}
|
||||
|
||||
private synchronized static void setThemes(List<Theme> themes) {
|
||||
if (sThemes != null)
|
||||
return;
|
||||
sThemes = Collections.unmodifiableList(themes);
|
||||
for (Theme theme : sThemes) {
|
||||
if (DEFAULT_THEME.equals(theme.getName())) {
|
||||
sDefaultTheme = theme;
|
||||
return;
|
||||
}
|
||||
}
|
||||
sDefaultTheme = sThemes.get(0);
|
||||
}
|
||||
|
||||
private static Observable<List<Theme>> getAllThemesInner(Context context) {
|
||||
if (sThemes != null) {
|
||||
return Observable.just(sThemes);
|
||||
}
|
||||
try {
|
||||
return Observable.fromArray(context.getAssets().list(ASSETS_THEMES_PATH))
|
||||
.map(file -> context.getAssets().open(ASSETS_THEMES_PATH + "/" + file))
|
||||
.map(stream -> Theme.fromJson(new InputStreamReader(stream)))
|
||||
.collectInto((List<Theme>) new ArrayList<Theme>(), List::add)
|
||||
.toObservable();
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static Observable<Theme> getCurrent(Context context) {
|
||||
String currentTheme = Pref.getCurrentTheme();
|
||||
if (currentTheme == null)
|
||||
return getDefault(context);
|
||||
return getAllThemes(context)
|
||||
.map(themes -> {
|
||||
for (Theme theme : themes) {
|
||||
if (currentTheme.equals(theme.getName()))
|
||||
return theme;
|
||||
}
|
||||
return themes.get(0);
|
||||
});
|
||||
}
|
||||
|
||||
public static void setCurrent(String name) {
|
||||
Pref.setCurrentTheme(name);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user