From 59a112523d911b333fdef54793a7c83bbf7934b1 Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Wed, 24 Aug 2022 15:55:53 -0400 Subject: [PATCH] feat: popup positioning depending on panel anchor --- Cargo.lock | 1 + .../cosmic-app-list/src/apps_container/mod.rs | 2 +- applets/cosmic-applet-notifications/Cargo.toml | 1 + .../cosmic-applet-notifications/src/main.rs | 14 ++++++++++++-- .../src/notification_popover.rs | 8 +++++--- libcosmic-applet/src/button.rs | 18 ++++++++++++++---- libcosmic-applet/src/lib.rs | 4 ---- 7 files changed, 34 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 20d66b2a..c0183c7a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -421,6 +421,7 @@ name = "cosmic-applet-notifications" version = "0.1.0" dependencies = [ "cascade", + "cosmic-panel-config", "futures", "gtk4", "libadwaita", diff --git a/applets/cosmic-app-list/src/apps_container/mod.rs b/applets/cosmic-app-list/src/apps_container/mod.rs index 1d9e416d..766e25f4 100644 --- a/applets/cosmic-app-list/src/apps_container/mod.rs +++ b/applets/cosmic-app-list/src/apps_container/mod.rs @@ -2,7 +2,7 @@ use crate::dock_list::DockList; use crate::dock_list::DockListType; use cascade::cascade; -use cosmic_panel_config::{CosmicPanelConfig, PanelAnchor}; +use cosmic_panel_config::{PanelAnchor}; use gtk4::prelude::*; use gtk4::subclass::prelude::*; use gtk4::Orientation; diff --git a/applets/cosmic-applet-notifications/Cargo.toml b/applets/cosmic-applet-notifications/Cargo.toml index 5ca4c6a8..22b0b7da 100644 --- a/applets/cosmic-applet-notifications/Cargo.toml +++ b/applets/cosmic-applet-notifications/Cargo.toml @@ -17,3 +17,4 @@ serde = "1" zbus = "2.0.1" zbus_names = "2" zvariant = "3" +cosmic-panel-config = {git = "https://github.com/pop-os/cosmic-panel", features = ["gtk4"] } diff --git a/applets/cosmic-applet-notifications/src/main.rs b/applets/cosmic-applet-notifications/src/main.rs index 48371d7a..cf387fc6 100644 --- a/applets/cosmic-applet-notifications/src/main.rs +++ b/applets/cosmic-applet-notifications/src/main.rs @@ -1,5 +1,6 @@ -use gtk4::{glib, prelude::*}; +use gtk4::{glib, prelude::*, PositionType}; use relm4_macros::view; +use cosmic_panel_config::PanelAnchor; mod dbus_service; mod deref_cell; @@ -31,7 +32,16 @@ fn main() { window.show(); // XXX show in correct place - let notification_popover = NotificationPopover::new(¬ifications); + let position = std::env::var("COSMIC_PANEL_ANCHOR") + .ok() + .and_then(|anchor| anchor.parse::().ok()) + .map(|anchor| match anchor { + PanelAnchor::Left => PositionType::Right, + PanelAnchor::Right => PositionType::Left, + PanelAnchor::Top => PositionType::Bottom, + PanelAnchor::Bottom => PositionType::Top, + }); + let notification_popover = NotificationPopover::new(¬ifications, position); notification_popover.set_parent(&applet_button); let main_loop = glib::MainLoop::new(None, false); diff --git a/applets/cosmic-applet-notifications/src/notification_popover.rs b/applets/cosmic-applet-notifications/src/notification_popover.rs index d5f3f1c2..a78ae964 100644 --- a/applets/cosmic-applet-notifications/src/notification_popover.rs +++ b/applets/cosmic-applet-notifications/src/notification_popover.rs @@ -2,7 +2,7 @@ use cascade::cascade; use gtk4::{ glib::{self, clone}, prelude::*, - subclass::prelude::*, + subclass::prelude::*, PositionType, }; use std::cell::RefCell; @@ -75,9 +75,11 @@ glib::wrapper! { } impl NotificationPopover { - pub fn new(notifications: &Notifications) -> Self { + pub fn new(notifications: &Notifications, position: Option) -> Self { let obj = glib::Object::new::(&[]).unwrap(); - + if let Some(position) = position { + obj.set_position(position); + } let notification_widget = cascade! { NotificationWidget::new(notifications); }; diff --git a/libcosmic-applet/src/button.rs b/libcosmic-applet/src/button.rs index ec63a192..17a29eaa 100644 --- a/libcosmic-applet/src/button.rs +++ b/libcosmic-applet/src/button.rs @@ -1,5 +1,5 @@ -use cosmic_panel_config::{CosmicPanelConfig, PanelSize}; -use gtk4::{glib, prelude::*, subclass::prelude::*}; +use cosmic_panel_config::{PanelSize, PanelAnchor}; +use gtk4::{glib, prelude::*, subclass::prelude::*, PositionType}; use relm4_macros::view; use crate::deref_cell::DerefCell; @@ -18,7 +18,6 @@ button.cosmic_applet_button { #[derive(Default)] pub struct AppletButtonInner { menu_button: DerefCell, - panel_config: DerefCell, popover: DerefCell, } @@ -31,6 +30,15 @@ impl ObjectSubclass for AppletButtonInner { impl ObjectImpl for AppletButtonInner { fn constructed(&self, obj: &AppletButton) { + let position = std::env::var("COSMIC_PANEL_ANCHOR") + .ok() + .and_then(|anchor| anchor.parse::().ok()) + .map(|anchor| match anchor { + PanelAnchor::Left => PositionType::Right, + PanelAnchor::Right => PositionType::Left, + PanelAnchor::Top => PositionType::Bottom, + PanelAnchor::Bottom => PositionType::Top, + }); view! { menu_button = gtk4::MenuButton { set_parent: obj, @@ -38,7 +46,6 @@ impl ObjectImpl for AppletButtonInner { set_has_frame: false, #[wrap(Some)] set_popover: popover = >k4::Popover { - // TODO: change if it can be positioned correctly? set_has_arrow: false, } }, @@ -46,6 +53,9 @@ impl ObjectImpl for AppletButtonInner { load_from_data: STYLE.as_bytes(), } } + if let Some(position) = position { + popover.set_position(position); + } obj.set_layout_manager(Some(>k4::BinLayout::new())); obj.style_context() .add_provider(&provider, gtk4::STYLE_PROVIDER_PRIORITY_APPLICATION); diff --git a/libcosmic-applet/src/lib.rs b/libcosmic-applet/src/lib.rs index 517c18ad..6e2b20b6 100644 --- a/libcosmic-applet/src/lib.rs +++ b/libcosmic-applet/src/lib.rs @@ -1,7 +1,3 @@ -use cosmic_panel_config::CosmicPanelConfig; -use gtk4::{glib, prelude::*, subclass::prelude::*}; -use relm4_macros::view; - mod button; pub use button::AppletButton; mod deref_cell;