Library for shared applet code; use in battery applet
Creates window, button, and popover with correct properties and CSS style.
This commit is contained in:
parent
ab8131f5e6
commit
f56b160635
7 changed files with 269 additions and 114 deletions
13
Cargo.lock
generated
13
Cargo.lock
generated
|
|
@ -356,6 +356,7 @@ version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"futures",
|
"futures",
|
||||||
"gtk4",
|
"gtk4",
|
||||||
|
"libcosmic-applet",
|
||||||
"relm4",
|
"relm4",
|
||||||
"zbus",
|
"zbus",
|
||||||
]
|
]
|
||||||
|
|
@ -502,7 +503,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cosmic-panel-config"
|
name = "cosmic-panel-config"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
source = "git+https://github.com/pop-os/cosmic-panel#49b8d8c84f9db93c72ef06778b76ccb38329a8c4"
|
source = "git+https://github.com/pop-os/cosmic-panel/#49b8d8c84f9db93c72ef06778b76ccb38329a8c4"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"gtk4",
|
"gtk4",
|
||||||
|
|
@ -1560,6 +1561,16 @@ dependencies = [
|
||||||
"x11",
|
"x11",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libcosmic-applet"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"cosmic-panel-config",
|
||||||
|
"gtk4",
|
||||||
|
"once_cell",
|
||||||
|
"relm4-macros",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libcosmic-widgets"
|
name = "libcosmic-widgets"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
|
|
||||||
|
|
@ -11,4 +11,5 @@ members = [
|
||||||
"applets/cosmic-applet-time",
|
"applets/cosmic-applet-time",
|
||||||
"applets/cosmic-app-list",
|
"applets/cosmic-app-list",
|
||||||
"applets/cosmic-panel-button",
|
"applets/cosmic-panel-button",
|
||||||
|
"libcosmic-applet",
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -6,5 +6,6 @@ edition = "2021"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
futures = "0.3"
|
futures = "0.3"
|
||||||
gtk4 = { git = "https://github.com/gtk-rs/gtk4-rs" }
|
gtk4 = { git = "https://github.com/gtk-rs/gtk4-rs" }
|
||||||
|
libcosmic-applet = { path = "../../libcosmic-applet" }
|
||||||
relm4 = { git = "https://github.com/relm4/relm4", branch = "next", features = ["macros"] }
|
relm4 = { git = "https://github.com/relm4/relm4", branch = "next", features = ["macros"] }
|
||||||
zbus = { version = "2", no-default-features = true }
|
zbus = { version = "2", no-default-features = true }
|
||||||
|
|
|
||||||
|
|
@ -77,19 +77,11 @@ impl SimpleComponent for AppModel {
|
||||||
type Output = ();
|
type Output = ();
|
||||||
|
|
||||||
view! {
|
view! {
|
||||||
gtk4::Window {
|
libcosmic_applet::Applet {
|
||||||
set_decorated: false,
|
|
||||||
set_resizable: false,
|
|
||||||
set_width_request: 1,
|
|
||||||
set_height_request: 1,
|
|
||||||
gtk4::MenuButton {
|
|
||||||
set_has_frame: false,
|
|
||||||
#[watch]
|
#[watch]
|
||||||
set_icon_name: &model.icon_name,
|
set_button_icon_name: &model.icon_name,
|
||||||
#[wrap(Some)]
|
#[wrap(Some)]
|
||||||
set_popover = >k4::Popover {
|
set_popover_child = >k4::Box {
|
||||||
#[wrap(Some)]
|
|
||||||
set_child = >k4::Box {
|
|
||||||
set_orientation: gtk4::Orientation::Vertical,
|
set_orientation: gtk4::Orientation::Vertical,
|
||||||
|
|
||||||
// Battery
|
// Battery
|
||||||
|
|
@ -199,8 +191,6 @@ impl SimpleComponent for AppModel {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn init(
|
fn init(
|
||||||
_params: Self::InitParams,
|
_params: Self::InitParams,
|
||||||
|
|
|
||||||
10
libcosmic-applet/Cargo.toml
Normal file
10
libcosmic-applet/Cargo.toml
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
[package]
|
||||||
|
name = "libcosmic-applet"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
cosmic-panel-config = { git = "https://github.com/pop-os/cosmic-panel/", features = ["gtk4"] }
|
||||||
|
gtk4 = { git = "https://github.com/gtk-rs/gtk4-rs", features = ["v4_6"] }
|
||||||
|
once_cell = "1.12.0"
|
||||||
|
relm4-macros = { git = "https://github.com/Relm4/Relm4.git", branch = "next" }
|
||||||
31
libcosmic-applet/src/deref_cell.rs
Normal file
31
libcosmic-applet/src/deref_cell.rs
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
use once_cell::unsync::OnceCell;
|
||||||
|
|
||||||
|
/// Wrapper around `OnceCell` implementing `Deref`, and thus also panicking
|
||||||
|
/// when not set (or set twice).
|
||||||
|
///
|
||||||
|
/// To be used in place of `gtk::TemplateChild`, but without xml.
|
||||||
|
pub struct DerefCell<T>(OnceCell<T>);
|
||||||
|
|
||||||
|
impl<T> DerefCell<T> {
|
||||||
|
#[track_caller]
|
||||||
|
pub fn set(&self, value: T) {
|
||||||
|
if self.0.set(value).is_err() {
|
||||||
|
panic!("Initialized twice");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Default for DerefCell<T> {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self(OnceCell::default())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> std::ops::Deref for DerefCell<T> {
|
||||||
|
type Target = T;
|
||||||
|
|
||||||
|
#[track_caller]
|
||||||
|
fn deref(&self) -> &T {
|
||||||
|
self.0.get().unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
111
libcosmic-applet/src/lib.rs
Normal file
111
libcosmic-applet/src/lib.rs
Normal file
|
|
@ -0,0 +1,111 @@
|
||||||
|
use gtk4::{glib, prelude::*, subclass::prelude::*};
|
||||||
|
use relm4_macros::view;
|
||||||
|
|
||||||
|
mod deref_cell;
|
||||||
|
use deref_cell::DerefCell;
|
||||||
|
|
||||||
|
// TODO make sure style fits different panel colors?
|
||||||
|
// TODO abstraction to start main loop? Work with relm4.
|
||||||
|
// TODO gir bindings
|
||||||
|
// TODO orientation, etc.
|
||||||
|
// TODO make image size dependent on CosmicPanelConfig?
|
||||||
|
|
||||||
|
static STYLE: &str = "
|
||||||
|
window.cosmic_applet_window {
|
||||||
|
background: transparent;
|
||||||
|
}
|
||||||
|
|
||||||
|
button.cosmic_applet_button {
|
||||||
|
border-radius: 12px;
|
||||||
|
transition: 100ms;
|
||||||
|
padding: 4px;
|
||||||
|
border-color: transparent;
|
||||||
|
background: transparent;
|
||||||
|
outline-color: transparent;
|
||||||
|
}
|
||||||
|
";
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct AppletInner {
|
||||||
|
menu_button: DerefCell<gtk4::MenuButton>,
|
||||||
|
popover: DerefCell<gtk4::Popover>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[glib::object_subclass]
|
||||||
|
impl ObjectSubclass for AppletInner {
|
||||||
|
const NAME: &'static str = "CosmicApplet";
|
||||||
|
type Type = Applet;
|
||||||
|
type ParentType = gtk4::Window;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ObjectImpl for AppletInner {
|
||||||
|
fn constructed(&self, obj: &Applet) {
|
||||||
|
let window = || obj;
|
||||||
|
view! {
|
||||||
|
window() {
|
||||||
|
add_css_class: "cosmic_applet_window",
|
||||||
|
set_decorated: false,
|
||||||
|
set_resizable: false,
|
||||||
|
set_width_request: 1,
|
||||||
|
set_height_request: 1,
|
||||||
|
#[wrap(Some)]
|
||||||
|
set_child: menu_button = >k4::MenuButton {
|
||||||
|
add_css_class: "cosmic_applet_button",
|
||||||
|
set_has_frame: false,
|
||||||
|
#[wrap(Some)]
|
||||||
|
set_popover: popover = >k4::Popover {
|
||||||
|
// TODO: change if it can be positioned correctly?
|
||||||
|
set_has_arrow: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let provider = gtk4::CssProvider::new();
|
||||||
|
provider.load_from_data(STYLE.as_bytes());
|
||||||
|
obj.style_context().add_provider(&provider, gtk4::STYLE_PROVIDER_PRIORITY_APPLICATION);
|
||||||
|
|
||||||
|
self.menu_button.set(menu_button);
|
||||||
|
self.popover.set(popover);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl WidgetImpl for AppletInner {}
|
||||||
|
impl WindowImpl for AppletInner {}
|
||||||
|
|
||||||
|
glib::wrapper! {
|
||||||
|
pub struct Applet(ObjectSubclass<AppletInner>)
|
||||||
|
@extends gtk4::Widget, gtk4::Window;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Applet {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Applet {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
glib::Object::new(&[]).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn inner(&self) -> &AppletInner {
|
||||||
|
AppletInner::from_instance(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_button_child(&self, child: Option<&impl IsA<gtk4::Widget>>) {
|
||||||
|
self.inner().menu_button.set_child(child);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_button_icon_name(&self, name: &str) {
|
||||||
|
self.inner().menu_button.set_icon_name(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_button_label(&self, label: &str) {
|
||||||
|
self.inner().menu_button.set_label(label);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_popover_child(&self, child: Option<&impl IsA<gtk4::Widget>>) {
|
||||||
|
self.inner().popover.set_child(child);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue