Skip to content

Commit

Permalink
Merge pull request #371 from JakeStanger/refactor/encapsulate
Browse files Browse the repository at this point in the history
refactor: begin restructuring core code to better encapsulate
  • Loading branch information
JakeStanger authored Dec 9, 2023
2 parents 7175cf2 + b2fa19a commit 56f423e
Show file tree
Hide file tree
Showing 14 changed files with 493 additions and 418 deletions.
388 changes: 222 additions & 166 deletions src/bar.rs

Large diffs are not rendered by default.

File renamed without changes.
5 changes: 2 additions & 3 deletions src/clients/system_tray.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::unique_id::get_unique_usize;
use crate::{arc_mut, lock, send};
use crate::{arc_mut, lock, send, Ironbar};
use async_once::AsyncOnce;
use color_eyre::Report;
use lazy_static::lazy_static;
Expand All @@ -25,7 +24,7 @@ pub struct TrayEventReceiver {

impl TrayEventReceiver {
async fn new() -> system_tray::error::Result<Self> {
let id = format!("ironbar-{}", get_unique_usize());
let id = format!("ironbar-{}", Ironbar::unique_id());

let (tx, rx) = mpsc::channel(16);
let (b_tx, b_rx) = broadcast::channel(16);
Expand Down
5 changes: 2 additions & 3 deletions src/clients/wayland/wlr_data_control/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use self::device::{DataControlDeviceDataExt, DataControlDeviceHandler};
use self::offer::{DataControlDeviceOffer, DataControlOfferHandler, SelectionOffer};
use self::source::DataControlSourceHandler;
use crate::clients::wayland::Environment;
use crate::unique_id::get_unique_usize;
use crate::{lock, send};
use crate::{lock, send, Ironbar};
use device::DataControlDevice;
use glib::Bytes;
use nix::fcntl::{fcntl, F_GETPIPE_SZ, F_SETPIPE_SZ};
Expand Down Expand Up @@ -145,7 +144,7 @@ impl Environment {
};

Ok(ClipboardItem {
id: get_unique_usize(),
id: Ironbar::unique_id(),
value,
mime_type: mime_type.value.clone(),
})
Expand Down
5 changes: 2 additions & 3 deletions src/clients/wayland/wlr_foreign_toplevel/handle.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use super::manager::ToplevelManagerState;
use crate::lock;
use crate::unique_id::get_unique_usize;
use crate::{lock, Ironbar};
use std::collections::HashSet;
use std::sync::{Arc, Mutex};
use tracing::trace;
Expand Down Expand Up @@ -68,7 +67,7 @@ pub struct ToplevelInfo {
impl Default for ToplevelInfo {
fn default() -> Self {
Self {
id: get_unique_usize(),
id: Ironbar::unique_id(),
app_id: String::new(),
title: String::new(),
fullscreen: false,
Expand Down
6 changes: 3 additions & 3 deletions src/dynamic_value/dynamic_bool.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(feature = "ipc")]
use crate::ironvar::get_variable_manager;
use crate::script::Script;
use crate::send;
#[cfg(feature = "ipc")]
use crate::Ironbar;
use cfg_if::cfg_if;
use glib::Continue;
use serde::Deserialize;
Expand Down Expand Up @@ -55,7 +55,7 @@ impl DynamicBool {
}
#[cfg(feature = "ipc")]
DynamicBool::Variable(variable) => {
let variable_manager = get_variable_manager();
let variable_manager = Ironbar::variable_manager();

let variable_name = variable[1..].into(); // remove hash
let mut rx = crate::write_lock!(variable_manager).subscribe(variable_name);
Expand Down
6 changes: 3 additions & 3 deletions src/dynamic_value/dynamic_string.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[cfg(feature = "ipc")]
use crate::ironvar::get_variable_manager;
use crate::script::{OutputStream, Script};
#[cfg(feature = "ipc")]
use crate::Ironbar;
use crate::{arc_mut, lock, send};
use gtk::prelude::*;
use tokio::spawn;
Expand Down Expand Up @@ -72,7 +72,7 @@ where
lock!(label_parts).push(String::new());

spawn(async move {
let variable_manager = get_variable_manager();
let variable_manager = Ironbar::variable_manager();
let mut rx = crate::write_lock!(variable_manager).subscribe(name);

while let Ok(value) = rx.recv().await {
Expand Down
43 changes: 0 additions & 43 deletions src/global_state.rs

This file was deleted.

7 changes: 1 addition & 6 deletions src/ipc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,21 @@ pub mod commands;
pub mod responses;
mod server;

use std::cell::RefCell;
use std::path::{Path, PathBuf};
use std::rc::Rc;
use tracing::warn;

use crate::GlobalState;
pub use commands::Command;
pub use responses::Response;

#[derive(Debug)]
pub struct Ipc {
path: PathBuf,
global_state: Rc<RefCell<GlobalState>>,
}

impl Ipc {
/// Creates a new IPC instance.
/// This can be used as both a server and client.
pub fn new(global_state: Rc<RefCell<GlobalState>>) -> Self {
pub fn new() -> Self {
let ipc_socket_file = std::env::var("XDG_RUNTIME_DIR")
.map_or_else(|_| PathBuf::from("/tmp"), PathBuf::from)
.join("ironbar-ipc.sock");
Expand All @@ -32,7 +28,6 @@ impl Ipc {

Self {
path: ipc_socket_file,
global_state,
}
}

Expand Down
140 changes: 77 additions & 63 deletions src/ipc/server.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::cell::RefCell;
use std::fs;
use std::path::Path;
use std::rc::Rc;
Expand All @@ -15,18 +14,17 @@ use tracing::{debug, error, info, warn};

use crate::bridge_channel::BridgeChannel;
use crate::ipc::{Command, Response};
use crate::ironvar::get_variable_manager;
use crate::modules::PopupButton;
use crate::style::load_css;
use crate::{read_lock, send_async, try_send, write_lock, GlobalState};
use crate::{read_lock, send_async, try_send, write_lock, Ironbar};

use super::Ipc;

impl Ipc {
/// Starts the IPC server on its socket.
///
/// Once started, the server will begin accepting connections.
pub fn start(&self, application: &Application) {
pub fn start(&self, application: &Application, ironbar: Rc<Ironbar>) {
let bridge = BridgeChannel::<Command>::new();
let cmd_tx = bridge.create_sender();
let (res_tx, mut res_rx) = mpsc::channel(32);
Expand Down Expand Up @@ -70,9 +68,8 @@ impl Ipc {
});

let application = application.clone();
let global_state = self.global_state.clone();
bridge.recv(move |command| {
let res = Self::handle_command(command, &application, &global_state);
let res = Self::handle_command(command, &application, ironbar.clone());
try_send!(res_tx, res);
Continue(true)
});
Expand Down Expand Up @@ -115,7 +112,7 @@ impl Ipc {
fn handle_command(
command: Command,
application: &Application,
global_state: &Rc<RefCell<GlobalState>>,
ironbar: Rc<Ironbar>,
) -> Response {
match command {
Command::Inspect => {
Expand All @@ -129,20 +126,20 @@ impl Ipc {
window.close();
}

crate::load_interface(application, global_state);
*ironbar.bars.borrow_mut() = crate::load_interface(application);

Response::Ok
}
Command::Set { key, value } => {
let variable_manager = get_variable_manager();
let variable_manager = Ironbar::variable_manager();
let mut variable_manager = write_lock!(variable_manager);
match variable_manager.set(key, value) {
Ok(()) => Response::Ok,
Err(err) => Response::error(&format!("{err}")),
}
}
Command::Get { key } => {
let variable_manager = get_variable_manager();
let variable_manager = Ironbar::variable_manager();
let value = read_lock!(variable_manager).get(&key);
match value {
Some(value) => Response::OkValue { value },
Expand All @@ -158,68 +155,85 @@ impl Ipc {
}
}
Command::TogglePopup { bar_name, name } => {
let global_state = global_state.borrow();
let response = global_state.with_popup_mut(&bar_name, |mut popup| {
let current_widget = popup.current_widget();
popup.hide();

let data = popup
.cache
.iter()
.find(|(_, (module_name, _))| module_name == &name)
.map(|module| (module, module.1 .1.buttons.first()));

match data {
Some(((&id, _), Some(button))) if current_widget != Some(id) => {
let button_id = button.popup_id();
popup.show(id, button_id);

Response::Ok
let bar = ironbar.bar_by_name(&bar_name);

match bar {
Some(bar) => {
let popup = bar.popup();
let current_widget = popup.borrow().current_widget();

popup.borrow_mut().hide();

let data = popup
.borrow()
.cache
.iter()
.find(|(_, value)| value.name == name)
.map(|(id, value)| (*id, value.content.buttons.first().cloned()));

match data {
Some((id, Some(button))) if current_widget != Some(id) => {
let button_id = button.popup_id();
let mut popup = popup.borrow_mut();

if popup.is_visible() {
popup.hide();
} else {
popup.show(id, button_id);
}

Response::Ok
}
Some((_, None)) => Response::error("Module has no popup functionality"),
Some(_) => Response::Ok,
None => Response::error("Invalid module name"),
}
Some((_, None)) => Response::error("Module has no popup functionality"),
Some(_) => Response::Ok,
None => Response::error("Invalid module name"),
}
});

response.unwrap_or_else(|| Response::error("Invalid monitor name"))
None => Response::error("Invalid bar name"),
}
}
Command::OpenPopup { bar_name, name } => {
let global_state = global_state.borrow();
let response = global_state.with_popup_mut(&bar_name, |mut popup| {
// only one popup per bar, so hide if open for another widget
popup.hide();

let data = popup
.cache
.iter()
.find(|(_, (module_name, _))| module_name == &name)
.map(|module| (module, module.1 .1.buttons.first()));

match data {
Some(((&id, _), Some(button))) => {
let button_id = button.popup_id();
popup.show(id, button_id);

Response::Ok
let bar = ironbar.bar_by_name(&bar_name);

match bar {
Some(bar) => {
let popup = bar.popup();

// only one popup per bar, so hide if open for another widget
popup.borrow_mut().hide();

let data = popup
.borrow()
.cache
.iter()
.find(|(_, value)| value.name == name)
.map(|(id, value)| (*id, value.content.buttons.first().cloned()));

match data {
Some((id, Some(button))) => {
let button_id = button.popup_id();
popup.borrow_mut().show(id, button_id);

Response::Ok
}
Some((_, None)) => Response::error("Module has no popup functionality"),
None => Response::error("Invalid module name"),
}
Some((_, None)) => Response::error("Module has no popup functionality"),
None => Response::error("Invalid module name"),
}
});

response.unwrap_or_else(|| Response::error("Invalid monitor name"))
None => Response::error("Invalid bar name"),
}
}
Command::ClosePopup { bar_name } => {
let global_state = global_state.borrow();
let popup_found = global_state
.with_popup_mut(&bar_name, |mut popup| popup.hide())
.is_some();
let bar = ironbar.bar_by_name(&bar_name);

if popup_found {
Response::Ok
} else {
Response::error("Invalid monitor name")
match bar {
Some(bar) => {
let popup = bar.popup();
popup.borrow_mut().hide();

Response::Ok
}
None => Response::error("Invalid bar name"),
}
}
Command::Ping => Response::Ok,
Expand Down
Loading

0 comments on commit 56f423e

Please sign in to comment.