winit-kora 0.30.13

Cross-platform window creation library. (Playtron's Kora fork of winit)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
//! # Wayland
//!
//! **Note:** Windows don't appear on Wayland until you draw/present to them.
//!
//! By default, Winit loads system libraries using `dlopen`. This can be
//! disabled by disabling the `"wayland-dlopen"` cargo feature.
//!
//! ## Client-side decorations
//!
//! Winit provides client-side decorations by default, but the behaviour can
//! be controlled with the following feature flags:
//!
//! * `wayland-csd-adwaita` (default).
//! * `wayland-csd-adwaita-crossfont`.
//! * `wayland-csd-adwaita-notitle`.

use std::ffi::c_void;
use std::ptr::NonNull;

use crate::event_loop::{ActiveEventLoop, EventLoop, EventLoopBuilder};
use crate::monitor::MonitorHandle;
use crate::window::{Window, WindowAttributes};

pub use crate::window::Theme;

// Re-export popup types for the public API
#[cfg(wayland_platform)]
pub use crate::platform_impl::wayland::types::xdg_popup::{
    PopupAnchor, PopupEvent, PopupGravity, PopupId, PopupSettings,
};

/// Additional methods on [`ActiveEventLoop`] that are specific to Wayland.
pub trait ActiveEventLoopExtWayland {
    /// True if the [`ActiveEventLoop`] uses Wayland.
    fn is_wayland(&self) -> bool;

    /// Create an xdg_popup surface relative to a parent window.
    ///
    /// This creates a proper Wayland popup that can extend outside the parent window bounds
    /// and receives compositor popup semantics (auto-dismiss, input grab, layer stacking).
    ///
    /// Returns the popup ID on success, or None if:
    /// - The [`ActiveEventLoop`] is not using Wayland
    /// - The parent window doesn't exist
    /// - The compositor doesn't support xdg_popup
    #[cfg(wayland_platform)]
    fn create_popup(&self, settings: PopupSettings) -> Option<PopupId>;

    /// Create an xdg_popup parented to another popup, e.g. a submenu.
    ///
    /// `settings.parent_id` must be the toplevel the parent popup belongs to;
    /// `anchor_rect` is relative to the parent popup. A `grab` is only sent when
    /// the parent popup holds the topmost grab, as xdg-shell requires; otherwise
    /// the popup is created without it. xdg-shell also needs the parent mapped,
    /// so draw it before nesting under it.
    ///
    /// Returns None if:
    /// - The [`ActiveEventLoop`] is not using Wayland
    /// - The parent popup doesn't exist or isn't configured yet
    /// - `settings.parent_id` isn't the parent popup's toplevel
    /// - The compositor doesn't support xdg_popup
    #[cfg(wayland_platform)]
    fn create_child_popup(&self, parent_popup: PopupId, settings: PopupSettings)
        -> Option<PopupId>;

    /// Destroy a popup surface.
    ///
    /// Returns true if the popup was one winit still knows about. Child popups
    /// still alive are destroyed first and reported as [`PopupEvent::Done`],
    /// since xdg-shell forbids destroying a popup under a live child. A popup
    /// the compositor already dismissed is not destroyed here: it is left to
    /// finish the grace window its `Done` opened, so it outlives this call by
    /// one [`take_popup_events`], and this still answers true.
    ///
    /// [`take_popup_events`]: ActiveEventLoopExtWayland::take_popup_events
    #[cfg(wayland_platform)]
    fn destroy_popup(&self, popup_id: PopupId) -> bool;

    /// Get the wl_surface for a popup (for rendering).
    ///
    /// Returns a raw pointer to the wl_surface that can be used for attaching
    /// graphical content. The popup must have been configured first.
    #[cfg(wayland_platform)]
    fn popup_wl_surface(&self, popup_id: PopupId) -> Option<NonNull<c_void>>;

    /// Get raw handles for a popup surface.
    /// Returns (surface_ptr, display_ptr) for creating a wgpu surface.
    #[cfg(wayland_platform)]
    fn popup_raw_handles(&self, popup_id: PopupId) -> Option<(NonNull<c_void>, NonNull<c_void>)>;

    /// Resize a popup surface.
    ///
    /// Updates the popup's surface size and viewport. The compositor will be
    /// notified via a surface commit. Returns true if the popup was found.
    #[cfg(wayland_platform)]
    fn resize_popup(&self, popup_id: PopupId, width: u32, height: u32) -> bool;

    /// Get pending popup events and clear the queue.
    ///
    /// Popups the compositor dismissed stay alive until the take after the one
    /// that returned their [`PopupEvent::Done`], so stop drawing to them before
    /// taking again; take once per event loop iteration.
    ///
    /// A popup the compositor gives keyboard focus, like a grabbing menu,
    /// reports its focus, modifiers and keys as [`PopupEvent::Window`].
    #[cfg(wayland_platform)]
    fn take_popup_events(&self) -> Vec<PopupEvent>;
}

impl ActiveEventLoopExtWayland for ActiveEventLoop {
    #[inline]
    fn is_wayland(&self) -> bool {
        self.p.is_wayland()
    }

    #[cfg(wayland_platform)]
    fn create_popup(&self, settings: PopupSettings) -> Option<PopupId> {
        match &self.p {
            crate::platform_impl::ActiveEventLoop::Wayland(w) => w.create_popup(settings),
            #[cfg(x11_platform)]
            _ => None,
        }
    }

    #[cfg(wayland_platform)]
    fn create_child_popup(
        &self,
        parent_popup: PopupId,
        settings: PopupSettings,
    ) -> Option<PopupId> {
        match &self.p {
            crate::platform_impl::ActiveEventLoop::Wayland(w) => {
                w.create_child_popup(parent_popup, settings)
            },
            #[cfg(x11_platform)]
            _ => None,
        }
    }

    #[cfg(wayland_platform)]
    fn destroy_popup(&self, popup_id: PopupId) -> bool {
        match &self.p {
            crate::platform_impl::ActiveEventLoop::Wayland(w) => w.destroy_popup(popup_id),
            #[cfg(x11_platform)]
            _ => false,
        }
    }

    #[cfg(wayland_platform)]
    fn popup_wl_surface(&self, popup_id: PopupId) -> Option<NonNull<c_void>> {
        match &self.p {
            crate::platform_impl::ActiveEventLoop::Wayland(w) => w.popup_wl_surface(popup_id),
            #[cfg(x11_platform)]
            _ => None,
        }
    }

    #[cfg(wayland_platform)]
    fn popup_raw_handles(&self, popup_id: PopupId) -> Option<(NonNull<c_void>, NonNull<c_void>)> {
        match &self.p {
            crate::platform_impl::ActiveEventLoop::Wayland(w) => w.popup_raw_handles(popup_id),
            #[cfg(x11_platform)]
            _ => None,
        }
    }

    #[cfg(wayland_platform)]
    fn resize_popup(&self, popup_id: PopupId, width: u32, height: u32) -> bool {
        match &self.p {
            crate::platform_impl::ActiveEventLoop::Wayland(w) => {
                w.resize_popup(popup_id, width, height)
            },
            #[cfg(x11_platform)]
            _ => false,
        }
    }

    #[cfg(wayland_platform)]
    fn take_popup_events(&self) -> Vec<PopupEvent> {
        match &self.p {
            crate::platform_impl::ActiveEventLoop::Wayland(w) => w.take_popup_events(),
            #[cfg(x11_platform)]
            _ => Vec::new(),
        }
    }
}

/// Additional methods on [`EventLoop`] that are specific to Wayland.
pub trait EventLoopExtWayland {
    /// True if the [`EventLoop`] uses Wayland.
    fn is_wayland(&self) -> bool;
}

impl<T: 'static> EventLoopExtWayland for EventLoop<T> {
    #[inline]
    fn is_wayland(&self) -> bool {
        self.event_loop.is_wayland()
    }
}

