-
-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
Showing
17 changed files
with
358 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
app/src/main/java/com/keylesspalace/tusky/components/compose/dialog/FocusDialog.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* 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.app.Activity | ||
import android.content.DialogInterface | ||
import android.graphics.drawable.Drawable | ||
import android.net.Uri | ||
import android.view.WindowManager | ||
import android.widget.FrameLayout | ||
import android.widget.Toast | ||
import androidx.appcompat.app.AlertDialog | ||
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.R | ||
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, | ||
onUpdateFocus: suspend (Focus) -> Boolean | ||
) where T : Activity, T : LifecycleOwner { | ||
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> { | ||
override fun onLoadFailed(p0: GlideException?, p1: Any?, p2: Target<Drawable?>?, p3: Boolean): Boolean { | ||
return false | ||
} | ||
|
||
override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable?>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean { | ||
val width = resource!!.intrinsicWidth | ||
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 { | ||
if (!onUpdateFocus(dialogBinding.focusIndicator.getFocus())) { | ||
showFailedFocusMessage() | ||
} | ||
} | ||
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() | ||
} | ||
|
||
private fun Activity.showFailedFocusMessage() { | ||
Toast.makeText(this, R.string.error_failed_set_focus, Toast.LENGTH_SHORT).show() | ||
} |
Oops, something went wrong.
7684f06
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh my gosh, this is super late, but thank you for this!!
Having to switch to something else every time I wanted to upload media was a huge hassle, and I super-appreciate the effort that went into this!