Skip to content

Commit

Permalink
Simplify logic and remove full clones
Browse files Browse the repository at this point in the history
  • Loading branch information
FieldofClay committed Jan 26, 2024
1 parent b8b053f commit 6f3d54e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 59 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hyprland-activewindow"
version = "0.4.4"
version = "0.5.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
125 changes: 68 additions & 57 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use hyprland::data::{Monitors, Workspaces};
use hyprland::event_listener::EventListenerMutable as EventListener;
use hyprland::shared::HyprData;
use hyprland::Result;
use std::env;
use serde::Serialize;
use serde_json::json;
use std::env;
use std::sync::Arc;

const HELP: &str = "\
hyprland-activewindow: a multi monitor aware active hyprland window title reporter, designed to be used with eww.
Expand All @@ -26,37 +27,55 @@ struct MonitorCustom {
pub title: String,
}

fn print_title(mon: &String) {
let active_workspace_id = Monitors::get()
.expect("unable to get monitors")
.find(|m| m.name == mon.to_string())
.unwrap()
.active_workspace
.id;
let title = Workspaces::get()
.expect("unable to get workspaces")
.find(|w| w.id == active_workspace_id)
.unwrap()
.last_window_title;
println!("{}", title);
struct WindowPrinter {
mon: String,
}

fn print_all() {
let monitors = Monitors::get().expect("unable to get monitors");
let mut out_monitors: Vec<MonitorCustom> = Vec::new();
for monitor in monitors {
impl WindowPrinter {
pub(crate) fn new(mon: String) -> Self {
Self { mon }
}

pub fn print(&self) {
if self.mon == "_" {
self.print_all();
} else {
self.print_single();
}
}

fn print_single(&self) {
let active_workspace_id = Monitors::get()
.expect("unable to get monitors")
.find(|m| m.name == self.mon.to_string())
.unwrap()
.active_workspace
.id;
let title = Workspaces::get()
.expect("unable to get workspaces")
.find(|w| w.id == monitor.active_workspace.id)
.unwrap()
.last_window_title;
let mc: MonitorCustom = MonitorCustom {
name: monitor.name,
title,
};
out_monitors.push(mc);
.expect("unable to get workspaces")
.find(|w| w.id == active_workspace_id)
.unwrap()
.last_window_title;
println!("{}", title);
}

fn print_all(&self) {
let monitors = Monitors::get().expect("unable to get monitors");
let mut out_monitors: Vec<MonitorCustom> = Vec::new();
for monitor in monitors {
let title = Workspaces::get()
.expect("unable to get workspaces")
.find(|w| w.id == monitor.active_workspace.id)
.unwrap()
.last_window_title;
let mc: MonitorCustom = MonitorCustom {
name: monitor.name,
title,
};
out_monitors.push(mc);
}
println!("{}", json!(out_monitors).to_string());
}
println!("{}", json!(out_monitors).to_string());
}

fn main() -> Result<()> {
Expand All @@ -68,41 +87,33 @@ fn main() -> Result<()> {
}
let mon = args[1].to_string();
let mon_object = Monitors::get()
.expect("unable to get monitors")
.find(|m| m.name == mon);
.expect("unable to get monitors")
.find(|m| m.name == mon);
if mon_object.is_none() && mon != "_" {
println!("Unable to find monitor {mon}");
std::process::exit(0);
}
if mon == "_" {
print_all();
} else {
print_title(&mon);
println!("Unable to find monitor {mon}");
std::process::exit(0);
}

let wp = Arc::new(WindowPrinter::new(mon));
wp.print();

// Create a event listener
let mut event_listener = EventListener::new();
let mon2 = mon.clone();
let mon3 = mon.clone();
event_listener.add_active_window_change_handler(move |_, state| {
if mon.eq(&state.active_monitor) {
print_title(&mon);
} else if mon == "_" {
print_all();
}
let wp_clone = Arc::clone(&wp);
event_listener.add_active_window_change_handler(move |_, _| {
wp_clone.print();
});
event_listener.add_window_close_handler(move |_, state| {
if mon2.eq(&state.active_monitor) {
print_title(&mon2);
} else if mon2 == "_" {
print_all();
}
let wp_clone = Arc::clone(&wp);
event_listener.add_window_close_handler(move |_, _| {
wp_clone.print();
});
event_listener.add_workspace_change_handler(move |_, state| {
if mon3.eq(&state.active_monitor) {
print_title(&mon3);
} else if mon3 == "_" {
print_all();
}
let wp_clone = Arc::clone(&wp);
event_listener.add_workspace_change_handler(move |_, _| {
wp_clone.print();
});
let wp_clone = Arc::clone(&wp);
event_listener.add_window_moved_handler(move |_, _| {
wp_clone.print();
});

event_listener.start_listener()
Expand Down

0 comments on commit 6f3d54e

Please sign in to comment.