Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions compose-rust/android-kotlin/shared/ComponentRules.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions compose-rust/android-kotlin/shared/DesignSystem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

/**
Expand Down
88 changes: 68 additions & 20 deletions compose-rust/android-kotlin/shared/WindowButtons.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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<Boolean> = 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<WindowActions?> { null }

Expand Down Expand Up @@ -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
Expand All @@ -105,28 +145,23 @@ 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
}
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)
Expand All @@ -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"
}

/**
Expand All @@ -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
Expand All @@ -167,20 +203,32 @@ 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),
strokeWidth = stroke.width,
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,
Expand Down
16 changes: 16 additions & 0 deletions renderer/desktop/c/renderer_entry.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
53 changes: 52 additions & 1 deletion renderer/desktop/src/Win32Window.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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?,
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions renderer/desktop/src/renderer/ComponentRules.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions renderer/desktop/src/renderer/DesignSystem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

/**
Expand Down
Loading
Loading