Skip to content

Commit

Permalink
feat(bar): add basic widget config opts
Browse files Browse the repository at this point in the history
  • Loading branch information
LGUG2Z committed Aug 28, 2024
1 parent bc2f4a1 commit ca6bf69
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 100 deletions.
11 changes: 6 additions & 5 deletions komorebi-bar/src/date.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::widget::BarWidget;

#[derive(Copy, Clone, Debug)]
pub enum DateFormat {
MonthDateYear,
YearMonthDate,
Expand Down Expand Up @@ -27,15 +28,15 @@ impl DateFormat {
}
}

#[derive(Copy, Clone, Debug)]
pub struct Date {
pub enable: bool,
pub format: DateFormat,
}

impl Default for Date {
fn default() -> Self {
Self {
format: DateFormat::MonthDateYear,
}
impl Date {
pub fn new(enable: bool, format: DateFormat) -> Self {
Self { enable, format }
}
}

Expand Down
209 changes: 125 additions & 84 deletions komorebi-bar/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ mod time;
mod widget;

use crate::date::Date;
use crate::date::DateFormat;
use crate::memory::Memory;
use crate::memory::MemoryConfig;
use crate::storage::Storage;
use crate::storage::StorageConfig;
use crate::time::TimeFormat;
use crate::widget::BarWidget;
use crossbeam_channel::Receiver;
use eframe::egui;
Expand All @@ -31,20 +35,20 @@ pub struct Position {
y: f32,
}

impl Into<Vec2> for Position {
fn into(self) -> Vec2 {
Vec2 {
x: self.x,
y: self.y,
impl From<Position> for Vec2 {
fn from(value: Position) -> Self {
Self {
x: value.x,
y: value.y,
}
}
}

impl Into<Pos2> for Position {
fn into(self) -> Pos2 {
Pos2 {
x: self.x,
y: self.y,
impl From<Position> for Pos2 {
fn from(value: Position) -> Self {
Self {
x: value.x,
y: value.y,
}
}
}
Expand All @@ -57,6 +61,10 @@ pub struct Config {
transparent: bool,
monitor_index: usize,
monitor_work_area_offset: Option<komorebi_client::Rect>,
time: Time,
date: Date,
storage: StorageConfig,
memory: MemoryConfig,
}

fn main() -> eframe::Result<()> {
Expand All @@ -72,6 +80,10 @@ fn main() -> eframe::Result<()> {
right: 0,
bottom: 40,
}),
time: Time::new(true, TimeFormat::TwentyFourHour),
date: Date::new(true, DateFormat::DayDateMonthYear),
storage: StorageConfig { enable: true },
memory: MemoryConfig { enable: true },
};

// TODO: ensure that config.monitor_index represents a valid komorebi monitor index
Expand Down Expand Up @@ -165,6 +177,8 @@ struct Komobar {
config: Config,
state_receiver: Receiver<komorebi_client::Notification>,
selected_workspace: String,
focused_window_title: String,
workspace_layout: String,
workspaces: Vec<String>,
time: Time,
date: Date,
Expand All @@ -184,35 +198,49 @@ impl Komobar {
// for e.g. egui::PaintCallback.

Self {
config: *config.as_ref(),
config: *config,
state_receiver: rx,
selected_workspace: String::new(),
focused_window_title: String::new(),
workspace_layout: String::new(),
workspaces: vec![],
time: Time::default(),
date: Date::default(),
memory: Memory::default(),
storage: Storage::default(),
time: config.time,
date: config.date,
memory: Memory::from(config.memory),
storage: Storage::from(config.storage),
}
}
}

impl Komobar {
fn handle_komorebi_notification(&mut self) {
if let Ok(notification) = self.state_receiver.try_recv() {
self.workspaces = {
let mut workspaces = vec![];
let monitor = &notification.state.monitors.elements()[self.config.monitor_index];
let focused_workspace_idx = monitor.focused_workspace_idx();
self.selected_workspace = monitor.workspaces()[focused_workspace_idx]
.name()
.to_owned()
.unwrap_or_else(|| format!("{}", focused_workspace_idx + 1));

for (i, ws) in monitor.workspaces().iter().enumerate() {
workspaces.push(ws.name().to_owned().unwrap_or_else(|| format!("{}", i + 1)));
}
let monitor = &notification.state.monitors.elements()[self.config.monitor_index];
let focused_workspace_idx = monitor.focused_workspace_idx();

let mut workspaces = vec![];
self.selected_workspace = monitor.workspaces()[focused_workspace_idx]
.name()
.to_owned()
.unwrap_or_else(|| format!("{}", focused_workspace_idx + 1));

workspaces
for (i, ws) in monitor.workspaces().iter().enumerate() {
workspaces.push(ws.name().to_owned().unwrap_or_else(|| format!("{}", i + 1)));
}

self.workspaces = workspaces;
self.workspace_layout = match monitor.workspaces()[focused_workspace_idx].layout() {
komorebi_client::Layout::Default(layout) => layout.to_string(),
komorebi_client::Layout::Custom(_) => String::from("Custom"),
};

if let Some(container) = monitor.workspaces()[focused_workspace_idx].focused_container()
{
if let Some(window) = container.focused_window() {
if let Ok(title) = window.title() {
self.focused_window_title.clone_from(&title);
}
}
}
}
}
Expand Down Expand Up @@ -267,78 +295,91 @@ impl eframe::App for Komobar {
komorebi_client::send_message(&SocketMessage::Retile).unwrap();
}
}
});

// TODO: make the order configurable
// TODO: make each widget optional??
ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
for time in self.time.output() {
ctx.request_repaint();
if ui
.label(format!("🕐 {}", time))
.on_hover_cursor(CursorIcon::default())
.clicked()
{
// TODO: make default format configurable
self.time.format.toggle()
}
}
ui.label(&self.workspace_layout);

// TODO: make spacing configurable
ui.add_space(10.0);

for date in self.date.output() {
if ui
.label(format!("📅 {}", date))
.on_hover_cursor(CursorIcon::default())
.clicked()
{
// TODO: make default format configurable
self.date.format.next()
};
}
ui.label(&self.focused_window_title);

// TODO: make spacing configurable
ui.add_space(10.0);
});

for ram in self.memory.output() {
if ui
// TODO: make label configurable??
.label(format!("🐏 {}", ram))
.on_hover_cursor(CursorIcon::default())
.clicked()
{
if let Err(error) =
Command::new("cmd.exe").args(["/C", "taskmgr.exe"]).output()
// TODO: make the order configurable
ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
if self.time.enable {
for time in self.time.output() {
ctx.request_repaint();
if ui
.label(format!("🕐 {}", time))
.on_hover_cursor(CursorIcon::default())
.clicked()
{
eprintln!("{}", error)
self.time.format.toggle()
}
};
}

// TODO: make spacing configurable
ui.add_space(10.0);
}

ui.add_space(10.0);
if self.date.enable {
for date in self.date.output() {
if ui
.label(format!("📅 {}", date))
.on_hover_cursor(CursorIcon::default())
.clicked()
{
self.date.format.next()
};
}

for disk in self.storage.output() {
if ui
// TODO: Make emoji configurable??
.label(format!("🖴 {}", disk))
.on_hover_cursor(CursorIcon::default())
.clicked()
{
if let Err(error) = Command::new("cmd.exe")
.args([
"/C",
"explorer.exe",
disk.split(' ').collect::<Vec<&str>>()[0],
])
.output()
// TODO: make spacing configurable
ui.add_space(10.0);
}

if self.memory.enable {
for ram in self.memory.output() {
if ui
// TODO: make label configurable??
.label(format!("🐏 {}", ram))
.on_hover_cursor(CursorIcon::default())
.clicked()
{
eprintln!("{}", error)
}
};
if let Err(error) =
Command::new("cmd.exe").args(["/C", "taskmgr.exe"]).output()
{
eprintln!("{}", error)
}
};
}

ui.add_space(10.0);
}

if self.storage.enable {
for disk in self.storage.output() {
if ui
// TODO: Make emoji configurable??
.label(format!("🖴 {}", disk))
.on_hover_cursor(CursorIcon::default())
.clicked()
{
if let Err(error) = Command::new("cmd.exe")
.args([
"/C",
"explorer.exe",
disk.split(' ').collect::<Vec<&str>>()[0],
])
.output()
{
eprintln!("{}", error)
}
};

ui.add_space(10.0);
}
}
})
})
});
Expand Down
11 changes: 9 additions & 2 deletions komorebi-bar/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@ use sysinfo::RefreshKind;
use sysinfo::System;

