From 43c78fa0df86fddf50c8561676224d5a3e150815 Mon Sep 17 00:00:00 2001 From: Michael Aaron Murphy Date: Wed, 1 Nov 2023 16:29:30 +0100 Subject: [PATCH] feat(examples): add image button example --- examples/image-button/Cargo.toml | 13 ++++ examples/image-button/src/main.rs | 105 ++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 examples/image-button/Cargo.toml create mode 100644 examples/image-button/src/main.rs diff --git a/examples/image-button/Cargo.toml b/examples/image-button/Cargo.toml new file mode 100644 index 00000000..b87ac435 --- /dev/null +++ b/examples/image-button/Cargo.toml @@ -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"] diff --git a/examples/image-button/src/main.rs b/examples/image-button/src/main.rs new file mode 100644 index 00000000..04d64440 --- /dev/null +++ b/examples/image-button/src/main.rs @@ -0,0 +1,105 @@ +// Copyright 2023 System76 +// 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> { + cosmic::app::run::(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) { + let mut app = App { core }; + + let command = app.update_title(); + + (app, command) + } + + /// Handle application events here. + fn update(&mut self, message: Self::Message) -> Command { + if let Message::Clicked = message { + eprintln!("clicked"); + } + + Command::none() + } + + /// Creates a view after each update. + fn view(&self) -> Element { + 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 { + self.set_header_title(String::from("Image Button Demo")); + self.set_window_title(String::from("Image Button Demo")) + } +}