Skip to content

Commit 61b87ba

Browse files
committed
Update TrayApp content to use DialogWindowScope for enhanced composable context handling
- Changed the `content` parameter type in `TrayApp` and related methods from `@Composable () -> Unit` to `@Composable DialogWindowScope.() -> Unit`. - Updated platform-specific and common implementations to support the new scope. - Removed redundant documentation blocks for improved clarity and maintainability.
1 parent d24257b commit 61b87ba

1 file changed

Lines changed: 31 additions & 93 deletions

File tree

  • src/commonMain/kotlin/com/kdroid/composetray/tray/api

src/commonMain/kotlin/com/kdroid/composetray/tray/api/TrayApp.kt

Lines changed: 31 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import androidx.compose.ui.window.ApplicationScope
2323
import androidx.compose.ui.window.DialogWindow
2424
import androidx.compose.ui.window.WindowPosition
2525
import androidx.compose.ui.window.rememberDialogState
26+
import androidx.compose.ui.window.DialogWindowScope
2627
import com.kdroid.composetray.lib.linux.LinuxOutsideClickWatcher
2728
import com.kdroid.composetray.lib.mac.MacOSWindowManager
2829
import com.kdroid.composetray.lib.mac.MacOutsideClickWatcher
@@ -49,23 +50,6 @@ import java.awt.event.WindowFocusListener
4950
* Creates a tray-based desktop application with support for customizable icons, tooltips, menus,
5051
* and composable content. The application integrates with the system's tray or menu bar and
5152
* supports optional window functionality.
52-
*
53-
* @param icon The primary icon to display in the system tray or menu bar.
54-
* @param tint Optional color tint for the icon. Defaults to null, using the appropriate dark or light mode color.
55-
* @param iconRenderProperties Properties for rendering the icon, with defaults based on the operating system.
56-
* @param tooltip Text to be displayed as a tooltip when hovering over the tray icon.
57-
* @param state Optional state for managing the tray application's properties or lifecycle.
58-
* @param windowSize Optional initial size of the window, if a window is displayed.
59-
* @param visibleOnStart Whether the application's window should be visible on startup. Defaults to false.
60-
* @param fadeDurationMs Duration in milliseconds for fade animations. Defaults to 200ms.
61-
* @param animationSpec Animation specification for fading effects. Defaults to a smooth easing with the specified duration.
62-
* @param transparent Whether the application's window should be transparent. Defaults to true.
63-
* @param windowsTitle Title of the window for Windows operating systems. Defaults to an empty string.
64-
* @param windowIcon Optional icon to be used for the application's window.
65-
* @param onPreviewKeyEvent Handler for previewing key events before they are processed. Returns `true` if the event is consumed.
66-
* @param onKeyEvent Handler for processing key events. Returns `true` if the event is consumed.
67-
* @param menu Optional builder block for defining the application's tray menu. Defaults to null if no menu is needed.
68-
* @param content Composable content to be displayed within the application's window.
6953
*/
7054
@ExperimentalTrayAppApi
7155
@Composable
@@ -87,7 +71,7 @@ fun ApplicationScope.TrayApp(
8771
onPreviewKeyEvent: (KeyEvent) -> Boolean = { false },
8872
onKeyEvent: (KeyEvent) -> Boolean = { false },
8973
menu: (TrayMenuBuilder.() -> Unit)? = null,
90-
content: @Composable () -> Unit,
74+
content: @Composable DialogWindowScope.() -> Unit,
9175
) {
9276
val iconContent: @Composable () -> Unit = {
9377
val isDark = isMenuBarInDarkMode()
@@ -120,25 +104,6 @@ fun ApplicationScope.TrayApp(
120104
)
121105
}
122106

123-
/**
124-
* Creates a system tray application with the provided configuration.
125-
*
126-
* @param icon The icon displayed in the system tray for this application.
127-
* @param iconRenderProperties The properties defining how the icon is rendered. Default is based on the current operating system.
128-
* @param tooltip The tooltip text displayed when hovering over the tray icon.
129-
* @param state The state of the tray app, which can be used to manage its visibility and behavior. Defaults to `null`.
130-
* @param windowSize The size of the application window when displayed. Defaults to `null`.
131-
* @param visibleOnStart Determines whether the application window is visible when the app starts. Defaults to `false`.
132-
* @param fadeDurationMs The duration of the fade animation (in milliseconds) when showing or hiding the window. Defaults to `200`.
133-
* @param animationSpec The animation specification used for window fade transitions. Defaults to an easing `tween` animation.
134-
* @param transparent Indicates if the application window background should be transparent. Defaults to `true`.
135-
* @param windowsTitle The title of the window displayed in the task manager or window list on Windows systems. Defaults to an empty string.
136-
* @param windowIcon The icon displayed for the application window (if any). Can be `null`.
137-
* @param onPreviewKeyEvent A callback invoked before key events are dispatched. It can intercept and handle key events. Defaults to returning `false`.
138-
* @param onKeyEvent A callback invoked to handle key events. Defaults to returning `false`.
139-
* @param menu An optional builder block to define the tray menu items.
140-
* @param content The content displayed in the tray application's main window.
141-
*/
142107
@ExperimentalTrayAppApi
143108
@Composable
144109
fun ApplicationScope.TrayApp(
@@ -158,7 +123,7 @@ fun ApplicationScope.TrayApp(
158123
onPreviewKeyEvent: (KeyEvent) -> Boolean = { false },
159124
onKeyEvent: (KeyEvent) -> Boolean = { false },
160125
menu: (TrayMenuBuilder.() -> Unit)? = null,
161-
content: @Composable () -> Unit,
126+
content: @Composable DialogWindowScope.() -> Unit,
162127
) {
163128
val iconContent: @Composable () -> Unit = {
164129
Image(painter = icon, contentDescription = null, modifier = Modifier.fillMaxSize())
@@ -184,28 +149,7 @@ fun ApplicationScope.TrayApp(
184149
)
185150
}
186151

187-
/**
188-
* Composable function for displaying a system tray application with a GUI window.
189-
* This function differs behavior based on the user's operating system.
190-
*
191-
* @param windowsIcon Icon displayed in the system tray on Windows systems.
192-
* @param macLinuxIcon Icon displayed in the system tray on macOS and Linux systems.
193-
* @param tint Optional tint applied to the macOS and Linux tray icon.
194-
* @param iconRenderProperties Properties determining how the icon should be rendered.
195-
* @param tooltip Text displayed as a tooltip when hovering over the tray icon.
196-
* @param state Optional state for managing the tray application window (visibility, etc.).
197-
* @param windowSize Desired size of the window when opened, if applicable.
198-
* @param visibleOnStart Whether the window should be visible immediately after starting the app.
199-
* @param fadeDurationMs Duration of the fade-in and fade-out animations for showing/hiding the window, in milliseconds.
200-
* @param animationSpec Animation specification for fade effects.
201-
* @param transparent Whether the window's background should be transparent.
202-
* @param windowsTitle Title of the GUI window on Windows.
203-
* @param windowIcon Icon displayed in the top-left corner of the window on Windows.
204-
* @param onPreviewKeyEvent Lambda for handling preview key events invoked before onKeyEvent. Defaults to ignoring key events.
205-
* @param onKeyEvent Lambda for handling key events when the window has focus. Defaults to ignoring key events.
206-
* @param menu Optional lambda for building the context menu attached to the tray icon.
207-
* @param content Composable content displayed within the GUI window.
208-
*/
152+
/** Platform-overloaded API (Painter for Windows, ImageVector for macOS/Linux) */
209153
@ExperimentalTrayAppApi
210154
@Composable
211155
fun ApplicationScope.TrayApp(
@@ -227,7 +171,7 @@ fun ApplicationScope.TrayApp(
227171
onPreviewKeyEvent: (KeyEvent) -> Boolean = { false },
228172
onKeyEvent: (KeyEvent) -> Boolean = { false },
229173
menu: (TrayMenuBuilder.() -> Unit)? = null,
230-
content: @Composable () -> Unit,
174+
content: @Composable DialogWindowScope.() -> Unit,
231175
) {
232176
if (getOperatingSystem() == WINDOWS) {
233177
TrayApp(
@@ -273,7 +217,6 @@ fun ApplicationScope.TrayApp(
273217
}
274218
}
275219

276-
277220
@ExperimentalTrayAppApi
278221
@Composable
279222
fun ApplicationScope.TrayApp(
@@ -293,7 +236,7 @@ fun ApplicationScope.TrayApp(
293236
onPreviewKeyEvent: (KeyEvent) -> Boolean = { false },
294237
onKeyEvent: (KeyEvent) -> Boolean = { false },
295238
menu: (TrayMenuBuilder.() -> Unit)? = null,
296-
content: @Composable () -> Unit,
239+
content: @Composable DialogWindowScope.() -> Unit,
297240
) {
298241
TrayApp(
299242
icon = painterResource(icon),
@@ -314,7 +257,6 @@ fun ApplicationScope.TrayApp(
314257
)
315258
}
316259

317-
318260
@ExperimentalTrayAppApi
319261
@Composable
320262
fun ApplicationScope.TrayApp(
@@ -336,28 +278,28 @@ fun ApplicationScope.TrayApp(
336278
onPreviewKeyEvent: (KeyEvent) -> Boolean = { false },
337279
onKeyEvent: (KeyEvent) -> Boolean = { false },
338280
menu: (TrayMenuBuilder.() -> Unit)? = null,
339-
content: @Composable () -> Unit,
281+
content: @Composable DialogWindowScope.() -> Unit,
340282
) {
341283
if (getOperatingSystem() == WINDOWS) {
342284
TrayApp(
343-
icon = painterResource(windowsIcon),
344-
iconRenderProperties = iconRenderProperties,
345-
tooltip = tooltip,
346-
state = state,
347-
windowSize = windowSize,
348-
visibleOnStart = visibleOnStart,
349-
fadeDurationMs = fadeDurationMs,
350-
animationSpec = animationSpec,
351-
transparent = transparent,
352-
windowsTitle = windowsTitle,
353-
windowIcon = windowIcon,
354-
undecorated = undecorated,
355-
resizable = resizable,
356-
onPreviewKeyEvent = onPreviewKeyEvent,
357-
onKeyEvent = onKeyEvent,
358-
menu = menu,
359-
content = content,
360-
)
285+
icon = painterResource(windowsIcon),
286+
iconRenderProperties = iconRenderProperties,
287+
tooltip = tooltip,
288+
state = state,
289+
windowSize = windowSize,
290+
visibleOnStart = visibleOnStart,
291+
fadeDurationMs = fadeDurationMs,
292+
animationSpec = animationSpec,
293+
transparent = transparent,
294+
windowsTitle = windowsTitle,
295+
windowIcon = windowIcon,
296+
undecorated = undecorated,
297+
resizable = resizable,
298+
onPreviewKeyEvent = onPreviewKeyEvent,
299+
onKeyEvent = onKeyEvent,
300+
menu = menu,
301+
content = content,
302+
)
361303
} else {
362304
TrayApp(
363305
icon = macLinuxIcon,
@@ -403,7 +345,7 @@ fun ApplicationScope.TrayApp(
403345
onPreviewKeyEvent: (KeyEvent) -> Boolean = { false },
404346
onKeyEvent: (KeyEvent) -> Boolean = { false },
405347
menu: (TrayMenuBuilder.() -> Unit)? = null,
406-
content: @Composable () -> Unit,
348+
content: @Composable DialogWindowScope.() -> Unit,
407349
) {
408350
when (getOperatingSystem()) {
409351
OperatingSystem.LINUX -> TrayAppImplLinux(
@@ -437,7 +379,7 @@ private fun ApplicationScope.TrayAppImplOriginal(
437379
onPreviewKeyEvent: (KeyEvent) -> Boolean,
438380
onKeyEvent: (KeyEvent) -> Boolean,
439381
menu: (TrayMenuBuilder.() -> Unit)?,
440-
content: @Composable () -> Unit,
382+
content: @Composable DialogWindowScope.() -> Unit,
441383
) {
442384
// State holder
443385
val trayAppState = state ?: rememberTrayAppState(
@@ -634,9 +576,7 @@ private fun ApplicationScope.TrayAppImplOriginal(
634576
}
635577
}
636578

637-
Box(
638-
modifier = Modifier.fillMaxSize().alpha(alpha)
639-
) { content() }
579+
Box(modifier = Modifier.fillMaxSize().alpha(alpha)) { content() }
640580
}
641581
}
642582

@@ -660,7 +600,7 @@ private fun ApplicationScope.TrayAppImplLinux(
660600
onPreviewKeyEvent: (KeyEvent) -> Boolean,
661601
onKeyEvent: (KeyEvent) -> Boolean,
662602
menu: (TrayMenuBuilder.() -> Unit)?,
663-
content: @Composable () -> Unit,
603+
content: @Composable DialogWindowScope.() -> Unit,
664604
) {
665605
// State holder (window always mounted; visibility toggled)
666606
val trayAppState = state ?: rememberTrayAppState(
@@ -715,7 +655,7 @@ private fun ApplicationScope.TrayAppImplLinux(
715655
} else {
716656
val wait = (minVisibleDurationMs - sinceShow).coerceAtLeast(0)
717657
CoroutineScope(Dispatchers.IO).launch {
718-
delay(wait.toLong())
658+
delay(wait)
719659
trayAppState.hide(); lastHiddenAt = System.currentTimeMillis()
720660
}
721661
}
@@ -810,8 +750,6 @@ private fun ApplicationScope.TrayAppImplLinux(
810750
}
811751
}
812752

813-
Box(
814-
modifier = Modifier.fillMaxSize().alpha(alpha)
815-
) { content() }
753+
Box(modifier = Modifier.fillMaxSize().alpha(alpha)) { content() }
816754
}
817755
}

0 commit comments

Comments
 (0)