app_window 0.3.3

Cross-platform window library
Documentation
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
// SPDX-License-Identifier: MPL-2.0
#[cfg(target_os = "macos")]
pub(crate) mod macos;
#[cfg(target_arch = "wasm32")]
pub(crate) mod wasm;

#[cfg(target_os = "windows")]
pub(crate) mod windows;

#[cfg(target_os = "linux")]
pub(crate) mod linux;

#[cfg(target_os = "macos")]
pub(crate) use macos as sys;
use std::ffi::c_void;
use std::hash::{Hash, Hasher};

#[cfg(target_arch = "wasm32")]
pub(crate) use wasm as sys;

#[cfg(target_os = "windows")]
pub(crate) use windows as sys;

#[cfg(target_os = "linux")]
pub(crate) use linux as sys;

use crate::application::is_main_thread_running;
use crate::input::Window;
use atomic_float::AtomicF64;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicPtr, Ordering};

/// Mouse button constant for the left mouse button.
///
/// # Examples
///
/// ```
/// # async fn example() {
/// use app_window::input::mouse::{Mouse, MOUSE_BUTTON_LEFT};
///
/// let mouse = Mouse::coalesced().await;
/// let left_pressed = mouse.button_state(MOUSE_BUTTON_LEFT);
/// # }
/// ```
pub const MOUSE_BUTTON_LEFT: u8 = 0;

/// Mouse button constant for the right mouse button.
///
/// # Examples
///
/// ```
/// # async fn example() {
/// use app_window::input::mouse::{Mouse, MOUSE_BUTTON_RIGHT};
///
/// let mouse = Mouse::coalesced().await;
/// let right_pressed = mouse.button_state(MOUSE_BUTTON_RIGHT);
/// # }
/// ```
pub const MOUSE_BUTTON_RIGHT: u8 = 1;

/// Mouse button constant for the middle mouse button (wheel button).
///
/// # Examples
///
/// ```
/// # async fn example() {
/// use app_window::input::mouse::{Mouse, MOUSE_BUTTON_MIDDLE};
///
/// let mouse = Mouse::coalesced().await;
/// let middle_pressed = mouse.button_state(MOUSE_BUTTON_MIDDLE);
/// # }
/// ```
pub const MOUSE_BUTTON_MIDDLE: u8 = 2;

/// Mouse's location within a window, in points.
///
/// The coordinate system has its origin at the upper-left corner of the window.
/// The position is reported in logical points, not physical pixels.
///
/// # Examples
///
/// ```
/// # async fn example() {
/// use app_window::input::mouse::Mouse;
///
/// let mouse = Mouse::coalesced().await;
/// if let Some(location) = mouse.window_pos() {
///     println!("Mouse at ({}, {})", location.pos_x(), location.pos_y());
///     println!("Window size: {}x{}", location.window_width(), location.window_height());
/// }
/// # }
/// ```
#[derive(Debug, Clone, Copy)]
pub struct MouseWindowLocation {
    pos_x: f64,
    pos_y: f64,
    window_width: f64,
    window_height: f64,
    window: Option<Window>,
}

impl MouseWindowLocation {
    fn new(
        pos_x: f64,
        pos_y: f64,
        window_width: f64,
        window_height: f64,
        window: Option<Window>,
    ) -> Self {
        MouseWindowLocation {
            pos_x,
            pos_y,
            window_width,
            window_height,
            window,
        }
    }

    /// Returns the X coordinate of the mouse position within the window.
    ///
    /// The X coordinate is measured from the left edge of the window.
    ///
    /// # Examples
    ///
    /// ```
    /// # async fn example() {
    /// # use app_window::input::mouse::Mouse;
    /// # let mouse = Mouse::coalesced().await;
    /// if let Some(location) = mouse.window_pos() {
    ///     let x = location.pos_x();
    ///     println!("Mouse X: {}", x);
    /// }
    /// # }
    /// ```
    pub fn pos_x(&self) -> f64 {
        self.pos_x
    }

    /// Returns the Y coordinate of the mouse position within the window.
    ///
    /// The Y coordinate is measured from the top edge of the window.
    ///
    /// # Examples
    ///
    /// ```
    /// # async fn example() {
    /// # use app_window::input::mouse::Mouse;
    /// # let mouse = Mouse::coalesced().await;
    /// if let Some(location) = mouse.window_pos() {
    ///     let y = location.pos_y();
    ///     println!("Mouse Y: {}", y);
    /// }
    /// # }
    /// ```
    pub fn pos_y(&self) -> f64 {
        self.pos_y
    }

