Initial commit

This commit is contained in:
Breadway 2026-05-17 13:13:37 +08:00
parent 3100ee0591
commit 633467b6f2
13 changed files with 158 additions and 41 deletions

1
assets/Battery.svg Normal file
View file

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-battery-icon lucide-battery"><path d="M 22 14 L 22 10"/><rect x="2" y="6" width="16" height="12" rx="2"/></svg>

After

Width:  |  Height:  |  Size: 313 B

1
assets/CPU.svg Normal file
View file

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-cpu-icon lucide-cpu"><path d="M12 20v2"/><path d="M12 2v2"/><path d="M17 20v2"/><path d="M17 2v2"/><path d="M2 12h2"/><path d="M2 17h2"/><path d="M2 7h2"/><path d="M20 12h2"/><path d="M20 17h2"/><path d="M20 7h2"/><path d="M7 20v2"/><path d="M7 2v2"/><rect x="4" y="4" width="16" height="16" rx="2"/><rect x="8" y="8" width="8" height="8" rx="1"/></svg>

After

Width:  |  Height:  |  Size: 555 B

1
assets/Power Draw.svg Normal file
View file

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-zap-icon lucide-zap"><path d="M4 14a1 1 0 0 1-.78-1.63l9.9-10.2a.5.5 0 0 1 .86.46l-1.92 6.02A1 1 0 0 0 13 10h7a1 1 0 0 1 .78 1.63l-9.9 10.2a.5.5 0 0 1-.86-.46l1.92-6.02A1 1 0 0 0 11 14z"/></svg>

After

Width:  |  Height:  |  Size: 396 B

1
assets/RAM Usage.svg Normal file
View file

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-memory-stick-icon lucide-memory-stick"><path d="M12 12v-2"/><path d="M12 18v-2"/><path d="M16 12v-2"/><path d="M16 18v-2"/><path d="M2 11h1.5"/><path d="M20 18v-2"/><path d="M20.5 11H22"/><path d="M4 18v-2"/><path d="M8 12v-2"/><path d="M8 18v-2"/><rect x="2" y="6" width="20" height="10" rx="2"/></svg>

After

Width:  |  Height:  |  Size: 505 B

View file

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-wifi-sync-icon lucide-wifi-sync"><path d="M11.965 10.105v4L13.5 12.5a5 5 0 0 1 8 1.5"/><path d="M11.965 14.105h4"/><path d="M17.965 18.105h4L20.43 19.71a5 5 0 0 1-8-1.5"/><path d="M2 8.82a15 15 0 0 1 20 0"/><path d="M21.965 22.105v-4"/><path d="M5 12.86a10 10 0 0 1 3-2.032"/><path d="M8.5 16.429h.01"/></svg>

After

Width:  |  Height:  |  Size: 511 B

1
assets/WiFi Medium.svg Normal file
View file

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-wifi-high-icon lucide-wifi-high"><path d="M12 20h.01"/><path d="M5 12.859a10 10 0 0 1 14 0"/><path d="M8.5 16.429a5 5 0 0 1 7 0"/></svg>

After

Width:  |  Height:  |  Size: 338 B

1
assets/WiFi Strong.svg Normal file
View file

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-wifi-icon lucide-wifi"><path d="M12 20h.01"/><path d="M2 8.82a15 15 0 0 1 20 0"/><path d="M5 12.859a10 10 0 0 1 14 0"/><path d="M8.5 16.429a5 5 0 0 1 7 0"/></svg>

After

Width:  |  Height:  |  Size: 364 B

1
assets/WiFi Weak.svg Normal file
View file

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-wifi-low-icon lucide-wifi-low"><path d="M12 20h.01"/><path d="M8.5 16.429a5 5 0 0 1 7 0"/></svg>

After

Width:  |  Height:  |  Size: 298 B

View file

