This commit is contained in:
Ryan Brue 2025-10-13 20:43:12 +03:00 • committed by GitHub
commit 45fec4b039
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -37,7 +37,7 @@ static TOOLTIP_WINDOW_ID: LazyLock<window::Id> = LazyLock::new(window::Id::uniqu
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Context { pub struct Context {
pub size: Size, pub size: PanelSize,
pub anchor: PanelAnchor, pub anchor: PanelAnchor,
pub spacing: u32, pub spacing: u32,
pub background: CosmicPanelBackground, pub background: CosmicPanelBackground,
@ -48,12 +48,6 @@ pub struct Context {
pub suggested_bounds: Option<iced::Size>, pub suggested_bounds: Option<iced::Size>,
} }
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Size {
// (width, height)
Hardcoded((u16, u16)),
PanelSize(PanelSize),
}
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub enum PanelType { pub enum PanelType {
Panel, Panel,
@ -84,12 +78,10 @@ impl From<String> for PanelType {
impl Default for Context { impl Default for Context {
fn default() -> Self { fn default() -> Self {
Self { Self {
size: Size::PanelSize( size: std::env::var("COSMIC_PANEL_SIZE")
std::env::var("COSMIC_PANEL_SIZE")
.ok() .ok()
.and_then(|size| ron::from_str(size.as_str()).ok()) .and_then(|size| ron::from_str(size.as_str()).ok())
.unwrap_or(PanelSize::S), .unwrap_or(PanelSize::S),
),
anchor: std::env::var("COSMIC_PANEL_ANCHOR") anchor: std::env::var("COSMIC_PANEL_ANCHOR")
.ok() .ok()
.and_then(|size| ron::from_str(size.as_str()).ok()) .and_then(|size| ron::from_str(size.as_str()).ok())
@ -112,14 +104,9 @@ impl Default for Context {
impl Context { impl Context {
#[must_use] #[must_use]
pub fn suggested_size(&self, is_symbolic: bool) -> (u16, u16) { pub fn suggested_size(&self, is_symbolic: bool) -> (u16, u16) {
match &self.size { let s = self.size.get_applet_icon_size(is_symbolic) as u16;
Size::PanelSize(size) => {
let s = size.get_applet_icon_size(is_symbolic) as u16;
(s, s) (s, s)
} }
Size::Hardcoded((width, height)) => (*width, *height),
}
}
#[must_use] #[must_use]
pub fn suggested_window_size(&self) -> (NonZeroU32, NonZeroU32) { pub fn suggested_window_size(&self) -> (NonZeroU32, NonZeroU32) {
@ -146,15 +133,12 @@ impl Context {
#[must_use] #[must_use]
pub fn suggested_padding(&self, is_symbolic: bool) -> u16 { pub fn suggested_padding(&self, is_symbolic: bool) -> u16 {
match &self.size { self.size.get_applet_padding(is_symbolic)
Size::PanelSize(size) => size.get_applet_padding(is_symbolic),
Size::Hardcoded(_) => 8,
}
} }
// Set the default window size. Helper for application init with hardcoded size. // Set the default window size. Helper for application init with hardcoded size.
pub fn window_size(&mut self, width: u16, height: u16) { pub fn window_size(&mut self, width: u32) {
self.size = Size::Hardcoded((width, height)); self.size = PanelSize::Custom(width);
} }
#[allow(clippy::cast_precision_loss)] #[allow(clippy::cast_precision_loss)]
@ -430,16 +414,14 @@ impl Context {
pub fn text<'a>(&self, msg: impl Into<Cow<'a, str>>) -> crate::widget::Text<'a, crate::Theme> { pub fn text<'a>(&self, msg: impl Into<Cow<'a, str>>) -> crate::widget::Text<'a, crate::Theme> {
let msg = msg.into(); let msg = msg.into();
let t = match self.size {
Size::Hardcoded(_) => crate::widget::text, let size = self.size.get_applet_icon_size_with_padding(false);
Size::PanelSize(ref s) => {
let size = s.get_applet_icon_size_with_padding(false);
let size_threshold_small = PanelSize::S.get_applet_icon_size_with_padding(false); let size_threshold_small = PanelSize::S.get_applet_icon_size_with_padding(false);
let size_threshold_medium = PanelSize::M.get_applet_icon_size_with_padding(false); let size_threshold_medium = PanelSize::M.get_applet_icon_size_with_padding(false);
let size_threshold_large = PanelSize::L.get_applet_icon_size_with_padding(false); let size_threshold_large = PanelSize::L.get_applet_icon_size_with_padding(false);
if size <= size_threshold_small { let t = if size <= size_threshold_small {
crate::widget::text::body crate::widget::text::body
} else if size <= size_threshold_medium { } else if size <= size_threshold_medium {
crate::widget::text::title4 crate::widget::text::title4
@ -447,9 +429,8 @@ impl Context {
crate::widget::text::title3 crate::widget::text::title3
} else { } else {
crate::widget::text::title2 crate::widget::text::title2
}
}
}; };
t(msg).font(crate::font::default()) t(msg).font(crate::font::default())
} }
} }