2023-08-02 11:54:07 +02:00
|
|
|
// Copyright 2023 System76 <info@system76.com>
|
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
2023-08-15 10:58:46 +02:00
|
|
|
//! Application API example
|
2023-08-02 11:54:07 +02:00
|
|
|
|
2024-10-16 20:36:46 -04:00
|
|
|
use cosmic::app::{Core, Settings, Task};
|
|
|
|
|
use cosmic::iced::widget::column;
|
2023-12-07 15:27:52 -05:00
|
|
|
use cosmic::iced_core::Size;
|
2023-08-02 11:54:07 +02:00
|
|
|
use cosmic::widget::nav_bar;
|
|
|
|
|
use cosmic::{executor, iced, ApplicationExt, Element};
|
|
|
|
|
|
2023-08-15 10:58:46 +02:00
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
|
pub enum Page {
|
|
|
|
|
Page1,
|
|
|
|
|
Page2,
|
|
|
|
|
Page3,
|
|
|
|
|
Page4,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Page {
|
|
|
|
|
const fn as_str(self) -> &'static str {
|
|
|
|
|
match self {
|
|
|
|
|
Page::Page1 => "Page 1",
|
|
|
|
|
Page::Page2 => "Page 2",
|
|
|
|
|
Page::Page3 => "Page 3",
|
|
|
|
|
Page::Page4 => "Page 4",
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-02 11:54:07 +02:00
|
|
|
/// Runs application with these settings
|
|
|
|
|
#[rustfmt::skip]
|
|
|
|
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
2024-03-01 20:21:52 -05:00
|
|
|
tracing_subscriber::fmt::init();
|
|
|
|
|
let _ = tracing_log::LogTracer::init();
|
2024-03-14 16:16:51 +01:00
|
|
|
|
2023-08-02 11:54:07 +02:00
|
|
|
let input = vec![
|
2023-08-15 10:58:46 +02:00
|
|
|
(Page::Page1, "🖖 Hello from libcosmic.".into()),
|
|
|
|
|
(Page::Page2, "🌟 This is an example application.".into()),
|
|
|
|
|
(Page::Page3, "🚧 The libcosmic API is not stable yet.".into()),
|
|
|
|
|
(Page::Page4, "🚀 Copy the source code and experiment today!".into()),
|
2023-08-02 11:54:07 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
let settings = Settings::default()
|
2024-03-01 20:21:52 -05:00
|
|
|
.size(Size::new(1024., 768.));
|
2023-08-02 11:54:07 +02:00
|
|
|
|
|
|
|
|
cosmic::app::run::<App>(settings, input)?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Messages that are used specifically by our [`App`].
|
|
|
|
|
#[derive(Clone, Debug)]
|
2024-10-16 20:36:46 -04:00
|
|
|
pub enum Message {
|
|
|
|
|
Input1(String),
|
|
|
|
|
Input2(String),
|
2024-10-21 09:12:41 -04:00
|
|
|
Ignore,
|
|
|
|
|
ToggleHide,
|
2024-10-16 20:36:46 -04:00
|
|
|
}
|
2023-08-02 11:54:07 +02:00
|
|
|
|
|
|
|
|
/// The [`App`] stores application-specific state.
|
|
|
|
|
pub struct App {
|
|
|
|
|
core: Core,
|
|
|
|
|
nav_model: nav_bar::Model,
|
2024-10-16 20:36:46 -04:00
|
|
|
input_1: String,
|
|
|
|
|
input_2: String,
|
2024-10-21 09:12:41 -04:00
|
|
|
hidden: bool,
|
2023-08-02 11:54:07 +02: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`].
|
2023-08-15 10:58:46 +02:00
|
|
|
type Flags = Vec<(Page, String)>;
|
2023-08-02 11:54:07 +02:00
|
|
|
|
|
|
|
|
/// Message type specific to our [`App`].
|
|
|
|
|
type Message = Message;
|
|
|
|
|
|
2023-09-13 16:01:04 +02:00
|
|
|
/// The unique application ID to supply to the window manager.
|
2023-08-02 11:54:07 +02:00
|
|
|
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.
|
2024-10-16 20:36:46 -04:00
|
|
|
fn init(core: Core, input: Self::Flags) -> (Self, Task<Self::Message>) {
|
2023-08-02 11:54:07 +02:00
|
|
|
let mut nav_model = nav_bar::Model::default();
|
|
|
|
|
|
|
|
|
|
for (title, content) in input {
|
2023-08-15 10:58:46 +02:00
|
|
|
nav_model.insert().text(title.as_str()).data(content);
|
2023-08-02 11:54:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nav_model.activate_position(0);
|
|
|
|
|
|
2024-10-16 20:36:46 -04:00
|
|
|
let mut app = App {
|
|
|
|
|
core,
|
|
|
|
|
nav_model,
|
|
|
|
|
input_1: String::new(),
|
|
|
|
|
input_2: String::new(),
|
2024-10-21 09:12:41 -04:00
|
|
|
hidden: true,
|
2024-10-16 20:36:46 -04:00
|
|
|
};
|
2023-08-02 11:54:07 +02:00
|
|
|
|
|
|
|
|
let command = app.update_title();
|
|
|
|
|
|
|
|
|
|
(app, command)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Allows COSMIC to integrate with your application's [`nav_bar::Model`].
|
|
|
|
|
fn nav_model(&self) -> Option<&nav_bar::Model> {
|
|
|
|
|
Some(&self.nav_model)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Called when a navigation item is selected.
|
2024-10-16 20:36:46 -04:00
|
|
|
fn on_nav_select(&mut self, id: nav_bar::Id) -> Task<Self::Message> {
|
2023-08-02 11:54:07 +02:00
|
|
|
self.nav_model.activate(id);
|
|
|
|
|
self.update_title()
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 10:58:46 +02:00
|
|
|
/// Handle application events here.
|
2024-10-16 20:36:46 -04:00
|
|
|
fn update(&mut self, message: Self::Message) -> Task<Self::Message> {
|
|
|
|
|
match message {
|
|
|
|
|
Message::Input1(v) => {
|
|
|
|
|
self.input_1 = v;
|
|
|
|
|
}
|
|
|
|
|
Message::Input2(v) => {
|
|
|
|
|
self.input_2 = v;
|
|
|
|
|
}
|
2024-10-21 09:12:41 -04:00
|
|
|
Message::Ignore => {}
|
|
|
|
|
Message::ToggleHide => {
|
|
|
|
|
self.hidden = !self.hidden;
|
|
|
|
|
}
|
2024-10-16 20:36:46 -04:00
|
|
|
}
|
|
|
|
|
Task::none()
|
2023-08-02 11:54:07 +02:00
|
|
|
}
|
|
|
|
|
|
2023-08-15 10:58:46 +02:00
|
|
|
/// Creates a view after each update.
|
2023-08-02 11:54:07 +02:00
|
|
|
fn view(&self) -> Element<Self::Message> {
|
|
|
|
|
let page_content = self
|
|
|
|
|
.nav_model
|
|
|
|
|
.active_data::<String>()
|
2024-03-14 16:16:51 +01:00
|
|
|
.map_or("No page selected", String::as_str);
|
2023-08-02 11:54:07 +02:00
|
|
|
|
|
|
|
|
let text = cosmic::widget::text(page_content);
|
|
|
|
|
|
2024-10-16 20:36:46 -04:00
|
|
|
let centered = cosmic::widget::container(
|
|
|
|
|
column![
|
|
|
|
|
text,
|
2024-10-21 09:12:41 -04:00
|
|
|
cosmic::widget::text_input::text_input("", &self.input_1)
|
|
|
|
|
.on_input(Message::Input1)
|
|
|
|
|
.on_clear(Message::Ignore),
|
|
|
|
|
cosmic::widget::text_input::secure_input(
|
|
|
|
|
"",
|
|
|
|
|
&self.input_1,
|
|
|
|
|
Some(Message::ToggleHide),
|
|
|
|
|
self.hidden
|
|
|
|
|
)
|
|
|
|
|
.on_input(Message::Input1),
|
2024-10-16 20:36:46 -04:00
|
|
|
cosmic::widget::text_input::text_input("", &self.input_1).on_input(Message::Input1),
|
2024-10-21 09:12:41 -04:00
|
|
|
cosmic::widget::text_input::search_input("", &self.input_2)
|
|
|
|
|
.on_input(Message::Input2)
|
|
|
|
|
.on_clear(Message::Ignore),
|
2024-10-16 20:36:46 -04:00
|
|
|
]
|
2023-08-02 11:54:07 +02:00
|
|
|
.width(iced::Length::Fill)
|
2023-09-01 07:29:19 +02:00
|
|
|
.height(iced::Length::Shrink)
|
2024-10-16 20:36:46 -04:00
|
|
|
.align_x(iced::alignment::Horizontal::Center),
|
|
|
|
|
)
|
|
|
|
|
.width(iced::Length::Fill)
|
|
|
|
|
.height(iced::Length::Shrink)
|
|
|
|
|
.align_x(iced::alignment::Horizontal::Center)
|
|
|
|
|
.align_y(iced::alignment::Vertical::Center);
|
2023-08-02 11:54:07 +02:00
|
|
|
|
|
|
|
|
Element::from(centered)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl App
|
|
|
|
|
where
|
|
|
|
|
Self: cosmic::Application,
|
|
|
|
|
{
|
|
|
|
|
fn active_page_title(&mut self) -> &str {
|
|
|
|
|
self.nav_model
|
|
|
|
|
.text(self.nav_model.active())
|
|
|
|
|
.unwrap_or("Unknown Page")
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-16 20:36:46 -04:00
|
|
|
fn update_title(&mut self) -> Task<Message> {
|
2023-10-12 13:54:08 +02:00
|
|
|
let header_title = self.active_page_title().to_owned();
|
|
|
|
|
let window_title = format!("{header_title} — COSMIC AppDemo");
|
|
|
|
|
self.set_header_title(header_title);
|
2024-10-16 20:36:46 -04:00
|
|
|
if let Some(id) = self.core.main_window_id() {
|
|
|
|
|
self.set_window_title(window_title, id)
|
|
|
|
|
} else {
|
|
|
|
|
Task::none()
|
|
|
|
|
}
|
2023-08-02 11:54:07 +02:00
|
|
|
}
|
|
|
|
|
}
|