    /// Returns the width of the window containing the mouse.
    ///
    /// # Examples
    ///
    /// ```
    /// # async fn example() {
    /// # use app_window::input::mouse::Mouse;
    /// # let mouse = Mouse::coalesced().await;
    /// if let Some(location) = mouse.window_pos() {
    ///     let width = location.window_width();
    ///     println!("Window width: {}", width);
    /// }
    /// # }
    /// ```
    pub fn window_width(&self) -> f64 {
        self.window_width
    }

    /// Returns the height of the window containing the mouse.
    ///
    /// # Examples
    ///
    /// ```
    /// # async fn example() {
    /// # use app_window::input::mouse::Mouse;
    /// # let mouse = Mouse::coalesced().await;
    /// if let Some(location) = mouse.window_pos() {
    ///     let height = location.window_height();
    ///     println!("Window height: {}", height);
    /// }
    /// # }
    /// ```
    pub fn window_height(&self) -> f64 {
        self.window_height
    }
}

#[derive(Debug)]
struct Shared {
    window: std::sync::Mutex<Option<MouseWindowLocation>>,

    buttons: [AtomicBool; 255],
    scroll_delta_x: AtomicF64,
    scroll_delta_y: AtomicF64,
    last_window: AtomicPtr<c_void>,
}
impl Shared {
    fn new() -> Self {
        Shared {
            window: std::sync::Mutex::new(None),
            buttons: [const { AtomicBool::new(false) }; 255],
            scroll_delta_x: AtomicF64::new(0.0),
            scroll_delta_y: AtomicF64::new(0.0),
            last_window: AtomicPtr::new(std::ptr::null_mut()),
        }
    }

    fn set_window_location(&self, location: MouseWindowLocation) {
        logwise::debuginternal_sync!(
            "Set mouse window location {location}",
            location = logwise::privacy::LogIt(&location)
        );
        *self.window.lock().unwrap() = Some(location);
        self.last_window.store(
            location.window.map(|e| e.0.as_ptr()).unwrap_or_default(),
            Ordering::Relaxed,
        )
    }
    fn set_key_state(&self, key: u8, down: bool, window: *mut c_void) {
        logwise::debuginternal_sync!("Set mouse key {key} state {down}", key = key, down = down);
        self.buttons[key as usize].store(down, std::sync::atomic::Ordering::Relaxed);
        self.last_window
            .store(window, std::sync::atomic::Ordering::Relaxed);
    }

    fn add_scroll_delta(&self, delta_x: f64, delta_y: f64, window: *mut c_void) {
        logwise::debuginternal_sync!(
            "Add mouse scroll delta {delta_x},{delta_y}",
            delta_x = delta_x,
            delta_y = delta_y
        );
        self.scroll_delta_x
            .fetch_add(delta_x, std::sync::atomic::Ordering::Relaxed);
        self.scroll_delta_y
            .fetch_add(delta_y, std::sync::atomic::Ordering::Relaxed);
        self.last_window
            .store(window, std::sync::atomic::Ordering::Relaxed);
    }
}

/// Provides access to mouse input from all mice on the system.
///
/// This type coalesces input from all connected mice into a single interface.
/// It provides access to:
/// - Mouse position within windows
/// - Button states (left, right, middle, and others)
/// - Accumulated scroll deltas
///
/// # Examples
///
/// ```
/// # async fn example() {
/// use app_window::input::mouse::{Mouse, MOUSE_BUTTON_LEFT};
///
/// let mouse = Mouse::coalesced().await;
///
/// // Check if left button is pressed
/// if mouse.button_state(MOUSE_BUTTON_LEFT) {
///     println!("Left button is pressed");
/// }
///
/// // Get mouse position
/// if let Some(pos) = mouse.window_pos() {
///     println!("Mouse at ({}, {})", pos.pos_x(), pos.pos_y());
/// }
/// # }
/// ```
///
/// # Platform-specific behavior
///
/// On some platforms you must create a window before you can get mouse input.
#[derive(Debug)]
pub struct Mouse {
    shared: Arc<Shared>,
    _sys: sys::PlatformCoalescedMouse,
}

