Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ And please only add new entries to the top of this list, right below the `# Unre

# Unreleased

- Add `Window::monitor_from_point` and `EventLoopWindowTarget::monitor_from_point`.
- Add `Window::is_minimized`.
- On X11, fix errors handled during `register_xlib_error_hook` invocation bleeding into winit.
- Add `Window::has_focus`.
Expand Down
13 changes: 13 additions & 0 deletions src/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use instant::{Duration, Instant};
use once_cell::sync::OnceCell;
use raw_window_handle::{HasRawDisplayHandle, RawDisplayHandle};

use crate::dpi::PhysicalPosition;
use crate::{event::Event, monitor::MonitorHandle, platform_impl};

/// Provides a way to retrieve events from the system and from the windows that were registered to
Expand Down Expand Up @@ -344,6 +345,18 @@ impl<T> EventLoopWindowTarget<T> {
.map(|inner| MonitorHandle { inner })
}

/// Returns the monitor that contains the given point.
///
/// ## Platform-specific:
///
/// - **Wayland / X11:** Not implemented.
/// - **Android / iOS / Orbital / Web:** Unsupported.
pub fn monitor_from_point(&self, point: PhysicalPosition<i32>) -> Option<MonitorHandle> {
self.p
.monitor_from_point(point)
.map(|inner| MonitorHandle { inner })
}

/// Change [`DeviceEvent`] filter mode.
///
/// Since the [`DeviceEvent`] capture can lead to high CPU usage for unfocused windows, winit
Expand Down
8 changes: 8 additions & 0 deletions src/platform_impl/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,10 @@ impl<T: 'static> EventLoopWindowTarget<T> {
Some(MonitorHandle::new(self.app.clone()))
}

pub fn monitor_from_point(&self, _point: PhysicalPosition<i32>) -> Option<MonitorHandle> {
Some(MonitorHandle::new(self.app.clone()))
}

pub fn available_monitors(&self) -> VecDeque<MonitorHandle> {
let mut v = VecDeque::with_capacity(1);
v.push_back(MonitorHandle::new(self.app.clone()));
Expand Down Expand Up @@ -898,6 +902,10 @@ impl Window {
Some(MonitorHandle::new(self.app.clone()))
}

pub fn monitor_from_point(&self, _point: PhysicalPosition<i32>) -> Option<MonitorHandle> {
Some(MonitorHandle::new(self.app.clone()))
}

pub fn available_monitors(&self) -> VecDeque<MonitorHandle> {
let mut v = VecDeque::with_capacity(1);
v.push_back(MonitorHandle::new(self.app.clone()));
Expand Down
16 changes: 15 additions & 1 deletion src/platform_impl/ios/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use super::uikit::{UIApplication, UIApplicationMain, UIDevice, UIScreen};
use super::view::WinitUIWindow;
use super::{app_state, monitor, view, MonitorHandle};
use crate::{
dpi::LogicalSize,
dpi::{LogicalSize, PhysicalPosition},
event::Event,
event_loop::{
ControlFlow, EventLoopClosed, EventLoopWindowTarget as RootEventLoopWindowTarget,
Expand Down Expand Up @@ -62,6 +62,20 @@ impl<T: 'static> EventLoopWindowTarget<T> {
)))
}

pub fn monitor_from_point(&self, point: PhysicalPosition<i32>) -> Option<MonitorHandle> {
monitor::uiscreens(MainThreadMarker::new().unwrap())
.into_iter()
.find(|m| {
let size = m.size();
let position = m.position();

position.x < point.x
&& point.x < position.x + size.width as i32
&& position.y < point.y
&& point.y < position.y + size.height as i32
})
}

pub fn raw_display_handle(&self) -> RawDisplayHandle {
RawDisplayHandle::UiKit(UiKitDisplayHandle::empty())
}
Expand Down
14 changes: 14 additions & 0 deletions src/platform_impl/ios/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,20 @@ impl Inner {
)))
}

pub fn monitor_from_point(&self, point: PhysicalPosition<i32>) -> Option<MonitorHandle> {
monitor::uiscreens(MainThreadMarker::new().unwrap())
.into_iter()
.find(|m| {
let size = m.size();
let position = m.position();

position.x < point.x
&& point.x < position.x + size.width as i32
&& position.y < point.y
&& point.y < position.y + size.height as i32
})
}

pub fn id(&self) -> WindowId {
self.window.id()
}
Expand Down
30 changes: 30 additions & 0 deletions src/platform_impl/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,21 @@ impl Window {
}
}

#[inline]
pub fn monitor_from_point(&self, point: PhysicalPosition<i32>) -> Option<MonitorHandle> {
match self {
#[cfg(x11_platform)]
Window::X(ref window) => {
let monitor = MonitorHandle::X(window.monitor_from_point(point));
Some(monitor)
}
#[cfg(wayland_platform)]
Window::Wayland(ref window) => {
window.monitor_from_point(point).map(MonitorHandle::Wayland)
}
}
}

#[inline]
pub fn raw_window_handle(&self) -> RawWindowHandle {
x11_or_wayland!(match self; Window(window) => window.raw_window_handle())
Expand Down Expand Up @@ -850,6 +865,21 @@ impl<T> EventLoopWindowTarget<T> {
}
}

#[inline]
pub fn monitor_from_point(&self, point: PhysicalPosition<i32>) -> Option<MonitorHandle> {
match *self {
#[cfg(wayland_platform)]
EventLoopWindowTarget::Wayland(ref evlp) => {
evlp.monitor_from_point(point).map(MonitorHandle::Wayland)
}
#[cfg(x11_platform)]
EventLoopWindowTarget::X(ref evlp) => {
let monitor = MonitorHandle::X(evlp.x_connection().monitor_from_point(point));
Some(monitor)
}
}
}

#[inline]
pub fn set_device_event_filter(&self, _filter: DeviceEventFilter) {
match *self {
Expand Down
17 changes: 17 additions & 0 deletions src/platform_impl/linux/wayland/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,21 @@ impl<T> EventLoopWindowTarget<T> {
// There's no primary monitor on Wayland.
None
}

#[inline]
pub fn monitor_from_point(&self, point: PhysicalPosition<i32>) -> Option<MonitorHandle> {
self.output_manager
.handle
.available_outputs()
.into_iter()
.find(|m| {
let size = m.size();
let position = m.position();

position.x < point.x
&& point.x < position.x + size.width as i32
&& position.y < point.y
&& point.y < position.y + size.height as i32
})
}
}
16 changes: 16 additions & 0 deletions src/platform_impl/linux/wayland/window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,22 @@ impl Window {
None
}

#[inline]
pub fn monitor_from_point(&self, point: PhysicalPosition<i32>) -> Option<MonitorHandle> {
self.output_manager_handle
.available_outputs()
.into_iter()
.find(|m| {
let size = m.size();
let position = m.position();

position.x < point.x
&& point.x < position.x + size.width as i32
&& position.y < point.y
&& point.y < position.y + size.height as i32
})
}

#[inline]
pub fn raw_window_handle(&self) -> RawWindowHandle {
let mut window_handle = WaylandWindowHandle::empty();
Expand Down
15 changes: 15 additions & 0 deletions src/platform_impl/linux/x11/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,21 @@ impl XConnection {
.unwrap_or_else(MonitorHandle::dummy)
}

pub fn monitor_from_point(&self, point: PhysicalPosition<i32>) -> MonitorHandle {
self.available_monitors()
.into_iter()
.find(|monitor| {
let (x, y) = monitor.position;
let (width, height) = monitor.dimensions;

x < point.x
&& point.x < x + width as i32
&& y < point.y
&& point.y < y + height as i32
})
.unwrap_or_else(MonitorHandle::dummy)
}

pub fn select_xrandr_input(&self, root: Window) -> Result<c_int, XError> {
let has_xrandr = unsafe {
let mut major = 0;
Expand Down
4 changes: 4 additions & 0 deletions src/platform_impl/linux/x11/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,10 @@ impl UnownedWindow {
self.xconn.primary_monitor()
}

pub fn monitor_from_point(&self, point: PhysicalPosition<i32>) -> X11MonitorHandle {
self.xconn.monitor_from_point(point)
}

#[inline]
pub fn is_minimized(&self) -> Option<bool> {
let state_atom = unsafe { self.xconn.get_atom_unchecked(b"_NET_WM_STATE\0") };
Expand Down
6 changes: 6 additions & 0 deletions src/platform_impl/macos/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use raw_window_handle::{AppKitDisplayHandle, RawDisplayHandle};

use super::appkit::{NSApp, NSApplicationActivationPolicy, NSEvent};
use crate::{
dpi::PhysicalPosition,
event::Event,
event_loop::{ControlFlow, EventLoopClosed, EventLoopWindowTarget as RootWindowTarget},
platform::macos::ActivationPolicy,
Expand Down Expand Up @@ -87,6 +88,11 @@ impl<T: 'static> EventLoopWindowTarget<T> {
Some(monitor)
}

#[inline]
pub fn monitor_from_point(&self, point: PhysicalPosition<i32>) -> Option<MonitorHandle> {
monitor::monitor_from_point(point.x, point.y)
}

#[inline]
pub fn raw_display_handle(&self) -> RawDisplayHandle {
RawDisplayHandle::AppKit(AppKitDisplayHandle::empty())
Expand Down
2 changes: 2 additions & 0 deletions src/platform_impl/macos/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use core_foundation::{
use core_graphics::{
base::CGError,
display::{CGDirectDisplayID, CGDisplayConfigRef},
geometry::{CGPoint, CGRect},
};

pub type CGDisplayFadeInterval = f32;
Expand Down Expand Up @@ -113,6 +114,7 @@ extern "C" {
pub fn CGDisplayModeCopyPixelEncoding(mode: CGDisplayModeRef) -> CFStringRef;
pub fn CGDisplayModeRetain(mode: CGDisplayModeRef);
pub fn CGDisplayModeRelease(mode: CGDisplayModeRef);
pub fn CGRectContainsPoint(bounds: CGRect, point: CGPoint) -> i8;
}

mod core_video {
Expand Down
20 changes: 18 additions & 2 deletions src/platform_impl/macos/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ use core_foundation::{
base::{CFRelease, TCFType},
string::CFString,
};
use core_graphics::display::{CGDirectDisplayID, CGDisplay, CGDisplayBounds};
use core_graphics::{
display::{CGDirectDisplayID, CGDisplay, CGDisplayBounds},
geometry::CGPoint,
};
use objc2::rc::{Id, Shared};

use super::appkit::NSScreen;
use super::ffi;
use super::{appkit::NSScreen, ffi::CGRectContainsPoint};
use crate::dpi::{PhysicalPosition, PhysicalSize};

#[derive(Clone)]
Expand Down Expand Up @@ -149,6 +152,19 @@ pub fn primary_monitor() -> MonitorHandle {
MonitorHandle(CGDisplay::main().id)
}

pub fn monitor_from_point(x: i32, y: i32) -> Option<MonitorHandle> {
for monitor in available_monitors() {
unsafe {
let bound = CGDisplayBounds(monitor.0);
if CGRectContainsPoint(bound, CGPoint::new(x as _, y as _)) == 1 {
return Some(monitor);
}
}
}

None
}

impl fmt::Debug for MonitorHandle {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// TODO: Do this using the proper fmt API
Expand Down
5 changes: 5 additions & 0 deletions src/platform_impl/macos/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1192,6 +1192,11 @@ impl WinitWindow {
Some(monitor)
}

#[inline]
pub fn monitor_from_point(&self, point: PhysicalPosition<i32>) -> Option<MonitorHandle> {
monitor::monitor_from_point(point.x, point.y)
}

#[inline]
pub fn raw_window_handle(&self) -> RawWindowHandle {
let mut window_handle = AppKitWindowHandle::empty();
Expand Down
5 changes: 5 additions & 0 deletions src/platform_impl/orbital/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use orbclient::{
use raw_window_handle::{OrbitalDisplayHandle, RawDisplayHandle};

use crate::{
dpi::PhysicalPosition,
event::{self, StartCause, VirtualKeyCode},
event_loop::{self, ControlFlow},
window::WindowId as RootWindowId,
Expand Down Expand Up @@ -697,6 +698,10 @@ impl<T: 'static> EventLoopWindowTarget<T> {
Some(MonitorHandle)
}

pub fn monitor_from_point(&self, _point: PhysicalPosition<i32>) -> Option<MonitorHandle> {
Some(MonitorHandle)
}

pub fn available_monitors(&self) -> VecDeque<MonitorHandle> {
let mut v = VecDeque::with_capacity(1);
v.push_back(MonitorHandle);
Expand Down
5 changes: 5 additions & 0 deletions src/platform_impl/orbital/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ impl Window {
Some(MonitorHandle)
}

#[inline]
pub fn monitor_from_point(&self, _point: PhysicalPosition<i32>) -> Option<MonitorHandle> {
Some(MonitorHandle)
}

#[inline]
pub fn available_monitors(&self) -> VecDeque<MonitorHandle> {
let mut v = VecDeque::with_capacity(1);
Expand Down
6 changes: 5 additions & 1 deletion src/platform_impl/web/event_loop/window_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use super::{
super::monitor::MonitorHandle, backend, device::DeviceId, proxy::EventLoopProxy, runner,
window::WindowId,
};
use crate::dpi::{PhysicalSize, Size};
use crate::dpi::{PhysicalPosition, PhysicalSize, Size};
use crate::event::{
DeviceEvent, DeviceId as RootDeviceId, ElementState, Event, KeyboardInput, Touch, TouchPhase,
WindowEvent,
Expand Down Expand Up @@ -349,6 +349,10 @@ impl<T> EventLoopWindowTarget<T> {
Some(MonitorHandle)
}

pub fn monitor_from_point(&self, _point: PhysicalPosition<i32>) -> Option<MonitorHandle> {
Some(MonitorHandle)
}

pub fn raw_display_handle(&self) -> RawDisplayHandle {
RawDisplayHandle::Web(WebDisplayHandle::empty())
}
Expand Down
5 changes: 5 additions & 0 deletions src/platform_impl/web/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,11 @@ impl Window {
Some(MonitorHandle)
}

#[inline]
pub fn monitor_from_point(&self, _point: PhysicalPosition<i32>) -> Option<MonitorHandle> {
Some(MonitorHandle)
}

#[inline]
pub fn id(&self) -> WindowId {
self.id
Expand Down
4 changes: 4 additions & 0 deletions src/platform_impl/windows/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,10 @@ impl<T> EventLoopWindowTarget<T> {
Some(monitor)
}

pub fn monitor_from_point(&self, point: PhysicalPosition<i32>) -> Option<MonitorHandle> {
Some(monitor::monitor_from_point(point.x, point.y))
}

pub fn raw_display_handle(&self) -> RawDisplayHandle {
RawDisplayHandle::Windows(WindowsDisplayHandle::empty())
}
Expand Down
Loading