feat(bar): allow right side widget ordering

This commit is contained in:
LGUG2Z
2024-09-07 12:54:24 -07:00
parent fdc7706d23
commit 025162769b
9 changed files with 260 additions and 197 deletions

View File

@@ -1,4 +1,7 @@
use crate::widget::BarWidget;
use eframe::egui::Label;
use eframe::egui::Sense;
use eframe::egui::Ui;
use starship_battery::units::ratio::percent;
use starship_battery::Manager;
use starship_battery::State;
@@ -78,4 +81,26 @@ impl BarWidget for Battery {
outputs
}
fn render(&mut self, ui: &mut Ui) {
if self.enable {
let output = self.output();
if !output.is_empty() {
for battery in output {
let emoji = match self.state {
BatteryState::Charging => "⚡️",
BatteryState::Discharging => "🔋",
};
ui.add(
Label::new(format!("{emoji} {battery}"))
.selectable(false)
.sense(Sense::click()),
);
}
ui.add_space(10.0);
}
}
}
}

View File

@@ -1,4 +1,22 @@
use crate::widget::BarWidget;
use eframe::egui::Label;
use eframe::egui::Sense;
use eframe::egui::Ui;
#[derive(Copy, Clone, Debug)]
pub struct DateConfig {
pub enable: bool,
pub format: DateFormat,
}
impl From<DateConfig> for Date {
fn from(value: DateConfig) -> Self {
Self {
enable: value.enable,
format: value.format,
}
}
}
#[derive(Copy, Clone, Debug)]
pub enum DateFormat {
@@ -34,16 +52,30 @@ pub struct Date {
pub format: DateFormat,
}
impl Date {
pub fn new(enable: bool, format: DateFormat) -> Self {
Self { enable, format }
}
}
impl BarWidget for Date {
fn output(&mut self) -> Vec<String> {
vec![chrono::Local::now()
.format(&self.format.fmt_string())
.to_string()]
}
fn render(&mut self, ui: &mut Ui) {
if self.enable {
for output in self.output() {
if ui
.add(
Label::new(format!("📅 {}", output))
.selectable(false)
.sense(Sense::click()),
)
.clicked()
{
self.format.next()
}
}
// TODO: make spacing configurable
ui.add_space(10.0);
}
}
}

View File

@@ -9,8 +9,8 @@ mod widget;
use crate::battery::Battery;
use crate::battery::BatteryConfig;
use crate::battery::BatteryState;
use crate::date::Date;
use crate::date::DateConfig;
use crate::date::DateFormat;
use crate::media::Media;
use crate::media::MediaConfig;
@@ -20,6 +20,7 @@ use crate::network::Network;
use crate::network::NetworkConfig;
use crate::storage::Storage;
use crate::storage::StorageConfig;
use crate::time::TimeConfig;
use crate::time::TimeFormat;
use crate::widget::BarWidget;
use crossbeam_channel::Receiver;
@@ -39,7 +40,6 @@ use komorebi_client::SocketMessage;
use std::io::BufReader;
use std::io::Read;
use std::ops::Deref;
use std::process::Command;
use std::sync::Arc;
use std::time::Duration;
use time::Time;
@@ -77,8 +77,8 @@ pub struct Config {
monitor_index: usize,
monitor_work_area_offset: Option<komorebi_client::Rect>,
font_family: Option<String>,
time: Time,
date: Date,
time: TimeConfig,
date: DateConfig,
storage: StorageConfig,
memory: MemoryConfig,
media: MediaConfig,
@@ -100,8 +100,14 @@ fn main() -> eframe::Result<()> {
right: 0,
bottom: 40,
}),
time: Time::new(true, TimeFormat::TwentyFourHour),
date: Date::new(true, DateFormat::DayDateMonthYear),
time: TimeConfig {
enable: true,
format: TimeFormat::TwentyFourHour,
},
date: DateConfig {
enable: true,
format: DateFormat::DayDateMonthYear,
},
storage: StorageConfig { enable: true },
memory: MemoryConfig { enable: true },
media: MediaConfig { enable: true },
@@ -213,13 +219,7 @@ struct Komobar {
focused_window_title: String,
layout: String,
workspaces: Vec<String>,
time: Time,
date: Date,
memory: Memory,
storage: Storage,
media: Media,
battery: Battery,
network: Network,
right_widgets: Vec<Box<dyn BarWidget>>,
}
fn add_custom_font(ctx: &egui::Context, name: &str) {
@@ -267,6 +267,18 @@ impl Komobar {
// Use the cc.gl (a glow::Context) to create graphics shaders and buffers that you can use
// for e.g. egui::PaintCallback.
let mut right_widgets: Vec<Box<dyn BarWidget>> = vec![
Box::new(Media::from(config.media)),
Box::new(Storage::from(config.storage)),
Box::new(Memory::from(config.memory)),
Box::new(Network::from(config.network)),
Box::new(Date::from(config.date)),
Box::new(Time::from(config.time)),
Box::new(Battery::from(config.battery)),
];
right_widgets.reverse();
Self {
config: config.deref().clone(),
state_receiver: rx,
@@ -274,13 +286,7 @@ impl Komobar {
focused_window_title: String::new(),
layout: String::new(),
workspaces: vec![],
time: config.time,
date: config.date,
memory: Memory::from(config.memory),
storage: Storage::from(config.storage),
media: Media::from(config.media),
battery: Battery::from(config.battery),
network: Network::from(config.network),
right_widgets,
}
}
}
@@ -409,171 +415,8 @@ impl eframe::App for Komobar {
// TODO: make the order configurable
ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
if self.battery.enable {
let battery_output = self.battery.output();
if !battery_output.is_empty() {
for battery in battery_output {
let emoji = match self.battery.state {
BatteryState::Charging => "⚡️",
BatteryState::Discharging => "🔋",
};
ui.add(
Label::new(format!("{emoji} {battery}"))
.selectable(false)
.sense(Sense::click()),
);
}
ui.add_space(10.0);
}
}
if self.time.enable {
for time in self.time.output() {
if ui
.add(
Label::new(format!("🕐 {}", time))
.selectable(false)
.sense(Sense::click()),
)
.clicked()
{
self.time.format.toggle()
}
}
// TODO: make spacing configurable
ui.add_space(10.0);
}
if self.date.enable {
for date in self.date.output() {
if ui
.add(
Label::new(format!("📅 {}", date))
.selectable(false)
.sense(Sense::click()),
)
.clicked()
{
self.date.format.next()
}
}
// TODO: make spacing configurable
ui.add_space(10.0);
}
if self.network.enable {
let network_output = self.network.output();
if !network_output.is_empty() {
match network_output.len() {
1 => {
if ui
.add(
Label::new(format!("📶 {}", network_output[0]))
.selectable(false)
.sense(Sense::click()),
)
.clicked()
{
if let Err(error) =
Command::new("cmd.exe").args(["/C", "ncpa"]).spawn()
{
eprintln!("{}", error)
}
}
}
2 => {
if ui
.add(
Label::new(format!(
"📶 {} - {}",
network_output[0], network_output[1]
))
.selectable(false)
.sense(Sense::click()),
)
.clicked()
{
if let Err(error) =
Command::new("cmd.exe").args(["/C", "ncpa"]).spawn()
{
eprintln!("{}", error)
}
};
}
_ => {}
}
ui.add_space(10.0);
}
}
if self.memory.enable {
for ram in self.memory.output() {
if ui
.add(
Label::new(format!("🐏 {}", ram))
.selectable(false)
.sense(Sense::click()),
)
.clicked()
{
if let Err(error) =
Command::new("cmd.exe").args(["/C", "taskmgr.exe"]).spawn()
{
eprintln!("{}", error)
}
}
}
ui.add_space(10.0);
}
if self.storage.enable {
for disk in self.storage.output() {
if ui
.add(
Label::new(format!("🖴 {}", disk))
.selectable(false)
.sense(Sense::click()),
)
.clicked()
{
if let Err(error) = Command::new("cmd.exe")
.args([
"/C",
"explorer.exe",
disk.split(' ').collect::<Vec<&str>>()[0],
])
.spawn()
{
eprintln!("{}", error)
}
}
ui.add_space(10.0);
}
}
if self.media.enable {
for media in self.media.output() {
if ui
.add(
Label::new(format!("🎧 {media}"))
.selectable(false)
.sense(Sense::click()),
)
.clicked()
{
self.media.toggle();
}
ui.add_space(10.0);
}
for w in &mut self.right_widgets {
w.render(ui);
}
})
})