/// Additional methods on [`EventLoopBuilder`] that are specific to Wayland.
pub trait EventLoopBuilderExtWayland {
    /// Force using Wayland.
    fn with_wayland(&mut self) -> &mut Self;

    /// Whether to allow the event loop to be created off of the main thread.
    ///
    /// By default, the window is only allowed to be created on the main
    /// thread, to make platform compatibility easier.
    fn with_any_thread(&mut self, any_thread: bool) -> &mut Self;
}

impl<T> EventLoopBuilderExtWayland for EventLoopBuilder<T> {
    #[inline]
    fn with_wayland(&mut self) -> &mut Self {
        self.platform_specific.forced_backend = Some(crate::platform_impl::Backend::Wayland);
        self
    }

    #[inline]
    fn with_any_thread(&mut self, any_thread: bool) -> &mut Self {
        self.platform_specific.any_thread = any_thread;
        self
    }
}

/// Additional methods on [`Window`] that are specific to Wayland.
///
/// [`Window`]: crate::window::Window
pub trait WindowExtWayland {
    /// Returns `xdg_toplevel` of the window or [`None`] if the window is X11 window.
    fn xdg_toplevel(&self) -> Option<NonNull<c_void>>;

    /// Returns `wl_surface` of the window or [`None`] if the window is X11 window.
    ///
    /// The returned pointer is to the wayland-client `wl_surface` object.
    /// This can be used for protocols that need direct access to the surface,
    /// such as surface embedding protocols.
    fn wl_surface(&self) -> Option<NonNull<c_void>>;

