feat!: implement Application API

This commit is contained in:
Michael Murphy 2023-08-02 11:54:07 +02:00 committed by GitHub
parent e24465ba37
commit a223b60a0c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 1420 additions and 138 deletions

View file

@ -0,0 +1,13 @@
[package]
name = "application"
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,131 @@
// Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
//! Testing ground for improving COSMIC application API ergonomics.
use cosmic::app::{Command, Core, Settings};
use cosmic::widget::nav_bar;
use cosmic::{executor, iced, ApplicationExt, Element};
/// Runs application with these settings
#[rustfmt::skip]
fn main() -> Result<(), Box<dyn std::error::Error>> {
let input = vec![
("Page 1".into(), "🖖 Hello from libcosmic.".into()),
("Page 2".into(), "🌟 This is an example application.".into()),
("Page 3".into(), "🚧 The libcosmic API is not stable yet.".into()),
("Page 4".into(), "🚀 Copy the source code and experiment today!".into()),
];
let settings = Settings::default()
.antialiasing(true)
.client_decorations(true)
.debug(false)
.default_icon_theme("Pop")
.default_text_size(16.0)
.scale_factor(1.0)
.size((1024, 768))
.theme(cosmic::Theme::dark());
cosmic::app::run::<App>(settings, input)?;
Ok(())
}
/// Messages that are used specifically by our [`App`].
#[derive(Clone, Debug)]
pub enum Message {}
/// The [`App`] stores application-specific state.
pub struct App {
core: Core,
nav_model: nav_bar::Model,
}
/// 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 = Vec<(String, String)>;
/// Message type specific to our [`App`].
type Message = Message;
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 nav_model = nav_bar::Model::default();
for (title, content) in input {
nav_model.insert().text(title).data(content);
}
nav_model.activate_position(0);
let mut app = App { core, nav_model };
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.
fn on_nav_select(&mut self, id: nav_bar::Id) -> Command<Self::Message> {
self.nav_model.activate(id);
self.update_title()
}
fn update(&mut self, _message: Self::Message) -> Command<Self::Message> {
Command::none()
}
fn view(&self) -> Element<Self::Message> {
let page_content = self
.nav_model
.active_data::<String>()
.map(String::as_str)
.unwrap_or("No page selected");
let text = cosmic::widget::text(page_content);
let centered = iced::widget::container(text)
.width(iced::Length::Fill)
.height(iced::Length::Fill)
.align_x(iced::alignment::Horizontal::Center)
.align_y(iced::alignment::Vertical::Center);
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")
}
fn update_title(&mut self) -> Command<Message> {
let title = self.active_page_title().to_owned();
self.set_title(title)
}
}

View file

@ -1,14 +1,11 @@
use cosmic::{
iced::{wayland::InitialSurface, Application},
settings,
};
use cosmic::iced::{wayland::InitialSurface, Application, Settings};
mod window;
pub use window::Window;
pub fn main() -> cosmic::iced::Result {
settings::set_default_icon_theme("Pop");
let mut settings = settings();
cosmic::icon_theme::set_default("Pop");
let mut settings = Settings::default();
settings.initial_surface = InitialSurface::XdgWindow(Default::default());
Window::run(settings)
}

View file

@ -6,7 +6,7 @@ use cosmic::{
iced::{
wayland::window::{start_drag_window, toggle_maximize},
widget::{column, container, horizontal_space, pick_list, progress_bar, row, slider},
window, Color, Event,
window, Color,
},
iced_futures::Subscription,
iced_style::application,
@ -15,7 +15,7 @@ use cosmic::{
widget::{
button, cosmic_container, header_bar, nav_bar, nav_bar_toggle,
rectangle_tracker::{rectangle_tracker_subscription, RectangleTracker, RectangleUpdate},
scrollable, segmented_button, segmented_selection, settings, toggler, IconSource,
scrollable, segmented_button, segmented_selection, settings, IconSource,
},
Element, ElementExt,
};
@ -336,9 +336,8 @@ impl Application for Window {
.on_drag(Message::Drag)
.start(
nav_bar_toggle()
.on_nav_bar_toggled(nav_bar_message)
.nav_bar_active(nav_bar_toggled)
.into(),
.on_toggle(nav_bar_message)
.active(nav_bar_toggled),
);
if self.show_maximize {
@ -509,7 +508,7 @@ impl Application for Window {
self.theme.clone()
}
fn close_requested(&self, id: window::Id) -> Self::Message {
fn close_requested(&self, _id: window::Id) -> Self::Message {
Message::Close
}
fn subscription(&self) -> iced::Subscription<Self::Message> {

View file

@ -1,7 +1,7 @@
// Copyright 2022 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
use cosmic::{iced::Application, settings};
use cosmic::iced::{Application, Settings};
mod window;
use env_logger::Env;
@ -13,8 +13,8 @@ pub fn main() -> cosmic::iced::Result {
.write_style_or("MY_LOG_STYLE", "always");
env_logger::init_from_env(env);
settings::set_default_icon_theme("Pop");
let mut settings = settings();
cosmic::icon_theme::set_default("Pop");
let mut settings = Settings::default();
settings.window.min_size = Some((600, 300));
Window::run(settings)
}

View file

@ -483,9 +483,8 @@ impl Application for Window {
.on_drag(Message::Drag)
.start(
nav_bar_toggle()
.on_nav_bar_toggled(nav_bar_message)
.nav_bar_active(nav_bar_toggled)
.into(),
.on_toggle(nav_bar_message)
.active(nav_bar_toggled),
);
if self.show_maximize {

View file

@ -184,7 +184,7 @@ impl State {
Message::IconTheme(key) => {
self.icon_themes.activate(key);
if let Some(theme) = self.icon_themes.text(key) {
cosmic::settings::set_default_icon_theme(theme);
cosmic::icon_theme::set_default(theme);
}
}
Message::InputChanged(s) => {