Skip to content

Commit

Permalink
suport long lived plugin, and sort top level manually
Browse files Browse the repository at this point in the history
  • Loading branch information
wiiznokes committed Jul 13, 2024
1 parent 1725320 commit fafed2e
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 21 deletions.
49 changes: 49 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ publish = false
pop-launcher-toolkit = { path = "../toolkit" }
tracing.workspace = true
tracing-subscriber = { version = "0.3.18", default-features = false, features = ["std", "fmt", "env-filter", "chrono"] }
tracing-journald = "0.3.0"
dirs.workspace = true
mimalloc = "0.1.39"

Expand Down
25 changes: 14 additions & 11 deletions bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ use pop_launcher_toolkit::plugins;
use pop_launcher_toolkit::service;

use mimalloc::MiMalloc;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::EnvFilter;

#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;
Expand Down Expand Up @@ -41,7 +39,7 @@ async fn main() {

// todo: support journald once this issue is resolved: https://github.com/tokio-rs/tracing/issues/2348
fn init_logging(cmd: &str) {
use tracing_subscriber::{fmt, Registry};
use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};

let logdir = match dirs::state_dir() {
Some(dir) => dir.join("pop-launcher/"),
Expand All @@ -59,7 +57,7 @@ fn init_logging(cmd: &str) {

if let Ok(file) = logfile {
if let Ok(meta) = file.metadata() {
if meta.len() > 1000 {
if meta.len() > 10000 {
let _ = file.set_len(0);
}
}
Expand All @@ -68,13 +66,18 @@ fn init_logging(cmd: &str) {
.or_else(|_| EnvFilter::try_new("warn"))
.unwrap();

let fmt_layer = fmt::layer()
.with_target(false)
.with_timer(fmt::time::ChronoLocal::new("%T".into()))
.with_writer(file);
let fmt_layer = fmt::layer().with_target(false).with_writer(file);

let subscriber = Registry::default().with(filter_layer).with(fmt_layer);

tracing::subscriber::set_global_default(subscriber).expect("Failed to set subscriber");
if let Ok(journal_layer) = tracing_journald::layer() {
tracing_subscriber::registry()
.with(journal_layer)
.with(filter_layer)
.init();
} else {
tracing_subscriber::registry()
.with(fmt_layer)
.with(filter_layer)
.init();
}
}
}
41 changes: 31 additions & 10 deletions plugins/src/cosmic_toplevel/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
mod toplevel_handler;

use cctk::cosmic_protocols::toplevel_info::v1::client::zcosmic_toplevel_handle_v1::State;
use cctk::wayland_client::Proxy;
use cctk::{cosmic_protocols, sctk::reexports::calloop, toplevel_info::ToplevelInfo};
use cosmic_protocols::toplevel_info::v1::client::zcosmic_toplevel_handle_v1::ZcosmicToplevelHandleV1;
use fde::DesktopEntry;
use freedesktop_desktop_entry as fde;
use tracing::{debug, info};

use crate::desktop_entries::utils::get_description;
use crate::send;
Expand All @@ -18,12 +20,13 @@ use pop_launcher::{
Request,
};
use std::borrow::Cow;
use std::collections::VecDeque;
use tokio::io::{AsyncWrite, AsyncWriteExt};

use self::toplevel_handler::{toplevel_handler, ToplevelAction, ToplevelEvent};

pub async fn main() {
tracing::info!("starting cosmic-toplevel");
info!("starting cosmic-toplevel");

let mut tx = async_stdout();

Expand Down Expand Up @@ -67,20 +70,37 @@ pub async fn main() {
Either::Right((Some(event), second_to_next_request)) => {
next_event = toplevel_rx.next();
next_request = second_to_next_request;

match event {
ToplevelEvent::Add(handle, info) => {
tracing::info!("{}", &info.app_id);
tracing::info!("add {}", &info.app_id);
app.toplevels.retain(|t| t.0 != handle);
app.toplevels.push((handle, info));
app.toplevels.push_front((handle, info));
}
ToplevelEvent::Remove(handle) => {
app.toplevels.retain(|t| t.0 != handle);
// ignore requests for this id until after the next search
app.ids_to_ignore.push(handle.id().protocol_id());
if let Some(pos) = app.toplevels.iter().position(|t| t.0 == handle) {
app.toplevels.remove(pos);
// ignore requests for this id until after the next search
app.ids_to_ignore.push(handle.id().protocol_id());
} else {
tracing::warn!("ToplevelEvent::Remove, no toplevel found");
}
}
ToplevelEvent::Update(handle, info) => {
if let Some(t) = app.toplevels.iter_mut().find(|t| t.0 == handle) {
t.1 = info;
debug!("Update {}", &info.app_id);
debug!("Update {:?}", &info.state);

if let Some(pos) = app.toplevels.iter().position(|t| t.0 == handle) {
if info.state.contains(&State::Activated) {
tracing::warn!("Update {:?}: push front", &info.app_id);
app.toplevels.remove(pos);
app.toplevels.push_front((handle, info));
} else {
app.toplevels[pos].1 = info;
}
} else {
tracing::warn!("ToplevelEvent::Update, no toplevel found");
app.toplevels.push_front((handle, info));
}
}
}
Expand All @@ -94,7 +114,8 @@ struct App<W> {
locales: Vec<String>,
desktop_entries: Vec<DesktopEntry<'static>>,
ids_to_ignore: Vec<u32>,
toplevels: Vec<(ZcosmicToplevelHandleV1, ToplevelInfo)>,
// XXX: use LinkedList?
toplevels: VecDeque<(ZcosmicToplevelHandleV1, ToplevelInfo)>,
calloop_tx: calloop::channel::Sender<ToplevelAction>,
tx: W,
}
Expand All @@ -118,7 +139,7 @@ impl<W: AsyncWrite + Unpin> App<W> {
locales,
desktop_entries,
ids_to_ignore: Vec::new(),
toplevels: Vec::new(),
toplevels: VecDeque::new(),
calloop_tx,
tx,
},
Expand Down
1 change: 1 addition & 0 deletions plugins/src/cosmic_toplevel/plugin.ron
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
query: (persistent: true, priority: High),
bin: (path: "cosmic-toplevel"),
icon: Name("focus-windows-symbolic"),
long_lived: true,
)
2 changes: 2 additions & 0 deletions plugins/src/desktop_entries/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ use futures::StreamExt;
use pop_launcher::*;
use std::borrow::Cow;
use tokio::io::AsyncWrite;
use tracing::info;
use utils::get_description;

pub(crate) mod utils;

pub async fn main() {
info!("starting desktop entries");
let mut app = App::new(async_stdout());
app.reload().await;

Expand Down
11 changes: 11 additions & 0 deletions service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,15 @@ impl<O: futures::Sink<Response> + Unpin> Service<O> {

break;
}
Request::Close => {
for (_key, plugin) in self.plugins.iter_mut() {
if !plugin.config.long_lived {
let tx = plugin.sender_exec();
_ = tx.send_async(Request::Exit).await;
plugin.sender_drop();
}
}
}
}
}

Expand Down Expand Up @@ -465,6 +474,8 @@ impl<O: futures::Sink<Response> + Unpin> Service<O> {
}
}
} else {
self.no_sort = query.is_empty();

for plugin_id in query_queue {
if let Some(plugin) = self.plugins.get_mut(plugin_id) {
if plugin
Expand Down
3 changes: 3 additions & 0 deletions service/src/plugins/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ pub struct PluginConfig {

#[serde(default)]
pub history: bool,

#[serde(default)]
pub long_lived: bool,
}

#[derive(Debug, Default, Deserialize, Clone)]
Expand Down
1 change: 1 addition & 0 deletions service/src/plugins/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub const CONFIG: PluginConfig = PluginConfig {
},
icon: Some(IconSource::Name(Cow::Borrowed("system-help-symbolic"))),
history: false,
long_lived: false,
};
pub struct HelpPlugin {
pub id: usize,
Expand Down
3 changes: 3 additions & 0 deletions service/src/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ where
self.exit();
break;
}
Request::Close => {
self.exit();
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ pub enum Request {
Context(Indice),
/// Request to end the service.
Exit,
/// The frontend was closed and the service should release resources
/// and prepare for the next query
Close,
/// Requests to cancel any active searches.
Interrupt,
/// Request to close the selected item.
Expand Down
4 changes: 4 additions & 0 deletions toolkit/src/plugin_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ where
self.exit();
break;
}
Request::Close => {
self.exit();
break;
}
},
Err(why) => tracing::error!("Malformed json request: {why}"),
}
Expand Down

0 comments on commit fafed2e

Please sign in to comment.