    /// Request an animated resize to the target size using the COSMIC protocol.
    ///
    /// This uses the `zcosmic_animated_resize_v1` protocol to request smooth
    /// compositor-driven resize animation. The compositor will send intermediate
    /// configure events for smooth animation.
    ///
    /// Returns `true` if the request was sent, `false` if:
    /// - The window is not a Wayland window
    /// - The compositor doesn't support the animated resize protocol
    ///
    /// # Arguments
    /// * `width` - Target width in logical pixels
    /// * `height` - Target height in logical pixels
    /// * `duration_ms` - Animation duration in milliseconds
    fn request_animated_resize(&self, width: i32, height: i32, duration_ms: u32) -> bool;

    /// Request an animated resize with explicit position using the COSMIC protocol.
    ///
    /// This uses the compositor's animated_resize protocol to smoothly animate
    /// the window from its current geometry to the target geometry.
    ///
    /// If the window is maximized, the position and size will be stored and used
    /// when the window is restored to normal state.
    ///
    /// Returns `true` if the request was sent, `false` if:
    /// - The window is not a Wayland window
    /// - The compositor doesn't support the animated resize protocol
    ///
    /// # Arguments
    /// * `x` - Target x position in logical pixels
    /// * `y` - Target y position in logical pixels
    /// * `width` - Target width in logical pixels
    /// * `height` - Target height in logical pixels
    /// * `duration_ms` - Animation duration in milliseconds
    fn request_animated_resize_with_position(
        &self,
        x: i32,
        y: i32,
        width: i32,
        height: i32,
        duration_ms: u32,
    ) -> bool;

    /// Set corner radius for this window using the COSMIC protocol.
    ///
    /// Communicates the corner radius hint to the compositor so it can
    /// draw proper blur outlines and apply rounded corners to the window.
    ///
    /// Returns `true` if the request was sent, `false` if:
    /// - The window is not a Wayland window
    /// - The compositor doesn't support the corner radius protocol
    ///
    /// # Arguments
    /// * `top_left` - Top-left corner radius in logical pixels
    /// * `top_right` - Top-right corner radius in logical pixels
    /// * `bottom_right` - Bottom-right corner radius in logical pixels
    /// * `bottom_left` - Bottom-left corner radius in logical pixels
    fn set_corner_radius(
        &self,
        top_left: u32,
        top_right: u32,
        bottom_right: u32,
        bottom_left: u32,
    ) -> bool;

    /// Set the compositor-rendered backdrop color for this window.
    ///
    /// The compositor will render a colored rectangle behind the window content,
    /// using the window's corner radius. RGBA components are in the range 0-255.
    ///
    /// Returns `false` if:
    /// - The window is not a Wayland window
    /// - The compositor doesn't support the backdrop color protocol
    fn set_backdrop_color(&self, r: u32, g: u32, b: u32, a: u32) -> bool;

    /// Embed a toplevel by process ID into this window's surface.
    ///
    /// This uses the `zcosmic_surface_embed_manager_v1` protocol to embed a
    /// foreign toplevel window within this window's surface. The compositor
    /// will monitor for new toplevels from the specified PID and embed the
    /// first matching one.
    ///
    /// Returns an embed ID that can be used to update geometry or remove the
    /// embed, or `None` if:
    /// - The window is not a Wayland window
    /// - The compositor doesn't support the surface embed protocol
    ///
    /// # Arguments
    /// * `pid` - Process ID of the application to embed
    /// * `app_id` - Optional app_id hint for verification (can be empty)
    /// * `x` - X position within this window's surface
    /// * `y` - Y position within this window's surface
    /// * `width` - Width of the embed region
    /// * `height` - Height of the embed region
    /// * `interactive` - Whether input should be routed to the embedded surface
    // Arity follows the protocol request; a struct would obscure it.
    #[allow(clippy::too_many_arguments)]
    fn embed_toplevel_by_pid(
        &self,
        pid: u32,
        app_id: &str,
        x: i32,
        y: i32,
        width: i32,
        height: i32,
        interactive: bool,
    ) -> Option<u64>;

    /// Update the geometry of an embedded surface.
    ///
    /// # Arguments
    /// * `embed_id` - The ID returned from `embed_toplevel_by_pid`
    /// * `x` - X position within this window's surface
    /// * `y` - Y position within this window's surface
    /// * `width` - Width of the embed region
    /// * `height` - Height of the embed region
    fn set_embed_geometry(&self, embed_id: u64, x: i32, y: i32, width: i32, height: i32) -> bool;

