feat: popup container helper

This commit is contained in:
Ashley Wulber 2022-11-14 16:33:05 +01:00
parent 49f0f34270
commit 00eed9c741
No known key found for this signature in database
GPG key ID: 5216D4F46A90A820
5 changed files with 112 additions and 51 deletions

View file

@ -1,9 +1,10 @@
use cosmic_panel_config::{PanelSize, PanelAnchor};
use iced::{widget::Button, Rectangle};
use iced::{widget::{Button, self}, Rectangle, alignment::{Vertical, Horizontal}, Color, Element};
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};
use crate::{button, widget::icon, theme::{Container, self}};
pub fn icon_button<'a, M: 'a, Renderer>() -> Button<'a, M, Renderer>
where
@ -62,4 +63,34 @@ pub fn get_popup_settings(parent: iced_native::window::Id, id: iced_native::wind
reactive: true,
..Default::default()
}, 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>
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,
{
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);
let (valign, halign) = match anchor {
PanelAnchor::Left => (Vertical::Center, Horizontal::Left),
PanelAnchor::Right => (Vertical::Center, Horizontal::Right),
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,
}))
.align_x(halign)
.align_y(valign)
}

View file

@ -300,7 +300,7 @@ impl Default for Container {
impl From<fn(&Theme) -> container::Appearance> for Container {
fn from(f: fn(&Theme) -> container::Appearance) -> Self {
Self::Custom(f)
Self::default()
}
}