event handling & state

This commit is contained in:
Ashley Wulber 2022-06-16 11:55:31 -04:00
parent d991f59c90
commit 3a41e58159
15 changed files with 212 additions and 68 deletions

2
Cargo.lock generated
View file

@ -394,7 +394,7 @@ dependencies = [
[[package]] [[package]]
name = "cosmic-panel-config" name = "cosmic-panel-config"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/pop-os/cosmic-panel#8787823d807ea9a9d7b96ecacf017d695ba7b58a" source = "git+https://github.com/pop-os/cosmic-panel/#8787823d807ea9a9d7b96ecacf017d695ba7b58a"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"gtk4", "gtk4",

View file

@ -67,7 +67,6 @@ impl AppsContainer {
self_.setup_callbacks(); self_.setup_callbacks();
self_.set_position(config.anchor); self_.set_position(config.anchor);
Self::setup_callbacks(&self_); Self::setup_callbacks(&self_);
self_ self_

View file

@ -21,8 +21,8 @@ glib::wrapper! {
impl CosmicAppListWindow { impl CosmicAppListWindow {
pub fn new(app: &gtk4::Application, tx: mpsc::Sender<Event>) -> Self { pub fn new(app: &gtk4::Application, tx: mpsc::Sender<Event>) -> Self {
let self_: Self = Object::new(&[("application", app)]) let self_: Self =
.expect("Failed to create `CosmicAppListWindow`."); Object::new(&[("application", app)]).expect("Failed to create `CosmicAppListWindow`.");
let imp = imp::CosmicAppListWindow::from_instance(&self_); let imp = imp::CosmicAppListWindow::from_instance(&self_);
cascade! { cascade! {

View file

@ -176,7 +176,6 @@ impl DockItem {
Anchor::Bottom => PositionType::Top, Anchor::Bottom => PositionType::Top,
Anchor::Center => unimplemented!(), Anchor::Center => unimplemented!(),
}); });
} }
pub fn add_popover(&self, obj: &DockObject) { pub fn add_popover(&self, obj: &DockObject) {

View file

@ -25,7 +25,7 @@ pub struct DockList {
pub popover_menu_index: Rc<Cell<Option<u32>>>, pub popover_menu_index: Rc<Cell<Option<u32>>>,
pub position: Rc<Cell<Anchor>>, pub position: Rc<Cell<Anchor>>,
pub tx: OnceCell<mpsc::Sender<Event>>, pub tx: OnceCell<mpsc::Sender<Event>>,
pub config: OnceCell<CosmicPanelConfig> pub config: OnceCell<CosmicPanelConfig>,
} }
#[glib::object_subclass] #[glib::object_subclass]

View file

@ -5,7 +5,7 @@ use crate::dock_object::DockObject;
use crate::utils::data_path; use crate::utils::data_path;
use crate::utils::{BoxedWindowList, Event, Item}; use crate::utils::{BoxedWindowList, Event, Item};
use cascade::cascade; use cascade::cascade;
use cosmic_panel_config::config::{CosmicPanelConfig, XdgWrapperConfig, Anchor}; use cosmic_panel_config::config::{Anchor, CosmicPanelConfig, XdgWrapperConfig};
use gio::DesktopAppInfo; use gio::DesktopAppInfo;
use gio::Icon; use gio::Icon;
use glib::Object; use glib::Object;

View file

@ -49,16 +49,19 @@ impl DockObject {
pub fn get_name(&self) -> Option<String> { pub fn get_name(&self) -> Option<String> {
let imp = imp::DockObject::from_instance(self); let imp = imp::DockObject::from_instance(self);
imp.appinfo.borrow().as_ref().map(|app_info| app_info.name().to_string()) imp.appinfo
.borrow()
.as_ref()
.map(|app_info| app_info.name().to_string())
} }
pub fn get_image(&self) -> gtk4::Image { pub fn get_image(&self) -> gtk4::Image {
let imp = imp::DockObject::from_instance(self); let imp = imp::DockObject::from_instance(self);
if let Some(app_info) = imp.appinfo.borrow().as_ref() { if let Some(app_info) = imp.appinfo.borrow().as_ref() {
let image = Image::new(); let image = Image::new();
let icon = app_info let icon = app_info.icon().unwrap_or_else(|| {
.icon() Icon::for_string("image-missing").expect("Failed to set default icon")
.unwrap_or_else(|| Icon::for_string("image-missing").expect("Failed to set default icon")); });
image.set_from_gicon(&icon); image.set_from_gicon(&icon);
image.set_tooltip_text(None); image.set_tooltip_text(None);
image image

View file

@ -126,7 +126,7 @@ fn main() {
let cached_results = cached_results.as_ref().lock().unwrap(); let cached_results = cached_results.as_ref().lock().unwrap();
let stack_active = cached_results.iter().fold( let stack_active = cached_results.iter().fold(
BTreeMap::new(), BTreeMap::new(),
|mut acc: BTreeMap<String, BoxedWindowList>, elem:&Item| { |mut acc: BTreeMap<String, BoxedWindowList>, elem: &Item| {
if let Some(v) = acc.get_mut(&elem.description) { if let Some(v) = acc.get_mut(&elem.description) {
v.0.push(elem.clone()); v.0.push(elem.clone());
} else { } else {
@ -162,11 +162,7 @@ fn main() {
// ); // );
let active = stack_active.remove(i); let active = stack_active.remove(i);
dock_obj.set_property("active", active.to_value()); dock_obj.set_property("active", active.to_value());
saved_app_model.items_changed( saved_app_model.items_changed(saved_i, 0, 0);
saved_i,
0,
0,
);
} else if cached_results } else if cached_results
.iter() .iter()
.any(|s| s.description == cur_app_info.name()) .any(|s| s.description == cur_app_info.name())
@ -175,11 +171,7 @@ fn main() {
"active", "active",
BoxedWindowList(Vec::new()).to_value(), BoxedWindowList(Vec::new()).to_value(),
); );
saved_app_model.items_changed( saved_app_model.items_changed(saved_i, 0, 0);
saved_i,
0,
0,
);
} }
} }
} }
@ -234,11 +226,7 @@ fn main() {
// println!("found active saved app {} at {}", s.0[0].name, i); // println!("found active saved app {} at {}", s.0[0].name, i);
let active = stack_active.remove(i); let active = stack_active.remove(i);
dock_obj.set_property("active", active.to_value()); dock_obj.set_property("active", active.to_value());
saved_app_model.items_changed( saved_app_model.items_changed(saved_i, 0, 0);
saved_i,
0,
0,
);
} else if results } else if results
.iter() .iter()
.any(|s| s.description == cur_app_info.name()) .any(|s| s.description == cur_app_info.name())
@ -247,11 +235,7 @@ fn main() {
"active", "active",
BoxedWindowList(Vec::new()).to_value(), BoxedWindowList(Vec::new()).to_value(),
); );
saved_app_model.items_changed( saved_app_model.items_changed(saved_i, 0, 0);
saved_i,
0,
0,
);
} }
} }
} }

View file

@ -10,6 +10,7 @@ pub mod graphics;
pub mod mode_box; pub mod mode_box;
use self::{dbus::PowerDaemonProxy, graphics::Graphics, mode_box::ModeSelection}; use self::{dbus::PowerDaemonProxy, graphics::Graphics, mode_box::ModeSelection};
use cosmic_panel_config::config::{CosmicPanelConfig, XdgWrapperConfig};
use gtk4::{ use gtk4::{
gdk::Display, gdk::Display,
gio::ApplicationFlags, gio::ApplicationFlags,
@ -20,7 +21,6 @@ use gtk4::{
}; };
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use tokio::runtime::Runtime; use tokio::runtime::Runtime;
use cosmic_panel_config::config::{CosmicPanelConfig, XdgWrapperConfig};
static RT: Lazy<Runtime> = Lazy::new(|| Runtime::new().expect("failed to build tokio runtime")); static RT: Lazy<Runtime> = Lazy::new(|| Runtime::new().expect("failed to build tokio runtime"));
@ -88,7 +88,7 @@ fn build_ui(application: &gtk4::Application) {
image.add_css_class("panel_icon"); image.add_css_class("panel_icon");
image.set_pixel_size(config.get_applet_icon_size().try_into().unwrap()); image.set_pixel_size(config.get_applet_icon_size().try_into().unwrap());
button.set_child(Some(&image)); button.set_child(Some(&image));
let current_graphics = RT let current_graphics = RT
.block_on(get_current_graphics()) .block_on(get_current_graphics())
.expect("failed to connect to system76-power"); .expect("failed to connect to system76-power");
view! { view! {

View file

@ -3,13 +3,13 @@ use std::{env, path::PathBuf, process::Command};
fn main() { fn main() {
if let Some(output) = Command::new("git") if let Some(output) = Command::new("git")
.args(&["rev-parse", "HEAD"]) .args(&["rev-parse", "HEAD"])
.output() .output()
.ok() .ok()
{ {
let git_hash = String::from_utf8(output.stdout).unwrap(); let git_hash = String::from_utf8(output.stdout).unwrap();
println!("cargo:rustc-env=GIT_HASH={}", git_hash); println!("cargo:rustc-env=GIT_HASH={}", git_hash);
} }
gio::compile_resources( gio::compile_resources(
"data/resources", "data/resources",
"data/resources/resources.gresource.xml", "data/resources/resources.gresource.xml",

View file

@ -10,7 +10,7 @@ use gtk4::{
use once_cell::sync::OnceCell; use once_cell::sync::OnceCell;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use tokio::sync::mpsc; use tokio::sync::mpsc;
use utils::{Activate, Workspace}; use utils::{Activate, WorkspaceEvent};
use window::CosmicWorkspacesWindow; use window::CosmicWorkspacesWindow;
mod localize; mod localize;
@ -56,7 +56,7 @@ fn main() {
app.connect_activate(|app| { app.connect_activate(|app| {
load_css(); load_css();
let (tx, mut rx) = mpsc::channel::<Vec<Workspace>>(100); let (tx, mut rx) = mpsc::channel::<Vec<WorkspaceEvent>>(100);
let wayland_tx = wayland::spawn_workspaces(tx.clone()); let wayland_tx = wayland::spawn_workspaces(tx.clone());
let window = CosmicWorkspacesWindow::new(app); let window = CosmicWorkspacesWindow::new(app);

View file

@ -7,7 +7,7 @@ use std::future::Future;
pub type Activate = u32; pub type Activate = u32;
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct Workspace { pub struct WorkspaceEvent {
pub(crate) id: u32, pub(crate) id: u32,
pub(crate) active: bool, pub(crate) active: bool,
} }

View file

@ -1,18 +1,17 @@
use crate::utils::{Activate, Workspace}; use crate::utils::{Activate, WorkspaceEvent};
use std::{ use std::{env, os::unix::net::UnixStream, path::PathBuf};
os::unix::{net::UnixStream}, env, path::PathBuf,
};
use wayland_client::{ConnectError, DelegateDispatch, protocol::wl_registry};
use tokio::sync::mpsc; use tokio::sync::mpsc;
use wayland_client::{ use wayland_client::{
Connection, Dispatch, QueueHandle, protocol::{wl_output::WlOutput, wl_registry},
ConnectError,
}; };
use wayland_client::{Connection, Dispatch, QueueHandle};
/// Generated protocol definitions /// Generated protocol definitions
mod generated { mod generated {
#![allow(dead_code,non_camel_case_types,unused_unsafe,unused_variables)] #![allow(dead_code, non_camel_case_types, unused_unsafe, unused_variables)]
#![allow(non_upper_case_globals,non_snake_case,unused_imports)] #![allow(non_upper_case_globals, non_snake_case, unused_imports)]
#![allow(missing_docs, clippy::all)] #![allow(missing_docs, clippy::all)]
pub mod client { pub mod client {
@ -32,7 +31,12 @@ mod generated {
use generated::client::zext_workspace_manager_v1; use generated::client::zext_workspace_manager_v1;
pub fn spawn_workspaces(tx: mpsc::Sender<Vec<Workspace>>) -> mpsc::Sender<Activate> { use self::generated::client::{
zext_workspace_group_handle_v1::{self, ZextWorkspaceGroupHandleV1},
zext_workspace_handle_v1::{self, ZextWorkspaceHandleV1},
};
pub fn spawn_workspaces(tx: mpsc::Sender<Vec<WorkspaceEvent>>) -> mpsc::Sender<Activate> {
let (workspaces_tx, mut workspaces_rx) = mpsc::channel(100); let (workspaces_tx, mut workspaces_rx) = mpsc::channel(100);
if let Ok(Ok(conn)) = std::env::var("HOST_WAYLAND_DISPLAY") if let Ok(Ok(conn)) = std::env::var("HOST_WAYLAND_DISPLAY")
.map_err(anyhow::Error::msg) .map_err(anyhow::Error::msg)
@ -47,7 +51,7 @@ pub fn spawn_workspaces(tx: mpsc::Sender<Vec<Workspace>>) -> mpsc::Sender<Activa
.and_then(|s| s.map(|s| Connection::from_socket(s).map_err(anyhow::Error::msg))) .and_then(|s| s.map(|s| Connection::from_socket(s).map_err(anyhow::Error::msg)))
{ {
std::thread::spawn(move || { std::thread::spawn(move || {
let mut event_queue= conn.new_event_queue::<State>(); let mut event_queue = conn.new_event_queue::<State>();
let qhandle = event_queue.handle(); let qhandle = event_queue.handle();
let display = conn.display(); let display = conn.display();
@ -55,6 +59,7 @@ pub fn spawn_workspaces(tx: mpsc::Sender<Vec<Workspace>>) -> mpsc::Sender<Activa
let mut state = State { let mut state = State {
workspace_manager: None, workspace_manager: None,
workspace_groups: Vec::new(),
tx, tx,
running: true, running: true,
}; };
@ -71,12 +76,24 @@ pub fn spawn_workspaces(tx: mpsc::Sender<Vec<Workspace>>) -> mpsc::Sender<Activa
workspaces_tx workspaces_tx
} }
struct State { struct State {
running: bool, running: bool,
tx: mpsc::Sender<Vec<Workspace>>, tx: mpsc::Sender<Vec<WorkspaceEvent>>,
workspace_manager: Option<zext_workspace_manager_v1::ZextWorkspaceManagerV1>, workspace_manager: Option<zext_workspace_manager_v1::ZextWorkspaceManagerV1>,
workspace_groups: Vec<WorkspaceGroup>,
}
struct WorkspaceGroup {
workspace_group_handle: ZextWorkspaceGroupHandleV1,
output: Option<WlOutput>,
workspaces: Vec<Workspace>,
}
struct Workspace {
workspace_handle: ZextWorkspaceHandleV1,
name: String,
coordinates: Vec<u8>,
state: Vec<u8>,
} }
impl Dispatch<wl_registry::WlRegistry, ()> for State { impl Dispatch<wl_registry::WlRegistry, ()> for State {
@ -88,13 +105,26 @@ impl Dispatch<wl_registry::WlRegistry, ()> for State {
_: &Connection, _: &Connection,
qh: &QueueHandle<Self>, qh: &QueueHandle<Self>,
) { ) {
if let wl_registry::Event::Global { name, interface, version } = event { if let wl_registry::Event::Global {
name,
interface,
version,
} = event
{
println!("[{}] {} (v{})", name, interface, version); println!("[{}] {} (v{})", name, interface, version);
match &interface[..] { match &interface[..] {
"zext_workspace_manager_v1" => { "zext_workspace_manager_v1" => {
println!("binding to workspace manager"); println!("binding to workspace manager");
registry.bind::<zext_workspace_manager_v1::ZextWorkspaceManagerV1, _, _>(name, 1, qh, ()).unwrap(); let workspace_manager = registry
}, .bind::<zext_workspace_manager_v1::ZextWorkspaceManagerV1, _, _>(
name,
1,
qh,
(),
)
.unwrap();
self.workspace_manager = Some(workspace_manager);
}
_ => {} _ => {}
} }
} }
@ -105,12 +135,132 @@ impl Dispatch<zext_workspace_manager_v1::ZextWorkspaceManagerV1, ()> for State {
fn event( fn event(
&mut self, &mut self,
_: &zext_workspace_manager_v1::ZextWorkspaceManagerV1, _: &zext_workspace_manager_v1::ZextWorkspaceManagerV1,
_: zext_workspace_manager_v1::Event, event: zext_workspace_manager_v1::Event,
_: &(), _: &(),
_: &Connection, _: &Connection,
_: &QueueHandle<Self>, _: &QueueHandle<Self>,
) { ) {
todo!() match event {
zext_workspace_manager_v1::Event::WorkspaceGroup { workspace_group } => {
self.workspace_groups.push(WorkspaceGroup {
workspace_group_handle: workspace_group,
output: None,
workspaces: Vec::new(),
});
}
zext_workspace_manager_v1::Event::Done => {
// TODO
println!("sending event with workspace list state");
let _ = self.tx.send(Vec::new());
}
zext_workspace_manager_v1::Event::Finished => {
self.workspace_manager.take();
}
}
// wl_compositor has no event // wl_compositor has no event
} }
} }
impl Dispatch<ZextWorkspaceGroupHandleV1, ()> for State {
fn event(
&mut self,
group: &ZextWorkspaceGroupHandleV1,
event: zext_workspace_group_handle_v1::Event,
_: &(),
_: &Connection,
_: &QueueHandle<Self>,
) {
match event {
zext_workspace_group_handle_v1::Event::OutputEnter { output } => {
if let Some(group) = self
.workspace_groups
.iter_mut()
.find(|g| &g.workspace_group_handle == group)
{
group.output = Some(output);
}
}
zext_workspace_group_handle_v1::Event::OutputLeave { output } => {
if let Some(group) = self.workspace_groups.iter_mut().find(|g| {
&g.workspace_group_handle == group && g.output.as_ref() == Some(&output)
}) {
group.output = None;
}
}
zext_workspace_group_handle_v1::Event::Workspace { workspace } => {
if let Some(group) = self
.workspace_groups
.iter_mut()
.find(|g| &g.workspace_group_handle == group)
{
group.workspaces.push(Workspace {
workspace_handle: workspace,
name: String::new(),
coordinates: Vec::new(),
state: Vec::new(),
})
}
}
zext_workspace_group_handle_v1::Event::Remove => {
if let Some(group) = self
.workspace_groups
.iter()
.position(|g| &g.workspace_group_handle == group)
{
self.workspace_groups.remove(group);
}
}
}
}
}
impl Dispatch<ZextWorkspaceHandleV1, ()> for State {
fn event(
&mut self,
workspace: &ZextWorkspaceHandleV1,
event: zext_workspace_handle_v1::Event,
_: &(),
_: &Connection,
_: &QueueHandle<Self>,
) {
match event {
zext_workspace_handle_v1::Event::Name { name } => {
if let Some(w) = self.workspace_groups.iter_mut().find_map(|g| {
g.workspaces
.iter_mut()
.find(|w| &w.workspace_handle == workspace)
}) {
w.name = name;
}
}
zext_workspace_handle_v1::Event::Coordinates { coordinates } => {
if let Some(w) = self.workspace_groups.iter_mut().find_map(|g| {
g.workspaces
.iter_mut()
.find(|w| &w.workspace_handle == workspace)
}) {
w.coordinates = coordinates;
}
}
zext_workspace_handle_v1::Event::State { state } => {
if let Some(w) = self.workspace_groups.iter_mut().find_map(|g| {
g.workspaces
.iter_mut()
.find(|w| &w.workspace_handle == workspace)
}) {
w.state = state;
}
}
zext_workspace_handle_v1::Event::Remove => {
if let Some((g, w_i)) = self.workspace_groups.iter_mut().find_map(|g| {
g.workspaces
.iter_mut()
.position(|w| &w.workspace_handle == workspace)
.map(|p| (g, p))
}) {
g.workspaces.remove(w_i);
}
}
}
}
}

View file

@ -34,7 +34,9 @@ impl CosmicPanelAppButtonWindow {
..add_css_class("root_window"); ..add_css_class("root_window");
}; };
if let Some(apps_desktop_info) = DesktopAppInfo::new(&format!("{}.desktop", app_desktop_file_name)) { if let Some(apps_desktop_info) =
DesktopAppInfo::new(&format!("{}.desktop", app_desktop_file_name))
{
let app_button = cascade! { let app_button = cascade! {
Button::new(); Button::new();
..add_css_class("apps"); ..add_css_class("apps");

View file

@ -44,7 +44,14 @@ fn main() {
localize(); localize();
gio::resources_register_include!("compiled.gresource").unwrap(); gio::resources_register_include!("compiled.gresource").unwrap();
let app = gtk4::Application::new(None, ApplicationFlags::default()); let app = gtk4::Application::new(None, ApplicationFlags::default());
app.add_main_option("id", glib::Char::from(b'i'), glib::OptionFlags::NONE, glib::OptionArg::String, "id of the launched application", None); app.add_main_option(
"id",
glib::Char::from(b'i'),
glib::OptionFlags::NONE,
glib::OptionArg::String,
"id of the launched application",
None,
);
app.connect_handle_local_options(|_app, args| { app.connect_handle_local_options(|_app, args| {
if let Ok(Some(id)) = args.lookup::<String>("id") { if let Ok(Some(id)) = args.lookup::<String>("id") {
ID.set(id).unwrap(); ID.set(id).unwrap();