    /// Set anchor-based positioning for an embedded surface.
    ///
    /// Instead of specifying absolute (x, y) coordinates, this allows positioning
    /// relative to the parent window edges. The geometry is automatically
    /// recalculated by the compositor when the parent window resizes.
    ///
    /// # Arguments
    /// * `embed_id` - The ID returned from `embed_toplevel_by_pid`
    /// * `anchor` - Bitflags indicating which edges to anchor to (0=none, 1=top, 2=bottom, 4=left,
    ///   8=right)
    /// * `margin_top` - Margin from top edge
    /// * `margin_right` - Margin from right edge
    /// * `margin_bottom` - Margin from bottom edge
    /// * `margin_left` - Margin from left edge
    /// * `width` - Width of embed region (0 to stretch between left/right anchors)
    /// * `height` - Height of embed region (0 to stretch between top/bottom anchors)
    // Arity follows the protocol request; a struct would obscure it.
    #[allow(clippy::too_many_arguments)]
    fn set_embed_anchor(
        &self,
        embed_id: u64,
        anchor: u32,
        margin_top: i32,
        margin_right: i32,
        margin_bottom: i32,
        margin_left: i32,
        width: i32,
        height: i32,
    ) -> bool;

    /// Set corner radius for an embedded surface.
    ///
    /// This allows the parent to specify rounded corners that match its own UI.
    /// Each corner can have a different radius. Values are in logical pixels.
    /// A value of 0 means no rounding for that corner.
    ///
    /// # Arguments
    /// * `embed_id` - The ID returned from `embed_toplevel_by_pid`
    /// * `top_left` - Top-left corner radius
    /// * `top_right` - Top-right corner radius
    /// * `bottom_right` - Bottom-right corner radius
    /// * `bottom_left` - Bottom-left corner radius
    fn set_embed_corner_radius(
        &self,
        embed_id: u64,
        top_left: u32,
        top_right: u32,
        bottom_right: u32,
        bottom_left: u32,
    ) -> bool;

    /// Set interactivity for an embedded surface.
    ///
    /// When interactive, pointer/keyboard/touch events within the embed
    /// region will be routed to the embedded toplevel.
    fn set_embed_interactive(&self, embed_id: u64, interactive: bool) -> bool;

    /// Remove an embedded surface.
    fn remove_embed(&self, embed_id: u64) -> bool;

    /// Register this window to receive the device's special key.
    ///
    /// The compositor owns the gesture — the key is usually a modifier it also
    /// needs for its own chords — and sends the resolved meaning as
    /// `WindowEvent::SpecialAction`: a tap asks the window to focus its input,
    /// a hold brackets push-to-talk.
    ///
    /// When `is_default` the window also becomes the fallback receiver, used
    /// whenever no registered surface is focused. Only one fallback exists at a
    /// time; registering a second replaces the first.
    ///
    /// Returns `false` if this is not a Wayland window, or the compositor does
    /// not implement `zcosmic_special_action_v1`.
    fn register_special_action(&self, is_default: bool) -> bool;

    /// Stop receiving the special key.
    fn unregister_special_action(&self) -> bool;

    /// Whether Kora's Halo has a drag strip over this window's top edge, so the
    /// window must keep its own controls clear of it.
    ///
    /// `true` only when the window was created with
    /// [`WindowAttributesExtWayland::with_halo_header_overlay`] and the
    /// compositor offers `kora_halo_header_manager_v1`. The answer is final at
    /// creation: the protocol takes the mode only before the initial commit and
    /// keeps it for the surface's lifetime, and window creation round-trips past
    /// that commit, so a rejected request fails creation instead.
    ///
    /// Always `false` on X11.
    fn is_halo_header_overlay(&self) -> bool;

    /// The complete compositor-authenticated identity for this mapped window.
    ///
    /// Returns `None` before the identity arrives, after revocation, on X11, or
    /// when the compositor lacks `kora_toplevel_identity_v1`. Changes are also
    /// delivered through [`crate::event::WindowEvent::IdentityChanged`].
    fn kora_identity(&self) -> Option<crate::window::Identity>;

    /// Publish this window's commands and recent items to the shell, replacing
    /// what it published before.
    ///
    /// The shell lists them in its own menus and sends back
    /// [`crate::event::WindowEvent::AppCommand`] when one is picked. A no-op on X11
    /// and on compositors without `kora_app_commands_v1`.
    fn set_app_commands(&self, commands: &crate::window::AppCommands);

    /// Ask the shell to open its command palette for this window, as if from the
    /// latest key press it received. The shell ignores it unless this window has
    /// keyboard focus.
    fn request_app_palette(&self);

    /// Start a Wayland drag-and-drop operation from this window.
    ///
    /// Creates a `wl_data_source`, offers the given MIME types, and calls
    /// `wl_data_device.start_drag(…)`. The `data` vector should parallel
    /// `mime_types` — entry `i` is the pre-serialized data for
    /// `mime_types[i]`.
    ///
    /// `actions` is a bitfield of supported DnD actions:
    /// `1` = copy, `2` = move, `4` = ask.
    ///
    /// `icon` is optional pre-rendered icon pixel data as
    /// `(width, height, argb_pixels, buffer_scale)` in pre-multiplied ARGB format.
    /// The `buffer_scale` is used for HiDPI support (set to 2 for 2x rendering).
    /// If `None`, a default generic icon is used.
    ///
    /// Returns `(true, mime_types, data)` if the drag was started, or
    /// `(false, mime_types, data)` if it could not be started (e.g. not
    /// Wayland, no pointer serial, protocol unavailable).
    fn start_drag(
        &self,
        mime_types: Vec<String>,
        actions: u32,
        data: Vec<Vec<u8>>,
        icon: Option<(u32, u32, Vec<u8>, i32)>,
    ) -> (bool, Vec<String>, Vec<Vec<u8>>);

    /// Accept a MIME type from the current DnD offer.
    ///
    /// Call this from `DndWindowEvent::Enter` or `Motion` handlers to signal
    /// that you can accept the drag with the specified MIME type.
    /// Pass `None` to reject the current drag.
    ///
    /// This must be called on every `Motion` event to continue accepting.
    fn dnd_accept_mime_type(&self, mime_type: Option<&str>);

    /// Set the accepted DnD actions and preferred action.
    ///
    /// `actions` is a bitfield of acceptable actions (1=copy, 2=move, 4=ask).
    /// `preferred` is the single preferred action from that set.
    fn dnd_set_actions(&self, actions: u32, preferred: u32);

    /// Signal that the destination has finished processing the drop.
    ///
    /// Call this after processing `DndWindowEvent::Drop` to tell the source
    /// the transfer is complete. Required for the drag to finalize properly.
    fn dnd_finish(&self);

    /// Request data from the current DnD offer for the specified MIME type.
    ///
    /// Call this after receiving `DndWindowEvent::Drop` to retrieve the actual
    /// data from the drag source. The data will be delivered via a
    /// `DndWindowEvent::DataReceived` event.
    ///
    /// # Arguments
    /// * `mime_type` - The MIME type to request (must be one of the types offered in the
    ///   `DndWindowEvent::Enter` event).
    fn dnd_request_data(&self, mime_type: &str);

    /// Inhibit (or release) the compositor's global keyboard shortcuts for this
    /// window.
    ///
    /// While inhibited, the compositor delivers **all** key events — including
    /// its own reserved combos such as `Super` — to this window instead of
    /// handling them as global shortcuts. This is what a key-capture /
    /// shortcut-recording UI needs; release it as soon as capture ends.
    ///
    /// No-op on X11 (which already delivers all keys to the focused window) or
    /// if the compositor lacks `zwp_keyboard_shortcuts_inhibit_manager_v1`.
    fn set_keyboard_shortcuts_inhibit(&self, inhibit: bool);

    /// Tell input methods the text around the caret of the focused field, so what
    /// they insert, such as dictated words, can be spaced and cased to fit.
    ///
    /// Call it again whenever the text, caret or selection changes. Pass `None` for
    /// a field that keeps its text to itself: the text input is then enabled again,
    /// which makes input methods forget what they were told. Nothing is sent while
    /// the IME purpose is [`ImePurpose::Password`](crate::window::ImePurpose::Password),
    /// and text past the protocol's 4000-byte limit is cut to a window around the
    /// caret and selection.
    ///
    /// A no-op on X11 and on compositors without `zwp_text_input_manager_v3`.
    fn set_ime_surrounding_text(&self, surrounding: Option<&ImeSurroundingText>);
}

impl WindowExtWayland for Window {
    #[inline]
    fn xdg_toplevel(&self) -> Option<NonNull<c_void>> {
        #[allow(clippy::single_match)]
        match &self.window {
            #[cfg(x11_platform)]
            crate::platform_impl::Window::X(_) => None,
            #[cfg(wayland_platform)]
            crate::platform_impl::Window::Wayland(window) => window.xdg_toplevel(),
        }
    }

