Separate CosmicAppletWindow and CosmicAppletButton

This commit is contained in:
Ian Douglas Scott 2022-07-05 14:41:09 -07:00
parent 74f6c2eca6
commit aac43de65d
16 changed files with 421 additions and 343 deletions

1
Cargo.lock generated
View file

@ -401,6 +401,7 @@ dependencies = [
"gtk4",
"libcosmic-applet",
"once_cell",
"relm4-macros",
"serde",
"zbus",
"zbus_names",

View file

@ -5,8 +5,8 @@ use crate::dock_list::DockList;
use crate::dock_list::DockListType;
use crate::utils::Event;
use cascade::cascade;
use cosmic_panel_config::config::PanelAnchor;
use cosmic_panel_config::config::CosmicPanelConfig;
use cosmic_panel_config::config::PanelAnchor;
use gtk4::prelude::*;
use gtk4::subclass::prelude::*;
use gtk4::Orientation;
@ -69,7 +69,6 @@ impl AppsContainer {
self_.setup_callbacks();
self_.set_position(config.anchor);
Self::setup_callbacks(&self_);
self_

View file

@ -1,6 +1,6 @@
// SPDX-License-Identifier: MPL-2.0-only
use cosmic_panel_config::config::{PanelAnchor, CosmicPanelConfig};
use cosmic_panel_config::config::{CosmicPanelConfig, PanelAnchor};
use glib::SignalHandlerId;
use gtk4::subclass::prelude::*;
use gtk4::{gio, glib};
@ -25,7 +25,7 @@ pub struct DockList {
pub popover_menu_index: Rc<Cell<Option<u32>>>,
pub position: Rc<Cell<PanelAnchor>>,
pub tx: OnceCell<mpsc::Sender<Event>>,
pub config: OnceCell<CosmicPanelConfig>
pub config: OnceCell<CosmicPanelConfig>,
}
#[glib::object_subclass]

View file

@ -76,9 +76,11 @@ fn app(application: &Application) {
});
pa.connect().unwrap(); // XXX unwrap
view! {
window = libcosmic_applet::Applet {
window = libcosmic_applet::AppletWindow {
set_application: Some(application),
set_title: Some("COSMIC Network Applet"),
#[wrap(Some)]
set_child = &libcosmic_applet::AppletButton {
// TODO: adjust based on volume, mute
set_button_icon_name: "multimedia-volume-control-symbolic",
#[wrap(Some)]
@ -164,6 +166,7 @@ fn app(application: &Application) {
}
}
}
}
glib::MainContext::default().spawn_local(
clone!(@weak inputs, @weak current_input, @weak input_volume, @strong pa => async move {

View file

@ -77,7 +77,9 @@ impl SimpleComponent for AppModel {
type Output = ();
view! {
libcosmic_applet::Applet {
libcosmic_applet::AppletWindow {
#[wrap(Some)]
set_child = &libcosmic_applet::AppletButton {
#[watch]
set_button_icon_name: &model.icon_name,
#[wrap(Some)]
@ -191,6 +193,7 @@ impl SimpleComponent for AppModel {
}
}
}
}
fn init(
_params: Self::InitParams,

View file

@ -10,6 +10,7 @@ pub mod graphics;
pub mod mode_box;
use self::{dbus::PowerDaemonProxy, graphics::Graphics, mode_box::ModeSelection};
use cosmic_panel_config::config::CosmicPanelConfig;
use gtk4::{
gdk::Display,
gio::ApplicationFlags,
@ -20,7 +21,6 @@ use gtk4::{
};
use once_cell::sync::Lazy;
use tokio::runtime::Runtime;
use cosmic_panel_config::config::CosmicPanelConfig;
static RT: Lazy<Runtime> = Lazy::new(|| Runtime::new().expect("failed to build tokio runtime"));

View file

@ -10,6 +10,7 @@ futures = "0.3"
gtk4 = { git = "https://github.com/gtk-rs/gtk4-rs" }
libcosmic-applet = { path = "../../libcosmic-applet" }
once_cell = "1.12"
relm4-macros = { git = "https://github.com/Relm4/Relm4.git", branch = "next" }
serde = "1"
zbus = "2.0.1"
zbus_names = "2"

View file

@ -1,5 +1,5 @@
use cascade::cascade;
use gtk4::{glib, prelude::*};
use relm4_macros::view;
mod dbus_service;
mod deref_cell;
@ -19,18 +19,20 @@ fn main() {
let notification_list = NotificationList::new(&notifications);
let window = cascade! {
libcosmic_applet::Applet::new();
..set_button_icon_name("user-invisible-symbolic"); // TODO
..set_popover_child(Some(&notification_list));
..show();
};
view! {
window = libcosmic_applet::AppletWindow {
#[wrap(Some)]
set_child: applet_button = &libcosmic_applet::AppletButton {
set_button_icon_name: "user-invisible-symbolic", // TODO
set_popover_child: Some(&notification_list)
}
}
}
window.show();
// XXX show in correct place
cascade! {
NotificationPopover::new(&notifications);
..set_parent(&window.child().unwrap()); // XXX better way?
};
let notification_popover = NotificationPopover::new(&notifications);
notification_popover.set_parent(&applet_button);
let main_loop = glib::MainLoop::new(None, false);
main_loop.run();

View file

@ -24,10 +24,12 @@ fn main() {
fn build_ui(application: &gtk4::Application) {
view! {
window = libcosmic_applet::Applet {
window = libcosmic_applet::AppletWindow {
set_title: Some("COSMIC Power Applet"),
set_application: Some(application),
// TODO adjust battery icon based on charge
#[wrap(Some)]
set_child = &libcosmic_applet::AppletButton {
set_button_icon_name: "system-shutdown-symbolic",
#[wrap(Some)]
set_popover_child: main_box = &gtk4::Box {
@ -55,5 +57,6 @@ fn build_ui(application: &gtk4::Application) {
}
}
}
}
window.show();
}

View file

@ -1,5 +1,6 @@
// SPDX-License-Identifier: MPL-2.0-only
use calloop::channel::SyncSender;
use gtk4::{
gdk::Display,
gio::{self, ApplicationFlags},
@ -13,7 +14,6 @@ use tokio::sync::mpsc;
use utils::{Activate, WorkspaceEvent};
use wayland::State;
use window::CosmicWorkspacesWindow;
use calloop::channel::SyncSender;
mod localize;
mod utils;

View file

@ -92,8 +92,8 @@ pub fn spawn_workspaces(tx: glib::Sender<State>) -> SyncSender<WorkspaceEvent> {
running: true,
};
let loop_handle = event_loop.handle();
loop_handle.insert_source(workspaces_rx, |e, _, state| {
match e {
loop_handle
.insert_source(workspaces_rx, |e, _, state| match e {
Event::Msg(WorkspaceEvent::Activate(id)) => {
if let Some(w) = state
.workspace_groups
@ -136,15 +136,16 @@ pub fn spawn_workspaces(tx: glib::Sender<State>) -> SyncSender<WorkspaceEvent> {
}
}
}
Event::Closed => if let Some(workspace_manager) = &mut state.workspace_manager {
Event::Closed => {
if let Some(workspace_manager) = &mut state.workspace_manager {
for g in &mut state.workspace_groups {
g.workspace_group_handle.destroy();
}
workspace_manager.stop();
},
}
}).unwrap();
}
})
.unwrap();
while state.running {
event_loop
.dispatch(Duration::from_millis(16), &mut state)

View file

@ -43,9 +43,7 @@ impl WorkspaceButton {
new_button.connect_clicked(move |_| {
let id_clone = id.clone();
if !is_active {
let _ = TX.get()
.unwrap()
.send(WorkspaceEvent::Activate(id_clone));
let _ = TX.get().unwrap().send(WorkspaceEvent::Activate(id_clone));
}
});

View file

@ -61,9 +61,7 @@ impl WorkspaceList {
.build();
scroll_controller.connect_scroll(|_, dx, dy| {
let _ = TX.get()
.unwrap()
.send(WorkspaceEvent::Scroll(dx + dy));
let _ = TX.get().unwrap().send(WorkspaceEvent::Scroll(dx + dy));
Inhibit::default()
});

View 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 = &gtk4::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(&gtk4::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);
}
}

View file

@ -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 = &gtk4::MenuButton {
add_css_class: "cosmic_applet_button",
set_has_frame: false,
#[wrap(Some)]
set_popover: popover = &gtk4::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?

View 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()
}
}