libcosmic/src/applet/mod.rs

439 lines
15 KiB
Rust
Raw Normal View History

#[cfg(feature = "applet-token")]
pub mod token;
2023-08-08 18:09:57 -04:00
use crate::{
app::Core,
cctk::sctk,
2023-08-08 18:09:57 -04:00
iced::{
self,
alignment::{Horizontal, Vertical},
widget::Container,
window, Color, Length, Limits, Rectangle,
},
iced_style, iced_widget,
theme::{self, system_dark, system_light, Button, THEME},
2024-07-16 11:15:22 -04:00
widget::{self, layer_container},
Application, Element, Renderer,
2023-08-08 18:09:57 -04:00
};
2024-07-16 11:15:22 -04:00
use cctk::sctk::shell::xdg::window::WindowConfigure;
2023-08-08 18:09:57 -04:00
pub use cosmic_panel_config;
use cosmic_panel_config::{CosmicPanelBackground, PanelAnchor, PanelSize};
use cosmic_theme::Theme;
use iced::Pixels;
2024-01-30 22:14:00 -05:00
use iced_core::{Padding, Shadow};
use iced_style::container::Appearance;
2023-08-08 18:09:57 -04:00
use iced_widget::runtime::command::platform_specific::wayland::popup::{
SctkPopupSettings, SctkPositioner,
};
use sctk::reexports::protocols::xdg::shell::client::xdg_positioner::{Anchor, Gravity};
2024-07-16 11:15:22 -04:00
use std::{borrow::Cow, num::NonZeroU32, rc::Rc};
2023-08-08 18:09:57 -04:00
use crate::app::cosmic;
2023-08-08 18:09:57 -04:00
#[derive(Debug, Clone)]
pub struct Context {
2023-08-08 18:09:57 -04:00
pub size: Size,
pub anchor: PanelAnchor,
pub background: CosmicPanelBackground,
pub output_name: String,
pub panel_type: PanelType,
2024-07-16 11:15:22 -04:00
/// Includes the suggested size of the window.
/// This can be used by apples to handle overflow themselves.
pub configure: Option<WindowConfigure>,
2023-08-08 18:09:57 -04:00
}
2024-07-16 11:15:22 -04:00
#[derive(Clone, Debug, PartialEq, Eq)]
2023-08-08 18:09:57 -04:00
pub enum Size {
// (width, height)
Hardcoded((u16, u16)),
2024-07-16 11:15:22 -04:00
PanelSize(PanelSize),
2023-08-08 18:09:57 -04:00
}
#[derive(Clone, Debug, PartialEq)]
pub enum PanelType {
Panel,
Dock,
Other(String),
}
impl ToString for PanelType {
fn to_string(&self) -> String {
match self {
Self::Panel => "Panel".to_string(),
Self::Dock => "Dock".to_string(),
Self::Other(other) => other.clone(),
}
}
}
impl From<String> for PanelType {
fn from(value: String) -> Self {
match value.as_str() {
"Panel" => PanelType::Panel,
"Dock" => PanelType::Dock,
other => PanelType::Other(other.to_string()),
}
}
}
impl Default for Context {
2023-08-08 18:09:57 -04:00
fn default() -> Self {
Self {
size: Size::PanelSize(
std::env::var("COSMIC_PANEL_SIZE")
.ok()
.and_then(|size| ron::from_str(size.as_str()).ok())
.unwrap_or(PanelSize::S),
),
anchor: std::env::var("COSMIC_PANEL_ANCHOR")
.ok()
.and_then(|size| ron::from_str(size.as_str()).ok())
.unwrap_or(PanelAnchor::Top),
background: std::env::var("COSMIC_PANEL_BACKGROUND")
.ok()
.and_then(|size| ron::from_str(size.as_str()).ok())
.unwrap_or(CosmicPanelBackground::ThemeDefault),
output_name: std::env::var("COSMIC_PANEL_OUTPUT").unwrap_or_default(),
panel_type: PanelType::from(std::env::var("COSMIC_PANEL_NAME").unwrap_or_default()),
2024-07-16 11:15:22 -04:00
configure: None,
2023-08-08 18:09:57 -04:00
}
}
}
impl Context {
2023-08-08 18:09:57 -04:00
#[must_use]
2024-04-15 18:23:02 -04:00
pub fn suggested_size(&self, is_symbolic: bool) -> (u16, u16) {
2023-08-08 18:09:57 -04:00
match &self.size {
Size::PanelSize(ref size) => {
2024-04-15 18:23:02 -04:00
let s = size.get_applet_icon_size(is_symbolic) as u16;
(s, s)
}
2023-08-08 18:09:57 -04:00
Size::Hardcoded((width, height)) => (*width, *height),
}
}
2024-07-16 11:15:22 -04:00
#[must_use]
pub fn suggested_window_size(&self) -> (NonZeroU32, NonZeroU32) {
let suggested = self.suggested_size(true);
let applet_padding = self.suggested_padding(true);
let configured_width = self
.configure
.as_ref()
.and_then(|c| c.new_size.0.map(|w| w))
.unwrap_or_else(|| {
NonZeroU32::new(suggested.0 as u32 + applet_padding as u32 * 2).unwrap()
});
let configured_height = self
.configure
.as_ref()
.and_then(|c| c.new_size.1.map(|h| h))
.unwrap_or_else(|| {
NonZeroU32::new(suggested.1 as u32 + applet_padding as u32 * 2).unwrap()
});
(configured_width, configured_height)
}
#[must_use]
2024-04-15 18:23:02 -04:00
pub fn suggested_padding(&self, is_symbolic: bool) -> u16 {
match &self.size {
2024-04-15 18:23:02 -04:00
Size::PanelSize(ref size) => size.get_applet_padding(is_symbolic),
Size::Hardcoded(_) => 8,
}
}
2023-08-08 18:09:57 -04:00
// Set the default window size. Helper for application init with hardcoded size.
pub fn window_size(&mut self, width: u16, height: u16) {
self.size = Size::Hardcoded((width, height));
}
#[must_use]
#[allow(clippy::cast_precision_loss)]
pub fn window_settings(&self) -> crate::app::Settings {
2024-04-15 18:23:02 -04:00
let (width, height) = self.suggested_size(true);
2023-12-08 11:58:33 -05:00
let width = f32::from(width);
let height = f32::from(height);
2024-04-15 18:23:02 -04:00
let applet_padding = self.suggested_padding(true);
let mut settings = crate::app::Settings::default()
2023-12-08 11:58:33 -05:00
.size(iced_core::Size::new(
width + applet_padding as f32 * 2.,
height + applet_padding as f32 * 2.,
2023-12-08 11:58:33 -05:00
))
2023-08-08 18:09:57 -04:00
.size_limits(
Limits::NONE
.min_height(height as f32 + applet_padding as f32 * 2.0)
2024-07-16 11:15:22 -04:00
.min_width(width as f32 + applet_padding as f32 * 2.0),
2023-08-08 18:09:57 -04:00
)
.resizable(None)
.default_text_size(14.0)
2024-10-03 21:27:06 +02:00
.default_font(crate::font::default())
.transparent(true);
if let Some(theme) = self.theme() {
settings = settings.theme(theme);
}
settings
2023-08-08 18:09:57 -04:00
}
2024-07-16 11:15:22 -04:00
#[must_use]
pub fn is_horizontal(&self) -> bool {
matches!(self.anchor, PanelAnchor::Top | PanelAnchor::Bottom)
}
2023-08-08 18:09:57 -04:00
#[must_use]
pub fn icon_button_from_handle<'a, Message: 'static>(
2023-08-08 18:09:57 -04:00
&self,
icon: widget::icon::Handle,
) -> crate::widget::Button<'a, Message> {
2024-07-16 11:15:22 -04:00
let suggested = self.suggested_size(icon.symbolic);
let applet_padding = self.suggested_padding(icon.symbolic);
let (mut configured_width, mut configured_height) = self.suggested_window_size();
// Adjust the width to include padding and force the crosswise dim to match the window size
let is_horizontal = self.is_horizontal();
if is_horizontal {
configured_width =
NonZeroU32::new(suggested.0 as u32 + applet_padding as u32 * 2).unwrap();
} else {
configured_height =
NonZeroU32::new(suggested.1 as u32 + applet_padding as u32 * 2).unwrap();
}
let symbolic = icon.symbolic;
2024-07-16 11:15:22 -04:00
crate::widget::button::custom(
2024-07-16 11:15:22 -04:00
layer_container(
widget::icon(icon)
.style(if symbolic {
theme::Svg::Custom(Rc::new(|theme| crate::iced_style::svg::Appearance {
color: Some(theme.cosmic().background.on.into()),
}))
} else {
theme::Svg::default()
})
.width(Length::Fixed(suggested.0 as f32))
.height(Length::Fixed(suggested.1 as f32)),
)
.align_x(Horizontal::Center)
.align_y(Vertical::Center)
.width(Length::Fill)
.height(Length::Fill),
)
2024-07-16 11:15:22 -04:00
.width(Length::Fixed(configured_width.get() as f32))
.height(Length::Fixed(configured_height.get() as f32))
.style(Button::AppletIcon)
2023-08-08 18:09:57 -04:00
}
#[must_use]
pub fn icon_button<'a, Message: 'static>(
&self,
icon_name: &'a str,
) -> crate::widget::Button<'a, Message> {
self.icon_button_from_handle(
widget::icon::from_name(icon_name)
.symbolic(true)
2024-04-15 18:23:02 -04:00
.size(self.suggested_size(true).0)
.into(),
)
}
2023-08-08 18:09:57 -04:00
// TODO popup container which tracks the size of itself and requests the popup to resize to match
pub fn popup_container<'a, Message: 'static>(
&self,
content: impl Into<Element<'a, Message>>,
2024-01-30 22:14:00 -05:00
) -> Container<'a, Message, crate::Theme, Renderer> {
2023-08-08 18:09:57 -04:00
let (vertical_align, horizontal_align) = match self.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),
};
2024-01-30 22:14:00 -05:00
Container::<Message, _, Renderer>::new(
Container::<Message, _, Renderer>::new(content).style(theme::Container::custom(
|theme| {
let cosmic = theme.cosmic();
let corners = cosmic.corner_radii.clone();
Appearance {
text_color: Some(cosmic.background.on.into()),
background: Some(Color::from(cosmic.background.base).into()),
border: iced::Border {
radius: corners.radius_m.into(),
width: 1.0,
color: cosmic.background.divider.into(),
},
shadow: Shadow::default(),
icon_color: Some(cosmic.background.on.into()),
}
},
)),
)
2023-08-08 18:09:57 -04:00
.width(Length::Shrink)
.height(Length::Shrink)
.align_x(horizontal_align)
.align_y(vertical_align)
}
#[must_use]
#[allow(clippy::cast_possible_wrap)]
pub fn get_popup_settings(
&self,
parent: window::Id,
id: window::Id,
size: Option<(u32, u32)>,
width_padding: Option<i32>,
height_padding: Option<i32>,
) -> SctkPopupSettings {
2024-04-15 18:23:02 -04:00
let (width, height) = self.suggested_size(true);
let applet_padding = self.suggested_padding(true);
2023-08-08 18:09:57 -04:00
let pixel_offset = 8;
let (offset, anchor, gravity) = match self.anchor {
PanelAnchor::Left => ((pixel_offset, 0), Anchor::Right, Gravity::Right),
PanelAnchor::Right => ((-pixel_offset, 0), Anchor::Left, Gravity::Left),
PanelAnchor::Top => ((0, pixel_offset), Anchor::Bottom, Gravity::Bottom),
PanelAnchor::Bottom => ((0, -pixel_offset), 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(applet_padding as i32) * 2 + i32::from(width),
height: height_padding.unwrap_or(applet_padding as i32) * 2 + i32::from(height),
2023-08-08 18:09:57 -04:00
},
reactive: true,
constraint_adjustment: 15, // slide_y, slide_x, flip_x, flip_y
..Default::default()
},
parent_size: None,
grab: true,
}
}
#[must_use]
pub fn theme(&self) -> Option<theme::Theme> {
2023-08-08 18:09:57 -04:00
match self.background {
CosmicPanelBackground::Dark => {
let mut theme = system_dark();
theme.theme_type.prefer_dark(Some(true));
Some(theme)
}
CosmicPanelBackground::Light => {
let mut theme = system_light();
theme.theme_type.prefer_dark(Some(false));
Some(theme)
}
_ => Some(theme::system_preference()),
2023-08-08 18:09:57 -04:00
}
}
pub fn text<'a>(&self, msg: impl Into<Cow<'a, str>>) -> crate::widget::Text<'a, crate::Theme> {
let msg = msg.into();
let t = match self.size {
2024-05-17 14:42:39 -04:00
Size::PanelSize(PanelSize::XL) => crate::widget::text::title2,
Size::PanelSize(PanelSize::L) => crate::widget::text::title3,
Size::PanelSize(PanelSize::M) => crate::widget::text::title4,
Size::PanelSize(PanelSize::S) => crate::widget::text::body,
Size::PanelSize(PanelSize::XS) => crate::widget::text::body,
Size::Hardcoded(_) => crate::widget::text,
};
2024-10-03 21:27:06 +02:00
t(msg).font(crate::font::default())
}
2023-08-08 18:09:57 -04:00
}
/// Launch the application with the given settings.
///
/// # Errors
///
/// Returns error on application failure.
pub fn run<App: Application>(autosize: bool, flags: App::Flags) -> iced::Result {
let helper = Context::default();
2023-08-08 18:09:57 -04:00
let mut settings = helper.window_settings();
settings.autosize = autosize;
if autosize {
settings.size_limits = Limits::NONE;
}
2023-08-08 18:09:57 -04:00
if let Some(icon_theme) = settings.default_icon_theme {
crate::icon_theme::set_default(icon_theme);
}
2023-12-08 11:58:33 -05:00
let (width, height) = (settings.size.width as u32, settings.size.height as u32);
2023-08-08 18:09:57 -04:00
let mut core = Core::default();
core.window.show_window_menu = false;
core.window.show_headerbar = false;
core.window.sharp_corners = true;
core.window.show_maximize = false;
core.window.show_minimize = false;
core.window.use_template = false;
core.debug = settings.debug;
core.set_scale_factor(settings.scale_factor);
2023-12-08 11:58:33 -05:00
core.set_window_width(width);
core.set_window_height(height);
2023-08-08 18:09:57 -04:00
THEME.lock().unwrap().set_theme(settings.theme.theme_type);
2023-08-08 18:09:57 -04:00
let mut iced = iced::Settings::with_flags((core, flags));
iced.antialiasing = settings.antialiasing;
iced.default_font = settings.default_font;
2023-12-01 16:24:37 -05:00
iced.default_text_size = settings.default_text_size.into();
2023-08-08 18:09:57 -04:00
iced.id = Some(App::APP_ID.to_owned());
{
use iced::wayland::actions::window::SctkWindowSettings;
use iced_sctk::settings::InitialSurface;
iced.initial_surface = InitialSurface::XdgWindow(SctkWindowSettings {
app_id: Some(App::APP_ID.to_owned()),
autosize: settings.autosize,
client_decorations: settings.client_decorations,
resizable: settings.resizable,
2023-12-08 11:58:33 -05:00
size: (width, height),
2023-08-08 18:09:57 -04:00
size_limits: settings.size_limits,
title: None,
transparent: settings.transparent,
..SctkWindowSettings::default()
});
}
<cosmic::Cosmic<App> as iced::Application>::run(iced)
}
#[must_use]
pub fn style() -> <crate::Theme as iced_style::application::StyleSheet>::Style {
<crate::Theme as iced_style::application::StyleSheet>::Style::Custom(Box::new(|theme| {
iced_style::application::Appearance {
background_color: Color::from_rgba(0.0, 0.0, 0.0, 0.0),
text_color: theme.cosmic().on_bg_color().into(),
icon_color: theme.cosmic().on_bg_color().into(),
2023-08-08 18:09:57 -04:00
}
}))
}
pub fn menu_button<'a, Message>(
content: impl Into<Element<'a, Message>>,
) -> crate::widget::Button<'a, Message> {
crate::widget::button::custom(content)
.style(Button::AppletMenu)
2023-10-20 13:05:04 -04:00
.padding(menu_control_padding())
.width(Length::Fill)
}
2023-10-20 13:05:04 -04:00
pub fn padded_control<'a, Message>(
content: impl Into<Element<'a, Message>>,
2024-01-30 22:14:00 -05:00
) -> crate::widget::container::Container<'a, Message, crate::Theme, crate::Renderer> {
2023-10-20 13:05:04 -04:00
crate::widget::container(content)
.padding(menu_control_padding())
.width(Length::Fill)
}
pub fn menu_control_padding() -> Padding {
let guard = THEME.lock().unwrap();
let cosmic = guard.cosmic();
[cosmic.space_xxs(), cosmic.space_m()].into()
2023-10-20 13:05:04 -04:00
}