fix: Updates & fixes the applets helpers
This commit is contained in:
parent
c96c1c3e20
commit
5dbb050d48
1 changed files with 102 additions and 116 deletions
|
|
@ -1,130 +1,116 @@
|
||||||
use cosmic_panel_config::{PanelAnchor, PanelSize};
|
use cosmic_panel_config::{PanelAnchor, PanelSize};
|
||||||
use iced::{
|
use iced::{
|
||||||
alignment::{Horizontal, Vertical},
|
alignment::{Horizontal, Vertical},
|
||||||
widget::{self, Button},
|
widget::{self, container, Container},
|
||||||
Color, Element, Length, Rectangle,
|
Color, Element, Length, Rectangle,
|
||||||
};
|
};
|
||||||
use iced_native::command::platform_specific::wayland::popup::{SctkPopupSettings, SctkPositioner};
|
use iced_native::{command::platform_specific::wayland::popup::{SctkPopupSettings, SctkPositioner}, widget::Button};
|
||||||
use iced_style::container::Appearance;
|
use iced_style::container::{Appearance};
|
||||||
use sctk::reexports::protocols::xdg::shell::client::xdg_positioner::{Anchor, Gravity};
|
use sctk::reexports::protocols::xdg::shell::client::xdg_positioner::{Anchor, Gravity};
|
||||||
|
|
||||||
use crate::{
|
use crate::{Renderer, Theme};
|
||||||
button,
|
|
||||||
theme::{self, Container},
|
|
||||||
widget::icon,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub fn icon_button<'a, M: 'a, Renderer>(
|
#[derive(Debug, Clone)]
|
||||||
name: &str,
|
pub struct CosmicAppletHelper {
|
||||||
icon_style: <Renderer::Theme as iced_native::svg::StyleSheet>::Style,
|
pub size: PanelSize,
|
||||||
) -> Button<'a, M, Renderer>
|
pub anchor: PanelAnchor,
|
||||||
where
|
|
||||||
Renderer::Theme: iced_native::svg::StyleSheet + iced_style::button::StyleSheet,
|
|
||||||
Renderer: iced_native::Renderer + iced_native::svg::Renderer + 'a,
|
|
||||||
{
|
|
||||||
let pixels = std::env::var("COSMIC_PANEL_SIZE")
|
|
||||||
.ok()
|
|
||||||
.and_then(|size| match size.parse::<PanelSize>() {
|
|
||||||
Ok(PanelSize::XL) => Some(64),
|
|
||||||
Ok(PanelSize::L) => Some(36),
|
|
||||||
Ok(PanelSize::M) => Some(24),
|
|
||||||
Ok(PanelSize::S) => Some(16),
|
|
||||||
Ok(PanelSize::XS) => Some(12),
|
|
||||||
Err(_) => Some(12),
|
|
||||||
})
|
|
||||||
.unwrap_or(16);
|
|
||||||
button!(icon(name, pixels).style(icon_style))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_popup_settings(
|
impl Default for CosmicAppletHelper {
|
||||||
parent: iced_native::window::Id,
|
fn default() -> Self {
|
||||||
id: iced_native::window::Id,
|
Self {
|
||||||
size: (u32, u32),
|
size: std::env::var("COSMIC_PANEL_SIZE")
|
||||||
width_padding: Option<i32>,
|
.ok()
|
||||||
height_padding: Option<i32>,
|
.and_then(|size| size.parse::<PanelSize>().ok())
|
||||||
) -> SctkPopupSettings {
|
.unwrap_or(PanelSize::S),
|
||||||
let anchor = std::env::var("COSMIC_PANEL_ANCHOR")
|
anchor: std::env::var("COSMIC_PANEL_ANCHOR")
|
||||||
.ok()
|
.ok()
|
||||||
.map(|size| match size.parse::<PanelAnchor>() {
|
.and_then(|size| size.parse::<PanelAnchor>().ok())
|
||||||
Ok(p) => p,
|
.unwrap_or(PanelAnchor::Top),
|
||||||
Err(_) => PanelAnchor::Top,
|
}
|
||||||
})
|
|
||||||
.unwrap_or(PanelAnchor::Top);
|
|
||||||
let pixels = std::env::var("COSMIC_PANEL_SIZE")
|
|
||||||
.ok()
|
|
||||||
.and_then(|size| match size.parse::<PanelSize>() {
|
|
||||||
Ok(PanelSize::XL) => Some(64),
|
|
||||||
Ok(PanelSize::L) => Some(36),
|
|
||||||
Ok(PanelSize::M) => Some(24),
|
|
||||||
Ok(PanelSize::S) => Some(16),
|
|
||||||
Ok(PanelSize::XS) => Some(12),
|
|
||||||
Err(_) => Some(12),
|
|
||||||
})
|
|
||||||
.unwrap_or(16);
|
|
||||||
let (offset, anchor, gravity) = match anchor {
|
|
||||||
PanelAnchor::Left => ((8, 0), Anchor::Right, Gravity::Right),
|
|
||||||
PanelAnchor::Right => ((-8, 0), Anchor::Left, Gravity::Left),
|
|
||||||
PanelAnchor::Top => ((0, 8), Anchor::Bottom, Gravity::Bottom),
|
|
||||||
PanelAnchor::Bottom => ((0, -8), Anchor::Top, Gravity::Top),
|
|
||||||
};
|
|
||||||
SctkPopupSettings {
|
|
||||||
parent,
|
|
||||||
id,
|
|
||||||
positioner: SctkPositioner {
|
|
||||||
anchor,
|
|
||||||
gravity,
|
|
||||||
offset,
|
|
||||||
size,
|
|
||||||
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,
|
|
||||||
constraint_adjustment: 15, // slide_y, slide_x, flip_x, flip_y
|
|
||||||
..Default::default()
|
|
||||||
},
|
|
||||||
parent_size: None,
|
|
||||||
grab: true,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO popup container which tracks the size of itself and requests the popup to resize to match
|
impl CosmicAppletHelper {
|
||||||
pub fn popup_container<'a, Message, Renderer>(
|
pub fn suggested_icon_size(&self) -> u16 {
|
||||||
content: impl Into<Element<'a, Message, Renderer>>,
|
match self.size {
|
||||||
) -> crate::widget::widget::Container<'a, Message, Renderer>
|
PanelSize::XL => 64,
|
||||||
where
|
PanelSize::L => 36,
|
||||||
Renderer: iced_native::Renderer + 'a,
|
PanelSize::M => 24,
|
||||||
Message: 'a,
|
PanelSize::S => 16,
|
||||||
<<Renderer as iced_native::Renderer>::Theme as iced_style::container::StyleSheet>::Style:
|
PanelSize::XS => 12,
|
||||||
From<theme::Container>,
|
}
|
||||||
Renderer::Theme: widget::container::StyleSheet,
|
}
|
||||||
{
|
|
||||||
let anchor = std::env::var("COSMIC_PANEL_ANCHOR")
|
|
||||||
.ok()
|
pub fn icon_button<'a, Message: 'static>(&self, icon_name: &'a str) -> widget::Button<'a, Message, Renderer> {
|
||||||
.map(|size| match size.parse::<PanelAnchor>() {
|
crate::widget::button(crate::theme::Button::Text).icon(crate::theme::Svg::Symbolic, icon_name, self.suggested_icon_size()).padding(8)
|
||||||
Ok(p) => p,
|
}
|
||||||
Err(_) => PanelAnchor::Top,
|
|
||||||
})
|
// TODO popup container which tracks the size of itself and requests the popup to resize to match
|
||||||
.unwrap_or(PanelAnchor::Top);
|
pub fn popup_container<'a, Message: 'static>(
|
||||||
let (valign, halign) = match anchor {
|
&self,
|
||||||
PanelAnchor::Left => (Vertical::Center, Horizontal::Left),
|
content: impl Into<Element<'a, Message, Renderer>>,
|
||||||
PanelAnchor::Right => (Vertical::Center, Horizontal::Right),
|
) -> Container<'a, Message, Renderer>
|
||||||
PanelAnchor::Top => (Vertical::Top, Horizontal::Center),
|
{
|
||||||
PanelAnchor::Bottom => (Vertical::Bottom, Horizontal::Center),
|
let (valign, halign) = match self.anchor {
|
||||||
};
|
PanelAnchor::Left => (Vertical::Center, Horizontal::Left),
|
||||||
crate::widget::widget::container(crate::widget::widget::container(content).style(
|
PanelAnchor::Right => (Vertical::Center, Horizontal::Right),
|
||||||
Container::Custom(|theme| Appearance {
|
PanelAnchor::Top => (Vertical::Top, Horizontal::Center),
|
||||||
text_color: Some(theme.cosmic().on_bg_color().into()),
|
PanelAnchor::Bottom => (Vertical::Bottom, Horizontal::Center),
|
||||||
background: Some(theme.extended_palette().background.base.color.into()),
|
};
|
||||||
border_radius: 12.0,
|
|
||||||
border_width: 0.0,
|
Container::<Message, Renderer>::new(
|
||||||
border_color: Color::TRANSPARENT,
|
Container::<Message, Renderer>::new(content).style(crate::theme::Container::Custom(|theme| Appearance {
|
||||||
}),
|
text_color: Some(theme.cosmic().on_bg_color().into()),
|
||||||
))
|
background: Some(theme.extended_palette().background.base.color.into()),
|
||||||
.width(Length::Fill)
|
border_radius: 12.0,
|
||||||
.height(Length::Fill)
|
border_width: 0.0,
|
||||||
.align_x(halign)
|
border_color: Color::TRANSPARENT,
|
||||||
.align_y(valign)
|
})),
|
||||||
|
)
|
||||||
|
.width(Length::Fill)
|
||||||
|
.height(Length::Fill)
|
||||||
|
.align_x(halign)
|
||||||
|
.align_y(valign)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_popup_settings(
|
||||||
|
&self,
|
||||||
|
parent: iced_native::window::Id,
|
||||||
|
id: iced_native::window::Id,
|
||||||
|
size: (u32, u32),
|
||||||
|
width_padding: Option<i32>,
|
||||||
|
height_padding: Option<i32>,
|
||||||
|
) -> SctkPopupSettings {
|
||||||
|
let pixels = self.suggested_icon_size();
|
||||||
|
let (offset, anchor, gravity) = match self.anchor {
|
||||||
|
PanelAnchor::Left => ((8, 0), Anchor::Right, Gravity::Right),
|
||||||
|
PanelAnchor::Right => ((-8, 0), Anchor::Left, Gravity::Left),
|
||||||
|
PanelAnchor::Top => ((0, 8), Anchor::Bottom, Gravity::Bottom),
|
||||||
|
PanelAnchor::Bottom => ((0, -8), Anchor::Top, Gravity::Top),
|
||||||
|
};
|
||||||
|
SctkPopupSettings {
|
||||||
|
parent,
|
||||||
|
id,
|
||||||
|
positioner: SctkPositioner {
|
||||||
|
anchor,
|
||||||
|
gravity,
|
||||||
|
offset,
|
||||||
|
size,
|
||||||
|
anchor_rect: Rectangle {
|
||||||
|
x: 0,
|
||||||
|
y: 0,
|
||||||
|
width: width_padding.unwrap_or(8) * 2 + pixels as i32,
|
||||||
|
height: height_padding.unwrap_or(8) * 2 + pixels as i32,
|
||||||
|
},
|
||||||
|
reactive: true,
|
||||||
|
constraint_adjustment: 15, // slide_y, slide_x, flip_x, flip_y
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
parent_size: None,
|
||||||
|
grab: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue