Skip to content
Draft
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
27 changes: 27 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ members = [
"cosmic-applet-notifications",
"cosmic-applet-power",
"cosmic-applet-status-area",
"cosmic-applet-status-line",
"cosmic-applet-tiling",
"cosmic-applet-time",
"cosmic-applet-workspaces",
Expand Down
15 changes: 15 additions & 0 deletions cosmic-applet-status-line/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "cosmic-applet-status-line"
version = "0.1.0"
edition = "2021"

[dependencies]
delegate = "0.9"
libcosmic.workspace = true
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tokio = { version = "1.27", features = ["io-util", "process", "sync"] }
tokio-stream = "0.1"
tracing-log.workspace = true
tracing-subscriber.workspace = true
tracing.workspace = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[Desktop Entry]
Name=Cosmic Applet Status Line
Comment=Applet for status lines via i3bar/swaybar protocol
Type=Application
Exec=cosmic-applet-status-line
Terminal=false
Categories=Cosmic;Iced;
Keywords=Cosmic;Iced;
# Translators: Do NOT translate or transliterate this text (this is an icon file name)!
Icon=com.system76.CosmicAppletStatusLine
StartupNotify=true
NoDisplay=true
X-CosmicApplet=true
146 changes: 146 additions & 0 deletions cosmic-applet-status-line/src/bar_widget.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
use cosmic::{
iced::{self, widget, Length, Rectangle},
iced_core::{
clipboard::Clipboard,
event::{self, Event},
layout::{Layout, Limits, Node},
mouse,
renderer::Style,
touch,
widget::{
operation::{Operation, OperationOutputWrapper},
Tree, Widget,
},
Shell,
},
};

use crate::protocol::ClickEvent;

const BTN_LEFT: u32 = 0x110;
const BTN_RIGHT: u32 = 0x111;
const BTN_MIDDLE: u32 = 0x112;

/// Wraps a `Row` widget, handling mouse input
pub struct BarWidget<'a, Msg> {
pub row: widget::Row<'a, Msg, cosmic::Theme, cosmic::Renderer>,
pub name_instance: Vec<(Option<&'a str>, Option<&'a str>)>,
pub on_press: fn(ClickEvent) -> Msg,
}

impl<'a, Msg> Widget<Msg, cosmic::Theme, cosmic::Renderer> for BarWidget<'a, Msg> {
delegate::delegate! {
to self.row {
fn children(&self) -> Vec<Tree>;
fn diff(&mut self, tree: &mut Tree);
fn layout(&self, tree: &mut Tree, renderer: &cosmic::Renderer, limits: &Limits) -> Node;
fn operate(
&self,
tree: &mut Tree,
layout: Layout<'_>,
renderer: &cosmic::Renderer,
operation: &mut dyn Operation<OperationOutputWrapper<Msg>>,
);
fn draw(
&self,
state: &Tree,
renderer: &mut cosmic::Renderer,
theme: &cosmic::Theme,
style: &Style,
layout: Layout,
cursor: iced::mouse::Cursor,
viewport: &Rectangle,
);
}
}

fn on_event(
&mut self,
tree: &mut Tree,
event: Event,
layout: Layout<'_>,
cursor: iced::mouse::Cursor,
renderer: &cosmic::Renderer,
clipboard: &mut dyn Clipboard,
shell: &mut Shell<'_, Msg>,
viewport: &Rectangle,
) -> event::Status {
if self.update(&event, layout, cursor, shell) == event::Status::Captured {
return event::Status::Captured;
}
self.row.on_event(
tree, event, layout, cursor, renderer, clipboard, shell, viewport,
)
}

fn size(&self) -> iced::Size<Length> {
Widget::size(&self.row)
}
}

impl<'a, Msg> From<BarWidget<'a, Msg>> for cosmic::Element<'a, Msg>
where
Msg: 'a,
{
fn from(widget: BarWidget<'a, Msg>) -> cosmic::Element<'a, Msg> {
cosmic::Element::new(widget)
}
}

impl<'a, Msg> BarWidget<'a, Msg> {
fn update(
&mut self,
event: &Event,
layout: Layout<'_>,
cursor: iced::mouse::Cursor,
shell: &mut Shell<'_, Msg>,
) -> event::Status {
let Some(cursor_position) = cursor.position() else {
return event::Status::Ignored;
};

let (button, event_code) = match event {
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) => (1, BTN_LEFT),
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Middle)) => (2, BTN_MIDDLE),
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Right)) => (3, BTN_RIGHT),
Event::Touch(touch::Event::FingerPressed { .. }) => (1, BTN_LEFT),
_ => {
return event::Status::Ignored;
}
};

let Some((n, bounds)) = layout
.children()
.map(|x| x.bounds())
.enumerate()
.find(|(_, bounds)| bounds.contains(cursor_position))
else {
return event::Status::Ignored;
};

let (name, instance) = self.name_instance.get(n).cloned().unwrap_or((None, None));

// TODO coordinate space? int conversion?
let x = cursor_position.x as u32;
let y = cursor_position.y as u32;
let relative_x = (cursor_position.x - bounds.x) as u32;
let relative_y = (cursor_position.y - bounds.y) as u32;
let width = bounds.width as u32;
let height = bounds.height as u32;

shell.publish((self.on_press)(ClickEvent {
name: name.map(str::to_owned),
instance: instance.map(str::to_owned),
x,
y,
button,
event: event_code,
relative_x,
relative_y,
width,
height,
}));

event::Status::Captured
}
}
103 changes: 103 additions & 0 deletions cosmic-applet-status-line/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// TODO: work vertically

use cosmic::{app, iced, iced_style::application, Theme};

mod bar_widget;
use bar_widget::BarWidget;
mod protocol;

#[derive(Clone, Debug)]
enum Msg {
Protocol(protocol::StatusLine),
ClickEvent(protocol::ClickEvent),
}

struct App {
core: app::Core,
status_line: protocol::StatusLine,
}

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

fn init(core: app::Core, _flags: ()) -> (Self, app::Command<Msg>) {
(
App {
core,
status_line: Default::default(),
},
iced::Command::none(),
)
}

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

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

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

fn subscription(&self) -> iced::Subscription<Msg> {
protocol::subscription().map(Msg::Protocol)
}

fn update(&mut self, message: Msg) -> app::Command<Msg> {
match message {
Msg::Protocol(status_line) => {
println!("{:?}", status_line);
self.status_line = status_line;
}
Msg::ClickEvent(click_event) => {
println!("{:?}", click_event);
if self.status_line.click_events {
// TODO: pass click event to backend
}
}
}
iced::Command::none()
}

fn view(&self) -> cosmic::Element<Msg> {
let (block_views, name_instance): (Vec<_>, Vec<_>) = self
.status_line
.blocks
.iter()
.map(|block| {
(
block_view(block),
(block.name.as_deref(), block.instance.as_deref()),
)
})
.unzip();
BarWidget {
row: iced::widget::row(block_views),
name_instance,
on_press: Msg::ClickEvent,
}
.into()
}
}

// TODO seperator
fn block_view(block: &protocol::Block) -> cosmic::Element<Msg> {
let theme = block
.color
.map(cosmic::theme::Text::Color)
.unwrap_or(cosmic::theme::Text::Default);
cosmic::widget::text(&block.full_text)
.size(14)
.style(theme)
.into()
}

pub fn run() -> iced::Result {
cosmic::applet::run::<App>(true, ())
}
10 changes: 10 additions & 0 deletions cosmic-applet-status-line/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 status line applet with version {VERSION}");

cosmic_applet_status_line::run()
}
Loading