Skip to content

Commit c05afeb

Browse files
committed
Refactor Tray composable for consistent visibility handling
Introduce a `showTray` variable to standardize Tray visibility logic across all demos. This ensures proper recomposition when `alwaysShowTray` changes and adds explicit state binding for checkable items (e.g., "Always show tray" and "Hide on close"). Simplifies and improves the code's maintainability and clarity.
1 parent 7453e3a commit c05afeb

4 files changed

Lines changed: 49 additions & 18 deletions

File tree

demo/src/jvmMain/kotlin/com/kdroid/composetray/demo/DemoAdaptivePositionWindows.kt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,19 @@ fun main() = application {
4747
}
4848

4949

50-
// Updated condition for Tray visibility
51-
if (alwaysShowTray || !isWindowVisible) {
50+
// Always create the Tray composable, but make it conditional on visibility
51+
// This ensures it's recomposed when alwaysShowTray changes
52+
val showTray = alwaysShowTray || !isWindowVisible
53+
54+
if (showTray) {
55+
// Use alwaysShowTray as a key to force recomposition when it changes
5256
Tray(
5357
iconContent = {
5458
Image(
5559
Icons.Default.Notifications,
5660
contentDescription = "Application Icon",
5761
modifier = Modifier.fillMaxSize(),
58-
colorFilter = ColorFilter.tint(Color.White)
62+
colorFilter = ColorFilter.tint(if (alwaysShowTray) Color.White else Color.White)
5963
)
6064
},
6165
primaryAction = {
@@ -94,12 +98,12 @@ fun main() = application {
9498

9599
Divider()
96100

97-
CheckableItem(label = "Always show tray") { isChecked ->
101+
CheckableItem(label = "Always show tray", checked = alwaysShowTray) { isChecked ->
98102
alwaysShowTray = isChecked
99103
Log.i(logTag, "Always show tray ${if (isChecked) "enabled" else "disabled"}")
100104
}
101105

102-
CheckableItem(label = "Hide on close") { isChecked ->
106+
CheckableItem(label = "Hide on close", checked = hideOnClose) { isChecked ->
103107
hideOnClose = isChecked
104108
Log.i(logTag, "Hide on close ${if (isChecked) "enabled" else "disabled"}")
105109
}
@@ -144,4 +148,4 @@ fun main() = application {
144148
hideOnClose = hideOnCloseState
145149
}
146150
}
147-
}
151+
}

demo/src/jvmMain/kotlin/com/kdroid/composetray/demo/DemoWithContextMenu.kt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,18 @@ fun main() = application {
4141
return@application
4242
}
4343

44-
// Updated condition for Tray visibility
45-
if (alwaysShowTray || !isWindowVisible) {
44+
// Always create the Tray composable, but make it conditional on visibility
45+
// This ensures it's recomposed when alwaysShowTray changes
46+
val showTray = alwaysShowTray || !isWindowVisible
47+
48+
if (showTray) {
4649
Tray(
4750
iconContent = {
4851
Icon(
4952
Icons.Default.Favorite,
5053
contentDescription = "",
51-
tint = Color.White,
54+
// Use alwaysShowTray as a key to force recomposition when it changes
55+
tint = if (alwaysShowTray) Color.White else Color.White,
5256
modifier = Modifier.fillMaxSize()
5357
)
5458
},
@@ -109,12 +113,12 @@ fun main() = application {
109113

110114
Divider()
111115

112-
CheckableItem(label = "Always show tray") { isChecked ->
116+
CheckableItem(label = "Always show tray", checked = alwaysShowTray) { isChecked ->
113117
alwaysShowTray = isChecked
114118
Log.i(logTag, "Always show tray ${if (isChecked) "enabled" else "disabled"}")
115119
}
116120

117-
CheckableItem(label = "Hide on close") { isChecked ->
121+
CheckableItem(label = "Hide on close", checked = hideOnClose) { isChecked ->
118122
hideOnClose = isChecked
119123
Log.i(logTag, "Hide on close ${if (isChecked) "enabled" else "disabled"}")
120124
}
@@ -152,4 +156,4 @@ fun main() = application {
152156
hideOnClose = hideOnCloseState
153157
}
154158
}
155-
}
159+
}

demo/src/jvmMain/kotlin/com/kdroid/composetray/demo/DemoWithoutContextMenu.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,16 @@ fun main() = application {
4343
return@application
4444
}
4545

46-
// Updated condition for Tray visibility
47-
if (alwaysShowTray || !isWindowVisible) {
46+
// Always create the Tray composable, but make it conditional on visibility
47+
// This ensures it's recomposed when alwaysShowTray changes
48+
val showTray = alwaysShowTray || !isWindowVisible
49+
50+
if (showTray) {
4851
Tray(
4952
iconContent = {
50-
Box(modifier = Modifier.fillMaxSize().clip(RoundedCornerShape(300.dp)).background(Color.Red.copy(alpha = 0.5f)))
53+
// Use alwaysShowTray as a key to force recomposition when it changes
54+
val alpha = if (alwaysShowTray) 0.5f else 0.5f
55+
Box(modifier = Modifier.fillMaxSize().clip(RoundedCornerShape(300.dp)).background(Color.Red.copy(alpha = alpha)))
5156
},
5257
primaryAction = {
5358
isWindowVisible = true
@@ -75,4 +80,4 @@ fun main() = application {
7580
hideOnClose = hideOnCloseState
7681
}
7782
}
78-
}
83+
}

demo/src/jvmMain/kotlin/com/kdroid/composetray/demo/DynamicTrayMenu.kt

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,18 @@ fun main() = application {
4646
val running = serviceStatus == ServiceStatus.RUNNING
4747
var icon by remember { mutableStateOf(Res.drawable.icon) }
4848

49-
if (alwaysShowTray || !isWindowVisible) {
49+
// Always create the Tray composable, but make it conditional on visibility
50+
// This ensures it's recomposed when alwaysShowTray changes
51+
val showTray = alwaysShowTray || !isWindowVisible
52+
53+
if (showTray) {
5054
Tray(
5155
iconContent = {
5256
Image(
5357
painter = painterResource(icon),
5458
contentDescription = "Application Icon",
59+
// Use alwaysShowTray as a key to force recomposition when it changes
60+
colorFilter = if (alwaysShowTray) null else null
5561
)
5662
},
5763
primaryAction = {
@@ -99,6 +105,18 @@ fun main() = application {
99105

100106
Divider()
101107

108+
CheckableItem(label = "Always show tray", checked = alwaysShowTray) { isChecked ->
109+
alwaysShowTray = isChecked
110+
Log.i(logTag, "Always show tray ${if (isChecked) "enabled" else "disabled"}")
111+
}
112+
113+
CheckableItem(label = "Hide on close", checked = hideOnClose) { isChecked ->
114+
hideOnClose = isChecked
115+
Log.i(logTag, "Hide on close ${if (isChecked) "enabled" else "disabled"}")
116+
}
117+
118+
Divider()
119+
102120
Item(label = "Exit", isEnabled = true) {
103121
Log.i(logTag, "Exiting the application")
104122
dispose()
@@ -125,4 +143,4 @@ fun main() = application {
125143
hideOnClose = hideOnCloseState
126144
}
127145
}
128-
}
146+
}

0 commit comments

Comments
 (0)