-
-
Notifications
You must be signed in to change notification settings - Fork 105
Add PDE window utilities for Compose and Swing #1284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4546a38
Refactor Locale class and add LocaleProvider test
Stefterv 643ec03
Make setLocale parameter nullable in Locale class
Stefterv 06e3094
Add compose ui test to the deps
Stefterv d42fb2f
Update locale change method in test
Stefterv 58c746b
Add PDE window utilities for Compose and Swing
Stefterv db69773
Refactor beta welcome window handling
Stefterv 4686345
Merge branch 'welcome-screen' into compose-windows
Stefterv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| package processing.app.ui.theme | ||
|
|
||
| import androidx.compose.foundation.layout.* | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.CompositionLocalProvider | ||
| import androidx.compose.runtime.LaunchedEffect | ||
| import androidx.compose.runtime.compositionLocalOf | ||
| import androidx.compose.runtime.remember | ||
| import androidx.compose.ui.Alignment | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.awt.ComposePanel | ||
| import androidx.compose.ui.unit.DpSize | ||
| import androidx.compose.ui.unit.dp | ||
| import androidx.compose.ui.window.Window | ||
| import androidx.compose.ui.window.WindowPosition | ||
| import androidx.compose.ui.window.rememberWindowState | ||
| import com.formdev.flatlaf.util.SystemInfo | ||
|
|
||
| import java.awt.event.KeyAdapter | ||
| import java.awt.event.KeyEvent | ||
| import javax.swing.JFrame | ||
|
|
||
| val LocalWindow = compositionLocalOf<JFrame> { error("No Window Set") } | ||
|
|
||
| /** | ||
| * A utility class to create a new Window with Compose content in a Swing application. | ||
| * It sets up the window with some default properties and allows for custom content. | ||
| * Use this when creating a Compose based window from Swing. | ||
| * | ||
| * Usage example: | ||
| * ``` | ||
| * SwingUtilities.invokeLater { | ||
| * PDESwingWindow("menu.help.welcome", fullWindowContent = true) { | ||
| * | ||
| * } | ||
| * } | ||
| * ``` | ||
| * | ||
| * @param titleKey The key for the window title, which will be localized. | ||
| * @param fullWindowContent If true, the content will extend into the title bar area on macOS. | ||
| * @param content The composable content to be displayed in the window. | ||
| */ | ||
| class PDESwingWindow(titleKey: String = "", fullWindowContent: Boolean = false, onClose: () -> Unit = {}, content: @Composable BoxScope.() -> Unit): JFrame(){ | ||
| init{ | ||
| val window = this | ||
| defaultCloseOperation = DISPOSE_ON_CLOSE | ||
| ComposePanel().apply { | ||
| setContent { | ||
| PDEWindowContent(window, titleKey, fullWindowContent, content) | ||
| } | ||
| window.add(this) | ||
| } | ||
| background = java.awt.Color.white | ||
| setLocationRelativeTo(null) | ||
| addKeyListener(object : KeyAdapter() { | ||
| override fun keyPressed(e: KeyEvent) { | ||
| if (e.keyCode != KeyEvent.VK_ESCAPE) return | ||
|
|
||
| window.dispose() | ||
| onClose() | ||
| } | ||
| }) | ||
| isResizable = false | ||
| isVisible = true | ||
| requestFocus() | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Internal Composable function to set up the window content with theming and localization. | ||
| * It also handles macOS specific properties for full window content. | ||
| * | ||
| * @param window The JFrame instance to be configured. | ||
| * @param titleKey The key for the window title, which will be localized. | ||
| * @param fullWindowContent If true, the content will extend into the title bar area on macOS. | ||
| * @param content The composable content to be displayed in the window. | ||
| */ | ||
| @Composable | ||
| private fun PDEWindowContent(window: JFrame, titleKey: String, fullWindowContent: Boolean = false, content: @Composable BoxScope.() -> Unit){ | ||
| val mac = SystemInfo.isMacOS && SystemInfo.isMacFullWindowContentSupported | ||
| remember { | ||
| window.rootPane.putClientProperty("apple.awt.fullWindowContent", mac && fullWindowContent) | ||
| window.rootPane.putClientProperty("apple.awt.transparentTitleBar", mac && fullWindowContent) | ||
| } | ||
|
|
||
| CompositionLocalProvider(LocalWindow provides window) { | ||
| ProcessingTheme { | ||
| val locale = LocalLocale.current | ||
| window.title = locale[titleKey] | ||
| LaunchedEffect(locale) { | ||
| window.pack() | ||
| window.setLocationRelativeTo(null) | ||
| } | ||
|
|
||
| Box(modifier = Modifier.padding(top = if (mac && !fullWindowContent) 22.dp else 0.dp),content = content) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A Composable function to create and display a new window with the specified content. | ||
| * This function sets up the window state and handles the close request. | ||
| * Use this when creating a Compose based window from another Compose context. | ||
| * | ||
| * Usage example: | ||
| * ``` | ||
| * PDEComposeWindow("window.title", fullWindowContent = true, onClose = { /* handle close */ }) { | ||
| * // Your window content here | ||
| * Text("Hello, World!") | ||
| * } | ||
| * ``` | ||
| * | ||
| * This will create a new window with the title localized from "window.title" key, | ||
| * with content extending into the title bar area on macOS, and a custom close handler. | ||
| * | ||
| * Fully standalone example: | ||
| * ``` | ||
| * application { | ||
| * PDEComposeWindow("window.title", fullWindowContent = true, onClose = ::exitApplication) { | ||
| * // Your window content here | ||
| * } | ||
| * } | ||
| * ``` | ||
| * | ||
| * @param titleKey The key for the window title, which will be localized. | ||
| * @param fullWindowContent If true, the content will extend into the title bar area on | ||
| * macOS. | ||
| * @param onClose A lambda function to be called when the window is requested to close. | ||
| * @param content The composable content to be displayed in the window. | ||
| * | ||
| * | ||
| * | ||
| */ | ||
| @Composable | ||
| fun PDEComposeWindow(titleKey: String, fullWindowContent: Boolean = false, onClose: () -> Unit = {}, content: @Composable BoxScope.() -> Unit){ | ||
| val windowState = rememberWindowState( | ||
| size = DpSize.Unspecified, | ||
| position = WindowPosition(Alignment.Center) | ||
| ) | ||
| Window(onCloseRequest = onClose, state = windowState, title = "") { | ||
| PDEWindowContent(window, titleKey, fullWindowContent, content) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<3