#plugin #pop-launcher #toolkit #front-end #user #service #pop-launcher-plugins

onagre-launcher-toolkit

A wrapper around pop-launcher, pop-launcher-service and pop-launcher-plugins types for writing plugins and frontends for pop-launcher

2 releases

0.1.2 Feb 14, 2024
0.1.1 Jan 22, 2024

#41 in #frontend

Download history 67/week @ 2024-02-22 42/week @ 2024-02-29 29/week @ 2024-03-07 48/week @ 2024-03-14 17/week @ 2024-03-21 50/week @ 2024-03-28 25/week @ 2024-04-04 27/week @ 2024-04-11 37/week @ 2024-04-18 19/week @ 2024-04-25 20/week @ 2024-05-02 24/week @ 2024-05-09 33/week @ 2024-05-16 36/week @ 2024-05-23 28/week @ 2024-05-30 20/week @ 2024-06-06

121 downloads per month

MPL-2.0 and GPL-3.0-only

22KB
88 lines

onagre-launcher-toolkit

A toolkit to write pop-launcher client and plugin.

Crates

  • launcher: re-export the pop-launcher crate, containing all the IPC message struct and some utility functions to locate plugins.
  • service: re-export the pop-launcher-service crate, containing deserializable plugin config struct. This is useful if your client needs to read user defined plugins configs.
  • plugins: re-export pop-launcher-plugins which defines all the default pop-launcher plugins. Useful if your client needs to read default plugin configs

Writing a plugin

Add the following to your Cargo.toml :

[dependencies]
tokio = { version = "1.18.2", features = ["rt"] }
onagre-launcher-toolkit = { git = "https://github.com/pop-os/launcher" }

And implement the PluginExt trait:

use pop_launcher_toolkit::launcher::{Indice, PluginResponse, PluginSearchResult};
use pop_launcher_toolkit::plugin_trait::{async_trait, PluginExt};
use pop_launcher_toolkit::plugins;

// The plugin struct, here it holds the search result
pub struct MyPlugin {
  data: Vec<String>
}

#[async_trait]
impl PluginExt for MyPlugin {

  // Define the name of you plugin, this will be used
  // to generate a logfile in $XDG_STATE_HOME at runtime.
  fn name(&self) -> &str {
      "my_awesome_plugin"
  }

  // Respond to `pop-launcher` 'search' query
  async fn search(&mut self, query: &str) {
     // `pop-launcher` dispatches request to plugins according to the regex defined in
     // the `plugin.ron` config file, here we get rid of the prefix
     // before processing the request.
     let query = query.strip_prefix("plug ").unwrap();

     // Iterate through our internal search results with their indices.
     let search_results = self.data.iter()
        .enumerate()
        .filter(|(idx, data)| data.contains(query));

     // Send our search results to `pop-launcher` using their indices as id.
     for (idx, search_result) in search_results {
        self.respond_with(PluginResponse::Append(PluginSearchResult {
            id: idx as u32,
            name: search_result.clone(),
            description: "".to_string(),
            keywords: None,
            icon: None,
            exec: None,
            window: None,
        })).await;
     }

    // tell `pop-launcher` we are done with this request
    self.respond_with(PluginResponse::Finished).await;
  }

  // Respond to `pop-launcher` 'activate' query
  async fn activate(&mut self, id: Indice) {
      // Get the selected entry
      let entry = self.data.get(id as usize).unwrap();
      // Here we use xdg_open to run the entry but this could be anything
      plugins::xdg_open(entry);
      // Tell pop launcher we are done
      self.respond_with(PluginResponse::Finished).await;
  }

  // Respond to `pop-launcher` 'close' request.
  async fn quit(&mut self, id: Indice) {
      self.respond_with(PluginResponse::Close).await;
  }
}

#[tokio::main(flavor = "current_thread")]
pub async fn main() {

    // Here we declare our plugin with dummy values, and never mutate them.
    // In a real plugin we would probably use some kind of mutable shared reference to
    // update our search results.
    let mut plugin = MyPlugin {
        data: vec!["https://crates.io".to_string(), "https://en.wikipedia.org".to_string()],
    };

    /// If you need to debug your plugin or display error messages use `tcracing` macros.
    tracing::info!("Starting my_awsome_plugin");

    // Call the plugin entry point function to start
    // talking with pop_launcherc
    plugin.run().await;
}

Dependencies

~29–47MB
~633K SLoC