impl Mouse {
    /// Creates a new `Mouse` instance that coalesces input from all mice on the system.
    ///
    /// This is the primary way to create a `Mouse` instance. The returned object
    /// will aggregate input from all connected mice.
    ///
    /// # Examples
    ///
    /// ```
    /// # async fn example() {
    /// use app_window::input::mouse::Mouse;
    ///
    /// let mouse = Mouse::coalesced().await;
    /// // Now you can query mouse state
    /// # }
    /// ```
    pub async fn coalesced() -> Self {
        assert!(
            is_main_thread_running(),
            "Main thread must be started before creating coalesced mouse"
        );
        let shared = Arc::new(Shared::new());
        let coalesced = sys::PlatformCoalescedMouse::new(&shared).await;
        Mouse {
            shared,
            _sys: coalesced,
        }
    }

    #[allow(rustdoc::broken_intra_doc_links)] //references to the platform-specific code
    /**
        Returns the [MouseWindowLocation]

        # Platform specifics

        You may need to create a window first, using APIs in this crate.
    */
    pub fn window_pos(&self) -> Option<MouseWindowLocation> {
        *self.shared.window.lock().unwrap()
    }

    /// Determines if the specified mouse button is currently pressed.
    ///
    /// # Arguments
    ///
    /// * `button` - The button to check. Use constants like [`MOUSE_BUTTON_LEFT`],
    ///   [`MOUSE_BUTTON_RIGHT`], or [`MOUSE_BUTTON_MIDDLE`]. Other button values
    ///   (e.g., for mice with additional buttons) may be supported on a best-effort basis.
    ///
    /// # Returns
    ///
    /// `true` if the button is currently pressed, `false` otherwise.
    ///
    /// # Examples
    ///
    /// ```
    /// # async fn example() {
    /// use app_window::input::mouse::{Mouse, MOUSE_BUTTON_LEFT, MOUSE_BUTTON_RIGHT};
    ///
    /// let mouse = Mouse::coalesced().await;
    ///
    /// if mouse.button_state(MOUSE_BUTTON_LEFT) {
    ///     println!("Left button is pressed");
    /// }
    ///
    /// if mouse.button_state(MOUSE_BUTTON_RIGHT) {
    ///     println!("Right button is pressed");
    /// }
    /// # }
    /// ```
    pub fn button_state(&self, button: u8) -> bool {
        self.shared.buttons[button as usize].load(Ordering::Relaxed)
    }

    /// Returns the accumulated scroll delta and resets it to zero.
    ///
    /// This method is useful for implementing scroll handling in your application.
    /// The scroll delta accumulates between calls, so you should call this
    /// periodically (e.g., once per frame) to process scroll events.
    ///
    /// # Returns
    ///
    /// A tuple `(delta_x, delta_y)` containing the horizontal and vertical
    /// scroll amounts since the last call to this method.
    ///
    /// # Examples
    ///
    /// ```
    /// # async fn example() {
    /// use app_window::input::mouse::Mouse;
    ///
    /// let mut mouse = Mouse::coalesced().await;
    ///
    /// // In your update loop:
    /// let (scroll_x, scroll_y) = mouse.load_clear_scroll_delta();
    /// if scroll_y != 0.0 {
    ///     println!("Scrolled vertically by {}", scroll_y);
    /// }
    /// # }
    /// ```
    pub fn load_clear_scroll_delta(&mut self) -> (f64, f64) {
        let x = self.shared.scroll_delta_x.swap(0.0, Ordering::Relaxed);
        let y = self.shared.scroll_delta_y.swap(0.0, Ordering::Relaxed);
        (x, y)
    }
}

impl PartialEq for Mouse {
    fn eq(&self, other: &Self) -> bool {
        Arc::ptr_eq(&self.shared, &other.shared)
    }
}

impl Eq for Mouse {}

impl Hash for Mouse {
    fn hash<H: Hasher>(&self, state: &mut H) {
        Arc::as_ptr(&self.shared).hash(state);
    }
}

#[cfg(test)]
mod test {
    use crate::input::mouse::Mouse;

    #[test]
    fn test_send_sync() {
        //I think basically the platform keyboard type operates as a kind of lifetime marker
        //(the main function is drop).  Accordingly it shouldn't be too bad to expect platforms to
        //implement send if necessary.
        fn assert_send<T: Send>() {}
        fn assert_sync<T: Sync>() {}

        fn assert_unpin<T: Unpin>() {}

        assert_send::<Mouse>();
        assert_sync::<Mouse>();
        assert_unpin::<Mouse>();
    }
}