diff --git a/compose-rust/android-kotlin/shared/ComponentRules.kt b/compose-rust/android-kotlin/shared/ComponentRules.kt index 1b07f8360..2c072c5dc 100644 --- a/compose-rust/android-kotlin/shared/ComponentRules.kt +++ b/compose-rust/android-kotlin/shared/ComponentRules.kt @@ -1649,6 +1649,10 @@ internal object FluentRules : ComponentRules { closeHover = WINDOWS_CLOSE_RED, glyph = theme.color(ColorRole.OnSurface), closeHoverGlyph = Color.White, + // Windows 11 darkens a held button a step past its hover, and draws a held close + // in the same red at 90%. Taken from the surface colour so dark and light both work. + pressed = theme.color(ColorRole.OnSurface).copy(alpha = PRESS_SHADE), + closePressed = WINDOWS_CLOSE_RED.copy(alpha = 0.9f), glyphStroke = 1.dp, glyphAtRest = true, spacing = 0.dp, diff --git a/compose-rust/android-kotlin/shared/DesignSystem.kt b/compose-rust/android-kotlin/shared/DesignSystem.kt index ec81b02f1..e27df1833 100644 --- a/compose-rust/android-kotlin/shared/DesignSystem.kt +++ b/compose-rust/android-kotlin/shared/DesignSystem.kt @@ -847,6 +847,13 @@ data class CaptionStyle( * modes existed. */ val height: Dp = 0.dp, + /** + * The fill under a button while it is held. Transparent means the system has no + * pressed fill of its own and the hover fill stays. + */ + val pressed: Color = Color.Transparent, + /** The fill under close while it is held, where the system reddens close on hover. */ + val closePressed: Color = Color.Transparent, ) /** diff --git a/compose-rust/android-kotlin/shared/WindowButtons.kt b/compose-rust/android-kotlin/shared/WindowButtons.kt index aa77946c3..583a0fdfe 100644 --- a/compose-rust/android-kotlin/shared/WindowButtons.kt +++ b/compose-rust/android-kotlin/shared/WindowButtons.kt @@ -4,6 +4,7 @@ import androidx.compose.foundation.background import androidx.compose.foundation.clickable import androidx.compose.foundation.interaction.MutableInteractionSource import androidx.compose.foundation.interaction.collectIsHoveredAsState +import androidx.compose.foundation.interaction.collectIsPressedAsState import androidx.compose.foundation.hoverable import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Row @@ -13,6 +14,8 @@ import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.width import androidx.compose.runtime.Composable import androidx.compose.runtime.Immutable +import androidx.compose.runtime.State +import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.getValue import androidx.compose.runtime.remember import androidx.compose.runtime.staticCompositionLocalOf @@ -43,8 +46,44 @@ data class WindowActions( val minimise: () -> Unit, val maximise: () -> Unit, val close: () -> Unit, + /** + * Whether the window is maximised now, so maximise draws as restore. The window owner + * updates it when the window's size changes; a window that never says stays at false. + */ + val maximised: State = mutableStateOf(false), ) +/** Which mark a window button shows: maximise turns into restore while the window is maximised. */ +internal enum class WindowGlyph { Minimise, Maximise, Restore, Close } + +/** The mark [button] shows, given whether the window is [maximised]. */ +internal fun windowGlyph(button: WindowButton, maximised: Boolean): WindowGlyph = when (button) { + WindowButton.Minimise -> WindowGlyph.Minimise + WindowButton.Maximise -> if (maximised) WindowGlyph.Restore else WindowGlyph.Maximise + WindowButton.Close -> WindowGlyph.Close +} + +/** + * The fill under a window button. + * + * Pressed beats hover, and a transparent fill means the system does not change it in that + * state, so the one before it stays. + */ +internal fun windowButtonFill(button: WindowButton, style: CaptionStyle, hovered: Boolean, pressed: Boolean): Color { + val rest = when (button) { + WindowButton.Minimise -> style.minimiseContainer + WindowButton.Maximise -> style.maximiseContainer + WindowButton.Close -> style.closeContainer + } + val hoverFill = if (button == WindowButton.Close) style.closeHover else style.hover + val pressedFill = if (button == WindowButton.Close) style.closePressed else style.pressed + return when { + pressed && pressedFill.alpha > 0f -> pressedFill + (hovered || pressed) && hoverFill.alpha > 0f -> hoverFill + else -> rest + } +} + /** The window this content is in, for the caption to operate. */ val LocalWindowActions = staticCompositionLocalOf { null } @@ -90,6 +129,7 @@ internal fun WindowButtons(style: CaptionStyle, modifier: Modifier = Modifier) { // A set of coloured discs shows its marks when the pointer is anywhere // over the set, not only over the one disc under it. showGlyph = style.glyphAtRest || groupHovered, + glyph = windowGlyph(button, actions.maximised.value), onClick = when (button) { WindowButton.Minimise -> actions.minimise WindowButton.Maximise -> actions.maximise @@ -105,20 +145,15 @@ private fun WindowButtonCell( button: WindowButton, style: CaptionStyle, showGlyph: Boolean, + glyph: WindowGlyph, onClick: () -> Unit, ) { val interaction = remember { MutableInteractionSource() } val hovered by interaction.collectIsHoveredAsState() - val rest = when (button) { - WindowButton.Minimise -> style.minimiseContainer - WindowButton.Maximise -> style.maximiseContainer - WindowButton.Close -> style.closeContainer - } - val hoverFill = if (button == WindowButton.Close) style.closeHover else style.hover - // A transparent hover colour means this system does not change the fill under the - // pointer, so the resting fill stays rather than being replaced by nothing. - val container = if (hovered && hoverFill.alpha > 0f) hoverFill else rest - val glyph = if (hovered && button == WindowButton.Close && style.closeHover.alpha > 0f) { + val pressed by interaction.collectIsPressedAsState() + val container = windowButtonFill(button, style, hovered, pressed) + val mark = glyph + val glyphColour = if ((hovered || pressed) && button == WindowButton.Close && style.closeHover.alpha > 0f) { style.closeHoverGlyph } else { style.glyph @@ -126,7 +161,7 @@ private fun WindowButtonCell( Row( modifier = Modifier .testTag(windowButtonTestTag(button)) - .semantics { contentDescription = button.label } + .semantics { contentDescription = mark.label } .size(width = style.buttonWidth, height = style.buttonHeight) .clip(style.shape) .background(container, style.shape) @@ -136,17 +171,18 @@ private fun WindowButtonCell( verticalAlignment = Alignment.CenterVertically, ) { if (showGlyph) { - WindowButtonGlyph(button, glyph, style) + WindowButtonGlyph(mark, glyphColour, style) } } } /** The accessible name of a window button, which the platform supplies on macOS. */ -private val WindowButton.label: String +private val WindowGlyph.label: String get() = when (this) { - WindowButton.Minimise -> "Minimise" - WindowButton.Maximise -> "Maximise" - WindowButton.Close -> "Close" + WindowGlyph.Minimise -> "Minimise" + WindowGlyph.Maximise -> "Maximise" + WindowGlyph.Restore -> "Restore" + WindowGlyph.Close -> "Close" } /** @@ -157,7 +193,7 @@ private val WindowButton.label: String * cannot afford a missing glyph: the button that closes it. */ @Composable -private fun WindowButtonGlyph(button: WindowButton, colour: Color, style: CaptionStyle) { +private fun WindowButtonGlyph(button: WindowGlyph, colour: Color, style: CaptionStyle) { // Small against the button, the way every one of these systems draws it: the button is // a target and the mark inside it is a hint. val extent = if (style.glyphAtRest) 10.dp else 7.dp @@ -167,7 +203,7 @@ private fun WindowButtonGlyph(button: WindowButton, colour: Color, style: Captio .drawBehind { val stroke = Stroke(width = style.glyphStroke.toPx(), cap = StrokeCap.Round) when (button) { - WindowButton.Minimise -> drawLine( + WindowGlyph.Minimise -> drawLine( color = colour, start = Offset(0f, size.height / 2f), end = Offset(size.width, size.height / 2f), @@ -175,12 +211,24 @@ private fun WindowButtonGlyph(button: WindowButton, colour: Color, style: Captio cap = StrokeCap.Round, ) - WindowButton.Maximise -> drawRect( + WindowGlyph.Maximise -> drawRect( color = colour, style = stroke, ) - WindowButton.Close -> { + // Two boxes, the front one low and to the left, the back one showing + // only its top and right edges: Windows' restore mark. + WindowGlyph.Restore -> { + val step = size.width * 0.2f + val front = androidx.compose.ui.geometry.Size(size.width - step, size.height - step) + drawRect(color = colour, topLeft = Offset(0f, step), size = front, style = stroke) + drawLine(colour, Offset(step, step), Offset(step, 0f), stroke.width) + drawLine(colour, Offset(step, 0f), Offset(size.width, 0f), stroke.width) + drawLine(colour, Offset(size.width, 0f), Offset(size.width, size.height - step), stroke.width) + drawLine(colour, Offset(size.width, size.height - step), Offset(size.width - step, size.height - step), stroke.width) + } + + WindowGlyph.Close -> { drawLine( color = colour, start = Offset.Zero, diff --git a/renderer/desktop/c/renderer_entry.c b/renderer/desktop/c/renderer_entry.c index a6a910aed..39c858187 100644 --- a/renderer/desktop/c/renderer_entry.c +++ b/renderer/desktop/c/renderer_entry.c @@ -703,3 +703,19 @@ void dxc_appkit_footprint_breakdown(int32_t elapsed_s, int32_t is_end) { (void)is_end; } #endif + +/* + * Whether the GraalVM image's Win32 window is maximised, for the caption's maximise or + * restore glyph. The image's Kotlin names this on every platform, so every platform has to + * define it; only Windows has a window that can answer. + */ +#ifdef _WIN32 +int32_t dxc_win32_window_zoomed(void *window) { + return IsZoomed((HWND)window) ? 1 : 0; +} +#else +int32_t dxc_win32_window_zoomed(void *window) { + (void)window; + return 0; +} +#endif diff --git a/renderer/desktop/src/Win32Window.kt b/renderer/desktop/src/Win32Window.kt index 4689cc5dd..95402c930 100644 --- a/renderer/desktop/src/Win32Window.kt +++ b/renderer/desktop/src/Win32Window.kt @@ -48,6 +48,24 @@ private external fun openWindow( out: Pointer?, ): Int +/** + * Says how the next window is made. Without it the fork's window takes the caption strip + * only when told the renderer draws the buttons (compose-rust#339): this path used to skip + * the call, and its windows lost the caption with nothing drawn in its place. + */ +@CFunction("dxc_native_window_configure") +private external fun configureWindow( + resizable: Int, + minWidth: Int, + minHeight: Int, + systemChrome: Int, + backdrop: Int, +) + +/** user32's IsZoomed on the window the C side opened (renderer_entry.c; 0 off Windows). */ +@CFunction("dxc_win32_window_zoomed") +private external fun isZoomed(window: Pointer?): Int + @CFunction("dxc_native_window_size") private external fun windowSize( window: Pointer?, @@ -183,6 +201,12 @@ fun openWin32Window(title: String, width: Int, height: Int): Win32NativeWindow? private const val WINDOW_STRUCT_BYTES = 40 +/** + * Windows 11's caption height, the number win32_caption.h hit-tests. Not the devshell's + * windowsCaptionHeight: that file names AWT, which this image does not carry. + */ +private val WIN32_CAPTION_HEIGHT = androidx.compose.ui.unit.Dp(32f) + /** * Hands the platform what the window would tell a reader who cannot see it. * @@ -364,6 +388,16 @@ internal fun runWin32Window(autoExitMillis: Long? = null) { val host = dev.darkpyonix.composerust.runtime.ComposeRustHost(NativeHostConnection()) host.start() val asked = host.table.window + // Who draws the caption, as the Kotlin/Native renderer decides it: with Chrome.System the + // system's, otherwise the caption strip is the content's and the buttons are drawn below. + val systemChrome = asked?.chrome == dev.darkpyonix.composerust.protocol.Chrome.System + configureWindow( + if (asked?.resizable == false) 0 else 1, + asked?.minWidth?.takeIf { it > 0 } ?: 0, + asked?.minHeight?.takeIf { it > 0 } ?: 0, + if (systemChrome) 1 else 0, + 0, + ) val window = openWin32Window( asked?.title?.takeIf { it.isNotEmpty() } ?: "compose-rust", if (asked != null && asked.width > 0) asked.width else 520, @@ -422,13 +456,24 @@ internal fun runWin32Window(autoExitMillis: Long? = null) { // The window's own clipboard (dxc_native_clipboard_read and _write), as the AppKit and X11 // windows have through NativeWindowContent: Compose's desktop default is the toolkit's, and // this image carries no toolkit. + // The caption's three buttons, which the window gave up with the caption strip: drawn + // by ComposeRustContent when it is handed a caption and the actions, as on every other + // platform where the renderer draws the caption. + val maximised = androidx.compose.runtime.mutableStateOf(false) + val actions = if (systemChrome) null else nativeWindowActions().copy(maximised = maximised) + val caption = if (systemChrome) { + dev.darkpyonix.composerust.runtime.WindowCaption.None + } else { + dev.darkpyonix.composerust.runtime.WindowCaption(height = WIN32_CAPTION_HEIGHT) + } @Suppress("DEPRECATION") scene.setContent { androidx.compose.runtime.CompositionLocalProvider( androidx.compose.ui.platform.LocalClipboard provides WindowClipboard, androidx.compose.ui.platform.LocalClipboardManager provides WindowClipboardManagerInstance, + dev.darkpyonix.composerust.runtime.LocalWindowActions provides actions, ) { - dev.darkpyonix.composerust.runtime.ComposeRustContent(host) + dev.darkpyonix.composerust.runtime.ComposeRustContent(host, caption = caption) } } WindowStartReport.sceneCreated(size.width, size.height, measured.scale) @@ -439,6 +484,12 @@ internal fun runWin32Window(autoExitMillis: Long? = null) { var painted = false var drew = false Win32Frames.paint = { + // Read every frame and written only on a change, so an idle window draws nothing + // extra. Maximising resizes the window, and a resize always draws a frame. + if (actions != null) { + val zoomed = isZoomed(WordFactory.pointer(window.window)) != 0 + if (maximised.value != zoomed) maximised.value = zoomed + } // The scene's own work first. A list that asked for rows on the last frame wants // them in hand before this one is measured, and during a drag of the window's // edge this is the only place that runs at all. Compose's main-thread work first, diff --git a/renderer/desktop/src/renderer/ComponentRules.kt b/renderer/desktop/src/renderer/ComponentRules.kt index 1b07f8360..2c072c5dc 100644 --- a/renderer/desktop/src/renderer/ComponentRules.kt +++ b/renderer/desktop/src/renderer/ComponentRules.kt @@ -1649,6 +1649,10 @@ internal object FluentRules : ComponentRules { closeHover = WINDOWS_CLOSE_RED, glyph = theme.color(ColorRole.OnSurface), closeHoverGlyph = Color.White, + // Windows 11 darkens a held button a step past its hover, and draws a held close + // in the same red at 90%. Taken from the surface colour so dark and light both work. + pressed = theme.color(ColorRole.OnSurface).copy(alpha = PRESS_SHADE), + closePressed = WINDOWS_CLOSE_RED.copy(alpha = 0.9f), glyphStroke = 1.dp, glyphAtRest = true, spacing = 0.dp, diff --git a/renderer/desktop/src/renderer/DesignSystem.kt b/renderer/desktop/src/renderer/DesignSystem.kt index ec81b02f1..e27df1833 100644 --- a/renderer/desktop/src/renderer/DesignSystem.kt +++ b/renderer/desktop/src/renderer/DesignSystem.kt @@ -847,6 +847,13 @@ data class CaptionStyle( * modes existed. */ val height: Dp = 0.dp, + /** + * The fill under a button while it is held. Transparent means the system has no + * pressed fill of its own and the hover fill stays. + */ + val pressed: Color = Color.Transparent, + /** The fill under close while it is held, where the system reddens close on hover. */ + val closePressed: Color = Color.Transparent, ) /** diff --git a/renderer/desktop/src/renderer/WindowButtons.kt b/renderer/desktop/src/renderer/WindowButtons.kt index aa77946c3..583a0fdfe 100644 --- a/renderer/desktop/src/renderer/WindowButtons.kt +++ b/renderer/desktop/src/renderer/WindowButtons.kt @@ -4,6 +4,7 @@ import androidx.compose.foundation.background import androidx.compose.foundation.clickable import androidx.compose.foundation.interaction.MutableInteractionSource import androidx.compose.foundation.interaction.collectIsHoveredAsState +import androidx.compose.foundation.interaction.collectIsPressedAsState import androidx.compose.foundation.hoverable import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Row @@ -13,6 +14,8 @@ import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.width import androidx.compose.runtime.Composable import androidx.compose.runtime.Immutable +import androidx.compose.runtime.State +import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.getValue import androidx.compose.runtime.remember import androidx.compose.runtime.staticCompositionLocalOf @@ -43,8 +46,44 @@ data class WindowActions( val minimise: () -> Unit, val maximise: () -> Unit, val close: () -> Unit, + /** + * Whether the window is maximised now, so maximise draws as restore. The window owner + * updates it when the window's size changes; a window that never says stays at false. + */ + val maximised: State = mutableStateOf(false), ) +/** Which mark a window button shows: maximise turns into restore while the window is maximised. */ +internal enum class WindowGlyph { Minimise, Maximise, Restore, Close } + +/** The mark [button] shows, given whether the window is [maximised]. */ +internal fun windowGlyph(button: WindowButton, maximised: Boolean): WindowGlyph = when (button) { + WindowButton.Minimise -> WindowGlyph.Minimise + WindowButton.Maximise -> if (maximised) WindowGlyph.Restore else WindowGlyph.Maximise + WindowButton.Close -> WindowGlyph.Close +} + +/** + * The fill under a window button. + * + * Pressed beats hover, and a transparent fill means the system does not change it in that + * state, so the one before it stays. + */ +internal fun windowButtonFill(button: WindowButton, style: CaptionStyle, hovered: Boolean, pressed: Boolean): Color { + val rest = when (button) { + WindowButton.Minimise -> style.minimiseContainer + WindowButton.Maximise -> style.maximiseContainer + WindowButton.Close -> style.closeContainer + } + val hoverFill = if (button == WindowButton.Close) style.closeHover else style.hover + val pressedFill = if (button == WindowButton.Close) style.closePressed else style.pressed + return when { + pressed && pressedFill.alpha > 0f -> pressedFill + (hovered || pressed) && hoverFill.alpha > 0f -> hoverFill + else -> rest + } +} + /** The window this content is in, for the caption to operate. */ val LocalWindowActions = staticCompositionLocalOf { null } @@ -90,6 +129,7 @@ internal fun WindowButtons(style: CaptionStyle, modifier: Modifier = Modifier) { // A set of coloured discs shows its marks when the pointer is anywhere // over the set, not only over the one disc under it. showGlyph = style.glyphAtRest || groupHovered, + glyph = windowGlyph(button, actions.maximised.value), onClick = when (button) { WindowButton.Minimise -> actions.minimise WindowButton.Maximise -> actions.maximise @@ -105,20 +145,15 @@ private fun WindowButtonCell( button: WindowButton, style: CaptionStyle, showGlyph: Boolean, + glyph: WindowGlyph, onClick: () -> Unit, ) { val interaction = remember { MutableInteractionSource() } val hovered by interaction.collectIsHoveredAsState() - val rest = when (button) { - WindowButton.Minimise -> style.minimiseContainer - WindowButton.Maximise -> style.maximiseContainer - WindowButton.Close -> style.closeContainer - } - val hoverFill = if (button == WindowButton.Close) style.closeHover else style.hover - // A transparent hover colour means this system does not change the fill under the - // pointer, so the resting fill stays rather than being replaced by nothing. - val container = if (hovered && hoverFill.alpha > 0f) hoverFill else rest - val glyph = if (hovered && button == WindowButton.Close && style.closeHover.alpha > 0f) { + val pressed by interaction.collectIsPressedAsState() + val container = windowButtonFill(button, style, hovered, pressed) + val mark = glyph + val glyphColour = if ((hovered || pressed) && button == WindowButton.Close && style.closeHover.alpha > 0f) { style.closeHoverGlyph } else { style.glyph @@ -126,7 +161,7 @@ private fun WindowButtonCell( Row( modifier = Modifier .testTag(windowButtonTestTag(button)) - .semantics { contentDescription = button.label } + .semantics { contentDescription = mark.label } .size(width = style.buttonWidth, height = style.buttonHeight) .clip(style.shape) .background(container, style.shape) @@ -136,17 +171,18 @@ private fun WindowButtonCell( verticalAlignment = Alignment.CenterVertically, ) { if (showGlyph) { - WindowButtonGlyph(button, glyph, style) + WindowButtonGlyph(mark, glyphColour, style) } } } /** The accessible name of a window button, which the platform supplies on macOS. */ -private val WindowButton.label: String +private val WindowGlyph.label: String get() = when (this) { - WindowButton.Minimise -> "Minimise" - WindowButton.Maximise -> "Maximise" - WindowButton.Close -> "Close" + WindowGlyph.Minimise -> "Minimise" + WindowGlyph.Maximise -> "Maximise" + WindowGlyph.Restore -> "Restore" + WindowGlyph.Close -> "Close" } /** @@ -157,7 +193,7 @@ private val WindowButton.label: String * cannot afford a missing glyph: the button that closes it. */ @Composable -private fun WindowButtonGlyph(button: WindowButton, colour: Color, style: CaptionStyle) { +private fun WindowButtonGlyph(button: WindowGlyph, colour: Color, style: CaptionStyle) { // Small against the button, the way every one of these systems draws it: the button is // a target and the mark inside it is a hint. val extent = if (style.glyphAtRest) 10.dp else 7.dp @@ -167,7 +203,7 @@ private fun WindowButtonGlyph(button: WindowButton, colour: Color, style: Captio .drawBehind { val stroke = Stroke(width = style.glyphStroke.toPx(), cap = StrokeCap.Round) when (button) { - WindowButton.Minimise -> drawLine( + WindowGlyph.Minimise -> drawLine( color = colour, start = Offset(0f, size.height / 2f), end = Offset(size.width, size.height / 2f), @@ -175,12 +211,24 @@ private fun WindowButtonGlyph(button: WindowButton, colour: Color, style: Captio cap = StrokeCap.Round, ) - WindowButton.Maximise -> drawRect( + WindowGlyph.Maximise -> drawRect( color = colour, style = stroke, ) - WindowButton.Close -> { + // Two boxes, the front one low and to the left, the back one showing + // only its top and right edges: Windows' restore mark. + WindowGlyph.Restore -> { + val step = size.width * 0.2f + val front = androidx.compose.ui.geometry.Size(size.width - step, size.height - step) + drawRect(color = colour, topLeft = Offset(0f, step), size = front, style = stroke) + drawLine(colour, Offset(step, step), Offset(step, 0f), stroke.width) + drawLine(colour, Offset(step, 0f), Offset(size.width, 0f), stroke.width) + drawLine(colour, Offset(size.width, 0f), Offset(size.width, size.height - step), stroke.width) + drawLine(colour, Offset(size.width, size.height - step), Offset(size.width - step, size.height - step), stroke.width) + } + + WindowGlyph.Close -> { drawLine( color = colour, start = Offset.Zero, diff --git a/renderer/desktop/test/WindowsCaptionButtonsTest.kt b/renderer/desktop/test/WindowsCaptionButtonsTest.kt new file mode 100644 index 000000000..aad371e35 --- /dev/null +++ b/renderer/desktop/test/WindowsCaptionButtonsTest.kt @@ -0,0 +1,100 @@ +package dev.darkpyonix.composerust.test + +import androidx.compose.runtime.CompositionLocalProvider +import androidx.compose.runtime.mutableStateOf +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.test.ExperimentalTestApi +import androidx.compose.ui.test.assertContentDescriptionEquals +import androidx.compose.ui.test.onNodeWithTag +import androidx.compose.ui.test.runComposeUiTest +import androidx.compose.ui.unit.dp +import dev.darkpyonix.composerust.design.HostPlatform +import dev.darkpyonix.composerust.design.resolveTheme +import dev.darkpyonix.composerust.protocol.ColorScheme +import dev.darkpyonix.composerust.protocol.DesignSystem +import dev.darkpyonix.composerust.protocol.Mutation +import dev.darkpyonix.composerust.protocol.Theme +import dev.darkpyonix.composerust.protocol.WidgetKind +import dev.darkpyonix.composerust.runtime.ComposeRustContent +import dev.darkpyonix.composerust.runtime.LocalWindowActions +import dev.darkpyonix.composerust.runtime.WindowActions +import dev.darkpyonix.composerust.runtime.WindowButton +import dev.darkpyonix.composerust.runtime.WindowCaption +import dev.darkpyonix.composerust.runtime.WindowGlyph +import dev.darkpyonix.composerust.runtime.rememberComposeRustHost +import dev.darkpyonix.composerust.runtime.windowButtonFill +import dev.darkpyonix.composerust.runtime.windowGlyph +import dev.darkpyonix.composerust.tooling.FakeHostConnection +import dev.darkpyonix.composerust.ui.platform.FrameRequestSource +import dev.darkpyonix.composerust.ui.platform.LocalFrameRequests +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNotEquals + +/** + * The Windows caption buttons the renderer draws once the window gives up its caption + * (compose-rust#339): Windows 11 sizes and colours, maximise turning into restore. + */ +@OptIn(ExperimentalTestApi::class) +class WindowsCaptionButtonsTest { + private fun fluent(scheme: ColorScheme) = resolveTheme( + theme = Theme(DesignSystem.Fluent, DesignSystem.Fluent, scheme, adaptive = false), + platform = HostPlatform.Unknown, + systemDark = scheme == ColorScheme.Dark, + ).let { it.rules.caption(it) } + + @Test + fun fr19_4_maximise_shows_restore_while_the_window_is_maximised() { + assertEquals(WindowGlyph.Maximise, windowGlyph(WindowButton.Maximise, maximised = false)) + assertEquals(WindowGlyph.Restore, windowGlyph(WindowButton.Maximise, maximised = true)) + assertEquals(WindowGlyph.Close, windowGlyph(WindowButton.Close, maximised = true)) + assertEquals(WindowGlyph.Minimise, windowGlyph(WindowButton.Minimise, maximised = true)) + } + + @Test + fun fr19_4_fluent_buttons_are_windows_11_sized_and_coloured_in_both_schemes() { + for (scheme in listOf(ColorScheme.Light, ColorScheme.Dark)) { + val style = fluent(scheme) + assertEquals(46.dp, style.buttonWidth) + assertEquals(32.dp, style.buttonHeight) + val red = Color(0xFFC42B1C) + assertEquals(red, windowButtonFill(WindowButton.Close, style, hovered = true, pressed = false)) + assertEquals(Color.White, style.closeHoverGlyph) + val rest = windowButtonFill(WindowButton.Maximise, style, hovered = false, pressed = false) + val hover = windowButtonFill(WindowButton.Maximise, style, hovered = true, pressed = false) + val held = windowButtonFill(WindowButton.Maximise, style, hovered = true, pressed = true) + assertEquals(Color.Transparent, rest, "$scheme: a resting button has no fill") + assertNotEquals(rest, hover, "$scheme: hover changes the fill") + assertNotEquals(hover, held, "$scheme: a held button is not the hover fill") + assertNotEquals(red, windowButtonFill(WindowButton.Close, style, hovered = true, pressed = true)) + } + } + + @Test + fun fr19_4_the_maximise_button_reads_restore_when_the_window_is_maximised() = runComposeUiTest { + val maximised = mutableStateOf(false) + setContent { + CompositionLocalProvider( + LocalFrameRequests provides FrameRequestSource(), + LocalWindowActions provides WindowActions({}, {}, {}, maximised = maximised), + ) { + ComposeRustContent( + rememberComposeRustHost( + FakeHostConnection( + listOf( + Mutation.SetTheme(Theme(DesignSystem.Fluent, DesignSystem.Fluent, ColorScheme.Light, adaptive = false)), + Mutation.Create(1, WidgetKind.Column), + ), + ), + ), + caption = WindowCaption(height = 32.dp), + ) + } + } + waitForIdle() + onNodeWithTag("window-button-maximise").assertContentDescriptionEquals("Maximise") + maximised.value = true + waitForIdle() + onNodeWithTag("window-button-maximise").assertContentDescriptionEquals("Restore") + } +} diff --git a/renderer/windows/src/Win32Window.kt b/renderer/windows/src/Win32Window.kt index 68304c1fb..49ee23704 100644 --- a/renderer/windows/src/Win32Window.kt +++ b/renderer/windows/src/Win32Window.kt @@ -18,6 +18,7 @@ import androidx.compose.ui.unit.IntSize import dev.darkpyonix.composerust.ui.node.LayoutProfile import java.lang.System import kotlinx.cinterop.COpaquePointer +import kotlinx.cinterop.reinterpret import kotlinx.cinterop.staticCFunction import org.jetbrains.skia.BackendRenderTarget import org.jetbrains.skia.ColorSpace @@ -68,6 +69,9 @@ internal class Win32Window private constructor( private val textInput = NativeTextInput() + /** Whether the window is maximised, for the caption's maximise or restore glyph. */ + val maximised = androidx.compose.runtime.mutableStateOf(false) + /** The right-click menu of text and selections, drawn by the system. */ private val textToolbar = Win32TextToolbar { native.window } @@ -216,6 +220,9 @@ internal class Win32Window private constructor( */ private fun drawFrame(): Boolean { val resource = native.beginFrame() ?: return false + // Read every frame, written only on a change: maximising resizes, and a resize draws. + val zoomed = platform.windows.IsZoomed(native.window.reinterpret()) != 0 + if (maximised.value != zoomed) maximised.value = zoomed // After the buffer and not before it: that is where a swapchain waiting to be refitted // is refitted, and what is measured here is the buffer that came back. val measured = native.measure() diff --git a/renderer/windows/src/WindowsRenderer.kt b/renderer/windows/src/WindowsRenderer.kt index 7c657b27c..8854afb23 100644 --- a/renderer/windows/src/WindowsRenderer.kt +++ b/renderer/windows/src/WindowsRenderer.kt @@ -68,6 +68,7 @@ internal fun runRenderer(connection: () -> HostConnection): Int { minimise = { Win32Window.action(0) }, maximise = { Win32Window.action(1) }, close = { Win32Window.action(2) }, + maximised = window.maximised, ) window.setContent { androidx.compose.runtime.CompositionLocalProvider(