From 91c836f71602548fdb3a8340e517cc83cdcac0e1 Mon Sep 17 00:00:00 2001 From: BREW <81044362+b-re-w@users.noreply.github.com> Date: Sun, 4 Oct 2026 17:33:17 +0900 Subject: [PATCH 1/8] Feat: Open the toolkit-free AppKit window by default on macOS (NFR-14) The native-image renderer opened the AppKit window of its own only when DXC_APPKIT_WINDOW was set. macOS now always opens it, and the older arrangement of a second thread for the toolkit is gone from the C entry. The window takes what the application asks for in its first batch: title, size, minimum size, resizable, system or transparent title bar with a unified toolbar so the traffic lights share the bar's line, the icon, and a material behind the page. Files dropped on a FileDropTarget reach the node under them without the toolkit, with the full path list read outside the 96 byte event record. Pointer and accessibility coordinates are converted between points and pixels. The smoke test's auto exit is honoured. Covers FR-27 (file drop) and NFR-14. fr27_* tests in NativeFileDropTest and scripts/tests/macos-window-default.test.sh. Korean IME, VoiceOver and resize feel remain manual checks on a Mac. --- CHANGELOG.md | 11 + renderer/desktop/c/appkit_window.m | 195 ++++++++- renderer/desktop/c/macos_main_thread.m | 43 +- renderer/desktop/c/renderer_entry.c | 413 +------------------- renderer/desktop/c/win32_window.c | 36 ++ renderer/desktop/c/x11_window.c | 36 ++ renderer/desktop/src/AppKitWindow.kt | 179 ++++++++- renderer/desktop/src/NativeFileDrop.kt | 121 ++++++ renderer/desktop/src/RendererEntryPoints.kt | 36 +- renderer/desktop/src/WindowEvents.kt | 1 + renderer/desktop/test/NativeFileDropTest.kt | 88 +++++ scripts/tests/macos-window-default.test.sh | 39 ++ 12 files changed, 721 insertions(+), 477 deletions(-) create mode 100644 renderer/desktop/src/NativeFileDrop.kt create mode 100644 renderer/desktop/test/NativeFileDropTest.kt create mode 100755 scripts/tests/macos-window-default.test.sh diff --git a/CHANGELOG.md b/CHANGELOG.md index 81eb86075..95d7bff1d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/renderer/desktop/c/appkit_window.m b/renderer/desktop/c/appkit_window.m index afdec41bd..c23a044c7 100644 --- a/renderer/desktop/c/appkit_window.m +++ b/renderer/desktop/c/appkit_window.m @@ -52,6 +52,8 @@ // field, separated by the one byte no path may contain. DXC_EVENT_FILES_ENTERED = 10, DXC_EVENT_FILES_DROPPED = 11, + // The files left without being let go. + DXC_EVENT_FILES_EXITED = 12, }; // Room for what an input method is composing, which is a syllable or a word and never a @@ -190,7 +192,10 @@ void dxc_native_set_accessibility(const struct dxc_element *elements, int32_t co frame:NSZeroRect label:label != nil ? label : @"" parent:view]; - NSRect local = NSMakeRect(element->x, element->y, element->width, element->height); + // The scene's pixels, turned into the view's points. + CGFloat scale = view.window.backingScaleFactor > 0 ? view.window.backingScaleFactor : 1; + NSRect local = NSMakeRect(element->x / scale, element->y / scale, + element->width / scale, element->height / scale); NSRect inWindow = [view convertRect:local toView:nil]; NSRect onScreen = [view.window convertRectToScreen:inWindow]; [made setAccessibilityFrame:onScreen]; @@ -389,6 +394,9 @@ int32_t dxc_native_poll_event(struct dxc_event *out) { * and to the one holding focus, and a plain NSView answers none of them; everything here * turns one into a record and puts it on the queue above. */ +// The paths of the files last dragged over the window, NUL separated. +static NSString *dxc_dropped_paths; + @interface DxcView : NSView @end @@ -454,11 +462,15 @@ - (BOOL)acceptsFirstMouse:(NSEvent *)event { return YES; } - (void)dxcSend:(int32_t)kind event:(NSEvent *)event { NSPoint where = [self convertPoint:event.locationInWindow fromView:nil]; + // In pixels, like the scene the events are given to: the view works in points, and a + // scene that is twice as many pixels across as points would otherwise hear every + // click at half the distance from the corner. + CGFloat scale = self.window.backingScaleFactor > 0 ? self.window.backingScaleFactor : 1; struct dxc_event record; memset(&record, 0, sizeof record); record.kind = kind; - record.x = (float)where.x; - record.y = (float)where.y; + record.x = (float)(where.x * scale); + record.y = (float)(where.y * scale); record.buttons = (int32_t)NSEvent.pressedMouseButtons; record.modifiers = (int32_t)event.modifierFlags; if (kind == DXC_EVENT_SCROLL) { @@ -514,7 +526,23 @@ - (void)dxcSendPaths:(int32_t)kind info:(id)info { } } // NUL, because it is the one byte no path on any desktop may contain. - [self dxcSendText:kind string:[paths componentsJoinedByString:@"\0"]]; + NSString *joined = [paths componentsJoinedByString:@"\0"]; + // Where the drag is, in the same pixels the pointer is reported in, so the renderer + // can tell which drop target the files are over. + NSPoint where = [self convertPoint:info.draggingLocation fromView:nil]; + CGFloat scale = self.window.backingScaleFactor > 0 ? self.window.backingScaleFactor : 1; + struct dxc_event record; + memset(&record, 0, sizeof record); + record.kind = kind; + record.x = (float)(where.x * scale); + record.y = (float)(where.y * scale); + // The paths themselves wait here, not in the event: an event has room for a syllable + // and a list of paths is longer than that by a long way. They are read once the event + // has been heard. + @synchronized ([NSApplication class]) { + dxc_dropped_paths = joined; + } + dxc_push_event(record); } - (NSDragOperation)draggingEntered:(id)sender { @@ -522,6 +550,19 @@ - (NSDragOperation)draggingEntered:(id)sender { return NSDragOperationCopy; } +// Said again as the files move, so the renderer can tell which node they are over. +- (NSDragOperation)draggingUpdated:(id)sender { + [self dxcSendPaths:DXC_EVENT_FILES_ENTERED info:sender]; + return NSDragOperationCopy; +} + +- (void)draggingExited:(id)sender { + struct dxc_event record; + memset(&record, 0, sizeof record); + record.kind = DXC_EVENT_FILES_EXITED; + dxc_push_event(record); +} + - (BOOL)performDragOperation:(id)sender { [self dxcSendPaths:DXC_EVENT_FILES_DROPPED info:sender]; return YES; @@ -676,6 +717,41 @@ - (void)updateTrackingAreas { void *layer; }; +// What the application asked of its window, held until the window is made. +// +// A struct filled in by a separate call rather than more arguments to the open call, +// because the open call is also the one a probe makes with nothing to ask for, and the +// defaults below are what that probe has always got. +static struct { + int32_t resizable; + int32_t min_width; + int32_t min_height; + int32_t system_chrome; + int32_t backdrop; +} dxc_options = {1, 0, 0, 0, 0}; + +/** + * Says how the next window should be made. Called once, before it is opened. + * + * `system_chrome` keeps the ordinary title bar above the content. Without it the content + * runs under a transparent bar and the system's three buttons stay where they are, which + * is the look every window of this renderer has had. `backdrop` puts what is behind the + * window under the page, for a design that draws glass. + */ +void dxc_native_window_configure( + int32_t resizable, + int32_t min_width, + int32_t min_height, + int32_t system_chrome, + int32_t backdrop +) { + dxc_options.resizable = resizable; + dxc_options.min_width = min_width; + dxc_options.min_height = min_height; + dxc_options.system_chrome = system_chrome; + dxc_options.backdrop = backdrop; +} + /** * Opens a window with a Metal layer filling it. * @@ -715,15 +791,34 @@ int32_t dxc_native_window_open( NSRect frame = NSMakeRect(0, 0, width, height); NSWindowStyleMask mask = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | - NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable | - NSWindowStyleMaskFullSizeContentView; + NSWindowStyleMaskMiniaturizable; + if (dxc_options.resizable) { + mask |= NSWindowStyleMaskResizable; + } + if (!dxc_options.system_chrome) { + mask |= NSWindowStyleMaskFullSizeContentView; + } NSWindow *window = [[NSWindow alloc] initWithContentRect:frame styleMask:mask backing:NSBackingStoreBuffered defer:NO]; window.title = [NSString stringWithUTF8String:title]; - window.titlebarAppearsTransparent = YES; - window.titleVisibility = NSWindowTitleHidden; + if (!dxc_options.system_chrome) { + window.titlebarAppearsTransparent = YES; + window.titleVisibility = NSWindowTitleHidden; + // An empty unified toolbar: its only job is to make the title bar the height a + // toolbar gives it, which centres the three buttons on the line the bar's own + // content is drawn on. Everything in the bar is the renderer's, underneath. + NSToolbar *toolbar = [[NSToolbar alloc] initWithIdentifier:@"compose-rust"]; + toolbar.showsBaselineSeparator = NO; + window.toolbar = toolbar; + if (@available(macOS 11.0, *)) { + window.toolbarStyle = NSWindowToolbarStyleUnified; + } + } + if (dxc_options.min_width > 0 || dxc_options.min_height > 0) { + window.contentMinSize = NSMakeSize(dxc_options.min_width, dxc_options.min_height); + } window.releasedWhenClosed = NO; // Held for the life of the window, which owns it through the delegate reference. static DxcWindowDelegate *delegate; @@ -763,6 +858,23 @@ int32_t dxc_native_window_open( [window makeFirstResponder:view]; [view registerForDraggedTypes:@[NSPasteboardTypeFileURL]]; window.acceptsMouseMovedEvents = YES; + if (dxc_options.backdrop) { + // The half of the glass that cannot be drawn: what is behind the window. The + // effect view is a sibling under the content view, in the frame view both + // hang from, so neither is asked to change. The window and the layer then + // stop claiming to fill their rectangle, or nothing composites behind them. + NSVisualEffectView *effect = [[NSVisualEffectView alloc] init]; + effect.material = NSVisualEffectMaterialSidebar; + effect.blendingMode = NSVisualEffectBlendingModeBehindWindow; + effect.state = NSVisualEffectStateActive; + effect.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable; + NSView *frame_view = window.contentView.superview; + effect.frame = frame_view.bounds; + [frame_view addSubview:effect positioned:NSWindowBelow relativeTo:window.contentView]; + window.opaque = NO; + window.backgroundColor = NSColor.clearColor; + layer.opaque = NO; + } [window center]; [window makeKeyAndOrderFront:nil]; [NSApp activateIgnoringOtherApps:YES]; @@ -779,6 +891,73 @@ int32_t dxc_native_window_open( return status; } +/** + * The strip across the top of the window that belongs to the title bar, and the room the + * system's three buttons take at its leading edge, both in points. + * + * Measured from the window, because the height follows the platform: a unified toolbar + * makes it taller than the standard bar, and the next release may change it again. Zero + * for a window that kept its ordinary title bar, which has nothing to run underneath. + */ +void dxc_native_window_caption(void *view_pointer, float *height, float *buttons_width) { + dxc_on_main(^{ + @autoreleasepool { + DxcView *view = (__bridge DxcView *)view_pointer; + NSWindow *window = view.window; + *height = 0; + *buttons_width = 0; + if (window == nil || dxc_options.system_chrome) { + return; + } + CGFloat strip = window.frame.size.height - window.contentLayoutRect.size.height; + if (strip < 0) { + return; + } + *height = (float)strip; + NSButton *close = [window standardWindowButton:NSWindowCloseButton]; + NSButton *zoom = [window standardWindowButton:NSWindowZoomButton]; + if (close != nil && zoom != nil) { + *buttons_width = (float)(NSMaxX(zoom.frame) + close.frame.origin.x); + } + } + }); +} + +/** + * The paths of the files last dragged over the window, copied into [out], and the number + * of bytes that is. They are separated by NUL, the one byte no path may contain, and the + * count is zero when nothing was carried. + */ +int32_t dxc_native_dropped_paths(char *out, int32_t capacity) { + NSString *paths; + @synchronized ([NSApplication class]) { + paths = dxc_dropped_paths; + } + const char *utf8 = paths.UTF8String; + if (utf8 == NULL) { + return 0; + } + NSUInteger bytes = [paths lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; + if ((NSUInteger)capacity < bytes) { + return 0; + } + memcpy(out, utf8, bytes); + return (int32_t)bytes; +} + +/** Gives the application the picture it named, from the bytes of an encoded image. */ +void dxc_native_set_icon(const uint8_t *bytes, int32_t length) { + NSData *data = [NSData dataWithBytes:bytes length:(NSUInteger)length]; + dxc_on_main(^{ + @autoreleasepool { + NSImage *image = [[NSImage alloc] initWithData:data]; + if (image != nil) { + NSApp.applicationIconImage = image; + } + } + }); +} + /** What the layer is drawn at, in pixels, and how many of them go to a point. */ void dxc_native_window_size(void *layer_pointer, int32_t *width, int32_t *height, float *scale) { dxc_on_main(^{ diff --git a/renderer/desktop/c/macos_main_thread.m b/renderer/desktop/c/macos_main_thread.m index cc11de026..244c50605 100644 --- a/renderer/desktop/c/macos_main_thread.m +++ b/renderer/desktop/c/macos_main_thread.m @@ -1,49 +1,16 @@ /* * macOS main-thread handling for the renderer. * - * AppKit must run on the main thread, and AWT waits for it there. If Java code occupies the - * main thread the process hangs, so the renderer runs on a second thread (as the JDK - * launcher does) and the main thread runs AppKit. - * - * The main thread creates and runs NSApplication itself instead of letting AWT do it. AWT - * then starts in embedded mode, the way it does inside an SWT or JavaFX host. Left to - * itself, AWT would enter `[NSApp run]` in a loop that never returns, and the Host would - * never get control back after the window closes. + * AppKit must run on the main thread, and the renderer runs on it: the window is its own, + * drawn and driven from the thread AppKit answers on. The application object is created + * here, before the renderer starts, so nothing finds it missing. */ #import #include -/* Called on the main thread before the renderer thread starts, so AWT finds NSApp already - created and does not install its own run loop. */ +/* Called on the main thread before the renderer thread starts, so nothing finds NSApp missing + and the renderer, which runs on this thread, finds it ready. */ void compose_rust_prepare_main_thread(void) { [NSApplication sharedApplication]; [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]; } - -/* Runs AppKit until compose_rust_stop_main_thread. */ -void compose_rust_park_main_thread(atomic_bool *finished) { - @autoreleasepool { - [NSApp activateIgnoringOtherApps:YES]; - /* The renderer may already be done if it failed early. */ - if (!atomic_load(finished)) { - [NSApp run]; - } - } -} - -void compose_rust_stop_main_thread(void) { - dispatch_async(dispatch_get_main_queue(), ^{ - [NSApp stop:nil]; - /* `stop:` takes effect after the next event, so post one. */ - NSEvent *wake = [NSEvent otherEventWithType:NSEventTypeApplicationDefined - location:NSZeroPoint - modifierFlags:0 - timestamp:0 - windowNumber:0 - context:nil - subtype:0 - data1:0 - data2:0]; - [NSApp postEvent:wake atStart:YES]; - }); -} diff --git a/renderer/desktop/c/renderer_entry.c b/renderer/desktop/c/renderer_entry.c index b10e5fb8e..ac461dbbe 100644 --- a/renderer/desktop/c/renderer_entry.c +++ b/renderer/desktop/c/renderer_entry.c @@ -76,8 +76,6 @@ void dxc_set_window_material(int32_t asked) { #include void compose_rust_prepare_main_thread(void); -void compose_rust_park_main_thread(atomic_bool *finished); -void compose_rust_stop_main_thread(void); #endif enum { @@ -233,6 +231,7 @@ static int renderer_library_dir(char *out, size_t out_size) { } #endif +#ifndef __APPLE__ /* Creates the isolate on the calling thread and runs the renderer until its window closes. */ static void *renderer_thread(void *arg) { struct renderer_run *run = arg; @@ -250,12 +249,10 @@ static void *renderer_thread(void *arg) { } #ifndef _WIN32 atomic_store(&run->finished, true); -#endif -#ifdef __APPLE__ - compose_rust_stop_main_thread(); #endif return NULL; } +#endif #ifdef _WIN32 /** @@ -515,371 +512,6 @@ static DWORD WINAPI dxc_reclaim_caption(LPVOID unused) { } #endif -#ifdef __APPLE__ -/* - * Bringing the window buttons down to the bar's line. - * - * A bar that is the window's caption puts its content on the same line as the close, - * minimise and zoom buttons, which is what macOS itself does. macOS makes that line by - * moving the buttons, not by moving the toolbar: a window with a unified toolbar has its - * buttons centred in the taller title bar. There is no way to ask for that from Java. The - * AWT peer reads eight client properties and none of them is this one, so a window built - * through AWT alone leaves the buttons centred in the standard 28 point bar while the - * bar's content sits lower, and on the calculator that was a 15 pixel step. - * - * So it is asked for here, through the Objective-C runtime, in the same file and the same - * spirit as the Windows window procedure a few hundred lines up. What may not be written - * by hand is the shim between the Host and the Renderer, which is generated; the renderer - * talking to its own window is not that. - * - * Only windows that asked for modern chrome are touched. Kotlin says so by setting - * `apple.awt.fullWindowContent`, which the peer turns into the full size content view - * style, so the style mask is the message: a window that kept the ordinary title bar - * never gets one. - */ - -// From NSWindow.h. Unified is the one that puts the title and the toolbar on one line, -// which is the line the buttons are then centred on. -enum { DXC_TOOLBAR_STYLE_UNIFIED = 3 }; -// NSWindowStyleMaskFullSizeContentView. -enum { DXC_FULL_SIZE_CONTENT_VIEW = 1 << 15 }; -// Titled, Closable, Miniaturizable, Resizable: what a window needs in its mask before -// AppKit gives it a title bar and draws the three buttons in it. -enum { - DXC_TITLED = 1 << 0, - DXC_CLOSABLE = 1 << 1, - DXC_MINIATURIZABLE = 1 << 2, - DXC_RESIZABLE = 1 << 3, -}; -// NSWindowTitleHidden. -enum { DXC_TITLE_HIDDEN = 1 }; - -static id dxc_send(id self, const char *selector) { - return ((id (*)(id, SEL))objc_msgSend)(self, sel_registerName(selector)); -} - -static id dxc_send_class(const char *name, const char *selector) { - Class type = objc_getClass(name); - if (type == NULL) { - return NULL; - } - return ((id (*)(Class, SEL))objc_msgSend)(type, sel_registerName(selector)); -} - -/** Puts a unified toolbar on one window, which is what moves its buttons. */ -static void dxc_unify_window(id window) { - long mask = ((long (*)(id, SEL))objc_msgSend)(window, sel_registerName("styleMask")); - if ((mask & DXC_FULL_SIZE_CONTENT_VIEW) == 0) { - return; - } - if (dxc_send(window, "toolbar") != NULL) { - return; - } - id identifier = ((id (*)(Class, SEL, const char *))objc_msgSend)( - objc_getClass("NSString"), sel_registerName("stringWithUTF8String:"), - "compose-rust"); - id toolbar = dxc_send_class("NSToolbar", "alloc"); - if (toolbar == NULL || identifier == NULL) { - return; - } - toolbar = ((id (*)(id, SEL, id))objc_msgSend)( - toolbar, sel_registerName("initWithIdentifier:"), identifier); - if (toolbar == NULL) { - return; - } - // The toolbar is empty and stays empty. Its job is to make the title bar the height a - // toolbar gives it, so the buttons are centred there; everything in the bar is drawn - // by the renderer underneath, through the full size content view. - ((void (*)(id, SEL, signed char))objc_msgSend)( - toolbar, sel_registerName("setShowsBaselineSeparator:"), 0); - ((void (*)(id, SEL, id))objc_msgSend)(window, sel_registerName("setToolbar:"), toolbar); - ((void (*)(id, SEL, long))objc_msgSend)( - window, sel_registerName("setToolbarStyle:"), DXC_TOOLBAR_STYLE_UNIFIED); -} - -// From NSVisualEffectView.h, blended with what is behind the window rather than with what -// is behind the view. -// -// The sidebar material rather than the window background one. The window background -// material is nearly opaque grey in the light appearance, so the desktop behind it reached -// the eye as a faint cast at best; the sidebar material is the one AppKit puts behind a -// source list, and it is what lets a pink wallpaper read as pink through the chrome that -// sits on it. The renderer paints the page over it at most of its opacity, so the page -// still reads as a page and only the chrome carries the desktop's colour at full strength. -enum { - DXC_MATERIAL_SIDEBAR = 7, - DXC_BLENDING_BEHIND_WINDOW = 0, - DXC_EFFECT_STATE_ACTIVE = 1, -}; -// NSViewWidthSizable | NSViewHeightSizable. -enum { DXC_VIEW_SIZABLE = 2 | 16 }; - -/** - * Puts a layer behind the window that shows what is behind the window. - * - * This is the half of the glass that cannot be drawn. Everything the renderer paints can - * be translucent over what it painted before, but the desktop is not something it - * painted, and a material that does not pick up the desktop is a flat tint whatever its - * alpha is: measured against a window that has it, a sidebar's blue channel climbed from - * 34 to 59 down its length while ours stayed on one value from top to bottom. - * - * The view is inserted under everything else in the content view and told to follow it, - * so it is the window's backmost layer and nothing has to lay it out. Whether any of it - * reaches the eye depends on what is painted over it, which is the renderer's half. - */ -/** - * Says why the window has no material behind it, when asked to. - * - * Every step here is a message to a toolkit object that may not be what this expects, and - * a silent early return leaves a window that simply looks ordinary with nothing to read. - * Off unless `DXC_REPORT_MATERIAL` is set, so it costs a pointer comparison in normal use. - */ -static void dxc_report_material(const char *what) { - static int asked = -1; - if (asked < 0) { - asked = getenv("DXC_REPORT_MATERIAL") != NULL; - } - if (asked) { - fprintf(stderr, "dxc material: %s\n", what); - } -} - -/** - * Stops the window claiming it fills its own rectangle, and takes its fill away. - * - * Both are needed and neither sticks. The system composites nothing behind an opaque - * window, and a window with a background colour paints that colour over whatever was - * composited; the toolkit sets the colour from its own side, so this is said again on - * every pass rather than once at the start. - */ -static void dxc_clear_window_background(id window) { - ((void (*)(id, SEL, signed char))objc_msgSend)(window, sel_registerName("setOpaque:"), 0); - id clear = dxc_send_class("NSColor", "clearColor"); - if (clear != NULL) { - ((void (*)(id, SEL, id))objc_msgSend)( - window, sel_registerName("setBackgroundColor:"), clear); - } -} - -/** - * Stops a view and everything under it from claiming to fill its own rectangle. - * - * A layer that says it is opaque is composited as though nothing behind it matters, so - * the material under the window never reaches the eye however much alpha the page was - * painted with. The renderer draws into a layer several views down, so the whole branch - * is asked rather than the top of it. - * - * Depth-limited because this walks a view tree the toolkit owns and a cycle there would - * be its problem becoming ours. - */ -static void dxc_open_layers(id view, int depth) { - if (view == NULL || depth > 12) { - return; - } - if (depth == 0) { - dxc_report_material("opening the layers under the content view"); - } - ((void (*)(id, SEL, signed char))objc_msgSend)(view, sel_registerName("setWantsLayer:"), 1); - id layer = dxc_send(view, "layer"); - if (layer != NULL) { - ((void (*)(id, SEL, signed char))objc_msgSend)(layer, sel_registerName("setOpaque:"), 0); - ((void (*)(id, SEL, void *))objc_msgSend)( - layer, sel_registerName("setBackgroundColor:"), NULL); - } - id children = dxc_send(view, "subviews"); - if (children == NULL) { - return; - } - unsigned long count = - ((unsigned long (*)(id, SEL))objc_msgSend)(children, sel_registerName("count")); - for (unsigned long i = 0; i < count; i++) { - dxc_open_layers( - ((id (*)(id, SEL, unsigned long))objc_msgSend)( - children, sel_registerName("objectAtIndex:"), i), - depth + 1); - } -} - -/** - * Gives a window its own frame back, with the system's buttons in it. - * - * The toolkit is asked for a window with no decoration, because that is the only window - * it will let draw transparently, and a window that cannot draw transparently can have - * any amount of material put behind it and show none of it. - * - * What is given up in that request is given up on the toolkit's side only. The window is - * a real one and AppKit will draw it a title bar and the three buttons the moment its - * style mask says to, so the mask is put back here: titled, closable, miniaturizable, - * resizable, and the content running the full height with the bar transparent over it. - * Nothing of the frame is drawn by this project, which is the whole point: an imitation - * of those three buttons is the most visible way to fail at looking native. - * - * Said again on every pass. Changing the mask makes AppKit rebuild the frame view, and a - * rebuilt frame has its own idea of what the window's background is. - */ -static void dxc_restore_window_frame(id window) { - long mask = ((long (*)(id, SEL))objc_msgSend)(window, sel_registerName("styleMask")); - long wanted = mask | DXC_TITLED | DXC_CLOSABLE | DXC_MINIATURIZABLE | DXC_RESIZABLE | - DXC_FULL_SIZE_CONTENT_VIEW; - if (mask != wanted) { - ((void (*)(id, SEL, long))objc_msgSend)(window, sel_registerName("setStyleMask:"), wanted); - long now = ((long (*)(id, SEL))objc_msgSend)(window, sel_registerName("styleMask")); - char line[160]; - snprintf(line, sizeof line, "frame asked for 0x%lx, window now reports 0x%lx", wanted, now); - dxc_report_material(line); - } - ((void (*)(id, SEL, signed char))objc_msgSend)( - window, sel_registerName("setTitlebarAppearsTransparent:"), 1); - ((void (*)(id, SEL, long))objc_msgSend)( - window, sel_registerName("setTitleVisibility:"), DXC_TITLE_HIDDEN); - { - static int said; - if (!said) { - said = 1; - // NSWindowCloseButton is 0. Whether AppKit built the three is the question - // this answers: a mask that reads back correctly and a window with no buttons - // in it are two different failures and look alike from a screenshot. - id close = ((id (*)(id, SEL, long))objc_msgSend)( - window, sel_registerName("standardWindowButton:"), 0); - id title = dxc_send(window, "title"); - const char *text = title == NULL ? NULL : - ((const char *(*)(id, SEL))objc_msgSend)(title, sel_registerName("UTF8String")); - signed char opaque = - ((signed char (*)(id, SEL))objc_msgSend)(window, sel_registerName("isOpaque")); - char line[220]; - snprintf(line, sizeof line, - "window %s (%s): close button %s, opaque %d", - text == NULL ? "?" : text, object_getClassName(window), - close == NULL ? "missing" : "present", (int)opaque); - dxc_report_material(line); - } - } -} - -static void dxc_back_window_with_material(id window) { - if (!atomic_load(&dxc_window_material_asked)) { - return; - } - // Only the window the renderer draws into. The toolkit keeps others of its own, and - // a material put behind one of those is a material nobody sees. - id content = dxc_send(window, "contentView"); - if (content == NULL) { - dxc_report_material("the window has no content view yet"); - return; - } - if (!((signed char (*)(id, SEL))objc_msgSend)(window, sel_registerName("isVisible"))) { - return; - } - dxc_restore_window_frame(window); - // Once is enough. This runs from a thread that asks repeatedly until the window - // exists, so without the mark it would stack a hundred of them. - id already = dxc_send(content, "superview"); - if (already != NULL) { - id siblings = dxc_send(already, "subviews"); - unsigned long count = siblings == NULL ? 0 : - ((unsigned long (*)(id, SEL))objc_msgSend)(siblings, sel_registerName("count")); - for (unsigned long i = 0; i < count; i++) { - id view = ((id (*)(id, SEL, unsigned long))objc_msgSend)( - siblings, sel_registerName("objectAtIndex:"), i); - if (view != NULL && - ((signed char (*)(id, SEL, Class))objc_msgSend)( - view, sel_registerName("isKindOfClass:"), - objc_getClass("NSVisualEffectView"))) { - // The view is there, and that is not the end of it. The toolkit sets the - // window's own background from its Java side whenever it realises or - // updates the peer, and an opaque one there covers the material however - // the view is configured, so the two are pressed down again each time - // rather than once. - dxc_clear_window_background(window); - dxc_open_layers(content, 0); - dxc_report_material("already backed"); - return; - } - } - } - - id effect = dxc_send_class("NSVisualEffectView", "alloc"); - if (effect == NULL) { - return; - } - effect = dxc_send(effect, "init"); - if (effect == NULL) { - return; - } - ((void (*)(id, SEL, long))objc_msgSend)( - effect, sel_registerName("setMaterial:"), DXC_MATERIAL_SIDEBAR); - ((void (*)(id, SEL, long))objc_msgSend)( - effect, sel_registerName("setBlendingMode:"), DXC_BLENDING_BEHIND_WINDOW); - ((void (*)(id, SEL, long))objc_msgSend)( - effect, sel_registerName("setState:"), DXC_EFFECT_STATE_ACTIVE); - ((void (*)(id, SEL, unsigned long))objc_msgSend)( - effect, sel_registerName("setAutoresizingMask:"), DXC_VIEW_SIZABLE); - - dxc_clear_window_background(window); - - // A sibling behind the content view, not a child of it and not in its place. - // - // A child is drawn over its parent's own layer, and the renderer draws into that - // layer, so an effect view added inside covered the application: the window showed - // the desktop and nothing else. Taking the content view's place instead broke the - // toolkit, which calls methods of its own on whatever the window says its content - // view is: `-[NSVisualEffectView mouseIsOver]`, unrecognised, and the process ended - // on the first frame. - // - // The window's frame view is the parent of both, so the material goes there, below - // the content view, and neither the toolkit nor the renderer is asked to change. - id frame = dxc_send(content, "superview"); - if (frame == NULL) { - return; - } - typedef struct { double x, y, w, h; } dxc_rect; - dxc_rect bounds = ((dxc_rect (*)(id, SEL))objc_msgSend)(frame, sel_registerName("bounds")); - ((void (*)(id, SEL, dxc_rect))objc_msgSend)(effect, sel_registerName("setFrame:"), bounds); - // NSWindowBelow is -1. - ((void (*)(id, SEL, id, long, id))objc_msgSend)( - frame, sel_registerName("addSubview:positioned:relativeTo:"), effect, -1, content); - dxc_open_layers(content, 0); - dxc_report_material("backed the window with a material"); -} - -/** Runs on the main thread, because AppKit is only safe there. */ -static void dxc_unify_all_windows(void *unused) { - (void)unused; - id app = dxc_send_class("NSApplication", "sharedApplication"); - if (app == NULL) { - return; - } - id windows = dxc_send(app, "windows"); - if (windows == NULL) { - return; - } - unsigned long count = - ((unsigned long (*)(id, SEL))objc_msgSend)(windows, sel_registerName("count")); - for (unsigned long i = 0; i < count; i++) { - id window = ((id (*)(id, SEL, unsigned long))objc_msgSend)( - windows, sel_registerName("objectAtIndex:"), i); - if (window != NULL) { - dxc_unify_window(window); - dxc_back_window_with_material(window); - } - } -} - -static void *dxc_unify_titlebars(void *unused) { - (void)unused; - // The window is created by AWT, on AWT's thread, some time after this returns control - // to Kotlin, and the client property that marks it arrives when the peer is realised. - // There is no callback to wait on that would not mean a bridge from Kotlin into this - // file, so this asks repeatedly for a while and then stops. A window that never - // appears leaves nothing running. - for (int attempt = 0; attempt < 600; attempt++) { - dispatch_async_f(dispatch_get_main_queue(), NULL, dxc_unify_all_windows); - usleep(50 * 1000); - } - return NULL; -} -#endif - int32_t compose_rust_renderer_run(void) { #ifdef _WIN32 @@ -928,35 +560,20 @@ int32_t compose_rust_renderer_run(void) { #ifdef __APPLE__ compose_rust_prepare_main_thread(); - // A window this library opens for itself runs on the main thread, which is the - // thread AppKit answers on and the only one it will make a window from. Everything - // else follows from that: the frame is drawn there, the scene is composed there, and - // the Host is started there, so nothing ever waits on another thread to reach the - // platform. Three separate faults came from a renderer thread waiting on this one, - // and the last of them left a window that heard nothing because the thread that - // drains its events was waiting for the thread delivering them. - // - // The toolkit's own window keeps the older arrangement, because the toolkit occupies - // the main thread itself and a renderer that took it would leave neither able to run. - if (getenv("DXC_APPKIT_WINDOW") != NULL) { - graal_isolate_t *isolate; - graal_isolatethread_t *thread; - if (graal_create_isolate(NULL, &isolate, &thread) != 0) { - return RUN_ISOLATE_FAILED; - } - atomic_store(&renderer_isolate, isolate); - return compose_rust_renderer_run_impl(thread, run.library_dir); - } - pthread_t unifier; - if (pthread_create(&unifier, NULL, dxc_unify_titlebars, NULL) == 0) { - pthread_detach(unifier); - } - pthread_t thread; - if (pthread_create(&thread, NULL, renderer_thread, &run) != 0) { - return RUN_THREAD_FAILED; + // The window is AppKit's own and runs on the main thread, which is the thread AppKit + // answers on and the only one it will make a window from. Everything else follows from + // that: the frame is drawn there, the scene is composed there, and the Host is started + // there, so nothing ever waits on another thread to reach the platform. Three separate + // faults came from a renderer thread waiting on this one, and the last of them left a + // window that heard nothing because the thread that drains its events was waiting for + // the thread delivering them. + graal_isolate_t *isolate; + graal_isolatethread_t *thread; + if (graal_create_isolate(NULL, &isolate, &thread) != 0) { + return RUN_ISOLATE_FAILED; } - compose_rust_park_main_thread(&run.finished); - pthread_join(thread, NULL); + atomic_store(&renderer_isolate, isolate); + return compose_rust_renderer_run_impl(thread, run.library_dir); #else renderer_thread(&run); #endif diff --git a/renderer/desktop/c/win32_window.c b/renderer/desktop/c/win32_window.c index 4e44fedad..da3cf474f 100644 --- a/renderer/desktop/c/win32_window.c +++ b/renderer/desktop/c/win32_window.c @@ -1443,6 +1443,42 @@ void dxc_native_clipboard_write(const char *text) { (void)text; } +/* + * Asked for by name by the window code every desktop shares, and answered here by saying + * nothing: this window takes its options another way, has no strip of the system's to + * measure, and reads no list of dropped paths from the platform. + */ +void dxc_native_window_configure( + int32_t resizable, + int32_t min_width, + int32_t min_height, + int32_t system_chrome, + int32_t backdrop +) { + (void)resizable; + (void)min_width; + (void)min_height; + (void)system_chrome; + (void)backdrop; +} + +void dxc_native_window_caption(void *view_pointer, float *height, float *buttons_width) { + (void)view_pointer; + *height = 0; + *buttons_width = 0; +} + +void dxc_native_set_icon(const uint8_t *bytes, int32_t length) { + (void)bytes; + (void)length; +} + +int32_t dxc_native_dropped_paths(char *out, int32_t capacity) { + (void)out; + (void)capacity; + return 0; +} + void dxc_native_set_frame_callback(void *callback, void *isolate_thread) { (void)callback; (void)isolate_thread; diff --git a/renderer/desktop/c/x11_window.c b/renderer/desktop/c/x11_window.c index cf89886e2..46c8cc6a9 100644 --- a/renderer/desktop/c/x11_window.c +++ b/renderer/desktop/c/x11_window.c @@ -355,6 +355,42 @@ void dxc_native_install_menu(const char *application_name) { (void)application_name; } +/* + * Asked for by name by the window code every desktop shares, and answered here by saying + * nothing: this window takes its options another way, has no strip of the system's to + * measure, and reads no list of dropped paths from the platform. + */ +void dxc_native_window_configure( + int32_t resizable, + int32_t min_width, + int32_t min_height, + int32_t system_chrome, + int32_t backdrop +) { + (void)resizable; + (void)min_width; + (void)min_height; + (void)system_chrome; + (void)backdrop; +} + +void dxc_native_window_caption(void *view_pointer, float *height, float *buttons_width) { + (void)view_pointer; + *height = 0; + *buttons_width = 0; +} + +void dxc_native_set_icon(const uint8_t *bytes, int32_t length) { + (void)bytes; + (void)length; +} + +int32_t dxc_native_dropped_paths(char *out, int32_t capacity) { + (void)out; + (void)capacity; + return 0; +} + void dxc_native_set_draw_callback(void *callback, void *isolate_thread) { (void)callback; (void)isolate_thread; diff --git a/renderer/desktop/src/AppKitWindow.kt b/renderer/desktop/src/AppKitWindow.kt index 20972fbb4..b36101aae 100644 --- a/renderer/desktop/src/AppKitWindow.kt +++ b/renderer/desktop/src/AppKitWindow.kt @@ -30,6 +30,8 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.SolidColor import androidx.compose.ui.graphics.asComposeCanvas +import androidx.compose.ui.graphics.asSkiaBitmap +import dev.darkpyonix.composerust.runtime.asksForWindowMaterial import androidx.compose.ui.scene.CanvasLayersComposeScene import androidx.compose.ui.scene.ComposeScene import androidx.compose.ui.text.TextStyle @@ -96,6 +98,24 @@ private external fun clipboardWrite(text: CCharPointer?) @CFunction("dxc_native_install_menu") private external fun installMenu(name: CCharPointer?) +@CFunction("dxc_native_window_configure") +private external fun configureWindow( + resizable: Int, + minWidth: Int, + minHeight: Int, + systemChrome: Int, + backdrop: Int, +) + +@CFunction("dxc_native_window_caption") +private external fun windowCaption(view: Pointer?, height: CFloatPointer?, buttonsWidth: CFloatPointer?) + +@CFunction("dxc_native_set_icon") +private external fun setIcon(bytes: CCharPointer?, length: Int) + +@CFunction("dxc_native_dropped_paths") +private external fun droppedPaths(out: Pointer?, capacity: Int): Int + @CFunction("dxc_native_window_closed") private external fun windowClosed(): Int @@ -228,6 +248,68 @@ internal fun describeWindow(view: Long, elements: List) { */ fun setPointerShape(shape: Int) = setCursorShape(shape) +/** + * What the application asked of its window, handed over before the window is made. + * + * Sizes are in points, which is what the window measures its content in. + */ +internal fun configureNativeWindow( + resizable: Boolean, + minWidth: Int, + minHeight: Int, + systemChrome: Boolean, + backdrop: Boolean, +) = configureWindow( + if (resizable) 1 else 0, + minWidth, + minHeight, + if (systemChrome) 1 else 0, + if (backdrop) 1 else 0, +) + +/** + * The strip of the window the title bar occupies and the room its three buttons take at the + * leading edge, as the window reports them. + * + * Measured rather than assumed, because the height follows the platform: it is taller + * under a toolbar than under the standard bar and has changed between releases. + */ +internal fun NativeWindow.measureCaption(): dev.darkpyonix.composerust.runtime.WindowCaption { + val height = StackValue.get(4) + val buttons = StackValue.get(4) + windowCaption(WordFactory.pointer(view), height, buttons) + return dev.darkpyonix.composerust.runtime.WindowCaption( + height = height.read().dp, + buttonsWidth = buttons.read().dp, + buttonsAtStart = true, + ) +} + +/** The paths of the files last dragged over the window, one string, NUL between them. */ +internal fun readDroppedPaths(): String { + val buffer = StackValue.get(DROPPED_PATHS_BYTES) + val length = droppedPaths(buffer, DROPPED_PATHS_BYTES) + if (length <= 0) return "" + val bytes = ByteArray(length) + for (index in 0 until length) { + bytes[index] = buffer.readByte(index) + } + return String(bytes, Charsets.UTF_8) +} + +private const val DROPPED_PATHS_BYTES = 64 * 1024 + +/** Puts [png] on the application, which is what the Dock and the switcher show. */ +internal fun setApplicationIcon(png: ByteArray) { + val holder = CTypeConversion.toCBytes(png) + try { + setIcon(holder.get(), png.size) + } finally { + holder.close() + } +} + + /** * Lets the window answer for itself for a moment. * @@ -341,16 +423,29 @@ fun openNativeWindow(title: String, width: Int, height: Int): NativeWindow? { private const val WINDOW_STRUCT_BYTES = 40 /** - * Draws a Compose scene into a window of our own, and holds it there. + * Draws the application into a window of our own, and holds it there until it is closed. * - * The step worth taking first, and the one everything after it rests on: a scene that - * Compose composed, painted by Skia into a drawable AppKit gave us, reaching the screen - * with no toolkit anywhere between. What follows is input, text and the rest, and none of - * it means anything until this does. + * A scene that Compose composed, painted by Skia into a drawable AppKit gave us, reaching + * the screen with no toolkit anywhere between. This is the window every macOS run opens: + * it is made from what the application asked for in its first batch, takes its title bar + * from the system, and hears the input method and the screen reader through AppKit's own + * protocols. * - * Reached by setting `DXC_APPKIT_WINDOW`, so the ordinary path is untouched. + * [autoExitMillis] closes the window by itself after that long, for runs that nobody + * watches. */ -internal fun runAppKitSpike() { +internal fun runAppKitWindow(autoExitMillis: Long? = null) { + // Before the first frame, because every piece of text drawn after this reads them. + dev.darkpyonix.composerust.design.installPlatformUiFamily() + dev.darkpyonix.composerust.ui.installReducedMotion() + dev.darkpyonix.composerust.ui.installHighContrast() + dev.darkpyonix.composerust.ui.node.platformFileDrop = { modifier, node, dispatcher -> + modifier.nativeFileDrop(node, dispatcher) + } + // The window is a real one with the desktop behind it, so a design that draws glass + // can let that show through. Set before the Host starts: its first batch may already + // ask. + dev.darkpyonix.composerust.runtime.platformBacksWindowWithMaterial = { true } // The Host is started before there is a window, because what the window should look // like is in its first batch and a window cannot be told afterwards. Started on this // thread, which is the one every later call to it is made from: the boundary is a @@ -358,6 +453,14 @@ internal fun runAppKitSpike() { val host = dev.darkpyonix.composerust.runtime.ComposeRustHost(NativeHostConnection()) host.start() val asked = host.table.window + val backdrop = host.table.asksForWindowMaterial(host.roots) + configureNativeWindow( + resizable = asked?.resizable ?: true, + minWidth = asked?.minWidth?.takeIf { it > 0 } ?: 0, + minHeight = asked?.minHeight?.takeIf { it > 0 } ?: 0, + systemChrome = asked?.chrome == dev.darkpyonix.composerust.protocol.Chrome.System, + backdrop = backdrop, + ) val window = openNativeWindow( asked?.title?.takeIf { it.isNotEmpty() } ?: "compose-rust", if (asked != null && asked.width > 0) asked.width else 520, @@ -379,6 +482,10 @@ internal fun runAppKitSpike() { // a size reads this: the scene, the render target, and what the scene is told about // the window it is in. var size = androidx.compose.ui.unit.IntSize(measured.width, measured.height) + // The strip the title bar takes, which the bar at the top of the application's tree is + // laid out around. Measured again whenever the window changes size, because entering + // full screen removes the bar and leaving it brings it back. + val caption = androidx.compose.runtime.mutableStateOf(window.measureCaption()) val textInput = NativeTextInput() val semantics = NativeSemantics { elements -> if (report) { @@ -397,11 +504,25 @@ internal fun runAppKitSpike() { coroutineContext = work, platformContext = NativePlatformContext({ size }, textInput, semantics), ) - // The application's own tree, drawn by the same interpreter the toolkit path uses. - // Nothing in it knows which of the two it is running on, which is the point. - scene.setContent { dev.darkpyonix.composerust.runtime.ComposeRustContent(host) } + // The application's own tree, drawn by the same interpreter every window uses. + // Nothing in it knows which window it is in, which is the point. + scene.setContent { + androidx.compose.runtime.CompositionLocalProvider( + dev.darkpyonix.composerust.runtime.LocalSystemDarkObserver provides { + rememberSystemDark().value + }, + ) { + dev.darkpyonix.composerust.runtime.ComposeRustContent( + host, + Modifier.fillMaxSize(), + caption = caption.value, + ) + } + } installApplicationMenu(asked?.title?.takeIf { it.isNotEmpty() } ?: "compose-rust") + val started = System.nanoTime() + var iconId = 0 try { // A plain loop rather than a clock. Pacing is the frame clock's work and comes // later; what this has to show is that what the window hears reaches the scene @@ -409,6 +530,11 @@ internal fun runAppKitSpike() { var painted = false var frame = 0 while (!isWindowClosed()) { + if (autoExitMillis != null && + (System.nanoTime() - started) / NANOS_PER_MILLI >= autoExitMillis + ) { + break + } frame++ // The window's own turn, before anything is read from it. This thread is the // one AppKit delivers on, so the events of this frame arrive here or not at @@ -419,19 +545,38 @@ internal fun runAppKitSpike() { // scene's own work, and a list that asked for rows on the last frame wants // them in hand before this one is measured. work.runPending() + // The application's picture, once the asset it named has arrived. The id is + // known from the first batch and the bitmap a little later, so this asks each + // frame until it is there and then stops. + if (iconId == 0 && asked != null && asked.icon != 0) { + val raster = host.table.assets.asset(asked.icon) + as? dev.darkpyonix.composerust.ui.node.Asset.Raster + if (raster != null) { + iconId = asked.icon + org.jetbrains.skia.Image.makeFromBitmap(raster.bitmap.asSkiaBitmap()) + .encodeToData(org.jetbrains.skia.EncodedImageFormat.PNG) + ?.bytes + ?.let(::setApplicationIcon) + } + } var heard = false var drew = false for (event in drainWindowEvents()) { if (report && event.kind != WindowEvent.POINTER_MOVE) { System.err.println("compose-rust: window heard $event") } - if (event.kind == WindowEvent.FILES_DROPPED) { - val paths = event.text.split('\u0000').filter { it.isNotEmpty() } - spikeDroppedFiles.value = "dropped ${paths.size}: ${paths.joinToString(", ")}" + if (event.kind == WindowEvent.FILES_ENTERED || + event.kind == WindowEvent.FILES_DROPPED || + event.kind == WindowEvent.FILES_EXITED + ) { + routeFileDrop(event.kind, androidx.compose.ui.geometry.Offset(event.x, event.y)) { + readDroppedPaths() + } } if (event.kind == WindowEvent.RESIZE) { size = androidx.compose.ui.unit.IntSize(event.x.toInt(), event.y.toInt()) scene.size = size + caption.value = window.measureCaption() } scene.receive(event) textInput.receive(event) @@ -443,7 +588,7 @@ internal fun runAppKitSpike() { // left the input method unable to reach this process at all, which showed up // as every letter being committed on its own instead of composing. if (!painted || heard || scene.hasInvalidations()) { - drawFrame(window, context, scene, frame.toLong() * FRAME_NANOS, size) + drawFrame(window, context, scene, frame.toLong() * FRAME_NANOS, size, backdrop) painted = true drew = true } @@ -461,6 +606,8 @@ internal fun runAppKitSpike() { } } +private const val NANOS_PER_MILLI = 1_000_000L + /** One frame: take a drawable, let the scene paint it, give it to the screen. */ private fun drawFrame( window: NativeWindow, @@ -468,6 +615,7 @@ private fun drawFrame( scene: ComposeScene, nanos: Long, size: androidx.compose.ui.unit.IntSize, + transparent: Boolean = false, ) { val texture = window.beginFrame() // Zero means the system had no drawable to give, which happens when frames are made @@ -487,6 +635,9 @@ private fun drawFrame( window.endFrame() return } + // A drawable comes back with whatever was last in it, and a window that shows what is + // behind it must start every frame from nothing rather than from the last one. + if (transparent) surface.canvas.clear(0) scene.render(surface.canvas.asComposeCanvas(), nanos) // Submitted, not only recorded. Skia's Metal backend keeps the frame in a command // buffer of its own, and a drawable presented before that buffer runs is a drawable diff --git a/renderer/desktop/src/NativeFileDrop.kt b/renderer/desktop/src/NativeFileDrop.kt new file mode 100644 index 000000000..1b99d08ae --- /dev/null +++ b/renderer/desktop/src/NativeFileDrop.kt @@ -0,0 +1,121 @@ +package dev.darkpyonix.composerust.ui.platform + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.DisposableEffect +import androidx.compose.ui.Modifier +import androidx.compose.ui.geometry.Offset +import androidx.compose.ui.geometry.Rect +import androidx.compose.ui.layout.boundsInWindow +import androidx.compose.ui.layout.onGloballyPositioned +import dev.darkpyonix.composerust.protocol.HostEvent +import dev.darkpyonix.composerust.protocol.PropertyKind +import dev.darkpyonix.composerust.protocol.WidgetKind +import dev.darkpyonix.composerust.runtime.EventDispatcher +import dev.darkpyonix.composerust.ui.node.Node + +// Files let go over a window of our own, delivered to the node they were let go over. +// +// The toolkit's window did this through the toolkit's drag and drop, which knows which +// component is under the pointer. A window of our own is told where the files are and +// nothing else, so the nodes that take files say where they are and this finds the one +// under the point. No toolkit type appears here, which is what lets the same file serve +// every window that has no toolkit. + +/** + * The nodes that take files, and which of them the files are over. + * + * One window's worth, because a drag has one place it is over at a time. Called only from + * the thread the scene runs on, which is the thread every event of the window arrives on. + */ +internal object NativeFileDrops { + + private class Target( + val nodeId: Int, + var bounds: Rect, + val entered: Long?, + val dropped: Long?, + val dispatcher: EventDispatcher, + ) + + // Insertion order is composition order, and a node composed later is drawn over one + // composed earlier, so the last that contains a point is the one on top. + private val targets = LinkedHashMap() + private var over: Int? = null + + fun register( + nodeId: Int, + bounds: Rect, + entered: Long?, + dropped: Long?, + dispatcher: EventDispatcher, + ) { + val known = targets[nodeId] + if (known != null && known.entered == entered && known.dropped == dropped) { + known.bounds = bounds + return + } + targets.remove(nodeId) + targets[nodeId] = Target(nodeId, bounds, entered, dropped, dispatcher) + } + + fun unregister(nodeId: Int) { + targets.remove(nodeId) + if (over == nodeId) over = null + } + + private fun at(position: Offset): Target? = + targets.values.lastOrNull { it.bounds.contains(position) } + + /** Files are over [position]. Said once when they come over a node, not on every move. */ + fun enter(position: Offset) { + val target = at(position) + if (target?.nodeId == over) return + over = target?.nodeId + val handler = target?.entered ?: return + target.dispatcher.dispatch(HostEvent.FilesEntered(target.nodeId, handler)) + } + + /** The files left the window without being let go. */ + fun leave() { + over = null + } + + /** Whether the files were taken by a node. */ + fun drop(position: Offset, paths: List): Boolean { + over = null + val target = at(position) ?: return false + val handler = target.dropped ?: return false + if (paths.isEmpty()) return false + target.dispatcher.dispatch( + HostEvent.FilesDropped(target.nodeId, handler, paths.joinToString("\u0000")), + ) + return true + } +} + +/** Makes a file drop target of a node that asked to be one, and leaves the rest alone. */ +@Composable +internal fun Modifier.nativeFileDrop(node: Node, dispatcher: EventDispatcher): Modifier { + if (node.widget != WidgetKind.FileDropTarget) return this + val entered = node.handler(PropertyKind.OnFilesEntered) + val dropped = node.handler(PropertyKind.OnFilesDropped) + DisposableEffect(node.id) { + onDispose { NativeFileDrops.unregister(node.id) } + } + return onGloballyPositioned { coordinates -> + NativeFileDrops.register(node.id, coordinates.boundsInWindow(), entered, dropped, dispatcher) + } +} + +/** Splits what the window carried into paths. They are separated by the byte no path holds. */ +internal fun splitDroppedPaths(carried: String): List = + carried.split('\u0000').filter { it.isNotEmpty() } + +/** Routes the window's file events to the node under the files. */ +internal fun routeFileDrop(kind: Int, position: Offset, carried: () -> String) { + when (kind) { + WindowEvent.FILES_ENTERED -> NativeFileDrops.enter(position) + WindowEvent.FILES_EXITED -> NativeFileDrops.leave() + WindowEvent.FILES_DROPPED -> NativeFileDrops.drop(position, splitDroppedPaths(carried())) + } +} diff --git a/renderer/desktop/src/RendererEntryPoints.kt b/renderer/desktop/src/RendererEntryPoints.kt index 59df5e90c..3d8017305 100644 --- a/renderer/desktop/src/RendererEntryPoints.kt +++ b/renderer/desktop/src/RendererEntryPoints.kt @@ -7,7 +7,7 @@ import org.graalvm.nativeimage.c.function.CEntryPoint import org.graalvm.nativeimage.c.type.CCharPointer import org.graalvm.nativeimage.c.type.CTypeConversion import dev.darkpyonix.composerust.ui.platform.NativeHostConnection -import dev.darkpyonix.composerust.ui.platform.runAppKitSpike +import dev.darkpyonix.composerust.ui.platform.runAppKitWindow import dev.darkpyonix.composerust.ui.platform.runWin32Window import dev.darkpyonix.composerust.ui.platform.runX11Window import org.graalvm.nativeimage.c.function.CFunction @@ -37,16 +37,15 @@ private external fun setWindowMaterial(asked: Int) fun rendererRun(thread: IsolateThread?, libraryDir: CCharPointer?): Int = try { configureRuntimeLayout(CTypeConversion.toJavaString(libraryDir)) - // The window of our own, while it is being built. Off unless asked for: every - // sample and every test still rides the toolkit's path until this one can carry - // them. - // - // One per platform, and each asked for by name. The C files behind them answer - // for the same symbols, so the one compiled into an image is the one any of them - // would reach: asking for the Windows window on a Mac would open an AppKit window - // and read its view as a Direct3D device. Which platform this is decides, rather - // than which variable was set. + // Which window this platform opens. macOS opens one of its own, made from AppKit + // and Metal with no toolkit between; the other platforms still open the + // toolkit's. The C files behind each answer for the same symbols, so the one + // compiled into an image is the one that can be reached: which platform this is + // decides, and nothing is read from the environment. val platform = System.getProperty("os.name", "") + val autoExitMillis = + (System.getenv("COMPOSE_RUST_AUTOEXIT_MS") ?: System.getenv("DIOXUS_COMPOSE_AUTOEXIT_MS")) + ?.toLongOrNull() // The notification centre, before the Host starts: its first batch may already post // one. macOS and Windows reach theirs through the C this image was linked with; // Linux speaks to the notification daemon over the session bus. A development run on @@ -62,14 +61,14 @@ fun rendererRun(thread: IsolateThread?, libraryDir: CCharPointer?): Int = ) else -> UnsupportedNotifications } - if (System.getenv("DXC_APPKIT_WINDOW") != null && platform.startsWith("Mac")) { + if (platform.startsWith("Mac")) { // Before anything else on this path. The toolkit, if it is ever woken, asks // the main thread to run the application, and this thread is the one drawing // the frames: that request is delivered on the first frame and never comes // back. Saying up front that there is no display to open keeps the toolkit // from asking, and nothing on this path wants one. System.setProperty("java.awt.headless", "true") - runAppKitSpike() + runAppKitWindow(autoExitMillis) return@rendererRun 0 } if (System.getenv("DXC_WIN32_WINDOW") != null && platform.startsWith("Windows")) { @@ -83,14 +82,13 @@ fun rendererRun(thread: IsolateThread?, libraryDir: CCharPointer?): Int = dev.darkpyonix.composerust.ui.node.platformWindowMaterial = { asked -> setWindowMaterial(if (asked) 1 else 0) } - // And the other half of that: whether asking actually put anything there. This - // build's C entry does, on macOS, which is what the line above calls. The - // Kotlin/Native renderer opens its own window and has no effect view to put under - // it, so it leaves the default alone and its design system draws for an opaque - // window rather than for a desktop that never shows through. - dev.darkpyonix.composerust.runtime.platformBacksWindowWithMaterial = { platform.startsWith("Mac") } + // Whether asking for a material actually put anything behind the window. The + // toolkit's window cannot, on any platform that still opens one, so its design + // system draws for an opaque window rather than for a desktop that never shows + // through. + dev.darkpyonix.composerust.runtime.platformBacksWindowWithMaterial = { false } // Lets automated smoke tests close the window; unset in normal use. - runRenderer((System.getenv("COMPOSE_RUST_AUTOEXIT_MS") ?: System.getenv("DIOXUS_COMPOSE_AUTOEXIT_MS"))?.toLongOrNull()) { + runRenderer(autoExitMillis) { NativeHostConnection() } 0 diff --git a/renderer/desktop/src/WindowEvents.kt b/renderer/desktop/src/WindowEvents.kt index 7b3c30c44..b9fc74809 100644 --- a/renderer/desktop/src/WindowEvents.kt +++ b/renderer/desktop/src/WindowEvents.kt @@ -53,6 +53,7 @@ data class WindowEvent( const val RESIZE = 9 const val FILES_ENTERED = 10 const val FILES_DROPPED = 11 + const val FILES_EXITED = 12 } } diff --git a/renderer/desktop/test/NativeFileDropTest.kt b/renderer/desktop/test/NativeFileDropTest.kt new file mode 100644 index 000000000..28879fce2 --- /dev/null +++ b/renderer/desktop/test/NativeFileDropTest.kt @@ -0,0 +1,88 @@ +package dev.darkpyonix.composerust.test + +import androidx.compose.ui.geometry.Offset +import androidx.compose.ui.geometry.Rect +import dev.darkpyonix.composerust.protocol.HostEvent +import dev.darkpyonix.composerust.runtime.EventDispatcher +import dev.darkpyonix.composerust.ui.platform.NativeFileDrops +import dev.darkpyonix.composerust.ui.platform.WindowEvent +import dev.darkpyonix.composerust.ui.platform.routeFileDrop +import dev.darkpyonix.composerust.ui.platform.splitDroppedPaths +import kotlin.test.AfterTest +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertTrue + +/** + * Files let go over a window that has no toolkit to tell which node they are over. + * + * The window reports a place and a list of paths; the node that takes files has said where + * it is. These check that the one under the place gets the files, once, and that a place + * nobody claims gets nothing. + */ +class NativeFileDropTest { + + private val heard = mutableListOf() + private val dispatcher = EventDispatcher { event -> heard += event; true } + + @AfterTest + fun clear() { + NativeFileDrops.unregister(1) + NativeFileDrops.unregister(2) + } + + @Test + fun fr27_files_let_go_over_a_target_reach_that_target() { + NativeFileDrops.register(1, Rect(0f, 0f, 100f, 100f), entered = 7L, dropped = 8L, dispatcher) + val taken = NativeFileDrops.drop(Offset(50f, 50f), listOf("/a.txt", "/b.txt")) + assertTrue(taken) + assertEquals(listOf(HostEvent.FilesDropped(1, 8L, "/a.txt\u0000/b.txt")), heard) + } + + @Test + fun fr27_files_let_go_where_no_target_is_reach_nothing() { + NativeFileDrops.register(1, Rect(0f, 0f, 100f, 100f), entered = null, dropped = 8L, dispatcher) + assertFalse(NativeFileDrops.drop(Offset(500f, 500f), listOf("/a.txt"))) + assertTrue(heard.isEmpty()) + } + + @Test + fun fr27_the_target_drawn_on_top_gets_the_files() { + NativeFileDrops.register(1, Rect(0f, 0f, 200f, 200f), entered = null, dropped = 1L, dispatcher) + NativeFileDrops.register(2, Rect(50f, 50f, 100f, 100f), entered = null, dropped = 2L, dispatcher) + NativeFileDrops.drop(Offset(60f, 60f), listOf("/a.txt")) + assertEquals(listOf(HostEvent.FilesDropped(2, 2L, "/a.txt")), heard) + } + + @Test + fun fr27_entering_is_said_once_per_target_not_on_every_move() { + NativeFileDrops.register(1, Rect(0f, 0f, 100f, 100f), entered = 7L, dropped = 8L, dispatcher) + NativeFileDrops.enter(Offset(10f, 10f)) + NativeFileDrops.enter(Offset(20f, 20f)) + assertEquals(listOf(HostEvent.FilesEntered(1, 7L)), heard) + NativeFileDrops.leave() + NativeFileDrops.enter(Offset(10f, 10f)) + assertEquals(2, heard.size) + } + + @Test + fun fr27_the_window_routes_its_file_events_to_the_target() { + NativeFileDrops.register(1, Rect(0f, 0f, 100f, 100f), entered = 7L, dropped = 8L, dispatcher) + routeFileDrop(WindowEvent.FILES_ENTERED, Offset(5f, 5f)) { "" } + routeFileDrop(WindowEvent.FILES_DROPPED, Offset(5f, 5f)) { "/x\u0000/y" } + assertEquals( + listOf( + HostEvent.FilesEntered(1, 7L), + HostEvent.FilesDropped(1, 8L, "/x\u0000/y"), + ), + heard, + ) + } + + @Test + fun fr27_a_path_the_window_could_not_give_is_dropped_and_the_rest_arrive() { + assertEquals(listOf("/a", "/b"), splitDroppedPaths("/a\u0000\u0000/b")) + assertTrue(splitDroppedPaths("").isEmpty()) + } +} diff --git a/scripts/tests/macos-window-default.test.sh b/scripts/tests/macos-window-default.test.sh new file mode 100755 index 000000000..b9ee92770 --- /dev/null +++ b/scripts/tests/macos-window-default.test.sh @@ -0,0 +1,39 @@ +#!/usr/bin/env bash +# The window macOS opens is the one of our own, with no environment variable asking for it. +# +# It used to be reached only when a variable was set, so every run without it opened the +# toolkit's window and the one of our own was exercised by hand. A test cannot open a window +# here, but it can read the two places that decide: the Kotlin entry point, which must not +# consult the environment to choose a macOS window, and the C entry, which must not start +# the older arrangement of a second thread for the toolkit. +set -uo pipefail + +repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" +kotlin="$repo_root/renderer/desktop/src/RendererEntryPoints.kt" +entry="$repo_root/renderer/desktop/c/renderer_entry.c" +status=0 + +fail() { echo "FAIL: $1"; status=1; } + +[[ -f "$kotlin" && -f "$entry" ]] || { echo "FAIL: the entry points are missing"; exit 1; } + +if grep -rn "DXC_APPKIT_WINDOW" "$repo_root/renderer" --include='*.kt' --include='*.c' --include='*.m' --include='*.sh' >/dev/null; then + fail "something still reads DXC_APPKIT_WINDOW, so the window depends on the environment" +fi + +if ! grep -Eq 'startsWith\("Mac"\)\) \{' "$kotlin" || ! grep -q 'runAppKitWindow(' "$kotlin"; then + fail "RendererEntryPoints.kt does not open the window of our own for every macOS run" +fi + +# Between the macOS branch and its closing brace there must be no environment read. +branch="$(awk '/if \(platform.startsWith\("Mac"\)\) \{/{on=1} on{print} on && /^ \}/{exit}' "$kotlin")" +if grep -q 'getenv' <<< "$branch"; then + fail "the macOS branch reads the environment to decide on its window" +fi + +if grep -q 'compose_rust_park_main_thread\|dxc_unify_titlebars\|DXC_APPKIT' "$entry"; then + fail "renderer_entry.c still carries the toolkit's main-thread arrangement" +fi + +[[ $status -eq 0 ]] && echo "macOS opens its own window by default: ok" +exit $status From 6939926093953df3b2d8dc67dcb1d808c327c98e Mon Sep 17 00:00:00 2001 From: BREW <81044362+b-re-w@users.noreply.github.com> Date: Sun, 4 Oct 2026 17:48:42 +0900 Subject: [PATCH 2/8] Fix: Carry on when the process manager refuses the foreground request (NFR-14) An unattended macOS runner refuses TransformProcessType, and the window can still be made once the activation policy is set to regular. The refusal is reported instead of stopping the renderer. --- renderer/desktop/c/appkit_window.m | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/renderer/desktop/c/appkit_window.m b/renderer/desktop/c/appkit_window.m index c23a044c7..9992f1774 100644 --- a/renderer/desktop/c/appkit_window.m +++ b/renderer/desktop/c/appkit_window.m @@ -773,9 +773,16 @@ int32_t dxc_native_window_open( // that this executable owns a foreground window. AWT performs this registration // for its own window, but the headless path bypasses it. ProcessSerialNumber process = {0, kCurrentProcess}; - if (TransformProcessType(&process, kProcessTransformToForegroundApplication) != noErr) { - status = 3; - return; + OSStatus transformed = + TransformProcessType(&process, kProcessTransformToForegroundApplication); + if (transformed != noErr) { + // Not fatal. The application's activation policy is set to regular below, + // which is what makes it a foreground application, and a session that + // refuses this call (an unattended runner, a remote login) still opens a + // window with it. What the system said is kept so a window that does not come + // forward has something to be read from. + fprintf(stderr, "compose-rust: the process manager answered %d to the " + "foreground request\n", (int)transformed); } id device = MTLCreateSystemDefaultDevice(); From 8c8d5f82b964a7d7d3bf256c7dd1e8abe17e1760 Mon Sep 17 00:00:00 2001 From: BREW <81044362+b-re-w@users.noreply.github.com> Date: Sun, 4 Oct 2026 17:50:53 +0900 Subject: [PATCH 3/8] Feat: Wire the window's clipboard and take the icon as pixels (NFR-14) A process told it has no display is given no clipboard by Compose desktop, so copy and paste in a text field went nowhere. The window now supplies the clipboard from its own pasteboard. The icon is passed as premultiplied RGBA bytes so that every desktop can read the same record. --- renderer/desktop/c/appkit_window.m | 33 +++++++++++++---- renderer/desktop/c/win32_window.c | 7 ++-- renderer/desktop/c/x11_window.c | 7 ++-- renderer/desktop/src/AppKitWindow.kt | 40 ++++++++++++++++----- renderer/desktop/src/NativeClipboard.kt | 48 +++++++++++++++++++++++++ renderer/desktop/test/IconPixelsTest.kt | 31 ++++++++++++++++ 6 files changed, 145 insertions(+), 21 deletions(-) create mode 100644 renderer/desktop/src/NativeClipboard.kt create mode 100644 renderer/desktop/test/IconPixelsTest.kt diff --git a/renderer/desktop/c/appkit_window.m b/renderer/desktop/c/appkit_window.m index 9992f1774..df67989ae 100644 --- a/renderer/desktop/c/appkit_window.m +++ b/renderer/desktop/c/appkit_window.m @@ -952,15 +952,36 @@ int32_t dxc_native_dropped_paths(char *out, int32_t capacity) { return (int32_t)bytes; } -/** Gives the application the picture it named, from the bytes of an encoded image. */ -void dxc_native_set_icon(const uint8_t *bytes, int32_t length) { - NSData *data = [NSData dataWithBytes:bytes length:(NSUInteger)length]; +/** + * Gives the application the picture it named, from its pixels: eight bits each of red, + * green, blue and alpha, with the colour already multiplied by the alpha, row after row + * with no padding. + */ +void dxc_native_set_icon(const uint8_t *rgba, int32_t width, int32_t height) { + if (rgba == NULL || width <= 0 || height <= 0) { + return; + } + NSData *data = [NSData dataWithBytes:rgba length:(NSUInteger)width * (NSUInteger)height * 4]; dxc_on_main(^{ @autoreleasepool { - NSImage *image = [[NSImage alloc] initWithData:data]; - if (image != nil) { - NSApp.applicationIconImage = image; + NSBitmapImageRep *representation = [[NSBitmapImageRep alloc] + initWithBitmapDataPlanes:NULL + pixelsWide:width + pixelsHigh:height + bitsPerSample:8 + samplesPerPixel:4 + hasAlpha:YES + isPlanar:NO + colorSpaceName:NSDeviceRGBColorSpace + bytesPerRow:(NSInteger)width * 4 + bitsPerPixel:32]; + if (representation == nil) { + return; } + memcpy(representation.bitmapData, data.bytes, data.length); + NSImage *image = [[NSImage alloc] initWithSize:NSMakeSize(width, height)]; + [image addRepresentation:representation]; + NSApp.applicationIconImage = image; } }); } diff --git a/renderer/desktop/c/win32_window.c b/renderer/desktop/c/win32_window.c index da3cf474f..d42c546f9 100644 --- a/renderer/desktop/c/win32_window.c +++ b/renderer/desktop/c/win32_window.c @@ -1468,9 +1468,10 @@ void dxc_native_window_caption(void *view_pointer, float *height, float *buttons *buttons_width = 0; } -void dxc_native_set_icon(const uint8_t *bytes, int32_t length) { - (void)bytes; - (void)length; +void dxc_native_set_icon(const uint8_t *rgba, int32_t width, int32_t height) { + (void)rgba; + (void)width; + (void)height; } int32_t dxc_native_dropped_paths(char *out, int32_t capacity) { diff --git a/renderer/desktop/c/x11_window.c b/renderer/desktop/c/x11_window.c index 46c8cc6a9..86fdb08d9 100644 --- a/renderer/desktop/c/x11_window.c +++ b/renderer/desktop/c/x11_window.c @@ -380,9 +380,10 @@ void dxc_native_window_caption(void *view_pointer, float *height, float *buttons *buttons_width = 0; } -void dxc_native_set_icon(const uint8_t *bytes, int32_t length) { - (void)bytes; - (void)length; +void dxc_native_set_icon(const uint8_t *rgba, int32_t width, int32_t height) { + (void)rgba; + (void)width; + (void)height; } int32_t dxc_native_dropped_paths(char *out, int32_t capacity) { diff --git a/renderer/desktop/src/AppKitWindow.kt b/renderer/desktop/src/AppKitWindow.kt index b36101aae..e0d8f8875 100644 --- a/renderer/desktop/src/AppKitWindow.kt +++ b/renderer/desktop/src/AppKitWindow.kt @@ -111,7 +111,7 @@ private external fun configureWindow( private external fun windowCaption(view: Pointer?, height: CFloatPointer?, buttonsWidth: CFloatPointer?) @CFunction("dxc_native_set_icon") -private external fun setIcon(bytes: CCharPointer?, length: Int) +private external fun setIcon(rgba: CCharPointer?, width: Int, height: Int) @CFunction("dxc_native_dropped_paths") private external fun droppedPaths(out: Pointer?, capacity: Int): Int @@ -299,16 +299,34 @@ internal fun readDroppedPaths(): String { private const val DROPPED_PATHS_BYTES = 64 * 1024 -/** Puts [png] on the application, which is what the Dock and the switcher show. */ -internal fun setApplicationIcon(png: ByteArray) { - val holder = CTypeConversion.toCBytes(png) +/** + * Puts a picture on the application, which is what the Dock and the switcher show. + * + * [rgba] is eight bits each of red, green, blue and alpha, the colour already multiplied by + * the alpha, row after row with no padding. + */ +internal fun setApplicationIcon(rgba: ByteArray, width: Int, height: Int) { + val holder = CTypeConversion.toCBytes(rgba) try { - setIcon(holder.get(), png.size) + setIcon(holder.get(), width, height) } finally { holder.close() } } +/** The pixels [setApplicationIcon] takes, from a picture Compose holds. */ +internal fun iconPixels(picture: androidx.compose.ui.graphics.ImageBitmap): Triple? { + val bitmap = picture.asSkiaBitmap() + val info = org.jetbrains.skia.ImageInfo( + bitmap.width, + bitmap.height, + org.jetbrains.skia.ColorType.RGBA_8888, + org.jetbrains.skia.ColorAlphaType.PREMUL, + ) + val pixels = bitmap.readPixels(info, info.minRowBytes) ?: return null + return Triple(pixels, bitmap.width, bitmap.height) +} + /** * Lets the window answer for itself for a moment. @@ -506,11 +524,16 @@ internal fun runAppKitWindow(autoExitMillis: Long? = null) { ) // The application's own tree, drawn by the same interpreter every window uses. // Nothing in it knows which window it is in, which is the point. + val clipboard = WindowClipboard() + @Suppress("DEPRECATION") + val clipboardManager = WindowClipboardManager() scene.setContent { androidx.compose.runtime.CompositionLocalProvider( dev.darkpyonix.composerust.runtime.LocalSystemDarkObserver provides { rememberSystemDark().value }, + androidx.compose.ui.platform.LocalClipboard provides clipboard, + androidx.compose.ui.platform.LocalClipboardManager provides clipboardManager, ) { dev.darkpyonix.composerust.runtime.ComposeRustContent( host, @@ -553,10 +576,9 @@ internal fun runAppKitWindow(autoExitMillis: Long? = null) { as? dev.darkpyonix.composerust.ui.node.Asset.Raster if (raster != null) { iconId = asked.icon - org.jetbrains.skia.Image.makeFromBitmap(raster.bitmap.asSkiaBitmap()) - .encodeToData(org.jetbrains.skia.EncodedImageFormat.PNG) - ?.bytes - ?.let(::setApplicationIcon) + iconPixels(raster.bitmap)?.let { (pixels, width, height) -> + setApplicationIcon(pixels, width, height) + } } } var heard = false diff --git a/renderer/desktop/src/NativeClipboard.kt b/renderer/desktop/src/NativeClipboard.kt new file mode 100644 index 000000000..b415a88b9 --- /dev/null +++ b/renderer/desktop/src/NativeClipboard.kt @@ -0,0 +1,48 @@ +@file:OptIn(androidx.compose.ui.ExperimentalComposeUiApi::class) + +package dev.darkpyonix.composerust.ui.platform + +import androidx.compose.ui.platform.Clipboard +import androidx.compose.ui.platform.ClipEntry +import androidx.compose.ui.platform.ClipboardManager +import androidx.compose.ui.platform.NativeClipboard +import androidx.compose.ui.platform.asAwtTransferable +import androidx.compose.ui.text.AnnotatedString +import java.awt.datatransfer.DataFlavor +import java.awt.datatransfer.StringSelection + +// The clipboard of a window that has no toolkit. +// +// Compose's own on desktop is the toolkit's, and a process that was told it has no display +// is answered with no clipboard at all: a copy goes nowhere and a paste finds nothing, and +// nothing says so. So the window's own pasteboard stands in, reached through the same two +// calls every window of ours shares. +// +// The entry Compose passes around is a toolkit data object on desktop. Only its text is +// read and written here, which is all a text field asks for. + +/** The text on the clipboard, or null where there is none. */ +private fun clipboardText(): String? = readClipboard().takeIf { it.isNotEmpty() } + +@Suppress("DEPRECATION") +internal class WindowClipboardManager : ClipboardManager { + override fun getText(): AnnotatedString? = clipboardText()?.let(::AnnotatedString) + + override fun setText(annotatedString: AnnotatedString) = writeClipboard(annotatedString.text) + + override fun hasText(): Boolean = clipboardText() != null +} + +internal class WindowClipboard : Clipboard { + override suspend fun getClipEntry(): ClipEntry? = + clipboardText()?.let { ClipEntry(StringSelection(it)) } + + override suspend fun setClipEntry(clipEntry: ClipEntry?) { + val text = clipEntry?.asAwtTransferable?.let { + runCatching { it.getTransferData(DataFlavor.stringFlavor) as? String }.getOrNull() + } + writeClipboard(text.orEmpty()) + } + + override val nativeClipboard: NativeClipboard get() = this +} diff --git a/renderer/desktop/test/IconPixelsTest.kt b/renderer/desktop/test/IconPixelsTest.kt new file mode 100644 index 000000000..7983b4fba --- /dev/null +++ b/renderer/desktop/test/IconPixelsTest.kt @@ -0,0 +1,31 @@ +package dev.darkpyonix.composerust.test + +import androidx.compose.ui.graphics.asComposeImageBitmap +import dev.darkpyonix.composerust.ui.platform.iconPixels +import org.jetbrains.skia.Bitmap +import org.jetbrains.skia.ImageInfo +import kotlin.test.Test +import kotlin.test.assertEquals + +/** + * The application's picture, as the pixels a window of our own is given. + * + * Every window without a toolkit takes the icon as red, green, blue and alpha bytes with + * the colour already multiplied by the alpha, so the order of the bytes is the thing to + * get right: a swapped red and blue is an icon in the wrong colours on every desktop. + */ +class IconPixelsTest { + + @Test + fun nfr14_an_icon_reaches_the_window_as_rgba_bytes() { + val bitmap = Bitmap() + bitmap.allocPixels(ImageInfo.makeN32Premul(2, 1)) + // Opaque blue. + bitmap.erase(0xFF0000FF.toInt()) + val (pixels, width, height) = iconPixels(bitmap.asComposeImageBitmap())!! + assertEquals(2, width) + assertEquals(1, height) + assertEquals(8, pixels.size) + assertEquals(listOf(0, 0, 255, 255), pixels.take(4).map { it.toInt() and 0xFF }) + } +} From 2375b0525e7c94984f5b16aed34283b95ae94cb3 Mon Sep 17 00:00:00 2001 From: BREW <81044362+b-re-w@users.noreply.github.com> Date: Sun, 4 Oct 2026 19:22:05 +0900 Subject: [PATCH 4/8] Fix: Send the first text edit to the Host at once instead of after a debounce (FR-5) TextChanged was held back 120 ms after the last keystroke and restarted by every key, so a line echoing the typed text trailed the key by the pause plus a frame or two, and by the whole burst when typing was fast. Measured on the AppKit window with real key events posted to it: 152 ms from key to the echo being on screen, and the Host heard nothing during a burst. The first edit after a quiet period is now sent immediately, later ones are collapsed to at most one per interval carrying the latest text, and the last one is always sent. Covers the FR-5 criterion that the Host is notified of edits through a debounced TextChanged: bursts still collapse. Adds a test that a request for a frame is served by the next turn of the loop. --- .../desktop/src/renderer/HostTextField.kt | 44 ++++++++++----- renderer/desktop/test/FrameWakeTest.kt | 54 +++++++++++++++++++ renderer/desktop/test/TextFieldTest.kt | 41 ++++++++++++++ 3 files changed, 127 insertions(+), 12 deletions(-) create mode 100644 renderer/desktop/test/FrameWakeTest.kt diff --git a/renderer/desktop/src/renderer/HostTextField.kt b/renderer/desktop/src/renderer/HostTextField.kt index f81b03e01..84a162580 100644 --- a/renderer/desktop/src/renderer/HostTextField.kt +++ b/renderer/desktop/src/renderer/HostTextField.kt @@ -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.Job +import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.delay -import kotlinx.coroutines.flow.collectLatest +import kotlinx.coroutines.launch import dev.darkpyonix.composerust.protocol.ColorRole import dev.darkpyonix.composerust.protocol.HostEvent import dev.darkpyonix.composerust.protocol.PropertyKind @@ -49,12 +51,13 @@ import dev.darkpyonix.composerust.ui.node.HostText import dev.darkpyonix.composerust.design.LocalGlassDepth /** - * Quiet period before a `TextChanged` notification is sent. + * The shortest time between two `TextChanged` notifications from one field. * * 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. + * not the mechanism that keeps the field's value. The first edit after a quiet period is + * sent immediately and the ones that follow are collapsed to one per interval, the last of + * them always sent. 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 a few events. */ const val TEXT_CHANGED_DEBOUNCE_MILLIS: Long = 120 @@ -104,16 +107,33 @@ 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, sent when typing starts and again when it has changed since. + // + // The first edit after a quiet period goes to the Host at once, so a line that echoes + // what was typed follows the key rather than trailing it by the length of a pause. + // Edits that keep coming are collapsed: at most one more is sent per + // [TEXT_CHANGED_DEBOUNCE_MILLIS], carrying the latest text, and the last one is always + // sent. A burst therefore costs the Host a handful of events and not one per key, and + // the field never waits for the typing to stop before the Host hears of it. 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 { + lastSent = latest + dispatcher.dispatch(HostEvent.TextChanged(nodeId, changeHandler, lastSent)) + delay(TEXT_CHANGED_DEBOUNCE_MILLIS) + } while (latest != lastSent) + } + } } } } diff --git a/renderer/desktop/test/FrameWakeTest.kt b/renderer/desktop/test/FrameWakeTest.kt new file mode 100644 index 000000000..a6427bee2 --- /dev/null +++ b/renderer/desktop/test/FrameWakeTest.kt @@ -0,0 +1,54 @@ +package dev.darkpyonix.composerust.test + +import dev.darkpyonix.composerust.ui.platform.FrameDispatcher +import dev.darkpyonix.composerust.ui.platform.FrameRequestSource +import dev.darkpyonix.composerust.ui.platform.serveFrameRequests +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlinx.coroutines.CoroutineStart +import kotlinx.coroutines.launch +import kotlinx.coroutines.runBlocking + +/** + * A request for a frame is served by the next turn of the window's loop, not by a timer. + * + * The window's loop runs what the scene has queued once per turn and draws when something + * is waiting. A request made between two turns has to be served by the turn that follows + * it, which is at most one frame away. Serving it only after the quarter second the frame + * clock is given before it is taken to be stopped is what the typed text trailed by. + */ +class FrameWakeTest { + + @Test + fun pr3_a_frame_request_is_served_by_the_next_turn_of_the_loop() { + val work = FrameDispatcher() + val requests = FrameRequestSource() + val served = mutableListOf() + var frameCallbacks = mutableListOf<(Long) -> Unit>() + runBlocking { + val job = launch(work, start = CoroutineStart.UNDISPATCHED) { + serveFrameRequests( + requests.counter, + awaitFrame = { onFrame -> + kotlinx.coroutines.suspendCancellableCoroutine { continuation -> + frameCallbacks.add { nanos -> + onFrame(nanos) + continuation.resume(Unit) {} + } + } + }, + serve = { served.add(it) }, + alreadyServed = 0, + ) + } + // One request, then the loop's turns: run what is queued, draw the frame. + requests.request() + work.runPending() + assertEquals(1, frameCallbacks.size, "the request did not reach the frame clock on the next turn") + frameCallbacks.removeAt(0)(16_000_000L) + assertEquals(listOf(16_000_000L), served, "the frame was not served by the turn that drew it") + job.cancel() + work.runPending() + } + } +} diff --git a/renderer/desktop/test/TextFieldTest.kt b/renderer/desktop/test/TextFieldTest.kt index 7d65c1ce1..708c235cb 100644 --- a/renderer/desktop/test/TextFieldTest.kt +++ b/renderer/desktop/test/TextFieldTest.kt @@ -103,6 +103,47 @@ class TextFieldTest { assertEquals("hi", changes.lastOrNull()?.text, "events were ${connection.events}") } + @Test + fun fr5_the_first_edit_reaches_the_host_without_waiting_out_the_debounce() = runComposeUiTest { + val connection = FakeHostConnection(field()) + setContent { ComposeRustContent(rememberComposeRustHost(connection)) } + mainClock.autoAdvance = false + + onNodeWithTag(nodeTestTag(FIELD)).performTextInput("h") + mainClock.advanceTimeByFrame() + mainClock.advanceTimeByFrame() + + // One frame or two, not the 120 ms the old trailing debounce held it back for. + val changes = connection.events.filterIsInstance() + assertEquals(listOf("h"), changes.map { it.text }, "events were ${connection.events}") + } + + @Test + fun fr5_a_burst_of_edits_is_collapsed_and_the_last_text_is_sent() = runComposeUiTest { + val connection = FakeHostConnection(field()) + setContent { ComposeRustContent(rememberComposeRustHost(connection)) } + mainClock.autoAdvance = false + + onNodeWithTag(nodeTestTag(FIELD)).performTextInput("a") + mainClock.advanceTimeByFrame() + onNodeWithTag(nodeTestTag(FIELD)).performTextInput("b") + mainClock.advanceTimeByFrame() + onNodeWithTag(nodeTestTag(FIELD)).performTextInput("c") + mainClock.advanceTimeByFrame() + // Inside the interval: the first went out, the others are waiting. + assertEquals( + listOf("a"), + connection.events.filterIsInstance().map { it.text }, + ) + + mainClock.advanceTimeBy(TEXT_CHANGED_DEBOUNCE_MILLIS * 2) + assertEquals( + listOf("a", "abc"), + connection.events.filterIsInstance().map { it.text }, + "events were ${connection.events}", + ) + } + @Test fun fr5_host_set_text_replaces_the_value_when_no_composition_is_active() = runComposeUiTest { val connection = FakeHostConnection(field()) From 4183ef2406c17df3e80247d838b59d2ba45a84c3 Mon Sep 17 00:00:00 2001 From: BREW <81044362+b-re-w@users.noreply.github.com> Date: Sun, 4 Oct 2026 19:22:05 +0900 Subject: [PATCH 5/8] Fix: Draw inside a live resize of the AppKit window so the picture is not stretched (NFR-14) AppKit stays in its own tracking loop while an edge is dragged and only calls the view back, so the renderer's frame loop did not run: the layer's drawable size changed and the last frame was shown scaled to it. Measured with a scripted resize of 120 sizes: 0 frames presented and 120 sizes shown stretched. setFrameSize now sets the drawable size and asks the renderer for a frame at that size through the draw callback the Windows window already uses, a live resize presents with the window's transaction, and the layer anchors a stale drawable to its corner instead of scaling it. Same scripted resize after: 120 presented, 0 stale, 0 stretched, 6.1 ms per size on average. Also adds the aids used to measure it: DXC_REPORT_LATENCY timestamps from key to pixels, DXC_REPORT_RESIZE counts, DXC_SYNTH=type,keys,resize to drive the window from inside, and DXC_LEGACY_RESIZE to see the old behaviour. The counting is in appkit_resize.h and is tested without a window by scripts/tests/appkit-live-resize.test.sh. --- renderer/desktop/c/appkit_resize.h | 58 +++++ renderer/desktop/c/appkit_window.m | 181 ++++++++++++++-- renderer/desktop/c/win32_window.c | 16 ++ renderer/desktop/c/x11_window.c | 16 ++ renderer/desktop/src/AppKitWindow.kt | 198 +++++++++++++----- renderer/desktop/src/LatencyTrace.kt | 43 ++++ renderer/desktop/src/RendererEntryPoints.kt | 1 + renderer/desktop/src/SyntheticInput.kt | 91 ++++++++ .../src/renderer/NativeHostConnection.kt | 4 + scripts/tests/appkit-live-resize.test.sh | 116 ++++++++++ 10 files changed, 658 insertions(+), 66 deletions(-) create mode 100644 renderer/desktop/c/appkit_resize.h create mode 100644 renderer/desktop/src/LatencyTrace.kt create mode 100644 renderer/desktop/src/SyntheticInput.kt create mode 100755 scripts/tests/appkit-live-resize.test.sh diff --git a/renderer/desktop/c/appkit_resize.h b/renderer/desktop/c/appkit_resize.h new file mode 100644 index 000000000..1e264d15a --- /dev/null +++ b/renderer/desktop/c/appkit_resize.h @@ -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 +#include + +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 diff --git a/renderer/desktop/c/appkit_window.m b/renderer/desktop/c/appkit_window.m index df67989ae..d10f238d2 100644 --- a/renderer/desktop/c/appkit_window.m +++ b/renderer/desktop/c/appkit_window.m @@ -16,6 +16,7 @@ #import #import +#import #import #import #include @@ -24,6 +25,9 @@ #include #include #include +#include "appkit_resize.h" +#include +#include // What happened in the window, waiting to be read. // @@ -304,19 +308,60 @@ void dxc_native_install_menu(const char *application_name) { }); } -/** - * The way back into the renderer that this window has no use for. - * - * Named because one piece of Kotlin drives both desktops and the symbol has to resolve on - * each. The Windows window needs it: Windows runs a loop of its own while an edge is - * being dragged, and the renderer has to be asked for a frame from inside that loop or - * nothing is drawn for the length of the drag. AppKit hands a live resize back to the - * same run loop the frame loop is already pumping, so there is nothing here to ask from. - * Nothing on this platform registers, and so nothing here is ever called. - */ +// The way back into the renderer, for the one moment the run loop cannot be pumped. +// +// While an edge is being dragged AppKit stays inside its own tracking loop and only calls +// the view back, so the frame loop that pumps events is not running. The window asks the +// renderer for a frame from inside that callback instead, and a window that waited for +// the loop would show the last frame stretched to the new size for the whole drag. +typedef void (*dxc_draw_frame_fn)(void *); +static dxc_draw_frame_fn dxc_draw_frame; +static void *dxc_draw_thread; + void dxc_native_set_draw_callback(void (*callback)(void *), void *isolate_thread) { - (void)callback; - (void)isolate_thread; + dxc_draw_frame = callback; + dxc_draw_thread = isolate_thread; +} + +// What a resize cost and what it showed, kept for the report asked for with +// DXC_REPORT_RESIZE. A stretched step is one the reader saw as the old picture scaled. +static struct dxc_resize_stats dxc_resize_stats; + +static int dxc_legacy_resize(void) { + static int cached = -1; + if (cached < 0) cached = getenv("DXC_LEGACY_RESIZE") != NULL ? 1 : 0; + return cached; +} + +static int dxc_report_resize(void) { + static int cached = -1; + if (cached < 0) { + cached = getenv("DXC_REPORT_RESIZE") != NULL ? 1 : 0; + } + return cached; +} + +static double dxc_ticks_to_millis(int64_t ticks) { + static mach_timebase_info_data_t base; + if (base.denom == 0) { + mach_timebase_info(&base); + } + return (double)ticks * base.numer / base.denom / 1.0e6; +} + +static void dxc_native_resize_report(void) { + if (!dxc_report_resize()) { + return; + } + int64_t stretched = dxc_resize_stretched(&dxc_resize_stats); + double average = dxc_resize_stats.steps > 0 + ? dxc_ticks_to_millis(dxc_resize_stats.callback_ticks) / dxc_resize_stats.steps : 0; + fprintf(stderr, + "dxc resize: steps=%lld presented=%lld stale=%lld stretched=%lld " + "per-step=%.2fms max=%.2fms\n", + (long long)dxc_resize_stats.steps, (long long)dxc_resize_stats.presented, + (long long)dxc_resize_stats.stale, (long long)stretched, average, + dxc_ticks_to_millis(dxc_resize_stats.callback_max_ticks)); } /** @@ -692,6 +737,37 @@ - (void)setFrameSize:(NSSize)size { record.x = (float)(size.width * scale); record.y = (float)(size.height * scale); dxc_push_event(record); + + dxc_resize_step(&dxc_resize_stats, (int32_t)record.x, (int32_t)record.y); + + // Drawn now, at the size the view just became. The frame loop is not running while + // an edge is dragged, so waiting for it leaves the layer showing the last frame + // scaled to fit. Skipped before the renderer has registered, which covers the sizes + // the window takes while it is being made. + if (dxc_draw_frame != NULL && !dxc_legacy_resize()) { + uint64_t begun = mach_absolute_time(); + dxc_draw_frame(dxc_draw_thread); + int64_t spent = (int64_t)(mach_absolute_time() - begun); + dxc_resize_stats.callback_ticks += spent; + if (spent > dxc_resize_stats.callback_max_ticks) { + dxc_resize_stats.callback_max_ticks = spent; + } + } +} + +// A drag of an edge is a run of frames that must reach the screen in step with the +// window's own geometry. The layer presents inside the transaction that moves the window +// for as long as the drag lasts, so a frame and the edge it was drawn for appear together +// rather than the edge arriving first and the picture a frame later. +- (void)viewWillStartLiveResize { + [super viewWillStartLiveResize]; + ((CAMetalLayer *)self.layer).presentsWithTransaction = !dxc_legacy_resize(); +} + +- (void)viewDidEndLiveResize { + [super viewDidEndLiveResize]; + ((CAMetalLayer *)self.layer).presentsWithTransaction = NO; + dxc_native_resize_report(); } - (void)updateTrackingAreas { @@ -838,6 +914,10 @@ int32_t dxc_native_window_open( layer.pixelFormat = MTLPixelFormatBGRA8Unorm; // Skia reads the texture back in places, and a framebuffer-only one cannot be. layer.framebufferOnly = NO; + // Where the layer and its drawable disagree about size, anchored to the corner + // and cropped rather than scaled: a stale frame then looks like an old picture + // that has not caught up, not like a stretched one. + if (!dxc_legacy_resize()) layer.contentsGravity = kCAGravityTopLeft; // Drawn at the density of the screen the window is on rather than in points, so // text is as sharp as the display can draw it. layer.contentsScale = window.backingScaleFactor; @@ -1038,11 +1118,82 @@ void dxc_native_frame_end(void *queue_pointer) { if (dxc_pending_drawable == nil) { return; } + id drawable = dxc_pending_drawable; + dxc_pending_drawable = nil; + dxc_resize_present(&dxc_resize_stats, (int32_t)drawable.texture.width, + (int32_t)drawable.texture.height); id queue = (__bridge id)queue_pointer; id buffer = [queue commandBuffer]; - [buffer presentDrawable:dxc_pending_drawable]; - [buffer commit]; - dxc_pending_drawable = nil; + if (drawable.layer.presentsWithTransaction) { + // Presented by the layer from inside the window's transaction, once the + // work is scheduled: the contract of a layer that presents with one. + [buffer commit]; + [buffer waitUntilScheduled]; + [drawable present]; + } else { + [buffer presentDrawable:drawable]; + [buffer commit]; + } + } + }); +} + +/** + * Resizes the window from the inside, the way a drag would, for measuring. + * + * A mouse drag cannot be scripted without the accessibility permission, so the sizes a + * drag passes through are taken here, one after another, with the thread held inside this + * call as it is held inside AppKit's tracking loop: nothing but the view's own callback + * runs between two sizes. Used only when asked for, by DXC_SYNTH_RESIZE. + */ +void dxc_native_debug_resize(void *window_pointer, void *view_pointer, int32_t from_width, + int32_t from_height, int32_t to_width, int32_t to_height, + int32_t steps, int32_t pause_micros) { + dxc_on_main(^{ + @autoreleasepool { + NSWindow *window = (__bridge NSWindow *)window_pointer; + NSView *view = (__bridge NSView *)view_pointer; + dxc_resize_reset(&dxc_resize_stats); + [view viewWillStartLiveResize]; + for (int32_t step = 1; step <= steps; step++) { + double t = (double)step / steps; + NSSize size = NSMakeSize(from_width + (to_width - from_width) * t, + from_height + (to_height - from_height) * t); + [window setContentSize:size]; + [CATransaction flush]; + usleep((useconds_t)pause_micros); + } + [view viewDidEndLiveResize]; + } + }); +} + +/** + * Posts a key press and its release to the window, as the keyboard would, for measuring. + * + * The events go through the application's own queue and so reach the view, the input + * method and the renderer by the road a real key takes. Used only when asked for, by + * DXC_SYNTH. + */ +void dxc_native_debug_key(void *window_pointer, int32_t key_code, const char *characters) { + dxc_on_main(^{ + @autoreleasepool { + NSWindow *window = (__bridge NSWindow *)window_pointer; + NSString *text = [NSString stringWithUTF8String:characters]; + NSTimeInterval now = NSProcessInfo.processInfo.systemUptime; + for (int up = 0; up < 2; up++) { + NSEvent *event = [NSEvent keyEventWithType:up ? NSEventTypeKeyUp : NSEventTypeKeyDown + location:NSZeroPoint + modifierFlags:0 + timestamp:now + windowNumber:window.windowNumber + context:nil + characters:text + charactersIgnoringModifiers:text + isARepeat:NO + keyCode:(unsigned short)key_code]; + [NSApp postEvent:event atStart:NO]; + } } }); } diff --git a/renderer/desktop/c/win32_window.c b/renderer/desktop/c/win32_window.c index d42c546f9..abcf7bc6e 100644 --- a/renderer/desktop/c/win32_window.c +++ b/renderer/desktop/c/win32_window.c @@ -1424,6 +1424,22 @@ void dxc_native_install_menu(const char *application_name) { (void)application_name; } +/* + * Measuring aids of the AppKit window, named because the Kotlin that drives every desktop + * names them and answered here by doing nothing: a drag cannot be scripted from inside + * this window, and nothing asks for it to be. + */ +void dxc_native_debug_resize(void *window_pointer, void *view_pointer, int32_t from_width, + int32_t from_height, int32_t to_width, int32_t to_height, + int32_t steps, int32_t pause_micros) { + (void)window_pointer; (void)view_pointer; (void)from_width; (void)from_height; + (void)to_width; (void)to_height; (void)steps; (void)pause_micros; +} + +void dxc_native_debug_key(void *window_pointer, int32_t key_code, const char *characters) { + (void)window_pointer; (void)key_code; (void)characters; +} + /* * The names below are answered here and do nothing. * diff --git a/renderer/desktop/c/x11_window.c b/renderer/desktop/c/x11_window.c index 86fdb08d9..6324ef9c2 100644 --- a/renderer/desktop/c/x11_window.c +++ b/renderer/desktop/c/x11_window.c @@ -355,6 +355,22 @@ void dxc_native_install_menu(const char *application_name) { (void)application_name; } +/* + * Measuring aids of the AppKit window, named because the Kotlin that drives every desktop + * names them and answered here by doing nothing: a drag cannot be scripted from inside + * this window, and nothing asks for it to be. + */ +void dxc_native_debug_resize(void *window_pointer, void *view_pointer, int32_t from_width, + int32_t from_height, int32_t to_width, int32_t to_height, + int32_t steps, int32_t pause_micros) { + (void)window_pointer; (void)view_pointer; (void)from_width; (void)from_height; + (void)to_width; (void)to_height; (void)steps; (void)pause_micros; +} + +void dxc_native_debug_key(void *window_pointer, int32_t key_code, const char *characters) { + (void)window_pointer; (void)key_code; (void)characters; +} + /* * Asked for by name by the window code every desktop shares, and answered here by saying * nothing: this window takes its options another way, has no strip of the system's to diff --git a/renderer/desktop/src/AppKitWindow.kt b/renderer/desktop/src/AppKitWindow.kt index e0d8f8875..f06eace8f 100644 --- a/renderer/desktop/src/AppKitWindow.kt +++ b/renderer/desktop/src/AppKitWindow.kt @@ -37,6 +37,9 @@ import androidx.compose.ui.scene.ComposeScene import androidx.compose.ui.text.TextStyle import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp +import org.graalvm.nativeimage.CurrentIsolate +import org.graalvm.nativeimage.IsolateThread +import org.graalvm.nativeimage.c.function.CFunctionPointer import org.graalvm.word.Pointer import org.graalvm.word.WordFactory @@ -89,6 +92,24 @@ private external fun setCursorShape(shape: Int) @CFunction("dxc_native_pump") private external fun pumpEvents(seconds: Double) +@CFunction("dxc_native_set_draw_callback") +private external fun setAppKitDrawCallback(callback: CFunctionPointer?, isolateThread: IsolateThread?) + +@CFunction("dxc_native_debug_resize") +private external fun debugResize( + window: Pointer?, + view: Pointer?, + fromWidth: Int, + fromHeight: Int, + toWidth: Int, + toHeight: Int, + steps: Int, + pauseMicros: Int, +) + +@CFunction("dxc_native_debug_key") +private external fun debugKey(window: Pointer?, keyCode: Int, characters: CCharPointer?) + @CFunction("dxc_native_clipboard_read") private external fun clipboardRead(out: Pointer?, capacity: Int): Int @@ -163,6 +184,24 @@ class NativeWindow internal constructor( /** Puts the painted frame on the screen. */ fun endFrame() = endFrame(WordFactory.pointer(queue)) + /** Posts a key press for [character] to the window. See `DXC_SYNTH`. */ + internal fun postKey(keyCode: Int, character: String) { + val holder = CTypeConversion.toCString(character) + try { + debugKey(WordFactory.pointer(window), keyCode, holder.get()) + } finally { + holder.close() + } + } + + /** Takes the window through the sizes a drag would, for measuring. See `DXC_SYNTH_RESIZE`. */ + internal fun scriptedResize(from: Pair, to: Pair, steps: Int, pauseMicros: Int) { + debugResize( + WordFactory.pointer(window), WordFactory.pointer(view), + from.first, from.second, to.first, to.second, steps, pauseMicros, + ) + } + } /** @@ -505,10 +544,12 @@ internal fun runAppKitWindow(autoExitMillis: Long? = null) { // full screen removes the bar and leaving it brings it back. val caption = androidx.compose.runtime.mutableStateOf(window.measureCaption()) val textInput = NativeTextInput() + val synthetic = System.getenv("DXC_SYNTH")?.let { SyntheticInput(it) } val semantics = NativeSemantics { elements -> if (report) { System.err.println("compose-rust: the window has ${elements.size} things to say") } + synthetic?.noteElements(elements) window.describeTo(elements) } // Kept rather than left to the scene. What a scene picks for itself is the toolkit's @@ -546,74 +587,111 @@ internal fun runAppKitWindow(autoExitMillis: Long? = null) { val started = System.nanoTime() var iconId = 0 + var painted = false + var frame = 0 + var drew = false + val legacyResize = System.getenv("DXC_LEGACY_RESIZE") != null + + // What one frame is, wherever the ask comes from. The loop below is one caller and the + // window's own resize is the other: AppKit stays inside a tracking loop for as long as + // an edge is dragged, so the loop is not running then and the view asks for the frame + // itself, at the size it has just become. Both go through the one door, which refuses + // a second ask while one is being drawn, because a scene rendered from inside its own + // render is not something Compose survives. + Win32Frames.paint = { + frame++ + // The scene's own work first, before anything is read from it: a list that asked + // for rows on the last frame wants them in hand before this one is measured. + work.runPending() + // The application's picture, once the asset it named has arrived. The id is + // known from the first batch and the bitmap a little later, so this asks each + // frame until it is there and then stops. + if (iconId == 0 && asked != null && asked.icon != 0) { + val raster = host.table.assets.asset(asked.icon) + as? dev.darkpyonix.composerust.ui.node.Asset.Raster + if (raster != null) { + iconId = asked.icon + iconPixels(raster.bitmap)?.let { (pixels, width, height) -> + setApplicationIcon(pixels, width, height) + } + } + } + var heard = false + val events = drainWindowEvents() + for (event in synthetic?.due(System.nanoTime(), size) ?: emptyList()) { + scene.receive(event) + textInput.receive(event) + LatencyTrace.mark("synthetic ${event.kind} sent") + } + for (event in events) { + if (report && event.kind != WindowEvent.POINTER_MOVE) { + System.err.println("compose-rust: window heard $event") + } + if (event.kind == WindowEvent.FILES_ENTERED || + event.kind == WindowEvent.FILES_DROPPED || + event.kind == WindowEvent.FILES_EXITED + ) { + routeFileDrop(event.kind, androidx.compose.ui.geometry.Offset(event.x, event.y)) { + readDroppedPaths() + } + } + if (event.kind == WindowEvent.RESIZE) { + size = androidx.compose.ui.unit.IntSize(event.x.toInt(), event.y.toInt()) + scene.size = size + caption.value = window.measureCaption() + } + if (LatencyTrace.enabled && event.kind != WindowEvent.POINTER_MOVE) { + LatencyTrace.mark("window heard ${event.kind}") + } + scene.receive(event) + textInput.receive(event) + heard = true + } + // Only when there is something to draw. Every frame reaches the window by asking + // the main thread for a drawable and waiting for it, and the main thread is where + // AppKit answers everything else: sixty of those a second left the input method + // unable to reach this process at all, which showed up as every letter being + // committed on its own instead of composing. + if (!painted || heard || scene.hasInvalidations()) { + val begun = System.nanoTime() + LatencyTrace.mark("draw begin (heard=$heard invalidated=${scene.hasInvalidations()})") + drawFrame(window, context, scene, frame.toLong() * FRAME_NANOS, size, backdrop) + LatencyTrace.mark("draw end") + LatencyTrace.frameDrawn(System.nanoTime() - begun) + painted = true + drew = true + } + } + if (!legacyResize) registerAppKitFrameCallback() + try { // A plain loop rather than a clock. Pacing is the frame clock's work and comes // later; what this has to show is that what the window hears reaches the scene // and changes what the next frame draws. - var painted = false - var frame = 0 while (!isWindowClosed()) { if (autoExitMillis != null && (System.nanoTime() - started) / NANOS_PER_MILLI >= autoExitMillis ) { break } - frame++ + // Cleared before the window is given its turn, because a resize inside it + // draws its own frames and the semantics below must hear of them. + drew = false // The window's own turn, before anything is read from it. This thread is the // one AppKit delivers on, so the events of this frame arrive here or not at // all. Waiting the frame's length rather than sleeping afterwards, because a // window with nothing happening should rest rather than spin. pumpWindowEvents(FRAME_SECONDS) - // Before the events and before the drawing. What is waiting here is the - // scene's own work, and a list that asked for rows on the last frame wants - // them in hand before this one is measured. - work.runPending() - // The application's picture, once the asset it named has arrived. The id is - // known from the first batch and the bitmap a little later, so this asks each - // frame until it is there and then stops. - if (iconId == 0 && asked != null && asked.icon != 0) { - val raster = host.table.assets.asset(asked.icon) - as? dev.darkpyonix.composerust.ui.node.Asset.Raster - if (raster != null) { - iconId = asked.icon - iconPixels(raster.bitmap)?.let { (pixels, width, height) -> - setApplicationIcon(pixels, width, height) - } - } - } - var heard = false - var drew = false - for (event in drainWindowEvents()) { - if (report && event.kind != WindowEvent.POINTER_MOVE) { - System.err.println("compose-rust: window heard $event") - } - if (event.kind == WindowEvent.FILES_ENTERED || - event.kind == WindowEvent.FILES_DROPPED || - event.kind == WindowEvent.FILES_EXITED - ) { - routeFileDrop(event.kind, androidx.compose.ui.geometry.Offset(event.x, event.y)) { - readDroppedPaths() - } - } - if (event.kind == WindowEvent.RESIZE) { - size = androidx.compose.ui.unit.IntSize(event.x.toInt(), event.y.toInt()) - scene.size = size - caption.value = window.measureCaption() - } - scene.receive(event) - textInput.receive(event) - heard = true + LatencyTrace.mark("loop turn $frame") + if (synthetic != null && synthetic.resizeDue(System.nanoTime())) { + window.scriptedResize(size.width / measured.scale.toInt() to size.height / measured.scale.toInt(), + 360 to 420, 60, 8_000) } - // Only when there is something to draw. Every frame reaches the window by - // asking the main thread for a drawable and waiting for it, and the main - // thread is where AppKit answers everything else: sixty of those a second - // left the input method unable to reach this process at all, which showed up - // as every letter being committed on its own instead of composing. - if (!painted || heard || scene.hasInvalidations()) { - drawFrame(window, context, scene, frame.toLong() * FRAME_NANOS, size, backdrop) - painted = true - drew = true + synthetic?.keysDue(System.nanoTime())?.let { (code, character) -> + LatencyTrace.mark("synthetic key '$character' posted") + window.postKey(code, character) } + Win32Frames.draw() // Every frame, and after the drawing. After, because that is when what is in // the window has been placed and can say where it is. Every frame, because a // tree that changed on the last one is a tree nobody has been told about, and @@ -621,13 +699,31 @@ internal fun runAppKitWindow(autoExitMillis: Long? = null) { // Costs a comparison when nothing has changed, which is almost always. semantics.pushIfChanged(afterDrawing = drew) } + LatencyTrace.summary() } finally { + Win32Frames.paint = null + forgetAppKitFrameCallback() scene.close() context.close() host.shutdown() } } +/** + * Gives the window an address to ask for a frame at, and takes it away again. + * + * The thread goes with the address because an entry point of this image cannot be called + * without being told which thread of which isolate is calling. Written out at the call + * because native-image accepts a word value in straight-line code inside one method only. + */ +private fun registerAppKitFrameCallback() = + setAppKitDrawCallback(Win32DrawCallback.POINTER.functionPointer, CurrentIsolate.getCurrentThread()) + +private fun forgetAppKitFrameCallback() = setAppKitDrawCallback( + WordFactory.nullPointer(), + WordFactory.nullPointer(), +) + private const val NANOS_PER_MILLI = 1_000_000L /** One frame: take a drawable, let the scene paint it, give it to the screen. */ diff --git a/renderer/desktop/src/LatencyTrace.kt b/renderer/desktop/src/LatencyTrace.kt new file mode 100644 index 000000000..2e692e586 --- /dev/null +++ b/renderer/desktop/src/LatencyTrace.kt @@ -0,0 +1,43 @@ +package dev.darkpyonix.composerust.ui.platform + +/** + * Timestamps along the path from a key press to the pixels, on request. + * + * Off unless `DXC_REPORT_LATENCY` is set, because it prints a line per step. Each line + * carries milliseconds on the monotonic clock, so two lines can be subtracted, and the + * label says which step was reached: the window heard the key, the field changed, the Host + * was called, it asked for a frame, the frame was rendered, the batch applied, the pixels + * drawn. + */ +internal object LatencyTrace { + val enabled: Boolean = System.getenv("DXC_REPORT_LATENCY") != null + + fun mark(label: String) { + if (enabled) { + val millis = System.nanoTime() / 1_000.0 / 1_000.0 + System.err.println("compose-rust: latency t=${"%.3f".format(millis)} $label") + } + } + + private var frames = 0 + private var totalNanos = 0L + private var worstNanos = 0L + + /** Counts one drawn frame and how long it took, for [summary]. */ + fun frameDrawn(nanos: Long) { + if (!enabled) return + frames++ + totalNanos += nanos + if (nanos > worstNanos) worstNanos = nanos + } + + /** One line at the end of a run: how many frames were drawn and what each cost. */ + fun summary() { + if (enabled && frames > 0) { + System.err.println( + "compose-rust: latency frames=$frames average=${"%.2f".format(totalNanos / frames / 1.0e6)}ms " + + "worst=${"%.2f".format(worstNanos / 1.0e6)}ms", + ) + } + } +} diff --git a/renderer/desktop/src/RendererEntryPoints.kt b/renderer/desktop/src/RendererEntryPoints.kt index 3d8017305..fe99768ba 100644 --- a/renderer/desktop/src/RendererEntryPoints.kt +++ b/renderer/desktop/src/RendererEntryPoints.kt @@ -102,6 +102,7 @@ fun rendererRun(thread: IsolateThread?, libraryDir: CCharPointer?): Int = @CEntryPoint(name = "compose_rust_renderer_request_frame_impl") fun rendererRequestFrame(thread: IsolateThread?) { + dev.darkpyonix.composerust.ui.platform.LatencyTrace.mark("request_frame") FrameRequests.request() } diff --git a/renderer/desktop/src/SyntheticInput.kt b/renderer/desktop/src/SyntheticInput.kt new file mode 100644 index 000000000..493bf017c --- /dev/null +++ b/renderer/desktop/src/SyntheticInput.kt @@ -0,0 +1,91 @@ +package dev.darkpyonix.composerust.ui.platform + +import androidx.compose.ui.unit.IntSize + +/** + * Input and resizing made up from inside the window, for measuring without a hand. + * + * A session with no accessibility permission cannot post a click, type a letter or drag + * an edge, and the two things worth timing here are exactly those: how long a typed letter + * takes to show, and what a drag does to the picture. Asked for with `DXC_SYNTH`, a + * comma separated list of `type` and `resize`; unset, nothing here runs. + * + * `type` presses on the first text field the window describes, then commits one letter + * every 700 ms from two seconds in. `resize` takes the window from its size to 360x420 in + * 60 steps, six seconds in, from a single call that holds the thread as a drag does. + */ +internal class SyntheticInput(what: String) { + private val wantsType = "type" in what.split(',') + private val wantsResize = "resize" in what.split(',') + private val started = System.nanoTime() + private var field: AccessibleElement? = null + private var clicked = false + private var letters = 0 + private var resized = false + private var lastTyped = "" + + // Real key presses posted to the window's own queue, so the input method and the key + // path are in the measurement. `DXC_SYNTH_KEYS` names the letters. + private val keys = if ("keys" in what.split(',')) { + (System.getenv("DXC_SYNTH_KEYS") ?: "abc").toList() + } else { + emptyList() + } + private var keysSent = 0 + + /** The next key to post, as a key code and the letter it types, once it is time. */ + fun keysDue(now: Long): Pair? { + if (keys.isEmpty() || !clicked || keysSent >= keys.size) return null + if (seconds(now) < 2.0 + 0.7 * keysSent) return null + val letter = keys[keysSent++] + return (ANSI_KEY_CODES[letter] ?: 0) to letter.toString() + } + + private fun seconds(now: Long) = (now - started) / 1.0e9 + + /** What the window should hear by now and has not yet. */ + fun due(now: Long, @Suppress("UNUSED_PARAMETER") size: IntSize): List { + if (!wantsType && keys.isEmpty()) return emptyList() + val at = seconds(now) + val found = ArrayList() + val target = field + if (!clicked && at >= 1.0 && target != null) { + clicked = true + val x = target.x + target.width / 2 + val y = target.y + target.height / 2 + found += WindowEvent(WindowEvent.POINTER_DOWN, x, y, 1, 0, 0, 0, "") + found += WindowEvent(WindowEvent.POINTER_UP, x, y, 0, 0, 0, 0, "") + } + if (wantsType && clicked && letters < 5 && at >= 2.0 + 0.7 * letters) { + val letter = ('a' + letters).toString() + letters++ + found += WindowEvent(WindowEvent.TEXT_COMMIT, 0f, 0f, 0, 0, 0, 0, letter) + } + return found + } + + /** True once, when it is time to take the window through its sizes. */ + fun resizeDue(now: Long): Boolean { + if (!wantsResize || resized || seconds(now) < 6.0) return false + resized = true + return true + } + + /** Remembers where the field is, and says when the echo line has caught up. */ + fun noteElements(elements: List) { + if (field == null) field = elements.firstOrNull { it.role == ElementRole.FIELD } + val echo = elements.firstOrNull { it.label.startsWith("typed: ") }?.label ?: return + if (echo != lastTyped && echo.length > "typed: ".length) { + lastTyped = echo + LatencyTrace.mark("semantics shows '$echo'") + } + } +} + +/** Where each letter is on an ANSI keyboard, for the keys posted to the window. */ +private val ANSI_KEY_CODES = mapOf( + 'a' to 0, 's' to 1, 'd' to 2, 'f' to 3, 'h' to 4, 'g' to 5, 'z' to 6, 'x' to 7, 'c' to 8, + 'v' to 9, 'b' to 11, 'q' to 12, 'w' to 13, 'e' to 14, 'r' to 15, 'y' to 16, 't' to 17, + 'o' to 31, 'u' to 32, 'i' to 34, 'p' to 35, 'l' to 37, 'j' to 38, 'k' to 40, 'n' to 45, + 'm' to 46, +) diff --git a/renderer/desktop/src/renderer/NativeHostConnection.kt b/renderer/desktop/src/renderer/NativeHostConnection.kt index fb998aaec..38d58abd5 100644 --- a/renderer/desktop/src/renderer/NativeHostConnection.kt +++ b/renderer/desktop/src/renderer/NativeHostConnection.kt @@ -75,6 +75,7 @@ class NativeHostConnection : HostConnection { } override fun dispatchEvent(event: HostEvent, onMutation: (Mutation) -> Unit): Long { + dev.darkpyonix.composerust.ui.platform.LatencyTrace.mark("dispatch_event in") eventBuffer.clear() val length = Protocol.encodeEvent(event, eventBuffer) var result = 0L @@ -99,10 +100,12 @@ class NativeHostConnection : HostConnection { } finally { pinned.close() } + dev.darkpyonix.composerust.ui.platform.LatencyTrace.mark("dispatch_event out") return result } override fun renderFrame(frameTimeNanos: Long, onMutation: (Mutation) -> Unit) { + dev.darkpyonix.composerust.ui.platform.LatencyTrace.mark("render_frame in") val batch = StackValue.get(BATCH_BYTES) checkStatus(hostRenderFrame(frameTimeNanos, batch), "render_frame") try { @@ -116,6 +119,7 @@ class NativeHostConnection : HostConnection { // Host reuses, so it is invalid the moment this call returns. hostReleaseBatch(batch) } + dev.darkpyonix.composerust.ui.platform.LatencyTrace.mark("render_frame out (batch applied)") } override fun shutdown() = hostShutdown() diff --git a/scripts/tests/appkit-live-resize.test.sh b/scripts/tests/appkit-live-resize.test.sh new file mode 100755 index 000000000..4e5d637ec --- /dev/null +++ b/scripts/tests/appkit-live-resize.test.sh @@ -0,0 +1,116 @@ +#!/usr/bin/env bash +# While the reader drags an edge of the AppKit window, AppKit stays inside a tracking loop of +# its own and only calls the view back, so the renderer's frame loop is not running. A size +# written down in that callback and left for the loop is a size nothing draws: the layer shows +# the last frame scaled to the new size for the whole drag, which is a stretched picture. +# +# The cure is to draw inside the callback, at the size the view just became, and to present +# inside the window's own transaction. Two halves are checked here, and neither needs a window. +# +# The accounting in `c/appkit_resize.h` is what the window asks of itself, and it is compiled +# with whatever C compiler is here and fed the sizes and presents a drag produces: every size +# followed by a frame at that size is a drag with nothing stretched and nothing stale; a size +# with no frame is a stretched one; a frame at the previous size is a stale one. +# +# The wiring is read out of the source: that the size callback sets the layer's drawable size +# and then draws, that a live resize presents with the transaction, and that every presented +# frame is counted against the size the view has. A run of the real window is +# `DXC_SYNTH=resize DXC_REPORT_RESIZE=1` on any application, and it must say stale=0 and +# stretched=0. +set -uo pipefail + +repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" +c_dir="$repo_root/renderer/desktop/c" +header="$c_dir/appkit_resize.h" +source_file="$c_dir/appkit_window.m" +status=0 + +fail() { echo "FAIL: $1"; status=1; } + +[[ -f "$header" && -f "$source_file" ]] || { echo "FAIL: the resize files are missing"; exit 1; } + +compiler="${CC:-cc}" +if command -v "$compiler" >/dev/null 2>&1; then + work="$(mktemp -d "$repo_root/.appkit-live-resize-test.XXXXXX")" + trap 'rm -rf "$work"' EXIT + cat > "$work/probe.c" <<'PROBE' +#include +#include "appkit_resize.h" + +int main(void) { + struct dxc_resize_stats followed, ignored, late; + + dxc_resize_reset(&followed); + for (int step = 1; step <= 60; step++) { + dxc_resize_step(&followed, 480 + step * 2, 640 + step * 2); + dxc_resize_present(&followed, 480 + step * 2, 640 + step * 2); + } + printf("followed %lld %lld %lld %lld\n", (long long)followed.steps, + (long long)followed.presented, (long long)followed.stale, + (long long)dxc_resize_stretched(&followed)); + + dxc_resize_reset(&ignored); + for (int step = 1; step <= 60; step++) { + dxc_resize_step(&ignored, 480 + step * 2, 640 + step * 2); + } + printf("ignored %lld %lld %lld %lld\n", (long long)ignored.steps, + (long long)ignored.presented, (long long)ignored.stale, + (long long)dxc_resize_stretched(&ignored)); + + dxc_resize_reset(&late); + dxc_resize_step(&late, 500, 700); + dxc_resize_present(&late, 480, 640); + printf("late %lld %lld %lld %lld\n", (long long)late.steps, (long long)late.presented, + (long long)late.stale, (long long)dxc_resize_stretched(&late)); + return 0; +} +PROBE + if ! "$compiler" -I "$c_dir" -o "$work/probe" "$work/probe.c" 2> "$work/cc.log"; then + cat "$work/cc.log" + fail "the accounting does not compile" + else + out="$("$work/probe")" + [[ "$(grep '^followed' <<< "$out")" == "followed 60 60 0 0" ]] || + fail "a drag with a frame at every size should count nothing stretched or stale: $out" + [[ "$(grep '^ignored' <<< "$out")" == "ignored 60 0 0 60" ]] || + fail "a drag with no frames should count every size stretched: $out" + [[ "$(grep '^late' <<< "$out")" == "late 1 1 1 1" ]] || + fail "a frame at the previous size should count stale and leave the size stretched: $out" + fi +else + echo "skipped the accounting: no C compiler" +fi + +# What a message does, between its definition and the closing brace at the margin. +body() { + awk -v pattern="$1" ' + index($0, pattern) { inside = 1 } + inside { print } + inside && /^}/ { exit } + ' "$source_file" +} + +resized="$(body '- (void)setFrameSize:(NSSize)size')" +grep -q 'layer.drawableSize' <<< "$resized" || fail "setFrameSize does not set the layer's drawable size" +grep -q 'dxc_draw_frame(dxc_draw_thread)' <<< "$resized" || + fail "setFrameSize does not draw a frame at the size it just took" +before="$(sed -n '/layer.drawableSize/=' <<< "$resized" | head -1)" +after="$(sed -n '/dxc_draw_frame(dxc_draw_thread)/=' <<< "$resized" | head -1)" +[[ -n "$before" && -n "$after" && "$before" -lt "$after" ]] || + fail "setFrameSize draws before it has told the layer the new size" + +grep -q 'viewWillStartLiveResize' "$source_file" && grep -q 'presentsWithTransaction = !dxc_legacy_resize()' "$source_file" || + fail "a live resize does not present with the transaction" +grep -q 'viewDidEndLiveResize' "$source_file" && grep -q 'presentsWithTransaction = NO' "$source_file" || + fail "the end of a live resize does not go back to presenting on its own" + +ended="$(body 'void dxc_native_frame_end')" +grep -q 'waitUntilScheduled' <<< "$ended" && grep -q '\[drawable present\]' <<< "$ended" || + fail "a frame presented with the transaction is not presented after its work is scheduled" +grep -q 'dxc_resize_present' <<< "$ended" || fail "presented frames are not counted against the view's size" + +grep -q 'dxc_native_set_draw_callback' "$source_file" && ! grep -A4 'void dxc_native_set_draw_callback' "$source_file" | grep -q '(void)callback' || + fail "the draw callback is a stub, so nothing draws during a drag" + +[[ $status -eq 0 ]] && echo "the AppKit window draws inside a live resize: ok" +exit $status From 4a2eddcfa7256267d0383d222f8583f7ae8bb4cc Mon Sep 17 00:00:00 2001 From: BREW <81044362+b-re-w@users.noreply.github.com> Date: Sun, 4 Oct 2026 20:31:49 +0900 Subject: [PATCH 6/8] Chore: Restage the Android Kotlin after the renderer changed --- .../android-kotlin/shared/HostTextField.kt | 44 ++++++++++++++----- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/compose-rust/android-kotlin/shared/HostTextField.kt b/compose-rust/android-kotlin/shared/HostTextField.kt index f81b03e01..84a162580 100644 --- a/compose-rust/android-kotlin/shared/HostTextField.kt +++ b/compose-rust/android-kotlin/shared/HostTextField.kt @@ -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.Job +import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.delay -import kotlinx.coroutines.flow.collectLatest +import kotlinx.coroutines.launch import dev.darkpyonix.composerust.protocol.ColorRole import dev.darkpyonix.composerust.protocol.HostEvent import dev.darkpyonix.composerust.protocol.PropertyKind @@ -49,12 +51,13 @@ import dev.darkpyonix.composerust.ui.node.HostText import dev.darkpyonix.composerust.design.LocalGlassDepth /** - * Quiet period before a `TextChanged` notification is sent. + * The shortest time between two `TextChanged` notifications from one field. * * 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. + * not the mechanism that keeps the field's value. The first edit after a quiet period is + * sent immediately and the ones that follow are collapsed to one per interval, the last of + * them always sent. 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 a few events. */ const val TEXT_CHANGED_DEBOUNCE_MILLIS: Long = 120 @@ -104,16 +107,33 @@ 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, sent when typing starts and again when it has changed since. + // + // The first edit after a quiet period goes to the Host at once, so a line that echoes + // what was typed follows the key rather than trailing it by the length of a pause. + // Edits that keep coming are collapsed: at most one more is sent per + // [TEXT_CHANGED_DEBOUNCE_MILLIS], carrying the latest text, and the last one is always + // sent. A burst therefore costs the Host a handful of events and not one per key, and + // the field never waits for the typing to stop before the Host hears of it. 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 { + lastSent = latest + dispatcher.dispatch(HostEvent.TextChanged(nodeId, changeHandler, lastSent)) + delay(TEXT_CHANGED_DEBOUNCE_MILLIS) + } while (latest != lastSent) + } + } } } } From b45d7669502727e45836e0753f6e1f61a20b9e0d Mon Sep 17 00:00:00 2001 From: BREW <81044362+b-re-w@users.noreply.github.com> Date: Sun, 4 Oct 2026 21:32:00 +0900 Subject: [PATCH 7/8] Fix: Send one TextChanged a frame, with the latest text, instead of a 120 ms throttle (FR-5) Typing in a field was held back by a 120 ms interval between notifications. An edit is now told to the Host on the next frame, carrying the text the field holds then, so keys inside one frame are one event and the last text is always sent. No measured reason for 120 ms was recorded anywhere in the repository. --- .../android-kotlin/shared/HostTextField.kt | 33 ++++++------------- .../desktop/src/renderer/HostTextField.kt | 33 ++++++------------- renderer/desktop/test/TextFieldTest.kt | 32 +++++++++++------- 3 files changed, 40 insertions(+), 58 deletions(-) diff --git a/compose-rust/android-kotlin/shared/HostTextField.kt b/compose-rust/android-kotlin/shared/HostTextField.kt index 84a162580..8c084dcca 100644 --- a/compose-rust/android-kotlin/shared/HostTextField.kt +++ b/compose-rust/android-kotlin/shared/HostTextField.kt @@ -35,7 +35,7 @@ import androidx.compose.ui.text.TextRange import androidx.compose.ui.text.input.TextFieldValue import kotlinx.coroutines.Job import kotlinx.coroutines.coroutineScope -import kotlinx.coroutines.delay +import androidx.compose.runtime.withFrameNanos import kotlinx.coroutines.launch import dev.darkpyonix.composerust.protocol.ColorRole import dev.darkpyonix.composerust.protocol.HostEvent @@ -50,22 +50,11 @@ import dev.darkpyonix.composerust.ui.textStyle import dev.darkpyonix.composerust.ui.node.HostText import dev.darkpyonix.composerust.design.LocalGlassDepth -/** - * The shortest time between two `TextChanged` notifications from one field. - * - * The Host is told about edits, but not once per keystroke: `TextChanged` is a notification, - * not the mechanism that keeps the field's value. The first edit after a quiet period is - * sent immediately and the ones that follow are collapsed to one per interval, the last of - * them always sent. 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 a few events. - */ -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. * @@ -107,16 +96,14 @@ internal fun HostTextField(node: Node, modifier: Modifier, dispatcher: EventDisp } } - // Change notification, sent when typing starts and again when it has changed since. + // Change notification, at most one a frame. // - // The first edit after a quiet period goes to the Host at once, so a line that echoes - // what was typed follows the key rather than trailing it by the length of a pause. - // Edits that keep coming are collapsed: at most one more is sent per - // [TEXT_CHANGED_DEBOUNCE_MILLIS], carrying the latest text, and the last one is always - // sent. A burst therefore costs the Host a handful of events and not one per key, and - // the field never waits for the typing to stop before the Host hears of it. 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. + // 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 @@ -128,9 +115,9 @@ internal fun HostTextField(node: Node, modifier: Modifier, dispatcher: EventDisp if (text == lastSent || sending?.isActive == true) return@collect sending = launch { do { + withFrameNanos { } lastSent = latest dispatcher.dispatch(HostEvent.TextChanged(nodeId, changeHandler, lastSent)) - delay(TEXT_CHANGED_DEBOUNCE_MILLIS) } while (latest != lastSent) } } diff --git a/renderer/desktop/src/renderer/HostTextField.kt b/renderer/desktop/src/renderer/HostTextField.kt index 84a162580..8c084dcca 100644 --- a/renderer/desktop/src/renderer/HostTextField.kt +++ b/renderer/desktop/src/renderer/HostTextField.kt @@ -35,7 +35,7 @@ import androidx.compose.ui.text.TextRange import androidx.compose.ui.text.input.TextFieldValue import kotlinx.coroutines.Job import kotlinx.coroutines.coroutineScope -import kotlinx.coroutines.delay +import androidx.compose.runtime.withFrameNanos import kotlinx.coroutines.launch import dev.darkpyonix.composerust.protocol.ColorRole import dev.darkpyonix.composerust.protocol.HostEvent @@ -50,22 +50,11 @@ import dev.darkpyonix.composerust.ui.textStyle import dev.darkpyonix.composerust.ui.node.HostText import dev.darkpyonix.composerust.design.LocalGlassDepth -/** - * The shortest time between two `TextChanged` notifications from one field. - * - * The Host is told about edits, but not once per keystroke: `TextChanged` is a notification, - * not the mechanism that keeps the field's value. The first edit after a quiet period is - * sent immediately and the ones that follow are collapsed to one per interval, the last of - * them always sent. 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 a few events. - */ -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. * @@ -107,16 +96,14 @@ internal fun HostTextField(node: Node, modifier: Modifier, dispatcher: EventDisp } } - // Change notification, sent when typing starts and again when it has changed since. + // Change notification, at most one a frame. // - // The first edit after a quiet period goes to the Host at once, so a line that echoes - // what was typed follows the key rather than trailing it by the length of a pause. - // Edits that keep coming are collapsed: at most one more is sent per - // [TEXT_CHANGED_DEBOUNCE_MILLIS], carrying the latest text, and the last one is always - // sent. A burst therefore costs the Host a handful of events and not one per key, and - // the field never waits for the typing to stop before the Host hears of it. 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. + // 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 @@ -128,9 +115,9 @@ internal fun HostTextField(node: Node, modifier: Modifier, dispatcher: EventDisp if (text == lastSent || sending?.isActive == true) return@collect sending = launch { do { + withFrameNanos { } lastSent = latest dispatcher.dispatch(HostEvent.TextChanged(nodeId, changeHandler, lastSent)) - delay(TEXT_CHANGED_DEBOUNCE_MILLIS) } while (latest != lastSent) } } diff --git a/renderer/desktop/test/TextFieldTest.kt b/renderer/desktop/test/TextFieldTest.kt index 708c235cb..4005fa155 100644 --- a/renderer/desktop/test/TextFieldTest.kt +++ b/renderer/desktop/test/TextFieldTest.kt @@ -24,7 +24,6 @@ import dev.darkpyonix.composerust.runtime.rememberComposeRustHost import dev.darkpyonix.composerust.tooling.FakeHostConnection import dev.darkpyonix.composerust.tooling.HostResponse import dev.darkpyonix.composerust.ui.node.nodeTestTag -import dev.darkpyonix.composerust.foundation.TEXT_CHANGED_DEBOUNCE_MILLIS private const val FIELD = 1 private const val CHANGE_HANDLER = 21L @@ -94,8 +93,6 @@ class TextFieldTest { onNodeWithTag(nodeTestTag(FIELD)).performTextInput("hi") waitForIdle() - // The debounce window has to elapse before the Host hears about the edit. - mainClock.advanceTimeBy(TEXT_CHANGED_DEBOUNCE_MILLIS * 2) waitForIdle() onNodeWithTag(nodeTestTag(FIELD)).assertTextEquals("hi") @@ -104,7 +101,7 @@ class TextFieldTest { } @Test - fun fr5_the_first_edit_reaches_the_host_without_waiting_out_the_debounce() = runComposeUiTest { + fun fr5_an_edit_reaches_the_host_on_the_next_frame() = runComposeUiTest { val connection = FakeHostConnection(field()) setContent { ComposeRustContent(rememberComposeRustHost(connection)) } mainClock.autoAdvance = false @@ -113,32 +110,43 @@ class TextFieldTest { mainClock.advanceTimeByFrame() mainClock.advanceTimeByFrame() - // One frame or two, not the 120 ms the old trailing debounce held it back for. + // One frame or two, with no timer in between. val changes = connection.events.filterIsInstance() assertEquals(listOf("h"), changes.map { it.text }, "events were ${connection.events}") } @Test - fun fr5_a_burst_of_edits_is_collapsed_and_the_last_text_is_sent() = runComposeUiTest { + fun fr5_edits_within_one_frame_are_one_event_with_the_latest_text() = runComposeUiTest { val connection = FakeHostConnection(field()) setContent { ComposeRustContent(rememberComposeRustHost(connection)) } mainClock.autoAdvance = false onNodeWithTag(nodeTestTag(FIELD)).performTextInput("a") - mainClock.advanceTimeByFrame() onNodeWithTag(nodeTestTag(FIELD)).performTextInput("b") - mainClock.advanceTimeByFrame() onNodeWithTag(nodeTestTag(FIELD)).performTextInput("c") mainClock.advanceTimeByFrame() - // Inside the interval: the first went out, the others are waiting. + mainClock.advanceTimeByFrame() assertEquals( - listOf("a"), + listOf("abc"), connection.events.filterIsInstance().map { it.text }, + "events were ${connection.events}", ) + } + + @Test + fun fr5_an_edit_after_the_last_send_is_sent_too() = runComposeUiTest { + val connection = FakeHostConnection(field()) + setContent { ComposeRustContent(rememberComposeRustHost(connection)) } + mainClock.autoAdvance = false - mainClock.advanceTimeBy(TEXT_CHANGED_DEBOUNCE_MILLIS * 2) + onNodeWithTag(nodeTestTag(FIELD)).performTextInput("a") + mainClock.advanceTimeByFrame() + mainClock.advanceTimeByFrame() + onNodeWithTag(nodeTestTag(FIELD)).performTextInput("b") + mainClock.advanceTimeByFrame() + mainClock.advanceTimeByFrame() assertEquals( - listOf("a", "abc"), + listOf("a", "ab"), connection.events.filterIsInstance().map { it.text }, "events were ${connection.events}", ) From 0a4ce6eb1bc6492080b9d96997cb96a8f2ec9c75 Mon Sep 17 00:00:00 2001 From: BREW <81044362+b-re-w@users.noreply.github.com> Date: Sun, 4 Oct 2026 22:47:44 +0900 Subject: [PATCH 8/8] Fix: Send TextChanged from inside the frame callback (FR-5) Resuming from withFrameNanos goes back through the scene's dispatcher, so the send and the recomposition after it each cost a frame. Sent in the callback, the Host's batch is applied before the same frame is composed. Measured on macOS: key to on-screen echo 26 ms to 12.7 ms. --- .../android-kotlin/shared/HostTextField.kt | 13 ++++++++++--- renderer/desktop/src/renderer/HostTextField.kt | 13 ++++++++++--- renderer/desktop/test/TextFieldTest.kt | 18 ++++++++++++++++++ 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/compose-rust/android-kotlin/shared/HostTextField.kt b/compose-rust/android-kotlin/shared/HostTextField.kt index 8c084dcca..130d763e6 100644 --- a/compose-rust/android-kotlin/shared/HostTextField.kt +++ b/compose-rust/android-kotlin/shared/HostTextField.kt @@ -115,9 +115,16 @@ internal fun HostTextField(node: Node, modifier: Modifier, dispatcher: EventDisp if (text == lastSent || sending?.isActive == true) return@collect sending = launch { do { - withFrameNanos { } - lastSent = latest - dispatcher.dispatch(HostEvent.TextChanged(nodeId, changeHandler, lastSent)) + // 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) } } diff --git a/renderer/desktop/src/renderer/HostTextField.kt b/renderer/desktop/src/renderer/HostTextField.kt index 8c084dcca..130d763e6 100644 --- a/renderer/desktop/src/renderer/HostTextField.kt +++ b/renderer/desktop/src/renderer/HostTextField.kt @@ -115,9 +115,16 @@ internal fun HostTextField(node: Node, modifier: Modifier, dispatcher: EventDisp if (text == lastSent || sending?.isActive == true) return@collect sending = launch { do { - withFrameNanos { } - lastSent = latest - dispatcher.dispatch(HostEvent.TextChanged(nodeId, changeHandler, lastSent)) + // 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) } } diff --git a/renderer/desktop/test/TextFieldTest.kt b/renderer/desktop/test/TextFieldTest.kt index 4005fa155..b894168cc 100644 --- a/renderer/desktop/test/TextFieldTest.kt +++ b/renderer/desktop/test/TextFieldTest.kt @@ -115,6 +115,24 @@ class TextFieldTest { assertEquals(listOf("h"), changes.map { it.text }, "events were ${connection.events}") } + @Test + fun fr5_the_text_changed_for_an_edit_is_dispatched_in_the_frame_that_saw_it() = runComposeUiTest { + val connection = FakeHostConnection(field()) + setContent { ComposeRustContent(rememberComposeRustHost(connection)) } + mainClock.autoAdvance = false + + onNodeWithTag(nodeTestTag(FIELD)).performTextInput("h") + mainClock.advanceTimeByFrame() + + // One frame, not the one after it: the send is made inside the frame callback, so + // the Host's reply is applied before the next frame is composed. + assertEquals( + listOf("h"), + connection.events.filterIsInstance().map { it.text }, + "events were ${connection.events}", + ) + } + @Test fun fr5_edits_within_one_frame_are_one_event_with_the_latest_text() = runComposeUiTest { val connection = FakeHostConnection(field())