1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-07-03 19:51:03 +02:00

refactor: update gtk/glib, remove glib channels

This is a major refactor which updates GTK, GLib and GTK Layer Shell to their latest versions.

GLib channels, previously used for receiving events on the GLib Main Context thread have been deprecated and a new method for running Futures on the main thread has been added instead. This commit also replaces all the deprecated code with this.

As part of the above, a bug was uncovered related to creating the GLib main context inside the Tokio runtime. Spawning of Tokio tasks has been refactored to fix this.
This commit is contained in:
Jake Stanger 2023-12-17 23:51:43 +00:00
parent 2c2f1c1243
commit bea442ed96
No known key found for this signature in database
GPG key ID: C51FC8F9CB0BEA61
39 changed files with 426 additions and 465 deletions

View file

@ -7,9 +7,9 @@ use std::path::PathBuf;
use std::process::exit;
use std::rc::Rc;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::mpsc;
#[cfg(feature = "ipc")]
use std::sync::{Arc, RwLock};
use std::sync::RwLock;
use std::sync::{mpsc, Arc};
use cfg_if::cfg_if;
#[cfg(feature = "cli")]
@ -21,8 +21,8 @@ use glib::PropertySet;
use gtk::gdk::Display;
use gtk::prelude::*;
use gtk::Application;
use tokio::runtime::Handle;
use tokio::task::{block_in_place, spawn_blocking};
use tokio::runtime::{Handle, Runtime};
use tokio::task::{block_in_place, JoinHandle};
use tracing::{debug, error, info, warn};
use universal_config::ConfigLoader;
@ -36,7 +36,6 @@ use crate::ironvar::VariableManager;
use crate::style::load_css;
mod bar;
mod bridge_channel;
#[cfg(feature = "cli")]
mod cli;
mod clients;
@ -60,13 +59,12 @@ mod style;
const GTK_APP_ID: &str = "dev.jstanger.ironbar";
const VERSION: &str = env!("CARGO_PKG_VERSION");
#[tokio::main]
async fn main() {
fn main() {
let _guard = logging::install_logging();
cfg_if! {
if #[cfg(feature = "cli")] {
run_with_args().await;
run_with_args();
} else {
start_ironbar();
}
@ -74,16 +72,19 @@ async fn main() {
}
#[cfg(feature = "cli")]
async fn run_with_args() {
fn run_with_args() {
let args = cli::Args::parse();
match args.command {
Some(command) => {
let ipc = ipc::Ipc::new();
match ipc.send(command).await {
Ok(res) => cli::handle_response(res),
Err(err) => error!("{err:?}"),
};
let rt = create_runtime();
rt.block_on(async move {
let ipc = ipc::Ipc::new();
match ipc.send(command).await {
Ok(res) => cli::handle_response(res),
Err(err) => error!("{err:?}"),
};
});
}
None => start_ironbar(),
}
@ -91,6 +92,10 @@ async fn run_with_args() {
static COUNTER: AtomicUsize = AtomicUsize::new(1);
lazy_static::lazy_static! {
static ref RUNTIME: Arc<Runtime> = Arc::new(create_runtime());
}
#[cfg(feature = "ipc")]
lazy_static::lazy_static! {
static ref VARIABLE_MANAGER: Arc<RwLock<VariableManager>> = arc_rw!(VariableManager::new());
@ -184,6 +189,12 @@ impl Ironbar {
app.run_with_args(&Vec::<&str>::new());
}
/// Gets the current Tokio runtime.
#[must_use]
pub fn runtime() -> Arc<Runtime> {
RUNTIME.clone()
}
/// Gets a `usize` ID value that is unique to the entire Ironbar instance.
/// This is just a static `AtomicUsize` that increments every time this function is called.
pub fn unique_id() -> usize {
@ -323,6 +334,31 @@ fn create_bars(app: &Application, display: &Display, config: &Config) -> Result<
Ok(all_bars)
}
fn create_runtime() -> Runtime {
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.expect("tokio to create a valid runtime")
}
/// Calls `spawn` on the Tokio runtime.
pub fn spawn<F>(f: F) -> JoinHandle<F::Output>
where
F: Future + Send + 'static,
F::Output: Send + 'static,
{
Ironbar::runtime().spawn(f)
}
/// Calls `spawn_blocking` on the Tokio runtime.
pub fn spawn_blocking<F, R>(f: F) -> JoinHandle<R>
where
F: FnOnce() -> R + Send + 'static,
R: Send + 'static,
{
Ironbar::runtime().spawn_blocking(f)
}
/// Blocks on a `Future` until it resolves.
///
/// This is not an `async` operation