diff --git a/.github/workflows/test-graalvm-renderer.yml b/.github/workflows/test-graalvm-renderer.yml new file mode 100644 index 000000000..99e07fe22 --- /dev/null +++ b/.github/workflows/test-graalvm-renderer.yml @@ -0,0 +1,199 @@ +# GraalVM renderer test: builds the GraalVM native-image renderer and smoke tests it. +# +# Kotlin/Native is the default renderer now, and the macOS and Linux native-image jobs were +# removed from test-native-renderer.yml with it. The GraalVM window code +# (renderer/desktop/c/appkit_window.m, x11_window.c and the shared C and Kotlin files that +# path uses) still lives in this repository, so this workflow keeps building and starting it. +# +# This file exists until the window code moves to the Compose fork. It then moves to the +# fork's CI and is deleted here. The Windows native-image job stays in +# test-native-renderer.yml and is not repeated here. + +name: GraalVM renderer test + +on: + pull_request: + paths: + - 'renderer/desktop/**' + - 'renderer/libs.versions.toml' + - 'renderer/kotlin' + - 'renderer/**/module.yaml' + - 'scripts/install-nik.sh' + - 'scripts/tests/renderer/**' + - '.github/workflows/test-graalvm-renderer.yml' + push: + branches: [develop] + paths: + - 'renderer/desktop/**' + - 'renderer/libs.versions.toml' + - 'renderer/kotlin' + - 'renderer/**/module.yaml' + - 'scripts/install-nik.sh' + - 'scripts/tests/renderer/**' + - '.github/workflows/test-graalvm-renderer.yml' + workflow_dispatch: + +concurrency: + group: test-graalvm-renderer-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: ${{ github.event_name == 'pull_request' }} + +permissions: + contents: read + +jobs: + macos: + name: Build and smoke test the GraalVM renderer (macOS, arm64) + runs-on: macos-latest + timeout-minutes: 90 + steps: + - uses: actions/checkout@v4 + + - name: Cache Liberica NIK + uses: actions/cache@v4 + with: + path: ~/.cache/compose-rust/nik + key: nik-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('scripts/install-nik.sh') }} + + - name: Install Liberica NIK 25 Full + run: ./scripts/install-nik.sh --github-env + + - name: Cache the Kotlin toolchain and Maven dependencies + uses: actions/cache@v4 + with: + path: | + ~/Library/Caches/JetBrains/Kotlin + ~/.m2/repository + key: kotlin-${{ runner.os }}-${{ hashFiles('renderer/kotlin', 'renderer/libs.versions.toml', 'renderer/**/module.yaml') }} + restore-keys: | + kotlin-${{ runner.os }}- + + - name: Build the renderer shared library + run: ./renderer/desktop/scripts/build-native.sh + + - name: Accessibility classes are linked in + run: ./scripts/tests/renderer/accessibility-link.test.sh + + - name: Smoke test + env: + COMPOSE_RUST_AUTOEXIT_MS: '5000' + run: ./renderer/desktop/scripts/smoke-test.sh + + - name: Report the distribution size + run: du -sh renderer/build/native-image/dist/lib + + - name: Upload the renderer for inspection + uses: actions/upload-artifact@v4 + with: + name: graalvm-renderer-macos-arm64 + path: renderer/build/native-image/dist/ + retention-days: 7 + + linux-x64: + name: Build and smoke test the GraalVM renderer (Linux, x64) + runs-on: ubuntu-24.04 + timeout-minutes: 90 + steps: + - uses: actions/checkout@v4 + + - name: Install upstream GraalVM Community for JDK 25 + uses: graalvm/setup-graalvm@v1 + with: + java-version: '25.0.1' + distribution: 'graalvm-community' + github-token: ${{ secrets.GITHUB_TOKEN }} + + - name: Install Linux AWT, Skia, font, and virtual-display dependencies + run: | + sudo apt-get update + sudo apt-get install -y build-essential zlib1g-dev unzip pkg-config fontconfig \ + libx11-dev libxext-dev libxi-dev libxrender-dev libxtst-dev libxrandr-dev \ + libfontconfig1-dev libfreetype6-dev libgl1 libgl1-mesa-dri libegl1 \ + libgl1-mesa-dev fonts-noto-cjk xvfb xauth dbus-x11 + + - name: Cache the Kotlin toolchain and Maven dependencies + uses: actions/cache@v4 + with: + path: | + ~/.cache/JetBrains/Kotlin + ~/.m2/repository + key: kotlin-linux-${{ hashFiles('renderer/kotlin', 'renderer/libs.versions.toml', 'renderer/**/module.yaml') }} + restore-keys: | + kotlin-linux- + + - name: Run Linux script contract tests + run: bash scripts/tests/renderer/linux-build.test.sh + + - name: Run the Linux metadata contract test + run: bash scripts/tests/renderer/linux-metadata.test.sh + + - name: Build the Linux renderer under Xvfb + run: xvfb-run -a ./renderer/desktop/scripts/build-native-linux.sh + + - name: Inspect staged dynamic dependencies + run: | + find renderer/build/native-image-linux/dist/lib -maxdepth 1 -type f -print | sort + ldd renderer/build/native-image-linux/dist/lib/*.so + + - name: Headless startup smoke only + env: + COMPOSE_RUST_AUTOEXIT_MS: '5000' + run: xvfb-run -a ./renderer/desktop/scripts/smoke-test-linux.sh + + - name: Upload the Linux renderer for inspection + uses: actions/upload-artifact@v4 + with: + name: graalvm-renderer-linux-x64 + path: renderer/build/native-image-linux/dist/ + retention-days: 7 + + linux-arm64: + name: Build and smoke test the GraalVM renderer (Linux, arm64) + runs-on: ubuntu-24.04-arm + timeout-minutes: 90 + steps: + - uses: actions/checkout@v4 + + - name: Install upstream GraalVM Community for JDK 25 + uses: graalvm/setup-graalvm@v1 + with: + java-version: '25.0.1' + distribution: 'graalvm-community' + github-token: ${{ secrets.GITHUB_TOKEN }} + + - name: Install Linux AWT, Skia, font, and virtual-display dependencies + run: | + sudo apt-get update + sudo apt-get install -y build-essential zlib1g-dev unzip pkg-config fontconfig \ + libx11-dev libxext-dev libxi-dev libxrender-dev libxtst-dev libxrandr-dev \ + libfontconfig1-dev libfreetype6-dev libgl1 libgl1-mesa-dri libegl1 \ + libgl1-mesa-dev fonts-noto-cjk xvfb xauth dbus-x11 + + - name: Cache the Kotlin toolchain and Maven dependencies + uses: actions/cache@v4 + with: + path: | + ~/.cache/JetBrains/Kotlin + ~/.m2/repository + key: kotlin-linux-${{ hashFiles('renderer/kotlin', 'renderer/libs.versions.toml', 'renderer/**/module.yaml') }} + restore-keys: | + kotlin-linux- + + - name: Build the Linux renderer under Xvfb + run: xvfb-run -a ./renderer/desktop/scripts/build-native-linux.sh + + - name: Inspect staged dynamic dependencies + run: | + find renderer/build/native-image-linux/dist/lib -maxdepth 1 -type f -print | sort + ldd renderer/build/native-image-linux/dist/lib/*.so + + - name: Headless startup smoke only + env: + COMPOSE_RUST_AUTOEXIT_MS: '5000' + run: xvfb-run -a ./renderer/desktop/scripts/smoke-test-linux.sh + + - name: Upload the Linux renderer for inspection + uses: actions/upload-artifact@v4 + with: + name: graalvm-renderer-linux-arm64 + path: renderer/build/native-image-linux/dist/ + retention-days: 7 diff --git a/AGENTS.md b/AGENTS.md index 5d6816922..168e01e3f 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -96,6 +96,7 @@ Workflow files and names, in `.github/workflows/`: - `publish-crates.yml`, "Publish to crates.io": the tag release, started by `workflow_run` once the renderer workflow has succeeded. - `pages.yml`, "Pages": docs. - `test-.yml`, " test": a special check for one target. +- `test-graalvm-renderer.yml`, "GraalVM renderer test": builds and smoke tests the GraalVM native-image renderer on macOS and Linux. It exists until the window code moves to the fork, then moves to the fork's CI and is deleted here. Job names are English sentence case and say briefly what the job does. Matrix jobs read " (, )". The same role keeps the same name across repos. 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/compose-rust/android-kotlin/shared/HostTextField.kt b/compose-rust/android-kotlin/shared/HostTextField.kt index f81b03e01..130d763e6 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.delay -import kotlinx.coroutines.flow.collectLatest +import kotlinx.coroutines.Job +import kotlinx.coroutines.coroutineScope +import androidx.compose.runtime.withFrameNanos +import kotlinx.coroutines.launch import dev.darkpyonix.composerust.protocol.ColorRole import dev.darkpyonix.composerust.protocol.HostEvent import dev.darkpyonix.composerust.protocol.PropertyKind @@ -48,21 +50,11 @@ import dev.darkpyonix.composerust.ui.textStyle import dev.darkpyonix.composerust.ui.node.HostText import dev.darkpyonix.composerust.design.LocalGlassDepth -/** - * Quiet period before a `TextChanged` notification is sent. - * - * The Host is told about edits, but not once per keystroke: `TextChanged` is a notification, - * not the mechanism that keeps the field's value, so it can wait for typing to pause. The - * interval is chosen here rather than negotiated; 120 ms is short enough to feel immediate - * and long enough to collapse a burst of typing into one event. - */ -const val TEXT_CHANGED_DEBOUNCE_MILLIS: Long = 120 - /** * The uncontrolled TextField. * * The editing value and the IME composition live in `remember` and never round-trip through - * the Host. The Host learns about edits through debounced `TextChanged` and through the + * the Host. The Host learns about edits through coalesced `TextChanged` and through the * commit events `TextSubmitted` and `FocusLost`; it changes the value only with `SetText`, * which is held back while a composition is in progress. * @@ -104,16 +96,38 @@ internal fun HostTextField(node: Node, modifier: Modifier, dispatcher: EventDisp } } - // Debounced change notification. The Host is told what the field now shows; it never - // sends the value back, so the composition cannot be reset by the round trip. + // Change notification, at most one a frame. + // + // An edit is told to the Host on the next frame, carrying the latest text, so keys that + // arrive within one frame cost it one event and a key is never held back by a timer: the + // line that echoes what was typed follows the key by a frame. The last text is always + // sent, because the frame that follows an edit sends whatever the field holds then, and + // a later edit starts another. The Host is told what the field now shows; it never sends + // the value back, so the composition cannot be reset by the round trip. if (changeHandler != null) { LaunchedEffect(nodeId, changeHandler) { var lastSent = value.text - snapshotFlow { value.text }.collectLatest { text -> - if (text == lastSent) return@collectLatest - delay(TEXT_CHANGED_DEBOUNCE_MILLIS) - lastSent = text - dispatcher.dispatch(HostEvent.TextChanged(nodeId, changeHandler, text)) + var latest = lastSent + var sending: Job? = null + coroutineScope { + snapshotFlow { value.text }.collect { text -> + latest = text + if (text == lastSent || sending?.isActive == true) return@collect + sending = launch { + do { + // Sent from inside the frame callback, not after it. Resuming + // from withFrameNanos goes back through the scene's dispatcher, + // which on the desktop windows runs what is waiting at the start + // of the next frame, so the send and the recomposition that + // follows it each cost a frame. In the callback the Host's batch + // is applied before this frame is composed and drawn. + withFrameNanos { + lastSent = latest + dispatcher.dispatch(HostEvent.TextChanged(nodeId, changeHandler, lastSent)) + } + } while (latest != lastSent) + } + } } } } 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 afdec41bd..f8c51aa21 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. // @@ -52,6 +56,11 @@ // 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, + // An item of the text context menu was chosen. The key_code field says which, as the + // number the menu was described with. + DXC_EVENT_MENU_COMMAND = 13, }; // Room for what an input method is composing, which is a syllable or a word and never a @@ -190,7 +199,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]; @@ -299,19 +311,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)); } /** @@ -389,6 +442,44 @@ 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; + +// The text context menu, described by the Kotlin side so that both macOS windows draw the +// same one from the same list: one line per row, a tab between the command number and its +// title, and a single dash for the line between groups. +static NSString *dxc_text_menu_spec = nil; +#define DXC_PASTE_COMMAND 3 + +void dxc_native_set_text_menu(const char *spec) { + dxc_text_menu_spec = spec == NULL ? nil : [NSString stringWithUTF8String:spec]; +} + +static NSMenu *dxc_text_menu(id target) { + if (dxc_text_menu_spec == nil) { + return nil; + } + NSMenu *menu = [[NSMenu alloc] init]; + menu.autoenablesItems = YES; + for (NSString *line in [dxc_text_menu_spec componentsSeparatedByString:@"\n"]) { + if ([line isEqualToString:@"-"]) { + [menu addItem:NSMenuItem.separatorItem]; + continue; + } + NSArray *parts = [line componentsSeparatedByString:@"\t"]; + if (parts.count != 2) { + continue; + } + NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:parts[1] + action:@selector(dxcMenuCommand:) + keyEquivalent:@""]; + item.tag = parts[0].integerValue; + item.target = target; + [menu addItem:item]; + } + return menu; +} + @interface DxcView : NSView @end @@ -454,11 +545,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) { @@ -479,8 +574,39 @@ - (void)mouseMoved:(NSEvent *)event { [self dxcSend:DXC_EVENT_POINTER_MOVE event - (void)mouseDragged:(NSEvent *)event { [self dxcSend:DXC_EVENT_POINTER_MOVE event:event]; } - (void)mouseDown:(NSEvent *)event { [self dxcSend:DXC_EVENT_POINTER_DOWN event:event]; } - (void)mouseUp:(NSEvent *)event { [self dxcSend:DXC_EVENT_POINTER_UP event:event]; } -- (void)rightMouseDown:(NSEvent *)event { [self dxcSend:DXC_EVENT_POINTER_DOWN event:event]; } -- (void)rightMouseUp:(NSEvent *)event { [self dxcSend:DXC_EVENT_POINTER_UP event:event]; } +// The press goes to the scene first, so the field under the pointer takes focus and a +// word under it can be selected the way a click would, and then the menu comes up under the +// pointer. The menu's tracking swallows the release, so it is sent once the menu has gone, +// or the scene would go on believing a button is held down. +- (void)rightMouseDown:(NSEvent *)event { + [self dxcSend:DXC_EVENT_POINTER_DOWN event:event]; + NSMenu *menu = dxc_text_menu(self); + if (menu != nil) { + [NSMenu popUpContextMenu:menu withEvent:event forView:self]; + } + [self dxcSend:DXC_EVENT_POINTER_UP event:event]; +} +- (void)rightMouseUp:(NSEvent *)event { } + +// An item of the text menu. Nothing is done here beyond saying which: the key presses it +// stands for are made on the Kotlin side, so an input method's composition is untouched +// and the field that has focus receives them as it would from the keyboard. +- (void)dxcMenuCommand:(NSMenuItem *)item { + struct dxc_event record; + memset(&record, 0, sizeof record); + record.kind = DXC_EVENT_MENU_COMMAND; + record.key_code = (int32_t)item.tag; + dxc_push_event(record); +} + +// Paste is greyed out while the clipboard holds no text. Everything else stays available: +// whether there is a selection to cut or copy is the field's to know. +- (BOOL)validateMenuItem:(NSMenuItem *)item { + if (item.action == @selector(dxcMenuCommand:) && item.tag == DXC_PASTE_COMMAND) { + return [NSPasteboard.generalPasteboard stringForType:NSPasteboardTypeString] != nil; + } + return YES; +} - (void)scrollWheel:(NSEvent *)event { [self dxcSend:DXC_EVENT_SCROLL event:event]; } // Both, and in this order. The key itself is what arrows, Enter and backspace are read // as, and `interpretKeyEvents:` is what turns the rest into text: it hands the event to @@ -514,7 +640,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 +664,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; @@ -651,6 +806,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 { @@ -676,6 +862,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. * @@ -697,9 +918,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(); @@ -715,15 +943,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; @@ -736,6 +983,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; @@ -763,6 +1014,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 +1047,94 @@ 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 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 { + 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; + } + }); +} + /** 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(^{ @@ -831,11 +1187,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/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..c61d7ac61 100644 --- a/renderer/desktop/c/win32_window.c +++ b/renderer/desktop/c/win32_window.c @@ -1424,6 +1424,27 @@ void dxc_native_install_menu(const char *application_name) { (void)application_name; } +/* The text context menu of this window is not drawn natively yet; the call is accepted. */ +void dxc_native_set_text_menu(const char *spec) { + (void)spec; +} + +/* + * 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. * @@ -1443,6 +1464,43 @@ 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 *rgba, int32_t width, int32_t height) { + (void)rgba; + (void)width; + (void)height; +} + +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..101c15fa4 100644 --- a/renderer/desktop/c/x11_window.c +++ b/renderer/desktop/c/x11_window.c @@ -355,6 +355,64 @@ void dxc_native_install_menu(const char *application_name) { (void)application_name; } +/* The text context menu of this window is not drawn natively yet; the call is accepted. */ +void dxc_native_set_text_menu(const char *spec) { + (void)spec; +} + +/* + * 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 + * 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 *rgba, int32_t width, int32_t height) { + (void)rgba; + (void)width; + (void)height; +} + +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..c03921099 100644 --- a/renderer/desktop/src/AppKitWindow.kt +++ b/renderer/desktop/src/AppKitWindow.kt @@ -30,11 +30,16 @@ 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 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 @@ -87,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 @@ -96,6 +119,27 @@ private external fun clipboardWrite(text: CCharPointer?) @CFunction("dxc_native_install_menu") private external fun installMenu(name: CCharPointer?) +@CFunction("dxc_native_set_text_menu") +private external fun setTextMenu(spec: 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(rgba: CCharPointer?, width: Int, height: 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 @@ -143,6 +187,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, + ) + } + } /** @@ -228,6 +290,86 @@ 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 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(), 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. * @@ -255,6 +397,21 @@ fun installApplicationMenu(name: String) { } } +/** + * Tells the window what its text context menu holds. + * + * Described here and drawn there, so the list, the labels and the order are the ones the + * Kotlin/Native macOS window shows too. Chosen items come back as menu command events. + */ +fun installTextMenu() { + val holder = CTypeConversion.toCString(textMenuSpec(textContextMenu(clipboardHasText = true))) + try { + setTextMenu(holder.get()) + } finally { + holder.close() + } +} + /** What is on the clipboard, or empty where it holds something that is not text. */ fun readClipboard(): String { val buffer = StackValue.get(CLIPBOARD_BYTES) @@ -341,16 +498,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 +528,15 @@ 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) + val minimum = contentMinimum(asked?.minWidth ?: 0, asked?.minHeight ?: 0) + configureNativeWindow( + resizable = asked?.resizable ?: true, + minWidth = minimum?.first?.toInt() ?: 0, + minHeight = minimum?.second?.toInt() ?: 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,11 +558,17 @@ 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 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 @@ -397,56 +582,144 @@ 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. + val clipboard = WindowClipboard() + // The same monitor the Kotlin/Native window uses. This one is polled, because the + // setting is read through the toolkit-free theme query rather than announced. + val appearance = SystemDarkMonitor(read = ::systemIsDarkNow) + @Suppress("DEPRECATION") + val clipboardManager = WindowClipboardManager() + scene.setContent { + androidx.compose.runtime.CompositionLocalProvider( + dev.darkpyonix.composerust.runtime.LocalSystemDarkObserver provides { + androidx.compose.runtime.LaunchedEffect(Unit) { + while (true) { + kotlinx.coroutines.delay(SYSTEM_DARK_POLL_MILLIS) + appearance.refresh() + } + } + appearance.dark.value + }, + androidx.compose.ui.platform.LocalClipboard provides clipboard, + androidx.compose.ui.platform.LocalClipboardManager provides clipboardManager, + ) { + dev.darkpyonix.composerust.runtime.ComposeRustContent( + host, + Modifier.fillMaxSize(), + caption = caption.value, + ) + } + } installApplicationMenu(asked?.title?.takeIf { it.isNotEmpty() } ?: "compose-rust") + installTextMenu() + + val started = System.nanoTime() + val dockIcon = DockIcon( + lookup = { id -> + (host.table.assets.asset(id) as? dev.darkpyonix.composerust.ui.node.Asset.Raster)?.bitmap + }, + apply = { picture -> + iconPixels(picture)?.let { (pixels, width, height) -> + setApplicationIcon(pixels, width, height) + } + }, + ) + 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. Asked each frame + // until it is there and then not at all, by the same rule the other macOS window uses. + if (!dockIcon.applied && asked != null) dockIcon.tryApply(asked.icon) + 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()) { - frame++ + if (autoExitMillis != null && + (System.nanoTime() - started) / NANOS_PER_MILLI >= autoExitMillis + ) { + break + } + // 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() - 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.RESIZE) { - size = androidx.compose.ui.unit.IntSize(event.x.toInt(), event.y.toInt()) - scene.size = size - } - 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) - 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 @@ -454,13 +727,33 @@ internal fun runAppKitSpike() { // 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. */ private fun drawFrame( window: NativeWindow, @@ -468,6 +761,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 +781,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/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/NativeClipboard.kt b/renderer/desktop/src/NativeClipboard.kt new file mode 100644 index 000000000..7baa2dc93 --- /dev/null +++ b/renderer/desktop/src/NativeClipboard.kt @@ -0,0 +1,53 @@ +@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 window's own pasteboard, reached through the two calls every window of ours shares. */ +private object WindowPasteboard : TextPasteboard { + override fun read(): String? = readClipboard() + override fun write(text: String) = writeClipboard(text) +} + +private fun clipboardText(): String? = WindowPasteboard.pasteText() + +@Suppress("DEPRECATION") +internal class WindowClipboardManager : ClipboardManager { + override fun getText(): AnnotatedString? = clipboardText()?.let(::AnnotatedString) + + override fun setText(annotatedString: AnnotatedString) = WindowPasteboard.copyText(annotatedString.text) + + override fun hasText(): Boolean = WindowPasteboard.hasText() +} + +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() + } + WindowPasteboard.copyText(text) + } + + override val nativeClipboard: NativeClipboard get() = this +} 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..fe99768ba 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 @@ -104,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/SystemAppearance.kt b/renderer/desktop/src/SystemAppearance.kt index c9a4b53c8..12810ab24 100644 --- a/renderer/desktop/src/SystemAppearance.kt +++ b/renderer/desktop/src/SystemAppearance.kt @@ -39,4 +39,9 @@ internal fun rememberSystemDark(): State { return state } -private const val POLL_INTERVAL_MILLIS = 1_000L +/** The system's setting, read now. */ +internal fun systemIsDarkNow(): Boolean = currentSystemTheme == SystemTheme.DARK + +internal const val SYSTEM_DARK_POLL_MILLIS = 1_000L + +private const val POLL_INTERVAL_MILLIS = SYSTEM_DARK_POLL_MILLIS diff --git a/renderer/desktop/src/TextContextMenu.kt b/renderer/desktop/src/TextContextMenu.kt new file mode 100644 index 000000000..a64481192 --- /dev/null +++ b/renderer/desktop/src/TextContextMenu.kt @@ -0,0 +1,80 @@ +@file:OptIn(androidx.compose.ui.InternalComposeUiApi::class) + +package dev.darkpyonix.composerust.ui.platform + +import androidx.compose.ui.input.key.Key +import androidx.compose.ui.input.key.KeyEvent +import androidx.compose.ui.input.key.KeyEventType + +// The text context menu of a window that has no toolkit, as data. +// +// One model for every window that draws its own menu: the GraalVM AppKit window shows it +// from C and the Kotlin/Native macOS window shows it from Kotlin, and both read the same +// list, the same labels, the same order and the same meaning for "Paste is greyed out". +// Nothing here names a platform, so it compiles on both through the symlink. +// +// An item acts by pressing the shortcut it is named after. The editor is Compose's and the +// key is the way in that every window already has, which also keeps an input method's +// composition where it was: no text is moved by the menu, and a key goes to the field that +// is focused exactly as it would from the keyboard. + +/** What the menu offers, in the order the platform puts them. */ +enum class TextCommand(val id: Int, val title: String, val key: Key) { + Cut(1, "Cut", Key.X), + Copy(2, "Copy", Key.C), + Paste(3, "Paste", Key.V), + SelectAll(4, "Select All", Key.A), + ; + + companion object { + /** The command a window reported by number, or null for a number nobody sent. */ + fun fromId(id: Int): TextCommand? = values().firstOrNull { it.id == id } + } +} + +/** One row of the menu. A null [command] is the line between the groups. */ +data class TextMenuEntry(val command: TextCommand?, val enabled: Boolean = true) + +/** + * The rows of the menu. + * + * Paste is the one that is known to be pointless: with no text on the clipboard it would do + * nothing, so it is greyed out. Cut and Copy cannot be judged from outside the field, which + * owns the selection, and a Copy with nothing selected is ignored by the field. + */ +fun textContextMenu(clipboardHasText: Boolean): List = listOf( + TextMenuEntry(TextCommand.Cut), + TextMenuEntry(TextCommand.Copy), + TextMenuEntry(TextCommand.Paste, enabled = clipboardHasText), + TextMenuEntry(null), + TextMenuEntry(TextCommand.SelectAll), +) + +/** The key presses that stand for [command]: the key down and the key up, with the modifier held. */ +internal fun TextCommand.keyPresses(commandKey: Boolean = true): List = + listOf(KeyEventType.KeyDown, KeyEventType.KeyUp).map { type -> + KeyEvent( + key = key, + type = type, + codePoint = 0, + isAltPressed = false, + // The shortcut modifier is Command on macOS and Control elsewhere. + isCtrlPressed = !commandKey, + isMetaPressed = commandKey, + isShiftPressed = false, + ) + } + +/** Carries out [command] by sending its key presses to [send], which is the scene's key entry. */ +internal fun TextCommand.perform(commandKey: Boolean = true, send: (KeyEvent) -> Unit) { + for (press in keyPresses(commandKey)) send(press) +} + +/** + * The menu as a line of text a native window can build from: one row per line, the + * command number and its title with a tab between them, and a single dash for the line + * between groups. + */ +fun textMenuSpec(entries: List): String = entries.joinToString("\n") { + it.command?.let { command -> "${command.id}\t${command.title}" } ?: "-" +} diff --git a/renderer/desktop/src/WindowEvents.kt b/renderer/desktop/src/WindowEvents.kt index 7b3c30c44..285eb6911 100644 --- a/renderer/desktop/src/WindowEvents.kt +++ b/renderer/desktop/src/WindowEvents.kt @@ -53,6 +53,10 @@ data class WindowEvent( const val RESIZE = 9 const val FILES_ENTERED = 10 const val FILES_DROPPED = 11 + const val FILES_EXITED = 12 + + /** A context menu item was chosen; [keyCode] carries which, as a [TextCommand] id. */ + const val MENU_COMMAND = 13 } } @@ -87,6 +91,9 @@ internal fun ComposeScene.receive(event: WindowEvent, win32: Boolean = false) { ), ) + WindowEvent.MENU_COMMAND -> TextCommand.fromId(event.keyCode) + ?.perform(commandKey = !win32) { sendKeyEvent(it) } + WindowEvent.POINTER_MOVE -> sendPointerEvent( eventType = PointerEventType.Move, position = Offset(event.x, event.y), diff --git a/renderer/desktop/src/WindowParity.kt b/renderer/desktop/src/WindowParity.kt new file mode 100644 index 000000000..4d3c56a1b --- /dev/null +++ b/renderer/desktop/src/WindowParity.kt @@ -0,0 +1,107 @@ +package dev.darkpyonix.composerust.ui.platform + +import androidx.compose.runtime.State +import androidx.compose.runtime.mutableStateOf + +// What both macOS windows decide the same way, once. +// +// The GraalVM window and the Kotlin/Native window each make the operating system calls in +// their own language. Everything that is a decision rather than a call lives here and is +// reached from both: the smallest size a window is given, when the Dock icon is ready to be +// put on, how a change of the system's light or dark setting is noticed, and what text on +// the clipboard means. Nothing here names AppKit, AWT or GraalVM. + +/** + * The smallest content area the window may be dragged to, in points, or null where the + * application named none. A measurement of zero means it did not ask; one axis may be + * named without the other. + */ +fun contentMinimum(minWidth: Int, minHeight: Int): Pair? = + if (minWidth <= 0 && minHeight <= 0) { + null + } else { + minWidth.coerceAtLeast(0).toDouble() to minHeight.coerceAtLeast(0).toDouble() + } + +/** + * Puts the application's picture on the Dock, once the asset it named has arrived. + * + * The id is known from the first batch and the picture a little later, so [tryApply] is + * asked until it succeeds and does nothing afterwards. What it looks the picture up in and + * where it puts it are parameters, so the waiting is the same on both windows and can be + * exercised without a Dock. + */ +class DockIcon( + private val lookup: (Int) -> T?, + private val apply: (T) -> Unit, +) { + var applied = false + private set + + /** True once the picture is on the Dock, from this call or an earlier one. */ + fun tryApply(assetId: Int): Boolean { + if (applied) return true + if (assetId == 0) return false + val picture = lookup(assetId) ?: return false + apply(picture) + applied = true + return true + } +} + +/** + * Whether the system is in dark mode, kept current. + * + * Compose's own answer is read once and never looks again, so a window opened in light + * mode stays light while every other application on screen changes. This reads it again + * when told to, writes the answer into snapshot state, which recomposes whatever read it, + * and asks [requestFrame] for a frame so that what changed is drawn without waiting for + * the next input. + * + * [read] is what touches the system. How [refresh] gets called is the window's: the + * Kotlin/Native one subscribes to appearance changes, the GraalVM one polls. + */ +class SystemDarkMonitor( + private val read: () -> Boolean, + private val requestFrame: () -> Unit = {}, +) { + private val state = mutableStateOf(read()) + + /** True while the system is dark. */ + val dark: State get() = state + + /** Reads the system again, and when the answer moved, publishes it and asks for a frame. */ + fun refresh() { + val now = read() + if (now != state.value) { + state.value = now + requestFrame() + } + } +} + +/** True for the appearance names of the dark family: dark Aqua and its vibrant and contrast forms. */ +fun isDarkAppearanceName(name: String?): Boolean = name?.contains("Dark") == true + +/** + * The text on a pasteboard, which is all a text field asks for. + * + * A seam so that what the clipboard does with it is one set of rules for both windows and + * can be exercised without a window server. + */ +interface TextPasteboard { + /** The text on it, or null where it holds nothing or holds something that is not text. */ + fun read(): String? + + /** Replaces what is on it with [text]. */ + fun write(text: String) +} + +/** The text to paste, or null: an empty string is nothing, not a paste of nothing. */ +fun TextPasteboard.pasteText(): String? = read()?.takeIf { it.isNotEmpty() } + +/** Whether there is text to paste, which is what decides if Paste is offered. */ +fun TextPasteboard.hasText(): Boolean = pasteText() != null + +/** Stores [text], where null clears the clipboard as Compose asks it to. */ +fun TextPasteboard.copyText(text: String?) = write(text.orEmpty()) diff --git a/renderer/desktop/src/renderer/HostTextField.kt b/renderer/desktop/src/renderer/HostTextField.kt index f81b03e01..130d763e6 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.delay -import kotlinx.coroutines.flow.collectLatest +import kotlinx.coroutines.Job +import kotlinx.coroutines.coroutineScope +import androidx.compose.runtime.withFrameNanos +import kotlinx.coroutines.launch import dev.darkpyonix.composerust.protocol.ColorRole import dev.darkpyonix.composerust.protocol.HostEvent import dev.darkpyonix.composerust.protocol.PropertyKind @@ -48,21 +50,11 @@ import dev.darkpyonix.composerust.ui.textStyle import dev.darkpyonix.composerust.ui.node.HostText import dev.darkpyonix.composerust.design.LocalGlassDepth -/** - * Quiet period before a `TextChanged` notification is sent. - * - * The Host is told about edits, but not once per keystroke: `TextChanged` is a notification, - * not the mechanism that keeps the field's value, so it can wait for typing to pause. The - * interval is chosen here rather than negotiated; 120 ms is short enough to feel immediate - * and long enough to collapse a burst of typing into one event. - */ -const val TEXT_CHANGED_DEBOUNCE_MILLIS: Long = 120 - /** * The uncontrolled TextField. * * The editing value and the IME composition live in `remember` and never round-trip through - * the Host. The Host learns about edits through debounced `TextChanged` and through the + * the Host. The Host learns about edits through coalesced `TextChanged` and through the * commit events `TextSubmitted` and `FocusLost`; it changes the value only with `SetText`, * which is held back while a composition is in progress. * @@ -104,16 +96,38 @@ internal fun HostTextField(node: Node, modifier: Modifier, dispatcher: EventDisp } } - // Debounced change notification. The Host is told what the field now shows; it never - // sends the value back, so the composition cannot be reset by the round trip. + // Change notification, at most one a frame. + // + // An edit is told to the Host on the next frame, carrying the latest text, so keys that + // arrive within one frame cost it one event and a key is never held back by a timer: the + // line that echoes what was typed follows the key by a frame. The last text is always + // sent, because the frame that follows an edit sends whatever the field holds then, and + // a later edit starts another. The Host is told what the field now shows; it never sends + // the value back, so the composition cannot be reset by the round trip. if (changeHandler != null) { LaunchedEffect(nodeId, changeHandler) { var lastSent = value.text - snapshotFlow { value.text }.collectLatest { text -> - if (text == lastSent) return@collectLatest - delay(TEXT_CHANGED_DEBOUNCE_MILLIS) - lastSent = text - dispatcher.dispatch(HostEvent.TextChanged(nodeId, changeHandler, text)) + var latest = lastSent + var sending: Job? = null + coroutineScope { + snapshotFlow { value.text }.collect { text -> + latest = text + if (text == lastSent || sending?.isActive == true) return@collect + sending = launch { + do { + // Sent from inside the frame callback, not after it. Resuming + // from withFrameNanos goes back through the scene's dispatcher, + // which on the desktop windows runs what is waiting at the start + // of the next frame, so the send and the recomposition that + // follows it each cost a frame. In the callback the Host's batch + // is applied before this frame is composed and drawn. + withFrameNanos { + lastSent = latest + dispatcher.dispatch(HostEvent.TextChanged(nodeId, changeHandler, lastSent)) + } + } while (latest != lastSent) + } + } } } } 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/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/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 }) + } +} 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/renderer/desktop/test/TextContextMenuTest.kt b/renderer/desktop/test/TextContextMenuTest.kt new file mode 100644 index 000000000..5c9e09e71 --- /dev/null +++ b/renderer/desktop/test/TextContextMenuTest.kt @@ -0,0 +1,82 @@ +package dev.darkpyonix.composerust.test + +import androidx.compose.ui.input.key.Key +import androidx.compose.ui.input.key.KeyEvent +import androidx.compose.ui.input.key.KeyEventType +import androidx.compose.ui.input.key.isCtrlPressed +import androidx.compose.ui.input.key.isMetaPressed +import androidx.compose.ui.input.key.key +import androidx.compose.ui.input.key.type +import dev.darkpyonix.composerust.ui.platform.TextCommand +import dev.darkpyonix.composerust.ui.platform.perform +import dev.darkpyonix.composerust.ui.platform.textContextMenu +import dev.darkpyonix.composerust.ui.platform.textMenuSpec +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertNull +import kotlin.test.assertTrue + +/** + * The text context menu both macOS windows draw, as data. + * + * Right-click menu copy and the keyboard shortcut have to mean the same thing, and the + * native window and the Kotlin/Native window have to offer the same rows. + */ +class TextContextMenuTest { + + @Test + fun fr33_6_the_menu_offers_cut_copy_paste_and_select_all_in_the_platform_order() { + val rows = textContextMenu(clipboardHasText = true) + assertEquals( + listOf(TextCommand.Cut, TextCommand.Copy, TextCommand.Paste, null, TextCommand.SelectAll), + rows.map { it.command }, + ) + assertTrue(rows.all { it.enabled }) + } + + @Test + fun fr5_paste_is_greyed_out_while_the_clipboard_holds_no_text() { + val paste = textContextMenu(clipboardHasText = false).single { it.command == TextCommand.Paste } + assertFalse(paste.enabled) + assertTrue( + textContextMenu(clipboardHasText = false) + .filter { it.command != TextCommand.Paste } + .all { it.enabled }, + "only paste can be judged from outside the field", + ) + } + + @Test + fun fr33_6_choosing_copy_presses_command_c_down_then_up() { + val sent = ArrayList() + TextCommand.Copy.perform { sent.add(it) } + assertEquals(listOf(KeyEventType.KeyDown, KeyEventType.KeyUp), sent.map { it.type }) + assertTrue(sent.all { it.key == Key.C && it.isMetaPressed && !it.isCtrlPressed }) + } + + @Test + fun fr5_every_command_stands_for_its_own_key_and_other_platforms_hold_control() { + val keys = mapOf( + TextCommand.Cut to Key.X, + TextCommand.Copy to Key.C, + TextCommand.Paste to Key.V, + TextCommand.SelectAll to Key.A, + ) + for ((command, key) in keys) { + val sent = ArrayList() + command.perform(commandKey = false) { sent.add(it) } + assertTrue(sent.all { it.key == key && it.isCtrlPressed && !it.isMetaPressed }, "$command") + } + } + + @Test + fun fr5_the_description_the_native_window_builds_from_round_trips_the_rows() { + val spec = textMenuSpec(textContextMenu(clipboardHasText = true)) + assertEquals("1\tCut\n2\tCopy\n3\tPaste\n-\n4\tSelect All", spec) + for (command in TextCommand.values()) { + assertEquals(command, TextCommand.fromId(command.id)) + } + assertNull(TextCommand.fromId(99), "a number nobody sent is not a command") + } +} diff --git a/renderer/desktop/test/TextFieldTest.kt b/renderer/desktop/test/TextFieldTest.kt index 7d65c1ce1..b894168cc 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") @@ -103,6 +100,76 @@ class TextFieldTest { assertEquals("hi", changes.lastOrNull()?.text, "events were ${connection.events}") } + @Test + fun fr5_an_edit_reaches_the_host_on_the_next_frame() = 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, with no timer in between. + val changes = connection.events.filterIsInstance() + 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()) + setContent { ComposeRustContent(rememberComposeRustHost(connection)) } + mainClock.autoAdvance = false + + onNodeWithTag(nodeTestTag(FIELD)).performTextInput("a") + onNodeWithTag(nodeTestTag(FIELD)).performTextInput("b") + onNodeWithTag(nodeTestTag(FIELD)).performTextInput("c") + mainClock.advanceTimeByFrame() + mainClock.advanceTimeByFrame() + assertEquals( + 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 + + onNodeWithTag(nodeTestTag(FIELD)).performTextInput("a") + mainClock.advanceTimeByFrame() + mainClock.advanceTimeByFrame() + onNodeWithTag(nodeTestTag(FIELD)).performTextInput("b") + mainClock.advanceTimeByFrame() + mainClock.advanceTimeByFrame() + assertEquals( + listOf("a", "ab"), + 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()) diff --git a/renderer/desktop/test/WindowParityTest.kt b/renderer/desktop/test/WindowParityTest.kt new file mode 100644 index 000000000..e9564e176 --- /dev/null +++ b/renderer/desktop/test/WindowParityTest.kt @@ -0,0 +1,103 @@ +package dev.darkpyonix.composerust.test + +import dev.darkpyonix.composerust.ui.platform.DockIcon +import dev.darkpyonix.composerust.ui.platform.SystemDarkMonitor +import dev.darkpyonix.composerust.ui.platform.TextPasteboard +import dev.darkpyonix.composerust.ui.platform.contentMinimum +import dev.darkpyonix.composerust.ui.platform.copyText +import dev.darkpyonix.composerust.ui.platform.hasText +import dev.darkpyonix.composerust.ui.platform.isDarkAppearanceName +import dev.darkpyonix.composerust.ui.platform.pasteText +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertNull +import kotlin.test.assertTrue + +/** + * The decisions both macOS windows share: the GraalVM window and the Kotlin/Native window + * call these and make only the system calls themselves, so one set of tests covers both. + */ +class WindowParityTest { + + private class FakePasteboard(var text: String? = null) : TextPasteboard { + override fun read() = text + override fun write(text: String) { this.text = text } + } + + @Test + fun fr19_a_window_that_named_a_minimum_gets_it_and_one_that_did_not_gets_none() { + assertEquals(320.0 to 240.0, contentMinimum(320, 240)) + assertEquals(320.0 to 0.0, contentMinimum(320, 0), "one axis may be named alone") + assertNull(contentMinimum(0, 0), "zero means the application did not ask") + assertNull(contentMinimum(-1, -1)) + } + + @Test + fun fr19_3_the_dock_icon_waits_for_the_asset_and_is_put_on_once() { + var arrived: String? = null + val applied = ArrayList() + val icon = DockIcon(lookup = { arrived }, apply = { applied.add(it) }) + + assertFalse(icon.tryApply(0), "no icon named") + assertFalse(icon.tryApply(7), "the asset has not arrived") + assertTrue(applied.isEmpty()) + + arrived = "picture" + assertTrue(icon.tryApply(7)) + assertTrue(icon.tryApply(7)) + assertEquals(listOf("picture"), applied, "applied once however often it is asked") + assertTrue(icon.applied) + } + + @Test + fun fr14_4_the_dark_family_of_appearance_names_reads_as_dark() { + assertTrue(isDarkAppearanceName("NSAppearanceNameDarkAqua")) + assertTrue(isDarkAppearanceName("NSAppearanceNameVibrantDark")) + assertFalse(isDarkAppearanceName("NSAppearanceNameAqua")) + assertFalse(isDarkAppearanceName(null)) + } + + @Test + fun fr14_4_a_change_after_the_window_opened_is_published_and_asks_for_a_frame() { + var system = false + var frames = 0 + val monitor = SystemDarkMonitor(read = { system }, requestFrame = { frames++ }) + assertFalse(monitor.dark.value) + system = true + monitor.refresh() + assertTrue(monitor.dark.value, "the window stayed light after the system went dark") + assertEquals(1, frames) + system = false + monitor.refresh() + assertFalse(monitor.dark.value) + assertEquals(2, frames) + } + + @Test + fun fr14_4_a_refresh_that_changes_nothing_costs_no_frame() { + var frames = 0 + val monitor = SystemDarkMonitor(read = { true }, requestFrame = { frames++ }) + assertTrue(monitor.dark.value, "the first answer is the system's own") + monitor.refresh() + monitor.refresh() + assertEquals(0, frames) + } + + @Test + fun fr5_a_copy_reaches_the_pasteboard_and_a_paste_reads_it_back() { + val board = FakePasteboard() + board.copyText("안녕 hello") + assertEquals("안녕 hello", board.text) + assertEquals("안녕 hello", board.pasteText()) + assertTrue(board.hasText()) + } + + @Test + fun fr5_an_empty_pasteboard_pastes_nothing_and_greys_out_paste() { + assertNull(FakePasteboard().pasteText()) + assertNull(FakePasteboard("").pasteText(), "an empty string is not a paste") + assertFalse(FakePasteboard().hasText()) + assertFalse(FakePasteboard(null).apply { copyText(null) }.hasText(), "null clears") + } +} diff --git a/renderer/linux/src/shared/TextContextMenu.kt b/renderer/linux/src/shared/TextContextMenu.kt new file mode 120000 index 000000000..7a8e4086c --- /dev/null +++ b/renderer/linux/src/shared/TextContextMenu.kt @@ -0,0 +1 @@ +../../../desktop/src/TextContextMenu.kt \ No newline at end of file diff --git a/renderer/linux/src/shared/WindowParity.kt b/renderer/linux/src/shared/WindowParity.kt new file mode 120000 index 000000000..a11f512b5 --- /dev/null +++ b/renderer/linux/src/shared/WindowParity.kt @@ -0,0 +1 @@ +../../../desktop/src/WindowParity.kt \ No newline at end of file diff --git a/renderer/macos/src/MacosAppearance.kt b/renderer/macos/src/MacosAppearance.kt new file mode 100644 index 000000000..29ee09c65 --- /dev/null +++ b/renderer/macos/src/MacosAppearance.kt @@ -0,0 +1,53 @@ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) + +package dev.darkpyonix.composerust.ui.platform + +import platform.AppKit.NSAppearance +import platform.AppKit.NSAppearanceNameAqua +import platform.AppKit.NSAppearanceNameDarkAqua +import platform.AppKit.NSApplication +import platform.Foundation.NSDistributedNotificationCenter +import platform.Foundation.NSOperationQueue +import platform.Foundation.valueForKey +import platform.darwin.DISPATCH_TIME_NOW +import platform.darwin.NSEC_PER_MSEC +import platform.darwin.dispatch_after +import platform.darwin.dispatch_get_main_queue +import platform.darwin.dispatch_time + +// The operating system calls only. What a change means, and what is done about it, is +// decided by SystemDarkMonitor in WindowParity.kt. + +/** The application's effective appearance, read now. */ +internal fun systemIsDark(): Boolean { + val appearance = NSApplication.sharedApplication().valueForKey("effectiveAppearance") as? NSAppearance + ?: return false + val best = appearance.bestMatchFromAppearancesWithNames( + listOf(NSAppearanceNameAqua, NSAppearanceNameDarkAqua), + ) as? String + return isDarkAppearanceName(best) +} + +/** + * Calls [onChange] when the system's light or dark setting changes. + * + * Listens for the system-wide interface theme notification, which is sent for the switch + * in System Settings and for the automatic day and night change. It can arrive before the + * application's own appearance has caught up, so [onChange] is called again shortly after; + * the monitor ignores a repeat that changes nothing. + */ +internal fun observeSystemAppearance(onChange: () -> Unit) { + NSDistributedNotificationCenter.defaultCenter.addObserverForName( + name = "AppleInterfaceThemeChangedNotification", + `object` = null, + queue = NSOperationQueue.mainQueue, + ) { _ -> + onChange() + for (delayMillis in listOf(150L, 750L)) { + dispatch_after( + dispatch_time(DISPATCH_TIME_NOW, delayMillis * NSEC_PER_MSEC.toLong()), + dispatch_get_main_queue(), + ) { onChange() } + } + } +} diff --git a/renderer/macos/src/MacosClipboard.kt b/renderer/macos/src/MacosClipboard.kt new file mode 100644 index 000000000..593313dbf --- /dev/null +++ b/renderer/macos/src/MacosClipboard.kt @@ -0,0 +1,62 @@ +@file:OptIn(androidx.compose.ui.ExperimentalComposeUiApi::class) + +package dev.darkpyonix.composerust.ui.platform + +import androidx.compose.ui.platform.ClipEntry +import androidx.compose.ui.platform.Clipboard +import androidx.compose.ui.platform.ClipboardManager +import androidx.compose.ui.platform.NativeClipboard +import androidx.compose.ui.text.AnnotatedString +import platform.AppKit.NSPasteboard +import platform.AppKit.NSPasteboardTypeString + +/** The system's general pasteboard, the one every other application copies to and from. */ +internal class GeneralPasteboard : TextPasteboard { + override fun read(): String? = + NSPasteboard.generalPasteboard.stringForType(NSPasteboardTypeString) + + override fun write(text: String) { + val board = NSPasteboard.generalPasteboard + board.clearContents() + board.setString(text, NSPasteboardTypeString) + } +} + +/** + * The clipboard Compose's text fields copy to and paste from, backed by a pasteboard. + * + * Provided to the content explicitly rather than left to the default. Whether copy, cut and + * paste work in a field is then this window's own doing and can be read here, and a copy + * that goes nowhere or a paste that finds nothing has one place to be looked for. + */ +internal class MacosClipboard( + private val pasteboard: TextPasteboard = GeneralPasteboard(), +) : Clipboard { + override suspend fun getClipEntry(): ClipEntry? = + pasteboard.pasteText()?.let { ClipEntry.withPlainText(it) } + + override suspend fun setClipEntry(clipEntry: ClipEntry?) { + pasteboard.copyText(clipEntry?.getPlainText()) + } + + override val nativeClipboard: NativeClipboard get() = NSPasteboard.generalPasteboard + + /** Whether there is text to paste, which is what decides if Paste is offered. */ + fun hasText(): Boolean = pasteboard.hasText() +} + +/** The older entry point to the same pasteboard, which some of Compose still asks for. */ +@Suppress("DEPRECATION") +internal class MacosClipboardManager( + private val pasteboard: TextPasteboard = GeneralPasteboard(), +) : ClipboardManager { + override fun getText(): AnnotatedString? = pasteboard.pasteText()?.let(::AnnotatedString) + + override fun setText(annotatedString: AnnotatedString) = pasteboard.copyText(annotatedString.text) + + override fun hasText(): Boolean = pasteboard.hasText() + + override fun getClip(): ClipEntry? = pasteboard.pasteText()?.let { ClipEntry.withPlainText(it) } + + override fun setClip(clipEntry: ClipEntry?) = pasteboard.copyText(clipEntry?.getPlainText()) +} diff --git a/renderer/macos/src/MacosRenderer.kt b/renderer/macos/src/MacosRenderer.kt index 1600892f7..1e0c6d109 100644 --- a/renderer/macos/src/MacosRenderer.kt +++ b/renderer/macos/src/MacosRenderer.kt @@ -13,6 +13,13 @@ import dev.darkpyonix.composerust.runtime.HostConnection import platform.AppKit.NSApplication import platform.AppKit.NSApplicationActivationPolicy import platform.AppKit.NSApplicationWillTerminateNotification +import androidx.compose.runtime.CompositionLocalProvider +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.ui.platform.LocalClipboard +import androidx.compose.ui.platform.LocalClipboardManager +import dev.darkpyonix.composerust.runtime.LocalSystemDarkObserver +import dev.darkpyonix.composerust.ui.node.Asset +import kotlinx.coroutines.delay import platform.AppKit.NSWindow import platform.Foundation.NSNotificationCenter import platform.Foundation.NSOperationQueue @@ -89,19 +96,51 @@ internal fun runRenderer(connection: () -> HostConnection): Int { // // The theme is resolved for this platform at the narrowest class: neither answer // depends on how wide the window is, and the window does not exist yet to be measured. + // Whether the system is dark, now and as it changes. Held here so the first answer is + // the real one and the window is not drawn light and corrected a moment later. + var requestFrame: () -> Unit = {} + val appearance = SystemDarkMonitor(read = ::systemIsDark, requestFrame = { requestFrame() }) + observeSystemAppearance(appearance::refresh) val dressing = resolveTheme( theme = host.table.theme, platform = HostPlatform.MacOs, - systemDark = false, + systemDark = appearance.dark.value, ).let { it.rules.caption(it, asked?.titleBar ?: TitleBar.Normal) } + val clipboard = MacosClipboard() + @Suppress("DEPRECATION") + val clipboardManager = MacosClipboardManager() val window = MacosWindow( name = asked?.title?.takeIf { it.isNotEmpty() } ?: "compose-rust", width = if (asked != null && asked.width > 0) asked.width else 520, height = if (asked != null && asked.height > 0) asked.height else 360, buttonInset = dressing.platformButtonInset, cornerRadius = dressing.windowCornerRadius, + minimumSize = contentMinimum(asked?.minWidth ?: 0, asked?.minHeight ?: 0), + clipboardHasText = { clipboard.hasText() }, ) - window.setContent { ComposeRustContent(host, caption = window.caption.value) } + requestFrame = window::requestFrame + val dockIcon = DockIcon( + lookup = { id -> (host.table.assets.asset(id) as? Asset.Raster)?.bitmap }, + apply = { picture -> picture.toNSImage()?.let { application.applicationIconImage = it } }, + ) + val iconAsset = asked?.icon ?: 0 + @Suppress("DEPRECATION") + window.setContent { + CompositionLocalProvider( + LocalSystemDarkObserver provides { appearance.dark.value }, + LocalClipboard provides clipboard, + LocalClipboardManager provides clipboardManager, + ) { + // The asset arrives a little after the first batch names it, so it is looked + // for until it is there. + if (iconAsset != 0) { + LaunchedEffect(Unit) { + while (!dockIcon.tryApply(iconAsset)) delay(100) + } + } + ComposeRustContent(host, caption = window.caption.value) + } + } application.activateIgnoringOtherApps(true) application.run() diff --git a/renderer/macos/src/MacosTextToolbar.kt b/renderer/macos/src/MacosTextToolbar.kt index 176d2a590..7cf5bf757 100644 --- a/renderer/macos/src/MacosTextToolbar.kt +++ b/renderer/macos/src/MacosTextToolbar.kt @@ -20,7 +20,8 @@ import platform.CoreGraphics.CGPointMake * * Which items appear is Compose's decision and is made afresh each time, because what a * selection can do changes: there is nothing to copy without a selection, and nothing to - * paste without a clipboard. + * paste while the pasteboard holds no text, which Compose reads from the clipboard the + * window provides. */ internal class MacosTextToolbar(private val view: () -> NSView) : TextToolbar { @@ -38,12 +39,12 @@ internal class MacosTextToolbar(private val view: () -> NSView) : TextToolbar { ) { val menu = NSMenu() // In the order every other application on this platform puts them. - add(menu, "Cut", onCutRequested) - add(menu, "Copy", onCopyRequested) - add(menu, "Paste", onPasteRequested) + add(menu, TextCommand.Cut.title, onCutRequested) + add(menu, TextCommand.Copy.title, onCopyRequested) + add(menu, TextCommand.Paste.title, onPasteRequested) if (onSelectAllRequested != null) { menu.addItem(NSMenuItem.separatorItem()) - add(menu, "Select All", onSelectAllRequested) + add(menu, TextCommand.SelectAll.title, onSelectAllRequested) } if (menu.numberOfItems.toInt() == 0) return diff --git a/renderer/macos/src/MacosWindow.kt b/renderer/macos/src/MacosWindow.kt index 6456b3bf5..2f57cad5a 100644 --- a/renderer/macos/src/MacosWindow.kt +++ b/renderer/macos/src/MacosWindow.kt @@ -95,6 +95,7 @@ import platform.AppKit.NSAccessibilityStaticTextRole import platform.AppKit.NSAccessibilityTextFieldRole import platform.CoreGraphics.CGRectMake import platform.Foundation.NSMakeRect +import platform.Foundation.NSMakeSize import androidx.compose.runtime.setValue import androidx.compose.runtime.getValue import platform.CoreGraphics.CGPointMake @@ -123,6 +124,10 @@ internal class MacosWindow( private val buttonInset: Dp = 0.dp, /** How round the window is. Zero leaves the system's own. */ private val cornerRadius: Dp = 0.dp, + /** The smallest content area the window may be resized to, or null for none. */ + private val minimumSize: Pair? = null, + /** Whether Paste is worth offering right now. Asked each time the menu is shown. */ + private val clipboardHasText: () -> Boolean = { true }, ) { private var measured = IntSize(width, height) private val components = DefaultArchitectureComponentsOwner() @@ -228,6 +233,11 @@ internal class MacosWindow( if (readerIsListening) semantics.pushIfChanged(afterDrawing = true) } + /** Asks for a frame, which is drawn where AppKit next draws the view. */ + fun requestFrame() { + view.needsDisplay = true + } + /** Whether anything has ever asked this window what is in it. */ private var readerIsListening = false @@ -574,10 +584,12 @@ internal class MacosWindow( // overridden to reach the scene: with no call back to it, the menu was built // and never asked for. NSMenu.popUpContextMenu(editingMenu(), withEvent = event, forView = this) + // The menu's tracking swallows the release, so it is sent here: without it the + // scene goes on believing the button is held. + send(event, PointerEventType.Release, PointerButton.Secondary) } - override fun rightMouseUp(event: NSEvent) = - send(event, PointerEventType.Release, PointerButton.Secondary) + override fun rightMouseUp(event: NSEvent) = Unit override fun mouseMoved(event: NSEvent) = send(event, PointerEventType.Move) @@ -632,6 +644,9 @@ internal class MacosWindow( view.wantsLayer = true view.layerContentsRedrawPolicy = NSViewLayerContentsRedrawDuringViewResize + minimumSize?.let { (minWidth, minHeight) -> + window.contentMinSize = NSMakeSize(minWidth, minHeight) + } window.center() window.makeKeyAndOrderFront(null) window.makeFirstResponder(view) @@ -705,46 +720,36 @@ internal class MacosWindow( /** * Cut, copy, paste and select all, as a menu of the system's own. * - * Each item presses the shortcut it is named after rather than calling into the editor, - * because the editor is Compose's and the keys are the way in that this window already - * has. Nothing here decides whether an item applies: the field the keys reach ignores a - * copy with nothing selected, which is the same answer as a greyed out item and is one - * fewer thing to keep in step with what is on screen. + * Built from the same list the GraalVM window draws, so the rows, labels and order are + * the same on both. Each item presses the shortcut it is named after rather than + * calling into the editor, because the editor is Compose's and the keys are the way in + * that this window already has; nothing moves text itself, so an input method's + * composition is left as it was. */ private fun editingMenu(): NSMenu { val menu = NSMenu() - shortcut(menu, "Cut", Key.X) - shortcut(menu, "Copy", Key.C) - shortcut(menu, "Paste", Key.V) - menu.addItem(NSMenuItem.separatorItem()) - shortcut(menu, "Select All", Key.A) + // Enabled is decided by the list, not by AppKit asking the target. + menu.autoenablesItems = false + for (entry in textContextMenu(clipboardHasText())) { + val command = entry.command + if (command == null) { + menu.addItem(NSMenuItem.separatorItem()) + continue + } + val item = NSMenuItem() + item.setTitle(command.title) + item.setEnabled(entry.enabled) + item.setTarget( + MenuShortcut { + command.perform { scene.sendKeyEvent(it) } + }, + ) + item.setAction(platform.darwin.sel_registerName("perform")) + menu.addItem(item) + } return menu } - private fun shortcut(menu: NSMenu, title: String, key: Key) { - val item = NSMenuItem() - item.setTitle(title) - item.setTarget( - MenuShortcut { - scene.sendKeyEvent(command(key, KeyEventType.KeyDown)) - scene.sendKeyEvent(command(key, KeyEventType.KeyUp)) - }, - ) - item.setAction(platform.darwin.sel_registerName("perform")) - menu.addItem(item) - } - - /** One key held with Command, built from parts the way a platform event is. */ - private fun command(key: Key, type: KeyEventType): KeyEvent = KeyEvent( - key = key, - type = type, - codePoint = 0, - isAltPressed = false, - isCtrlPressed = false, - isMetaPressed = true, - isShiftPressed = false, - ) - private fun send(event: NSEvent, kind: PointerEventType, button: PointerButton? = null) { scene.sendPointerEvent( eventType = kind, diff --git a/renderer/macos/src/MacosWindowChrome.kt b/renderer/macos/src/MacosWindowChrome.kt new file mode 100644 index 000000000..fa11aaa47 --- /dev/null +++ b/renderer/macos/src/MacosWindowChrome.kt @@ -0,0 +1,24 @@ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) + +package dev.darkpyonix.composerust.ui.platform + +import androidx.compose.ui.graphics.ImageBitmap +import androidx.compose.ui.graphics.asSkiaBitmap +import kotlinx.cinterop.addressOf +import kotlinx.cinterop.usePinned +import org.jetbrains.skia.EncodedImageFormat +import org.jetbrains.skia.Image +import platform.AppKit.NSImage +import platform.Foundation.NSData +import platform.Foundation.create + +// The operating system calls only. When the icon is put on, and what size a window is +// given, are decided in WindowParity.kt. + +/** The picture as the system's image type, by way of the encoded form every reader of images accepts. */ +internal fun ImageBitmap.toNSImage(): NSImage? { + val bytes = Image.makeFromBitmap(asSkiaBitmap()).encodeToData(EncodedImageFormat.PNG)?.bytes + ?: return null + val data = bytes.usePinned { NSData.create(bytes = it.addressOf(0), length = bytes.size.toULong()) } + return NSImage(data = data) +} diff --git a/renderer/macos/src/shared/TextContextMenu.kt b/renderer/macos/src/shared/TextContextMenu.kt new file mode 120000 index 000000000..7a8e4086c --- /dev/null +++ b/renderer/macos/src/shared/TextContextMenu.kt @@ -0,0 +1 @@ +../../../desktop/src/TextContextMenu.kt \ No newline at end of file diff --git a/renderer/macos/src/shared/WindowParity.kt b/renderer/macos/src/shared/WindowParity.kt new file mode 120000 index 000000000..a11f512b5 --- /dev/null +++ b/renderer/macos/src/shared/WindowParity.kt @@ -0,0 +1 @@ +../../../desktop/src/WindowParity.kt \ No newline at end of file diff --git a/renderer/macos/test/MacosClipboardTest.kt b/renderer/macos/test/MacosClipboardTest.kt new file mode 100644 index 000000000..38ed79652 --- /dev/null +++ b/renderer/macos/test/MacosClipboardTest.kt @@ -0,0 +1,55 @@ +package dev.darkpyonix.composerust.test + +import androidx.compose.ui.ExperimentalComposeUiApi +import androidx.compose.ui.platform.ClipEntry +import androidx.compose.ui.text.AnnotatedString +import dev.darkpyonix.composerust.ui.platform.MacosClipboard +import dev.darkpyonix.composerust.ui.platform.MacosClipboardManager +import dev.darkpyonix.composerust.ui.platform.TextPasteboard +import kotlinx.coroutines.runBlocking +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertNull +import kotlin.test.assertTrue + +/** + * The Compose clipboard types over a pasteboard that is a variable. The rules for what + * counts as text live in shared code and are tested there; this is the ClipEntry glue. + */ +@OptIn(ExperimentalComposeUiApi::class) +class MacosClipboardTest { + + private class FakePasteboard(var text: String? = null) : TextPasteboard { + override fun read() = text + override fun write(text: String) { this.text = text } + } + + @Test + fun fr5_a_copy_reaches_the_pasteboard_and_a_paste_reads_it_back() = runBlocking { + val board = FakePasteboard() + val clipboard = MacosClipboard(board) + clipboard.setClipEntry(ClipEntry.withPlainText("안녕 hello")) + assertEquals("안녕 hello", board.text) + assertEquals("안녕 hello", clipboard.getClipEntry()?.getPlainText()) + } + + @Test + fun fr5_a_paste_with_nothing_on_the_pasteboard_finds_nothing() = runBlocking { + val clipboard = MacosClipboard(FakePasteboard()) + assertNull(clipboard.getClipEntry()) + assertFalse(clipboard.hasText(), "Paste is greyed out on an empty pasteboard") + assertTrue(MacosClipboard(FakePasteboard("x")).hasText()) + } + + @Test + fun fr33_6_the_older_manager_sees_the_same_text() { + val board = FakePasteboard() + val manager = MacosClipboardManager(board) + assertFalse(manager.hasText()) + manager.setText(AnnotatedString("copied")) + assertEquals("copied", board.text) + assertEquals("copied", manager.getText()?.text) + assertTrue(manager.hasText()) + } +} 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 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