Skip to content

Commit c237e59

Browse files
committed
enable regex highlights in settings
1 parent b2d3666 commit c237e59

File tree

3 files changed

+76
-70
lines changed

3 files changed

+76
-70
lines changed

app/src/main/kotlin/cc/warlock/warlock3/app/components/ColorPickerDialog.kt

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ fun ColorPickerDialog(
3030
) {
3131
var currentColor by remember { mutableStateOf(initialColor?.let { HsvColor.from(it) }) }
3232
Dialog(
33+
title = "Choose color",
3334
state = state,
3435
onCloseRequest = onCloseRequest,
3536
) {

app/src/main/kotlin/cc/warlock/warlock3/app/ui/settings/HighlightsView.kt

+75-69
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
package cc.warlock.warlock3.app.ui.settings
22

3-
import androidx.compose.foundation.background
4-
import androidx.compose.foundation.gestures.Orientation
5-
import androidx.compose.foundation.gestures.scrollable
3+
import androidx.compose.foundation.*
64
import androidx.compose.foundation.layout.*
7-
import androidx.compose.foundation.rememberScrollState
8-
import androidx.compose.foundation.shape.RoundedCornerShape
95
import androidx.compose.material.*
106
import androidx.compose.material.icons.Icons
117
import androidx.compose.material.icons.filled.Add
128
import androidx.compose.material.icons.filled.Delete
139
import androidx.compose.material.icons.filled.Edit
1410
import androidx.compose.material.icons.filled.Palette
1511
import androidx.compose.runtime.*
12+
import androidx.compose.ui.Alignment
1613
import androidx.compose.ui.Modifier
17-
import androidx.compose.ui.draw.clip
1814
import androidx.compose.ui.graphics.Color
19-
import androidx.compose.ui.text.TextStyle
2015
import androidx.compose.ui.unit.dp
2116
import androidx.compose.ui.window.Dialog
2217
import androidx.compose.ui.window.rememberDialogState
@@ -25,14 +20,11 @@ import cc.warlock.warlock3.app.components.ColorPickerDialog
2520
import cc.warlock.warlock3.app.util.toColor
2621
import cc.warlock.warlock3.core.client.GameCharacter
2722
import cc.warlock.warlock3.core.prefs.HighlightRepository
28-
import cc.warlock.warlock3.core.prefs.PresetRepository
29-
import cc.warlock.warlock3.core.prefs.defaultStyles
3023
import cc.warlock.warlock3.core.prefs.models.Highlight
3124
import cc.warlock.warlock3.core.text.*
3225
import cc.warlock.warlock3.core.util.toWarlockColor
3326
import kotlinx.coroutines.DelicateCoroutinesApi
3427
import kotlinx.coroutines.GlobalScope
35-
import kotlinx.coroutines.flow.map
3628
import kotlinx.coroutines.launch
3729
import kotlinx.coroutines.runBlocking
3830
import java.util.*
@@ -43,7 +35,6 @@ fun HighlightsView(
4335
currentCharacter: GameCharacter?,
4436
allCharacters: List<GameCharacter>,
4537
highlightRepository: HighlightRepository,
46-
presetRepository: PresetRepository,
4738
) {
4839
var selectedCharacter by remember(currentCharacter) { mutableStateOf(currentCharacter) }
4940
val currentCharacterId = selectedCharacter?.id
@@ -54,17 +45,7 @@ fun HighlightsView(
5445
}
5546
.collectAsState(emptyList())
5647
var editingHighlight by remember { mutableStateOf<Highlight?>(null) }
57-
var defaultStyle by remember { mutableStateOf(defaultStyles["default"]) }
58-
val defaultTextColor = defaultStyle?.textColor?.specifiedOrNull()?.toColor() ?: Color.Unspecified
59-
val defaultBackgroundColor = defaultStyle?.backgroundColor?.toColor() ?: Color.Unspecified
60-
LaunchedEffect(currentCharacterId) {
61-
if (currentCharacterId != null) {
62-
presetRepository.observePresetsForCharacter(currentCharacterId).map { it["default"] }
63-
.collect {
64-
defaultStyle = it
65-
}
66-
}
67-
}
48+
6849
Column(Modifier.fillMaxSize()) {
6950
SettingsCharacterSelector(
7051
selectedCharacter = selectedCharacter,
@@ -76,14 +57,12 @@ fun HighlightsView(
7657
Text(text = "Highlights", style = MaterialTheme.typography.h5)
7758
Spacer(Modifier.height(8.dp))
7859
Column(
79-
Modifier.fillMaxWidth().weight(1f).background(defaultBackgroundColor).clip(MaterialTheme.shapes.medium)
60+
Modifier.fillMaxWidth().weight(1f)
8061
) {
8162
highlights.forEach { highlight ->
82-
val color = highlight.styles[0]?.textColor?.specifiedOrNull()?.toColor() ?: defaultTextColor
83-
val background = highlight.styles[0]?.backgroundColor?.toColor() ?: defaultBackgroundColor
8463
ListItem(
8564
text = {
86-
Text(text = highlight.pattern, style = TextStyle(color = color, background = background))
65+
Text(text = highlight.pattern)
8766
},
8867
trailing = {
8968
Row {
@@ -93,7 +72,6 @@ fun HighlightsView(
9372
Icon(
9473
imageVector = Icons.Default.Edit,
9574
contentDescription = "Edit",
96-
tint = defaultTextColor
9775
)
9876
}
9977
Spacer(Modifier.width(8.dp))
@@ -105,7 +83,6 @@ fun HighlightsView(
10583
Icon(
10684
imageVector = Icons.Default.Delete,
10785
contentDescription = "Delete",
108-
tint = defaultTextColor
10986
)
11087
}
11188
}
@@ -136,9 +113,8 @@ fun HighlightsView(
136113
highlight = highlight,
137114
saveHighlight = { newHighlight ->
138115
runBlocking {
139-
val characterId = currentCharacter?.id
140-
if (characterId != null) {
141-
highlightRepository.save(characterId, newHighlight)
116+
if (currentCharacterId != null) {
117+
highlightRepository.save(currentCharacterId, newHighlight)
142118
} else {
143119
highlightRepository.saveGlobal(newHighlight)
144120
}
@@ -163,18 +139,30 @@ fun EditHighlightDialog(
163139
var ignoreCase by remember { mutableStateOf(highlight.ignoreCase) }
164140

165141
Dialog(
166-
state = rememberDialogState(width = 600.dp, height = 300.dp),
142+
state = rememberDialogState(width = 640.dp, height = 480.dp),
167143
onCloseRequest = onClose,
168144
title = "Edit Highlight"
169145
) {
170146
Column(
171-
modifier = Modifier
172-
.scrollable(
173-
state = rememberScrollState(),
174-
orientation = Orientation.Horizontal
175-
)
176-
.padding(24.dp)
147+
modifier = Modifier.padding(24.dp)
177148
) {
149+
Row {
150+
Row(Modifier.clickable { isRegex = false }) {
151+
RadioButton(
152+
selected = !isRegex,
153+
onClick = { isRegex = false },
154+
)
155+
Text(text = "Text highlight", modifier = Modifier.align(Alignment.CenterVertically))
156+
}
157+
Spacer(Modifier.width(16.dp))
158+
Row(Modifier.clickable { isRegex = true }) {
159+
RadioButton(
160+
selected = isRegex,
161+
onClick = { isRegex = true }
162+
)
163+
Text(text = "Regex highlight", modifier = Modifier.align(Alignment.CenterVertically))
164+
}
165+
}
178166
TextField(value = pattern, label = { Text("Pattern") }, onValueChange = { pattern = it })
179167
// Add | to match empty string, then match and see how many groups there are
180168
val groupCount = if (isRegex) try {
@@ -187,43 +175,62 @@ fun EditHighlightDialog(
187175
styles.add(StyleDefinition())
188176
}
189177
}
190-
Column {
191-
for (i in 0 until groupCount) {
192-
val style = styles[i]
193-
Row {
194-
var textColorValue by remember { mutableStateOf(style.textColor.toHexString() ?: "") }
195-
ColorTextField(
196-
label = "Text color",
197-
value = textColorValue,
198-
onValueChanged = {
199-
textColorValue = it
200-
it.toWarlockColor()?.let { color ->
201-
styles[i] = style.copy(textColor = color)
202-
}
178+
Box(modifier = Modifier.fillMaxWidth().weight(1f)) {
179+
val showScrollbar = groupCount > 1
180+
val scrollState = rememberScrollState()
181+
Column(
182+
modifier = if (showScrollbar) Modifier.verticalScroll(scrollState).padding(end = LocalScrollbarStyle.current.thickness) else Modifier
183+
) {
184+
for (i in 0 until groupCount) {
185+
val style = styles[i]
186+
Row {
187+
if (isRegex) {
188+
Text("$i:", Modifier.align(Alignment.CenterVertically))
189+
Spacer(Modifier.width(8.dp))
203190
}
204-
)
205-
206-
Spacer(Modifier.width(16.dp))
207-
var backgroundColorValue by remember {
208-
mutableStateOf(style.backgroundColor.toHexString() ?: "")
209-
}
210-
ColorTextField(
211-
label = "Background color",
212-
value = backgroundColorValue,
213-
onValueChanged = {
214-
backgroundColorValue = it
215-
it.toWarlockColor()?.let { color ->
216-
styles[i] = style.copy(backgroundColor = color)
191+
var textColorValue by remember { mutableStateOf(style.textColor.toHexString() ?: "") }
192+
ColorTextField(
193+
label = "Text color",
194+
value = textColorValue,
195+
onValueChanged = {
196+
textColorValue = it
197+
styles[i] = style.copy(textColor = it.toWarlockColor() ?: WarlockColor.Unspecified)
217198
}
199+
)
200+
201+
Spacer(Modifier.width(16.dp))
202+
var backgroundColorValue by remember {
203+
mutableStateOf(style.backgroundColor.toHexString() ?: "")
218204
}
219-
)
205+
ColorTextField(
206+
label = "Background color",
207+
value = backgroundColorValue,
208+
onValueChanged = {
209+
backgroundColorValue = it
210+
styles[i] =
211+
style.copy(backgroundColor = it.toWarlockColor() ?: WarlockColor.Unspecified)
212+
}
213+
)
214+
}
220215
}
221216
}
217+
if (showScrollbar) {
218+
VerticalScrollbar(
219+
adapter = rememberScrollbarAdapter(scrollState),
220+
modifier = Modifier.align(Alignment.CenterEnd).fillMaxHeight()
221+
)
222+
}
223+
}
224+
if (!isRegex) {
222225
Row {
223226
Checkbox(checked = matchPartialWord, onCheckedChange = { matchPartialWord = it })
224-
Text("Match partial words")
227+
Text(text = "Match partial words", modifier = Modifier.align(Alignment.CenterVertically))
225228
}
226229
}
230+
Row {
231+
Checkbox(checked = ignoreCase, onCheckedChange = { ignoreCase = it })
232+
Text(text = "Ignore case", modifier = Modifier.align(Alignment.CenterVertically))
233+
}
227234
Row(
228235
modifier = Modifier.fillMaxWidth(),
229236
horizontalArrangement = Arrangement.End
@@ -275,7 +282,7 @@ fun ColorTextField(
275282
value = value,
276283
onValueChange = {
277284
onValueChanged(it)
278-
invalidColor = it.toWarlockColor() == null
285+
invalidColor = it.toWarlockColor() == null && it.isNotEmpty()
279286
},
280287
trailingIcon = {
281288
IconButton(
@@ -294,8 +301,7 @@ fun ColorTextField(
294301
isError = invalidColor
295302
)
296303
editColor?.let { (colorText, setColor) ->
297-
val color = colorText.toWarlockColor() ?: WarlockColor.Unspecified
298-
val initialColor = if (color.isUnspecified()) Color.Black else color.toColor()
304+
val initialColor = colorText.toWarlockColor()?.specifiedOrNull()?.toColor() ?: Color.Black
299305
ColorPickerDialog(
300306
initialColor = initialColor,
301307
onCloseRequest = { editColor = null },

app/src/main/kotlin/cc/warlock/warlock3/app/ui/settings/SettingsDialog.kt

-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ fun SettingsDialog(
9999
currentCharacter = currentCharacter,
100100
allCharacters = characters,
101101
highlightRepository = highlightRepository,
102-
presetRepository = presetRepository,
103102
)
104103
SettingsPage.Appearance -> {
105104
AppearanceView(

0 commit comments

Comments
 (0)