Skip to main content

ferrix_app/
ferrix.rs

1/* ferrix.rs
2 *
3 * Copyright 2025-2026 Michail Krasnov <mskrasnov07@ya.ru>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: GPL-3.0-or-later
19 */
20
21//! Data from `ferrix-lib`
22
23use std::collections::HashSet;
24
25use crate::{
26    SETTINGS_PATH,
27    dmi::DMIData,
28    load_state::LoadState,
29    messages::Message,
30    pages::Page,
31    settings::{FXSettings, Style},
32    sidebar::sidebar,
33    utils::get_home,
34    widgets::line_charts::LineChart,
35};
36use ferrix_lib::{
37    battery::BatInfo,
38    cpu::{Processors, Stat},
39    cpu_freq::CpuFreq,
40    drm::Video,
41    init::SystemdServices,
42    parts::Mounts,
43    ram::{RAM, Swaps},
44    soft::InstalledPackages,
45    sys::{Groups, KModules, Kernel, OsRelease, Users},
46    vulnerabilities::Vulnerabilities,
47};
48
49#[derive(Debug)]
50pub struct Ferrix {
51    pub current_page: Page,
52    pub settings: FXSettings,
53    pub data: FerrixData,
54}
55
56impl Default for Ferrix {
57    fn default() -> Self {
58        let args = std::env::args().nth(1);
59        let page = match &args {
60            Some(a) => Page::from(a as &str),
61            None => Page::default(),
62        };
63        let settings = FXSettings::read(get_home().join(".config").join(SETTINGS_PATH))
64                .unwrap_or_default();
65        let style = settings.style.clone();
66
67        Self {
68            current_page: page,
69            settings,
70            data: FerrixData::new(&style),
71        }
72    }
73}
74
75impl Ferrix {
76    pub fn theme(&self) -> iced::Theme {
77        self.settings.style.to_theme()
78    }
79
80    pub fn title(&self) -> String {
81        "Ferrix System Monitor".to_string()
82    }
83
84    pub fn update(&mut self, message: Message) -> iced::Task<Message> {
85        message.update(self)
86    }
87
88    pub fn view<'a>(&'a self) -> iced::Element<'a, Message> {
89        iced::widget::row![sidebar(self.current_page), self.current_page.page(&self)]
90            .spacing(5)
91            .padding(5)
92            .into()
93    }
94}
95
96#[derive(Debug)]
97pub struct FerrixData {
98    pub is_polkit: bool,
99
100    pub proc_data: LoadState<Processors>,
101    pub prev_proc_stat: LoadState<Stat>,
102    pub curr_proc_stat: LoadState<Stat>,
103    pub cpu_usage_chart: LineChart,
104    pub show_cpus_chart: HashSet<usize>,
105    pub show_chart_elements: usize,
106    pub cpu_freq: LoadState<CpuFreq>,
107    pub cpu_vulnerabilities: LoadState<Vulnerabilities>,
108
109    pub ram_data: LoadState<RAM>,
110    pub swap_data: LoadState<Swaps>,
111    pub show_mem_chart: HashSet<usize>,
112    pub show_ram_chart: bool,
113    pub ram_usage_chart: LineChart,
114
115    pub storages: LoadState<Mounts>,
116    pub dmi_data: LoadState<DMIData>,
117    pub bat_data: LoadState<BatInfo>,
118    pub drm_data: LoadState<Video>,
119    pub osrel_data: LoadState<OsRelease>,
120
121    pub kernel_data: LoadState<Kernel>,
122    pub kmods_data: LoadState<KModules>,
123
124    pub users_list: LoadState<Users>,
125    pub groups_list: LoadState<Groups>,
126    pub sysd_services_list: LoadState<SystemdServices>,
127    pub installed_pkgs_list: LoadState<InstalledPackages>,
128    pub system: LoadState<crate::System>,
129}
130
131impl Default for FerrixData {
132    fn default() -> Self {
133        Self {
134            is_polkit: false,
135
136            cpu_usage_chart: LineChart::new(),
137            show_cpus_chart: HashSet::new(),
138            show_chart_elements: 100,
139            ram_usage_chart: LineChart::new(),
140            show_mem_chart: HashSet::new(),
141            show_ram_chart: true,
142
143            proc_data: LoadState::default(),
144            prev_proc_stat: LoadState::default(),
145            curr_proc_stat: LoadState::default(),
146            cpu_freq: LoadState::default(),
147            cpu_vulnerabilities: LoadState::default(),
148            ram_data: LoadState::default(),
149            swap_data: LoadState::default(),
150            storages: LoadState::default(),
151            dmi_data: LoadState::default(),
152            bat_data: LoadState::default(),
153            drm_data: LoadState::default(),
154            osrel_data: LoadState::default(),
155            kernel_data: LoadState::default(),
156            kmods_data: LoadState::default(),
157            users_list: LoadState::default(),
158            groups_list: LoadState::default(),
159            sysd_services_list: LoadState::default(),
160            installed_pkgs_list: LoadState::default(),
161            system: LoadState::default(),
162        }
163    }
164}
165
166impl FerrixData {
167    pub fn new(style: &Style) -> Self {
168        let mut cpu_usage_chart = LineChart::new();
169        cpu_usage_chart.set_style(&style.to_theme());
170        let mut ram_usage_chart = LineChart::new();
171        ram_usage_chart.set_style(&style.to_theme());
172
173        Self {
174            cpu_usage_chart,
175            ram_usage_chart,
176            ..Default::default()
177        }
178    }
179}