cosmic/lib.rs
1// Copyright 2022 System76 <info@system76.com>
2// SPDX-License-Identifier: MPL-2.0
3
4#![allow(clippy::module_name_repetitions)]
5#![cfg_attr(target_os = "redox", feature(lazy_cell))]
6#![cfg_attr(docsrs, feature(doc_cfg))]
7
8//! # The COSMIC Toolkit
9//!
10//! Quickly code your project with modular elements created with customization in mind.
11//! Panels, applets, theming, tiling, launcher, app library, keyboard shortcuts and
12//! dynamic or pinned workspaces are all made flexible to bend to your users needs.
13//!
14//! [COSMIC](https://system76.com/cosmic) empowers frictionless development by being
15//! approachable, easy to maintain, and modular. Use the same language and toolkit to
16//! build apps and applets, with helpful templates provided. Even shell components and
17//! the compositor use the same toolkit. Learn once and use your knowledge anywhere in
18//! the desktop.
19//!
20//! ## Architecture
21//!
22//! Based on [iced](https://iced.rs/), COSMIC apps and applets are modeled with the
23//! MVU (Model-View-Update) pattern from [The Elm Architecture](https://guide.elm-lang.org/architecture/).
24//! For more details, see the [architecture page of the iced book](https://book.iced.rs/architecture.html).
25//!
26//! An application will consist of:
27//!
28//! * An application **model** for holding persistent application state which implements
29//! the [Application] trait.
30//! * A [view](Application::view) function which borrows data from the model to construct
31//! a view with stateless widgets
32//! * An [update](Application::update) function which receives application messages from
33//! widgets, tasks, and subscriptions.
34//!
35//! Messages handled by the update function are used to update the
36//! application model and spawn background tasks that can emit messages back to the
37//! app's update function.
38//!
39//! ### Tasks
40//!
41//! Tasks returned by the update function are scheduled for concurrent execution on a
42//! background thread managed by the application's async executor, which is tokio by default.
43//! They can be constructed from futures and may also stream events back to the application
44//! asynchronously.
45//!
46//! ### Subscriptions
47//!
48//! Applications may also use a [subscription](Application::subscription) function to subscribe to external
49//! asynchronous event streams. These can run perpetually from application start; or
50//! optionally started, stopped, and restarted based on changes to the application model
51//! between updates. Such as:
52//!
53//! * Conditionally starting and stopping a subscription based on the state of a boolean
54//! value or enum
55//! * Restarting a subscription when the hash of its borrowed data changes
56//! * Dynamically spawning a subscription for each item in a list.
57//!
58//! ## Templates
59//!
60//! Get started using [cargo-generate](https://github.com/cargo-generate/cargo-generate)
61//! with one of the following templates. The app template is for developing desktop
62//! applications and the applet template is for developing COSMIC applets.
63//!
64//! - [App Template](https://github.com/pop-os/cosmic-app-template/)
65//! - [Applet Template](https://github.com/pop-os/cosmic-applet-template/)
66//!
67//! ## Widgets
68//!
69//! Reference the [`widget`] module for available widgets for use in the view function.
70//! Widgets are composable and can be configured through chainable builder methods.
71//! Compose widgets together to create complex interfaces and higher level widgets.
72//!
73//! Composed widgets may be managed by their own custom type with its own view and
74//! update functions. It is a common pattern to use these functions within the
75//! application's own view and update functions. If using a custom type, implement
76//! [From] for the [Element] type to have API treat a composed widget the same as a
77//! native custom widget.
78//!
79//! If a widget does not exist for a specific use case, use the [Widget](iced::advanced::Widget)
80//! trait to create an advanced custom widget. This can then be used to composed higher
81//! level widgets by chaining composable widgets together.
82//!
83//! ## Core
84//!
85//! Every application model requires a [cosmic::Core](app::Core). This contains
86//! application state which is managed by libcosmic's runtime for its generated
87//! interfaces. Such as the context drawner, nav bar, and the headerbar. This can be
88//! used by the app to subscribe to configuration changes and to emit events to the
89//! libcosmic-managed portion of the application's state and view.
90
91/// Recommended default imports.
92pub mod prelude {
93 #[cfg(feature = "winit")]
94 pub use crate::ApplicationExt;
95 pub use crate::ext::*;
96 pub use crate::{Also, Apply, Element, Renderer, Task, Theme};
97}
98
99pub use apply::{Also, Apply};
100
101/// Actions are managed internally by the cosmic runtime.
102pub mod action;
103pub use action::Action;
104
105pub mod anim;
106
107#[cfg(feature = "winit")]
108pub mod app;
109#[cfg(feature = "winit")]
110#[doc(inline)]
111pub use app::{Application, ApplicationExt};
112
113#[cfg(feature = "applet")]
114pub mod applet;
115
116pub mod command;
117
118/// State which is managed by the cosmic runtime.
119pub mod core;
120#[doc(inline)]
121pub use core::Core;
122
123pub mod config;
124
125#[doc(inline)]
126pub use cosmic_config;
127
128#[doc(inline)]
129pub use cosmic_theme;
130
131#[cfg(feature = "single-instance")]
132pub mod dbus_activation;
133#[cfg(feature = "single-instance")]
134pub use dbus_activation::DbusActivation;
135
136#[cfg(feature = "desktop")]
137pub mod desktop;
138
139#[cfg(any(feature = "xdg-portal", feature = "rfd"))]
140pub mod dialog;
141
142pub mod executor;
143#[cfg(feature = "tokio")]
144pub use executor::single::Executor as SingleThreadExecutor;
145
146mod ext;
147
148pub mod font;
149
150#[doc(inline)]
151pub use iced;
152
153pub mod icon_theme;
154pub mod keyboard_nav;
155
156mod localize;
157
158#[cfg(all(target_env = "gnu", not(target_os = "windows")))]
159pub(crate) mod malloc;
160
161#[cfg(all(feature = "process", not(windows)))]
162pub mod process;
163
164#[doc(inline)]
165#[cfg(all(feature = "wayland", target_os = "linux"))]
166pub use cctk;
167
168pub mod surface;
169
170pub use iced::Task;
171pub mod task;
172
173pub mod theme;
174
175pub mod scroll;
176
177#[doc(inline)]
178pub use theme::{Theme, style};
179
180pub mod widget;
181type Plain = iced_core::text::paragraph::Plain<<Renderer as iced_core::text::Renderer>::Paragraph>;
182type Paragraph = <Renderer as iced_core::text::Renderer>::Paragraph;
183pub type Renderer = iced::Renderer;
184pub type Element<'a, Message> = iced::Element<'a, Message, crate::Theme, Renderer>;