Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
feat: make the floating toolbar draggable
Browse files Browse the repository at this point in the history
  • Loading branch information
SuhasDissa committed Dec 17, 2023
1 parent c7713c2 commit bba05b2
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.bnyro.recorder.canvas_overlay

import android.annotation.SuppressLint
import android.content.Context
import android.content.Context.WINDOW_SERVICE
import android.graphics.PixelFormat
import android.os.Build
import android.util.Log
import android.view.Gravity
import android.view.MotionEvent
import android.view.WindowManager
import androidx.activity.ComponentActivity
import androidx.annotation.RequiresApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.pointer.motionEventSpy
import androidx.compose.ui.platform.ComposeView
import androidx.core.view.isInvisible
import androidx.core.view.isVisible
Expand All @@ -17,10 +21,11 @@ import androidx.lifecycle.setViewTreeViewModelStoreOwner
import androidx.savedstate.setViewTreeSavedStateRegistryOwner
import com.bnyro.recorder.ui.theme.RecordYouTheme

@SuppressLint("ClickableViewAccessibility")
@RequiresApi(Build.VERSION_CODES.O)
class CanvasOverlay(context: Context) {
private var windowManager = context.getSystemService(WINDOW_SERVICE) as WindowManager
private var canvasView = ComposeView(context).apply {
private val windowManager = context.getSystemService(WINDOW_SERVICE) as WindowManager
private val canvasView = ComposeView(context).apply {
val activity = context as ComponentActivity
setViewTreeLifecycleOwner(activity)
setViewTreeViewModelStoreOwner(activity)
Expand All @@ -31,54 +36,86 @@ class CanvasOverlay(context: Context) {
}
}
}
private var toolbarView = ComposeView(context).apply {
private var initialX = 0
private var initialY = 0
private var initialTouchX = 0f
private var initialTouchY = 0f
private val toolbarView = ComposeView(context).apply {
val activity = context as ComponentActivity
setViewTreeLifecycleOwner(activity)
setViewTreeViewModelStoreOwner(activity)
setViewTreeSavedStateRegistryOwner(activity)
setContent {
RecordYouTheme {
ToolbarView(hideCanvas = { hide ->
if (hide) {
hideCanvas()
} else {
showCanvas()
ToolbarView(
Modifier.motionEventSpy { event ->
when (event.action) {
MotionEvent.ACTION_DOWN -> {
initialX = toolbarViewParams.x
initialY = toolbarViewParams.y
initialTouchX = event.rawX
initialTouchY = event.rawY
}

MotionEvent.ACTION_MOVE -> {
val newX = (
initialX -
(event.rawX - initialTouchX).toInt()
)
val newY = (
initialY +
(event.rawY - initialTouchY).toInt()
)
toolbarViewParams.x = newX
toolbarViewParams.y = newY
windowManager.updateViewLayout(this@apply, toolbarViewParams)
}
}
},
hideCanvas = { hide ->
if (hide) {
hideCanvas()
} else {
showCanvas()
}
}
})
)
}
}
}

private val toolbarViewParams =
getWindowLayoutParams(WindowManager.LayoutParams.WRAP_CONTENT).apply {
gravity = Gravity.TOP or Gravity.END
x = 0
y = 0
}

init {
hideCanvas()
}

private fun getWindowLayoutParams(size: Int): WindowManager.LayoutParams {
return WindowManager.LayoutParams(
size,
size,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSPARENT
)
}

fun showAll() {
try {
if (canvasView.windowToken == null && canvasView.parent == null) {
val params = getWindowLayoutParams(WindowManager.LayoutParams.MATCH_PARENT)
windowManager.addView(canvasView, params)
}
if (toolbarView.windowToken == null && toolbarView.parent == null) {
val params = getWindowLayoutParams(WindowManager.LayoutParams.WRAP_CONTENT)
params.gravity = Gravity.TOP or Gravity.END
windowManager.addView(toolbarView, params)
windowManager.addView(toolbarView, toolbarViewParams)
}
} catch (e: Exception) {
Log.e("Show Overlay", e.toString())
}
}

private fun getWindowLayoutParams(size: Int): WindowManager.LayoutParams {
return WindowManager.LayoutParams(
size,
size,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSPARENT
)
}

fun showCanvas() {
canvasView.isVisible = true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.lifecycle.viewmodel.compose.viewModel
Expand All @@ -25,12 +26,13 @@ import com.bnyro.recorder.ui.models.RecorderModel

@Composable
fun ToolbarView(
modifier: Modifier = Modifier,
hideCanvas: (Boolean) -> Unit,
canvasViewModel: CanvasViewModel = viewModel(),
recorderModel: RecorderModel = viewModel()
) {
var currentDrawMode by remember { mutableStateOf(DrawMode.Eraser) }
Card() {
Card(modifier) {
Row {
IconButton(
onClick = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ class RecorderModel : ViewModel() {
Preferences.prefs.getBoolean(Preferences.showOverlayAnnotationToolKey, false)
if (supportsOverlay && showOverlayAnnotation) {
canvasOverlay = CanvasOverlay(context)
canvasOverlay?.showAll()
}
}

Expand Down

0 comments on commit bba05b2

Please sign in to comment.