Skip to content

feat(i3status): new applet to embed i3status in panel/dock #344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ members = [
"cosmic-applet-audio",
"cosmic-applet-battery",
"cosmic-applet-bluetooth",
"cosmic-applet-i3status",
"cosmic-applet-minimize",
"cosmic-applet-network",
"cosmic-applet-notifications",
Expand Down
20 changes: 20 additions & 0 deletions cosmic-applet-i3status/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "cosmic-applet-i3status"
version = "0.1.1"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow.workspace = true
i18n-embed-fl.workspace = true
i18n-embed.workspace = true
libcosmic.workspace = true
once_cell = "1"
rust-embed.workspace = true
serde_json = "1"
swaybar-types = "3.0.0"
tokio = { version = "1.36.0", features = ["time", "process"] }
tracing-log.workspace = true
tracing-subscriber.workspace = true
tracing.workspace = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[Desktop Entry]
Name=Cosmic Applet i3status
Comment=Applet for Cosmic Panel
Type=Application
Exec=cosmic-applet-i3status
Terminal=false
Categories=Cosmic;Iced;
Keywords=Cosmic;Iced;
StartupNotify=true
NoDisplay=true
X-CosmicApplet=true
X-HostWaylandDisplay=true
4 changes: 4 additions & 0 deletions cosmic-applet-i3status/i18n.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fallback_language = "en"

[fluent]
assets_dir = "i18n"
Empty file.
116 changes: 116 additions & 0 deletions cosmic-applet-i3status/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
mod localize;
mod subprocess;

use crate::localize::localize;
use cosmic::app::Command;
use cosmic::applet::cosmic_panel_config::PanelAnchor;
use cosmic::iced::Length;
use cosmic::iced_futures::Subscription;
use cosmic::iced_style::application;
use cosmic::iced_widget::{Column, Row};
use cosmic::{Element, Theme};
use subprocess::Output;
use swaybar_types::Block;
use tracing::{span, Level};

pub fn run() -> cosmic::iced::Result {
localize();
cosmic::applet::run::<I3status>(true, ())
}

#[derive(Default)]
struct I3status {
blocks: Vec<Block>,
core: cosmic::app::Core,
text: String,
}

impl cosmic::Application for I3status {
type Message = Output;
type Executor = cosmic::SingleThreadExecutor;
type Flags = ();
const APP_ID: &'static str = "com.system76.CosmicAppletI3status";

fn init(core: cosmic::app::Core, _flags: ()) -> (Self, Command<Output>) {
(
Self {
blocks: vec![],
core,
text: String::new(),
},
Command::none(),
)
}

fn core(&self) -> &cosmic::app::Core {
&self.core
}

fn core_mut(&mut self) -> &mut cosmic::app::Core {
&mut self.core
}

fn style(&self) -> Option<<Theme as application::StyleSheet>::Style> {
Some(cosmic::applet::style())
}

fn update(&mut self, message: Output) -> Command<Output> {
let span = span!(Level::TRACE, "I3status::update()");
let _ = span.enter();
match message {
Output::Blocks(blocks) => {
self.blocks = blocks;
self.text = String::new();
}
Output::Raw(output) => {
self.blocks = vec![];
self.text = output;
}
Output::None => {}
}
Command::none()
}

fn subscription(&self) -> Subscription<Output> {
let span = span!(Level::TRACE, "I3status::subscription()");
let _ = span.enter();
subprocess::child_process()
}

fn view(&self) -> Element<Output> {
let theme = self.core.system_theme().cosmic();
let space_xxs = theme.space_xxs();

let children = if !self.blocks.is_empty() {
self.blocks
.iter()
.map(|block| cosmic::iced_widget::text(&block.full_text).into())
.collect::<Vec<Element<Output>>>()
} else if !self.text.is_empty() {
vec![cosmic::iced_widget::text(&self.text).into()]
} else {
vec![cosmic::iced_widget::text("no output").into()]
};

if matches!(
self.core.applet.anchor,
PanelAnchor::Top | PanelAnchor::Bottom
) {
Row::with_children(children)
.align_items(cosmic::iced_core::Alignment::Center)
.height(Length::Shrink)
.width(Length::Shrink)
.spacing(space_xxs)
.padding([0, space_xxs])
.into()
} else {
Column::with_children(children)
.align_items(cosmic::iced_core::Alignment::Center)
.height(Length::Shrink)
.width(Length::Shrink)
.spacing(space_xxs)
.padding([space_xxs, 0])
.into()
}
}
}
47 changes: 47 additions & 0 deletions cosmic-applet-i3status/src/localize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// SPDX-License-Identifier: MPL-2.0-only

use i18n_embed::{
fluent::{fluent_language_loader, FluentLanguageLoader},
DefaultLocalizer, LanguageLoader, Localizer,
};
use once_cell::sync::Lazy;
use rust_embed::RustEmbed;

#[derive(RustEmbed)]
#[folder = "i18n/"]
struct Localizations;

pub static LANGUAGE_LOADER: Lazy<FluentLanguageLoader> = Lazy::new(|| {
let loader: FluentLanguageLoader = fluent_language_loader!();

loader
.load_fallback_language(&Localizations)
.expect("Error while loading fallback language");

loader
});

#[macro_export]
macro_rules! fl {
($message_id:literal) => {{
i18n_embed_fl::fl!($crate::localize::LANGUAGE_LOADER, $message_id)
}};

($message_id:literal, $($args:expr),*) => {{
i18n_embed_fl::fl!($crate::localize::LANGUAGE_LOADER, $message_id, $($args), *)
}};
}

// Get the `Localizer` to be used for localizing this library.
pub fn localizer() -> Box<dyn Localizer> {
Box::from(DefaultLocalizer::new(&*LANGUAGE_LOADER, &Localizations))
}

pub fn localize() {
let localizer = localizer();
let requested_languages = i18n_embed::DesktopLanguageRequester::requested_languages();

if let Err(error) = localizer.select(&requested_languages) {
eprintln!("Error while loading language for i3status {}", error);
}
}
10 changes: 10 additions & 0 deletions cosmic-applet-i3status/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const VERSION: &str = env!("CARGO_PKG_VERSION");

fn main() -> cosmic::iced::Result {
tracing_subscriber::fmt::init();
let _ = tracing_log::LogTracer::init();

tracing::info!("Starting minimize applet with version {VERSION}");

cosmic_applet_i3status::run()
}
Loading
Loading