2023-11-14 16:55:06 -05:00
|
|
|
#[cfg(feature = "applet-token")]
|
|
|
|
|
pub mod token;
|
|
|
|
|
|
2023-08-08 18:09:57 -04:00
|
|
|
use crate::{
|
2024-10-16 20:36:46 -04:00
|
|
|
app::{self, iced_settings, Core},
|
2023-11-14 16:55:06 -05:00
|
|
|
cctk::sctk,
|
2023-08-08 18:09:57 -04:00
|
|
|
iced::{
|
|
|
|
|
self,
|
|
|
|
|
alignment::{Horizontal, Vertical},
|
|
|
|
|
widget::Container,
|
|
|
|
|
window, Color, Length, Limits, Rectangle,
|
|
|
|
|
},
|
2024-10-16 20:36:46 -04:00
|
|
|
iced_widget,
|
2024-05-13 22:11:59 -04:00
|
|
|
theme::{self, system_dark, system_light, Button, THEME},
|
2024-10-16 20:36:46 -04:00
|
|
|
widget::{
|
|
|
|
|
self,
|
|
|
|
|
autosize::{autosize, Autosize},
|
|
|
|
|
layer_container,
|
|
|
|
|
},
|
2024-07-16 11:15:22 -04:00
|
|
|
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};
|
2024-05-15 14:33:42 -04:00
|
|
|
use cosmic_theme::Theme;
|
|
|
|
|
use iced::Pixels;
|
2024-01-30 22:14:00 -05:00
|
|
|
use iced_core::{Padding, Shadow};
|
2024-10-16 20:36:46 -04:00
|
|
|
use iced_widget::runtime::platform_specific::wayland::popup::{SctkPopupSettings, SctkPositioner};
|
2023-08-08 18:09:57 -04:00
|
|
|
use sctk::reexports::protocols::xdg::shell::client::xdg_positioner::{Anchor, Gravity};
|
2024-10-16 20:36:46 -04:00
|
|
|
use std::{borrow::Cow, num::NonZeroU32, rc::Rc, sync::LazyLock};
|
|
|
|
|
use tracing::info;
|
2023-08-08 18:09:57 -04:00
|
|
|
|
2023-09-18 07:45:11 +02:00
|
|
|
use crate::app::cosmic;
|
2024-10-16 20:36:46 -04:00
|
|
|
static AUTOSIZE_ID: LazyLock<iced::id::Id> =
|
|
|
|
|
LazyLock::new(|| iced::id::Id::new("cosmic-applet-autosize"));
|
|
|
|
|
static AUTOSIZE_MAIN_ID: LazyLock<iced::id::Id> =
|
|
|
|
|
LazyLock::new(|| iced::id::Id::new("cosmic-applet-autosize-main"));
|
2023-08-08 18:09:57 -04:00
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
2023-09-18 07:45:11 +02:00
|
|
|
pub struct Context {
|
2023-08-08 18:09:57 -04:00
|
|
|
pub size: Size,
|
|
|
|
|
pub anchor: PanelAnchor,
|
|
|
|
|
pub background: CosmicPanelBackground,
|
|
|
|
|
pub output_name: String,
|
2024-05-06 15:56:13 -05:00
|
|
|
pub panel_type: PanelType,
|
2024-10-16 20:36:46 -04:00
|
|
|
/// Includes the configured size of the window.
|
2024-07-16 11:15:22 -04:00
|
|
|
/// This can be used by apples to handle overflow themselves.
|
2024-10-21 10:57:29 -04:00
|
|
|
pub suggested_bounds: Option<iced::Size>,
|
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
|
|
|
}
|
2024-05-06 15:56:13 -05:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
|
|
|
pub enum PanelType {
|
|
|
|
|
Panel,
|
|
|
|
|
Dock,
|
|
|
|
|
Other(String),
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-06 16:09:35 -05:00
|
|
|
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(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-06 15:56:13 -05:00
|
|
|
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()),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-18 07:45:11 +02:00
|
|
|
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(),
|
2024-05-06 15:56:13 -05:00
|
|
|
panel_type: PanelType::from(std::env::var("COSMIC_PANEL_NAME").unwrap_or_default()),
|
2024-10-16 20:36:46 -04:00
|
|
|
suggested_bounds: None,
|
2023-08-08 18:09:57 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-18 07:45:11 +02: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 {
|
2024-03-08 17:21:34 -05:00
|
|
|
Size::PanelSize(ref size) => {
|
2024-04-15 18:23:02 -04:00
|
|
|
let s = size.get_applet_icon_size(is_symbolic) as u16;
|
2024-03-08 17:21:34 -05:00
|
|
|
(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
|
2024-10-16 20:36:46 -04:00
|
|
|
.suggested_bounds
|
2024-07-16 11:15:22 -04:00
|
|
|
.as_ref()
|
2024-10-16 20:36:46 -04:00
|
|
|
.and_then(|c| NonZeroU32::new(c.width as u32)) // TODO: should this be physical size instead of logical?
|
2024-07-16 11:15:22 -04:00
|
|
|
.unwrap_or_else(|| {
|
|
|
|
|
NonZeroU32::new(suggested.0 as u32 + applet_padding as u32 * 2).unwrap()
|
|
|
|
|
});
|
2024-10-16 20:36:46 -04:00
|
|
|
|
2024-07-16 11:15:22 -04:00
|
|
|
let configured_height = self
|
2024-10-16 20:36:46 -04:00
|
|
|
.suggested_bounds
|
2024-07-16 11:15:22 -04:00
|
|
|
.as_ref()
|
2024-10-16 20:36:46 -04:00
|
|
|
.and_then(|c| NonZeroU32::new(c.height as u32))
|
2024-07-16 11:15:22 -04:00
|
|
|
.unwrap_or_else(|| {
|
|
|
|
|
NonZeroU32::new(suggested.1 as u32 + applet_padding as u32 * 2).unwrap()
|
|
|
|
|
});
|
2024-10-16 20:36:46 -04:00
|
|
|
info!("{configured_height:?}");
|
2024-07-16 11:15:22 -04:00
|
|
|
(configured_width, configured_height)
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-08 17:21:34 -05:00
|
|
|
#[must_use]
|
2024-04-15 18:23:02 -04:00
|
|
|
pub fn suggested_padding(&self, is_symbolic: bool) -> u16 {
|
2024-03-08 17:21:34 -05:00
|
|
|
match &self.size {
|
2024-04-15 18:23:02 -04:00
|
|
|
Size::PanelSize(ref size) => size.get_applet_padding(is_symbolic),
|
|
|
|
|
Size::Hardcoded(_) => 8,
|
2024-03-08 17:21:34 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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)]
|
2023-09-18 07:45:11 +02:00
|
|
|
pub fn window_settings(&self) -> crate::app::Settings {
|
2024-04-15 18:23:02 -04:00
|
|
|
let (width, height) = self.suggested_size(true);
|
|
|
|
|
let applet_padding = self.suggested_padding(true);
|
2024-10-16 20:36:46 -04:00
|
|
|
let width = f32::from(width) + applet_padding as f32 * 2.;
|
|
|
|
|
let height = f32::from(height) + applet_padding as f32 * 2.;
|
2023-09-18 07:45:11 +02:00
|
|
|
let mut settings = crate::app::Settings::default()
|
2024-10-16 20:36:46 -04:00
|
|
|
.size(iced_core::Size::new(width, height))
|
2023-08-08 18:09:57 -04:00
|
|
|
.size_limits(
|
|
|
|
|
Limits::NONE
|
2024-10-16 20:36:46 -04:00
|
|
|
.min_height(height as f32)
|
|
|
|
|
.min_width(width as f32),
|
2023-08-08 18:09:57 -04:00
|
|
|
)
|
|
|
|
|
.resizable(None)
|
2024-06-06 22:47:27 +02:00
|
|
|
.default_text_size(14.0)
|
2024-10-03 21:27:06 +02:00
|
|
|
.default_font(crate::font::default())
|
2023-08-10 11:07:52 -04:00
|
|
|
.transparent(true);
|
|
|
|
|
if let Some(theme) = self.theme() {
|
2023-08-15 13:44:06 -07:00
|
|
|
settings = settings.theme(theme);
|
2023-08-10 11:07:52 -04:00
|
|
|
}
|
2024-10-16 20:36:46 -04:00
|
|
|
settings.exit_on_close = true;
|
2023-08-10 11:07:52 -04:00
|
|
|
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]
|
2024-03-27 09:10:41 -07:00
|
|
|
pub fn icon_button_from_handle<'a, Message: 'static>(
|
2023-08-08 18:09:57 -04:00
|
|
|
&self,
|
2024-03-27 09:10:41 -07:00
|
|
|
icon: widget::icon::Handle,
|
2024-05-17 19:08:43 +02:00
|
|
|
) -> crate::widget::Button<'a, Message> {
|
2024-10-16 20:36:46 -04:00
|
|
|
let mut suggested = self.suggested_size(icon.symbolic);
|
2024-07-16 11:15:22 -04:00
|
|
|
let applet_padding = self.suggested_padding(icon.symbolic);
|
|
|
|
|
|
|
|
|
|
let is_horizontal = self.is_horizontal();
|
2024-10-16 20:36:46 -04:00
|
|
|
|
2024-05-06 18:49:32 -04:00
|
|
|
let symbolic = icon.symbolic;
|
2024-07-16 11:15:22 -04:00
|
|
|
|
2024-09-16 19:11:29 +02:00
|
|
|
crate::widget::button::custom(
|
2024-07-16 11:15:22 -04:00
|
|
|
layer_container(
|
|
|
|
|
widget::icon(icon)
|
2024-10-16 20:36:46 -04:00
|
|
|
.class(if symbolic {
|
|
|
|
|
theme::Svg::Custom(Rc::new(|theme| crate::iced_widget::svg::Style {
|
2024-07-16 11:15:22 -04:00
|
|
|
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),
|
2023-09-17 17:35:50 -07:00
|
|
|
)
|
2024-10-16 20:36:46 -04:00
|
|
|
.width(Length::Fixed((suggested.0 + 2 * applet_padding) as f32))
|
|
|
|
|
.height(Length::Fixed((suggested.1 + 2 * applet_padding) as f32))
|
|
|
|
|
.class(Button::AppletIcon)
|
2023-08-08 18:09:57 -04:00
|
|
|
}
|
|
|
|
|
|
2024-03-27 09:10:41 -07:00
|
|
|
#[must_use]
|
|
|
|
|
pub fn icon_button<'a, Message: 'static>(
|
|
|
|
|
&self,
|
|
|
|
|
icon_name: &'a str,
|
2024-05-17 19:08:43 +02:00
|
|
|
) -> crate::widget::Button<'a, Message> {
|
2024-10-16 20:36:46 -04:00
|
|
|
let suggested_size = self.suggested_size(true);
|
2024-03-27 09:10:41 -07:00
|
|
|
self.icon_button_from_handle(
|
|
|
|
|
widget::icon::from_name(icon_name)
|
|
|
|
|
.symbolic(true)
|
2024-10-16 20:36:46 -04:00
|
|
|
.size(suggested_size.0)
|
2024-03-27 09:10:41 -07:00
|
|
|
.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-10-16 20:36:46 -04:00
|
|
|
) -> Autosize<'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-10-16 20:36:46 -04:00
|
|
|
autosize(
|
|
|
|
|
Container::<Message, _, Renderer>::new(
|
|
|
|
|
Container::<Message, _, Renderer>::new(content).style(|theme| {
|
2024-01-30 22:14:00 -05:00
|
|
|
let cosmic = theme.cosmic();
|
|
|
|
|
let corners = cosmic.corner_radii.clone();
|
2024-10-16 20:36:46 -04:00
|
|
|
iced_widget::container::Style {
|
2024-01-30 22:14:00 -05:00
|
|
|
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()),
|
|
|
|
|
}
|
2024-10-16 20:36:46 -04:00
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
.width(Length::Shrink)
|
|
|
|
|
.height(Length::Shrink)
|
|
|
|
|
.align_x(horizontal_align)
|
|
|
|
|
.align_y(vertical_align),
|
|
|
|
|
AUTOSIZE_ID.clone(),
|
|
|
|
|
)
|
|
|
|
|
.limits(
|
|
|
|
|
Limits::NONE
|
|
|
|
|
.min_width(1.)
|
|
|
|
|
.min_height(1.)
|
|
|
|
|
.max_width(500.)
|
|
|
|
|
.max_height(1000.),
|
2024-01-30 22:14:00 -05:00
|
|
|
)
|
2023-08-08 18:09:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[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);
|
2024-10-16 20:36:46 -04:00
|
|
|
let pixel_offset = 4;
|
2023-08-08 18:09:57 -04:00
|
|
|
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,
|
2024-03-08 17:21:34 -05:00
|
|
|
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,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-16 20:36:46 -04:00
|
|
|
pub fn autosize_window<'a, Message: 'static>(
|
|
|
|
|
&self,
|
|
|
|
|
content: impl Into<Element<'a, Message>>,
|
|
|
|
|
) -> Autosize<'a, Message, crate::Theme, crate::Renderer> {
|
|
|
|
|
let force_configured = matches!(&self.panel_type, &PanelType::Other(ref n) if n.is_empty());
|
|
|
|
|
let w = autosize(content, AUTOSIZE_MAIN_ID.clone());
|
|
|
|
|
let mut limits = Limits::NONE;
|
|
|
|
|
let suggested_window_size = self.suggested_window_size();
|
|
|
|
|
|
|
|
|
|
if let Some(width) = self
|
|
|
|
|
.suggested_bounds
|
|
|
|
|
.as_ref()
|
|
|
|
|
.filter(|c| c.width as i32 > 0)
|
|
|
|
|
.map(|c| c.width)
|
|
|
|
|
{
|
|
|
|
|
limits = limits.width(width as f32);
|
|
|
|
|
}
|
|
|
|
|
if let Some(height) = self
|
|
|
|
|
.suggested_bounds
|
|
|
|
|
.as_ref()
|
|
|
|
|
.filter(|c| c.height as i32 > 0)
|
|
|
|
|
.map(|c| c.height)
|
|
|
|
|
{
|
|
|
|
|
limits = limits.height(height as f32);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.limits(limits)
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-08 18:09:57 -04:00
|
|
|
#[must_use]
|
2023-08-10 11:07:52 -04:00
|
|
|
pub fn theme(&self) -> Option<theme::Theme> {
|
2023-08-08 18:09:57 -04:00
|
|
|
match self.background {
|
2024-05-13 22:11:59 -04:00
|
|
|
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)
|
|
|
|
|
}
|
2023-11-10 18:57:56 -05:00
|
|
|
_ => Some(theme::system_preference()),
|
2023-08-08 18:09:57 -04:00
|
|
|
}
|
|
|
|
|
}
|
2024-05-15 14:33:42 -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,
|
2024-05-15 14:33:42 -04:00
|
|
|
Size::PanelSize(PanelSize::XS) => crate::widget::text::body,
|
2024-05-15 14:39:30 -04:00
|
|
|
Size::Hardcoded(_) => crate::widget::text,
|
2024-05-15 14:33:42 -04:00
|
|
|
};
|
2024-10-03 21:27:06 +02:00
|
|
|
t(msg).font(crate::font::default())
|
2024-05-15 14:33:42 -04:00
|
|
|
}
|
2023-08-08 18:09:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Launch the application with the given settings.
|
|
|
|
|
///
|
|
|
|
|
/// # Errors
|
|
|
|
|
///
|
|
|
|
|
/// Returns error on application failure.
|
2024-10-16 20:36:46 -04:00
|
|
|
pub fn run<App: Application>(flags: App::Flags) -> iced::Result {
|
2023-09-18 07:45:11 +02:00
|
|
|
let helper = Context::default();
|
2024-10-16 20:36:46 -04:00
|
|
|
|
2023-08-08 18:09:57 -04:00
|
|
|
let mut settings = helper.window_settings();
|
2024-10-16 20:36:46 -04:00
|
|
|
settings.resizable = None;
|
2023-08-08 18:09:57 -04:00
|
|
|
|
2024-10-16 20:36:46 -04:00
|
|
|
if let Some(icon_theme) = settings.default_icon_theme.clone() {
|
2023-08-08 18:09:57 -04:00
|
|
|
crate::icon_theme::set_default(icon_theme);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-16 20:36:46 -04:00
|
|
|
THEME
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.set_theme(settings.theme.theme_type.clone());
|
2023-12-08 11:58:33 -05:00
|
|
|
|
2024-10-16 20:36:46 -04:00
|
|
|
let (iced_settings, (mut core, flags, mut window_settings)) =
|
|
|
|
|
iced_settings::<App>(settings, flags);
|
2023-08-08 18:09:57 -04:00
|
|
|
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;
|
|
|
|
|
|
2024-10-16 20:36:46 -04:00
|
|
|
window_settings.decorations = false;
|
|
|
|
|
window_settings.exit_on_close_request = true;
|
|
|
|
|
window_settings.resizable = false;
|
|
|
|
|
window_settings.resize_border = 0;
|
|
|
|
|
|
|
|
|
|
// TODO make multi-window not mandatory
|
|
|
|
|
|
|
|
|
|
let mut app = super::app::multi_window::multi_window(
|
|
|
|
|
cosmic::Cosmic::title,
|
|
|
|
|
cosmic::Cosmic::update,
|
|
|
|
|
cosmic::Cosmic::view,
|
|
|
|
|
);
|
2024-10-21 17:32:16 -04:00
|
|
|
if core.main_window.is_none() {
|
2024-10-16 20:36:46 -04:00
|
|
|
app = app.window(window_settings.clone());
|
2024-10-21 17:32:16 -04:00
|
|
|
core.main_window = Some(iced_core::window::Id::RESERVED);
|
2023-08-08 18:09:57 -04:00
|
|
|
}
|
2024-10-16 20:36:46 -04:00
|
|
|
app.subscription(cosmic::Cosmic::subscription)
|
|
|
|
|
.style(cosmic::Cosmic::style)
|
|
|
|
|
.theme(cosmic::Cosmic::theme)
|
|
|
|
|
.settings(iced_settings)
|
|
|
|
|
.run_with(move || cosmic::Cosmic::<App>::init((core, flags, window_settings)))
|
2023-08-08 18:09:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[must_use]
|
2024-10-16 20:36:46 -04:00
|
|
|
pub fn style() -> iced_runtime::Appearance {
|
|
|
|
|
let theme = crate::theme::THEME.lock().unwrap();
|
|
|
|
|
iced_runtime::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
|
|
|
}
|
2023-10-19 14:35:46 -04:00
|
|
|
|
|
|
|
|
pub fn menu_button<'a, Message>(
|
|
|
|
|
content: impl Into<Element<'a, Message>>,
|
2024-05-17 19:08:43 +02:00
|
|
|
) -> crate::widget::Button<'a, Message> {
|
2024-09-16 19:11:29 +02:00
|
|
|
crate::widget::button::custom(content)
|
2024-10-16 20:36:46 -04:00
|
|
|
.class(Button::AppletMenu)
|
2023-10-20 13:05:04 -04:00
|
|
|
.padding(menu_control_padding())
|
2023-10-19 14:35:46 -04:00
|
|
|
.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 {
|
2024-08-02 20:00:16 +02:00
|
|
|
let guard = THEME.lock().unwrap();
|
|
|
|
|
let cosmic = guard.cosmic();
|
|
|
|
|
[cosmic.space_xxs(), cosmic.space_m()].into()
|
2023-10-20 13:05:04 -04:00
|
|
|
}
|