feat(examples): add image button example

This commit is contained in:
Michael Aaron Murphy 2023-11-01 16:29:30 +01:00 committed by Michael Murphy
parent 34386561b3
commit 43c78fa0df
2 changed files with 118 additions and 0 deletions

View file

@ -0,0 +1,13 @@
[package]
name = "cosmic-image-button"
version = "0.1.0"
edition = "2021"
[dependencies]
tracing = "0.1.37"
tracing-subscriber = "0.3.17"
[dependencies.libcosmic]
path = "../../"
default-features = false
features = ["debug", "wayland", "tokio"]

View file

@ -0,0 +1,105 @@
// 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 {
Clicked,
}
/// The [`App`] stores application-specific state.
pub struct App {
core: Core,
}
/// 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>) {
let mut app = App { core };
let command = app.update_title();
(app, command)
}
/// Handle application events here.
fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
if let Message::Clicked = message {
eprintln!("clicked");
}
Command::none()
}
/// Creates a view after each update.
fn view(&self) -> Element<Self::Message> {
let content = cosmic::widget::column()
.spacing(12)
.push(
cosmic::widget::button::image("/usr/share/backgrounds/pop/kait-herzog-8242.jpg")
.width(600.0)
.selected(true)
.on_press(Message::Clicked),
)
.push(
cosmic::widget::button::image(
"/usr/share/backgrounds/pop/kate-hazen-unleash-your-robot-blue.png",
)
.width(600.0)
.selected(true)
.on_press(Message::Clicked),
);
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"))
}
}