Skip to content

Commit

Permalink
Update to sysinfo 0.30
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Apr 21, 2024
1 parent 88e2465 commit 777237b
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 72 deletions.
25 changes: 22 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ keywords = ["GUI", "process", "viewer", "gtk"]

[dependencies]
gtk = { version = "0.8", package = "gtk4" }
sysinfo = "0.28.2"
sysinfo = "0.30"
libc = "0.2"
serde = "1.0"
serde_derive = "1.0"
Expand Down
26 changes: 9 additions & 17 deletions src/display_disk.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::{Arc, Mutex};

use crate::utils::format_number;

use gtk::glib;
use gtk::prelude::*;

use sysinfo::{DiskExt, SystemExt};

struct DiskInfo {
label: gtk::Label,
progress: gtk::ProgressBar,
Expand Down Expand Up @@ -76,7 +73,7 @@ fn refresh_disks(container: &gtk::Box, disks: &[sysinfo::Disk], elems: &mut Vec<
}
}

pub fn create_disk_info(sys: &Arc<Mutex<sysinfo::System>>, stack: &gtk::Stack) {
pub fn create_disk_info(stack: &gtk::Stack) {
let elems: Rc<RefCell<Vec<DiskInfo>>> = Rc::new(RefCell::new(Vec::new()));
let vertical_layout = gtk::Box::new(gtk::Orientation::Vertical, 0);
let container = gtk::Box::new(gtk::Orientation::Vertical, 0);
Expand All @@ -86,24 +83,19 @@ pub fn create_disk_info(sys: &Arc<Mutex<sysinfo::System>>, stack: &gtk::Stack) {
.child(&container)
.build();

let disks = RefCell::new(sysinfo::Disks::new_with_refreshed_list());

refresh_disks(&container, &disks.borrow(), &mut elems.borrow_mut());

let refresh_but = gtk::Button::with_label("Refresh disks");

refresh_but.connect_clicked(
glib::clone!(@weak sys, @weak container, @strong elems => move |_| {
let mut sys = sys.lock().expect("failed to lock to refresh disks");
sys.refresh_disks();
refresh_disks(&container, sys.disks(), &mut elems.borrow_mut());
}),
);
refresh_but.connect_clicked(glib::clone!(@weak container, @strong elems => move |_| {
disks.borrow_mut().refresh_list();
refresh_disks(&container, &disks.borrow(), &mut elems.borrow_mut());
}));

vertical_layout.append(&scroll);
vertical_layout.append(&refresh_but);

stack.add_titled(&vertical_layout, Some("Disks"), "Disks");

refresh_disks(
&container,
sys.lock().expect("failed to lock to get disks").disks(),
&mut elems.borrow_mut(),
);
}
28 changes: 14 additions & 14 deletions src/display_network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::network_dialog::{self, NetworkDialog};
use crate::utils::{format_number, format_number_full};
use gtk::glib;
use gtk::prelude::*;
use sysinfo::{NetworkExt, NetworksExt, System, SystemExt};
use sysinfo::Networks;

use std::cell::RefCell;
use std::collections::HashSet;
Expand Down Expand Up @@ -41,13 +41,12 @@ fn append_column(

pub struct Network {
list_store: gtk::ListStore,
pub filter_entry: gtk::SearchEntry,
pub search_bar: gtk::SearchBar,
dialogs: Rc<RefCell<Vec<NetworkDialog>>>,
}

impl Network {
pub fn new(stack: &gtk::Stack, sys: &Arc<Mutex<System>>) -> Network {
pub fn new(stack: &gtk::Stack, networks: &Arc<Mutex<Networks>>) -> Self {
let tree = gtk::TreeView::builder().headers_visible(true).build();
let scroll = gtk::ScrolledWindow::builder().child(&tree).build();
let info_button = gtk::Button::builder()
Expand Down Expand Up @@ -99,7 +98,7 @@ impl Network {
// The filter model
let filter_model = gtk::TreeModelFilter::new(&list_store, None);
filter_model.set_visible_func(
glib::clone!(@weak filter_entry => @default-return false, move |model, iter| {
glib::clone!(@strong filter_entry => @default-return false, move |model, iter| {
if !WidgetExt::is_visible(&filter_entry) {
return true;
}
Expand Down Expand Up @@ -173,19 +172,19 @@ impl Network {

let dialogs = Rc::new(RefCell::new(Vec::new()));

info_button.connect_clicked(glib::clone!(@weak dialogs, @weak sys => move |_| {
info_button.connect_clicked(glib::clone!(@weak dialogs, @weak networks => move |_| {
let current_network = current_network.borrow();
if let Some(ref interface_name) = *current_network {
create_network_dialog(
&mut dialogs.borrow_mut(),
interface_name,
&sys.lock().expect("failed to lock for new network dialog"),
&networks.lock().expect("failed to lock for new network dialog"),
);
}
}));

tree.connect_row_activated(
glib::clone!(@weak sys, @weak dialogs => move |tree_view, path, _| {
glib::clone!(@weak networks, @weak dialogs => move |tree_view, path, _| {
let model = tree_view.model().expect("couldn't get model");
let iter = model.iter(path).expect("couldn't get iter");
let interface_name = model.get_value(&iter, 0)
Expand All @@ -194,26 +193,24 @@ impl Network {
create_network_dialog(
&mut dialogs.borrow_mut(),
&interface_name,
&sys.lock().expect("failed to lock for new network dialog (from tree)"),
&networks.lock().expect("failed to lock for new network dialog (from tree)"),
);
}),
);

Network {
list_store,
filter_entry,
search_bar,
dialogs,
}
}

pub fn update_networks(&mut self, sys: &System) {
pub fn update_networks(&mut self, networks: &Networks) {
// first part, deactivate sorting
let sorted = TreeSortableExtManual::sort_column_id(&self.list_store);
self.list_store.set_unsorted();

let mut seen: HashSet<String> = HashSet::new();
let networks = sys.networks();

if let Some(iter) = self.list_store.iter_first() {
let mut valid = true;
Expand Down Expand Up @@ -319,15 +316,18 @@ fn create_and_fill_model(
);
}

fn create_network_dialog(dialogs: &mut Vec<NetworkDialog>, interface_name: &str, sys: &System) {
fn create_network_dialog(
dialogs: &mut Vec<NetworkDialog>,
interface_name: &str,
networks: &Networks,
) {
for dialog in dialogs.iter() {
if dialog.name == interface_name {
dialog.show();
return;
}
}
if let Some((_, data)) = sys
.networks()
if let Some((_, data)) = networks
.iter()
.find(|(name, _)| name.as_str() == interface_name)
{
Expand Down
6 changes: 3 additions & 3 deletions src/display_procs.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use gtk::glib;
use gtk::prelude::*;

use sysinfo::{Pid, PidExt, Process, ProcessExt};
use sysinfo::{Pid, Process};

use crate::utils::format_number;

Expand Down Expand Up @@ -42,6 +42,7 @@ impl Procs {
.margin_top(6)
.margin_bottom(6)
.margin_end(6)
.margin_start(6)
.sensitive(false)
.build();

Expand Down Expand Up @@ -80,8 +81,7 @@ impl Procs {
for pro in proc_list.values() {
if let Some(exe) = pro
.exe()
.file_name()
.and_then(|f| f.to_str())
.and_then(|exe| exe.file_name().and_then(|f| f.to_str()))
.or_else(|| Some(pro.name()))
{
create_and_fill_model(
Expand Down
18 changes: 11 additions & 7 deletions src/display_sysinfo.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use gtk::glib;
use gtk::prelude::*;
use sysinfo::{ComponentExt, CpuExt, SystemExt};

use std::cell::RefCell;
use std::iter;
Expand Down Expand Up @@ -71,6 +70,7 @@ pub struct DisplaySysInfo {
impl DisplaySysInfo {
pub fn new(
sys: &Arc<Mutex<sysinfo::System>>,
sys_components: &sysinfo::Components,
stack: &gtk::Stack,
settings: &Settings,
) -> DisplaySysInfo {
Expand Down Expand Up @@ -192,13 +192,13 @@ impl DisplaySysInfo {
//
// TEMPERATURES PART
//
if !sys.components().is_empty() {
if !sys_components.is_empty() {
check_box3 = Some(create_header(
"Components' temperature",
&vertical_layout,
settings.display_graph,
));
for component in sys.components() {
for component in sys_components {
let horizontal_layout = gtk::Box::new(gtk::Orientation::Horizontal, 10);
// TODO: add max and critical temperatures as well
let temp = gtk::Label::new(Some(&format!("{:.1} °C", component.temperature())));
Expand Down Expand Up @@ -273,7 +273,7 @@ impl DisplaySysInfo {
temperature_usage_history,
temperature_check_box: check_box3,
};
tmp.update_system_info(&sys, settings.display_fahrenheit);
tmp.update_system_info(&sys, sys_components, settings.display_fahrenheit);
tmp
}

Expand All @@ -285,7 +285,12 @@ impl DisplaySysInfo {
}
}

pub fn update_system_info(&mut self, sys: &sysinfo::System, display_fahrenheit: bool) {
pub fn update_system_info(
&mut self,
sys: &sysinfo::System,
sys_components: &sysinfo::Components,
display_fahrenheit: bool,
) {
let disp = |total, used| {
format!(
"{} / {}",
Expand Down Expand Up @@ -337,8 +342,7 @@ impl DisplaySysInfo {

// temperature part
let t = self.temperature_usage_history.borrow_mut();
for (pos, (component, label)) in sys
.components()
for (pos, (component, label)) in sys_components
.iter()
.zip(self.components.iter())
.enumerate()
Expand Down
Loading

0 comments on commit 777237b

Please sign in to comment.