mirror of
https://github.com/mihonapp/mihon.git
synced 2025-06-25 10:37:51 +02:00
Add an overlay on top of the reader to simulate a lower brightness. Closes #362
This commit is contained in:
@ -48,7 +48,7 @@ class PreferencesHelper(context: Context) {
|
||||
|
||||
fun customBrightness() = rxPrefs.getBoolean(keys.customBrightness, false)
|
||||
|
||||
fun customBrightnessValue() = rxPrefs.getFloat(keys.customBrightnessValue, 0f)
|
||||
fun customBrightnessValue() = rxPrefs.getInteger(keys.customBrightnessValue, 0)
|
||||
|
||||
fun defaultViewer() = prefs.getInt(keys.defaultViewer, 1)
|
||||
|
||||
|
@ -415,20 +415,39 @@ class ReaderActivity : BaseRxActivity<ReaderPresenter>() {
|
||||
private fun setCustomBrightness(enabled: Boolean) {
|
||||
if (enabled) {
|
||||
customBrightnessSubscription = preferences.customBrightnessValue().asObservable()
|
||||
.map { Math.max(0.01f, it) }
|
||||
.subscribe { setCustomBrightnessValue(it) }
|
||||
|
||||
subscriptions.add(customBrightnessSubscription)
|
||||
} else {
|
||||
if (customBrightnessSubscription != null) {
|
||||
subscriptions.remove(customBrightnessSubscription)
|
||||
}
|
||||
setCustomBrightnessValue(WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE)
|
||||
customBrightnessSubscription?.let { subscriptions.remove(it) }
|
||||
setCustomBrightnessValue(0)
|
||||
}
|
||||
}
|
||||
|
||||
private fun setCustomBrightnessValue(value: Float) {
|
||||
window.attributes = window.attributes.apply { screenBrightness = value }
|
||||
/**
|
||||
* Sets the brightness of the screen. Range is [-50, 100].
|
||||
* From -50 to -1 a semi-transparent black view is shown at the top with the minimum brightness.
|
||||
* From 1 to 100 it sets that value as brightness.
|
||||
* 0 sets system brightness and hides the overlay.
|
||||
*/
|
||||
private fun setCustomBrightnessValue(value: Int) {
|
||||
// Calculate and set reader brightness.
|
||||
val readerBrightness = if (value > 0) {
|
||||
value / 100f
|
||||
} else if (value < 0) {
|
||||
0.01f
|
||||
} else WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE
|
||||
|
||||
window.attributes = window.attributes.apply { screenBrightness = readerBrightness }
|
||||
|
||||
// Set black overlay visibility.
|
||||
if (value < 0) {
|
||||
brightness_overlay.visibility = View.VISIBLE
|
||||
val alpha = (Math.abs(value) * 2.56).toInt()
|
||||
brightness_overlay.setBackgroundColor(Color.argb(alpha, 0, 0, 0))
|
||||
} else {
|
||||
brightness_overlay.visibility = View.GONE
|
||||
}
|
||||
}
|
||||
|
||||
private fun applyTheme(theme: Int) {
|
||||
|
@ -4,15 +4,14 @@ import android.app.Dialog
|
||||
import android.os.Bundle
|
||||
import android.support.v4.app.DialogFragment
|
||||
import android.view.View
|
||||
import android.widget.SeekBar
|
||||
import com.afollestad.materialdialogs.MaterialDialog
|
||||
import eu.kanade.tachiyomi.R
|
||||
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
|
||||
import eu.kanade.tachiyomi.data.preference.getOrDefault
|
||||
import eu.kanade.tachiyomi.util.plusAssign
|
||||
import eu.kanade.tachiyomi.widget.IgnoreFirstSpinnerListener
|
||||
import eu.kanade.tachiyomi.widget.SimpleSeekBarListener
|
||||
import kotlinx.android.synthetic.main.dialog_reader_settings.view.*
|
||||
import org.adw.library.widgets.discreteseekbar.DiscreteSeekBar
|
||||
import rx.Observable
|
||||
import rx.android.schedulers.AndroidSchedulers
|
||||
import rx.subscriptions.CompositeSubscription
|
||||
@ -108,17 +107,18 @@ class ReaderSettingsDialog : DialogFragment() {
|
||||
preferences.customBrightness().set(isChecked)
|
||||
}
|
||||
|
||||
brightness_seekbar.max = 100
|
||||
brightness_seekbar.progress = Math.round(
|
||||
preferences.customBrightnessValue().getOrDefault() * brightness_seekbar.max)
|
||||
brightness_seekbar.setOnSeekBarChangeListener(object : SimpleSeekBarListener() {
|
||||
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
|
||||
brightness_seekbar.progress = preferences.customBrightnessValue().getOrDefault()
|
||||
brightness_seekbar.setOnProgressChangeListener(object : DiscreteSeekBar.OnProgressChangeListener {
|
||||
override fun onProgressChanged(seekBar: DiscreteSeekBar, value: Int, fromUser: Boolean) {
|
||||
if (fromUser) {
|
||||
preferences.customBrightnessValue().set(progress.toFloat() / seekBar.max)
|
||||
preferences.customBrightnessValue().set(value)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
override fun onStartTrackingTouch(seekBar: DiscreteSeekBar) {}
|
||||
|
||||
override fun onStopTrackingTouch(seekBar: DiscreteSeekBar) {}
|
||||
})
|
||||
}
|
||||
|
||||
override fun onDestroyView() {
|
||||
|
Reference in New Issue
Block a user