Skip to content

Commit

Permalink
Reconnect Clipboard on window close
Browse files Browse the repository at this point in the history
Fixes #2564
  • Loading branch information
hecrj committed Sep 3, 2024
1 parent 9957481 commit 9628dc2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 22 deletions.
10 changes: 9 additions & 1 deletion winit/src/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::core::clipboard::Kind;
use std::sync::Arc;
use winit::window::Window;
use winit::window::{Window, WindowId};

/// A buffer for short-term storage and transfer within and between
/// applications.
Expand Down Expand Up @@ -83,6 +83,14 @@ impl Clipboard {
State::Unavailable => {}
}
}

/// Returns the identifier of the window used to create the [`Clipboard`], if any.
pub fn window_id(&self) -> Option<WindowId> {
match &self.state {
State::Connected { window, .. } => Some(window.id()),
State::Unavailable => None,
}
}
}

impl crate::core::Clipboard for Clipboard {
Expand Down
54 changes: 33 additions & 21 deletions winit/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,6 @@ where
}
};

let clipboard = Clipboard::connect(window.clone());

let finish_boot = async move {
let mut compositor =
C::new(graphics_settings, window.clone()).await?;
Expand All @@ -318,10 +316,7 @@ where
}

sender
.send(Boot {
compositor,
clipboard,
})
.send(Boot { compositor })
.ok()
.expect("Send boot event");

Expand Down Expand Up @@ -617,7 +612,6 @@ where

struct Boot<C> {
compositor: C,
clipboard: Clipboard,
}

#[derive(Debug)]
Expand Down Expand Up @@ -662,10 +656,7 @@ async fn run_instance<P, C>(
use winit::event;
use winit::event_loop::ControlFlow;

let Boot {
mut compositor,
mut clipboard,
} = boot.await.expect("Receive boot");
let Boot { mut compositor } = boot.await.expect("Receive boot");

let mut window_manager = WindowManager::new();
let mut is_window_opening = !is_daemon;
Expand All @@ -676,6 +667,7 @@ async fn run_instance<P, C>(

let mut ui_caches = FxHashMap::default();
let mut user_interfaces = ManuallyDrop::new(FxHashMap::default());
let mut clipboard = Clipboard::unconnected();

debug.startup_finished();

Expand Down Expand Up @@ -734,6 +726,10 @@ async fn run_instance<P, C>(
}),
));

if clipboard.window_id().is_none() {
clipboard = Clipboard::connect(window.raw.clone());
}

let _ = on_open.send(id);
is_window_opening = false;
}
Expand Down Expand Up @@ -979,14 +975,22 @@ async fn run_instance<P, C>(
winit::event::WindowEvent::CloseRequested
) && window.exit_on_close_request
{
let _ = window_manager.remove(id);
let _ = user_interfaces.remove(&id);
let _ = ui_caches.remove(&id);

events.push((
id,
core::Event::Window(window::Event::Closed),
));
run_action(
Action::Window(runtime::window::Action::Close(
id,
)),
&program,
&mut compositor,
&mut events,
&mut messages,
&mut clipboard,
&mut control_sender,
&mut debug,
&mut user_interfaces,
&mut window_manager,
&mut ui_caches,
&mut is_window_opening,
);
} else {
window.state.update(
&window.raw,
Expand Down Expand Up @@ -1223,10 +1227,18 @@ fn run_action<P, C>(
*is_window_opening = true;
}
window::Action::Close(id) => {
let window = window_manager.remove(id);
let _ = ui_caches.remove(&id);
let _ = interfaces.remove(&id);

if let Some(window) = window_manager.remove(id) {
if clipboard.window_id() == Some(window.raw.id()) {
*clipboard = window_manager
.first()
.map(|window| window.raw.clone())
.map(Clipboard::connect)
.unwrap_or_else(Clipboard::unconnected);
}

if window.is_some() {
events.push((
id,
core::Event::Window(core::window::Event::Closed),
Expand Down
4 changes: 4 additions & 0 deletions winit/src/program/window_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ where
self.entries.is_empty()
}

pub fn first(&self) -> Option<&Window<P, C>> {
self.entries.first_key_value().map(|(_id, window)| window)
}

pub fn iter_mut(
&mut self,
) -> impl Iterator<Item = (Id, &mut Window<P, C>)> {
Expand Down

0 comments on commit 9628dc2

Please sign in to comment.