Skip to content

Commit

Permalink
Feat[log]: reimplement using DefocusableScrollView
Browse files Browse the repository at this point in the history
  • Loading branch information
artdeell committed Apr 11, 2024
1 parent f8eddc0 commit 4caa308
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
28 changes: 13 additions & 15 deletions app_pojavlauncher/src/main/java/com/kdt/LoggerView.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.ToggleButton;

Expand All @@ -23,14 +22,8 @@
public class LoggerView extends ConstraintLayout {
private Logger.eventLogListener mLogListener;
private ToggleButton mLogToggle;
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 DefocusableScrollView mScrollView;
private TextView mLogTextView;
private boolean mAutoScroll = true;


public LoggerView(@NonNull Context context) {
Expand Down Expand Up @@ -76,25 +69,30 @@ private void init(){
});
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
ImageButton cancelButton = findViewById(R.id.log_view_cancel);
cancelButton.setOnClickListener(view -> LoggerView.this.setVisibility(GONE));

// Set the scroll view
mScrollView = findViewById(R.id.content_log_scroll);
mScrollView.setKeepFocusing(true);

//Set up the autoscroll switch
ToggleButton autoscrollToggle = findViewById(R.id.content_log_toggle_autoscroll);
autoscrollToggle.setOnCheckedChangeListener(
(compoundButton, isChecked) -> {
if(isChecked) mScrollView.fullScroll(View.FOCUS_DOWN);
mScrollView.setKeepFocusing(isChecked);
}
);
autoscrollToggle.setChecked(true);

// Listen to logs
mLogListener = text -> {
if(mLogTextView.getVisibility() != VISIBLE) return;
post(() -> {
mLogTextView.append(text + '\n');
if(mAutoScroll) mScrollView.fullScroll(View.FOCUS_DOWN);
if(mScrollView.isKeepFocusing()) mScrollView.fullScroll(View.FOCUS_DOWN);
});

};
Expand Down
5 changes: 2 additions & 3 deletions app_pojavlauncher/src/main/res/layout/view_logger.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,12 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ScrollView
<com.kdt.DefocusableScrollView
android:id="@+id/content_log_scroll"
android:layout_width="match_parent"
android:layout_height="0dp"
android:alpha="0.8"
android:background="#000000"
android:descendantFocusability="blocksDescendants"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/top_log_view">

Expand All @@ -70,6 +69,6 @@
android:layout_height="wrap_content"
android:textIsSelectable="true"/>

</ScrollView>
</com.kdt.DefocusableScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit 4caa308

Please sign in to comment.