feat: init function, & refactor for features
This commit is contained in:
parent
851449c3ef
commit
6589eed954
5 changed files with 73 additions and 26 deletions
22
Cargo.toml
22
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"]
|
||||
|
|
|
|||
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::*};
|
||||
|
|
|
|||
|
|
@ -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<Self>,
|
||||
_sender: Arc<ComponentSenderInner<LabeledItemMessage, (), ()>>,
|
||||
) -> ComponentParts<Self> {
|
||||
let model = LabeledItem {
|
||||
_title: String::default(),
|
||||
|
|
@ -130,7 +130,7 @@ impl SimpleComponent for LabeledItem {
|
|||
fn update(
|
||||
&mut self,
|
||||
msg: Self::Input,
|
||||
_sender: &ComponentSender<Self>,
|
||||
_sender: Arc<ComponentSenderInner<LabeledItemMessage, (), ()>>,
|
||||
) {
|
||||
self.reset();
|
||||
match msg {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue