feat: init function, & refactor for features
This commit is contained in:
parent
851449c3ef
commit
6589eed954
5 changed files with 73 additions and 26 deletions
50
src/lib.rs
50
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<FileMonitor> {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
|
@ -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<Vec<Signal>> = 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::<gdk4::FocusEvent>().unwrap().is_in();
|
||||
if event.event_type() == gdk::EventType::FocusChange {
|
||||
let is_active = event.downcast_ref::<gdk::FocusEvent>().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();
|
||||
}
|
||||
|
|
|
|||
1
src/x.rs
1
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::*};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue