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
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