kotlin-desktop-toolkit / desktop-win32
AGENTS.md201 starsChanged 6 months agoApache-2.0View on GitHub
You are reading this because you've been pointed at the native/desktop-win32 crate (or its Kotlin counterpart at kotlin-desktop-toolkit/src/main/kotlin/org/jetbrains/desktop/win32/) and need to navigate, modify, or debug it. Read this file first; it indexes the rest. Windows backend of the kotlin-desktop-toolkit. Rust crate (cdylib) exposing a flat C ABI via cbindgen, consumed from Kotlin via JExtract-generated bindings plus hand-written wrappers. UI runs on a single thread (OleInitialize STA) with a classic GetMessage pump in event_loop.rs. Composition uses WinRT Windows.UI.Composition via ICompositorDesktopInterop —…
Where files are headingNew AGENTS.md files have more rules about keeping secrets out of the code, and fewer about checking a change actually works.
Of the AGENTS.md files we read, the 11,773 first seen in the last six months, against the older ones: the kind of rule that gained most share, and the kind that lost most. About files like this in general, not this one, and an estimate, since a kind is a pattern in the words.
The file
We split this AGENTS.md into the rules it gives an agent
3 rules. About 0 are common, 3 are distinctive, and 0 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈3
Distinctive · estimate
This author's own choices. Worth a look.
0
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 18
If you only read one section before touching code, read this one.
- Distinctive: line 41
Confirm interpretation when offered A vs B: When asked to choose between two approaches, explicitly state which one you're adopting and why.
- Distinctive: line 58
Don't let any of these docs claim something the code doesn't do.
≈ “Common” is an estimate: at least 1 in 5 of the 16,595 AGENTS.md files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Agent orientation: desktop-win32
You are reading this because you've been pointed at the native/desktop-win32 crate (or its Kotlin counterpart at kotlin-desktop-toolkit/src/main/kotlin/org/jetbrains/desktop/win32/) and need to navigate, modify, or debug it. Read this file first; it indexes the rest.
What this crate is
Windows backend of the kotlin-desktop-toolkit. Rust crate (cdylib) exposing a flat C ABI via cbindgen, consumed from Kotlin via JExtract-generated bindings plus hand-written wrappers. UI runs on a single thread (OleInitialize STA) with a classic GetMessage pump in event_loop.rs. Composition uses WinRT Windows.UI.Composition via ICompositorDesktopInterop — not DirectComposition. Rendering is ANGLE-on-D3D11.
Start here
ARCHITECTURE.md— module layout, FFI pipeline, threading and ownership models, error channel, headline data flows.FFI_CONVENTIONS.md— the*_api.rs↔ Kotlin contract; pointer/array/option type zoo;ffiDownCallscoping rule; COM lifecycle.SUBSYSTEMS.md— per-subsystem reference. Look up the area you're touching.TODO.md— confirmed bugs, capability gaps, smells, open design questions. Cross-reference before claiming something is "broken" or "missing".
Top things that surprise
If you only read one section before touching code, read this one.
- This is the Win32-first backend. Default to Win32 APIs (
CreateWindowExW,GetMessageW,RegisterDragDrop,IFileOpenDialog, …). Use WinRT only when there's a documented reason — there are exactly four such subsystems today, each justified inARCHITECTURE.md→ Scope. Never propose WinUI 3 or Windows App SDK (Microsoft.UI.*,Microsoft.WindowsAppSDK) APIs in this crate. A WinUI 3 backend, if built, lives in a separate crate. - Composition is
Windows.UI.Composition(WinRT), not DirectComposition. They're distinct APIs with different lifetimes and threading. SeeARCHITECTURE.md→ Composition section. - Single UI thread.
OleInitializeSTA,DispatcherQueuewithDQTYPE_THREAD_CURRENT, the wndproc, and most state assume one thread. Cross-thread work goes throughapplication_dispatcher_invoke. Several pieces arethread_local!(key-message stash, exception store). - Error channel is
anyhow::Resultthroughffi_boundary, not return codes. Rust functions returnanyhow::Result<T>;ffi_boundarylogs anyErr, appends the message to thread-localLAST_EXCEPTION_MSGS, and returnsR::default(). Kotlin'sffiDownCallpolls after every call and throwsNativeError. Panic catching insideffi_boundaryis a safety net for unexpected unwinds, not a designed error path — if Rust code panics, treat it as a bug. Background-thread errors are lost (thread-local store). ffiDownCall { ... }must wrap only the native call. NotArena.use, notwithPointer, not helpers (which wrap their own native calls). Wider scopes conflate exception attribution. SeeFFI_CONVENTIONS.md.- Window starts at
1×1and is then resized. Intentional: managed code uses logical pixels but the DPI scale only exists once anHWNDexists (GetDpiForWindow). Consequence: creation emits repeatedWM_WINDOWPOSCHANGEDnotifications; this crate handles that message and returns0, so it does not rely on a downstreamDefWindowProc-generatedWM_SIZEpath. Size/move handlers must be idempotent. - Coordinates are mostly logical, but with deliberate physical-pixel exceptions. Pointer events'
locationOnScreen, several Window events, drag-drop callbacks, andscreen_map_to_clientcarryPhysicalPoint/PhysicalSize. SeeSUBSYSTEMS.md→ Geometry → Exceptions. EnableMouseInPointer(true)is process-wide and irreversible. Anything in the same process expecting rawWM_MOUSE*will silently break.- The
borrowpattern onRustAllocatedRawPtr(ffi_utils.rs) reconstructs and immediately leaks aBoxper call to produce a&R. Sound under the toolkit's single-thread-of-ownership assumption; soundness is by convention. Currently under deferred review.
Watch out for in code reviews / edits
- Kotlin
tryRead*wrappers returnnullonly for format-unavailable and throw other clipboard/data-object failures. Use result-bearing FFI for nullable read semantics. - COM impls have no
// SAFETY:comments anywhere (anddesktop-common::ffi_utilshas a module-level#![allow(clippy::missing_safety_doc)]). Add one when you touch anunsafeblock. - Most clipboard / drag-drop work assumes the OLE STA. There is no thread-affinity assertion at the FFI boundary — the synchronous
ClipboardAPI trusts the caller to stay on the dispatcher thread and to handleDataTransferStatus.Busyretries itself.
Working with the human
The high-impact conventions for collaborating on this crate:
- Outline before executing anything multi-file or API-shaping. Wait for confirmation before reaching for
Edit/Write. - Confirm interpretation when offered A vs B. When asked to choose between two approaches, explicitly state which one you're adopting and why; don't silently compose hybrids.
ffiDownCallscoping: wrap only the native call (notArena.use, notwithPointer, not helpers).- Throwing helper naming:
require*/check*/ensure*for throwing preconditions; reservelet/if*/takeIf*for genuine no-op semantics. - Distinguish documented contract from observed implementation behaviour — never state behaviour observed in source code or experiment as if it were a documented guarantee. Prefix inferred-from-behaviour claims with "implementation-defined" / "in practice" / "per the X implementation". If asked where a claim is documented and you can't cite the source, admit it was inferred — bluffing about the docs erodes trust and leads to design decisions built on false premises.
- Use authoritative-docs lookup skills when your agent platform exposes them. For Win32 / WinRT questions, Claude Code provides
microsoft-docs:microsoft-docs(concept lookup) andmicrosoft-docs:microsoft-code-reference(API / SDK code verification). Prefer these over speculating from internal knowledge — especially when distinguishing documented contract from observed behaviour (above). - Don't add redundant explanatory clauses on top of content that already conveys the same point. Trust the prose to do its job.
- A wrong claim about current state may still be a valid design suggestion. When you (or a reviewer) correct a factual error of yours, check whether the wrong version was nonetheless what the code should be. If so, voice both halves: "I was wrong that X is Y, but X should be Y, and here's why." Don't silently retract — your hallucination may have surfaced a real bug.
- Doc-fix sweeps can introduce new overreach. When tightening a stale doc claim, verify the replacement against every reader the claim now covers — a fix scoped to one function can over-promise for a class of callers that doesn't share the new property. Example: a
Window::get_scalecache fix restated as "chrome / hit-test code never re-syscalls per frame", missing thaton_nchitteststill callsGetSystemMetricsForDpi(SM_CYSIZE)live. Enumerate cached fields explicitly rather than relying on broad "never" claims.
When to update this doc set
- Subsystem refactor (e.g. another
_readercousin, a new_api.rs) → updateSUBSYSTEMS.mdand probablyARCHITECTURE.md. - New FFI primitive in
desktop-common::ffi_utils→ updateFFI_CONVENTIONS.md. - Shipped fix for a
TODO.mdentry → remove the entry (don't leave stale ones). - Found a new bug or capability gap → add to
TODO.md. - The "top surprises" above stop being accurate → update this file.
Don't let any of these docs claim something the code doesn't do. Before recommending an action that names a specific file, line, function, or flag, verify it still exists and behaves as described — docs decay even when carefully maintained.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

