Use cosmic::Application/cosmic::app::applet

This saves a bit of duplicated boilerplate.

Applets seem to work fairly well with this. Though we should see if any
other changes to initial window settings are needed in certain applets
for the exact behavior we want.
This commit is contained in:
Ian Douglas Scott 2023-08-03 13:22:17 -07:00
parent 3ad64df5f3
commit a23f181b7f
28 changed files with 2029 additions and 2597 deletions

View file

@ -1,11 +1,4 @@
use cosmic::{
iced::Limits,
iced::{self, wayland::InitialSurface, Application},
iced_runtime::core::window,
iced_style::application,
theme::Theme,
};
use cosmic_applet::CosmicAppletHelper;
use cosmic::{app, iced, iced_style::application, theme::Theme};
use freedesktop_desktop_entry::DesktopEntry;
use std::{env, fs, process::Command};
@ -17,70 +10,47 @@ struct Desktop {
}
struct Button {
core: cosmic::app::Core,
desktop: Desktop,
applet_helper: CosmicAppletHelper,
theme: Theme,
}
#[derive(Debug, Clone)]
enum Msg {
Press,
Theme(Theme),
}
impl iced::Application for Button {
impl cosmic::Application for Button {
type Message = Msg;
type Theme = cosmic::Theme;
type Executor = cosmic::SingleThreadExecutor;
type Flags = Desktop;
const APP_ID: &'static str = "com.system76.CosmicPanelButton";
fn new(desktop: Desktop) -> (Self, iced::Command<Msg>) {
let applet_helper = CosmicAppletHelper::default();
let theme = applet_helper.theme();
(
Button {
desktop,
applet_helper,
theme,
},
iced::Command::none(),
)
fn init(core: cosmic::app::Core, desktop: Desktop) -> (Self, app::Command<Msg>) {
(Button { core, desktop }, app::Command::none())
}
fn title(&self) -> String {
String::from("Button")
fn core(&self) -> &cosmic::app::Core {
&self.core
}
fn close_requested(&self, _id: window::Id) -> Msg {
unimplemented!()
fn core_mut(&mut self) -> &mut cosmic::app::Core {
&mut self.core
}
fn style(&self) -> <Self::Theme as application::StyleSheet>::Style {
<Self::Theme as application::StyleSheet>::Style::Custom(Box::new(|theme| {
application::Appearance {
background_color: iced::Color::from_rgba(0.0, 0.0, 0.0, 0.0),
text_color: theme.cosmic().on_bg_color().into(),
}
}))
fn style(&self) -> Option<<Theme as application::StyleSheet>::Style> {
Some(cosmic::app::applet::style())
}
fn subscription(&self) -> iced::Subscription<Msg> {
self.applet_helper.theme_subscription(0).map(Msg::Theme)
}
fn update(&mut self, message: Msg) -> iced::Command<Msg> {
fn update(&mut self, message: Msg) -> app::Command<Msg> {
match message {
Msg::Press => {
let _ = Command::new("sh").arg("-c").arg(&self.desktop.exec).spawn();
}
Msg::Theme(t) => {
self.theme = t;
}
}
iced::Command::none()
app::Command::none()
}
fn view(&self, _id: window::Id) -> cosmic::Element<Msg> {
fn view(&self) -> cosmic::Element<Msg> {
// TODO icon?
cosmic::widget::button(cosmic::theme::Button::Text)
.text(&self.desktop.name)
@ -119,17 +89,5 @@ pub fn main() -> iced::Result {
let desktop = desktop.expect(&format!(
"Failed to find valid desktop file '{filename}' in search paths"
));
let helper = CosmicAppletHelper::default();
let mut settings = iced::Settings {
flags: desktop,
..helper.window_settings()
};
match &mut settings.initial_surface {
InitialSurface::XdgWindow(s) => {
s.autosize = true;
s.size_limits = Limits::NONE.min_height(1.0).min_width(1.0);
}
_ => unreachable!(),
};
Button::run(settings)
cosmic::app::applet::run::<Button>(true, desktop)
}