From 5927e29fc6b76f2bf3e13b20ec536ee67651914a Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Tue, 14 Dec 2021 16:36:28 -0500 Subject: [PATCH] hide dock when mouse not hovered on transparent dock window --- examples/dock/main.rs | 7 +++-- examples/dock/style.css | 3 +-- examples/dock/window/imp.rs | 9 ++++++- examples/dock/window/mod.rs | 48 +++++++++++++++++++++++++++++++--- examples/dock/window/window.ui | 34 +++++++++++++++++++----- 5 files changed, 85 insertions(+), 16 deletions(-) diff --git a/examples/dock/main.rs b/examples/dock/main.rs index c8177db4..d1af73a7 100644 --- a/examples/dock/main.rs +++ b/examples/dock/main.rs @@ -6,6 +6,7 @@ use gio::DesktopAppInfo; use gtk::Application; use gtk4 as gtk; use gtk4::CssProvider; +use gtk4::Revealer; use gtk4::StyleContext; use once_cell::sync::OnceCell; @@ -98,14 +99,12 @@ fn main() { std::process::exit(1); }; - let (conn, screen_num) = x11rb::connect(None).expect("Failed to connect to X"); + 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); }; - let window = Window::new(app); - let wclone = window.clone(); window.show(); glib::MainContext::default().spawn_local(async move { @@ -138,7 +137,7 @@ fn main() { DesktopAppInfo::new(&path.file_name().expect("desktop entry path needs to be a valid filename").to_string_lossy()) .expect("failed to create a Desktop App info for launching the application."); app_info - .launch(&[], Some(&wclone.display().app_launch_context().clone())).expect("failed to launch the application."); + .launch(&[], Some(&window.display().app_launch_context())).expect("failed to launch the application."); } } } diff --git a/examples/dock/style.css b/examples/dock/style.css index 4bfa4acf..54cbc5af 100644 --- a/examples/dock/style.css +++ b/examples/dock/style.css @@ -9,6 +9,5 @@ listview { background: #333333; } window { - background: #333333; - border-radius: 15px; + background: rgba(50,50,50,0.0); } diff --git a/examples/dock/window/imp.rs b/examples/dock/window/imp.rs index 36612e87..5cf86933 100644 --- a/examples/dock/window/imp.rs +++ b/examples/dock/window/imp.rs @@ -1,4 +1,7 @@ use gtk4 as gtk; +use gtk4::EventController; +use gtk4::EventControllerMotion; +use gtk4::Revealer; use glib::subclass::InitializingObject; use gtk::prelude::*; @@ -11,9 +14,12 @@ use once_cell::sync::OnceCell; #[derive(CompositeTemplate, Default)] #[template(file = "window.ui")] pub struct Window { + // #[template_child] + // pub list_view: TemplateChild, #[template_child] - pub list_view: TemplateChild, + pub revealer: TemplateChild, pub model: OnceCell, + pub event_controller: OnceCell, } // The central trait for subclassing a GObject @@ -39,6 +45,7 @@ impl ObjectImpl for Window { self.parent_constructed(obj); // Setup + obj.setup_event_controller(); obj.setup_model(); obj.setup_callbacks(); obj.setup_factory(); diff --git a/examples/dock/window/mod.rs b/examples/dock/window/mod.rs index e3aa796f..81417d67 100644 --- a/examples/dock/window/mod.rs +++ b/examples/dock/window/mod.rs @@ -7,6 +7,7 @@ use gdk4::Surface; use gdk4_x11::X11Surface; use gtk4 as gtk; use gtk4::Allocation; +use gtk4::EventControllerMotion; use postage::prelude::Sink; use x11rb::connection::Connection; use x11rb::protocol::xproto::ConnectionExt; @@ -62,8 +63,15 @@ impl Window { // Get state let imp = imp::Window::from_instance(self); let window = self.clone().upcast::(); - let list_view = &imp.list_view; - let lv = list_view.get(); + // let list_view = &imp.list_view; + // let lv = list_view.get(); + + // let revealer = Revealer::builder() + // .child(&window) + // .reveal_child(false) + // .transition_duration(200) + // .transition_type(gtk4::RevealerTransitionType::SlideUp) + // .build(); // let app_selection_model = list_view // .model() @@ -91,6 +99,16 @@ impl Window { // } // })); + let event_controller = &imp.event_controller.get().unwrap(); + let revealer = &imp.revealer.get(); + window.connect_show( + glib::clone!(@weak revealer, @weak event_controller => move |_| { + dbg!(!event_controller.contains_pointer()); + if !event_controller.contains_pointer() { + revealer.set_reveal_child(false); + } + }), + ); window.connect_realize(move |window| { if let Some((display, surface)) = x::get_window_x11(window) { unsafe { @@ -143,10 +161,34 @@ impl Window { s.connect_height_notify(surface_resize_handler.clone()); s.connect_width_notify(surface_resize_handler.clone()); s.connect_scale_factor_notify(surface_resize_handler); + // s.connect_enter_monitor(glib::clone!(@weak rv => move |s, monitor| { + // monitor.connect_connector_notify + // })); } else { println!("failed to get X11 window"); } }); + event_controller.connect_enter(glib::clone!(@weak revealer => move |_evc, _x, _y| { + dbg!("hello, mouse entered me :)"); + revealer.set_reveal_child(true); + })); + event_controller.connect_leave(glib::clone!(@weak revealer => move |_evc| { + dbg!("hello, mouse left me :)"); + revealer.set_reveal_child(false); + })); + } + + fn setup_event_controller(&self) { + let imp = imp::Window::from_instance(self); + let window = &imp.revealer.get(); + let ev = EventControllerMotion::builder() + .propagation_limit(gtk4::PropagationLimit::None) + .propagation_phase(gtk4::PropagationPhase::Capture) + .build(); + window.add_controller(&ev); + imp.event_controller + .set(ev) + .expect("Could not set event controller"); } fn setup_factory(&self) { @@ -174,6 +216,6 @@ impl Window { // }); // Set the factory of the list view let imp = imp::Window::from_instance(self); - imp.list_view.set_factory(Some(&factory)); + // imp.list_view.set_factory(Some(&factory)); } } diff --git a/examples/dock/window/window.ui b/examples/dock/window/window.ui index 481cd2ca..afd0f868 100644 --- a/examples/dock/window/window.ui +++ b/examples/dock/window/window.ui @@ -7,12 +7,34 @@ false false - - horizontal - 4 - 4 - 4 - 4 + + true + true + true + 200 + swing-up + + + + 256 + 64 + hello world + true + 4 + 4 + +