cadmus_core/view/settings_editor/kinds/
library.rs

1//! Setting kinds for the Libraries category.
2
3use super::{SettingData, SettingIdentity, SettingKind, WidgetKind};
4use crate::fl;
5use crate::i18n::I18nDisplay;
6use crate::settings::{FinishedAction, Settings};
7use crate::view::{EntryId, EntryKind, Event};
8
9/// Shows a summary of a library (path) and opens the library editor on tap.
10pub struct LibraryInfo(pub usize);
11
12impl SettingKind for LibraryInfo {
13    fn identity(&self) -> SettingIdentity {
14        SettingIdentity::LibraryInfo(self.0)
15    }
16
17    fn label(&self, settings: &Settings) -> String {
18        settings
19            .libraries
20            .get(self.0)
21            .map(|lib| lib.name.clone())
22            .unwrap_or_else(|| fl!("settings-general-unknown"))
23    }
24
25    fn fetch(&self, settings: &Settings) -> SettingData {
26        let value = settings
27            .libraries
28            .get(self.0)
29            .map(|lib| lib.path.display().to_string())
30            .unwrap_or_else(|| fl!("settings-general-unknown"));
31
32        SettingData {
33            value,
34            widget: WidgetKind::ActionLabel(Event::EditLibrary(self.0)),
35        }
36    }
37}
38
39/// Library name editing setting
40pub struct LibraryName(pub usize);
41
42impl SettingKind for LibraryName {
43    fn identity(&self) -> SettingIdentity {
44        SettingIdentity::LibraryName(self.0)
45    }
46
47    fn label(&self, _settings: &Settings) -> String {
48        fl!("settings-library-name")
49    }
50
51    fn fetch(&self, settings: &Settings) -> SettingData {
52        let value = settings
53            .libraries
54            .get(self.0)
55            .map(|lib| lib.name.clone())
56            .unwrap_or_else(|| fl!("settings-general-unknown"));
57
58        SettingData {
59            value,
60            widget: WidgetKind::ActionLabel(Event::Select(EntryId::EditLibraryName)),
61        }
62    }
63}
64
65/// Library path editing setting
66pub struct LibraryPath(pub usize);
67
68impl SettingKind for LibraryPath {
69    fn identity(&self) -> SettingIdentity {
70        SettingIdentity::LibraryPath(self.0)
71    }
72
73    fn label(&self, _settings: &Settings) -> String {
74        fl!("settings-library-path")
75    }
76
77    fn fetch(&self, settings: &Settings) -> SettingData {
78        let value = settings
79            .libraries
80            .get(self.0)
81            .map(|lib| lib.path.display().to_string())
82            .unwrap_or_else(|| fl!("settings-general-unknown"));
83
84        SettingData {
85            value,
86            widget: WidgetKind::ActionLabel(Event::Select(EntryId::EditLibraryPath)),
87        }
88    }
89}
90
91/// Library finished action setting
92pub struct LibraryFinishedAction(pub usize);
93
94impl SettingKind for LibraryFinishedAction {
95    fn identity(&self) -> SettingIdentity {
96        SettingIdentity::LibraryFinishedAction(self.0)
97    }
98
99    fn label(&self, _settings: &Settings) -> String {
100        fl!("settings-library-end-of-book-action")
101    }
102
103    fn fetch(&self, settings: &Settings) -> SettingData {
104        let index = self.0;
105        let current = settings.libraries.get(index).and_then(|lib| lib.finished);
106
107        let value = current
108            .map(|action| action.to_i18n_string())
109            .unwrap_or_else(|| fl!("settings-library-inherit"));
110
111        let entries = vec![
112            EntryKind::RadioButton(
113                fl!("settings-library-inherit"),
114                EntryId::ClearLibraryFinishedAction(index),
115                current.is_none(),
116            ),
117            EntryKind::RadioButton(
118                FinishedAction::Notify.to_i18n_string(),
119                EntryId::SetLibraryFinishedAction(index, FinishedAction::Notify),
120                current == Some(FinishedAction::Notify),
121            ),
122            EntryKind::RadioButton(
123                FinishedAction::Close.to_i18n_string(),
124                EntryId::SetLibraryFinishedAction(index, FinishedAction::Close),
125                current == Some(FinishedAction::Close),
126            ),
127            EntryKind::RadioButton(
128                FinishedAction::GoToNext.to_i18n_string(),
129                EntryId::SetLibraryFinishedAction(index, FinishedAction::GoToNext),
130                current == Some(FinishedAction::GoToNext),
131            ),
132        ];
133
134        SettingData {
135            value,
136            widget: WidgetKind::SubMenu(entries),
137        }
138    }
139}