Skip to content

Commit

Permalink
use radio button to switch between light/dark mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Joseph5610 committed Mar 17, 2023
1 parent a875812 commit d2d69e4
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 22 deletions.
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ dependencies {
implementation("io.humble:humble-video-all:$humbleVersion")
implementation("uk.co.caprica:vlcj:$vlcjVersion")

implementation("com.github.Dansoftowner:jSystemThemeDetector:3.8")

testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:$junitVersion")
testImplementation("org.junit.jupiter:junit-jupiter-api:$junitVersion")
testImplementation("org.testfx:testfx-core:$testFxVersion")
Expand Down
4 changes: 4 additions & 0 deletions src/main/kotlin/online/hudacek/fxradio/FxRadio.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import online.hudacek.fxradio.util.macos.MacUtils
import online.hudacek.fxradio.util.saveProperties
import online.hudacek.fxradio.util.value
import online.hudacek.fxradio.viewmodel.PlayerViewModel
import online.hudacek.fxradio.viewmodel.PreferencesViewModel
import org.apache.logging.log4j.LogManager
import tornadofx.App
import tornadofx.FX
Expand Down Expand Up @@ -71,6 +72,7 @@ open class FxRadio(

private val trayIcon: TrayIcon by inject()
private val playerViewModel: PlayerViewModel by inject()
private val preferencesViewModel: PreferencesViewModel by inject()

/**
* override app.config path to ${user.home}/.fxradio
Expand Down Expand Up @@ -106,7 +108,9 @@ open class FxRadio(
// Disable built-in tornadofx layout debugger
FX.layoutDebuggerShortcut = null
}

trayIcon.createIcon()
MacUtils.setAppearance(preferencesViewModel.darkModeProperty.value)
}

override fun stop() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,20 @@ class PreferencesFragment : BaseFragment() {

field(messages["menu.app.darkmode"]) {
labelContainer.alignment = Pos.CENTER_RIGHT
checkbox {
bind(preferencesViewModel.darkModeProperty)
action {
preferencesViewModel.commit()
togglegroup {
radiobutton(messages["menu.app.light"], value = false, group = this) {
action {
preferencesViewModel.commit()
}
addClass(Styles.colorRadioButton)
}
radiobutton(messages["menu.app.dark"], value = true, group = this) {
action {
preferencesViewModel.commit()
}
addClass(Styles.colorRadioButton)
}
bind(preferencesViewModel.darkModeProperty)
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/online/hudacek/fxradio/ui/style/Styles.kt
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ class Styles : Stylesheet() {
borderRadius += box(4.px)
backgroundColor += c(colors.background)
borderColor += box(c(colors.backgroundBorder))
maxWidth = 280.px
minWidth = 280.px
prefWidth = 280.px
maxWidth = 300.px
minWidth = 300.px
prefWidth = 300.px
alignment = Pos.CENTER
prefHeight = 40.px
minHeight = 40.px
Expand Down
8 changes: 5 additions & 3 deletions src/main/kotlin/online/hudacek/fxradio/ui/style/StylesDark.kt
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ class StylesDark : Stylesheet() {
borderRadius += box(4.px)
backgroundColor += c(colors.playerBox)
borderColor += box(c(colors.backgroundBorder))
maxWidth = 280.px
minWidth = 280.px
prefWidth = 280.px
maxWidth = 300.px
minWidth = 300.px
prefWidth = 300.px
alignment = Pos.CENTER
prefHeight = 40.px
minHeight = 40.px
Expand Down Expand Up @@ -472,6 +472,8 @@ class StylesDark : Stylesheet() {
}

colorRadioButton {
baseColor = c(colors.primary)
textFill = c(colors.label)
and(selected) {
radio {
dot {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ package online.hudacek.fxradio.ui.view.library

import javafx.beans.property.BooleanProperty
import javafx.scene.layout.Priority
import javafx.scene.text.Font
import javafx.util.Duration
import online.hudacek.fxradio.ui.BaseFragment
import online.hudacek.fxradio.ui.style.Styles
import online.hudacek.fxradio.ui.util.make
import online.hudacek.fxradio.ui.util.showWhen
import online.hudacek.fxradio.ui.util.smallLabel
import online.hudacek.fxradio.util.toObservable
import org.controlsfx.glyphfont.FontAwesome
import org.controlsfx.glyphfont.Glyph
import tornadofx.addClass
import tornadofx.hbox
import tornadofx.hgrow
import tornadofx.onLeftClick
Expand Down Expand Up @@ -82,6 +85,7 @@ class LibraryTitleFragment : BaseFragment() {
override val root = hbox {
smallLabel(libraryTitle) {
paddingLeft = 10.0
addClass(Styles.boldText)
}
region { hgrow = Priority.ALWAYS }
vbox {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class StationsHeaderView : BaseView() {
paddingTop = 8.0
paddingBottom = 8.0
addClass(Styles.subheader)
addClass(Styles.boldText)
}

label(searchViewModel.queryBinding) {
Expand All @@ -84,6 +85,7 @@ class StationsHeaderView : BaseView() {
}
addClass(Styles.grayTextColor)
addClass(Styles.subheader)
addClass(Styles.boldText)
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions src/main/kotlin/online/hudacek/fxradio/util/macos/MacUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,38 @@

package online.hudacek.fxradio.util.macos

import de.jangassen.jfa.appkit.NSAppearance
import de.jangassen.jfa.appkit.NSApplication
import online.hudacek.fxradio.util.Command
import org.controlsfx.tools.Platform

object MacUtils {

val isMac = Platform.getCurrent() == Platform.OSX

private val darkAppearance by lazy {
NSAppearance.appearanceNamed(NSAppearance.NSAppearanceName.NSAppearanceNameVibrantDark)
}

private val lightAppearance by lazy {
NSAppearance.appearanceNamed(NSAppearance.NSAppearanceName.NSAppearanceNameVibrantLight)
}

val isSystemDarkMode: Boolean
get() = Command("defaults read -g AppleInterfaceStyle").exec() == "Dark"

val systemAccentColor: Int
get() = Command("defaults read -g AppleAccentColor").exec().toIntOrNull() ?: Int.MIN_VALUE


fun setAppearance(isDark: Boolean) {
if (!isMac) return
NSApplication.sharedApplication().setAppearance(
if (isDark) {
darkAppearance
} else {
lightAppearance
}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package online.hudacek.fxradio.util.macos

import de.jangassen.MenuToolkit
import de.jangassen.model.AppearanceMode
import javafx.scene.control.Menu
import javafx.scene.control.MenuBar
import javafx.scene.control.MenuItem
Expand All @@ -35,9 +34,7 @@ import tornadofx.get
*/
class NSMenuHelper : Component() {

private val tk = MenuToolkit.toolkit(FX.locale).apply {
setAppearanceMode(AppearanceMode.AUTO)
}
private val tk by lazy { MenuToolkit.toolkit(FX.locale) }

/**
* macOS style default About Menu
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,14 @@

package online.hudacek.fxradio.viewmodel

import com.jthemedetecor.OsThemeDetector
import javafx.beans.property.BooleanProperty
import javafx.beans.property.ObjectProperty
import online.hudacek.fxradio.FxRadio
import online.hudacek.fxradio.ui.style.AccentColor
import online.hudacek.fxradio.ui.style.Appearance
import online.hudacek.fxradio.util.Properties
import online.hudacek.fxradio.util.Property
import online.hudacek.fxradio.util.*
import online.hudacek.fxradio.util.macos.MacUtils
import online.hudacek.fxradio.util.reloadStylesheets
import online.hudacek.fxradio.util.save
import online.hudacek.fxradio.util.value
import tornadofx.booleanProperty
import tornadofx.property

Expand Down Expand Up @@ -64,5 +61,6 @@ class PreferencesViewModel : BaseViewModel<Preferences>(Preferences()) {
Properties.UseTrayIcon.save(useTrayIconProperty.value)
Properties.DarkMode.save(darkModeProperty.value)
reloadStylesheets(darkModeProperty.value)
MacUtils.setAppearance(darkModeProperty.value)
}
}
9 changes: 6 additions & 3 deletions src/main/resources/Messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ close=Close
cancel=Cancel
save=Save
open=Open
search=Search radio directory
search=Search
loading=Loading...
countries=Countries
library=Library
Expand Down Expand Up @@ -69,14 +69,17 @@ app.preferences=Preferences
app.appearance=App Appearance
app.useSystemColor=Use system accent color:
app.accentColor=Accent color:
app.darkMode=Dark Mode
app.darkMode=App Theme
app.trayIcon=System Tray Icon
app.trayIcon.enable=Enable Tray Icon:
menu.app.quit=Quit
menu.app.about=About
menu.app.attributions=Attributions
menu.app.server=Change API server
menu.app.darkmode=Enable dark mode:
menu.app.darkmode=Select App Theme:
menu.app.dark=Dark
menu.app.light=Light
menu.app.system=System
menu.player.controls=_Playback
menu.player.stop=Stop
menu.player.start=Play
Expand Down

0 comments on commit d2d69e4

Please sign in to comment.