Skip to content

Commit

Permalink
feat(bar): add accents to widget emojis
Browse files Browse the repository at this point in the history
  • Loading branch information
LGUG2Z committed Sep 14, 2024
1 parent c580ff7 commit bc4dabd
Show file tree
Hide file tree
Showing 9 changed files with 420 additions and 251 deletions.
351 changes: 178 additions & 173 deletions komorebi-bar/src/bar.rs

Large diffs are not rendered by default.

69 changes: 46 additions & 23 deletions komorebi-bar/src/battery.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use crate::widget::BarWidget;
use crate::WIDGET_SPACING;
use eframe::egui::text::LayoutJob;
use eframe::egui::Context;
use eframe::egui::FontId;
use eframe::egui::Label;
use eframe::egui::Sense;
use eframe::egui::TextFormat;
use eframe::egui::TextStyle;
use eframe::egui::Ui;
use schemars::JsonSchema;
use serde::Deserialize;
Expand All @@ -24,7 +28,7 @@ pub struct BatteryConfig {
impl From<BatteryConfig> for Battery {
fn from(value: BatteryConfig) -> Self {
let manager = Manager::new().unwrap();
let mut last_state = vec![];
let mut last_state = String::new();
let mut state = None;

if let Ok(mut batteries) = manager.batteries() {
Expand All @@ -35,7 +39,8 @@ impl From<BatteryConfig> for Battery {
State::Discharging => state = Some(BatteryState::Discharging),
_ => {}
}
last_state.push(format!("{percentage}%"));

last_state = format!("{percentage}%");
}
}

Expand All @@ -60,17 +65,17 @@ pub struct Battery {
manager: Manager,
pub state: BatteryState,
data_refresh_interval: u64,
last_state: Vec<String>,
last_state: String,
last_updated: Instant,
}

impl Battery {
fn output(&mut self) -> Vec<String> {
let mut outputs = self.last_state.clone();
fn output(&mut self) -> String {
let mut output = self.last_state.clone();

let now = Instant::now();
if now.duration_since(self.last_updated) > Duration::from_secs(self.data_refresh_interval) {
outputs.clear();
output.clear();

if let Ok(mut batteries) = self.manager.batteries() {
if let Some(Ok(first)) = batteries.nth(0) {
Expand All @@ -81,38 +86,56 @@ impl Battery {
_ => {}
}

outputs.push(format!("{percentage}%"));
output = format!("{percentage}%");
}
}

self.last_state.clone_from(&outputs);
self.last_state.clone_from(&output);
self.last_updated = now;
}

outputs
output
}
}

impl BarWidget for Battery {
fn render(&mut self, _ctx: &Context, ui: &mut Ui) {
fn render(&mut self, ctx: &Context, 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 => egui_phosphor::regular::BATTERY_CHARGING,
BatteryState::Discharging => egui_phosphor::regular::BATTERY_FULL,
};

ui.add(
Label::new(format!("{emoji} {battery}"))
.selectable(false)
.sense(Sense::click()),
);
}
let emoji = match self.state {
BatteryState::Charging => egui_phosphor::regular::BATTERY_CHARGING,
BatteryState::Discharging => egui_phosphor::regular::BATTERY_FULL,
};

let font_id = ctx
.style()
.text_styles
.get(&TextStyle::Body)
.cloned()
.unwrap_or_else(FontId::default);

ui.add_space(WIDGET_SPACING);
let mut layout_job = LayoutJob::simple(
emoji.to_string(),
font_id.clone(),
ctx.style().visuals.selection.stroke.color,
100.0,
);

layout_job.append(
&output,
10.0,
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
);

ui.add(
Label::new(layout_job)
.selectable(false)
.sense(Sense::click()),
);
}

ui.add_space(WIDGET_SPACING);
}
}
}
46 changes: 34 additions & 12 deletions komorebi-bar/src/date.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
use crate::widget::BarWidget;
use crate::WIDGET_SPACING;
use eframe::egui::text::LayoutJob;
use eframe::egui::Context;
use eframe::egui::FontId;
use eframe::egui::Label;
use eframe::egui::Sense;
use eframe::egui::TextFormat;
use eframe::egui::TextStyle;
use eframe::egui::Ui;
use eframe::egui::WidgetText;
use schemars::JsonSchema;
use serde::Deserialize;
use serde::Serialize;
Expand Down Expand Up @@ -68,26 +73,43 @@ pub struct Date {
}

