feat: popup positioning depending on panel anchor

This commit is contained in:
Ashley Wulber 2022-08-24 15:55:53 -04:00
parent ac0b555946
commit 59a112523d
No known key found for this signature in database
GPG key ID: 5216D4F46A90A820
7 changed files with 34 additions and 14 deletions

View file

@ -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<gtk4::MenuButton>,
panel_config: DerefCell<CosmicPanelConfig>,
popover: DerefCell<gtk4::Popover>,
}
@ -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::<PanelAnchor>().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 = &gtk4::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(&gtk4::BinLayout::new()));
obj.style_context()
.add_provider(&provider, gtk4::STYLE_PROVIDER_PRIORITY_APPLICATION);