pub struct Memory {
pub enable: bool,
system: System,
}

impl Default for Memory {
fn default() -> Self {
#[derive(Copy, Clone, Debug)]
pub struct MemoryConfig {
pub enable: bool,
}

impl From<MemoryConfig> for Memory {
fn from(value: MemoryConfig) -> Self {
Self {
enable: value.enable,
system: System::new_with_specifics(
RefreshKind::default().without_cpu().without_processes(),
),
Expand Down
15 changes: 11 additions & 4 deletions komorebi-bar/src/storage.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
use crate::widget::BarWidget;
use sysinfo::Disks;

pub struct Storage {
disks: Disks,
#[derive(Copy, Clone, Debug)]
pub struct StorageConfig {
pub enable: bool,
}

impl Default for Storage {
fn default() -> Self {
impl From<StorageConfig> for Storage {
fn from(value: StorageConfig) -> Self {
Self {
enable: value.enable,
disks: Disks::new_with_refreshed_list(),
}
}
}

pub struct Storage {
pub enable: bool,
disks: Disks,
}

impl BarWidget for Storage {
fn output(&mut self) -> Vec<String> {
self.disks.refresh();
Expand Down
11 changes: 6 additions & 5 deletions komorebi-bar/src/time.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::widget::BarWidget;

#[derive(Copy, Clone, Debug)]
pub enum TimeFormat {
TwelveHour,
TwentyFourHour,
Expand All @@ -21,15 +22,15 @@ impl TimeFormat {
}
}

#[derive(Copy, Clone, Debug)]
pub struct Time {
pub enable: bool,
pub format: TimeFormat,
}

impl Default for Time {
fn default() -> Self {
Self {
format: TimeFormat::TwelveHour,
}
impl Time {
pub fn new(enable: bool, format: TimeFormat) -> Self {
Self { enable, format }
}
}

Expand Down

0 comments on commit ca6bf69

Please sign in to comment.