    #[inline]
    fn wl_surface(&self) -> Option<NonNull<c_void>> {
        #[allow(clippy::single_match)]
        match &self.window {
            #[cfg(x11_platform)]
            crate::platform_impl::Window::X(_) => None,
            #[cfg(wayland_platform)]
            crate::platform_impl::Window::Wayland(window) => window.wl_surface_ptr(),
        }
    }

    #[inline]
    fn request_animated_resize(&self, width: i32, height: i32, duration_ms: u32) -> bool {
        match &self.window {
            #[cfg(x11_platform)]
            crate::platform_impl::Window::X(_) => false,
            #[cfg(wayland_platform)]
            crate::platform_impl::Window::Wayland(window) => {
                window.request_animated_resize(width, height, duration_ms)
            },
        }
    }

    #[inline]
    fn request_animated_resize_with_position(
        &self,
        x: i32,
        y: i32,
        width: i32,
        height: i32,
        duration_ms: u32,
    ) -> bool {
        match &self.window {
            #[cfg(x11_platform)]
            crate::platform_impl::Window::X(_) => false,
            #[cfg(wayland_platform)]
            crate::platform_impl::Window::Wayland(window) => {
                window.request_animated_resize_with_position(x, y, width, height, duration_ms)
            },
        }
    }

    #[inline]
    fn embed_toplevel_by_pid(
        &self,
        pid: u32,
        app_id: &str,
        x: i32,
        y: i32,
        width: i32,
        height: i32,
        interactive: bool,
    ) -> Option<u64> {
        match &self.window {
            #[cfg(x11_platform)]
            crate::platform_impl::Window::X(_) => None,
            #[cfg(wayland_platform)]
            crate::platform_impl::Window::Wayland(window) => {
                window.embed_toplevel_by_pid(pid, app_id, x, y, width, height, interactive)
            },
        }
    }

    #[inline]
    fn set_embed_geometry(&self, embed_id: u64, x: i32, y: i32, width: i32, height: i32) -> bool {
        match &self.window {
            #[cfg(x11_platform)]
            crate::platform_impl::Window::X(_) => false,
            #[cfg(wayland_platform)]
            crate::platform_impl::Window::Wayland(window) => {
                window.set_embed_geometry(embed_id, x, y, width, height)
            },
        }
    }

    #[inline]
    fn set_embed_anchor(
        &self,
        embed_id: u64,
        anchor: u32,
        margin_top: i32,
        margin_right: i32,
        margin_bottom: i32,
        margin_left: i32,
        width: i32,
        height: i32,
    ) -> bool {
        match &self.window {
            #[cfg(x11_platform)]
            crate::platform_impl::Window::X(_) => false,
            #[cfg(wayland_platform)]
            crate::platform_impl::Window::Wayland(window) => window.set_embed_anchor(
                embed_id,
                anchor,
                margin_top,
                margin_right,
                margin_bottom,
                margin_left,
                width,
                height,
            ),
        }
    }

    #[inline]
    fn set_embed_corner_radius(
        &self,
        embed_id: u64,
        top_left: u32,
        top_right: u32,
        bottom_right: u32,
        bottom_left: u32,
    ) -> bool {
        match &self.window {
            #[cfg(x11_platform)]
            crate::platform_impl::Window::X(_) => false,
            #[cfg(wayland_platform)]
            crate::platform_impl::Window::Wayland(window) => window.set_embed_corner_radius(
                embed_id,
                top_left,
                top_right,
                bottom_right,
                bottom_left,
            ),
        }
    }

    #[inline]
    fn set_embed_interactive(&self, embed_id: u64, interactive: bool) -> bool {
        match &self.window {
            #[cfg(x11_platform)]
            crate::platform_impl::Window::X(_) => false,
            #[cfg(wayland_platform)]
            crate::platform_impl::Window::Wayland(window) => {
                window.set_embed_interactive(embed_id, interactive)
            },
        }
    }

    #[inline]
    fn remove_embed(&self, embed_id: u64) -> bool {
        match &self.window {
            #[cfg(x11_platform)]
            crate::platform_impl::Window::X(_) => false,
            #[cfg(wayland_platform)]
            crate::platform_impl::Window::Wayland(window) => window.remove_embed(embed_id),
        }
    }

    #[inline]
    fn set_corner_radius(
        &self,
        top_left: u32,
        top_right: u32,
        bottom_right: u32,
        bottom_left: u32,
    ) -> bool {
        match &self.window {
            #[cfg(x11_platform)]
            crate::platform_impl::Window::X(_) => false,
            #[cfg(wayland_platform)]
            crate::platform_impl::Window::Wayland(window) => {
                window.set_corner_radius(top_left, top_right, bottom_right, bottom_left)
            },
        }
    }

