Redo linux state again (#10452)

With the recent Linux rewrite, I attempted to simplify the number of
wrapper structs involved in the Linux code, following the macOS code as
an example. Unfortunately, I missed a vital component: pointers to the
platform state, held by platform data structures. As we hold all of the
platform data structures on Linux, this PR reintroduces a wrapper around
the internal state of both the platform and the window. This allows us
to close and drop windows correctly.

This PR also fixes a performance problem introduced by:
https://github.com/zed-industries/zed/pull/10343, where each configure
request would add a new frame callback quickly saturating the main
thread and slowing everything down.

Release Notes:
- N/A
This commit is contained in:
Mikayla Maki 2024-04-11 16:12:14 -07:00 committed by GitHub
parent 8d7f5eab79
commit 9d96ae6e78
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 233 additions and 183 deletions

View file

@ -45,63 +45,6 @@ pub(crate) const DOUBLE_CLICK_INTERVAL: Duration = Duration::from_millis(400);
pub(crate) const DOUBLE_CLICK_DISTANCE: Pixels = px(5.0);
pub(crate) const KEYRING_LABEL: &str = "zed-github-account";
pub struct RcRefCell<T>(Rc<RefCell<T>>);
impl<T> RcRefCell<T> {
pub fn new(value: T) -> Self {
RcRefCell(Rc::new(RefCell::new(value)))
}
#[inline]
#[track_caller]
pub fn borrow_mut(&self) -> std::cell::RefMut<'_, T> {
#[cfg(debug_assertions)]
{
if option_env!("TRACK_BORROW_MUT").is_some() {
eprintln!(
"borrow_mut-ing {} at {}",
type_name::<T>(),
Location::caller()
);
}
}
self.0.borrow_mut()
}
#[inline]
#[track_caller]
pub fn borrow(&self) -> std::cell::Ref<'_, T> {
#[cfg(debug_assertions)]
{
if option_env!("TRACK_BORROW_MUT").is_some() {
eprintln!("borrow-ing {} at {}", type_name::<T>(), Location::caller());
}
}
self.0.borrow()
}
}
impl<T> Deref for RcRefCell<T> {
type Target = Rc<RefCell<T>>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> DerefMut for RcRefCell<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<T> Clone for RcRefCell<T> {
fn clone(&self) -> Self {
RcRefCell(self.0.clone())
}
}
pub trait LinuxClient {
fn with_common<R>(&self, f: impl FnOnce(&mut LinuxCommon) -> R) -> R;
fn displays(&self) -> Vec<Rc<dyn PlatformDisplay>>;

View file

@ -1,7 +1,8 @@
use std::cell::RefCell;
use std::rc::Rc;
use std::cell::{RefCell, RefMut};
use std::rc::{Rc, Weak};
use std::time::{Duration, Instant};
use async_task::Runnable;
use calloop::timer::{TimeoutAction, Timer};
use calloop::{EventLoop, LoopHandle};
use calloop_wayland_source::WaylandSource;
@ -35,12 +36,13 @@ use xkbcommon::xkb::ffi::XKB_KEYMAP_FORMAT_TEXT_V1;
use xkbcommon::xkb::{self, Keycode, KEYMAP_COMPILE_NO_FLAGS};
use super::super::DOUBLE_CLICK_INTERVAL;
use super::window::{WaylandWindowState, WaylandWindowStatePtr};
use crate::platform::linux::is_within_click_distance;
use crate::platform::linux::wayland::cursor::Cursor;
use crate::platform::linux::wayland::window::WaylandWindow;
use crate::platform::linux::LinuxClient;
use crate::platform::PlatformWindow;
use crate::{point, px, MouseExitEvent};
use crate::{point, px, ForegroundExecutor, MouseExitEvent};
use crate::{
AnyWindowHandle, CursorStyle, DisplayId, KeyDownEvent, KeyUpEvent, Keystroke, Modifiers,
ModifiersChangedEvent, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent,
@ -52,9 +54,9 @@ use crate::{LinuxCommon, WindowParams};
/// Used to convert evdev scancode to xkb scancode
const MIN_KEYCODE: u32 = 8;
#[derive(Debug, Clone)]
#[derive(Clone)]
pub struct Globals {
pub qh: QueueHandle<WaylandClient>,
pub qh: QueueHandle<WaylandClientStatePtr>,
pub compositor: wl_compositor::WlCompositor,
pub wm_base: xdg_wm_base::XdgWmBase,
pub shm: wl_shm::WlShm,
@ -62,10 +64,15 @@ pub struct Globals {
pub fractional_scale_manager:
Option<wp_fractional_scale_manager_v1::WpFractionalScaleManagerV1>,
pub decoration_manager: Option<zxdg_decoration_manager_v1::ZxdgDecorationManagerV1>,
pub executor: ForegroundExecutor,
}
impl Globals {
fn new(globals: GlobalList, qh: QueueHandle<WaylandClient>) -> Self {
fn new(
globals: GlobalList,
executor: ForegroundExecutor,
qh: QueueHandle<WaylandClientStatePtr>,
) -> Self {
Globals {
compositor: globals
.bind(
@ -80,6 +87,7 @@ impl Globals {
viewporter: globals.bind(&qh, 1..=1, ()).ok(),
fractional_scale_manager: globals.bind(&qh, 1..=1, ()).ok(),
decoration_manager: globals.bind(&qh, 1..=1, ()).ok(),
executor,
qh,
}
}
@ -88,7 +96,7 @@ impl Globals {
pub(crate) struct WaylandClientState {
globals: Globals,
// Surface to Window mapping
windows: HashMap<ObjectId, WaylandWindow>,
windows: HashMap<ObjectId, WaylandWindowStatePtr>,
// Output to scale mapping
output_scales: HashMap<ObjectId, i32>,
keymap_state: Option<xkb::State>,
@ -100,14 +108,14 @@ pub(crate) struct WaylandClientState {
mouse_location: Option<Point<Pixels>>,
enter_token: Option<()>,
button_pressed: Option<MouseButton>,
mouse_focused_window: Option<WaylandWindow>,
keyboard_focused_window: Option<WaylandWindow>,
loop_handle: LoopHandle<'static, WaylandClient>,
mouse_focused_window: Option<WaylandWindowStatePtr>,
keyboard_focused_window: Option<WaylandWindowStatePtr>,
loop_handle: LoopHandle<'static, WaylandClientStatePtr>,
cursor_icon_name: String,
cursor: Cursor,
clipboard: Clipboard,
primary: Primary,
event_loop: Option<EventLoop<'static, WaylandClient>>,
event_loop: Option<EventLoop<'static, WaylandClientStatePtr>>,
common: LinuxCommon,
}
@ -124,6 +132,35 @@ pub(crate) struct KeyRepeat {
current_keysym: Option<xkb::Keysym>,
}
/// This struct is required to conform to Rust's orphan rules, so we can dispatch on the state but hand the
/// window to GPUI.
#[derive(Clone)]
pub struct WaylandClientStatePtr(Weak<RefCell<WaylandClientState>>);
impl WaylandClientStatePtr {
fn get_client(&self) -> Rc<RefCell<WaylandClientState>> {
self.0
.upgrade()
.expect("The pointer should always be valid when dispatching in wayland")
}
pub fn drop_window(&self, surface_id: &ObjectId) {
let mut client = self.get_client();
let mut state = client.borrow_mut();
let closed_window = state.windows.remove(surface_id).unwrap();
if let Some(window) = state.mouse_focused_window.take() {
if !window.ptr_eq(&closed_window) {
state.mouse_focused_window = Some(window);
}
}
if let Some(window) = state.keyboard_focused_window.take() {
if !window.ptr_eq(&closed_window) {
state.mouse_focused_window = Some(window);
}
}
}
}
#[derive(Clone)]
pub struct WaylandClient(Rc<RefCell<WaylandClientState>>);
@ -147,7 +184,8 @@ impl WaylandClient {
pub(crate) fn new() -> Self {
let conn = Connection::connect_to_env().unwrap();
let (globals, mut event_queue) = registry_queue_init::<WaylandClient>(&conn).unwrap();
let (globals, mut event_queue) =
registry_queue_init::<WaylandClientStatePtr>(&conn).unwrap();
let qh = event_queue.handle();
let mut outputs = HashMap::default();
@ -179,19 +217,19 @@ impl WaylandClient {
let display = conn.backend().display_ptr() as *mut std::ffi::c_void;
let (primary, clipboard) = unsafe { create_clipboards_from_external(display) };
let event_loop = EventLoop::try_new().unwrap();
let event_loop = EventLoop::<WaylandClientStatePtr>::try_new().unwrap();
let (common, main_receiver) = LinuxCommon::new(event_loop.get_signal());
let handle = event_loop.handle();
handle.insert_source(main_receiver, |event, _, _: &mut WaylandClient| {
handle.insert_source(main_receiver, |event, _, _: &mut WaylandClientStatePtr| {
if let calloop::channel::Event::Msg(runnable) = event {
runnable.run();
}
});
let globals = Globals::new(globals, qh);
let globals = Globals::new(globals, common.foreground_executor.clone(), qh);
let cursor = Cursor::new(&conn, &globals, 24);
@ -260,8 +298,12 @@ impl LinuxClient for WaylandClient {
) -> Box<dyn PlatformWindow> {
let mut state = self.0.borrow_mut();
let (window, surface_id) = WaylandWindow::new(state.globals.clone(), params);
state.windows.insert(surface_id, window.clone());
let (window, surface_id) = WaylandWindow::new(
state.globals.clone(),
WaylandClientStatePtr(Rc::downgrade(&self.0)),
params,
);
state.windows.insert(surface_id, window.0.clone());
Box::new(window)
}
@ -307,7 +349,13 @@ impl LinuxClient for WaylandClient {
.take()
.expect("App is already running");
event_loop.run(None, &mut self.clone(), |_| {}).log_err();
event_loop
.run(
None,
&mut WaylandClientStatePtr(Rc::downgrade(&self.0)),
|_| {},
)
.log_err();
}
fn write_to_clipboard(&self, item: crate::ClipboardItem) {
@ -327,16 +375,18 @@ impl LinuxClient for WaylandClient {
}
}
impl Dispatch<wl_registry::WlRegistry, GlobalListContents> for WaylandClient {
impl Dispatch<wl_registry::WlRegistry, GlobalListContents> for WaylandClientStatePtr {
fn event(
state: &mut Self,
this: &mut Self,
registry: &wl_registry::WlRegistry,
event: wl_registry::Event,
_: &GlobalListContents,
_: &Connection,
qh: &QueueHandle<Self>,
) {
let mut state = state.0.borrow_mut();
let mut client = this.get_client();
let mut state = client.borrow_mut();
match event {
wl_registry::Event::Global {
name,
@ -360,50 +410,60 @@ impl Dispatch<wl_registry::WlRegistry, GlobalListContents> for WaylandClient {
}
}
delegate_noop!(WaylandClient: ignore wl_compositor::WlCompositor);
delegate_noop!(WaylandClient: ignore wl_shm::WlShm);
delegate_noop!(WaylandClient: ignore wl_shm_pool::WlShmPool);
delegate_noop!(WaylandClient: ignore wl_buffer::WlBuffer);
delegate_noop!(WaylandClient: ignore wp_fractional_scale_manager_v1::WpFractionalScaleManagerV1);
delegate_noop!(WaylandClient: ignore zxdg_decoration_manager_v1::ZxdgDecorationManagerV1);
delegate_noop!(WaylandClient: ignore wp_viewporter::WpViewporter);
delegate_noop!(WaylandClient: ignore wp_viewport::WpViewport);
delegate_noop!(WaylandClientStatePtr: ignore wl_compositor::WlCompositor);
delegate_noop!(WaylandClientStatePtr: ignore wl_shm::WlShm);
delegate_noop!(WaylandClientStatePtr: ignore wl_shm_pool::WlShmPool);
delegate_noop!(WaylandClientStatePtr: ignore wl_buffer::WlBuffer);
delegate_noop!(WaylandClientStatePtr: ignore wp_fractional_scale_manager_v1::WpFractionalScaleManagerV1);
delegate_noop!(WaylandClientStatePtr: ignore zxdg_decoration_manager_v1::ZxdgDecorationManagerV1);
delegate_noop!(WaylandClientStatePtr: ignore wp_viewporter::WpViewporter);
delegate_noop!(WaylandClientStatePtr: ignore wp_viewport::WpViewport);
impl Dispatch<WlCallback, ObjectId> for WaylandClient {
impl Dispatch<WlCallback, ObjectId> for WaylandClientStatePtr {
fn event(
this: &mut WaylandClient,
state: &mut WaylandClientStatePtr,
_: &wl_callback::WlCallback,
event: wl_callback::Event,
surface_id: &ObjectId,
_: &Connection,
qh: &QueueHandle<Self>,
) {
let state = this.0.borrow_mut();
let Some(window) = state.windows.get(surface_id).cloned() else {
let client = state.get_client();
let mut state = client.borrow_mut();
let Some(window) = get_window(&mut state, surface_id) else {
return;
};
drop(state);
match event {
wl_callback::Event::Done { callback_data } => {
window.frame();
window.frame(true);
}
_ => {}
}
}
}
impl Dispatch<wl_surface::WlSurface, ()> for WaylandClient {
fn get_window(
mut state: &mut RefMut<WaylandClientState>,
surface_id: &ObjectId,
) -> Option<WaylandWindowStatePtr> {
state.windows.get(surface_id).cloned()
}
impl Dispatch<wl_surface::WlSurface, ()> for WaylandClientStatePtr {
fn event(
state: &mut Self,
this: &mut Self,
surface: &wl_surface::WlSurface,
event: <wl_surface::WlSurface as Proxy>::Event,
_: &(),
_: &Connection,
_: &QueueHandle<Self>,
) {
let mut state = state.0.borrow_mut();
let Some(window) = state.windows.get(&surface.id()).cloned() else {
let mut client = this.get_client();
let mut state = client.borrow_mut();
let Some(window) = get_window(&mut state, &surface.id()) else {
return;
};
let scales = state.output_scales.clone();
@ -413,16 +473,18 @@ impl Dispatch<wl_surface::WlSurface, ()> for WaylandClient {
}
}
impl Dispatch<wl_output::WlOutput, ()> for WaylandClient {
impl Dispatch<wl_output::WlOutput, ()> for WaylandClientStatePtr {
fn event(
state: &mut Self,
this: &mut Self,
output: &wl_output::WlOutput,
event: <wl_output::WlOutput as Proxy>::Event,
_: &(),
_: &Connection,
_: &QueueHandle<Self>,
) {
let mut state = state.0.borrow_mut();
let mut client = this.get_client();
let mut state = client.borrow_mut();
let Some(mut output_scale) = state.output_scales.get_mut(&output.id()) else {
return;
};
@ -436,7 +498,7 @@ impl Dispatch<wl_output::WlOutput, ()> for WaylandClient {
}
}
impl Dispatch<xdg_surface::XdgSurface, ObjectId> for WaylandClient {
impl Dispatch<xdg_surface::XdgSurface, ObjectId> for WaylandClientStatePtr {
fn event(
state: &mut Self,
xdg_surface: &xdg_surface::XdgSurface,
@ -445,17 +507,17 @@ impl Dispatch<xdg_surface::XdgSurface, ObjectId> for WaylandClient {
_: &Connection,
_: &QueueHandle<Self>,
) {
let mut state = state.0.borrow_mut();
let Some(window) = state.windows.get(surface_id).cloned() else {
let client = state.get_client();
let mut state = client.borrow_mut();
let Some(window) = get_window(&mut state, surface_id) else {
return;
};
drop(state);
window.handle_xdg_surface_event(event);
}
}
impl Dispatch<xdg_toplevel::XdgToplevel, ObjectId> for WaylandClient {
impl Dispatch<xdg_toplevel::XdgToplevel, ObjectId> for WaylandClientStatePtr {
fn event(
this: &mut Self,
xdg_toplevel: &xdg_toplevel::XdgToplevel,
@ -464,9 +526,9 @@ impl Dispatch<xdg_toplevel::XdgToplevel, ObjectId> for WaylandClient {
_: &Connection,
_: &QueueHandle<Self>,
) {
let mut state = this.0.borrow_mut();
let Some(window) = state.windows.get(surface_id).cloned() else {
let client = this.get_client();
let mut state = client.borrow_mut();
let Some(window) = get_window(&mut state, surface_id) else {
return;
};
@ -474,13 +536,12 @@ impl Dispatch<xdg_toplevel::XdgToplevel, ObjectId> for WaylandClient {
let should_close = window.handle_toplevel_event(event);
if should_close {
let mut state = this.0.borrow_mut();
state.windows.remove(surface_id);
this.drop_window(surface_id);
}
}
}
impl Dispatch<xdg_wm_base::XdgWmBase, ()> for WaylandClient {
impl Dispatch<xdg_wm_base::XdgWmBase, ()> for WaylandClientStatePtr {
fn event(
_: &mut Self,
wm_base: &xdg_wm_base::XdgWmBase,
@ -495,7 +556,7 @@ impl Dispatch<xdg_wm_base::XdgWmBase, ()> for WaylandClient {
}
}
impl Dispatch<wl_seat::WlSeat, ()> for WaylandClient {
impl Dispatch<wl_seat::WlSeat, ()> for WaylandClientStatePtr {
fn event(
state: &mut Self,
seat: &wl_seat::WlSeat,
@ -518,7 +579,7 @@ impl Dispatch<wl_seat::WlSeat, ()> for WaylandClient {
}
}
impl Dispatch<wl_keyboard::WlKeyboard, ()> for WaylandClient {
impl Dispatch<wl_keyboard::WlKeyboard, ()> for WaylandClientStatePtr {
fn event(
this: &mut Self,
keyboard: &wl_keyboard::WlKeyboard,
@ -527,7 +588,8 @@ impl Dispatch<wl_keyboard::WlKeyboard, ()> for WaylandClient {
conn: &Connection,
qh: &QueueHandle<Self>,
) {
let mut state = this.0.borrow_mut();
let mut client = this.get_client();
let mut state = client.borrow_mut();
match event {
wl_keyboard::Event::RepeatInfo { rate, delay } => {
state.repeat.characters_per_second = rate as u32;
@ -559,7 +621,7 @@ impl Dispatch<wl_keyboard::WlKeyboard, ()> for WaylandClient {
state.keymap_state = Some(xkb::State::new(&keymap));
}
wl_keyboard::Event::Enter { surface, .. } => {
state.keyboard_focused_window = state.windows.get(&surface.id()).cloned();
state.keyboard_focused_window = get_window(&mut state, &surface.id());
if let Some(window) = state.keyboard_focused_window.clone() {
drop(state);
@ -567,7 +629,7 @@ impl Dispatch<wl_keyboard::WlKeyboard, ()> for WaylandClient {
}
}
wl_keyboard::Event::Leave { surface, .. } => {
let keyboard_focused_window = state.windows.get(&surface.id()).cloned();
let keyboard_focused_window = get_window(&mut state, &surface.id());
state.keyboard_focused_window = None;
if let Some(window) = keyboard_focused_window {
@ -629,8 +691,9 @@ impl Dispatch<wl_keyboard::WlKeyboard, ()> for WaylandClient {
.loop_handle
.insert_source(Timer::from_duration(state.repeat.delay), {
let input = input.clone();
move |event, _metadata, client| {
let state = client.0.borrow_mut();
move |event, _metadata, this| {
let mut client = this.get_client();
let mut state = client.borrow_mut();
let is_repeating = id == state.repeat.current_id
&& state.repeat.current_keysym.is_some()
&& state.keyboard_focused_window.is_some();
@ -691,16 +754,17 @@ fn linux_button_to_gpui(button: u32) -> Option<MouseButton> {
})
}
impl Dispatch<wl_pointer::WlPointer, ()> for WaylandClient {
impl Dispatch<wl_pointer::WlPointer, ()> for WaylandClientStatePtr {
fn event(
client: &mut Self,
this: &mut Self,
wl_pointer: &wl_pointer::WlPointer,
event: wl_pointer::Event,
data: &(),
conn: &Connection,
qh: &QueueHandle<Self>,
) {
let mut state = client.0.borrow_mut();
let mut client = this.get_client();
let mut state = client.borrow_mut();
let cursor_icon_name = state.cursor_icon_name.clone();
match event {
@ -713,7 +777,7 @@ impl Dispatch<wl_pointer::WlPointer, ()> for WaylandClient {
} => {
state.mouse_location = Some(point(px(surface_x as f32), px(surface_y as f32)));
if let Some(window) = state.windows.get(&surface.id()).cloned() {
if let Some(window) = get_window(&mut state, &surface.id()) {
state.enter_token = Some(());
state.mouse_focused_window = Some(window.clone());
state.cursor.set_serial_id(serial);
@ -901,18 +965,19 @@ impl Dispatch<wl_pointer::WlPointer, ()> for WaylandClient {
}
}
impl Dispatch<wp_fractional_scale_v1::WpFractionalScaleV1, ObjectId> for WaylandClient {
impl Dispatch<wp_fractional_scale_v1::WpFractionalScaleV1, ObjectId> for WaylandClientStatePtr {
fn event(
state: &mut Self,
this: &mut Self,
_: &wp_fractional_scale_v1::WpFractionalScaleV1,
event: <wp_fractional_scale_v1::WpFractionalScaleV1 as Proxy>::Event,
surface_id: &ObjectId,
_: &Connection,
_: &QueueHandle<Self>,
) {
let mut state = state.0.borrow_mut();
let client = this.get_client();
let mut state = client.borrow_mut();
let Some(window) = state.windows.get(surface_id).cloned() else {
let Some(window) = get_window(&mut state, surface_id) else {
return;
};
@ -921,18 +986,20 @@ impl Dispatch<wp_fractional_scale_v1::WpFractionalScaleV1, ObjectId> for Wayland
}
}
impl Dispatch<zxdg_toplevel_decoration_v1::ZxdgToplevelDecorationV1, ObjectId> for WaylandClient {
impl Dispatch<zxdg_toplevel_decoration_v1::ZxdgToplevelDecorationV1, ObjectId>
for WaylandClientStatePtr
{
fn event(
state: &mut Self,
this: &mut Self,
_: &zxdg_toplevel_decoration_v1::ZxdgToplevelDecorationV1,
event: zxdg_toplevel_decoration_v1::Event,
surface_id: &ObjectId,
_: &Connection,
_: &QueueHandle<Self>,
) {
let mut state = state.0.borrow_mut();
let Some(window) = state.windows.get(surface_id).cloned() else {
let client = this.get_client();
let mut state = client.borrow_mut();
let Some(window) = get_window(&mut state, surface_id) else {
return;
};

View file

@ -1,8 +1,8 @@
use std::any::Any;
use std::cell::RefCell;
use std::cell::{Ref, RefCell, RefMut};
use std::ffi::c_void;
use std::num::NonZeroU32;
use std::rc::Rc;
use std::rc::{Rc, Weak};
use std::sync::Arc;
use blade_graphics as gpu;
@ -27,8 +27,8 @@ use crate::platform::{PlatformAtlas, PlatformInputHandler, PlatformWindow};
use crate::scene::Scene;
use crate::{
px, size, Bounds, DevicePixels, Globals, Modifiers, Pixels, PlatformDisplay, PlatformInput,
Point, PromptLevel, RcRefCell, Size, WindowAppearance, WindowBackgroundAppearance,
WindowParams,
Point, PromptLevel, Size, WaylandClientState, WaylandClientStatePtr, WindowAppearance,
WindowBackgroundAppearance, WindowParams,
};
#[derive(Default)]
@ -79,6 +79,14 @@ pub struct WaylandWindowState {
decoration_state: WaylandDecorationState,
fullscreen: bool,
maximized: bool,
client: WaylandClientStatePtr,
callbacks: Callbacks,
}
#[derive(Clone)]
pub struct WaylandWindowStatePtr {
state: Rc<RefCell<WaylandWindowState>>,
callbacks: Rc<RefCell<Callbacks>>,
}
impl WaylandWindowState {
@ -87,6 +95,7 @@ impl WaylandWindowState {
xdg_surface: xdg_surface::XdgSurface,
viewport: Option<wp_viewport::WpViewport>,
toplevel: xdg_toplevel::XdgToplevel,
client: WaylandClientStatePtr,
globals: Globals,
options: WindowParams,
) -> Self {
@ -136,22 +145,47 @@ impl WaylandWindowState {
decoration_state: WaylandDecorationState::Client,
fullscreen: false,
maximized: false,
callbacks: Callbacks::default(),
client,
}
}
}
#[derive(Clone)]
pub(crate) struct WaylandWindow {
pub(crate) state: RcRefCell<WaylandWindowState>,
pub(crate) callbacks: Rc<RefCell<Callbacks>>,
pub(crate) struct WaylandWindow(pub WaylandWindowStatePtr);
impl Drop for WaylandWindow {
fn drop(&mut self) {
let mut state = self.0.state.borrow_mut();
let surface_id = state.surface.id();
let client = state.client.clone();
state.renderer.destroy();
state.toplevel.destroy();
state.xdg_surface.destroy();
state.surface.destroy();
let state_ptr = self.0.clone();
state.globals.executor.spawn(async move {
state_ptr.close();
client.drop_window(&surface_id)
});
drop(state);
}
}
impl WaylandWindow {
pub fn ptr_eq(&self, other: &Self) -> bool {
Rc::ptr_eq(&self.state, &other.state)
fn borrow(&self) -> Ref<WaylandWindowState> {
self.0.state.borrow()
}
pub fn new(globals: Globals, params: WindowParams) -> (Self, ObjectId) {
fn borrow_mut(&self) -> RefMut<WaylandWindowState> {
self.0.state.borrow_mut()
}
pub fn new(
globals: Globals,
client: WaylandClientStatePtr,
params: WindowParams,
) -> (Self, ObjectId) {
let surface = globals.compositor.create_surface(&globals.qh, ());
let xdg_surface = globals
.wm_base
@ -178,31 +212,37 @@ impl WaylandWindow {
surface.frame(&globals.qh, surface.id());
let window_state = RcRefCell::new(WaylandWindowState::new(
surface.clone(),
xdg_surface,
viewport,
toplevel,
globals,
params,
));
let this = Self {
state: window_state,
let this = Self(WaylandWindowStatePtr {
state: Rc::new(RefCell::new(WaylandWindowState::new(
surface.clone(),
xdg_surface,
viewport,
toplevel,
client,
globals,
params,
))),
callbacks: Rc::new(RefCell::new(Callbacks::default())),
};
});
// Kick things off
surface.commit();
(this, surface.id())
}
}
pub fn frame(&self) {
let state = self.state.borrow_mut();
state.surface.frame(&state.globals.qh, state.surface.id());
drop(state);
impl WaylandWindowStatePtr {
pub fn ptr_eq(&self, other: &Self) -> bool {
Rc::ptr_eq(&self.state, &other.state)
}
pub fn frame(&self, from_frame_callback: bool) {
if from_frame_callback {
let state = self.state.borrow_mut();
state.surface.frame(&state.globals.qh, state.surface.id());
drop(state);
}
let mut cb = self.callbacks.borrow_mut();
if let Some(fun) = cb.request_frame.as_mut() {
fun();
@ -215,7 +255,7 @@ impl WaylandWindow {
let state = self.state.borrow();
state.xdg_surface.ack_configure(serial);
drop(state);
self.frame();
self.frame(false);
}
_ => {}
}
@ -273,6 +313,10 @@ impl WaylandWindow {
if let Some(mut should_close) = cb.should_close.take() {
let result = (should_close)();
cb.should_close = Some(should_close);
if result {
drop(cb);
self.close();
}
result
} else {
false
@ -428,7 +472,6 @@ impl WaylandWindow {
if let Some(fun) = callbacks.close.take() {
fun()
}
self.state.borrow_mut().toplevel.destroy();
}
pub fn handle_input(&self, input: PlatformInput) {
@ -471,11 +514,11 @@ impl HasDisplayHandle for WaylandWindow {
impl PlatformWindow for WaylandWindow {
fn bounds(&self) -> Bounds<DevicePixels> {
self.state.borrow().bounds.map(|p| DevicePixels(p as i32))
self.borrow().bounds.map(|p| DevicePixels(p as i32))
}
fn is_maximized(&self) -> bool {
self.state.borrow().maximized
self.borrow().maximized
}
fn is_minimized(&self) -> bool {
@ -484,7 +527,7 @@ impl PlatformWindow for WaylandWindow {
}
fn content_size(&self) -> Size<Pixels> {
let state = self.state.borrow();
let state = self.borrow();
Size {
width: Pixels(state.bounds.size.width as f32),
height: Pixels(state.bounds.size.height as f32),
@ -492,7 +535,7 @@ impl PlatformWindow for WaylandWindow {
}
fn scale_factor(&self) -> f32 {
self.state.borrow().scale
self.borrow().scale
}
// todo(linux)
@ -520,11 +563,11 @@ impl PlatformWindow for WaylandWindow {
}
fn set_input_handler(&mut self, input_handler: PlatformInputHandler) {
self.state.borrow_mut().input_handler = Some(input_handler);
self.borrow_mut().input_handler = Some(input_handler);
}
fn take_input_handler(&mut self) -> Option<PlatformInputHandler> {
self.state.borrow_mut().input_handler.take()
self.borrow_mut().input_handler.take()
}
fn prompt(
@ -547,10 +590,7 @@ impl PlatformWindow for WaylandWindow {
}
fn set_title(&mut self, title: &str) {
self.state
.borrow_mut()
.toplevel
.set_title(title.to_string());
self.borrow_mut().toplevel.set_title(title.to_string());
}
fn set_background_appearance(&mut self, _background_appearance: WindowBackgroundAppearance) {
@ -566,7 +606,7 @@ impl PlatformWindow for WaylandWindow {
}
fn minimize(&self) {
self.state.borrow_mut().toplevel.set_minimized();
self.borrow_mut().toplevel.set_minimized();
}
fn zoom(&self) {
@ -574,7 +614,7 @@ impl PlatformWindow for WaylandWindow {
}
fn toggle_fullscreen(&self) {
let state = self.state.borrow_mut();
let state = self.borrow_mut();
if !state.fullscreen {
state.toplevel.set_fullscreen(None);
} else {
@ -583,39 +623,39 @@ impl PlatformWindow for WaylandWindow {
}
fn is_fullscreen(&self) -> bool {
self.state.borrow().fullscreen
self.borrow().fullscreen
}
fn on_request_frame(&self, callback: Box<dyn FnMut()>) {
self.callbacks.borrow_mut().request_frame = Some(callback);
self.0.callbacks.borrow_mut().request_frame = Some(callback);
}
fn on_input(&self, callback: Box<dyn FnMut(PlatformInput) -> crate::DispatchEventResult>) {
self.callbacks.borrow_mut().input = Some(callback);
self.0.callbacks.borrow_mut().input = Some(callback);
}
fn on_active_status_change(&self, callback: Box<dyn FnMut(bool)>) {
self.callbacks.borrow_mut().active_status_change = Some(callback);
self.0.callbacks.borrow_mut().active_status_change = Some(callback);
}
fn on_resize(&self, callback: Box<dyn FnMut(Size<Pixels>, f32)>) {
self.callbacks.borrow_mut().resize = Some(callback);
self.0.callbacks.borrow_mut().resize = Some(callback);
}
fn on_fullscreen(&self, callback: Box<dyn FnMut(bool)>) {
self.callbacks.borrow_mut().fullscreen = Some(callback);
self.0.callbacks.borrow_mut().fullscreen = Some(callback);
}
fn on_moved(&self, callback: Box<dyn FnMut()>) {
self.callbacks.borrow_mut().moved = Some(callback);
self.0.callbacks.borrow_mut().moved = Some(callback);
}
fn on_should_close(&self, callback: Box<dyn FnMut() -> bool>) {
self.callbacks.borrow_mut().should_close = Some(callback);
self.0.callbacks.borrow_mut().should_close = Some(callback);
}
fn on_close(&self, callback: Box<dyn FnOnce()>) {
self.callbacks.borrow_mut().close = Some(callback);
self.0.callbacks.borrow_mut().close = Some(callback);
}
fn on_appearance_changed(&self, callback: Box<dyn FnMut()>) {
@ -628,17 +668,17 @@ impl PlatformWindow for WaylandWindow {
}
fn draw(&self, scene: &Scene) {
let mut state = self.state.borrow_mut();
let mut state = self.borrow_mut();
state.renderer.draw(scene);
}
fn completed_frame(&self) {
let mut state = self.state.borrow_mut();
let mut state = self.borrow_mut();
state.surface.commit();
}
fn sprite_atlas(&self) -> Arc<dyn PlatformAtlas> {
let state = self.state.borrow();
let state = self.borrow();
state.renderer.sprite_atlas().clone()
}
}