Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 18 additions & 22 deletions komorebi-bar/src/bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ use komorebi_client::NotificationEvent;
use komorebi_client::PathExt;
use komorebi_client::SocketMessage;
use komorebi_client::VirtualDesktopNotification;
use komorebi_client::Window;
use komorebi_themes::catppuccin_egui;
use komorebi_themes::Base16Value;
use komorebi_themes::Base16Wrapper;
Expand Down Expand Up @@ -128,7 +129,7 @@ fn stop_powershell() -> Result<()> {

pub fn exec_powershell(cmd: &str) -> Result<()> {
if let Some(session_stdin) = SESSION_STDIN.lock().as_mut() {
if let Err(e) = writeln!(session_stdin, "{}", cmd) {
if let Err(e) = writeln!(session_stdin, "{cmd}") {
tracing::error!(error = %e, cmd = cmd, "failed to write command to PowerShell stdin");
return Err(e);
}
Expand All @@ -148,7 +149,7 @@ pub fn exec_powershell(cmd: &str) -> Result<()> {
}

pub struct Komobar {
pub hwnd: Option<isize>,
pub window: Option<Window>,
pub monitor_index: Option<usize>,
pub disabled: bool,
pub config: KomobarConfig,
Expand Down Expand Up @@ -635,8 +636,7 @@ impl Komobar {

assert!(
home.is_dir(),
"$Env:KOMOREBI_CONFIG_HOME is set to '{}', which is not a valid directory",
home_path
"$Env:KOMOREBI_CONFIG_HOME is set to '{home_path}', which is not a valid directory",
);

home
Expand Down Expand Up @@ -720,7 +720,7 @@ impl Komobar {
config: KomobarConfig,
) -> Self {
let mut komobar = Self {
hwnd: process_hwnd(),
window: process_hwnd().map(Window::from),
monitor_index: None,
disabled: false,
config,
Expand Down Expand Up @@ -828,8 +828,7 @@ impl Komobar {
}

pub fn position_bar(&self) {
if let Some(hwnd) = self.hwnd {
let window = komorebi_client::Window::from(hwnd);
if let Some(window) = self.window {
match window.set_position(&self.size_rect, false) {
Ok(_) => {
tracing::info!("updated bar position");
Expand Down Expand Up @@ -865,8 +864,8 @@ impl eframe::App for Komobar {
}

fn update(&mut self, ctx: &Context, _frame: &mut eframe::Frame) {
if self.hwnd.is_none() {
self.hwnd = process_hwnd();
if self.window.is_none() {
self.window = process_hwnd().map(Window::from);
}

if self.scale_factor != ctx.native_pixels_per_point().unwrap_or(1.0) {
Expand Down Expand Up @@ -905,18 +904,18 @@ impl eframe::App for Komobar {
tracing::debug!(
"back on komorebi's associated virtual desktop - restoring bar"
);
if let Some(hwnd) = self.hwnd {
komorebi_client::WindowsApi::restore_window(hwnd);
}
if let Some(window) = self.window {
komorebi_client::WindowsApi::restore_window(window)
};
}
NotificationEvent::VirtualDesktop(
VirtualDesktopNotification::LeftAssociatedVirtualDesktop,
) => {
tracing::debug!(
"no longer on komorebi's associated virtual desktop - minimizing bar"
);
if let Some(hwnd) = self.hwnd {
komorebi_client::WindowsApi::minimize_window(hwnd);
if let Some(window) = self.window {
komorebi_client::WindowsApi::minimize_window(window)
}
}
_ => {}
Expand All @@ -925,7 +924,7 @@ impl eframe::App for Komobar {
if self.monitor_index.is_none()
|| self
.monitor_index
.is_some_and(|idx| idx >= state.monitors.elements().len())
.is_some_and(|idx| idx >= state.monitors.len())
{
if !self.disabled {
// Monitor for this bar got disconnected lets disable the bar until it
Expand All @@ -942,11 +941,8 @@ impl eframe::App for Komobar {

// Restore the bar in case it has been minimized when the monitor
// disconnected
if let Some(hwnd) = self.hwnd {
let window = komorebi_client::Window::from(hwnd);
if window.is_miminized() {
komorebi_client::WindowsApi::restore_window(hwnd);
}
if let Some(window) = self.window.filter(|w| w.is_miminized()) {
komorebi_client::WindowsApi::restore_window(window)
}

// Reset the current `work_area_offset` so that it gets recalculated and
Expand All @@ -966,7 +962,7 @@ impl eframe::App for Komobar {
) {
let monitor_index = self.monitor_index.expect("should have a monitor index");

let monitor_size = state.monitors.elements()[monitor_index].size();
let monitor_size = state.monitors[monitor_index].size();

self.update_monitor_coordinates(monitor_size);

Expand All @@ -979,7 +975,7 @@ impl eframe::App for Komobar {

// Check if monitor coordinates/size has changed
if let Some(monitor_index) = self.monitor_index {
let monitor_size = state.monitors.elements()[monitor_index].size();
let monitor_size = state.monitors[monitor_index].size();
let top = MONITOR_TOP.load(Ordering::SeqCst);
let left = MONITOR_LEFT.load(Ordering::SeqCst);
let right = MONITOR_RIGHT.load(Ordering::SeqCst);
Expand Down
36 changes: 13 additions & 23 deletions komorebi-bar/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ extern "system" fn enum_window(hwnd: HWND, lparam: LPARAM) -> BOOL {
}
}

fn process_hwnd() -> Option<isize> {
fn process_hwnd() -> Option<HWND> {
unsafe {
let mut hwnd = HWND::default();
let _ = EnumThreadWindows(
Expand All @@ -94,10 +94,10 @@ fn process_hwnd() -> Option<isize> {
LPARAM(&mut hwnd as *mut HWND as isize),
);

if hwnd.0 as isize == 0 {
if hwnd.is_invalid() {
None
} else {
Some(hwnd.0 as isize)
Some(hwnd)
}
}
}
Expand Down Expand Up @@ -159,8 +159,7 @@ fn main() -> color_eyre::Result<()> {

assert!(
home.is_dir(),
"$Env:KOMOREBI_CONFIG_HOME is set to '{}', which is not a valid directory",
home_path
"$Env:KOMOREBI_CONFIG_HOME is set to '{home_path}', which is not a valid directory",
);

home
Expand Down Expand Up @@ -230,47 +229,38 @@ fn main() -> color_eyre::Result<()> {
.get(&usr_monitor_index)
.map_or(usr_monitor_index, |i| *i);

MONITOR_RIGHT.store(
state.monitors.elements()[monitor_index].size().right,
Ordering::SeqCst,
);
MONITOR_RIGHT.store(state.monitors[monitor_index].size().right, Ordering::SeqCst);

MONITOR_TOP.store(
state.monitors.elements()[monitor_index].size().top,
Ordering::SeqCst,
);
MONITOR_TOP.store(state.monitors[monitor_index].size().top, Ordering::SeqCst);

MONITOR_LEFT.store(
state.monitors.elements()[monitor_index].size().left,
Ordering::SeqCst,
);
MONITOR_LEFT.store(state.monitors[monitor_index].size().left, Ordering::SeqCst);

MONITOR_INDEX.store(monitor_index, Ordering::SeqCst);

match config.position {
None => {
config.position = Some(PositionConfig {
start: Some(Position {
x: state.monitors.elements()[monitor_index].size().left as f32,
y: state.monitors.elements()[monitor_index].size().top as f32,
x: state.monitors[monitor_index].size().left as f32,
y: state.monitors[monitor_index].size().top as f32,
}),
end: Some(Position {
x: state.monitors.elements()[monitor_index].size().right as f32,
x: state.monitors[monitor_index].size().right as f32,
y: 50.0,
}),
})
}
Some(ref mut position) => {
if position.start.is_none() {
position.start = Some(Position {
x: state.monitors.elements()[monitor_index].size().left as f32,
y: state.monitors.elements()[monitor_index].size().top as f32,
x: state.monitors[monitor_index].size().left as f32,
y: state.monitors[monitor_index].size().top as f32,
});
}

if position.end.is_none() {
position.end = Some(Position {
x: state.monitors.elements()[monitor_index].size().right as f32,
x: state.monitors[monitor_index].size().right as f32,
y: 50.0,
})
}
Expand Down
2 changes: 1 addition & 1 deletion komorebi-bar/src/widgets/battery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl BarWidget for Battery {
.args(["/C", "start", "ms-settings:batterysaver"])
.spawn()
{
eprintln!("{}", error)
eprintln!("{error}")
}
}
});
Expand Down
6 changes: 3 additions & 3 deletions komorebi-bar/src/widgets/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ impl Cpu {

CpuOutput {
label: match self.label_prefix {
LabelPrefix::Text | LabelPrefix::IconAndText => format!("CPU: {}%", used),
LabelPrefix::None | LabelPrefix::Icon => format!("{}%", used),
LabelPrefix::Text | LabelPrefix::IconAndText => format!("CPU: {used}%"),
LabelPrefix::None | LabelPrefix::Icon => format!("{used}%"),
},
selected,
}
Expand Down Expand Up @@ -124,7 +124,7 @@ impl BarWidget for Cpu {
if let Err(error) =
Command::new("cmd.exe").args(["/C", "taskmgr.exe"]).spawn()
{
eprintln!("{}", error)
eprintln!("{error}")
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion komorebi-bar/src/widgets/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ impl Date {
.to_string()
.trim()
.to_string(),
Err(_) => format!("Invalid timezone: {}", timezone),
Err(_) => format!("Invalid timezone: {timezone}"),
},
None => Local::now()
.format(&self.format.fmt_string())
Expand Down
21 changes: 10 additions & 11 deletions komorebi-bar/src/widgets/komorebi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ impl KomorebiNotificationState {
self.monitor_usr_idx_map = notification.state.monitor_usr_idx_map.clone();

if monitor_index.is_none()
|| monitor_index.is_some_and(|idx| idx >= notification.state.monitors.elements().len())
|| monitor_index.is_some_and(|idx| idx >= notification.state.monitors.len())
{
// The bar's monitor is diconnected, so the bar is disabled no need to check anything
// any further otherwise we'll get `OutOfBounds` panics.
Expand All @@ -770,9 +770,8 @@ impl KomorebiNotificationState {

self.mouse_follows_focus = notification.state.mouse_follows_focus;

let monitor = &notification.state.monitors.elements()[monitor_index];
self.work_area_offset =
notification.state.monitors.elements()[monitor_index].work_area_offset();
let monitor = &notification.state.monitors[monitor_index];
self.work_area_offset = notification.state.monitors[monitor_index].work_area_offset();

let focused_workspace_idx = monitor.focused_workspace_idx();

Expand All @@ -783,7 +782,7 @@ impl KomorebiNotificationState {
.to_owned()
.unwrap_or_else(|| format!("{}", focused_workspace_idx + 1));

for (i, ws) in monitor.workspaces().iter().enumerate() {
for (i, ws) in monitor.workspaces().indexed() {
let should_show = if self.hide_empty_workspaces {
focused_workspace_idx == i || !ws.is_empty()
} else {
Expand All @@ -803,7 +802,7 @@ impl KomorebiNotificationState {
}

// add all tiled windows
for (i, container) in ws.containers().iter().enumerate() {
for (i, container) in ws.containers().indexed() {
containers.push((
!has_monocle && i == ws.focused_container_idx(),
container.into(),
Expand Down Expand Up @@ -887,10 +886,10 @@ impl From<&Container> for KomorebiNotificationStateContainerInformation {
let windows = value.windows().iter().collect::<Vec<_>>();

let icons = windows
.iter()
.into_iter()
.map(|window| {
ImageIcon::try_load(window.hwnd, || {
windows_icons::get_icon_by_hwnd(window.hwnd).or_else(|| {
ImageIcon::try_load(*window, || {
windows_icons::get_icon_by_hwnd(window.as_isize()).or_else(|| {
windows_icons_fallback::get_icon_by_process_id(window.process_id())
})
})
Expand All @@ -911,8 +910,8 @@ impl From<&Container> for KomorebiNotificationStateContainerInformation {

impl From<&Window> for KomorebiNotificationStateContainerInformation {
fn from(value: &Window) -> Self {
let icons = ImageIcon::try_load(value.hwnd, || {
windows_icons::get_icon_by_hwnd(value.hwnd)
let icons = ImageIcon::try_load(*value, || {
windows_icons::get_icon_by_hwnd(value.as_isize())
.or_else(|| windows_icons_fallback::get_icon_by_process_id(value.process_id()))
});

Expand Down
5 changes: 2 additions & 3 deletions komorebi-bar/src/widgets/komorebi_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ impl<'de> Deserialize<'de> for KomorebiLayout {
let s: String = String::deserialize(deserializer)?;

// Attempt to deserialize the string as a DefaultLayout
if let Ok(default_layout) =
from_str::<komorebi_client::DefaultLayout>(&format!("\"{}\"", s))
if let Ok(default_layout) = from_str::<komorebi_client::DefaultLayout>(&format!("\"{s}\""))
{
return Ok(KomorebiLayout::Default(default_layout));
}
Expand All @@ -53,7 +52,7 @@ impl<'de> Deserialize<'de> for KomorebiLayout {
"Floating" => Ok(KomorebiLayout::Floating),
"Paused" => Ok(KomorebiLayout::Paused),
"Custom" => Ok(KomorebiLayout::Custom),
_ => Err(Error::custom(format!("Invalid layout: {}", s))),
_ => Err(Error::custom(format!("Invalid layout: {s}"))),
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions komorebi-bar/src/widgets/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ impl Memory {
MemoryOutput {
label: match self.label_prefix {
LabelPrefix::Text | LabelPrefix::IconAndText => {
format!("RAM: {}%", usage)
format!("RAM: {usage}%")
}
LabelPrefix::None | LabelPrefix::Icon => format!("{}%", usage),
LabelPrefix::None | LabelPrefix::Icon => format!("{usage}%"),
},
selected,
}
Expand Down Expand Up @@ -128,7 +128,7 @@ impl BarWidget for Memory {
if let Err(error) =
Command::new("cmd.exe").args(["/C", "taskmgr.exe"]).spawn()
{
eprintln!("{}", error)
eprintln!("{error}")
}
}
});
Expand Down
9 changes: 5 additions & 4 deletions komorebi-bar/src/widgets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use eframe::egui::Context;
use eframe::egui::TextureHandle;
use eframe::egui::TextureOptions;
use image::RgbaImage;
use komorebi_client::Window;
use std::collections::HashMap;
use std::path::Path;
use std::sync::Arc;
Expand Down Expand Up @@ -155,7 +156,7 @@ pub enum ImageIconId {
/// Identifier based on a file system path.
Path(Arc<Path>),
/// Windows HWND handle.
Hwnd(isize),
Window(Window),
}

impl From<&Path> for ImageIconId {
Expand All @@ -165,9 +166,9 @@ impl From<&Path> for ImageIconId {
}
}

impl From<isize> for ImageIconId {
impl From<Window> for ImageIconId {
#[inline]
fn from(value: isize) -> Self {
Self::Hwnd(value)
fn from(value: Window) -> Self {
Self::Window(value)
}
}
Loading