    #[inline]
    fn set_backdrop_color(&self, r: u32, g: u32, b: u32, a: u32) -> bool {
        match &self.window {
            #[cfg(x11_platform)]
            crate::platform_impl::Window::X(_) => false,
            #[cfg(wayland_platform)]
            crate::platform_impl::Window::Wayland(window) => window.set_backdrop_color(r, g, b, a),
        }
    }

    #[inline]
    fn register_special_action(&self, is_default: bool) -> bool {
        match &self.window {
            #[cfg(x11_platform)]
            crate::platform_impl::Window::X(_) => false,
            #[cfg(wayland_platform)]
            crate::platform_impl::Window::Wayland(window) => {
                window.register_special_action(is_default)
            },
        }
    }

    #[inline]
    fn unregister_special_action(&self) -> bool {
        match &self.window {
            #[cfg(x11_platform)]
            crate::platform_impl::Window::X(_) => false,
            #[cfg(wayland_platform)]
            crate::platform_impl::Window::Wayland(window) => window.unregister_special_action(),
        }
    }

    #[inline]
    fn is_halo_header_overlay(&self) -> bool {
        match &self.window {
            #[cfg(x11_platform)]
            crate::platform_impl::Window::X(_) => false,
            #[cfg(wayland_platform)]
            crate::platform_impl::Window::Wayland(window) => window.is_halo_header_overlay(),
        }
    }

    #[inline]
    fn kora_identity(&self) -> Option<crate::window::Identity> {
        match &self.window {
            #[cfg(x11_platform)]
            crate::platform_impl::Window::X(_) => None,
            #[cfg(wayland_platform)]
            crate::platform_impl::Window::Wayland(window) => window.kora_identity(),
        }
    }

    #[inline]
    fn set_app_commands(&self, commands: &crate::window::AppCommands) {
        match &self.window {
            #[cfg(x11_platform)]
            crate::platform_impl::Window::X(_) => {
                let _ = commands;
            },
            #[cfg(wayland_platform)]
            crate::platform_impl::Window::Wayland(window) => window.set_app_commands(commands),
        }
    }

    #[inline]
    fn request_app_palette(&self) {
        match &self.window {
            #[cfg(x11_platform)]
            crate::platform_impl::Window::X(_) => (),
            #[cfg(wayland_platform)]
            crate::platform_impl::Window::Wayland(window) => window.request_app_palette(),
        }
    }

    #[inline]
    fn start_drag(
        &self,
        mime_types: Vec<String>,
        actions: u32,
        data: Vec<Vec<u8>>,
        icon: Option<(u32, u32, Vec<u8>, i32)>,
    ) -> (bool, Vec<String>, Vec<Vec<u8>>) {
        match &self.window {
            #[cfg(x11_platform)]
            crate::platform_impl::Window::X(_) => (false, mime_types, data),
            #[cfg(wayland_platform)]
            crate::platform_impl::Window::Wayland(window) => {
                window.start_drag(mime_types, actions, data, icon)
            },
        }
    }

    #[inline]
    fn dnd_accept_mime_type(&self, mime_type: Option<&str>) {
        match &self.window {
            #[cfg(x11_platform)]
            crate::platform_impl::Window::X(_) => {},
            #[cfg(wayland_platform)]
            crate::platform_impl::Window::Wayland(window) => {
                window.dnd_accept_mime_type(mime_type);
            },
        }
    }

    #[inline]
    fn dnd_set_actions(&self, actions: u32, preferred: u32) {
        match &self.window {
            #[cfg(x11_platform)]
            crate::platform_impl::Window::X(_) => {},
            #[cfg(wayland_platform)]
            crate::platform_impl::Window::Wayland(window) => {
                window.dnd_set_actions(actions, preferred);
            },
        }
    }

    #[inline]
    fn dnd_finish(&self) {
        match &self.window {
            #[cfg(x11_platform)]
            crate::platform_impl::Window::X(_) => {},
            #[cfg(wayland_platform)]
            crate::platform_impl::Window::Wayland(window) => {
                window.dnd_finish();
            },
        }
    }

    #[inline]
    fn dnd_request_data(&self, mime_type: &str) {
        match &self.window {
            #[cfg(x11_platform)]
            crate::platform_impl::Window::X(_) => {},
            #[cfg(wayland_platform)]
            crate::platform_impl::Window::Wayland(window) => {
                window.dnd_request_data(mime_type);
            },
        }
    }

