Add UI for image-attachment "focus" (#2620)
* Attempt-zero implementation of a "focus" feature for image attachments. Choose "Set focus" in the attachment menu, tap once to select focus point (no visual feedback currently), tap "OK". Works in tests.
* Remove code duplication between 'update description' and 'update focus'
* Fix ktlint/bitrise failures
* Make updateMediaItem private
* When focus is set on a post attachment the preview focuses correctly. ProgressImageView now inherits from MediaPreviewImageView.
* Replace use of PointF for Focus where focus is represented, fix ktlint
* Substitute 'focus' for 'focus point' in strings
* First attempt draw focus point. Only updates on initial load. Modeled on code from RoundedCorners builtin from Glide
* Redraw focus after each tap
* Dark curtain where focus isn't (now looks like mastosoc)
* Correct ktlint for FocusDialog
* draft: switch to overlay for focus indicator
* Draw focus circle, but ImageView and FocusIndicatorView seem to share a single canvas
* Switch focus circle to path approach
* Correctly scale, save and load focuses. Clamp to visible area. Focus editor looks and feels right
* ktlint fixes and comments
* Focus indicator drawing should use device-independent pixels
* Shrink focus window when it gets unattractively tall (no linting, misbehaves on wide aspect ratio screens)
* Correct max-height behavior for screens in landscape mode
* Focus attachment result is are flipped on x axis; fix this
* Correctly thread focus through on scheduled posts, redrafted posts, and drafts (but draft focus is lost on post)
* More focus ktlint fixes
* Fix specific case where a draft is given a focus, then deleted, then posted in that order
* Fix accidental file change in focus PR
* ktLint fix
* Fix property style warnings in focus
* Fix remaining style warnings from focus PR
Co-authored-by: Conny Duck <k.pozniak@gmx.at>
2022-09-21 20:28:06 +02:00
|
|
|
/* Copyright 2019 Tusky Contributors
|
|
|
|
*
|
|
|
|
* This file is a part of Tusky.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with Tusky; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
|
|
|
package com.keylesspalace.tusky.components.compose.dialog
|
|
|
|
|
|
|
|
import android.content.DialogInterface
|
|
|
|
import android.graphics.drawable.Drawable
|
|
|
|
import android.net.Uri
|
|
|
|
import android.view.WindowManager
|
|
|
|
import android.widget.FrameLayout
|
|
|
|
import androidx.appcompat.app.AlertDialog
|
2023-06-11 12:58:55 +02:00
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
Add UI for image-attachment "focus" (#2620)
* Attempt-zero implementation of a "focus" feature for image attachments. Choose "Set focus" in the attachment menu, tap once to select focus point (no visual feedback currently), tap "OK". Works in tests.
* Remove code duplication between 'update description' and 'update focus'
* Fix ktlint/bitrise failures
* Make updateMediaItem private
* When focus is set on a post attachment the preview focuses correctly. ProgressImageView now inherits from MediaPreviewImageView.
* Replace use of PointF for Focus where focus is represented, fix ktlint
* Substitute 'focus' for 'focus point' in strings
* First attempt draw focus point. Only updates on initial load. Modeled on code from RoundedCorners builtin from Glide
* Redraw focus after each tap
* Dark curtain where focus isn't (now looks like mastosoc)
* Correct ktlint for FocusDialog
* draft: switch to overlay for focus indicator
* Draw focus circle, but ImageView and FocusIndicatorView seem to share a single canvas
* Switch focus circle to path approach
* Correctly scale, save and load focuses. Clamp to visible area. Focus editor looks and feels right
* ktlint fixes and comments
* Focus indicator drawing should use device-independent pixels
* Shrink focus window when it gets unattractively tall (no linting, misbehaves on wide aspect ratio screens)
* Correct max-height behavior for screens in landscape mode
* Focus attachment result is are flipped on x axis; fix this
* Correctly thread focus through on scheduled posts, redrafted posts, and drafts (but draft focus is lost on post)
* More focus ktlint fixes
* Fix specific case where a draft is given a focus, then deleted, then posted in that order
* Fix accidental file change in focus PR
* ktLint fix
* Fix property style warnings in focus
* Fix remaining style warnings from focus PR
Co-authored-by: Conny Duck <k.pozniak@gmx.at>
2022-09-21 20:28:06 +02:00
|
|
|
import androidx.lifecycle.LifecycleOwner
|
|
|
|
import androidx.lifecycle.lifecycleScope
|
|
|
|
import com.bumptech.glide.Glide
|
|
|
|
import com.bumptech.glide.load.DataSource
|
|
|
|
import com.bumptech.glide.load.engine.GlideException
|
|
|
|
import com.bumptech.glide.load.resource.bitmap.DownsampleStrategy
|
|
|
|
import com.bumptech.glide.request.RequestListener
|
|
|
|
import com.bumptech.glide.request.target.Target
|
|
|
|
import com.keylesspalace.tusky.databinding.DialogFocusBinding
|
|
|
|
import com.keylesspalace.tusky.entity.Attachment.Focus
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
|
|
|
|
fun <T> T.makeFocusDialog(
|
|
|
|
existingFocus: Focus?,
|
|
|
|
previewUri: Uri,
|
2023-04-24 11:48:40 +02:00
|
|
|
onUpdateFocus: suspend (Focus) -> Unit
|
2023-06-11 12:58:55 +02:00
|
|
|
) where T : AppCompatActivity, T : LifecycleOwner {
|
Add UI for image-attachment "focus" (#2620)
* Attempt-zero implementation of a "focus" feature for image attachments. Choose "Set focus" in the attachment menu, tap once to select focus point (no visual feedback currently), tap "OK". Works in tests.
* Remove code duplication between 'update description' and 'update focus'
* Fix ktlint/bitrise failures
* Make updateMediaItem private
* When focus is set on a post attachment the preview focuses correctly. ProgressImageView now inherits from MediaPreviewImageView.
* Replace use of PointF for Focus where focus is represented, fix ktlint
* Substitute 'focus' for 'focus point' in strings
* First attempt draw focus point. Only updates on initial load. Modeled on code from RoundedCorners builtin from Glide
* Redraw focus after each tap
* Dark curtain where focus isn't (now looks like mastosoc)
* Correct ktlint for FocusDialog
* draft: switch to overlay for focus indicator
* Draw focus circle, but ImageView and FocusIndicatorView seem to share a single canvas
* Switch focus circle to path approach
* Correctly scale, save and load focuses. Clamp to visible area. Focus editor looks and feels right
* ktlint fixes and comments
* Focus indicator drawing should use device-independent pixels
* Shrink focus window when it gets unattractively tall (no linting, misbehaves on wide aspect ratio screens)
* Correct max-height behavior for screens in landscape mode
* Focus attachment result is are flipped on x axis; fix this
* Correctly thread focus through on scheduled posts, redrafted posts, and drafts (but draft focus is lost on post)
* More focus ktlint fixes
* Fix specific case where a draft is given a focus, then deleted, then posted in that order
* Fix accidental file change in focus PR
* ktLint fix
* Fix property style warnings in focus
* Fix remaining style warnings from focus PR
Co-authored-by: Conny Duck <k.pozniak@gmx.at>
2022-09-21 20:28:06 +02:00
|
|
|
val focus = existingFocus ?: Focus(0.0f, 0.0f) // Default to center
|
|
|
|
|
|
|
|
val dialogBinding = DialogFocusBinding.inflate(layoutInflater)
|
|
|
|
|
|
|
|
dialogBinding.focusIndicator.setFocus(focus)
|
|
|
|
|
|
|
|
Glide.with(this)
|
|
|
|
.load(previewUri)
|
|
|
|
.downsample(DownsampleStrategy.CENTER_INSIDE)
|
|
|
|
.listener(object : RequestListener<Drawable> {
|
2024-01-04 17:00:55 +01:00
|
|
|
override fun onLoadFailed(
|
|
|
|
p0: GlideException?,
|
|
|
|
p1: Any?,
|
|
|
|
p2: Target<Drawable?>,
|
|
|
|
p3: Boolean
|
|
|
|
): Boolean {
|
Add UI for image-attachment "focus" (#2620)
* Attempt-zero implementation of a "focus" feature for image attachments. Choose "Set focus" in the attachment menu, tap once to select focus point (no visual feedback currently), tap "OK". Works in tests.
* Remove code duplication between 'update description' and 'update focus'
* Fix ktlint/bitrise failures
* Make updateMediaItem private
* When focus is set on a post attachment the preview focuses correctly. ProgressImageView now inherits from MediaPreviewImageView.
* Replace use of PointF for Focus where focus is represented, fix ktlint
* Substitute 'focus' for 'focus point' in strings
* First attempt draw focus point. Only updates on initial load. Modeled on code from RoundedCorners builtin from Glide
* Redraw focus after each tap
* Dark curtain where focus isn't (now looks like mastosoc)
* Correct ktlint for FocusDialog
* draft: switch to overlay for focus indicator
* Draw focus circle, but ImageView and FocusIndicatorView seem to share a single canvas
* Switch focus circle to path approach
* Correctly scale, save and load focuses. Clamp to visible area. Focus editor looks and feels right
* ktlint fixes and comments
* Focus indicator drawing should use device-independent pixels
* Shrink focus window when it gets unattractively tall (no linting, misbehaves on wide aspect ratio screens)
* Correct max-height behavior for screens in landscape mode
* Focus attachment result is are flipped on x axis; fix this
* Correctly thread focus through on scheduled posts, redrafted posts, and drafts (but draft focus is lost on post)
* More focus ktlint fixes
* Fix specific case where a draft is given a focus, then deleted, then posted in that order
* Fix accidental file change in focus PR
* ktLint fix
* Fix property style warnings in focus
* Fix remaining style warnings from focus PR
Co-authored-by: Conny Duck <k.pozniak@gmx.at>
2022-09-21 20:28:06 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-01-04 17:00:55 +01:00
|
|
|
override fun onResourceReady(
|
|
|
|
resource: Drawable,
|
|
|
|
model: Any,
|
|
|
|
target: Target<Drawable?>?,
|
|
|
|
dataSource: DataSource,
|
|
|
|
isFirstResource: Boolean
|
|
|
|
): Boolean {
|
2023-09-11 19:29:50 +02:00
|
|
|
val width = resource.intrinsicWidth
|
Add UI for image-attachment "focus" (#2620)
* Attempt-zero implementation of a "focus" feature for image attachments. Choose "Set focus" in the attachment menu, tap once to select focus point (no visual feedback currently), tap "OK". Works in tests.
* Remove code duplication between 'update description' and 'update focus'
* Fix ktlint/bitrise failures
* Make updateMediaItem private
* When focus is set on a post attachment the preview focuses correctly. ProgressImageView now inherits from MediaPreviewImageView.
* Replace use of PointF for Focus where focus is represented, fix ktlint
* Substitute 'focus' for 'focus point' in strings
* First attempt draw focus point. Only updates on initial load. Modeled on code from RoundedCorners builtin from Glide
* Redraw focus after each tap
* Dark curtain where focus isn't (now looks like mastosoc)
* Correct ktlint for FocusDialog
* draft: switch to overlay for focus indicator
* Draw focus circle, but ImageView and FocusIndicatorView seem to share a single canvas
* Switch focus circle to path approach
* Correctly scale, save and load focuses. Clamp to visible area. Focus editor looks and feels right
* ktlint fixes and comments
* Focus indicator drawing should use device-independent pixels
* Shrink focus window when it gets unattractively tall (no linting, misbehaves on wide aspect ratio screens)
* Correct max-height behavior for screens in landscape mode
* Focus attachment result is are flipped on x axis; fix this
* Correctly thread focus through on scheduled posts, redrafted posts, and drafts (but draft focus is lost on post)
* More focus ktlint fixes
* Fix specific case where a draft is given a focus, then deleted, then posted in that order
* Fix accidental file change in focus PR
* ktLint fix
* Fix property style warnings in focus
* Fix remaining style warnings from focus PR
Co-authored-by: Conny Duck <k.pozniak@gmx.at>
2022-09-21 20:28:06 +02:00
|
|
|
val height = resource.intrinsicHeight
|
|
|
|
|
|
|
|
dialogBinding.focusIndicator.setImageSize(width, height)
|
|
|
|
|
|
|
|
// We want the dialog to be a little taller than the image, so you can slide your thumb past the image border,
|
|
|
|
// but if it's *too* much taller that looks weird. See if a threshold has been crossed:
|
|
|
|
if (width > height) {
|
|
|
|
val maxHeight = dialogBinding.focusIndicator.maxAttractiveHeight()
|
|
|
|
|
|
|
|
if (dialogBinding.imageView.height > maxHeight) {
|
|
|
|
val verticalShrinkLayout = FrameLayout.LayoutParams(width, maxHeight)
|
|
|
|
dialogBinding.imageView.layoutParams = verticalShrinkLayout
|
|
|
|
dialogBinding.focusIndicator.layoutParams = verticalShrinkLayout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false // Pass through
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.into(dialogBinding.imageView)
|
|
|
|
|
|
|
|
val okListener = { dialog: DialogInterface, _: Int ->
|
|
|
|
lifecycleScope.launch {
|
2023-04-24 11:48:40 +02:00
|
|
|
onUpdateFocus(dialogBinding.focusIndicator.getFocus())
|
Add UI for image-attachment "focus" (#2620)
* Attempt-zero implementation of a "focus" feature for image attachments. Choose "Set focus" in the attachment menu, tap once to select focus point (no visual feedback currently), tap "OK". Works in tests.
* Remove code duplication between 'update description' and 'update focus'
* Fix ktlint/bitrise failures
* Make updateMediaItem private
* When focus is set on a post attachment the preview focuses correctly. ProgressImageView now inherits from MediaPreviewImageView.
* Replace use of PointF for Focus where focus is represented, fix ktlint
* Substitute 'focus' for 'focus point' in strings
* First attempt draw focus point. Only updates on initial load. Modeled on code from RoundedCorners builtin from Glide
* Redraw focus after each tap
* Dark curtain where focus isn't (now looks like mastosoc)
* Correct ktlint for FocusDialog
* draft: switch to overlay for focus indicator
* Draw focus circle, but ImageView and FocusIndicatorView seem to share a single canvas
* Switch focus circle to path approach
* Correctly scale, save and load focuses. Clamp to visible area. Focus editor looks and feels right
* ktlint fixes and comments
* Focus indicator drawing should use device-independent pixels
* Shrink focus window when it gets unattractively tall (no linting, misbehaves on wide aspect ratio screens)
* Correct max-height behavior for screens in landscape mode
* Focus attachment result is are flipped on x axis; fix this
* Correctly thread focus through on scheduled posts, redrafted posts, and drafts (but draft focus is lost on post)
* More focus ktlint fixes
* Fix specific case where a draft is given a focus, then deleted, then posted in that order
* Fix accidental file change in focus PR
* ktLint fix
* Fix property style warnings in focus
* Fix remaining style warnings from focus PR
Co-authored-by: Conny Duck <k.pozniak@gmx.at>
2022-09-21 20:28:06 +02:00
|
|
|
}
|
|
|
|
dialog.dismiss()
|
|
|
|
}
|
|
|
|
|
|
|
|
val dialog = AlertDialog.Builder(this)
|
|
|
|
.setView(dialogBinding.root)
|
|
|
|
.setPositiveButton(android.R.string.ok, okListener)
|
|
|
|
.setNegativeButton(android.R.string.cancel, null)
|
|
|
|
.create()
|
|
|
|
|
|
|
|
val window = dialog.window
|
|
|
|
window?.setSoftInputMode(
|
|
|
|
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
|
|
|
|
)
|
|
|
|
|
|
|
|
dialog.show()
|
|
|
|
}
|