Skip to content

Commit

Permalink
feat: Match ace editor monokai theme with app colors
Browse files Browse the repository at this point in the history
  • Loading branch information
SyntaxGalaxy committed Sep 17, 2023
1 parent a923d46 commit c453df6
Show file tree
Hide file tree
Showing 11 changed files with 286 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,5 @@ dependencies {
implementation project(path:':markdown-viewer')
implementation project(path:':editor')
implementation project(path:':treeview')
implementation project(path: ':common')
implementation project(path:':common')
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package android.code.editor.ui.activities;

import android.code.editor.R;
import android.code.editor.common.utils.ColorUtils;
import android.code.editor.common.utils.FileUtils;
import android.code.editor.databinding.ActivityCodeEditorBinding;
import android.code.editor.handlers.FileTypeHandler;
Expand Down Expand Up @@ -48,6 +49,7 @@
import com.unnamed.b.atv.model.TreeNode;
import com.unnamed.b.atv.view.AndroidTreeView;
import com.unnamed.b.atv.view.TreeNodeWrapperView;
import editor.tsd.editors.ace.AceEditorColors;
import editor.tsd.tools.EditorListeners;
import editor.tsd.widget.CodeEditorLayout;
import java.io.File;
Expand All @@ -68,6 +70,7 @@ public class CodeEditorActivity extends BaseActivity {
public RecyclerView fileTab;
public FileTabAdapter adapter;
public ArrayList<FileTabDataItem> fileTabData = new ArrayList<FileTabDataItem>();
public AceEditorColors aceColors;

public ActivityCodeEditorBinding binding;

Expand Down Expand Up @@ -161,6 +164,24 @@ public void onConfigurationChanged(Configuration arg0) {
}

public void initActivity() {
aceColors = new AceEditorColors();
aceColors.setEditorBackground(
ColorUtils.materialIntToHexColor(this, com.google.android.material.R.attr.colorSurface));
aceColors.setActiveLineColor(
ColorUtils.materialIntToHexColor(
this, com.google.android.material.R.attr.colorSurfaceVariant));
aceColors.setGutterActiveLineColor(
ColorUtils.materialIntToHexColor(
this, com.google.android.material.R.attr.colorSurfaceVariant));
aceColors.setGutterBackground(
ColorUtils.materialIntToHexColor(
this, com.google.android.material.R.attr.colorSurface));
aceColors.setGutterTextColor(
ColorUtils.materialIntToHexColor(
this, com.google.android.material.R.attr.colorOnSurface));

aceColors.apply(this);

fileTab = binding.fileTab;
Toolbar toolbar = binding.toolbar;
setSupportActionBar(toolbar);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@
import android.provider.Settings;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.CallSuper;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.firebase.analytics.FirebaseAnalytics;
import editor.tsd.editors.AceEditor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MainActivity extends BaseActivity {

Expand Down Expand Up @@ -69,6 +71,11 @@ public void onClick(View arg0) {
bundle.putString(FirebaseAnalytics.Param.SCREEN_NAME, "MainActivity");
bundle.putString(FirebaseAnalytics.Param.SCREEN_CLASS, "MainActivity");
mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.SCREEN_VIEW, bundle);
ExecutorService exec = Executors.newSingleThreadExecutor();
exec.execute(
() -> {
AceEditor.install(this);
});
}

public static boolean isStoagePermissionGranted(Context context) {
Expand Down
1 change: 1 addition & 0 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ android {
}

dependencies {
implementation 'com.google.android.material:material:1.8.0'
implementation 'com.google.code.gson:gson:2.8.7'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package android.code.editor.common.utils;

import android.content.Context;
import android.content.res.AssetManager;

public class AssetsManager {
private Context myContext;

public AssetsManager(Context c) {
myContext = c;
}

public void saveFile(String path, String pathTo) {
copyFile(path, pathTo);
}

public void saveFolder(String path, String pathTo) {
copyAssets(path, pathTo);
}

private void copyAssets(final String _folder, final String _to) {
AssetManager assetManager = myContext.getAssets();
String[] files = null;
try {
files = assetManager.list(_folder);
} catch (java.io.IOException e) {
}
if (files != null)
for (String filename : files) {
java.io.InputStream in = null;
java.io.OutputStream out = null;
try {
in = assetManager.open(_folder + "/" + filename);
if (!new java.io.File(_to).exists()) {
new java.io.File(_to).mkdir();

java.io.File outFile = new java.io.File(_to, filename);
if (!(outFile.exists())) {
out = new java.io.FileOutputStream(outFile);
copyFile(in, out);
}

} else {

java.io.File outFile = new java.io.File(_to, filename);
if (!(outFile.exists())) {
out = new java.io.FileOutputStream(outFile);
copyFile(in, out);
}
}
} catch (java.io.IOException e) {
} finally {
if (in != null) {
try {
in.close();
} catch (java.io.IOException e) {
}
}
if (out != null) {
try {
out.close();
} catch (java.io.IOException e) {
}
}
}
}
}

private void copyFile(java.io.InputStream in, java.io.OutputStream out)
throws java.io.IOException {
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
}

private void copyFile(String filename, String outPath) {
AssetManager assetManager = myContext.getAssets();

java.io.InputStream in;
java.io.OutputStream out;
try {
in = assetManager.open(filename);
String newFileName = outPath + "/" + filename;
out = new java.io.FileOutputStream(newFileName);

byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
out.flush();
out.close();
} catch (Exception e) {
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package android.code.editor.common.utils;

import android.content.Context;
import com.google.android.material.color.MaterialColors;

public class ColorUtils {
public static String materialIntToHexColor(Context context, int res) {
return String.format("#%06X", (0xFFFFFF & MaterialColors.getColor(context, res, "#000000")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

package android.code.editor.common.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.net.Uri;
import java.io.BufferedReader;
Expand Down Expand Up @@ -106,6 +109,18 @@ public static boolean ifFileFormatIsEqualTo(String path, String format) {
}
}

public static String getDataDir(Context context) {
PackageManager pm = context.getPackageManager();
String packageName = context.getPackageName();
PackageInfo packageInfo;
try {
packageInfo = pm.getPackageInfo(packageName, 0);
return packageInfo.applicationInfo.dataDir;
} catch (PackageManager.NameNotFoundException e) {
return "";
}
}

public static String getPathFormat(String path) {
return path.substring(path.lastIndexOf(".") + 1, path.length());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
.ace-monokai {
/* Background */
/* Orginal Background */
/* background-color: #272822; */
background-color: ace_background;
/* Text color */
color: #F8F8F2;
}

.ace-monokai .ace_gutter {
/* line number background */
/* background: #2F3129; */
background: ace_gutter_background;
/* line number color */
/* color: #8F908A; */
color: ace_gutter_text_color;
}

.ace-monokai .ace_marker-layer .ace_active-line {
/* Active line */
/* background: #202020; */
background: ace_active_line;
}

.ace-monokai .ace_gutter-active-line {
/* Side bar active line color */
/* background-color: #272727; */
background-color: ace_gutter_active_line;
}
3 changes: 3 additions & 0 deletions editor/src/main/assets/Editor/Ace-Editor/AceEditor/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
<script src="js/ace.js" type="text/javascript" charset="utf-8"></script>
<script src="js/ext-language_tools.js"></script>
<title>CodeEditor</title>

<!-- themes -->
<link rel="stylesheet" href="css/themes/monokai.css">
</head>

<body>
Expand Down
33 changes: 31 additions & 2 deletions editor/src/main/java/editor/tsd/editors/AceEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package editor.tsd.editors;

import android.code.editor.common.utils.AssetsManager;
import android.code.editor.common.utils.FileUtils;
import android.content.Context;
import android.view.ScaleGestureDetector;
import android.view.ViewGroup;
Expand All @@ -27,6 +29,7 @@
import editor.tsd.tools.Language;
import editor.tsd.tools.Themes;
import editor.tsd.widget.CodeEditorLayout;
import java.io.File;

public class AceEditor implements Editor, ScaleGestureDetector.OnScaleGestureListener {
public Context context;
Expand All @@ -35,6 +38,7 @@ public class AceEditor implements Editor, ScaleGestureDetector.OnScaleGestureLis
public float fontSize = 8;
private ScaleGestureDetector scaleGestureDetector;
private EditorListeners listener;
public static final String AceEditorPath = File.separator + "AceEditor";

public AceEditor(Context c) {
context = c;
Expand Down Expand Up @@ -65,7 +69,12 @@ public AceEditor(Context c) {
aceEditor.addJavascriptInterface(aceJSInterface, "aceEditor");

// load editor file
aceEditor.loadUrl("file:///android_asset/Editor/Ace-Editor/AceEditor/index.html");
aceEditor.loadUrl(
"file://"
.concat(FileUtils.getDataDir(context))
.concat(AceEditorPath)
.concat(File.separator)
.concat("index.html"));

scaleGestureDetector = new ScaleGestureDetector(context, this);

Expand Down Expand Up @@ -173,7 +182,7 @@ public void setLanguageMode(String LanguageMode) {
aceJSInterface.languageMode = "java";
break;
case Language.Kt:
aceJSInterface.languageMode = "kotlin";
aceJSInterface.languageMode = "kotlin";
break;
case Language.XML:
aceJSInterface.languageMode = "xml";
Expand Down Expand Up @@ -245,4 +254,24 @@ public void moveCursorVertically(int steps) {
}
}
}

public static void install(Context context) {
save_assets_folder(
context,
"Editor/Ace-Editor/AceEditor",
FileUtils.getDataDir(context).concat(AceEditorPath));
save_assets_folder(
context,
"Editor/Ace-Editor/AceEditor/js",
FileUtils.getDataDir(context).concat(AceEditorPath).concat("/js"));
save_assets_folder(
context,
"Editor/Ace-Editor/AceEditor/js/snippets",
FileUtils.getDataDir(context).concat(AceEditorPath).concat("/js/snippets"));
}

public static void save_assets_folder(
final Context context, final String _path, final String _save_path) {
new AssetsManager(context).saveFolder(_path, _save_path);
}
}
Loading

0 comments on commit c453df6

Please sign in to comment.