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]]
name = "cosmic-panel-config"
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 = [
"anyhow",
"gtk4",

View file

@ -66,13 +66,12 @@ impl AppsContainer {
// Setup
self_.setup_callbacks();
self_.set_position(config.anchor);
Self::setup_callbacks(&self_);
self_
}
pub fn model(&self, type_: DockListType) -> &gio::ListStore {
// Get state
let imp = imp::AppsContainer::from_instance(self);

View file

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

View file

@ -176,7 +176,6 @@ impl DockItem {
Anchor::Bottom => PositionType::Top,
Anchor::Center => unimplemented!(),
});
}
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 position: Rc<Cell<Anchor>>,
pub tx: OnceCell<mpsc::Sender<Event>>,
pub config: OnceCell<CosmicPanelConfig>
pub config: OnceCell<CosmicPanelConfig>,
}
#[glib::object_subclass]

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -10,7 +10,7 @@ use gtk4::{
use once_cell::sync::OnceCell;
use std::sync::{Arc, Mutex};
use tokio::sync::mpsc;
use utils::{Activate, Workspace};
use utils::{Activate, WorkspaceEvent};
use window::CosmicWorkspacesWindow;
mod localize;
@ -56,7 +56,7 @@ fn main() {
app.connect_activate(|app| {
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 window = CosmicWorkspacesWindow::new(app);

View file

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

View file

@ -1,18 +1,17 @@
use crate::utils::{Activate, Workspace};
use std::{
os::unix::{net::UnixStream}, env, path::PathBuf,
};
use wayland_client::{ConnectError, DelegateDispatch, protocol::wl_registry};
use crate::utils::{Activate, WorkspaceEvent};
use std::{env, os::unix::net::UnixStream, path::PathBuf};
use tokio::sync::mpsc;
use wayland_client::{
Connection, Dispatch, QueueHandle,
protocol::{wl_output::WlOutput, wl_registry},
ConnectError,
};
use wayland_client::{Connection, Dispatch, QueueHandle};
/// Generated protocol definitions
mod generated {
#![allow(dead_code,non_camel_case_types,unused_unsafe,unused_variables)]
#![allow(non_upper_case_globals,non_snake_case,unused_imports)]
#![allow(dead_code, non_camel_case_types, unused_unsafe, unused_variables)]
#![allow(non_upper_case_globals, non_snake_case, unused_imports)]
#![allow(missing_docs, clippy::all)]
pub mod client {
@ -32,7 +31,12 @@ mod generated {
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);
if let Ok(Ok(conn)) = std::env::var("HOST_WAYLAND_DISPLAY")
.map_err(anyhow::Error::msg)
@ -47,18 +51,19 @@ 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)))
{
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 display = conn.display();
display.get_registry(&qhandle, ()).unwrap();
let mut state = State {
workspace_manager: None,
workspace_groups: Vec::new(),
tx,
running: true,
};
while state.running {
event_queue.blocking_dispatch(&mut state).unwrap();
}
@ -71,12 +76,24 @@ pub fn spawn_workspaces(tx: mpsc::Sender<Vec<Workspace>>) -> mpsc::Sender<Activa
workspaces_tx
}
struct State {
running: bool,
tx: mpsc::Sender<Vec<Workspace>>,
tx: mpsc::Sender<Vec<WorkspaceEvent>>,
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 {
@ -88,13 +105,26 @@ impl Dispatch<wl_registry::WlRegistry, ()> for State {
_: &Connection,
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);
match &interface[..] {
"zext_workspace_manager_v1" => {
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(
&mut self,
_: &zext_workspace_manager_v1::ZextWorkspaceManagerV1,
_: zext_workspace_manager_v1::Event,
event: zext_workspace_manager_v1::Event,
_: &(),
_: &Connection,
_: &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
}
}
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");
};
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! {
Button::new();
..add_css_class("apps");

View file

@ -44,7 +44,14 @@ fn main() {
localize();
gio::resources_register_include!("compiled.gresource").unwrap();
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| {
if let Ok(Some(id)) = args.lookup::<String>("id") {
ID.set(id).unwrap();