Skip to content

Commit

Permalink
add support for window maximize and resizable states
Browse files Browse the repository at this point in the history
  • Loading branch information
SpoilerRules committed Sep 14, 2024
1 parent 433efdb commit 2b7b299
Show file tree
Hide file tree
Showing 7 changed files with 374 additions and 83 deletions.
34 changes: 13 additions & 21 deletions ktlint-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<baseline version="1.0">
<file name="src/main/kotlin/com/spoiligaming/explorer/StartupCoordinator.kt">
<error line="88" column="1" source="standard:max-line-length" />
</file>
<file name="src/main/kotlin/com/spoiligaming/explorer/server/ServerFileHandler.kt">
<error line="72" column="1" source="standard:max-line-length" />
<error line="138" column="1" source="standard:max-line-length" />
Expand All @@ -28,6 +25,9 @@
<file name="src/main/kotlin/com/spoiligaming/explorer/ui/components/StationaryView.kt">
<error line="77" column="1" source="standard:max-line-length" />
</file>
<file name="src/main/kotlin/com/spoiligaming/explorer/ui/components/WindowHeaderView.kt">
<error line="118" column="1" source="standard:max-line-length" />
</file>
<file name="src/main/kotlin/com/spoiligaming/explorer/ui/dialogs/MapleValueReplacementDialog.kt">
<error line="82" column="1" source="standard:max-line-length" />
<error line="92" column="1" source="standard:max-line-length" />
Expand All @@ -36,18 +36,21 @@
<error line="72" column="1" source="standard:max-line-length" />
<error line="78" column="1" source="standard:max-line-length" />
<error line="126" column="1" source="standard:max-line-length" />
<error line="243" column="1" source="standard:max-line-length" />
<error line="244" column="1" source="standard:max-line-length" />
<error line="245" column="1" source="standard:max-line-length" />
<error line="254" column="1" source="standard:max-line-length" />
</file>
<file name="src/main/kotlin/com/spoiligaming/explorer/ui/screens/home/InformationContainer.kt">
<error line="41" column="1" source="standard:max-line-length" />
</file>
<file name="src/main/kotlin/com/spoiligaming/explorer/ui/screens/home/list/ServerListView.kt">
<error line="79" column="1" source="standard:max-line-length" />
<error line="80" column="1" source="standard:max-line-length" />
<error line="96" column="1" source="standard:max-line-length" />
<error line="98" column="1" source="standard:max-line-length" />
<error line="108" column="1" source="standard:max-line-length" />
<error line="81" column="1" source="standard:max-line-length" />
<error line="90" column="1" source="standard:max-line-length" />
<error line="112" column="1" source="standard:max-line-length" />
<error line="114" column="1" source="standard:max-line-length" />
<error line="124" column="1" source="standard:max-line-length" />
</file>
<file name="src/main/kotlin/com/spoiligaming/explorer/ui/screens/settings/SettingsTheme.kt">
<error line="170" column="1" source="standard:max-line-length" />
</file>
<file name="src/main/kotlin/com/spoiligaming/explorer/ui/state/DialogController.kt">
<error line="105" column="1" source="standard:max-line-length" />
Expand All @@ -61,15 +64,4 @@
<error line="229" column="1" source="standard:max-line-length" />
<error line="243" column="1" source="standard:max-line-length" />
</file>
<file name="src/main/kotlin/com/spoiligaming/explorer/ui/widgets/MapleToggleSwitch.kt">
<error line="52" column="1" source="standard:max-line-length" />
<error line="70" column="1" source="standard:max-line-length" />
</file>
<file name="src/main/kotlin/com/spoiligaming/explorer/ui/widgets/MapleVerticalScrollbar.kt">
<error line="28" column="1" source="standard:max-line-length" />
<error line="47" column="1" source="standard:max-line-length" />
</file>
<file name="src/main/kotlin/com/spoiligaming/explorer/utils/ColorPaletteUtility.kt">
<error line="37" column="1" source="standard:max-line-length" />
</file>
</baseline>
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,20 @@ data class SettingsAdvanced(
var compressServerFile: Boolean = false,
)

@Serializable
data class PropertiesWindow(
var previousScale: String = "100%",
var wasPreviousScaleResizable: Boolean = false,
var isMaximized: Boolean = false,
var currentWindowSize: Pair<Int, Int>? = null,
)

