zng_app/third_party.rs
1//! Third party licenses service and types.
2
3use zng_app_context::app_local;
4pub use zng_tp_licenses::{License, LicenseUsed, User, UserLicense};
5use zng_var::{ArcVar, Var as _, var};
6
7use crate::{
8 event::{CommandNameExt as _, command},
9 view_process::VIEW_PROCESS,
10};
11
12/// Third party licenses.
13pub struct LICENSES;
14
15impl LICENSES {
16 /// Aggregates all registered third party licenses, grouped by license, sorted by name.
17 ///
18 /// Exact licenses and users are deduplicated.
19 pub fn licenses(&self) -> Vec<LicenseUsed> {
20 let mut r = vec![];
21
22 let sv = LICENSES_SV.read();
23
24 for l in sv.sources.iter() {
25 let l = l();
26 zng_tp_licenses::merge_licenses(&mut r, l);
27 }
28
29 if sv.include_view_process.get() {
30 let l = self.view_process_licenses();
31 zng_tp_licenses::merge_licenses(&mut r, l);
32 }
33
34 zng_tp_licenses::sort_licenses(&mut r);
35
36 r
37 }
38
39 /// Third party licenses provided by the view-process, grouped by license, sorted by name.
40 ///
41 /// Returns an empty vec if there is no view-process running or the view-process does not provide any license.
42 pub fn view_process_licenses(&self) -> Vec<LicenseUsed> {
43 let mut r = VIEW_PROCESS.third_party_licenses().unwrap_or_default();
44 zng_tp_licenses::sort_licenses(&mut r);
45 r
46 }
47
48 /// Aggregates all registered third party licenses, by user, sorted by name.
49 ///
50 /// Exact licenses and users are deduplicated.
51 pub fn user_licenses(&self) -> Vec<UserLicense> {
52 zng_tp_licenses::user_licenses(&self.licenses())
53 }
54
55 /// Third party licenses provided by the view-process, by user, sorted by name.
56 ///
57 /// Returns an empty vec if there is no view-process running or the view-process does not provide any license.
58 pub fn view_process_user_licenses(&self) -> Vec<UserLicense> {
59 zng_tp_licenses::user_licenses(&self.view_process_licenses())
60 }
61
62 /// If view-process provided third party licenses are included in [`licenses`].
63 ///
64 /// Note that prebuilt view-process licenses may not be found by license scraper tools.
65 ///
66 /// This is `true` by default.
67 ///
68 /// [`licenses`]: Self::licenses
69 pub fn include_view_process(&self) -> ArcVar<bool> {
70 LICENSES_SV.read().include_view_process.clone()
71 }
72
73 /// Register a function that loads some third party licenses used by this app.
74 pub fn register(&self, source: fn() -> Vec<LicenseUsed>) {
75 LICENSES_SV.write().sources.push(source);
76 }
77}
78
79app_local! {
80 static LICENSES_SV: Licenses = Licenses {
81 sources: vec![],
82 include_view_process: var(true),
83 };
84}
85
86struct Licenses {
87 sources: Vec<fn() -> Vec<LicenseUsed>>,
88 include_view_process: ArcVar<bool>,
89}
90
91command! {
92 /// Open or focus the third party licenses screen.
93 ///
94 /// Note that the `zng` crate provides a default implementation for this command, you can override this
95 /// default by handling the command in an [`on_pre_event`] handle.
96 ///
97 /// [`on_pre_event`]: crate::event::Command::on_pre_event
98 pub static OPEN_LICENSES_CMD = {
99 l10n!: true,
100 name: "Third Party Licenses"
101 };
102}