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
1 change: 1 addition & 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 @@ -34,6 +34,7 @@ secret-service = { version = "5.1.0", features = [
], optional = true }
thiserror = { version = "2.0", optional = true }
secstr = { version = "0.5", optional = true }
raw-window-handle = "0.6.2"

[dependencies.cosmic-files]
git = "https://github.com/pop-os/cosmic-files.git"
Expand Down
55 changes: 55 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use cosmic::widget::menu::key_bind::KeyBind;
use cosmic::{
Application, ApplicationExt, Element, action,
app::{Core, Settings, Task, context_drawer},
cctk::wayland_client::{Connection, Proxy, backend::ObjectId, protocol::wl_surface::WlSurface},
cosmic_config::{self, ConfigSet, CosmicConfigEntry},
cosmic_theme, executor,
iced::{
Expand All @@ -27,6 +28,7 @@ use cosmic::{Apply, surface};
use cosmic_files::dialog::{Dialog, DialogKind, DialogMessage, DialogResult, DialogSettings};
use cosmic_text::{Family, Stretch, Weight, fontdb::FaceInfo};
use localize::LANGUAGE_SORTER;
use raw_window_handle::RawWindowHandle;
use std::{
any::TypeId,
cmp,
Expand Down Expand Up @@ -71,6 +73,7 @@ mod password_manager;
mod terminal_theme;

mod dnd;
mod wayland;

use clap_lex::RawArgs;

Expand Down Expand Up @@ -418,6 +421,9 @@ pub enum Message {
ZoomIn,
ZoomOut,
ZoomReset,
WaylandConn(Connection),
Bell(wayland::Bell),
Ignore,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -474,6 +480,8 @@ pub struct App {
modifiers: Modifiers,
#[cfg(feature = "password_manager")]
password_mgr: password_manager::PasswordManager,
wayland_conn: Option<Connection>,
bell: Option<wayland::Bell>,
}

impl App {
Expand Down Expand Up @@ -1592,6 +1600,8 @@ impl Application for App {
modifiers: Modifiers::empty(),
#[cfg(feature = "password_manager")]
password_mgr: Default::default(),
wayland_conn: None,
bell: None,
};

app.set_curr_font_weights_and_stretches();
Expand Down Expand Up @@ -2524,6 +2534,9 @@ impl Application for App {
match event {
TermEvent::Bell => {
//TODO: audible or visible bell options?
if let Some(bell) = &self.bell {
bell.ring();
}
}
TermEvent::ClipboardLoad(kind, callback) => {
match kind {
Expand Down Expand Up @@ -2746,6 +2759,39 @@ impl Application for App {
cosmic::app::Action::Surface(a),
));
}
Message::WaylandConn(conn) => {
if self.wayland_conn.is_none() {
self.wayland_conn = Some(conn.clone());
return cosmic::iced_runtime::window::run_with_handle(
self.core.main_window_id().unwrap(),
move |id| {
cosmic::action::app(
if let RawWindowHandle::Wayland(wayland_handle) = id.as_raw() {
//registry_queue_init(&conn);
let surface_id = unsafe {
ObjectId::from_ptr(
WlSurface::interface(),
wayland_handle.surface.as_ptr().cast(),
)
}
.unwrap(); // XXX unwrap
let surface = WlSurface::from_id(&conn, surface_id).unwrap();
match wayland::bind_bell(&conn, &surface) {
Some(bell) => Message::Bell(bell),
None => Message::Ignore,
}
} else {
Message::Ignore
},
)
},
);
}
}
Message::Bell(bell) => {
self.bell = Some(bell);
}
Message::Ignore => {}
}

Task::none()
Expand Down Expand Up @@ -2984,6 +3030,15 @@ impl Application for App {
Event::Mouse(MouseEvent::ButtonReleased(MouseButton::Left)) => {
Some(Message::CopyPrimary(None))
}
iced::Event::PlatformSpecific(iced::event::PlatformSpecific::Wayland(
event::wayland::Event::Output(_, output),
)) => {
if let Some(backend) = output.backend().upgrade() {
Some(Message::WaylandConn(Connection::from_backend(backend)))
} else {
None
}
}
_ => None,
}),
Subscription::run_with_id(
Expand Down
53 changes: 53 additions & 0 deletions src/wayland.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use cosmic::cctk::{
wayland_client::{
Connection, Dispatch, QueueHandle, delegate_noop,
globals::{GlobalListContents, registry_queue_init},
protocol::{wl_registry, wl_surface::WlSurface},
},
wayland_protocols::xdg::system_bell::v1::client::xdg_system_bell_v1::XdgSystemBellV1,
};

#[derive(Clone, Debug)]
pub struct Bell {
bell: XdgSystemBellV1,
surface: WlSurface,
conn: Connection,
}

impl Bell {
pub fn ring(&self) {
self.bell.ring(Some(&self.surface));
let _ = self.conn.flush();
}
}

struct State {}

pub fn bind_bell(conn: &Connection, surface: &WlSurface) -> Option<Bell> {
// XXX unwrap
let (globals, event_queue) = registry_queue_init::<State>(&conn).unwrap();
let qh = event_queue.handle();
match globals.bind(&qh, 1..=1, ()) {
Ok(bell) => Some(Bell {
bell,
surface: surface.clone(),
conn: conn.clone(),
}),
Err(_) => None,
}
}

impl Dispatch<wl_registry::WlRegistry, GlobalListContents> for State {
fn event(
_: &mut State,
_: &wl_registry::WlRegistry,
_: wl_registry::Event,
_: &GlobalListContents,
_: &Connection,
_: &QueueHandle<State>,
) {
// Ignore globals added after initialization
}
}

delegate_noop!(State: XdgSystemBellV1);