impl Date {
fn output(&mut self) -> Vec<String> {
vec![chrono::Local::now()
fn output(&mut self) -> String {
chrono::Local::now()
.format(&self.format.fmt_string())
.to_string()]
.to_string()
}
}

impl BarWidget for Date {
fn render(&mut self, _ctx: &Context, ui: &mut Ui) {
fn render(&mut self, ctx: &Context, ui: &mut Ui) {
if self.enable {
for output in self.output() {
let output = self.output();
if !output.is_empty() {
let font_id = ctx
.style()
.text_styles
.get(&TextStyle::Body)
.cloned()
.unwrap_or_else(FontId::default);

let mut layout_job = LayoutJob::simple(
egui_phosphor::regular::CALENDAR_DOTS.to_string(),
font_id.clone(),
ctx.style().visuals.selection.stroke.color,
100.0,
);

layout_job.append(
&output,
10.0,
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
);

if ui
.add(
Label::new(format!(
"{} {}",
egui_phosphor::regular::CALENDAR_DOTS,
output
))
.selectable(false)
.sense(Sense::click()),
Label::new(WidgetText::LayoutJob(layout_job.clone()))
.selectable(false)
.sense(Sense::click()),
)
.clicked()
{
Expand Down
9 changes: 6 additions & 3 deletions komorebi-bar/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,19 @@ fn main() -> color_eyre::Result<()> {
let config_path_cl = config_path.clone();

hotwatch.watch(config_path, move |event| match event.kind {
EventKind::Modify(_) | EventKind::Remove(_) => {
if let Ok(updated) = KomobarConfig::read(&config_path_cl) {
EventKind::Modify(_) | EventKind::Remove(_) => match KomobarConfig::read(&config_path_cl) {
Ok(updated) => {
tx_config.send(updated).unwrap();

tracing::info!(
"configuration file updated: {}",
config_path_cl.as_path().to_string_lossy()
);
}
}
Err(error) => {
tracing::error!("{error}");
}
},
_ => {}
})?;

Expand Down
41 changes: 33 additions & 8 deletions komorebi-bar/src/media.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use crate::widget::BarWidget;
use crate::WIDGET_SPACING;
use eframe::egui::text::LayoutJob;
use eframe::egui::Context;
use eframe::egui::FontId;
use eframe::egui::Label;
use eframe::egui::Sense;
use eframe::egui::TextFormat;
use eframe::egui::TextStyle;
use eframe::egui::Ui;
use schemars::JsonSchema;
use serde::Deserialize;
Expand Down Expand Up @@ -46,36 +50,57 @@ impl Media {
}
}

fn output(&mut self) -> Vec<String> {
fn output(&mut self) -> String {
if let Ok(session) = self.session_manager.GetCurrentSession() {
if let Ok(operation) = session.TryGetMediaPropertiesAsync() {
if let Ok(properties) = operation.get() {
if let (Ok(artist), Ok(title)) = (properties.Artist(), properties.Title()) {
if artist.is_empty() {
return vec![format!("{title}")];
return format!("{title}");
}

if title.is_empty() {
return vec![format!("{artist}")];
return format!("{artist}");
}

return vec![format!("{artist} - {title}")];
return format!("{artist} - {title}");
}
}
}
}

vec![]
String::new()
}
}

impl BarWidget for Media {
fn render(&mut self, _ctx: &Context, ui: &mut Ui) {
fn render(&mut self, ctx: &Context, ui: &mut Ui) {
if self.enable {
for output in self.output() {
let output = self.output();
if !output.is_empty() {
let font_id = ctx
.style()
.text_styles
.get(&TextStyle::Body)
.cloned()
.unwrap_or_else(FontId::default);

let mut layout_job = LayoutJob::simple(
egui_phosphor::regular::HEADPHONES.to_string(),
font_id.clone(),
ctx.style().visuals.selection.stroke.color,
100.0,
);

layout_job.append(
&output,
10.0,
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
);

if ui
.add(
Label::new(format!("{} {output}", egui_phosphor::regular::HEADPHONES))
Label::new(layout_job)
.selectable(false)
.sense(Sense::click()),
)
Expand Down
35 changes: 30 additions & 5 deletions komorebi-bar/src/memory.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use crate::widget::BarWidget;
use crate::WIDGET_SPACING;
use eframe::egui::text::LayoutJob;
use eframe::egui::Context;
use eframe::egui::FontId;
use eframe::egui::Label;
use eframe::egui::Sense;
use eframe::egui::TextFormat;
use eframe::egui::TextStyle;
use eframe::egui::Ui;
use schemars::JsonSchema;
use serde::Deserialize;
Expand Down Expand Up @@ -45,7 +49,7 @@ pub struct Memory {
}

impl Memory {
fn output(&mut self) -> Vec<String> {
fn output(&mut self) -> String {
let now = Instant::now();
if now.duration_since(self.last_updated) > Duration::from_secs(self.data_refresh_interval) {
self.system.refresh_memory();
Expand All @@ -54,17 +58,38 @@ impl Memory {

let used = self.system.used_memory();
let total = self.system.total_memory();
vec![format!("RAM: {}%", (used * 100) / total)]
format!("RAM: {}%", (used * 100) / total)
}
}

impl BarWidget for Memory {
fn render(&mut self, _ctx: &Context, ui: &mut Ui) {
fn render(&mut self, ctx: &Context, ui: &mut Ui) {
if self.enable {
for output in self.output() {
let output = self.output();
if !output.is_empty() {
let font_id = ctx
.style()
.text_styles
.get(&TextStyle::Body)
.cloned()
.unwrap_or_else(FontId::default);

let mut layout_job = LayoutJob::simple(
egui_phosphor::regular::MEMORY.to_string(),
font_id.clone(),
ctx.style().visuals.selection.stroke.color,
100.0,
);

layout_job.append(
&output,
10.0,
TextFormat::simple(font_id, ctx.style().visuals.text_color()),
);

if ui
.add(
Label::new(format!("{} {}", egui_phosphor::regular::MEMORY, output))
Label::new(layout_job)
.selectable(false)
.sense(Sense::click()),
)
Expand Down
Loading

0 comments on commit bc4dabd

Please sign in to comment.