View File

@@ -1,4 +1,7 @@
use crate::widget::BarWidget;
use eframe::egui::Label;
use eframe::egui::Sense;
use eframe::egui::Ui;
use windows::Media::Control::GlobalSystemMediaTransportControlsSessionManager;
#[derive(Copy, Clone, Debug)]
@@ -60,4 +63,23 @@ impl BarWidget for Media {
vec![]
}
fn render(&mut self, ui: &mut Ui) {
if self.enable {
for output in self.output() {
if ui
.add(
Label::new(format!("🎧 {output}"))
.selectable(false)
.sense(Sense::click()),
)
.clicked()
{
self.toggle();
}
ui.add_space(10.0);
}
}
}
}

View File

@@ -1,4 +1,8 @@
use crate::widget::BarWidget;
use eframe::egui::Label;
use eframe::egui::Sense;
use eframe::egui::Ui;
use std::process::Command;
use std::time::Duration;
use std::time::Instant;
use sysinfo::RefreshKind;
@@ -42,4 +46,26 @@ impl BarWidget for Memory {
let total = self.system.total_memory();
vec![format!("RAM: {}%", (used * 100) / total)]
}
fn render(&mut self, ui: &mut Ui) {
if self.enable {
for output in self.output() {
if ui
.add(
Label::new(format!("🐏 {}", output))
.selectable(false)
.sense(Sense::click()),
)
.clicked()
{
if let Err(error) = Command::new("cmd.exe").args(["/C", "taskmgr.exe"]).spawn()
{
eprintln!("{}", error)
}
}
}
ui.add_space(10.0);
}
}
}

