2023-11-01 16:29:30 +01:00
|
|
|
// Copyright 2023 System76 <info@system76.com>
|
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
|
|
|
|
//! Application API example
|
|
|
|
|
|
|
|
|
|
use cosmic::app::{Command, Core, Settings};
|
|
|
|
|
use cosmic::{executor, iced, ApplicationExt, Element};
|
|
|
|
|
|
|
|
|
|
/// Runs application with these settings
|
|
|
|
|
#[rustfmt::skip]
|
|
|
|
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
|
cosmic::app::run::<App>(Settings::default(), ())?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Messages that are used specifically by our [`App`].
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
pub enum Message {
|
2023-11-15 16:09:17 +01:00
|
|
|
Clicked(usize),
|
|
|
|
|
Remove(usize),
|
2023-11-01 16:29:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The [`App`] stores application-specific state.
|
|
|
|
|
pub struct App {
|
|
|
|
|
core: Core,
|
2023-11-15 16:09:17 +01:00
|
|
|
selected: usize,
|
|
|
|
|
images: Vec<String>,
|
2023-11-01 16:29:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Implement [`cosmic::Application`] to integrate with COSMIC.
|
|
|
|
|
impl cosmic::Application for App {
|
|
|
|
|
/// Default async executor to use with the app.
|
|
|
|
|
type Executor = executor::Default;
|
|
|
|
|
|
|
|
|
|
/// Argument received [`cosmic::Application::new`].
|
|
|
|
|
type Flags = ();
|
|
|
|
|
|
|
|
|
|
/// Message type specific to our [`App`].
|
|
|
|
|
type Message = Message;
|
|
|
|
|
|
|
|
|
|
/// The unique application ID to supply to the window manager.
|
|
|
|
|
const APP_ID: &'static str = "org.cosmic.AppDemo";
|
|
|
|
|
|
|
|
|
|
fn core(&self) -> &Core {
|
|
|
|
|
&self.core
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn core_mut(&mut self) -> &mut Core {
|
|
|
|
|
&mut self.core
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Creates the application, and optionally emits command on initialize.
|
|
|
|
|
fn init(core: Core, _input: Self::Flags) -> (Self, Command<Self::Message>) {
|
2023-11-15 16:09:17 +01:00
|
|
|
let mut app = App {
|
|
|
|
|
core,
|
|
|
|
|
selected: 0,
|
|
|
|
|
images: vec![
|
|
|
|
|
"/usr/share/backgrounds/pop/kait-herzog-8242.jpg".into(),
|
|
|
|
|
"/usr/share/backgrounds/pop/kate-hazen-unleash-your-robot-blue.png".into(),
|
|
|
|
|
],
|
|
|
|
|
};
|
2023-11-01 16:29:30 +01:00
|
|
|
|
|
|
|
|
let command = app.update_title();
|
|
|
|
|
|
|
|
|
|
(app, command)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Handle application events here.
|
|
|
|
|
fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
|
2023-11-15 16:09:17 +01:00
|
|
|
match message {
|
|
|
|
|
Message::Clicked(id) => self.selected = id,
|
|
|
|
|
Message::Remove(id) => {
|
|
|
|
|
self.images.remove(id);
|
|
|
|
|
}
|
2023-11-01 16:29:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Command::none()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Creates a view after each update.
|
|
|
|
|
fn view(&self) -> Element<Self::Message> {
|
2023-11-15 16:09:17 +01:00
|
|
|
let mut content = cosmic::widget::column().spacing(12);
|
|
|
|
|
|
|
|
|
|
for (id, image) in self.images.iter().enumerate() {
|
|
|
|
|
content = content.push(
|
|
|
|
|
cosmic::widget::button::image(image)
|
|
|
|
|
.width(300.0)
|
|
|
|
|
.on_press(Message::Clicked(id))
|
|
|
|
|
.selected(self.selected == id)
|
|
|
|
|
.on_remove(Message::Remove(id)),
|
2023-11-01 16:29:30 +01:00
|
|
|
);
|
2023-11-15 16:09:17 +01:00
|
|
|
}
|
2023-11-01 16:29:30 +01:00
|
|
|
|
|
|
|
|
let centered = cosmic::widget::container(content)
|
|
|
|
|
.width(iced::Length::Fill)
|
|
|
|
|
.height(iced::Length::Shrink)
|
|
|
|
|
.align_x(iced::alignment::Horizontal::Center)
|
|
|
|
|
.align_y(iced::alignment::Vertical::Center);
|
|
|
|
|
|
|
|
|
|
Element::from(centered)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl App
|
|
|
|
|
where
|
|
|
|
|
Self: cosmic::Application,
|
|
|
|
|
{
|
|
|
|
|
fn update_title(&mut self) -> Command<Message> {
|
|
|
|
|
self.set_header_title(String::from("Image Button Demo"));
|
|
|
|
|
self.set_window_title(String::from("Image Button Demo"))
|
|
|
|
|
}
|
|
|
|
|
}
|