fix: correctly align the container for popups

This commit is contained in:
Ashley Wulber 2022-11-14 16:58:04 +01:00
parent 0a0972ddc8
commit 9174e42099
No known key found for this signature in database
GPG key ID: 5216D4F46A90A820
3 changed files with 87 additions and 61 deletions

View file

@ -1,10 +1,18 @@
use cosmic_panel_config::{PanelSize, PanelAnchor};
use iced::{widget::{Button, self}, Rectangle, alignment::{Vertical, Horizontal}, Color, Element};
use iced_native::{command::platform_specific::wayland::popup::{SctkPopupSettings, SctkPositioner}};
use cosmic_panel_config::{PanelAnchor, PanelSize};
use iced::{
alignment::{Horizontal, Vertical},
widget::{self, Button},
Color, Element, Length, Rectangle,
};
use iced_native::command::platform_specific::wayland::popup::{SctkPopupSettings, SctkPositioner};
use iced_style::container::Appearance;
use sctk::reexports::protocols::xdg::shell::client::xdg_positioner::{Anchor, Gravity};
use crate::{button, widget::icon, theme::{Container, self}};
use crate::{
button,
theme::{self, Container},
widget::icon,
};
pub fn icon_button<'a, M: 'a, Renderer>() -> Button<'a, M, Renderer>
where
@ -25,14 +33,19 @@ where
button!(icon("input-gaming-symbolic", pixels))
}
pub fn get_popup_settings(parent: iced_native::window::Id, id: iced_native::window::Id, width_padding: Option<i32>, height_padding: Option<i32>) -> SctkPopupSettings {
pub fn get_popup_settings(
parent: iced_native::window::Id,
id: iced_native::window::Id,
width_padding: Option<i32>,
height_padding: Option<i32>,
) -> SctkPopupSettings {
let anchor = std::env::var("COSMIC_PANEL_ANCHOR")
.ok()
.map(|size| match size.parse::<PanelAnchor>() {
Ok(p) => p,
Err(_) => PanelAnchor::Top,
})
.unwrap_or(PanelAnchor::Top);
.ok()
.map(|size| match size.parse::<PanelAnchor>() {
Ok(p) => p,
Err(_) => PanelAnchor::Top,
})
.unwrap_or(PanelAnchor::Top);
let pixels = std::env::var("COSMIC_PANEL_SIZE")
.ok()
.and_then(|size| match size.parse::<PanelSize>() {
@ -50,32 +63,44 @@ pub fn get_popup_settings(parent: iced_native::window::Id, id: iced_native::wind
PanelAnchor::Top => (Anchor::Bottom, Gravity::Bottom),
PanelAnchor::Bottom => (Anchor::Top, Gravity::Top),
};
SctkPopupSettings { parent, id, positioner: SctkPositioner {
anchor,
gravity,
size: (200, 200),
anchor_rect: Rectangle {
x: 0,
y: 0,
width: width_padding.unwrap_or(16) * 2 + pixels as i32,
height: height_padding.unwrap_or(8) * 2 + pixels as i32,
SctkPopupSettings {
parent,
id,
positioner: SctkPositioner {
anchor,
gravity,
size: (200, 200),
anchor_rect: Rectangle {
x: 0,
y: 0,
width: width_padding.unwrap_or(16) * 2 + pixels as i32,
height: height_padding.unwrap_or(8) * 2 + pixels as i32,
},
reactive: true,
..Default::default()
},
reactive: true,
..Default::default()
}, parent_size: None, grab: true }
parent_size: None,
grab: true,
}
}
pub fn popup_container<'a, Message, Renderer>(content: impl Into<Element<'a, Message, Renderer>>,) -> crate::widget::widget::Container<'a, Message, Renderer>
// TODO popup container which tracks the size of itself and requests the popup to resize to match
pub fn popup_container<'a, Message, Renderer>(
content: impl Into<Element<'a, Message, Renderer>>,
) -> crate::widget::widget::Container<'a, Message, Renderer>
where
Renderer: iced_native::Renderer,
<<Renderer as iced_native::Renderer>::Theme as iced_style::container::StyleSheet>::Style: From<theme::Container>, Renderer::Theme: widget::container::StyleSheet,
Renderer: iced_native::Renderer + 'a,
Message: 'a,
<<Renderer as iced_native::Renderer>::Theme as iced_style::container::StyleSheet>::Style:
From<theme::Container>,
Renderer::Theme: widget::container::StyleSheet,
{
let anchor = std::env::var("COSMIC_PANEL_ANCHOR")
.ok()
.map(|size| match size.parse::<PanelAnchor>() {
Ok(p) => p,
Err(_) => PanelAnchor::Top,
})
.ok()
.map(|size| match size.parse::<PanelAnchor>() {
Ok(p) => p,
Err(_) => PanelAnchor::Top,
})
.unwrap_or(PanelAnchor::Top);
let (valign, halign) = match anchor {
PanelAnchor::Left => (Vertical::Center, Horizontal::Left),
@ -83,14 +108,18 @@ where
PanelAnchor::Top => (Vertical::Top, Horizontal::Center),
PanelAnchor::Bottom => (Vertical::Bottom, Horizontal::Center),
};
crate::widget::widget::container(content)
.style(Container::Custom(|theme| Appearance {
text_color: Some(theme.cosmic().on_bg_color().into()),
background: Some(theme.extended_palette().background.base.color.into()),
border_radius: 12.0,
border_width: 0.0,
border_color: Color::TRANSPARENT,
}))
crate::widget::widget::container(crate::widget::widget::container(content).style(
Container::Custom(|theme| Appearance {
text_color: Some(theme.cosmic().on_bg_color().into()),
background: Some(theme.extended_palette().background.base.color.into()),
border_radius: 12.0,
border_width: 0.0,
border_color: Color::TRANSPARENT,
}),
))
.width(Length::Fill)
.height(Length::Fill)
.align_x(halign)
.align_y(valign)
}
}