6.6.2 - Alpha5 - 代码编辑器标题文字支持字体大小自适应

This commit is contained in:
SuperMonster003
2025-03-24 19:51:24 +08:00
parent 2235cba860
commit fb93e169cb
8 changed files with 212 additions and 100 deletions

View File

@@ -27,6 +27,7 @@
"应用内页面导航栏设置为透明或半透明以增强视觉体验",
"UI 模式状态栏及导航栏默认为 `md_grey_50` 色值且设置为亮色模式",
"长按应用主页 \"文件\" 标签可切换浮动按钮可见状态",
"代码编辑器标题文字支持字体大小自适应",
"日志页面浮动按钮可见状态与列表滚动操作联动",
"脚本项目配置文件 project.json 支持更多打包选项 _[`issue #305`](http://issues.autojs6.com/305)_ _[`issue #306`](http://issues.autojs6.com/306)_",
"脚本项目配置文件 project.json 支持选项名称宽松匹配及别名兼容",

View File

@@ -6,10 +6,16 @@ import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.text.Layout
import android.text.StaticLayout
import android.text.TextPaint
import android.util.Log
import android.util.TypedValue
import android.view.ActionMode
import android.view.Menu
import android.view.MenuItem
import android.view.ViewTreeObserver
import android.widget.TextView
import androidx.appcompat.widget.Toolbar
import com.afollestad.materialdialogs.MaterialDialog
import io.reactivex.Observable
@@ -22,10 +28,11 @@ import org.autojs.autojs.core.permission.PermissionRequestProxyActivity
import org.autojs.autojs.core.permission.RequestPermissionCallbacks
import org.autojs.autojs.pio.PFiles
import org.autojs.autojs.storage.file.TmpScriptFiles
import org.autojs.autojs.theme.widget.ThemeColorToolbar
import org.autojs.autojs.ui.BaseActivity
import org.autojs.autojs.ui.main.MainActivity
import org.autojs.autojs.util.Observers
import org.autojs.autojs.util.ViewUtils
import org.autojs.autojs.util.ViewUtils.setMenuIconsColorByThemeColorLuminance
import org.autojs.autojs6.R
import org.autojs.autojs6.databinding.ActivityEditBinding
import java.io.File
@@ -37,7 +44,7 @@ import java.io.IOException
*/
open class EditActivity : BaseActivity(), DelegateHost, PermissionRequestProxyActivity {
private var mToolbar: Toolbar? = null
private var mToolbar: ThemeColorToolbar? = null
private val mMediator = OnActivityResultDelegate.Mediator()
private lateinit var mEditorView: EditorView
@@ -51,7 +58,9 @@ open class EditActivity : BaseActivity(), DelegateHost, PermissionRequestProxyAc
super.onCreate(savedInstanceState)
val binding = ActivityEditBinding.inflate(layoutInflater).also { setContentView(it.root) }
mToolbar = findViewById(R.id.toolbar)
mToolbar = findViewById<ThemeColorToolbar>(R.id.toolbar).apply {
setTitleTextAppearance(this@EditActivity, R.style.TextAppearanceEditorTitle)
}
mEditorView = binding.editorView.apply {
handleIntent(intent)
.observeOn(AndroidSchedulers.mainThread())
@@ -62,12 +71,97 @@ open class EditActivity : BaseActivity(), DelegateHost, PermissionRequestProxyAc
setUpToolbar()
}
override fun onWindowStartingActionMode(callback: ActionMode.Callback): ActionMode? {
return super.onWindowStartingActionMode(callback)
}
// @Created by JetBrains AI Assistant on Mar 24, 2025.
private fun adjustTitleTextView(textView: TextView) {
textView.post {
// 可用宽度, 排除内边距
val availableWidth = textView.width - textView.paddingLeft - textView.paddingRight
if (availableWidth <= 0) return@post
override fun onWindowStartingActionMode(callback: ActionMode.Callback, type: Int): ActionMode? {
return super.onWindowStartingActionMode(callback, type)
val textStr = textView.text.toString()
val isNonAscii = textStr.any { it.code >= 0x80 }
val paint: TextPaint = textView.paint
val originTextSizePx = textView.textSize
val step = resources.getDimension(R.dimen.editor_title_text_size_step)
/* ---------- 尝试一行显示 (单行) ---------- */
textView.isSingleLine = true
textView.maxLines = 1
var oneLineTextSizePx = originTextSizePx
val minOneLineSizePx = resources.getDimension(R.dimen.editor_title_min_text_size_single_line)
// 测量一行文字宽度
paint.textSize = oneLineTextSizePx
var measuredWidth = paint.measureText(textStr)
// 当文字宽度超出可用宽度并且字号还高于下限的时候逐步降低字号
while (measuredWidth > availableWidth && oneLineTextSizePx > minOneLineSizePx) {
oneLineTextSizePx -= step
paint.textSize = oneLineTextSizePx
measuredWidth = paint.measureText(textStr)
}
// 如果一行能够显示文字, 则直接应用修改
if (measuredWidth <= availableWidth) {
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, oneLineTextSizePx)
return@post
}
/* ---------- 切换为两行显示 ---------- */
textView.isSingleLine = false
textView.maxLines = 2
// 重置字号
var twoLineTextSize = when (isNonAscii) {
true -> resources.getDimension(R.dimen.editor_title_text_size_double_line_non_ascii)
else -> resources.getDimension(R.dimen.editor_title_text_size_double_line_ascii)
}
val minTwoLineSize = resources.getDimension(R.dimen.editor_title_min_text_size_double_line)
paint.textSize = twoLineTextSize
// 检查两行显示是否足够: 利用 StaticLayout 测量文字显示效果
fun createStaticLayout(textSize: Float): StaticLayout {
paint.textSize = textSize
return StaticLayout.Builder
.obtain(textStr, 0, textStr.length, paint, availableWidth)
.setAlignment(Layout.Alignment.ALIGN_NORMAL)
.setLineSpacing(0f, 1f)
.build()
}
var layout = createStaticLayout(twoLineTextSize)
// 当行数超出了两行且字号尚未到达最低要求, 则逐步降低字号
while (layout.lineCount > 2 && twoLineTextSize > minTwoLineSize) {
twoLineTextSize -= step
layout = createStaticLayout(twoLineTextSize)
}
if (layout.lineCount <= 2) {
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, twoLineTextSize)
return@post
}
/* ---------- 切换为三行显示 ---------- */
textView.maxLines = 3
var threeLineSize = when (isNonAscii) {
true -> resources.getDimension(R.dimen.editor_title_text_size_triple_line_non_ascii)
else -> resources.getDimension(R.dimen.editor_title_text_size_triple_line_ascii)
}
val minThreeLineSize = resources.getDimension(R.dimen.editor_title_min_text_size_triple_line)
paint.textSize = threeLineSize
layout = createStaticLayout(threeLineSize)
while (layout.lineCount > 3 && threeLineSize > minThreeLineSize) {
threeLineSize -= step
layout = createStaticLayout(threeLineSize)
}
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, threeLineSize)
}
}
private fun onLoadFileError(message: String?) {
@@ -86,7 +180,18 @@ open class EditActivity : BaseActivity(), DelegateHost, PermissionRequestProxyAc
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_editor, menu)
ViewUtils.setToolbarMenuIconsColorByThemeColorLuminance(this, mToolbar)
mToolbar?.let { toolbar ->
toolbar.setMenuIconsColorByThemeColorLuminance(this)
toolbar.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
val titleView = toolbar.findViewById(com.google.android.material.R.id.action_bar_title) ?: run {
Toolbar::class.java.getDeclaredField("mTitleTextView").apply { isAccessible = true }.get(toolbar) as TextView?
}
titleView?.let { adjustTitleTextView(it) }
toolbar.viewTreeObserver.removeOnGlobalLayoutListener(this)
}
})
}
return true
}
@@ -94,16 +199,6 @@ open class EditActivity : BaseActivity(), DelegateHost, PermissionRequestProxyAc
return mEditorMenu.onOptionsItemSelected(item)
}
override fun onPrepareOptionsMenu(menu: Menu): Boolean {
Log.d(LOG_TAG, "onPrepareOptionsMenu: $menu")
// val isScriptRunning = mEditorView.scriptExecutionId != ScriptExecution.NO_ID
// val forceStopItem = menu.findItem(R.id.action_force_stop)
// forceStopItem.isEnabled = isScriptRunning
return super.onPrepareOptionsMenu(menu)
}
override fun onActionModeStarted(mode: ActionMode) {
Log.d(LOG_TAG, "onActionModeStarted: $mode")

View File

@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

View File

@@ -1,44 +1,45 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:orientation="horizontal">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="end"
android:orientation="horizontal">
<org.autojs.autojs.ui.widget.ToolbarMenuItem
android:id="@+id/step_over"
android:layout_width="40dp"
android:layout_width="@dimen/toolbar_menu_item_width"
android:layout_height="match_parent"
app:icon="@drawable/ic_debug_step_over"
app:text="@string/text_debug_step_over"/>
app:text="@string/text_debug_step_over" />
<org.autojs.autojs.ui.widget.ToolbarMenuItem
android:id="@+id/step_into"
android:layout_width="40dp"
android:layout_width="@dimen/toolbar_menu_item_width"
android:layout_height="match_parent"
app:icon="@drawable/ic_debug_step_into"
app:text="@string/text_debug_step_into"/>
app:text="@string/text_debug_step_into" />
<org.autojs.autojs.ui.widget.ToolbarMenuItem
android:id="@+id/step_out"
android:layout_width="40dp"
android:layout_width="@dimen/toolbar_menu_item_width"
android:layout_height="match_parent"
app:icon="@drawable/ic_debug_step_out"
app:text="@string/text_debug_step_out"/>
app:text="@string/text_debug_step_out" />
<org.autojs.autojs.ui.widget.ToolbarMenuItem
android:id="@+id/resume"
android:layout_width="40dp"
android:layout_width="@dimen/toolbar_menu_item_width"
android:layout_height="match_parent"
app:icon="@drawable/ic_play_arrow_white_48dp"
app:text="@string/text_debug_resume_script"/>
app:text="@string/text_debug_resume_script" />
<org.autojs.autojs.ui.widget.ToolbarMenuItem
android:id="@+id/force_stop"
android:layout_width="40dp"
android:layout_width="@dimen/toolbar_menu_item_width"
android:layout_height="match_parent"
app:icon="@drawable/ic_close_white_48dp"
app:text="@string/text_stop"/>
app:text="@string/text_stop" />
</LinearLayout>

View File

@@ -1,38 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:orientation="horizontal">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="end"
android:orientation="horizontal">
<org.autojs.autojs.ui.widget.ToolbarMenuItem
android:id="@+id/run"
android:layout_width="40dp"
android:layout_height="match_parent"
app:icon="@drawable/ic_play_arrow_white_48dp"
app:text="@string/text_run_simplified" />
android:id="@+id/run"
android:layout_width="@dimen/toolbar_menu_item_width"
android:layout_height="match_parent"
app:icon="@drawable/ic_play_arrow_white_48dp"
app:text="@string/text_run_simplified" />
<org.autojs.autojs.ui.widget.ToolbarMenuItem
android:id="@+id/undo"
android:layout_width="40dp"
android:layout_height="match_parent"
app:icon="@drawable/ic_undo_white_48dp"
app:text="@string/text_undo_simplified" />
android:id="@+id/undo"
android:layout_width="@dimen/toolbar_menu_item_width"
android:layout_height="match_parent"
app:icon="@drawable/ic_undo_white_48dp"
app:text="@string/text_undo_simplified" />
<org.autojs.autojs.ui.widget.ToolbarMenuItem
android:id="@+id/redo"
android:layout_width="40dp"
android:layout_height="match_parent"
app:icon="@drawable/ic_redo_white_48dp"
app:text="@string/text_redo_simplified" />
android:id="@+id/redo"
android:layout_width="@dimen/toolbar_menu_item_width"
android:layout_height="match_parent"
app:icon="@drawable/ic_redo_white_48dp"
app:text="@string/text_redo_simplified" />
<org.autojs.autojs.ui.widget.ToolbarMenuItem
android:id="@+id/save"
android:layout_width="40dp"
android:layout_height="match_parent"
android:enabled="false"
app:icon="@drawable/ic_save_white_48dp"
app:text="@string/text_save_simplified" />
android:id="@+id/save"
android:layout_width="@dimen/toolbar_menu_item_width"
android:layout_height="match_parent"
android:enabled="false"
app:icon="@drawable/ic_save_white_48dp"
app:text="@string/text_save_simplified" />
</LinearLayout>

View File

@@ -1,37 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:orientation="horizontal">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="end"
android:orientation="horizontal">
<org.autojs.autojs.ui.widget.ToolbarMenuItem
android:id="@+id/replace"
android:layout_width="40dp"
android:layout_width="@dimen/toolbar_menu_item_width"
android:layout_height="match_parent"
app:icon="@drawable/ic_ali_replace"
app:text="@string/text_replace_simplified"/>
app:text="@string/text_replace_simplified" />
<org.autojs.autojs.ui.widget.ToolbarMenuItem
android:id="@+id/find_prev"
android:layout_width="40dp"
android:layout_width="@dimen/toolbar_menu_item_width"
android:layout_height="match_parent"
app:icon="@drawable/ic_ali_up"
app:text="@string/text_find_prev_simplified"/>
app:text="@string/text_find_prev_simplified" />
<org.autojs.autojs.ui.widget.ToolbarMenuItem
android:id="@+id/find_next"
android:layout_width="40dp"
android:layout_width="@dimen/toolbar_menu_item_width"
android:layout_height="match_parent"
app:icon="@drawable/ic_ali_down"
app:text="@string/text_find_next_simplified"/>
app:text="@string/text_find_next_simplified" />
<org.autojs.autojs.ui.widget.ToolbarMenuItem
android:id="@+id/cancel_search"
android:layout_width="40dp"
android:layout_width="@dimen/toolbar_menu_item_width"
android:layout_height="match_parent"
app:icon="@drawable/ic_close_white_48dp"
app:text="@string/text_cancel_simplified"/>
app:text="@string/text_cancel_simplified" />
</LinearLayout>

View File

@@ -1,29 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="?selectableItemBackgroundBorderless"
android:orientation="vertical">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="@dimen/toolbar_menu_item_width"
android:layout_height="?attr/actionBarSize"
android:gravity="center"
android:layout_gravity="center"
android:background="?selectableItemBackgroundBorderless"
android:orientation="vertical">
<ImageView
android:id="@+id/icon"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:paddingBottom="20dp"
android:paddingTop="10dp"
tools:src="@drawable/autojs6_material" />
android:id="@+id/icon"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:paddingBottom="23dp"
android:paddingTop="5dp"
tools:src="@drawable/autojs6_material" />
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-20dp"
android:gravity="center"
android:maxLines="1"
android:ellipsize="end"
android:textSize="12sp"
tools:textColor="@color/day_night"
tools:text="@string/text_sample_string" />
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-23dp"
android:gravity="center"
android:maxLines="1"
android:ellipsize="end"
android:textSize="12sp"
tools:textColor="@color/day_night"
tools:text="@string/text_sample_string" />
</LinearLayout>

View File

@@ -23,7 +23,15 @@
<dimen name="snackbar_padding_horizontal">12dp</dimen>
<dimen name="snackbar_padding_vertical">14dp</dimen>
<dimen name="snackbar_text_size">14sp</dimen>
<dimen name="editor_title_text_size">18sp</dimen>
<dimen name="editor_title_text_size">19sp</dimen>
<dimen name="editor_title_text_size_double_line_ascii">19sp</dimen>
<dimen name="editor_title_text_size_double_line_non_ascii">16sp</dimen>
<dimen name="editor_title_text_size_triple_line_ascii">13sp</dimen>
<dimen name="editor_title_text_size_triple_line_non_ascii">11sp</dimen>
<dimen name="editor_title_min_text_size_single_line">16sp</dimen>
<dimen name="editor_title_min_text_size_double_line">13sp</dimen>
<dimen name="editor_title_min_text_size_triple_line">11sp</dimen>
<dimen name="editor_title_text_size_step">1sp</dimen>
<dimen name="main_title_text_size">20sp</dimen>
<dimen name="textSize_item_property">14sp</dimen>
<dimen name="first_char_view_stroke_width">2dp</dimen>
@@ -51,5 +59,6 @@
<dimen name="ref_md_listitem_vertical_margin">12dp</dimen>
<dimen name="ref_md_listitem_textsize">16sp</dimen>
<dimen name="ref_md_listitem_height">48dp</dimen>
<dimen name="toolbar_menu_item_width">40dp</dimen>
</resources>