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
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

## Unreleased

### The native-image renderer opens its own window on macOS

On the GraalVM native-image path, macOS now opens the AppKit window the renderer makes
itself, with no environment variable and no toolkit between the scene and the screen. It
carries what the toolkit's window did: the application's title, size, minimum size and
resizable choice, the system or the transparent title bar, the icon, the material behind a
glass design, files dropped on a drop target, and the window closing by itself for unattended
runs. Pointer positions and accessibility rectangles are now converted between points and
pixels, which they were not at 200% before. Korean input, VoiceOver and the feel of a live
resize at 100% and 200% still need a person on a Mac.

### The Dioxus path lives in dioxus-compose

The Dioxus adapter, the Dioxus baseline and the twelve `rsx!` samples moved to
Expand Down
54 changes: 34 additions & 20 deletions compose-rust/android-kotlin/shared/HostTextField.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ import androidx.compose.ui.input.key.onPreviewKeyEvent
import androidx.compose.ui.input.key.type
import androidx.compose.ui.text.TextRange
import androidx.compose.ui.text.input.TextFieldValue
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.Job
import kotlinx.coroutines.coroutineScope
import androidx.compose.runtime.withFrameNanos
import kotlinx.coroutines.launch
import dev.darkpyonix.composerust.protocol.ColorRole
import dev.darkpyonix.composerust.protocol.HostEvent
import dev.darkpyonix.composerust.protocol.PropertyKind
Expand All @@ -48,21 +50,11 @@ import dev.darkpyonix.composerust.ui.textStyle
import dev.darkpyonix.composerust.ui.node.HostText
import dev.darkpyonix.composerust.design.LocalGlassDepth

/**
* Quiet period before a `TextChanged` notification is sent.
*
* The Host is told about edits, but not once per keystroke: `TextChanged` is a notification,
* not the mechanism that keeps the field's value, so it can wait for typing to pause. The
* interval is chosen here rather than negotiated; 120 ms is short enough to feel immediate
* and long enough to collapse a burst of typing into one event.
*/
const val TEXT_CHANGED_DEBOUNCE_MILLIS: Long = 120

/**
* The uncontrolled TextField.
*
* The editing value and the IME composition live in `remember` and never round-trip through
* the Host. The Host learns about edits through debounced `TextChanged` and through the
* the Host. The Host learns about edits through coalesced `TextChanged` and through the
* commit events `TextSubmitted` and `FocusLost`; it changes the value only with `SetText`,
* which is held back while a composition is in progress.
*
Expand Down Expand Up @@ -104,16 +96,38 @@ internal fun HostTextField(node: Node, modifier: Modifier, dispatcher: EventDisp
}
}

// Debounced change notification. The Host is told what the field now shows; it never
// sends the value back, so the composition cannot be reset by the round trip.
// Change notification, at most one a frame.
//
// An edit is told to the Host on the next frame, carrying the latest text, so keys that
// arrive within one frame cost it one event and a key is never held back by a timer: the
// line that echoes what was typed follows the key by a frame. The last text is always
// sent, because the frame that follows an edit sends whatever the field holds then, and
// a later edit starts another. The Host is told what the field now shows; it never sends
// the value back, so the composition cannot be reset by the round trip.
if (changeHandler != null) {
LaunchedEffect(nodeId, changeHandler) {
var lastSent = value.text
snapshotFlow { value.text }.collectLatest { text ->
if (text == lastSent) return@collectLatest
delay(TEXT_CHANGED_DEBOUNCE_MILLIS)
lastSent = text
dispatcher.dispatch(HostEvent.TextChanged(nodeId, changeHandler, text))
var latest = lastSent
var sending: Job? = null
coroutineScope {
snapshotFlow { value.text }.collect { text ->
latest = text
if (text == lastSent || sending?.isActive == true) return@collect
sending = launch {
do {
// Sent from inside the frame callback, not after it. Resuming
// from withFrameNanos goes back through the scene's dispatcher,
// which on the desktop windows runs what is waiting at the start
// of the next frame, so the send and the recomposition that
// follows it each cost a frame. In the callback the Host's batch
// is applied before this frame is composed and drawn.
withFrameNanos {
lastSent = latest
dispatcher.dispatch(HostEvent.TextChanged(nodeId, changeHandler, lastSent))
}
} while (latest != lastSent)
}
}
}
}
}
Expand Down
58 changes: 58 additions & 0 deletions renderer/desktop/c/appkit_resize.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// What a live resize shows, counted.
//
// A drag of the window's edge is a run of sizes the view takes. For the picture to follow
// the edge, a frame has to be presented at each of those sizes before the next one
// arrives; a size that was left without one is shown as the previous frame scaled to fit,
// which is the stretch a reader sees. This keeps the count of both, with no AppKit in it,
// so a test can feed it the sequence of sizes and presents a drag produces and ask the
// same question the window asks of itself.
#ifndef DXC_APPKIT_RESIZE_H
#define DXC_APPKIT_RESIZE_H

#include <stdint.h>
#include <string.h>

struct dxc_resize_stats {
int64_t steps; // sizes the view took
int64_t presented; // frames handed to the screen
int64_t stale; // presented frames whose texture was not the view's size
int64_t stretched; // sizes that were left without a frame of their own
int64_t callback_ticks;
int64_t callback_max_ticks;
int32_t view_width;
int32_t view_height;
int32_t step_unanswered;
};

static inline void dxc_resize_reset(struct dxc_resize_stats *stats) {
memset(stats, 0, sizeof *stats);
}

/** The view took a new size, in pixels. */
static inline void dxc_resize_step(struct dxc_resize_stats *stats, int32_t width, int32_t height) {
if (stats->step_unanswered) {
stats->stretched++;
}
stats->steps++;
stats->step_unanswered = 1;
stats->view_width = width;
stats->view_height = height;
}

/** A frame whose texture is this size was handed to the screen. */
static inline void dxc_resize_present(struct dxc_resize_stats *stats, int32_t width,
int32_t height) {
stats->presented++;
if (width != stats->view_width || height != stats->view_height) {
stats->stale++;
} else {
stats->step_unanswered = 0;
}
}

/** Sizes left without a frame, counting the last one, which has no successor to say so. */
static inline int64_t dxc_resize_stretched(const struct dxc_resize_stats *stats) {
return stats->stretched + (stats->step_unanswered ? 1 : 0);
}

#endif
Loading
Loading