View File

@@ -1,4 +1,8 @@
use crate::widget::BarWidget;
use eframe::egui::Label;
use eframe::egui::Sense;
use eframe::egui::Ui;
use std::process::Command;
use std::time::Duration;
use std::time::Instant;
use sysinfo::Networks;
@@ -84,4 +88,48 @@ impl BarWidget for Network {
outputs
}
fn render(&mut self, ui: &mut Ui) {
if self.enable {
let output = self.output();
if !output.is_empty() {
match output.len() {
1 => {
if ui
.add(
Label::new(format!("📶 {}", output[0]))
.selectable(false)
.sense(Sense::click()),
)
.clicked()
{
if let Err(error) = Command::new("cmd.exe").args(["/C", "ncpa"]).spawn()
{
eprintln!("{}", error)
}
}
}
2 => {
if ui
.add(
Label::new(format!("📶 {} - {}", output[0], output[1]))
.selectable(false)
.sense(Sense::click()),
)
.clicked()
{
if let Err(error) = Command::new("cmd.exe").args(["/C", "ncpa"]).spawn()
{
eprintln!("{}", error)
}
};
}
_ => {}
}
ui.add_space(10.0);
}
}
}
}

View File

@@ -1,4 +1,8 @@
use crate::widget::BarWidget;
use eframe::egui::Label;
use eframe::egui::Sense;
use eframe::egui::Ui;
use std::process::Command;
use std::time::Duration;
use std::time::Instant;
use sysinfo::Disks;
@@ -52,4 +56,32 @@ impl BarWidget for Storage {
disks
}
fn render(&mut self, ui: &mut Ui) {
if self.enable {
for output in self.output() {
if ui
.add(
Label::new(format!("🖴 {}", output))
.selectable(false)
.sense(Sense::click()),
)
.clicked()
{
if let Err(error) = Command::new("cmd.exe")
.args([
"/C",
"explorer.exe",
output.split(' ').collect::<Vec<&str>>()[0],
])
.spawn()
{
eprintln!("{}", error)
}
}
ui.add_space(10.0);
}
}
}
}

View File

@@ -1,4 +1,22 @@
use crate::widget::BarWidget;
use eframe::egui::Label;
use eframe::egui::Sense;
use eframe::egui::Ui;
#[derive(Copy, Clone, Debug)]
pub struct TimeConfig {
pub enable: bool,
pub format: TimeFormat,
}
impl From<TimeConfig> for Time {
fn from(value: TimeConfig) -> Self {
Self {
enable: value.enable,
format: value.format,
}
}
}
#[derive(Copy, Clone, Debug)]
pub enum TimeFormat {
@@ -28,16 +46,30 @@ pub struct Time {
pub format: TimeFormat,
}
impl Time {
pub fn new(enable: bool, format: TimeFormat) -> Self {
Self { enable, format }
}
}
impl BarWidget for Time {
fn output(&mut self) -> Vec<String> {
vec![chrono::Local::now()
.format(&self.format.fmt_string())
.to_string()]
}
fn render(&mut self, ui: &mut Ui) {
if self.enable {
for output in self.output() {
if ui
.add(
Label::new(format!("🕐 {}", output))
.selectable(false)
.sense(Sense::click()),
)
.clicked()
{
self.format.toggle()
}
}
// TODO: make spacing configurable
ui.add_space(10.0);
}
}
}

View File

@@ -1,3 +1,6 @@
use eframe::egui::Ui;
pub trait BarWidget {
fn output(&mut self) -> Vec<String>;
fn render(&mut self, ui: &mut Ui);
}