@Serializable
data class ConfigurationHandler(
@SerialName("General") var generalSettings: SettingsGeneral = SettingsGeneral(),
@SerialName("Theme") var themeSettings: SettingsTheme = SettingsTheme(),
@SerialName("Advanced") var advancedSettings: SettingsAdvanced = SettingsAdvanced(),
@SerialName("WindowProperties") var windowProperties: PropertiesWindow = PropertiesWindow(),
) {
companion object {
private var configFactoryInstance: ConfigurationHandler? = null
Expand Down
135 changes: 103 additions & 32 deletions src/main/kotlin/com/spoiligaming/explorer/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,26 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.WindowDecoration
import androidx.compose.ui.window.WindowPosition
import androidx.compose.ui.window.application
import androidx.compose.ui.window.rememberWindowState
import com.spoiligaming.explorer.ui.MapleColorPalette
import com.spoiligaming.explorer.ui.state.DialogController
import com.spoiligaming.explorer.utils.WindowUtility
import com.spoiligaming.explorer.utils.WindowUtility.centerOnScreen
import com.spoiligaming.logging.Logger
import org.jetbrains.skiko.OS
import org.jetbrains.skiko.SkiaLayer
import org.jetbrains.skiko.hostOs
import java.awt.GraphicsEnvironment
import java.awt.Toolkit
import java.awt.event.ComponentAdapter
import java.awt.event.ComponentEvent
import javax.swing.JFrame

val controlButtonAsString: String =
Expand All @@ -36,6 +43,9 @@ val controlButtonAsString: String =

lateinit var windowFrame: JFrame
lateinit var windowSize: Pair<Dp, Dp>
var isWindowMaximized by mutableStateOf(
ConfigurationHandler.getInstance().windowProperties.isMaximized,
)

var disableIconIndexing = false
var disableServerInfoIndexing = false
Expand Down Expand Up @@ -79,56 +89,117 @@ fun main(args: Array<String>) {

args.forEach { argumentActions[it]?.invoke() }

val windowScale by mutableStateOf(
ConfigurationHandler.getInstance().themeSettings.windowScale.run {
when (this) {
"150%" -> 1.5f
"125%" -> 1.25f
"100%" -> 1f
else ->
toFloatOrNull()
?: 1f.also {
Logger.printWarning(
"Invalid window scale value. Falling back to 100%.",
)
}
application {
val windowScale by mutableStateOf(
ConfigurationHandler.getInstance().themeSettings.windowScale.let { scale ->
WindowUtility.windowScaleMapping[scale]
?: WindowUtility.windowScaleMapping[
ConfigurationHandler.getInstance()
.windowProperties.previousScale,
]
?: scale.toFloatOrNull()
?: 1f
},
)

val (defaultWidth, defaultHeight) =
(800 * windowScale).toInt() to (600 * windowScale).toInt()

val (windowWidth, windowHeight) =
when (ConfigurationHandler.getInstance().themeSettings.windowScale) {
"Maximized" -> WindowUtility.getUsableScreenSize().also { isWindowMaximized = true }
"Resizable" ->
ConfigurationHandler.getInstance().windowProperties.currentWindowSize?.let {
it.first to it.second
} ?: (defaultWidth to defaultHeight)
else -> defaultWidth to defaultHeight
}
},
)
val screenSize = Toolkit.getDefaultToolkit().screenSize
val windowWidth = (800 * windowScale).toInt().dp
val windowHeight = (600 * windowScale).toInt().dp

application {
val state =
val screenSize = Toolkit.getDefaultToolkit().screenSize

val windowState =
rememberWindowState(
width = windowWidth,
height = windowHeight,
width = windowWidth.dp,
height = windowHeight.dp,
position =
WindowPosition(
((screenSize.width - windowWidth.value.toInt()) / 2).dp,
((screenSize.height - windowHeight.value.toInt()) / 2).dp,
),
if (isWindowMaximized) {
WindowPosition.PlatformDefault
} else {
WindowPosition(
((screenSize.width - windowWidth.dp.value) / 2).dp,
((screenSize.height - windowHeight.dp.value) / 2).dp,
)
},
)

Window(
onCloseRequest = ::exitApplication,
visible = true,
title = "Server List Explorer",
resizable = false,
undecorated = true,
decoration = WindowDecoration.Undecorated(8.dp),
transparent = true,
state = state,
state = windowState,
) {
if (ConfigurationHandler.getInstance().themeSettings.windowScale == "Resizable") {
window.isResizable = true
}
windowFrame = window
windowSize = remember { Pair(state.size.width, state.size.height) }
windowSize =
if (
ConfigurationHandler.getInstance().themeSettings.windowScale in
listOf("Resizable", "Maximized")
) {
with(LocalDensity.current) {
Pair(window.size.width.toDp(), window.size.height.toDp())
}
} else {
remember {
Pair(windowState.size.width, windowState.size.height)
}
}

window.isResizable =
ConfigurationHandler.getInstance().themeSettings.windowScale == "Resizable"
window.apply {
val screenInsets =
Toolkit.getDefaultToolkit()
.getScreenInsets(
GraphicsEnvironment.getLocalGraphicsEnvironment()
.defaultScreenDevice.defaultConfiguration,
)

if (ConfigurationHandler.getInstance().themeSettings.windowScale == "Maximized") {
setLocation(screenInsets.left, screenInsets.top)
} else {
centerOnScreen()
}
addComponentListener(
object : ComponentAdapter() {
override fun componentResized(event: ComponentEvent) {
if (
ConfigurationHandler.getInstance()
.themeSettings
.windowScale == "Resizable"
) {
val newSize = event.component.size
ConfigurationHandler.updateValue {
windowProperties.currentWindowSize =
newSize.width to newSize.height
}
}
}
},
)
}

val roundedCornerShape =
RoundedCornerShape(if (isWindowMaximized) 0.dp else 24.dp)
Surface(
modifier =
Modifier
.fillMaxSize()
.clip(RoundedCornerShape(24.dp))
.background(Color.Transparent, RoundedCornerShape(24.dp)),
.clip(roundedCornerShape)
.background(Color.Transparent, roundedCornerShape),
color = MapleColorPalette.menu,
) {
Box(modifier = Modifier.fillMaxSize()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Fullscreen
import androidx.compose.material.icons.filled.FullscreenExit
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
Expand All @@ -26,13 +30,16 @@ import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.spoiligaming.explorer.ConfigurationHandler
import com.spoiligaming.explorer.SoftwareInformation
import com.spoiligaming.explorer.isBackupRestoreInProgress
import com.spoiligaming.explorer.isWindowMaximized
import com.spoiligaming.explorer.ui.MapleColorPalette
import com.spoiligaming.explorer.ui.fonts.FontFactory
import com.spoiligaming.explorer.ui.icons.IconFactory
import com.spoiligaming.explorer.ui.navigation.NavigationController
import com.spoiligaming.explorer.ui.navigation.Screen
import com.spoiligaming.explorer.utils.WindowUtility
import com.spoiligaming.explorer.windowFrame
import java.awt.Cursor
import javax.swing.JFrame
Expand Down Expand Up @@ -82,6 +89,7 @@ fun WindowHeaderView(allowNavigation: Boolean) {
Spacer(Modifier.weight(1f))
Row(horizontalArrangement = Arrangement.spacedBy(10.dp)) {
ControlButton(ActionType.MINIMIZE)
ControlButton(ActionType.MAXIMIZE)
ControlButton(ActionType.EXIT)
}
}
Expand All @@ -100,10 +108,31 @@ fun WindowHeaderView(allowNavigation: Boolean) {
private fun ControlButton(type: ActionType) =
Button(
onClick = {
if (type == ActionType.MINIMIZE) {
windowFrame.extendedState = JFrame.ICONIFIED
} else {
exitProcess(0)
when (type) {
ActionType.EXIT -> exitProcess(0)
ActionType.MAXIMIZE -> {
val currentScale = ConfigurationHandler.getInstance().themeSettings.windowScale

if (isWindowMaximized) {
WindowUtility.restoreWindowSize(
ConfigurationHandler.getInstance().windowProperties.wasPreviousScaleResizable,
)
} else {
ConfigurationHandler.updateValue {
themeSettings.windowScale = "Maximized"
if (currentScale != "Resizable") {
windowProperties.previousScale = currentScale
if (currentScale != "Maximized") {
windowProperties.wasPreviousScaleResizable = false
}
} else {
windowProperties.wasPreviousScaleResizable = true
}
}
WindowUtility.maximizeWindow()
}
}
ActionType.MINIMIZE -> windowFrame.extendedState = JFrame.ICONIFIED
}
},
modifier = Modifier.size(60.dp, 40.dp).pointerHoverIcon(PointerIcon.Hand),
Expand All @@ -114,17 +143,31 @@ private fun ControlButton(type: ActionType) =
contentColor = MapleColorPalette.fadedText,
),
) {
Text(
text = if (type == ActionType.MINIMIZE) "_" else "x",
color = MapleColorPalette.fadedText,
style =
TextStyle(
fontFamily = FontFactory.comfortaaMedium,
fontWeight = FontWeight.Normal,
fontSize = 17.sp,
),
modifier = Modifier.padding(horizontal = 10.dp),
)
if (type != ActionType.MAXIMIZE) {
Text(
text = if (type == ActionType.MINIMIZE) "_" else "x",
color = MapleColorPalette.fadedText,
style =
TextStyle(
fontFamily = FontFactory.comfortaaMedium,
fontWeight = FontWeight.Normal,
fontSize = 17.sp,
),
modifier = Modifier.padding(horizontal = 10.dp),
)
} else {
Icon(
imageVector =
if (isWindowMaximized) {
Icons.Filled.FullscreenExit
} else {
Icons.Filled.Fullscreen
},
contentDescription = "Icon for window maximize/restore button",
tint = MapleColorPalette.fadedText,
modifier = Modifier.size(17.dp),
)
}
}

@Composable
Expand Down Expand Up @@ -190,5 +233,6 @@ private fun WindowTitle() =

enum class ActionType {
EXIT,
MAXIMIZE,
MINIMIZE,
}
Loading

0 comments on commit 2b7b299

Please sign in to comment.