    #[inline]
    fn set_keyboard_shortcuts_inhibit(&self, inhibit: bool) {
        match &self.window {
            #[cfg(x11_platform)]
            crate::platform_impl::Window::X(_) => {},
            #[cfg(wayland_platform)]
            crate::platform_impl::Window::Wayland(window) => {
                window.set_keyboard_shortcuts_inhibit(inhibit);
            },
        }
    }

    #[inline]
    fn set_ime_surrounding_text(&self, surrounding: Option<&ImeSurroundingText>) {
        match &self.window {
            #[cfg(x11_platform)]
            crate::platform_impl::Window::X(_) => {
                let _ = surrounding;
            },
            #[cfg(wayland_platform)]
            crate::platform_impl::Window::Wayland(window) => {
                window.set_ime_surrounding_text(surrounding);
            },
        }
    }
}

/// The text around the caret of the focused field, for
/// [`WindowExtWayland::set_ime_surrounding_text`].
#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
pub struct ImeSurroundingText {
    /// The field's text, or the part of it around the caret and selection.
    pub text: String,
    /// The byte offset of the caret in `text`.
    pub cursor: usize,
    /// The byte offset of the selection's other end in `text`, or `cursor` when
    /// nothing is selected.
    pub anchor: usize,
}

/// Additional methods on [`WindowAttributes`] that are specific to Wayland.
pub trait WindowAttributesExtWayland {
    /// Build window with the given name.
    ///
    /// The `general` name sets an application ID, which should match the `.desktop`
    /// file distributed with your program. The `instance` is a `no-op`.
    ///
    /// For details about application ID conventions, see the
    /// [Desktop Entry Spec](https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#desktop-file-id)
    fn with_name(self, general: impl Into<String>, instance: impl Into<String>) -> Self;

    /// Parent this window to a foreign toplevel exported by another client.
    ///
    /// `handle` is an xdg-foreign handle (the bare handle string, without any
    /// `wayland:` prefix) obtained by the other client via `zxdg_exporter_v2` —
    /// for example the `parent_window` an xdg-desktop-portal `FileChooser`
    /// backend receives. The window is imported via `zxdg_importer_v2` and set
    /// as a child of that toplevel, so the compositor places it as a dialog over
    /// the requesting application's window. No-op if the compositor lacks
    /// `zxdg_importer_v2`.
    fn with_wayland_parent(self, handle: impl Into<String>) -> Self;

    /// Give Kora's Halo a drag strip over this window's top edge
    /// (`kora_halo_header_manager_v1`, overlay mode). The Halo itself always
    /// floats above the window, outside its geometry; without this nothing of
    /// it reaches into the window.
    ///
    /// For a window laid out chromeless: its content is full-bleed and its
    /// first row already sits clear of the strip. The mode is sent before the window's initial
    /// commit, which is the only time the protocol accepts it. No-op if the compositor lacks
    /// the global; [`WindowExtWayland::is_halo_header_overlay`] tells which.
    fn with_halo_header_overlay(self, overlay: bool) -> Self;
}

impl WindowAttributesExtWayland for WindowAttributes {
    #[inline]
    fn with_name(mut self, general: impl Into<String>, instance: impl Into<String>) -> Self {
        self.platform_specific.name =
            Some(crate::platform_impl::ApplicationName::new(general.into(), instance.into()));
        self
    }

    #[inline]
    fn with_wayland_parent(mut self, handle: impl Into<String>) -> Self {
        self.platform_specific.wayland_parent = Some(handle.into());
        self
    }

    #[inline]
    fn with_halo_header_overlay(mut self, overlay: bool) -> Self {
        self.platform_specific.halo_header_overlay = overlay;
        self
    }
}

/// Additional methods on `MonitorHandle` that are specific to Wayland.
pub trait MonitorHandleExtWayland {
    /// Returns the inner identifier of the monitor.
    fn native_id(&self) -> u32;
}

impl MonitorHandleExtWayland for MonitorHandle {
    #[inline]
    fn native_id(&self) -> u32 {
        self.inner.native_identifier()
    }
}

#[cfg(test)]
mod tests {
    use super::WindowAttributesExtWayland;
    use crate::window::WindowAttributes;

    #[test]
    fn halo_header_overlay_is_opt_in() {
        let attributes = WindowAttributes::default();
        assert!(!attributes.platform_specific.halo_header_overlay);
        let attributes = attributes.with_halo_header_overlay(true);
        assert!(attributes.platform_specific.halo_header_overlay);
        let attributes = attributes.with_halo_header_overlay(false);
        assert!(!attributes.platform_specific.halo_header_overlay);
    }
}