From 6589eed954cf3c227eb8cf915e43b7e3ff5b3693 Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Tue, 16 Aug 2022 12:32:21 -0400 Subject: [PATCH] feat: init function, & refactor for features --- Cargo.toml | 22 +++++++++------ src/lib.rs | 50 +++++++++++++++++++++++++++++++++ src/wayland.rs | 18 ++++-------- src/x.rs | 1 - widgets/src/labeled_item/imp.rs | 8 +++--- 5 files changed, 73 insertions(+), 26 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f5a711ee..cd7f12fe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,20 +6,24 @@ edition = "2021" [dependencies] cascade = "1.0.0" derivative = { version = "2", optional = true } -gtk4 = { git = "https://github.com/gtk-rs/gtk4-rs", features = ["v4_4"] } -gdk4 = { git = "https://github.com/gtk-rs/gtk4-rs" } +gtk4 = { git = "https://github.com/gtk-rs/gtk4-rs", features = ["v4_6"] } +adw = { git = "https://gitlab.gnome.org/World/Rust/libadwaita-rs", package = "libadwaita"} +adw-user-colors-lib = { git = "https://github.com/pop-os/user-color-editor" } gdk4-wayland = { git = "https://github.com/gtk-rs/gtk4-rs", features = ["wayland_crate"], optional = true } -gdk4-x11 = { git = "https://github.com/gtk-rs/gtk4-rs", features = ["xlib"] } -gio = { git = "https://github.com/gtk-rs/gtk-rs-core" } -gobject-sys = { git = "https://github.com/gtk-rs/gtk-rs-core" } +gdk4-x11 = { git = "https://github.com/gtk-rs/gtk4-rs", features = ["xlib"], optional = true } +x11 = { version = "2.19.1", features = ["xlib"], optional = true } +gobject-sys = { git = "https://github.com/gtk-rs/gtk-rs-core", optional = true } wayland-client = { version = "0.29.4", optional = true } wayland-protocols = { version = "0.29.4", features = ["client", "unstable_protocols"], optional = true } -x11 = { version = "2.19.1", features = ["xlib"] } -once_cell = "1.9.0" -libcosmic-widgets = { path = "widgets" } +once_cell = "1.13.0" +libcosmic-widgets = { path = "widgets", optional = true } +xdg = "2.4.1" [features] -layer-shell = ["derivative", "gdk4-wayland", "wayland-client", "wayland-protocols"] +default = ["layer-shell", "x", "widgets"] +layer-shell = ["derivative", "gdk4-wayland", "wayland-client", "wayland-protocols", "gobject-sys"] +x = ["x11", "gdk4-x11"] +widgets = ["libcosmic-widgets"] [workspace] members = ["widgets"] diff --git a/src/lib.rs b/src/lib.rs index 9a5e77fb..aad51e2c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,56 @@ mod deref_cell; pub mod wayland; #[cfg(feature = "layer-shell")] mod wayland_custom_surface; +#[cfg(feature = "x")] pub mod x; +use adw::StyleManager; +#[cfg(feature = "widgets")] pub use libcosmic_widgets as widgets; + +use gtk4::{gdk, gio::{self, FileMonitorFlags, FileMonitorEvent, FileMonitor}, glib, prelude::*}; + +pub fn init() -> Option { + let _ = gtk4::init(); + adw::init(); + + let user_provider = gtk4::CssProvider::new(); + if let Some(display) = gdk::Display::default() { + gtk4::StyleContext::add_provider_for_display( + &display, + &user_provider, + gtk4::STYLE_PROVIDER_PRIORITY_USER, + ); + } + + let path = xdg::BaseDirectories::with_prefix("gtk-4.0") + .ok() + .and_then(|xdg_dirs| xdg_dirs.find_config_file("gtk.css")) + .unwrap_or_else(|| "~/.config/gtk-4.0/gtk.css".into()); + let file = gio::File::for_path(path); + if let Ok(monitor) = file.monitor(FileMonitorFlags::all(), None::<&gio::Cancellable>) { + monitor.connect_changed(glib::clone!(@strong user_provider => move |_monitor, file, _other_file, event| { + match event { + FileMonitorEvent::Deleted | FileMonitorEvent::MovedOut | FileMonitorEvent::Renamed => { + if adw::is_initialized() { + let manager = StyleManager::default(); + let css = if manager.is_dark() { + adw_user_colors_lib::colors::ColorOverrides::dark_default().as_css() + } else { + adw_user_colors_lib::colors::ColorOverrides::light_default().as_css() + }; + user_provider + .load_from_data(css.as_bytes()); + } + }, + FileMonitorEvent::ChangesDoneHint | FileMonitorEvent::Created | FileMonitorEvent::MovedIn => { + user_provider.load_from_file(file); + }, + _ => {} // ignored + } + })); + Some(monitor) + } else { + None + } +} \ No newline at end of file diff --git a/src/wayland.rs b/src/wayland.rs index 9f2fea33..122a2705 100644 --- a/src/wayland.rs +++ b/src/wayland.rs @@ -56,7 +56,7 @@ impl CosmicWaylandDisplay { let wayland_display = unsafe { wayland_client::Display::from_external_display( - display.wl_display().as_ref().c_ptr() as *mut _ + display.wl_display().c_ptr() as *mut _ ) }; // XXX is this sound? @@ -175,14 +175,8 @@ impl ObjectImpl for LayerShellWindowInner { fn signals() -> &'static [Signal] { static SIGNALS: Lazy> = Lazy::new(|| { - vec![Signal::builder( - // Signal name - "is-active-notify", - // Types of the values which will be sent to the signal handler - &[bool::static_type().into()], - // Type of the value the signal handler sends back - <()>::static_type().into(), - ) + vec![Signal::builder("is-active-notify") + .param_types(&[bool::static_type().into()]) .build()] }); SIGNALS.as_ref() @@ -218,8 +212,8 @@ impl WidgetImpl for LayerShellWindowInner { }); surface.connect_event( glib::clone!(@weak widget => @default-return true, move |_, event| { - if event.event_type() == gdk4::EventType::FocusChange { - let is_active = event.downcast_ref::().unwrap().is_in(); + if event.event_type() == gdk::EventType::FocusChange { + let is_active = event.downcast_ref::().unwrap().is_in(); widget.set_property("is-active", is_active); widget.emit_by_name::<()>("is-active-notify", &[&is_active]); } @@ -300,7 +294,7 @@ impl WidgetImpl for LayerShellWindowInner { } fn show(&self, widget: &Self::Type) { - widget.realize(); + WidgetExt::realize(widget); self.parent_show(widget); widget.map(); } diff --git a/src/x.rs b/src/x.rs index 60630a2a..95784769 100644 --- a/src/x.rs +++ b/src/x.rs @@ -1,5 +1,4 @@ use cascade::cascade; -use gdk4::prelude::*; use gdk4_x11::x11::xlib; use glib::translate::ToGlibPtr; use gtk4::{glib, prelude::*}; diff --git a/widgets/src/labeled_item/imp.rs b/widgets/src/labeled_item/imp.rs index f119a6c0..3a09eb06 100644 --- a/widgets/src/labeled_item/imp.rs +++ b/widgets/src/labeled_item/imp.rs @@ -1,8 +1,8 @@ use relm4::{ gtk::{prelude::*, Align, Box as GtkBox, Label, Orientation, Widget}, - ComponentParts, ComponentSender, SimpleComponent, + ComponentParts, ComponentSender, SimpleComponent, component::ComponentSenderInner, }; -use std::cell::RefCell; +use std::{cell::RefCell, sync::Arc}; #[derive(Debug)] pub(crate) enum LabeledItemMessage { @@ -111,7 +111,7 @@ impl SimpleComponent for LabeledItem { fn init( _init_params: Self::InitParams, root: &Self::Root, - _sender: &ComponentSender, + _sender: Arc>, ) -> ComponentParts { let model = LabeledItem { _title: String::default(), @@ -130,7 +130,7 @@ impl SimpleComponent for LabeledItem { fn update( &mut self, msg: Self::Input, - _sender: &ComponentSender, + _sender: Arc>, ) { self.reset(); match msg {