Skip to content

Commit

Permalink
feat: add option to disable auto preview refresh + add manual refresh…
Browse files Browse the repository at this point in the history
… button (allow support for bigger documents)

Signed-off-by: Stephen L. <[email protected]>
  • Loading branch information
lrq3000 committed May 20, 2024
1 parent c095078 commit 3e34f23
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 3 deletions.
18 changes: 18 additions & 0 deletions app/src/main/kotlin/dev/soupslurpr/beautyxt/BeauTyXT.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material.icons.filled.Info
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.material.icons.filled.Refresh
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material.icons.filled.Share
import androidx.compose.material3.AlertDialog
Expand Down Expand Up @@ -167,6 +168,7 @@ fun BeauTyXTAppBar(
deleteFileDialogConfirmButton: @Composable () -> Unit,
deleteFileDialogDismissButton: @Composable () -> Unit,

onTypstProjectRefreshButtonClicked: () -> Unit,
onTypstProjectOpenAnotherFileInTheProjectButtonClicked: () -> Unit,
onTypstProjectCreateAndOpenAnotherFileInTheProjectButtonClicked: () -> Unit,

Expand Down Expand Up @@ -225,6 +227,15 @@ fun BeauTyXTAppBar(
}
)
}
IconButton(
onClick = onTypstProjectRefreshButtonClicked,
content = {
Icon(
imageVector = Icons.Filled.Refresh,
contentDescription = stringResource(R.string.refresh)
)
}
)
IconButton(
onClick = onTypstProjectCreateAndOpenAnotherFileInTheProjectButtonClicked,
content = {
Expand Down Expand Up @@ -618,6 +629,7 @@ fun BeauTyXTApp(
()
) {
if (it != null) {
Log.d("BeauTyXT", "setTypstCurrentOpenedPathLauncher: $it")
typstProjectViewModel.refreshProjectFiles(context)
typstProjectViewModel.setCurrentOpenedPath(it, context.contentResolver)
typstProjectViewModel.setTypstProjectFileText(
Expand Down Expand Up @@ -1271,6 +1283,12 @@ ${
)
},

onTypstProjectRefreshButtonClicked = {
// Refresh the Typst project files
typstProjectViewModel.refreshProjectFiles(context)
// Refresh the preview (SVG rendering)
typstProjectViewModel.renderProjectToSvgs(typstProjectViewModel.rustService!!)
},
onTypstProjectOpenAnotherFileInTheProjectButtonClicked = {
setTypstCurrentOpenedPathLauncher.launch(arrayOf("*/*"))
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ data class PreferencesUiState(
mutableStateOf(true)
),

/** Automatically refresh the render of the preview on typing (can be slow on big documents)
*/
val autoPreviewOnTyping: Pair<Preferences.Key<Boolean>, MutableState<Boolean>> = Pair(
(booleanPreferencesKey("AUTO_PREVIEW_ON_TYPING")),
mutableStateOf(true)
),

/** Experimental feature that shows a button when a markdown file is open which will toggle
* a fullscreen view of the rendered markdown preview.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ class PreferencesViewModel(private val dataStore: DataStore<Preferences>) : View
.typstProjectShowWarningsAndErrors.second.value
)
),
autoPreviewOnTyping = Pair(
uiState.value.autoPreviewOnTyping.first,
mutableStateOf(
settings[uiState.value.autoPreviewOnTyping.first] ?: uiState.value.autoPreviewOnTyping
.second.value
)
),
experimentalFeaturePreviewRenderedMarkdownInFullscreen = Pair(
uiState.value.experimentalFeaturePreviewRenderedMarkdownInFullscreen.first,
mutableStateOf(
Expand Down
17 changes: 17 additions & 0 deletions app/src/main/kotlin/dev/soupslurpr/beautyxt/ui/SettingsScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,23 @@ fun SettingsScreen(
}
}
)
SettingsItem(
name = stringResource(R.string.auto_preview_on_typing_setting_name),
description = stringResource(
R.string.auto_preview_on_typing_setting_description,
stringResource(R.string.typst_project)
),
hasSwitch = true,
checked = preferencesUiState.autoPreviewOnTyping.second.value,
onCheckedChange = {
coroutineScope.launch {
preferencesViewModel.setPreference(
preferencesUiState.autoPreviewOnTyping
.first, it
)
}
}
)
}

Column {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ fun TypstProjectScreen(

typstProjectViewModel.updateProjectFileWithNewText(it, currentOpenedPath)

typstProjectViewModel.renderProjectToSvgs(typstProjectViewModel.rustService!!)
if (preferencesUiState.autoPreviewOnTyping.second.value) {
typstProjectViewModel.renderProjectToSvgs(typstProjectViewModel.rustService!!)
}
}

Column(
Expand Down Expand Up @@ -151,7 +153,9 @@ fun TypstProjectScreen(

typstProjectViewModel.updateProjectFileWithNewText(it, currentOpenedPath)

typstProjectViewModel.renderProjectToSvgs(typstProjectViewModel.rustService!!)
if (preferencesUiState.autoPreviewOnTyping.second.value) {
typstProjectViewModel.renderProjectToSvgs(typstProjectViewModel.rustService!!)
}
}

Column(
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@
<string name="create_and_edit_another_file_in_the_typst_project" tools:ignore="MissingTranslation">Create and edit another file in the Typst project</string>
<string name="typst_project_show_warnings_and_errors_setting_name" tools:ignore="MissingTranslation">Show warnings and errors</string>
<string name="typst_project_show_warnings_and_errors_setting_description" tools:ignore="MissingTranslation">Show warnings and errors below the %1$s preview</string>
<string name="auto_preview_on_typing_setting_name" tools:ignore="MissingTranslation">Auto update the preview on typing</string>
<string name="auto_preview_on_typing_setting_description" tools:ignore="MissingTranslation">Refresh the render after each character input (slow for big documents), otherwise you will need to refresh manually</string>
<string name="rendered_typst_project_preview" tools:ignore="MissingTranslation">Rendered Typst project preview</string>
<string name="toggle_previewing_typst_project_in_fullscreen" tools:ignore="MissingTranslation">Toggle previewing Typst project in fullscreen</string>
<string name="fullscreen_typst_preview_button_setting_name" tools:ignore="MissingTranslation">Fullscreen Typst Preview Button</string>
Expand All @@ -97,5 +99,6 @@
<string name="donation_setting_description" tools:ignore="MissingTranslation">View how to donate to BeauTyXT\'s lead developer in-app</string>
<string name="view_credits_for_rust_library_for_plain_text_and_markdown_support" tools:ignore="MissingTranslation">View Credits for Rust library for Plain Text and Markdown support</string>
<string name="view_credits_for_rust_library_for_typst_support" tools:ignore="MissingTranslation">View Credits for Rust library for Typst support</string>
<string name="typst_rust_library_credits">Typst Rust Library Credits</string>
<string name="typst_rust_library_credits" translatable="false">Typst Rust Library Credits</string>
<string name="refresh">Refresh</string>
</resources>

0 comments on commit 3e34f23

Please sign in to comment.