Skip to content
Open
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
26 changes: 12 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,6 @@ useless_conversion = "deny"

[workspace.lints.rustdoc]
broken_intra_doc_links = "forbid"

[patch.crates-io]
winit = { git = "https://github.com/kevinard/winit", rev = "7a606df981b5baf6b4e3fc446cdc5d976b484e99" }
10 changes: 10 additions & 0 deletions runtime/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,21 @@ use crate::futures::futures::channel::oneshot;
use crate::task::{self, Task};

pub use raw_window_handle;
/// Focus-scoped compositor shortcut inhibition and actual permission state.
pub mod keyboard_shortcuts;

use raw_window_handle::{HasDisplayHandle, HasWindowHandle};

/// Request focus-scoped compositor shortcut inhibition. Other backends and
/// retired windows return None. Observe the guard's actual state before capture.
pub fn inhibit_keyboard_shortcuts(id:Id)->Task<Option<keyboard_shortcuts::Inhibitor>> {
task::oneshot(move |channel|crate::Action::Window(Action::InhibitKeyboardShortcuts(id,channel)))
}

/// An operation to be performed on some window.
pub enum Action {
/// Request an inhibitor for an ordinary window, when natively supported.
InhibitKeyboardShortcuts(Id,oneshot::Sender<Option<keyboard_shortcuts::Inhibitor>>),
/// Open a new window with some [`Settings`].
Open(Id, Settings, oneshot::Sender<Id>),

Expand Down
109 changes: 109 additions & 0 deletions runtime/src/window/keyboard_shortcuts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
//! Optional, focus-scoped keyboard shortcut inhibition for ordinary windows.
use crate::futures::futures::{
channel::mpsc,
stream::{self, BoxStream},
StreamExt,
};
use std::{
fmt,
sync::{Arc, Mutex},
};

/// Observed native permission and focus. A successful request is not Active.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Status {
/// No keyboard seat currently focuses this window.
Unfocused,
/// A focused seat has not yet acknowledged inhibition.
Pending,
/// Every focused keyboard seat acknowledges inhibition.
Active,
/// The compositor disabled inhibition on a focused seat.
Inactive,
/// The window or native owner retired; this state cannot revive.
Closed,
}
/// Platform implementation, owned until the final handle retires.
pub trait Driver: Send + Sync {
/// Read the latest actual state without consuming a notification.
fn status(&self) -> Status;
}
struct Inner {
driver: Box<dyn Driver>,
updates: Mutex<Option<mpsc::Receiver<()>>>,
}
/// Keeps a native request alive; window closure revokes retained clones too.
#[derive(Clone)]
pub struct Inhibitor(Arc<Inner>);
impl fmt::Debug for Inhibitor {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Keyboard shortcut inhibitor")
.field(&self.status())
.finish()
}
}
impl Inhibitor {
/// Construct from a native adapter and coalesced, bounded wake-ups.
pub fn new(driver: impl Driver + 'static, updates: mpsc::Receiver<()>) -> Self {
Self(Arc::new(Inner {
driver: Box::new(driver),
updates: Mutex::new(Some(updates)),
}))
}
/// Inspect actual focus and compositor permission.
pub fn status(&self) -> Status {
self.0.driver.status()
}
/// Take the single change stream, starting with the current state.
pub fn changes(&self) -> Option<BoxStream<'static, Status>> {
let updates = self.0.updates.lock().unwrap().take()?;
Some(
stream::unfold(Some((self.clone(), updates, true)), |state| async move {
let (inhibitor, mut updates, initial) = state?;
if !initial {
updates.next().await?;
}
let status = inhibitor.status();
let next = if status == Status::Closed {
None
} else {
Some((inhibitor, updates, false))
};
Some((status, next))
})
.boxed(),
)
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::futures::futures::FutureExt;
struct Fake(Arc<Mutex<Status>>);
impl Driver for Fake {
fn status(&self) -> Status {
*self.0.lock().unwrap()
}
}
#[test]
fn coalesced_changes_report_latest_state_and_closed_stream_cannot_revive() {
let state = Arc::new(Mutex::new(Status::Unfocused));
let (mut send, receive) = mpsc::channel(0);
let inhibitor = Inhibitor::new(Fake(state.clone()), receive);
let mut changes = inhibitor.changes().unwrap();
assert!(inhibitor.changes().is_none());
assert_eq!(changes.next().now_or_never(), Some(Some(Status::Unfocused)));
*state.lock().unwrap() = Status::Pending;
send.try_send(()).unwrap();
*state.lock().unwrap() = Status::Active;
let _ = send.try_send(());
assert_eq!(changes.next().now_or_never(), Some(Some(Status::Active)));
*state.lock().unwrap() = Status::Closed;
send.try_send(()).unwrap();
assert_eq!(changes.next().now_or_never(), Some(Some(Status::Closed)));
*state.lock().unwrap() = Status::Active;
let _ = send.try_send(());
assert_eq!(changes.next().now_or_never(), Some(None));
}
}
38 changes: 38 additions & 0 deletions winit/src/keyboard_shortcuts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//! Optional ordinary-window protocol bridge; notifications are coalesced.
use crate::runtime::window::keyboard_shortcuts as shared;
#[cfg(not(all(target_os = "linux", feature = "wayland")))]
pub(crate) fn enable(_: &winit::window::Window) -> Option<shared::Inhibitor> {
None
}
#[cfg(all(target_os = "linux", feature = "wayland"))]
pub(crate) fn enable(window: &winit::window::Window) -> Option<shared::Inhibitor> {
use crate::futures::futures::channel::mpsc;
use std::sync::{Arc, Mutex};
use winit::platform::wayland::{keyboard_shortcuts as native, WindowExtWayland};
fn channel() -> (native::Changed, mpsc::Receiver<()>) {
let (sender, receiver) = mpsc::channel(0);
let sender = Arc::new(Mutex::new(sender));
let changed: native::Changed = Arc::new(move || {
let _ = sender
.lock()
.unwrap_or_else(|e| e.into_inner())
.try_send(());
});
(changed, receiver)
}
let (changed, receiver) = channel();
let endpoint = window.inhibit_keyboard_shortcuts(changed)?;
struct Driver(native::KeyboardShortcutsInhibitor);
impl shared::Driver for Driver {
fn status(&self) -> shared::Status {
match self.0.status() {
native::Status::Unfocused => shared::Status::Unfocused,
native::Status::Pending => shared::Status::Pending,
native::Status::Active => shared::Status::Active,
native::Status::Inactive => shared::Status::Inactive,
native::Status::Closed => shared::Status::Closed,
}
}
}
Some(shared::Inhibitor::new(Driver(endpoint), receiver))
}
5 changes: 5 additions & 0 deletions winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub mod conversion;

mod error;
mod proxy;
mod keyboard_shortcuts;
mod window;

pub use clipboard::Clipboard;
Expand Down Expand Up @@ -1380,6 +1381,10 @@ fn run_action<'a, P, C>(
}
},
Action::Window(action) => match action {
window::Action::InhibitKeyboardShortcuts(id,reply)=>{
let endpoint=window_manager.get_mut(id).and_then(|window|keyboard_shortcuts::enable(&window.raw));
let _=reply.send(endpoint);
}
window::Action::Open(id, settings, channel) => {
let monitor = window_manager.last_monitor();

Expand Down