Skip to main content

zng/
third_party.rs

1#![cfg(feature = "third_party")]
2
3//! Third party licenses service and types.
4//!
5//! Rust projects depend on many crated with a variety of licenses, some of these licenses require that they must be
6//! displayed in the app binary, usually in an "about" screen. This module can be used together with the [`zng_tp_licenses`]
7//! crate to collect and embed licenses of all used crates in your project.
8//!
9//! The [`LICENSES`] service serves as an aggregation center for licenses of multiple sources, the [`OPEN_LICENSES_CMD`]
10//! can be implemented [`on_pre_event`] to show a custom licenses screen, or it can just be used to show the default
11//! screen provided by the default app.
12//!
13//! # Embedding Setup
14//!
15//! Follow these steps to configure your crate and build workflow to collect and embed crate licenses.
16//!
17//! ### Install `cargo about`
18//!
19//! To collect and embed licenses in your project you must have [`cargo-about`] installed:
20//!
21//! ```console
22//! cargo install cargo-about
23//! ```
24//!
25//! Next add file `.cargo/about.toml` in your crate or workspace root:
26//!
27//! ```toml
28//! # cargo about generate -c .cargo/about.toml --format json --workspace --all-features
29//!
30//! accepted = [
31//!     "Apache-2.0",
32//!     "MIT",
33//!     "MPL-2.0",
34//!     "Unicode-DFS-2016",
35//!     "BSL-1.0",
36//!     "BSD-2-Clause",
37//!     "BSD-3-Clause",
38//!     "ISC",
39//!     "Zlib",
40//!     "CC0-1.0",
41//! ]
42//!
43//! ignore-build-dependencies = true
44//! ignore-dev-dependencies = true
45//! filter-noassertion = true
46//! private = { ignore = true }
47//! ```
48//!
49//! Next call the command to test and modify the `accepted` config:
50//!
51//! ```console
52//! cargo about generate -c .cargo/about.toml --format json --workspace --all-features
53//! ```
54//!
55//! If the command prints a JSON dump you are done with this step.
56//!
57//! ### Add `zng-tp-licenses`
58//!
59//! Next, add dependency to the [`zng_tp_licenses`] your crate `Cargo.toml`:
60//!
61//! ```toml
62//! [package]
63//! resolver = "2" # recommended, to not include "build" feature in the normal dependency.
64//!
65//! [features]
66//! # Recommended, so you only embed in release builds.
67//! #
68//! # Note that if you use a feature, don't forget to build with `--features embed_licenses`.
69//! embed_licenses = ["dep:zng-tp-licenses"]
70//!
71//! [dependencies]
72//! zng-tp-licenses = { version = "0.10.0", features = ["embed"], optional = true }
73//!
74//! [build-dependencies]
75//! zng-tp-licenses = { version = "0.2.0", features = ["build"], optional = true }
76//! ```
77//!
78//! ### Implement Embed
79//!
80//! Next, in your crates build script (`build.rs`) add:
81//!
82//! ```
83//! fn main() {
84//!     #[cfg(feature = "embed_licenses")]
85//!     {
86//!         let licenses = zng_tp_licenses::collect_cargo_about("../.cargo/about.toml");
87//!         zng_tp_licenses::write_embedding(&licenses);
88//!     }
89//! }
90//! ```
91//!
92//! Implement a function that includes the embedding and decodes it. Register the function it in your app init code:
93//!
94//! ```
95//! #[cfg(feature = "embed_licenses")]
96//! fn embedded_licenses() -> Vec<zng::third_party::LicenseUsed> {
97//!     zng_tp_licenses::include_embedding!()
98//! }
99//!
100//! # fn demo() {
101//! # use zng::prelude::*;
102//! APP.defaults().run(async {
103//!     #[cfg(feature = "embed_licenses")]
104//!     zng::third_party::LICENSES.register(embedded_licenses);
105//! });
106//! # }
107//! # fn main() { }
108//! ```
109//!
110//! ### Review Licenses
111//!
112//! Call the [`OPEN_LICENSES_CMD`] in a test button, check if all the required licenses are present,
113//! `cargo about` and `zng_tp_licenses` are a **best effort only** helpers, you must ensure that the generated results
114//! meet yours or your company's legal obligations.
115//!
116//! ```
117//! use zng::prelude::*;
118//!
119//! fn review_licenses() -> UiNode {
120//!     // zng::third_party::LICENSES.include_view_process().set(false);
121//!
122//!     Button!(zng::third_party::OPEN_LICENSES_CMD)
123//! }
124//! ```
125//!
126//! #### Limitations
127//!
128//! Only crate licenses reachable thought cargo metadata are included. Static linked libraries in `-sys` crates may
129//! have required licenses that are not included. Other resources such as fonts and images may also be licensed.
130//!
131//! The [`LICENSES`] service accepts multiple sources, so you can implement your own custom embedding, the [`zng_tp_licenses`]
132//! crate provides helpers for manually encoding (compressing) licenses. See the `zng-view` build script for an example of
133//! how to include more licenses.
134//!
135//! # Full API
136//!
137//! See [`zng_app::third_party`] and [`zng_tp_licenses`] for the full API.
138//!
139//! [`zng_tp_licenses`]: https://zng-ui.github.io/doc/zng_tp_licenses/
140//! [`cargo-about`]: https://github.com/EmbarkStudios/cargo-about/
141//! [`on_pre_event`]: crate::event::Command::on_pre_event
142
143pub use zng_app::third_party::{LICENSES, License, LicenseUsed, OPEN_LICENSES_CMD, User, UserLicense};
144
145#[cfg(feature = "third_party_default")]
146pub(crate) fn setup_default_view() {
147    use crate::prelude::*;
148    use zng_wgt_container::ChildInsert;
149
150    let id = WindowId::named("zng-third_party-default");
151    OPEN_LICENSES_CMD
152        .on_event(
153            true,
154            true,
155            false,
156            hn!(|args| {
157                args.propagation.stop();
158
159                let parent = FOCUS.focused().with(|p| p.as_ref().map(|p| p.window_id()));
160
161                WINDOWS.focus_or_open(id, async move {
162                    if let Some(p) = parent
163                        && let Some(p) = WINDOWS.vars(p)
164                    {
165                        let v = WINDOW.vars();
166                        p.icon().set_bind(&v.icon()).perm();
167                    }
168
169                    Window! {
170                        title = l10n!(
171                            "window.title",
172                            "{$app} - Third Party Licenses",
173                            app = zng::env::about().app.clone()
174                        );
175                        child = default_view();
176                        can_fullscreen = false;
177                        parent;
178                    }
179                });
180            }),
181        )
182        .perm();
183
184    fn default_view() -> UiNode {
185        let mut licenses = LICENSES.user_licenses();
186        if licenses.is_empty() {
187            // l10n-# "user" is the package that uses the license
188            let user_name = l10n!("license-none.user-name", "<none>").get();
189            // l10n-# License name
190            let license_name = l10n!("license-none.name", "No license data").get();
191            licenses.push(UserLicense {
192                user: User::new(user_name, "", ""),
193                license: License::new(l10n!("license-none.id", "<none>").get(), license_name, ""),
194            });
195        }
196        let selected = var(licenses[0].clone());
197        let search = var(Txt::from(""));
198
199        let actual_width = var(zng_layout::unit::Dip::new(0));
200        let alternate_layout = actual_width.map(|&w| w <= 500 && w > 1);
201
202        let selector = Container! {
203            widget::background_color = light_dark(rgb(0.82, 0.82, 0.82), rgb(0.18, 0.18, 0.18));
204
205            // search
206            child_top = TextInput! {
207                txt = search.clone();
208                style_fn = zng_wgt_text_input::SearchStyle!();
209                zng_wgt_input::focus::focus_shortcut = [shortcut![CTRL + 'F'], shortcut![Find]];
210                placeholder_txt = l10n!("search.placeholder", "search licenses ({$shortcut})", shortcut = "Ctrl+F");
211            };
212            // list
213            child = Scroll! {
214                layout::min_width = 100;
215                layout::sticky_width = true;
216                mode = zng::scroll::ScrollMode::VERTICAL;
217                child_align = Align::FILL;
218                child = DataView! {
219                    view::<Txt> =
220                        search,
221                        hn!(selected, |a| {
222                            let search = a.data().get();
223                            let licenses = if search.is_empty() {
224                                licenses.clone()
225                            } else {
226                                licenses.iter().filter(|t| t.user.name.contains(search.as_str())).cloned().collect()
227                            };
228
229                            a.set_view(Stack! {
230                                toggle::selector = toggle::Selector::single(selected.clone());
231                                direction = StackDirection::top_to_bottom();
232                                children = licenses.into_iter().map(default_item_view);
233                            })
234                        }),
235                    ;
236                };
237                when *#{alternate_layout.clone()} {
238                    layout::max_height = 100; // placed on top in small width screens
239                    layout::sticky_width = false; // reset sticky width
240                }
241            };
242        };
243
244        Container! {
245            layout::actual_width;
246
247            child_insert = {
248                placement: alternate_layout.map(|&y| if y { ChildInsert::Top } else { ChildInsert::Start }),
249                node: selector,
250            };
251            // selected
252            child = Scroll! {
253                mode = zng::scroll::ScrollMode::VERTICAL;
254                child_align = Align::TOP_START;
255                padding = 10;
256                child = zng::markdown::Markdown! {
257                    txt = selected.map(default_markdown);
258                    txt_selectable = true;
259                };
260            };
261        }
262    }
263
264    fn default_item_view(item: UserLicense) -> UiNode {
265        let txt = if item.user.version.is_empty() {
266            item.user.name.clone()
267        } else {
268            formatx!("{} - {}", item.user.name, item.user.version)
269        };
270        Toggle! {
271            child = Text!(txt);
272            value = item;
273            child_align = layout::Align::START;
274            widget::corner_radius = 0;
275            layout::padding = 2;
276            widget::border = unset!;
277        }
278    }
279
280    fn default_markdown(item: &UserLicense) -> Txt {
281        use std::fmt::*;
282
283        let mut t = Txt::from("");
284
285        if item.user.version.is_empty() {
286            writeln!(&mut t, "# {}\n", item.user.name).unwrap();
287        } else {
288            writeln!(&mut t, "# {} - {}\n", item.user.name, item.user.version).unwrap();
289        }
290        if !item.user.url.is_empty() {
291            writeln!(&mut t, "[{0}]({0})\n", item.user.url).unwrap();
292        }
293
294        writeln!(&mut t, "## {}\n\n", item.license.name).unwrap();
295
296        if !item.license.text.is_empty() {
297            writeln!(&mut t, "```\n{}\n```\n", item.license.text).unwrap();
298        }
299
300        t.end_mut();
301        t
302    }
303}