Просмотр исходного кода

为亮暗色模式添加底部按钮

HapeLee 1 год назад
Родитель
Сommit
1288d7da2e

+ 24 - 2
app/src/main/java/io/legato/kazusa/ui/book/read/ReadMenu.kt

@@ -23,6 +23,7 @@ import io.legato.kazusa.constant.PreferKey
 import io.legato.kazusa.databinding.ViewReadMenuBinding
 import io.legato.kazusa.help.config.AppConfig
 import io.legato.kazusa.help.config.LocalConfig
+import io.legato.kazusa.help.config.ThemeConfig
 import io.legato.kazusa.help.coroutine.Coroutine
 import io.legato.kazusa.help.source.getSourceType
 import io.legato.kazusa.lib.dialogs.alert
@@ -470,6 +471,7 @@ class ReadMenu @JvmOverloads constructor(
                 contentDescription = btn.description
                 tooltipText = btn.description
                 strokeWidth = 0
+                iconGravity = MaterialButton.ICON_GRAVITY_TEXT_START
                 setOnClickListener { btn.onClick() }
                 btn.onLongClick?.let { longAction ->
                     setOnLongClickListener {
@@ -478,8 +480,16 @@ class ReadMenu @JvmOverloads constructor(
                     }
                 }
             }
-
-            group.addView(button)
+            group.addView(button,
+                MaterialButtonGroup.LayoutParams(
+                    LayoutParams.WRAP_CONTENT,
+                    LayoutParams.WRAP_CONTENT
+                ).apply {
+                    weight = 1f
+                }
+            )
+            val lp = button.layoutParams as MaterialButtonGroup.LayoutParams
+            lp.overflowText = btn.description
             buttonMap[btn.id] = button
         }
     }
@@ -522,6 +532,18 @@ class ReadMenu @JvmOverloads constructor(
                 iconRes = R.drawable.ic_bookmark,
                 description = context.getString(R.string.bookmark),
                 onClick = { runMenuOut { callBack.addBookmark() } }
+            ),
+            ToolButton(
+                id = "theme",
+                iconRes = if (AppConfig.isNightTheme) R.drawable.ic_daytime else R.drawable.ic_brightness,
+                description = context.getString(R.string.day_night_switch),
+                onClick = {
+                    AppConfig.isNightTheme = !AppConfig.isNightTheme
+                    ThemeConfig.applyDayNight(context)
+                    buttonMap["theme"]?.setIconResource(
+                        if (AppConfig.isNightTheme) R.drawable.ic_daytime else R.drawable.ic_brightness
+                    )
+                }
             )
         )
     }

+ 2 - 1
app/src/main/java/io/legato/kazusa/ui/book/read/config/ToolButtonConfigDialog.kt

@@ -114,7 +114,7 @@ class ToolButtonConfigDialog : BaseBottomSheetDialogFragment(R.layout.dialog_too
     }
 
     private fun getAllButtonIds(): List<String> {
-        return listOf("search", "auto_page", "catalog", "read_aloud", "setting", "addBookmark")
+        return listOf("search", "auto_page", "catalog", "read_aloud", "setting", "addBookmark", "theme")
     }
 
     private fun getButtonInfo(id: String): Pair<Int, String> {
@@ -125,6 +125,7 @@ class ToolButtonConfigDialog : BaseBottomSheetDialogFragment(R.layout.dialog_too
             "read_aloud" -> R.drawable.ic_read_aloud to getString(R.string.read_aloud)
             "setting" -> R.drawable.ic_settings to getString(R.string.setting)
             "addBookmark" -> R.drawable.ic_bookmark to getString(R.string.bookmark)
+            "theme" -> R.drawable.ic_daytime to getString(R.string.day_night_switch)
             else -> R.drawable.ic_help to id
         }
     }

+ 2 - 4
app/src/main/res/layout-land/view_read_menu.xml

@@ -183,14 +183,12 @@
             android:id="@+id/bottom_view"
             android:layout_width="0dp"
             android:layout_height="wrap_content"
+            app:overflowMode="menu"
             android:layout_weight="2"
             android:importantForAccessibility="no"
             android:paddingVertical="8dp"
-            android:gravity="center"
             android:paddingHorizontal="16dp"
-            android:orientation="horizontal">
-
-        </com.google.android.material.button.MaterialButtonGroup>
+            android:orientation="horizontal"/>
 
     </LinearLayout>
 

+ 47 - 36
app/src/main/res/layout/item_tool_button.xml

@@ -1,45 +1,56 @@
 <?xml version="1.0" encoding="utf-8"?>
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+<com.google.android.material.card.MaterialCardView
+    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="wrap_content"
-    android:gravity="center_vertical"
-    android:orientation="horizontal"
-    android:paddingHorizontal="16dp"
-    android:paddingVertical="8dp">
-
-    <!-- 图标 -->
-    <com.google.android.material.button.MaterialButton
-        android:id="@+id/ivIcon"
-        style="@style/Widget.Material3Expressive.Button.IconButton.Tonal"
-        android:layout_width="wrap_content"
+    app:strokeWidth="0dp"
+    android:layout_marginHorizontal="16dp"
+    android:layout_marginVertical="8dp">
+    <LinearLayout
+        android:layout_width="match_parent"
         android:layout_height="wrap_content"
-        android:clickable="false" />
+        android:clickable="true"
+        android:background="?attr/colorSurface"
+        android:foreground="?attr/selectableItemBackground"
+        android:gravity="center_vertical"
+        android:orientation="horizontal"
+        android:paddingHorizontal="16dp"
+        android:paddingVertical="8dp">
 
-    <!-- 按钮名称 -->
-    <TextView
-        android:id="@+id/tvName"
-        android:layout_width="0dp"
-        android:layout_height="wrap_content"
-        android:layout_marginStart="12dp"
-        android:layout_weight="1"
-        android:text=""
-        android:textColor="?attr/colorOnSurface"
-        android:textSize="16sp" />
+        <!-- 图标 -->
+        <com.google.android.material.button.MaterialButton
+            android:id="@+id/ivIcon"
+            style="@style/Widget.Material3Expressive.Button.IconButton.Tonal"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:clickable="false" />
 
-    <!-- 禁用/隐藏按钮 -->
-    <com.google.android.material.button.MaterialButton
-        android:id="@+id/btnDisable"
-        style="@style/Widget.Material3Expressive.Button.IconButton.Outlined"
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:layout_marginEnd="8dp"
-        app:icon="@drawable/ic_visibility_on" />
+        <!-- 按钮名称 -->
+        <TextView
+            android:id="@+id/tvName"
+            android:layout_width="0dp"
+            android:layout_height="wrap_content"
+            android:layout_marginStart="12dp"
+            android:layout_weight="1"
+            android:text=""
+            android:textColor="?attr/colorOnSurface"
+            android:textSize="16sp" />
 
-    <ImageView
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:layout_marginEnd="4dp"
-        android:src="@drawable/ic_drag_handle" />
+        <!-- 禁用/隐藏按钮 -->
+        <com.google.android.material.button.MaterialButton
+            android:id="@+id/btnDisable"
+            style="@style/Widget.Material3Expressive.Button.IconButton.Outlined"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_marginEnd="8dp"
+            app:icon="@drawable/ic_visibility_on" />
+
+        <ImageView
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_marginEnd="4dp"
+            android:src="@drawable/ic_drag_handle" />
 
-</LinearLayout>
+    </LinearLayout>
+</com.google.android.material.card.MaterialCardView>

+ 9 - 12
app/src/main/res/layout/view_read_menu.xml

@@ -172,19 +172,16 @@
                 tools:ignore="TouchTargetSizeCheck"/>
         </LinearLayout>
 
-        <com.google.android.material.button.MaterialButtonGroup
-            android:id="@+id/bottom_view"
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:importantForAccessibility="no"
-            android:paddingVertical="8dp"
-            android:paddingHorizontal="16dp"
-            android:background="?attr/colorSurfaceContainer"
-            android:gravity="center"
-            android:orientation="horizontal">
-
+            <com.google.android.material.button.MaterialButtonGroup
+                android:id="@+id/bottom_view"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                app:overflowMode="menu"
+                android:paddingVertical="8dp"
+                android:paddingHorizontal="16dp"
+                android:background="?attr/colorSurfaceContainer"
+                android:orientation="horizontal"/>
 
-        </com.google.android.material.button.MaterialButtonGroup>
 
     </LinearLayout>
 

+ 1 - 0
app/src/main/res/values-zh/strings.xml

@@ -1300,4 +1300,5 @@
     <string name="restart_required_message">此更改需要重启应用才能生效,是否立即重启?</string>
     <string name="restart_later_message">更改将在下次启动应用时生效</string>
     <string name="search_select_group">选择书源分组</string>
+    <string name="day_night_switch">深浅色模式开关</string>
 </resources>

+ 1 - 0
app/src/main/res/values/strings.xml

@@ -1301,4 +1301,5 @@
     <string name="restart_required_message">修改该选项需要重启</string>
     <string name="restart_later_message">稍后重启</string>
     <string name="search_select_group">选择书源分组</string>
+    <string name="day_night_switch">深浅色模式开关</string>
 </resources>