gpui: Decouple X11 logic from LinuxPlatform (#7598)
Release Notes: - Separated Linux platform and X11-specific code, so that we can add Wayland support now. --------- Co-authored-by: Mikayla <mikayla@zed.dev>
This commit is contained in:
parent
2e7db57e16
commit
266988adea
11 changed files with 337 additions and 215 deletions
|
@ -1,43 +1,36 @@
|
|||
#![allow(unused)]
|
||||
|
||||
use crate::{
|
||||
Action, AnyWindowHandle, BackgroundExecutor, Bounds, ClipboardItem, CursorStyle, DisplayId,
|
||||
ForegroundExecutor, Keymap, LinuxDispatcher, LinuxDisplay, LinuxTextSystem, LinuxWindow,
|
||||
LinuxWindowState, Menu, PathPromptOptions, Platform, PlatformDisplay, PlatformInput,
|
||||
PlatformTextSystem, PlatformWindow, Point, Result, SemanticVersion, Size, Task, WindowOptions,
|
||||
};
|
||||
|
||||
use async_task::Runnable;
|
||||
use collections::{HashMap, HashSet};
|
||||
use futures::channel::oneshot;
|
||||
use parking_lot::Mutex;
|
||||
|
||||
use std::{
|
||||
path::{Path, PathBuf},
|
||||
rc::Rc,
|
||||
sync::Arc,
|
||||
time::Duration,
|
||||
};
|
||||
use time::UtcOffset;
|
||||
use xcb::{x, Xid as _};
|
||||
|
||||
xcb::atoms_struct! {
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct XcbAtoms {
|
||||
pub wm_protocols => b"WM_PROTOCOLS",
|
||||
pub wm_del_window => b"WM_DELETE_WINDOW",
|
||||
wm_state => b"_NET_WM_STATE",
|
||||
wm_state_maxv => b"_NET_WM_STATE_MAXIMIZED_VERT",
|
||||
wm_state_maxh => b"_NET_WM_STATE_MAXIMIZED_HORZ",
|
||||
}
|
||||
}
|
||||
use async_task::Runnable;
|
||||
use futures::channel::oneshot;
|
||||
use parking_lot::Mutex;
|
||||
use time::UtcOffset;
|
||||
|
||||
use collections::{HashMap, HashSet};
|
||||
|
||||
use crate::platform::linux::client::Client;
|
||||
use crate::platform::linux::client_dispatcher::ClientDispatcher;
|
||||
use crate::platform::{X11Client, X11ClientDispatcher, XcbAtoms};
|
||||
use crate::{
|
||||
Action, AnyWindowHandle, BackgroundExecutor, Bounds, ClipboardItem, CursorStyle, DisplayId,
|
||||
ForegroundExecutor, Keymap, LinuxDispatcher, LinuxTextSystem, Menu, PathPromptOptions,
|
||||
Platform, PlatformDisplay, PlatformInput, PlatformTextSystem, PlatformWindow, Point, Result,
|
||||
SemanticVersion, Size, Task, WindowAppearance, WindowOptions, X11Display, X11Window,
|
||||
X11WindowState,
|
||||
};
|
||||
|
||||
#[derive(Default)]
|
||||
struct Callbacks {
|
||||
pub(crate) struct Callbacks {
|
||||
open_urls: Option<Box<dyn FnMut(Vec<String>)>>,
|
||||
become_active: Option<Box<dyn FnMut()>>,
|
||||
resign_active: Option<Box<dyn FnMut()>>,
|
||||
quit: Option<Box<dyn FnMut()>>,
|
||||
pub(crate) quit: Option<Box<dyn FnMut()>>,
|
||||
reopen: Option<Box<dyn FnMut()>>,
|
||||
event: Option<Box<dyn FnMut(PlatformInput) -> bool>>,
|
||||
app_menu_action: Option<Box<dyn FnMut(&dyn Action)>>,
|
||||
|
@ -45,21 +38,22 @@ struct Callbacks {
|
|||
validate_app_menu_command: Option<Box<dyn FnMut(&dyn Action) -> bool>>,
|
||||
}
|
||||
|
||||
pub(crate) struct LinuxPlatformInner {
|
||||
pub(crate) background_executor: BackgroundExecutor,
|
||||
pub(crate) foreground_executor: ForegroundExecutor,
|
||||
pub(crate) main_receiver: flume::Receiver<Runnable>,
|
||||
pub(crate) text_system: Arc<LinuxTextSystem>,
|
||||
pub(crate) callbacks: Mutex<Callbacks>,
|
||||
pub(crate) state: Mutex<LinuxPlatformState>,
|
||||
}
|
||||
|
||||
pub(crate) struct LinuxPlatform {
|
||||
xcb_connection: Arc<xcb::Connection>,
|
||||
x_root_index: i32,
|
||||
atoms: XcbAtoms,
|
||||
background_executor: BackgroundExecutor,
|
||||
foreground_executor: ForegroundExecutor,
|
||||
main_receiver: flume::Receiver<Runnable>,
|
||||
text_system: Arc<LinuxTextSystem>,
|
||||
callbacks: Mutex<Callbacks>,
|
||||
state: Mutex<LinuxPlatformState>,
|
||||
client: Arc<dyn Client>,
|
||||
inner: Arc<LinuxPlatformInner>,
|
||||
}
|
||||
|
||||
pub(crate) struct LinuxPlatformState {
|
||||
quit_requested: bool,
|
||||
windows: HashMap<x::Window, Rc<LinuxWindowState>>,
|
||||
pub(crate) quit_requested: bool,
|
||||
}
|
||||
|
||||
impl Default for LinuxPlatform {
|
||||
|
@ -75,16 +69,12 @@ impl LinuxPlatform {
|
|||
|
||||
let xcb_connection = Arc::new(xcb_connection);
|
||||
let (main_sender, main_receiver) = flume::unbounded::<Runnable>();
|
||||
let dispatcher = Arc::new(LinuxDispatcher::new(
|
||||
main_sender,
|
||||
&xcb_connection,
|
||||
x_root_index,
|
||||
));
|
||||
let client_dispatcher: Arc<dyn ClientDispatcher + Send + Sync> =
|
||||
Arc::new(X11ClientDispatcher::new(&xcb_connection, x_root_index));
|
||||
let dispatcher = LinuxDispatcher::new(main_sender, &client_dispatcher);
|
||||
let dispatcher = Arc::new(dispatcher);
|
||||
|
||||
Self {
|
||||
xcb_connection,
|
||||
x_root_index,
|
||||
atoms,
|
||||
let inner = LinuxPlatformInner {
|
||||
background_executor: BackgroundExecutor::new(dispatcher.clone()),
|
||||
foreground_executor: ForegroundExecutor::new(dispatcher.clone()),
|
||||
main_receiver,
|
||||
|
@ -92,83 +82,39 @@ impl LinuxPlatform {
|
|||
callbacks: Mutex::new(Callbacks::default()),
|
||||
state: Mutex::new(LinuxPlatformState {
|
||||
quit_requested: false,
|
||||
windows: HashMap::default(),
|
||||
}),
|
||||
};
|
||||
let inner = Arc::new(inner);
|
||||
|
||||
let x11client = X11Client::new(Arc::clone(&inner), xcb_connection, x_root_index, atoms);
|
||||
let x11client = Arc::new(x11client);
|
||||
|
||||
Self {
|
||||
client: x11client,
|
||||
inner: Arc::clone(&inner),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Platform for LinuxPlatform {
|
||||
fn background_executor(&self) -> BackgroundExecutor {
|
||||
self.background_executor.clone()
|
||||
self.inner.background_executor.clone()
|
||||
}
|
||||
|
||||
fn foreground_executor(&self) -> ForegroundExecutor {
|
||||
self.foreground_executor.clone()
|
||||
self.inner.foreground_executor.clone()
|
||||
}
|
||||
|
||||
fn text_system(&self) -> Arc<dyn PlatformTextSystem> {
|
||||
self.text_system.clone()
|
||||
self.inner.text_system.clone()
|
||||
}
|
||||
|
||||
fn run(&self, on_finish_launching: Box<dyn FnOnce()>) {
|
||||
on_finish_launching();
|
||||
//Note: here and below, don't keep the lock() open when calling
|
||||
// into window functions as they may invoke callbacks that need
|
||||
// to immediately access the platform (self).
|
||||
while !self.state.lock().quit_requested {
|
||||
let event = self.xcb_connection.wait_for_event().unwrap();
|
||||
match event {
|
||||
xcb::Event::X(x::Event::ClientMessage(ev)) => {
|
||||
if let x::ClientMessageData::Data32([atom, ..]) = ev.data() {
|
||||
if atom == self.atoms.wm_del_window.resource_id() {
|
||||
// window "x" button clicked by user, we gracefully exit
|
||||
let window = self.state.lock().windows.remove(&ev.window()).unwrap();
|
||||
window.destroy();
|
||||
let mut state = self.state.lock();
|
||||
state.quit_requested |= state.windows.is_empty();
|
||||
}
|
||||
}
|
||||
}
|
||||
xcb::Event::X(x::Event::Expose(ev)) => {
|
||||
let window = {
|
||||
let state = self.state.lock();
|
||||
Rc::clone(&state.windows[&ev.window()])
|
||||
};
|
||||
window.expose();
|
||||
}
|
||||
xcb::Event::X(x::Event::ConfigureNotify(ev)) => {
|
||||
let bounds = Bounds {
|
||||
origin: Point {
|
||||
x: ev.x().into(),
|
||||
y: ev.y().into(),
|
||||
},
|
||||
size: Size {
|
||||
width: ev.width().into(),
|
||||
height: ev.height().into(),
|
||||
},
|
||||
};
|
||||
let window = {
|
||||
let state = self.state.lock();
|
||||
Rc::clone(&state.windows[&ev.window()])
|
||||
};
|
||||
window.configure(bounds)
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
if let Ok(runnable) = self.main_receiver.try_recv() {
|
||||
runnable.run();
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(ref mut fun) = self.callbacks.lock().quit {
|
||||
fun();
|
||||
}
|
||||
self.client.run(on_finish_launching)
|
||||
}
|
||||
|
||||
fn quit(&self) {
|
||||
self.state.lock().quit_requested = true;
|
||||
self.inner.state.lock().quit_requested = true;
|
||||
}
|
||||
|
||||
//todo!(linux)
|
||||
|
@ -187,22 +133,11 @@ impl Platform for LinuxPlatform {
|
|||
fn unhide_other_apps(&self) {}
|
||||
|
||||
fn displays(&self) -> Vec<Rc<dyn PlatformDisplay>> {
|
||||
let setup = self.xcb_connection.get_setup();
|
||||
setup
|
||||
.roots()
|
||||
.enumerate()
|
||||
.map(|(root_id, _)| {
|
||||
Rc::new(LinuxDisplay::new(&self.xcb_connection, root_id as i32))
|
||||
as Rc<dyn PlatformDisplay>
|
||||
})
|
||||
.collect()
|
||||
self.client.displays()
|
||||
}
|
||||
|
||||
fn display(&self, id: DisplayId) -> Option<Rc<dyn PlatformDisplay>> {
|
||||
Some(Rc::new(LinuxDisplay::new(
|
||||
&self.xcb_connection,
|
||||
id.0 as i32,
|
||||
)))
|
||||
self.client.display(id)
|
||||
}
|
||||
|
||||
//todo!(linux)
|
||||
|
@ -215,21 +150,7 @@ impl Platform for LinuxPlatform {
|
|||
handle: AnyWindowHandle,
|
||||
options: WindowOptions,
|
||||
) -> Box<dyn PlatformWindow> {
|
||||
let x_window = self.xcb_connection.generate_id();
|
||||
|
||||
let window_ptr = Rc::new(LinuxWindowState::new(
|
||||
options,
|
||||
&self.xcb_connection,
|
||||
self.x_root_index,
|
||||
x_window,
|
||||
&self.atoms,
|
||||
));
|
||||
|
||||
self.state
|
||||
.lock()
|
||||
.windows
|
||||
.insert(x_window, Rc::clone(&window_ptr));
|
||||
Box::new(LinuxWindow(window_ptr))
|
||||
self.client.open_window(handle, options)
|
||||
}
|
||||
|
||||
fn open_url(&self, url: &str) {
|
||||
|
@ -237,7 +158,7 @@ impl Platform for LinuxPlatform {
|
|||
}
|
||||
|
||||
fn on_open_urls(&self, callback: Box<dyn FnMut(Vec<String>)>) {
|
||||
self.callbacks.lock().open_urls = Some(callback);
|
||||
self.inner.callbacks.lock().open_urls = Some(callback);
|
||||
}
|
||||
|
||||
fn prompt_for_paths(
|
||||
|
@ -256,35 +177,35 @@ impl Platform for LinuxPlatform {
|
|||
}
|
||||
|
||||
fn on_become_active(&self, callback: Box<dyn FnMut()>) {
|
||||
self.callbacks.lock().become_active = Some(callback);
|
||||
self.inner.callbacks.lock().become_active = Some(callback);
|
||||
}
|
||||
|
||||
fn on_resign_active(&self, callback: Box<dyn FnMut()>) {
|
||||
self.callbacks.lock().resign_active = Some(callback);
|
||||
self.inner.callbacks.lock().resign_active = Some(callback);
|
||||
}
|
||||
|
||||
fn on_quit(&self, callback: Box<dyn FnMut()>) {
|
||||
self.callbacks.lock().quit = Some(callback);
|
||||
self.inner.callbacks.lock().quit = Some(callback);
|
||||
}
|
||||
|
||||
fn on_reopen(&self, callback: Box<dyn FnMut()>) {
|
||||
self.callbacks.lock().reopen = Some(callback);
|
||||
self.inner.callbacks.lock().reopen = Some(callback);
|
||||
}
|
||||
|
||||
fn on_event(&self, callback: Box<dyn FnMut(PlatformInput) -> bool>) {
|
||||
self.callbacks.lock().event = Some(callback);
|
||||
self.inner.callbacks.lock().event = Some(callback);
|
||||
}
|
||||
|
||||
fn on_app_menu_action(&self, callback: Box<dyn FnMut(&dyn Action)>) {
|
||||
self.callbacks.lock().app_menu_action = Some(callback);
|
||||
self.inner.callbacks.lock().app_menu_action = Some(callback);
|
||||
}
|
||||
|
||||
fn on_will_open_app_menu(&self, callback: Box<dyn FnMut()>) {
|
||||
self.callbacks.lock().will_open_app_menu = Some(callback);
|
||||
self.inner.callbacks.lock().will_open_app_menu = Some(callback);
|
||||
}
|
||||
|
||||
fn on_validate_app_menu_command(&self, callback: Box<dyn FnMut(&dyn Action) -> bool>) {
|
||||
self.callbacks.lock().validate_app_menu_command = Some(callback);
|
||||
self.inner.callbacks.lock().validate_app_menu_command = Some(callback);
|
||||
}
|
||||
|
||||
fn os_name(&self) -> &'static str {
|
||||
|
@ -361,8 +282,6 @@ impl Platform for LinuxPlatform {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::ClipboardItem;
|
||||
|
||||
use super::*;
|
||||
|
||||
fn build_platform() -> LinuxPlatform {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue