Skip to content

Commit

Permalink
feat(custombuttons): Add documentation for custom buttons and mpvkt.l…
Browse files Browse the repository at this point in the history
…ua interface
  • Loading branch information
Secozzi committed Nov 27, 2024
1 parent 64fb1a3 commit 834060a
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 4 deletions.
43 changes: 43 additions & 0 deletions CUSTOMBUTTONS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# CUSTOM BUTTONS

Custom buttons provides a way to execute lua code by pressing a button in the player. mpvKt also provides an interface to interact with some parts of the player.

The interface is defined in a file placed in the `scripts` directory and can be accessed through the `mpvkt` table.

## Lua interface

### `mpvkt.show_text(text)`
Display a message on the player.

### `mpvkt.hide_ui()`
Hide the player UI.

### `mpvkt.show_ui()`
Show the player UI.

### `mpvkt.toggle_ui()`
Toggle the visibility of the player UI.

### `mpvkt.show_subtitle_settings()`
Show the subtitle settings sheet.

### `mpvkt.show_subtitle_delay()`
Show the subtitle delay sheet.

### `mpvkt.show_audio_delay()`
Show the subtitle delay sheet.

### `mpvkt.show_video_filters()`
Show the video filters sheet.

### `mpvkt.set_button_title(text)`
Change the title for the primary custom button.

### `mpvkt.reset_button_title(text)`
Reset the title for the primary custom button.

## Call a custom button from key input or from lua

Custom buttons can be called from key inputs or from other lua scripts, if so desired. This is done through `script-message` with the message `call_button_<id>` for normal press and `call_button_<id>_long` for long press, where `<id>` is the id for the button (shown in top right when editing a button).

Example: `a script-message call_button_1` will call the button of id 1 when `a` is pressed, if added to `input.conf`.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.automirrored.outlined.HelpOutline
import androidx.compose.material.icons.filled.Add
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ExtendedFloatingActionButton
Expand Down Expand Up @@ -48,6 +49,7 @@ fun CustomButtonsScreen(
onClickMoveUp: (CustomButtonEntity) -> Unit,
onClickMoveDown: (CustomButtonEntity) -> Unit,
onTogglePrimary: (CustomButtonEntity) -> Unit,
onClickFaq: () -> Unit,
onNavigateBack: () -> Unit,
) {
val lazyListState = rememberLazyListState()
Expand All @@ -62,6 +64,11 @@ fun CustomButtonsScreen(
Icon(Icons.AutoMirrored.Default.ArrowBack, null)
}
},
actions = {
IconButton(onClick = { onClickFaq() }) {
Icon(Icons.AutoMirrored.Outlined.HelpOutline, null)
}
}
)
},
floatingActionButton = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package live.mehiz.mpvkt.ui.custombuttons
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.platform.LocalUriHandler
import cafe.adriel.voyager.navigator.LocalNavigator
import cafe.adriel.voyager.navigator.currentOrThrow
import kotlinx.collections.immutable.toImmutableList
Expand All @@ -21,6 +22,7 @@ object CustomButtonsScreen : Screen() {
@Composable
override fun Content() {
val navigator = LocalNavigator.currentOrThrow
val uriHandler = LocalUriHandler.current
val viewModel = koinViewModel<CustomButtonsScreenViewModel>()
val playerPreferences = koinInject<PlayerPreferences>()

Expand All @@ -37,6 +39,7 @@ object CustomButtonsScreen : Screen() {
onTogglePrimary = viewModel::togglePrimary,
onClickMoveUp = viewModel::moveUp,
onClickMoveDown = viewModel::moveDown,
onClickFaq = { uriHandler.openUri(CUSTOM_BUTTONS_DOC_URL) },
onNavigateBack = navigator::pop,
)

Expand Down Expand Up @@ -82,3 +85,5 @@ sealed interface CustomButtonDialog {
data class Edit(val customButton: CustomButtonEntity) : CustomButtonDialog
data class Delete(val customButton: CustomButtonEntity) : CustomButtonDialog
}

private const val CUSTOM_BUTTONS_DOC_URL = "https://github.com/abdallahmehiz/mpvKt/blob/main/CUSTOMBUTTONS.md"
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,11 @@ class PlayerActivity : AppCompatActivity() {
appendLine("function button${button.id}()")
appendLine(button.content)
appendLine("end")
appendLine("mp.register_script_message('call_button${button.id}', button${button.id})")
appendLine("mp.register_script_message('call_button_${button.id}', button${button.id})")
appendLine("function button${button.id}long()")
appendLine(button.longPressContent)
appendLine("end")
appendLine("mp.register_script_message('call_button${button.id}long', button${button.id}long)")
appendLine("mp.register_script_message('call_button_${button.id}_long', button${button.id}long)")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -583,9 +583,9 @@ fun Float.normalize(inMin: Float, inMax: Float, outMin: Float, outMax: Float): F
}

fun CustomButtonEntity.execute() {
MPVLib.command(arrayOf("script-message", "call_button$id"))
MPVLib.command(arrayOf("script-message", "call_button_$id"))
}

fun CustomButtonEntity.executeLongClick() {
MPVLib.command(arrayOf("script-message", "call_button${id}long"))
MPVLib.command(arrayOf("script-message", "call_button_${id}_long"))
}

0 comments on commit 834060a

Please sign in to comment.