Separate CosmicAppletWindow and CosmicAppletButton
This commit is contained in:
parent
74f6c2eca6
commit
aac43de65d
16 changed files with 421 additions and 343 deletions
126
libcosmic-applet/src/button.rs
Normal file
126
libcosmic-applet/src/button.rs
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
use cosmic_panel_config::config::CosmicPanelConfig;
|
||||
use gtk4::{glib, prelude::*, subclass::prelude::*};
|
||||
use relm4_macros::view;
|
||||
|
||||
use crate::deref_cell::DerefCell;
|
||||
|
||||
static STYLE: &str = "
|
||||
button.cosmic_applet_button {
|
||||
border-radius: 12px;
|
||||
transition: 100ms;
|
||||
padding: 4px;
|
||||
border-color: transparent;
|
||||
background: transparent;
|
||||
outline-color: transparent;
|
||||
}
|
||||
";
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct AppletButtonInner {
|
||||
menu_button: DerefCell<gtk4::MenuButton>,
|
||||
panel_config: DerefCell<CosmicPanelConfig>,
|
||||
popover: DerefCell<gtk4::Popover>,
|
||||
}
|
||||
|
||||
#[glib::object_subclass]
|
||||
impl ObjectSubclass for AppletButtonInner {
|
||||
const NAME: &'static str = "CosmicAppletButton";
|
||||
type Type = AppletButton;
|
||||
type ParentType = gtk4::Widget;
|
||||
}
|
||||
|
||||
impl ObjectImpl for AppletButtonInner {
|
||||
fn constructed(&self, obj: &AppletButton) {
|
||||
view! {
|
||||
menu_button = gtk4::MenuButton {
|
||||
set_parent: obj,
|
||||
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,
|
||||
}
|
||||
},
|
||||
provider = gtk4::CssProvider {
|
||||
load_from_data: STYLE.as_bytes(),
|
||||
}
|
||||
}
|
||||
obj.set_layout_manager(Some(>k4::BinLayout::new()));
|
||||
obj.style_context()
|
||||
.add_provider(&provider, gtk4::STYLE_PROVIDER_PRIORITY_APPLICATION);
|
||||
|
||||
self.menu_button.set(menu_button);
|
||||
self.popover.set(popover);
|
||||
self.panel_config
|
||||
.set(CosmicPanelConfig::load_from_env().unwrap_or_default());
|
||||
}
|
||||
|
||||
fn dispose(&self, _obj: &AppletButton) {
|
||||
self.menu_button.unparent();
|
||||
}
|
||||
}
|
||||
|
||||
impl WidgetImpl for AppletButtonInner {
|
||||
fn compute_expand(&self, _obj: &AppletButton, hexpand: &mut bool, vexpand: &mut bool) {
|
||||
*hexpand = self
|
||||
.menu_button
|
||||
.compute_expand(gtk4::Orientation::Horizontal);
|
||||
*vexpand = self.menu_button.compute_expand(gtk4::Orientation::Vertical);
|
||||
}
|
||||
|
||||
fn request_mode(&self, _obj: &AppletButton) -> gtk4::SizeRequestMode {
|
||||
self.menu_button.request_mode()
|
||||
}
|
||||
}
|
||||
|
||||
impl WindowImpl for AppletButtonInner {}
|
||||
|
||||
glib::wrapper! {
|
||||
pub struct AppletButton(ObjectSubclass<AppletButtonInner>)
|
||||
@extends gtk4::Widget;
|
||||
}
|
||||
|
||||
impl Default for AppletButton {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl AppletButton {
|
||||
pub fn new() -> Self {
|
||||
glib::Object::new(&[]).unwrap()
|
||||
}
|
||||
|
||||
fn inner(&self) -> &AppletButtonInner {
|
||||
AppletButtonInner::from_instance(self)
|
||||
}
|
||||
|
||||
// TODO: avoid multiple instances?
|
||||
pub fn panel_config(&self) -> &CosmicPanelConfig {
|
||||
&*self.inner().panel_config
|
||||
}
|
||||
|
||||
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) {
|
||||
let image = gtk4::Image::from_icon_name(name);
|
||||
image.set_pixel_size(
|
||||
self.panel_config()
|
||||
.get_applet_icon_size()
|
||||
.try_into()
|
||||
.unwrap(),
|
||||
); // XXX unwrap
|
||||
self.set_button_child(Some(&image));
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,9 +1,8 @@
|
|||
use cosmic_panel_config::config::CosmicPanelConfig;
|
||||
use gtk4::{glib, prelude::*, subclass::prelude::*};
|
||||
use relm4_macros::view;
|
||||
|
||||
mod button;
|
||||
pub use button::AppletButton;
|
||||
mod deref_cell;
|
||||
use deref_cell::DerefCell;
|
||||
mod window;
|
||||
pub use window::AppletWindow;
|
||||
|
||||
// TODO make sure style fits different panel colors?
|
||||
// TODO abstraction to start main loop? Work with relm4.
|
||||
|
|
@ -11,118 +10,4 @@ use deref_cell::DerefCell;
|
|||
// TODO orientation, etc.
|
||||
// TODO make image size dependent on CosmicPanelConfig?
|
||||
// TODO way to have multiple applets with this style, for system tray.
|
||||
|
||||
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 {
|
||||
panel_config: DerefCell<CosmicPanelConfig>,
|
||||
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);
|
||||
self.panel_config
|
||||
.set(CosmicPanelConfig::load_from_env().unwrap_or_default());
|
||||
}
|
||||
}
|
||||
|
||||
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 panel_config(&self) -> &CosmicPanelConfig {
|
||||
&*self.inner().panel_config
|
||||
}
|
||||
|
||||
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) {
|
||||
let image = gtk4::Image::from_icon_name(name);
|
||||
image.set_pixel_size(
|
||||
self.panel_config()
|
||||
.get_applet_icon_size()
|
||||
.try_into()
|
||||
.unwrap(),
|
||||
); // XXX unwrap
|
||||
self.set_button_child(Some(&image));
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
// TODO also handle non-popover button? Is GtkMenuButton particularly special, or just use a toggle button?
|
||||
|
|
|
|||
58
libcosmic-applet/src/window.rs
Normal file
58
libcosmic-applet/src/window.rs
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
use gtk4::{glib, prelude::*, subclass::prelude::*};
|
||||
use relm4_macros::view;
|
||||
|
||||
static STYLE: &str = "
|
||||
window.cosmic_applet_window {
|
||||
background: transparent;
|
||||
}
|
||||
";
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct AppletWindowInner;
|
||||
|
||||
#[glib::object_subclass]
|
||||
impl ObjectSubclass for AppletWindowInner {
|
||||
const NAME: &'static str = "CosmicAppletWindow";
|
||||
type Type = AppletWindow;
|
||||
type ParentType = gtk4::Window;
|
||||
}
|
||||
|
||||
impl ObjectImpl for AppletWindowInner {
|
||||
fn constructed(&self, obj: &AppletWindow) {
|
||||
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,
|
||||
},
|
||||
provider = gtk4::CssProvider {
|
||||
load_from_data: STYLE.as_bytes(),
|
||||
}
|
||||
}
|
||||
obj.style_context()
|
||||
.add_provider(&provider, gtk4::STYLE_PROVIDER_PRIORITY_APPLICATION);
|
||||
}
|
||||
}
|
||||
|
||||
impl WidgetImpl for AppletWindowInner {}
|
||||
impl WindowImpl for AppletWindowInner {}
|
||||
|
||||
glib::wrapper! {
|
||||
pub struct AppletWindow(ObjectSubclass<AppletWindowInner>)
|
||||
@extends gtk4::Widget, gtk4::Window;
|
||||
}
|
||||
|
||||
impl Default for AppletWindow {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl AppletWindow {
|
||||
pub fn new() -> Self {
|
||||
glib::Object::new(&[]).unwrap()
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue