Feat[log]: add autoscroll switch

This commit is contained in:
artdeell 2024-04-10 20:21:18 -04:00 committed by Maksim Belov
parent af3d4abd75
commit f8eddc0eb5
3 changed files with 39 additions and 8 deletions

View File

@ -22,9 +22,15 @@ import net.kdt.pojavlaunch.R;
*/ */
public class LoggerView extends ConstraintLayout { public class LoggerView extends ConstraintLayout {
private Logger.eventLogListener mLogListener; private Logger.eventLogListener mLogListener;
private ToggleButton mToggleButton; private ToggleButton mLogToggle;
private ScrollView mScrollView; private ScrollView mScrollView;
/*
* android:descendantFocusability="blocksDescendants"
* This is set for the ScrollView, since under focus the TextView always autoscrolls.
* By not allowing focus, we are able to control its behaviour from the code that we have here.
*/
private TextView mLogTextView; private TextView mLogTextView;
private boolean mAutoScroll = true;
public LoggerView(@NonNull Context context) { public LoggerView(@NonNull Context context) {
@ -40,7 +46,7 @@ public class LoggerView extends ConstraintLayout {
public void setVisibility(int visibility) { public void setVisibility(int visibility) {
super.setVisibility(visibility); super.setVisibility(visibility);
// Triggers the log view shown state by default when viewing it // Triggers the log view shown state by default when viewing it
mToggleButton.setChecked(visibility == VISIBLE); mLogToggle.setChecked(visibility == VISIBLE);
} }
/** /**
@ -56,8 +62,8 @@ public class LoggerView extends ConstraintLayout {
mLogTextView.setVisibility(GONE); mLogTextView.setVisibility(GONE);
// Toggle log visibility // Toggle log visibility
mToggleButton = findViewById(R.id.content_log_toggle_log); mLogToggle = findViewById(R.id.content_log_toggle_log);
mToggleButton.setOnCheckedChangeListener( mLogToggle.setOnCheckedChangeListener(
(compoundButton, isChecked) -> { (compoundButton, isChecked) -> {
mLogTextView.setVisibility(isChecked ? VISIBLE : GONE); mLogTextView.setVisibility(isChecked ? VISIBLE : GONE);
if(isChecked) { if(isChecked) {
@ -68,7 +74,13 @@ public class LoggerView extends ConstraintLayout {
// NOTE: was tested by rapidly smashing the log on/off button, no sync issues found :) // NOTE: was tested by rapidly smashing the log on/off button, no sync issues found :)
} }
}); });
mToggleButton.setChecked(false); mLogToggle.setChecked(false);
ToggleButton autoscrollToggle = findViewById(R.id.content_log_toggle_autoscroll);
autoscrollToggle.setOnCheckedChangeListener(
(compoundButton, isChecked) -> mAutoScroll = isChecked
);
autoscrollToggle.setChecked(true);
// Remove the loggerView from the user View // Remove the loggerView from the user View
ImageButton cancelButton = findViewById(R.id.log_view_cancel); ImageButton cancelButton = findViewById(R.id.log_view_cancel);
@ -82,7 +94,7 @@ public class LoggerView extends ConstraintLayout {
if(mLogTextView.getVisibility() != VISIBLE) return; if(mLogTextView.getVisibility() != VISIBLE) return;
post(() -> { post(() -> {
mLogTextView.append(text + '\n'); mLogTextView.append(text + '\n');
mScrollView.fullScroll(View.FOCUS_DOWN); if(mAutoScroll) mScrollView.fullScroll(View.FOCUS_DOWN);
}); });
}; };

View File

@ -15,18 +15,31 @@
android:id="@+id/log_title" android:id="@+id/log_title"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingStart="25dp" android:paddingHorizontal="25dp"
android:text="Log output:" android:text="@string/log_view_label_log_output"
android:textAppearance="?android:attr/textAppearanceMedium" android:textAppearance="?android:attr/textAppearanceMedium"
app:layout_constraintBottom_toBottomOf="@+id/top_log_view" app:layout_constraintBottom_toBottomOf="@+id/top_log_view"
app:layout_constraintStart_toStartOf="@+id/top_log_view" app:layout_constraintStart_toStartOf="@+id/top_log_view"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
<ToggleButton
android:id="@+id/content_log_toggle_autoscroll"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginHorizontal="10dp"
android:textOff="@string/log_view_button_scroll_off"
android:textOn="@string/log_view_button_scroll_on"
app:layout_constraintBottom_toBottomOf="@+id/top_log_view"
app:layout_constraintEnd_toStartOf="@+id/content_log_toggle_log"
app:layout_constraintTop_toTopOf="parent"/>
<ToggleButton <ToggleButton
android:id="@+id/content_log_toggle_log" android:id="@+id/content_log_toggle_log"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_marginHorizontal="10dp" android:layout_marginHorizontal="10dp"
android:textOff="@string/log_view_button_output_off"
android:textOn="@string/log_view_button_output_on"
app:layout_constraintBottom_toBottomOf="@+id/top_log_view" app:layout_constraintBottom_toBottomOf="@+id/top_log_view"
app:layout_constraintEnd_toStartOf="@+id/log_view_cancel" app:layout_constraintEnd_toStartOf="@+id/log_view_cancel"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
@ -47,6 +60,7 @@
android:layout_height="0dp" android:layout_height="0dp"
android:alpha="0.8" android:alpha="0.8"
android:background="#000000" android:background="#000000"
android:descendantFocusability="blocksDescendants"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/top_log_view"> app:layout_constraintTop_toBottomOf="@+id/top_log_view">

View File

@ -376,4 +376,9 @@
<string name="cropper_lock_horizontal">H. lock</string> <string name="cropper_lock_horizontal">H. lock</string>
<string name="cropper_reset">Reset</string> <string name="cropper_reset">Reset</string>
<string name="cropper_select_cancelled">Selection cancelled</string> <string name="cropper_select_cancelled">Selection cancelled</string>
<string name="log_view_button_scroll_off">Autoscroll\nOFF</string>
<string name="log_view_button_scroll_on">Autoscroll\nON</string>
<string name="log_view_button_output_off">Output\nOFF</string>
<string name="log_view_button_output_on">Output\nON</string>
<string name="log_view_label_log_output">Log output:</string>
</resources> </resources>