Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rwxroot committed Jan 14, 2025
1 parent a4c986a commit 6a97c1d
Show file tree
Hide file tree
Showing 10 changed files with 239 additions and 10 deletions.
10 changes: 0 additions & 10 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,3 @@ Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

# RustRover
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
31 changes: 31 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[package]
name = "cosmic-ext-applet-sysinfo"
version = "0.1.0"
edition = "2021"
license = "GPL-3.0"
authors = ["rwxroot <[email protected]>"]
homepage = "https://github.com/rwxroot/cosmic-ext-applet-sysinfo/"
repository = "https://github.com/rwxroot/cosmic-ext-applet-sysinfo.git"

[profile.release]
lto = "fat"

[dependencies]
sysinfo = { version = "0.33.1", default-features = false, features = [
"system",
"network",
] }

tracing = "0.1"
tracing-log = "0.2.0"
tracing-subscriber = { version = "0.3", default-features = false, features = [
"fmt",
"env-filter",
"ansi",
] }

libcosmic = { git = "https://github.com/pop-os/libcosmic", default-features = false, features = [
"applet",
"tokio",
"wayland",
] }
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Simple system info applet for cosmic

<p align="center">
<img alt="Applet Screenshot" src="https://github.com/rwxroot/cosmic-ext-applet-sysinfo/blob/main/extra/applet_screenshot.png">
</p>

## Install

```sh
sudo apt install libxkbcommon-dev
git clone https://github.com/rwxroot/cosmic-ext-applet-sysinfo.git
cd cosmic-ext-applet-sysinfo
just build
sudo just install
```
3 changes: 3 additions & 0 deletions extra/applet_icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added extra/applet_screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions extra/applet_sysinfo.desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[Desktop Entry]
Name=Simple System Monitor
Keywords=Cosmic;Iced;
Categories=Cosmic;Iced;

Type=Application
Icon=io.github.rwxroot.cosmic-ext-applet-sysinfo-symbolic
Exec=cosmic-ext-applet-sysinfo
Terminal=false
StartupNotify=true
NoDisplay=true
X-CosmicApplet=true
X-CosmicHoverPopup=Auto
27 changes: 27 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
default: build

build:
cargo build --release

export NAME := 'cosmic-ext-applet-sysinfo'
export APPID := 'io.github.rwxroot.' + NAME

cargo-target-dir := env('CARGO_TARGET_DIR', 'target')
bin-src := cargo-target-dir / 'release' / NAME

base-dir := '/usr'
share-dst := base-dir / 'share'

bin-dst := base-dir / 'bin' / NAME
desktop-dst := share-dst / 'applications' / APPID + '.desktop'
icon-dst := share-dst / 'icons/hicolor/scalable/apps' / APPID + '-symbolic.svg'

install:
install -Dm0755 {{ bin-src }} {{ bin-dst }}
install -Dm0644 extra/applet_icon.svg {{ icon-dst }}
install -Dm0644 extra/applet_sysinfo.desktop {{ desktop-dst }}

uninstall:
rm {{ bin-dst }}
rm {{ icon-dst }}
rm {{ desktop-dst }}
137 changes: 137 additions & 0 deletions src/applet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
use std::time::Duration;

use cosmic::{
app,
iced::{
widget::{row, text},
Alignment, Subscription,
},
widget::{autosize, button},
Element,
};
use sysinfo::{CpuRefreshKind, MemoryRefreshKind, Networks, RefreshKind, System};

pub fn run() -> cosmic::iced::Result {
cosmic::applet::run::<SysInfo>(())
}

struct SysInfo {
core: cosmic::app::Core,
system: System,
networks: Networks,
pub cpu_usage: f32,
pub ram_usage: u64,
pub download_speed: f64,
pub upload_speed: f64,
}

impl SysInfo {
fn update(&mut self) {
self.system.refresh_specifics(
RefreshKind::nothing()
.with_memory(MemoryRefreshKind::nothing().with_ram())
.with_cpu(CpuRefreshKind::nothing().with_cpu_usage()),
);

self.networks.refresh(true);

self.cpu_usage = self.system.global_cpu_usage();
self.ram_usage = (self.system.used_memory() * 100) / self.system.total_memory();

let mut upload = 0;
let mut download = 0;

for (_, data) in self.networks.iter() {
upload += data.transmitted();
download += data.received();
}

self.upload_speed = (upload as f64) / 1_000_000.0;
self.download_speed = (download as f64) / 1_000_000.0;
}
}

#[derive(Debug, Clone)]
pub enum Message {
Tick,
}

impl cosmic::Application for SysInfo {
type Flags = ();
type Message = Message;
type Executor = cosmic::SingleThreadExecutor;

const APP_ID: &'static str = "io.github.rwxroot.cosmic-ext-applet-sysinfo";

fn init(
core: app::Core,
_flags: Self::Flags,
) -> (Self, cosmic::iced::Task<app::Message<Self::Message>>) {
let system = System::new_with_specifics(
RefreshKind::nothing()
.with_memory(MemoryRefreshKind::nothing().with_ram())
.with_cpu(CpuRefreshKind::nothing().with_cpu_usage()),
);

let networks = Networks::new();

(
Self {
core,
system,
networks,
cpu_usage: 0.0,
ram_usage: 0,
download_speed: 0.00,
upload_speed: 0.00,
},
cosmic::iced::Task::none(),
)
}

fn core(&self) -> &cosmic::app::Core {
&self.core
}

fn core_mut(&mut self) -> &mut cosmic::app::Core {
&mut self.core
}

fn subscription(&self) -> Subscription<Message> {
cosmic::iced::time::every(Duration::from_secs(2)).map(|_| Message::Tick)
}

fn update(&mut self, message: Message) -> cosmic::iced::Task<app::Message<Self::Message>> {
match message {
Message::Tick => {
self.update();
}
}

cosmic::iced::Task::none()
}

fn view(&self) -> Element<Message> {
let content = {
row![
text(format!("C: {:.0}%", self.cpu_usage)),
text(format!("R: {}%", self.ram_usage)),
text(format!(
"N: ↓{:.2}MB/s ↑{:.2}MB/s",
self.download_speed, self.upload_speed
)),
]
.spacing(8)
.align_y(Alignment::Center)
};

let button = button::custom(content)
.padding([
self.core.applet.suggested_padding(false),
self.core.applet.suggested_padding(false),
])
.class(cosmic::theme::Button::AppletIcon);

autosize::autosize(button, cosmic::widget::Id::unique()).into()
}
}
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub mod applet;

pub fn run() -> cosmic::iced::Result {
applet::run()
}
8 changes: 8 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fn main() -> cosmic::iced::Result {
tracing_subscriber::fmt::init();
let _ = tracing_log::LogTracer::init();

tracing::info!("Starting sysinfo applet");

cosmic_ext_applet_sysinfo::run()
}

0 comments on commit 6a97c1d

Please sign in to comment.