@ -2,6 +2,21 @@ use crate::{App, AppInput};
use relm4::ComponentSender; use relm4::ComponentSender;
use std::{fs, path::PathBuf, sync::Mutex}; use std::{fs, path::PathBuf, sync::Mutex};
const WIFI_STRONG: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/assets/WiFi Strong.svg");
const WIFI_MEDIUM: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/assets/WiFi Medium.svg");
const WIFI_WEAK: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/assets/WiFi Weak.svg");
const WIFI_OFF: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/assets/WiFi Connecting.svg");
#[derive(Debug)]
pub struct Stats {
pub cpu: String,
pub mem: String,
pub power: String,
pub bat: String,
pub wifi_ssid: String,
pub wifi_icon: &'static str,
}
struct CpuSnapshot { struct CpuSnapshot {
total: u64, total: u64,
idle: u64, idle: u64,
@ -35,7 +50,7 @@ fn read_cpu() -> f32 {
(dtotal - didle) as f32 / dtotal as f32 * 100.0 (dtotal - didle) as f32 / dtotal as f32 * 100.0
} }
fn read_ram() -> f32 { fn read_ram() -> u64 {
let text = fs::read_to_string("/proc/meminfo").unwrap_or_default(); let text = fs::read_to_string("/proc/meminfo").unwrap_or_default();
let mut total = 0u64; let mut total = 0u64;
let mut avail = 0u64; let mut avail = 0u64;
@ -47,10 +62,7 @@ fn read_ram() -> f32 {
_ => {} _ => {}
} }
} }
if total == 0 { total.saturating_sub(avail)
return 0.0;
}
(total - avail) as f32 / total as f32 * 100.0
} }
fn bat_path() -> Option<PathBuf> { fn bat_path() -> Option<PathBuf> {
@ -92,29 +104,76 @@ fn read_battery() -> Option<u8> {
.ok() .ok()
} }
async fn read_wifi() -> String { async fn read_wifi() -> (String, &'static str) {
let out = tokio::process::Command::new("iw") let dev_out = tokio::process::Command::new("iw")
.arg("dev") .arg("dev")
.output() .output()
.await .await
.ok(); .ok();
let stdout = match out { let dev_stdout = match dev_out {
Some(o) if o.status.success() => String::from_utf8_lossy(&o.stdout).into_owned(), Some(o) if o.status.success() => String::from_utf8_lossy(&o.stdout).into_owned(),
_ => return "".into(), _ => return ("".into(), WIFI_OFF),
}; };
stdout
let iface = dev_stdout
.lines() .lines()
.find_map(|l| l.trim().strip_prefix("ssid ").map(str::to_string)) .find_map(|l| l.trim().strip_prefix("Interface ").map(str::to_string));
.unwrap_or_else(|| "".into()) let Some(iface) = iface else {
return ("".into(), WIFI_OFF);
};
let link_out = tokio::process::Command::new("iw")
.args(["dev", &iface, "link"])
.output()
.await
.ok();
let link_stdout = match link_out {
Some(o) if o.status.success() => String::from_utf8_lossy(&o.stdout).into_owned(),
_ => return ("".into(), WIFI_OFF),
};
let mut ssid = None;
let mut rssi: Option<i32> = None;
for line in link_stdout.lines() {
let t = line.trim();
if let Some(s) = t.strip_prefix("SSID: ") {
ssid = Some(s.to_string());
} else if let Some(r) = t.strip_prefix("signal: ") {
rssi = r.split_whitespace().next().and_then(|s| s.parse().ok());
}
}
let Some(ssid) = ssid else {
return ("".into(), WIFI_OFF);
};
let icon = match rssi {
Some(r) if r >= -55 => WIFI_STRONG,
Some(r) if r >= -70 => WIFI_MEDIUM,
_ => WIFI_WEAK,
};
(ssid, icon)
} }
pub async fn poll() -> String { pub async fn poll() -> Stats {
let cpu = read_cpu(); let cpu = read_cpu();
let ram = read_ram(); let mem = read_ram();
let power = read_power().map_or_else(|| "—W".into(), |w| format!("{w:.1}W")); let power = read_power().map_or_else(|| " —W".into(), |w| format!("{w:4.1}W"));
let bat = read_battery().map_or_else(|| "".into(), |p| format!("{p}%")); let bat = read_battery().map_or_else(|| "".into(), |p| format!("{p:3}%"));
let wifi = read_wifi().await; let (wifi_ssid, wifi_icon) = read_wifi().await;
format!("CPU {cpu:.0}% MEM {ram:.0}% {power} BAT {bat} {wifi}") Stats {
cpu: format!("{cpu:3.0}%"),
mem: if mem >= 1024 * 1024 {
format!("{:.1}G", mem as f32 / (1024.0 * 1024.0))
} else {
format!("{}M", mem / 1024)
},
power,
bat,
wifi_ssid,
wifi_icon,
}
} }
pub fn spawn_poller(sender: ComponentSender<App>) { pub fn spawn_poller(sender: ComponentSender<App>) {

View file

@ -1,3 +1,9 @@
macro_rules! asset {
($n:literal) => {
concat!(env!("CARGO_MANIFEST_DIR"), "/assets/", $n)
};
}
mod bar; mod bar;
mod notifications; mod notifications;
mod theme; mod theme;
@ -12,9 +18,13 @@ pub struct App {
workspaces: Vec<Workspace>, workspaces: Vec<Workspace>,
active_ws: WorkspaceId, active_ws: WorkspaceId,
time_str: String, time_str: String,
stats_str: String,
// GObject handle — manipulated directly in update() to avoid update_view conflicts.
workspace_box: gtk4::Box, workspace_box: gtk4::Box,
cpu_lbl: gtk4::Label,
mem_lbl: gtk4::Label,
pwr_lbl: gtk4::Label,
bat_lbl: gtk4::Label,
wifi_lbl: gtk4::Label,
wifi_img: gtk4::Image,
} }
#[derive(Debug)] #[derive(Debug)]
@ -22,7 +32,7 @@ pub enum AppInput {
WorkspaceList(Vec<Workspace>), WorkspaceList(Vec<Workspace>),
ActiveWorkspace(WorkspaceId), ActiveWorkspace(WorkspaceId),
ClockTick, ClockTick,
StatsUpdate(String), StatsUpdate(bar::stats::Stats),
} }
#[relm4::component(pub)] #[relm4::component(pub)]
@ -37,6 +47,7 @@ impl SimpleComponent for App {
set_title: Some("aster"), set_title: Some("aster"),
set_default_height: 32, set_default_height: 32,
#[name = "center_box"]
gtk::CenterBox { gtk::CenterBox {
#[wrap(Some)] #[wrap(Some)]
set_start_widget = &gtk::Box { set_start_widget = &gtk::Box {
@ -56,18 +67,6 @@ impl SimpleComponent for App {
#[watch] #[watch]
set_label: &model.time_str, set_label: &model.time_str,
}, },
#[wrap(Some)]
set_end_widget = &gtk::Box {
set_orientation: gtk::Orientation::Horizontal,
set_spacing: 8,
set_margin_end: 8,
gtk::Label {
#[watch]
set_label: &model.stats_str,
}
},
} }
} }
} }
@ -84,17 +83,42 @@ impl SimpleComponent for App {
root.set_anchor(Edge::Right, true); root.set_anchor(Edge::Right, true);
root.set_exclusive_zone(32); root.set_exclusive_zone(32);
let cpu_lbl = stat_label(4);
let mem_lbl = stat_label(4);
let pwr_lbl = stat_label(5);
let bat_lbl = stat_label(4);
let wifi_lbl = gtk4::Label::new(None);
wifi_lbl.set_ellipsize(gtk4::pango::EllipsizeMode::End);
wifi_lbl.set_max_width_chars(12);
let wifi_img = gtk4::Image::from_paintable(Some(&svg_texture(asset!("WiFi Connecting.svg"))));
let mut model = App { let mut model = App {
workspaces: vec![], workspaces: vec![],
active_ws: 1, active_ws: 1,
time_str: bar::clock::current(), time_str: bar::clock::current(),
stats_str: String::new(),
workspace_box: gtk4::Box::new(gtk4::Orientation::Horizontal, 4), workspace_box: gtk4::Box::new(gtk4::Orientation::Horizontal, 4),
cpu_lbl: cpu_lbl.clone(),
mem_lbl: mem_lbl.clone(),
pwr_lbl: pwr_lbl.clone(),
bat_lbl: bat_lbl.clone(),
wifi_lbl: wifi_lbl.clone(),
wifi_img: wifi_img.clone(),
}; };
let widgets = view_output!(); let widgets = view_output!();
model.workspace_box = widgets.workspace_box.clone(); model.workspace_box = widgets.workspace_box.clone();
let stats_box = gtk4::Box::new(gtk4::Orientation::Horizontal, 8);
stats_box.set_margin_end(8);
stats_box.append(&stat_pair(asset!("CPU.svg"), &cpu_lbl));
stats_box.append(&stat_pair(asset!("RAM Usage.svg"), &mem_lbl));
stats_box.append(&stat_pair(asset!("Power Draw.svg"), &pwr_lbl));
stats_box.append(&stat_pair(asset!("Battery.svg"), &bat_lbl));
let wifi_pair = gtk4::Box::new(gtk4::Orientation::Horizontal, 4);
wifi_pair.append(&wifi_img);
wifi_pair.append(&wifi_lbl);
stats_box.append(&wifi_pair);
widgets.center_box.set_end_widget(Some(&stats_box));
theme::apply(); theme::apply();
bar::workspaces::spawn_watcher(sender.clone()); bar::workspaces::spawn_watcher(sender.clone());
bar::clock::spawn_ticker(sender.clone()); bar::clock::spawn_ticker(sender.clone());
@ -119,8 +143,13 @@ impl SimpleComponent for App {
AppInput::ClockTick => { AppInput::ClockTick => {
self.time_str = bar::clock::current(); self.time_str = bar::clock::current();
} }
AppInput::StatsUpdate(s) => { AppInput::StatsUpdate(stats) => {
self.stats_str = s; self.cpu_lbl.set_label(&stats.cpu);
self.mem_lbl.set_label(&stats.mem);
self.pwr_lbl.set_label(&stats.power);
self.bat_lbl.set_label(&stats.bat);
self.wifi_lbl.set_label(&stats.wifi_ssid);
self.wifi_img.set_paintable(Some(&svg_texture(stats.wifi_icon)));
} }
} }
} }
@ -138,8 +167,29 @@ impl App {
} }
} }
fn stat_pair(icon_path: &str, label: &gtk4::Label) -> gtk4::Box {
let pair = gtk4::Box::new(gtk4::Orientation::Horizontal, 4);
pair.append(&gtk4::Image::from_paintable(Some(&svg_texture(icon_path))));
pair.append(label);
pair
}
fn svg_texture(path: &str) -> gtk4::gdk::Texture {
let svg = std::fs::read_to_string(path)
.unwrap_or_default()
.replace("currentColor", "white");
let bytes = gtk4::glib::Bytes::from_owned(svg.into_bytes());
gtk4::gdk::Texture::from_bytes(&bytes).expect("svg load")
}
fn stat_label(width_chars: i32) -> gtk4::Label {
let lbl = gtk4::Label::new(None);
lbl.set_width_chars(width_chars);
lbl.set_xalign(1.0);
lbl
}
fn main() { fn main() {
// Reload theme CSS on SIGHUP (e.g. after pywal runs).
relm4::spawn(async { relm4::spawn(async {
use tokio::signal::unix::{signal, SignalKind}; use tokio::signal::unix::{signal, SignalKind};
let mut stream = signal(SignalKind::hangup()).expect("SIGHUP handler"); let mut stream = signal(SignalKind::hangup()).expect("SIGHUP handler");

View file

@ -55,7 +55,7 @@ impl NotifServer {
} }
fn get_server_information(&self) -> (String, String, String, String) { fn get_server_information(&self) -> (String, String, String, String) {
("aster".into(), "breadway".into(), "0.1.0".into(), "1.2".into()) ("breadbar".into(), "breadway".into(), "0.1.0".into(), "1.2".into())
} }
} }

View file

@ -58,7 +58,7 @@ fn dismiss(cards_box: &gtk4::Box, window: &gtk4::Window, cards: &Cards, id: u32)
fn create_window() -> gtk4::Window { fn create_window() -> gtk4::Window {
let window = gtk4::Window::new(); let window = gtk4::Window::new();
window.add_css_class("aster-notification"); window.add_css_class("breadbar-notification");
window.init_layer_shell(); window.init_layer_shell();
window.set_layer(Layer::Overlay); window.set_layer(Layer::Overlay);
window.set_anchor(Edge::Top, true); window.set_anchor(Edge::Top, true);

View file

@ -44,7 +44,7 @@ fn load_css() -> String {
}; };
format!( format!(
"window.aster-bar {{ background-color: {bg_rgba}; }}\ "window.breadbar {{ background-color: {bg_rgba}; }}\
.workspace-btn {{ background: {surface}; color: {fg}; border-radius: 4px;\ .workspace-btn {{ background: {surface}; color: {fg}; border-radius: 4px;\
border: none; min-width: 24px; padding: 0 8px; }}\ border: none; min-width: 24px; padding: 0 8px; }}\
.workspace-btn:hover, .workspace-btn.active {{ background: {accent}; }}\ .workspace-btn:hover, .workspace-btn.active {{ background: {accent}; }}\