From eefe17e3c63cda110902500e1eb079c967afadb9 Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Fri, 14 Jan 2022 18:24:31 -0500 Subject: [PATCH] refactor x11 --- examples/app_library/main.rs | 10 +--------- examples/app_library/window/mod.rs | 25 ++++++++++++++----------- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/examples/app_library/main.rs b/examples/app_library/main.rs index 388b74ed..f68d05d7 100644 --- a/examples/app_library/main.rs +++ b/examples/app_library/main.rs @@ -2,8 +2,6 @@ use gtk4::gdk::Display; use gtk4::prelude::*; use gtk4::CssProvider; use gtk4::StyleContext; -use once_cell::sync::OnceCell; -use x11rb::rust_connection::RustConnection; use window::AppLibraryWindow; @@ -14,8 +12,6 @@ mod group_grid; mod utils; mod window; -static X11_CONN: OnceCell = OnceCell::new(); - fn main() { let app = gtk4::Application::new(Some("com.cosmic.app_library"), Default::default()); app.connect_startup(|_app| { @@ -45,10 +41,6 @@ fn load_css() { fn build_ui(app: >k4::Application) { // Create a new custom window and show it let window = AppLibraryWindow::new(app); - let (conn, _screen_num) = x11rb::connect(None).expect("Failed to connect to X"); - if X11_CONN.set(conn).is_err() { - println!("failed to set X11_CONN. Exiting"); - std::process::exit(1); - }; + window.show(); } diff --git a/examples/app_library/window/mod.rs b/examples/app_library/window/mod.rs index 5f9e7c10..2df57f92 100644 --- a/examples/app_library/window/mod.rs +++ b/examples/app_library/window/mod.rs @@ -1,3 +1,7 @@ +use std::rc::Rc; + +use crate::app_grid::AppGrid; +use crate::group_grid::GroupGrid; use cascade::cascade; use gdk4::Rectangle; use gdk4_x11::X11Display; @@ -13,15 +17,10 @@ use gtk4::Orientation; use gtk4::SearchEntry; use gtk4::Separator; use gtk4::{gio, glib}; -use x11rb::connection::Connection; -use x11rb::protocol::xproto; -use x11rb::protocol::xproto::ConnectionExt; - use libcosmic::x; - -use crate::app_grid::AppGrid; -use crate::group_grid::GroupGrid; -use crate::X11_CONN; +use once_cell::sync::OnceCell; +use x11rb::connection::Connection; +use x11rb::protocol::xproto::*; mod imp; @@ -151,6 +150,10 @@ impl AppLibraryWindow { window.connect_realize(move |window| { if let Some((display, surface)) = x::get_window_x11(window) { + let (conn, _screen_num) = x11rb::connect(None).expect("Failed to connect to X"); + let x11rb_conn = Rc::new(OnceCell::new()); + x11rb_conn.set(conn).unwrap(); + // ignore all x11 errors... let xdisplay = display .clone() @@ -166,7 +169,7 @@ impl AppLibraryWindow { &[x::Atom::new(&display, "_NET_WM_WINDOW_TYPE_DIALOG").unwrap()], ); } - let resize = glib::clone!(@weak window => move || { + let resize = glib::clone!(@weak window, @strong x11rb_conn => move || { let s = window.surface().expect("Failed to get Surface for AppLibraryWindow"); let height = window.height(); let width = window.width(); @@ -185,15 +188,15 @@ impl AppLibraryWindow { // dbg!(monitor_height); // dbg!(width); // dbg!(height); - let w_conf = xproto::ConfigureWindowAux::default() + let w_conf = ConfigureWindowAux::default() .x(monitor_x + monitor_width / 2 - width / 2) .y(monitor_y + monitor_height / 2 - height / 2); - let conn = X11_CONN.get().expect("Failed to get X11_CONN"); let x11surface = gdk4_x11::X11Surface::xid( &s.clone().downcast::() .expect("Failed to downcast Surface to X11Surface"), ); + let conn = x11rb_conn.get().unwrap(); conn.configure_window( x11surface.try_into().expect("Failed to convert XID"), &w_conf,