mirror of
https://github.com/Zedfrigg/ironbar.git
synced 2025-07-01 18:51:04 +02:00
feat(ipc): reload config command
This commit is contained in:
parent
4620f29d38
commit
7d3bb02b46
4 changed files with 82 additions and 47 deletions
|
@ -57,6 +57,20 @@ Responds with `ok`.
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### `reload`
|
||||||
|
|
||||||
|
Restarts the bars, reloading the config in the process.
|
||||||
|
|
||||||
|
The IPC server and main GTK application are untouched.
|
||||||
|
|
||||||
|
Responds with `ok`.
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"type": "reload"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### `get`
|
### `get`
|
||||||
|
|
||||||
Gets an [ironvar](ironvars) value.
|
Gets an [ironvar](ironvars) value.
|
||||||
|
|
|
@ -11,12 +11,15 @@ pub enum Command {
|
||||||
/// Open the GTK inspector
|
/// Open the GTK inspector
|
||||||
Inspect,
|
Inspect,
|
||||||
|
|
||||||
|
/// Reload the config
|
||||||
|
Reload,
|
||||||
|
|
||||||
/// Set an `ironvar` value.
|
/// Set an `ironvar` value.
|
||||||
/// This creates it if it does not already exist, and updates it if it does.
|
/// This creates it if it does not already exist, and updates it if it does.
|
||||||
/// Any references to this variable are automatically and immediately updated.
|
/// Any references to this variable are automatically and immediately updated.
|
||||||
/// Keys and values can be any valid UTF-8 string.
|
/// Keys and values can be any valid UTF-8 string.
|
||||||
Set {
|
Set {
|
||||||
/// Variable key. Can be any valid UTF-8 string.
|
/// Variable key. Can be any alphanumeric ASCII string.
|
||||||
key: Box<str>,
|
key: Box<str>,
|
||||||
/// Variable value. Can be any valid UTF-8 string.
|
/// Variable value. Can be any valid UTF-8 string.
|
||||||
value: String,
|
value: String,
|
||||||
|
|
|
@ -6,19 +6,20 @@ use crate::style::load_css;
|
||||||
use crate::{read_lock, send_async, try_send, write_lock};
|
use crate::{read_lock, send_async, try_send, write_lock};
|
||||||
use color_eyre::{Report, Result};
|
use color_eyre::{Report, Result};
|
||||||
use glib::Continue;
|
use glib::Continue;
|
||||||
|
use gtk::prelude::*;
|
||||||
|
use gtk::Application;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||||
use tokio::net::{UnixListener, UnixStream};
|
use tokio::net::{UnixListener, UnixStream};
|
||||||
use tokio::spawn;
|
use tokio::spawn;
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc::{self, Receiver, Sender};
|
||||||
use tokio::sync::mpsc::{Receiver, Sender};
|
|
||||||
use tracing::{debug, error, info, warn};
|
use tracing::{debug, error, info, warn};
|
||||||
|
|
||||||
impl Ipc {
|
impl Ipc {
|
||||||
/// Starts the IPC server on its socket.
|
/// Starts the IPC server on its socket.
|
||||||
///
|
///
|
||||||
/// Once started, the server will begin accepting connections.
|
/// Once started, the server will begin accepting connections.
|
||||||
pub fn start(&self) {
|
pub fn start(&self, application: &Application) {
|
||||||
let bridge = BridgeChannel::<Command>::new();
|
let bridge = BridgeChannel::<Command>::new();
|
||||||
let cmd_tx = bridge.create_sender();
|
let cmd_tx = bridge.create_sender();
|
||||||
let (res_tx, mut res_rx) = mpsc::channel(32);
|
let (res_tx, mut res_rx) = mpsc::channel(32);
|
||||||
|
@ -61,8 +62,9 @@ impl Ipc {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let application = application.clone();
|
||||||
bridge.recv(move |command| {
|
bridge.recv(move |command| {
|
||||||
let res = Self::handle_command(command);
|
let res = Self::handle_command(command, &application);
|
||||||
try_send!(res_tx, res);
|
try_send!(res_tx, res);
|
||||||
Continue(true)
|
Continue(true)
|
||||||
});
|
});
|
||||||
|
@ -102,12 +104,23 @@ impl Ipc {
|
||||||
/// Takes an input command, runs it and returns with the appropriate response.
|
/// Takes an input command, runs it and returns with the appropriate response.
|
||||||
///
|
///
|
||||||
/// This runs on the main thread, allowing commands to interact with GTK.
|
/// This runs on the main thread, allowing commands to interact with GTK.
|
||||||
fn handle_command(command: Command) -> Response {
|
fn handle_command(command: Command, application: &Application) -> Response {
|
||||||
match command {
|
match command {
|
||||||
Command::Inspect => {
|
Command::Inspect => {
|
||||||
gtk::Window::set_interactive_debugging(true);
|
gtk::Window::set_interactive_debugging(true);
|
||||||
Response::Ok
|
Response::Ok
|
||||||
}
|
}
|
||||||
|
Command::Reload => {
|
||||||
|
info!("Closing existing bars");
|
||||||
|
let windows = application.windows();
|
||||||
|
for window in windows {
|
||||||
|
window.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
crate::load_interface(application);
|
||||||
|
|
||||||
|
Response::Ok
|
||||||
|
}
|
||||||
Command::Set { key, value } => {
|
Command::Set { key, value } => {
|
||||||
let variable_manager = get_variable_manager();
|
let variable_manager = get_variable_manager();
|
||||||
let mut variable_manager = write_lock!(variable_manager);
|
let mut variable_manager = write_lock!(variable_manager);
|
||||||
|
|
85
src/main.rs
85
src/main.rs
|
@ -102,10 +102,54 @@ fn start_ironbar() {
|
||||||
cfg_if! {
|
cfg_if! {
|
||||||
if #[cfg(feature = "ipc")] {
|
if #[cfg(feature = "ipc")] {
|
||||||
let ipc = ipc::Ipc::new();
|
let ipc = ipc::Ipc::new();
|
||||||
ipc.start();
|
ipc.start(app);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
load_interface(app);
|
||||||
|
|
||||||
|
let style_path = env::var("IRONBAR_CSS").ok().map_or_else(
|
||||||
|
|| {
|
||||||
|
config_dir().map_or_else(
|
||||||
|
|| {
|
||||||
|
let report = Report::msg("Failed to locate user config dir");
|
||||||
|
error!("{:?}", report);
|
||||||
|
exit(ExitCode::CreateBars as i32);
|
||||||
|
},
|
||||||
|
|dir| dir.join("ironbar").join("style.css"),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
PathBuf::from,
|
||||||
|
);
|
||||||
|
|
||||||
|
if style_path.exists() {
|
||||||
|
load_css(style_path);
|
||||||
|
}
|
||||||
|
|
||||||
|
let (tx, rx) = mpsc::channel();
|
||||||
|
|
||||||
|
spawn_blocking(move || {
|
||||||
|
rx.recv().expect("to receive from channel");
|
||||||
|
|
||||||
|
info!("Shutting down");
|
||||||
|
|
||||||
|
#[cfg(feature = "ipc")]
|
||||||
|
ipc.shutdown();
|
||||||
|
|
||||||
|
exit(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
ctrlc::set_handler(move || tx.send(()).expect("Could not send signal on channel."))
|
||||||
|
.expect("Error setting Ctrl-C handler");
|
||||||
|
});
|
||||||
|
|
||||||
|
// Ignore CLI args
|
||||||
|
// Some are provided by swaybar_config but not currently supported
|
||||||
|
app.run_with_args(&Vec::<&str>::new());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Loads the Ironbar config and interface.
|
||||||
|
pub fn load_interface(app: &Application) {
|
||||||
let display = Display::default().map_or_else(
|
let display = Display::default().map_or_else(
|
||||||
|| {
|
|| {
|
||||||
let report = Report::msg("Failed to get default GTK display");
|
let report = Report::msg("Failed to get default GTK display");
|
||||||
|
@ -146,45 +190,6 @@ fn start_ironbar() {
|
||||||
}
|
}
|
||||||
|
|
||||||
debug!("Created bars");
|
debug!("Created bars");
|
||||||
|
|
||||||
let style_path = env::var("IRONBAR_CSS").ok().map_or_else(
|
|
||||||
|| {
|
|
||||||
config_dir().map_or_else(
|
|
||||||
|| {
|
|
||||||
let report = Report::msg("Failed to locate user config dir");
|
|
||||||
error!("{:?}", report);
|
|
||||||
exit(ExitCode::CreateBars as i32);
|
|
||||||
},
|
|
||||||
|dir| dir.join("ironbar").join("style.css"),
|
|
||||||
)
|
|
||||||
},
|
|
||||||
PathBuf::from,
|
|
||||||
);
|
|
||||||
|
|
||||||
if style_path.exists() {
|
|
||||||
load_css(style_path);
|
|
||||||
}
|
|
||||||
|
|
||||||
let (tx, rx) = mpsc::channel();
|
|
||||||
|
|
||||||
spawn_blocking(move || {
|
|
||||||
rx.recv().expect("to receive from channel");
|
|
||||||
|
|
||||||
info!("Shutting down");
|
|
||||||
|
|
||||||
#[cfg(feature = "ipc")]
|
|
||||||
ipc.shutdown();
|
|
||||||
|
|
||||||
exit(0);
|
|
||||||
});
|
|
||||||
|
|
||||||
ctrlc::set_handler(move || tx.send(()).expect("Could not send signal on channel."))
|
|
||||||
.expect("Error setting Ctrl-C handler");
|
|
||||||
});
|
|
||||||
|
|
||||||
// Ignore CLI args
|
|
||||||
// Some are provided by swaybar_config but not currently supported
|
|
||||||
app.run_with_args(&Vec::<&str>::new());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates each of the bars across each of the (configured) outputs.
|
/// Creates each of the bars across each of the (configured) outputs.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue