Skip to content

Commit

Permalink
Fixed bugs for low dpi device
Browse files Browse the repository at this point in the history
  • Loading branch information
RainbowC0 committed Jul 10, 2024
1 parent 1e78c5a commit 989152c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 15 deletions.
14 changes: 7 additions & 7 deletions app/src/main/java/cn/rbc/termuc/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ protected void onCreate(Bundle savedInstanceState) {
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode==PackageManager.PERMISSION_GRANTED
&& grantResults[0]==PackageManager.PERMISSION_GRANTED
&& grantResults[1]==PackageManager.PERMISSION_GRANTED)
&& grantResults[0]==requestCode
&& grantResults[1]==requestCode)
refresh();
}

Expand All @@ -122,11 +122,11 @@ private void refresh() {
String[] list = this.pwd.list();
if (list == null)
list = new String[0];
if (!root.equals(this.pwd))
adp.add("..");
adp.addAll(list);
adp.sort(cmp);
adp.notifyDataSetChanged();
adp.addAll(list);
adp.sort(cmp);
if (!root.equals(this.pwd))
adp.insert("..", 0);
adp.notifyDataSetChanged();
}

@Override
Expand Down
9 changes: 8 additions & 1 deletion app/src/main/java/cn/rbc/termuc/TextEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class TextEditor extends FreeScrollingTextField{
private Context mContext;
private String _lastSelectFile;
private int _index;
//private int mSize = 14;
/*
private Handler handler = new Handler() {
@Override
Expand Down Expand Up @@ -59,7 +60,7 @@ private void init() {
setTypeface(Typeface.MONOSPACE);
DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
//设置字体大小
float size = TypedValue.applyDimension(2, BASE_TEXT_SIZE_PIXELS, dm);
float size = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16.f, dm);
setTextSize((int) size);
setShowLineNumbers(true);
setHighlightCurrentRow(true);
Expand Down Expand Up @@ -114,6 +115,12 @@ public boolean onKeyShortcut(int keyCode, KeyEvent event) {
case KeyEvent.KEYCODE_V:
paste();
return true;
case KeyEvent.KEYCODE_EQUALS:
setTextSize((int)(getTextSize()+HelperUtils.getDpi(mContext)));
return true;
case KeyEvent.KEYCODE_MINUS:
setTextSize((int)(getTextSize()-HelperUtils.getDpi(mContext)));
return true;
}
}
return super.onKeyShortcut(keyCode, event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@
import java.util.List;
import android.util.SparseIntArray;
import cn.rbc.codeeditor.util.*;
import android.view.*;
import android.widget.*;

/**
* A custom text view that uses a solid shaded caret (aka cursor) instead of a
Expand Down Expand Up @@ -214,7 +216,7 @@ public abstract class FreeScrollingTextField extends View implements Document.Te
}

//光标宽度
public final int mCursorWidth = 4;
public int mCursorWidth;
public TextFieldController mCtrlr; // the controller in MVC
public TextFieldInputConnection mInputConnection;
public OnRowChangedListener mRowListener;
Expand Down Expand Up @@ -244,6 +246,7 @@ public abstract class FreeScrollingTextField extends View implements Document.Te
private int mTopOffset, mLeftOffset;
private int mLineMaxWidth, xExtent;
private int mAlphaWidth, mSpaceWidth;
private int mSizeMax, mSizeMin;
private long mLastScroll;
private boolean isAutoCompeted = true; //代码提示
private boolean isShowLineNumbers = true;
Expand Down Expand Up @@ -398,8 +401,9 @@ public void scrollTo(int x, int y) {
}

public void setTextSize(int pix, float cx, float cy) {
if (pix <= 20 || pix >= 80 || pix == mTextPaint.getTextSize())
if (pix<mSizeMin || pix>mSizeMax || pix == (int)mTextPaint.getTextSize())
return;
//pix = Math.max(mSizeMin, Math.min(mSizeMax, pix));
float oldHeight = rowHeight();
float oldWidth = getCharAdvance('a');
mZoomFactor = pix / BASE_TEXT_SIZE_PIXELS;
Expand Down Expand Up @@ -459,6 +463,11 @@ private void initView(Context context) {
setFocusableInTouchMode(true);
setHapticFeedbackEnabled(true);
mColorScheme = new ColorSchemeLight();
mSpaceWidth = (int)mTextPaint.measureText(" ");
mCursorWidth = (int)(HelperUtils.getDpi(mContext)*1.5f);
android.util.DisplayMetrics dm = getResources().getDisplayMetrics();
mSizeMin = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 8.f, dm);
mSizeMax = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 32.f, dm);

mRowListener = new OnRowChangedListener() {
@Override
Expand All @@ -470,7 +479,6 @@ public void onRowChanged(int newRowIndex) {
selLis = new OnSelectionChangedListener() {
@Override
public void onSelectionChanged(boolean active, int selStart, int selEnd) {
// TODO: Implement this method
if (active)
mClipboardPanel.show();
else
Expand Down Expand Up @@ -622,7 +630,6 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
// TODO: Implement this method
if (changed) {
Rect rect = new Rect();
getWindowVisibleDisplayFrame(rect);
Expand Down Expand Up @@ -993,7 +1000,6 @@ private int drawSelectedText(Canvas canvas, char c, int paintX, int paintY) {
return advance;
}

//光标
private void drawCaret(Canvas canvas, int paintX, int paintY) {
int originalColor = mTextPaint.getColor();
mCaretX = paintX - mCursorWidth / 2;
Expand All @@ -1005,7 +1011,6 @@ private void drawCaret(Canvas canvas, int paintX, int paintY) {
mTextPaint.setColor(originalColor);
}

// 绘制行号
private void drawLineNum(Canvas canvas, String s, int paintX, int paintY) {
mLineBrush.setColor(mColorScheme.getColor(Colorable.NON_PRINTING_GLYPH));
canvas.drawText(s, paintX, paintY, mLineBrush);
Expand Down Expand Up @@ -2243,6 +2248,15 @@ && isPointInView((int) event.getX(), (int) event.getY()))
return true;
}

// TODO support mouse rolling
/*@Override
public boolean onGenericMotionEvent(MotionEvent event) {
if (event.isFromSource(InputDevice.SOURCE_CLASS_POINTER) && event.getAction()==MotionEvent.ACTION_SCROLL) {
HelperUtils.show(Toast.makeText(mContext, ""+mScroller.isFinished(), 1));
}
return super.onGenericMotionEvent(event);
}*/

private boolean isPointInView(int x, int y) {
return (x >= 0 && x < getWidth() &&
y >= 0 && y < getHeight());
Expand All @@ -2254,6 +2268,11 @@ protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyF
invalidateCaretRow();
}

@Override
public PointerIcon onResolvePointerIcon(MotionEvent event, int pointerIndex) {
return PointerIcon.getSystemIcon(mContext, PointerIcon.TYPE_TEXT);
}

/**
* Not public to allow access by {@link TouchNavigationMethod}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,8 @@ public boolean onDoubleTap(MotionEvent e) {
TextView tv = new TextView(ctx);
tv.setTextColor(0xffffffff);
tv.setText(errspan.msg);
ll.setPadding(40,40,40,40);
int pd = (int)(12*HelperUtils.getDpi(ctx)+.5f);
ll.setPadding(pd, pd, pd, pd);
ll.setBackgroundColor(ColorScheme.DIAG[errspan.severity]&0xf0ffffff);
ll.addView(tv);
t.setView(ll);
Expand Down

0 comments on commit 989152c

Please sign in to comment.