merge workspaces_dev
This commit is contained in:
commit
74f6c2eca6
22 changed files with 1275 additions and 155 deletions
|
|
@ -0,0 +1,306 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<protocol name="ext_workspace_unstable_v1">
|
||||
<copyright>
|
||||
Copyright © 2019 Christopher Billington
|
||||
Copyright © 2020 Ilia Bozhinov
|
||||
|
||||
Permission to use, copy, modify, distribute, and sell this
|
||||
software and its documentation for any purpose is hereby granted
|
||||
without fee, provided that the above copyright notice appear in
|
||||
all copies and that both that copyright notice and this permission
|
||||
notice appear in supporting documentation, and that the name of
|
||||
the copyright holders not be used in advertising or publicity
|
||||
pertaining to distribution of the software without specific,
|
||||
written prior permission. The copyright holders make no
|
||||
representations about the suitability of this software for any
|
||||
purpose. It is provided "as is" without express or implied
|
||||
warranty.
|
||||
|
||||
THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
|
||||
SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
|
||||
AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
|
||||
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
|
||||
THIS SOFTWARE.
|
||||
</copyright>
|
||||
|
||||
<interface name="zext_workspace_manager_v1" version="1">
|
||||
<description summary="list and control workspaces">
|
||||
Workspaces, also called virtual desktops, are groups of surfaces. A
|
||||
compositor with a concept of workspaces may only show some such groups of
|
||||
surfaces (those of 'active' workspaces) at a time. 'Activating' a
|
||||
workspace is a request for the compositor to display that workspace's
|
||||
surfaces as normal, whereas the compositor may hide or otherwise
|
||||
de-emphasise surfaces that are associated only with 'inactive' workspaces.
|
||||
Workspaces are grouped by which sets of outputs they correspond to, and
|
||||
may contain surfaces only from those outputs. In this way, it is possible
|
||||
for each output to have its own set of workspaces, or for all outputs (or
|
||||
any other arbitrary grouping) to share workspaces. Compositors may
|
||||
optionally conceptually arrange each group of workspaces in an
|
||||
N-dimensional grid.
|
||||
|
||||
The purpose of this protocol is to enable the creation of taskbars and
|
||||
docks by providing them with a list of workspaces and their properties,
|
||||
and allowing them to activate and deactivate workspaces.
|
||||
|
||||
After a client binds the zext_workspace_manager_v1, each workspace will be
|
||||
sent via the workspace event.
|
||||
</description>
|
||||
|
||||
<event name="workspace_group">
|
||||
<description summary="a workspace group has been created">
|
||||
This event is emitted whenever a new workspace group has been created.
|
||||
|
||||
All initial details of the workspace group (workspaces, outputs) will be
|
||||
sent immediately after this event via the corresponding events in
|
||||
zext_workspace_group_handle_v1.
|
||||
</description>
|
||||
<arg name="workspace_group" type="new_id" interface="zext_workspace_group_handle_v1"/>
|
||||
</event>
|
||||
|
||||
<request name="commit">
|
||||
<description summary="all requests about the workspaces have been sent">
|
||||
The client must send this request after it has finished sending other
|
||||
requests. The compositor must process a series of requests preceding a
|
||||
commit request atomically.
|
||||
|
||||
This allows changes to the workspace properties to be seen as atomic,
|
||||
even if they happen via multiple events, and even if they involve
|
||||
multiple zext_workspace_handle_v1 objects, for example, deactivating one
|
||||
workspace and activating another.
|
||||
</description>
|
||||
</request>
|
||||
|
||||
<event name="done">
|
||||
<description summary="all information about the workspace groups has been sent">
|
||||
This event is sent after all changes in all workspace groups have been
|
||||
sent.
|
||||
|
||||
This allows changes to one or more zext_workspace_group_handle_v1
|
||||
properties to be seen as atomic, even if they happen via multiple
|
||||
events. In particular, an output moving from one workspace group to
|
||||
another sends an output_enter event and an output_leave event to the two
|
||||
zext_workspace_group_handle_v1 objects in question. The compositor sends
|
||||
the done event only after updating the output information in both
|
||||
workspace groups.
|
||||
</description>
|
||||
</event>
|
||||
|
||||
<event name="finished">
|
||||
<description summary="the compositor has finished with the workspace_manager">
|
||||
This event indicates that the compositor is done sending events to the
|
||||
zext_workspace_manager_v1. The server will destroy the object
|
||||
immediately after sending this request, so it will become invalid and
|
||||
the client should free any resources associated with it.
|
||||
</description>
|
||||
</event>
|
||||
|
||||
<request name="stop">
|
||||
<description summary="stop sending events">
|
||||
Indicates the client no longer wishes to receive events for new
|
||||
workspace groups. However the compositor may emit further workspace
|
||||
events, until the finished event is emitted.
|
||||
|
||||
The client must not send any more requests after this one.
|
||||
</description>
|
||||
</request>
|
||||
</interface>
|
||||
|
||||
<interface name="zext_workspace_group_handle_v1" version="1">
|
||||
<description summary="a workspace group assigned to a set of outputs">
|
||||
A zext_workspace_group_handle_v1 object represents a a workspace group
|
||||
that is assigned a set of outputs and contains a number of workspaces.
|
||||
|
||||
The set of outputs assigned to the workspace group is conveyed to the client via
|
||||
output_enter and output_leave events, and its workspaces are conveyed with
|
||||
workspace events.
|
||||
|
||||
For example, a compositor which has a set of workspaces for each output may
|
||||
advertise a workspace group (and its workspaces) per output, whereas a compositor
|
||||
where a workspace spans all outputs may advertise a single workspace group for all
|
||||
outputs.
|
||||
</description>
|
||||
|
||||
<event name="output_enter">
|
||||
<description summary="output assigned to workspace group">
|
||||
This event is emitted whenever an output is assigned to the workspace
|
||||
group.
|
||||
</description>
|
||||
<arg name="output" type="object" interface="wl_output"/>
|
||||
</event>
|
||||
|
||||
<event name="output_leave">
|
||||
<description summary="output removed from workspace group">
|
||||
This event is emitted whenever an output is removed from the workspace
|
||||
group.
|
||||
</description>
|
||||
<arg name="output" type="object" interface="wl_output"/>
|
||||
</event>
|
||||
|
||||
<event name="workspace">
|
||||
<description summary="workspace added to workspace group">
|
||||
This event is emitted whenever a new workspace has been created.
|
||||
|
||||
All initial details of the workspace (name, coordinates, state) will
|
||||
be sent immediately after this event via the corresponding events in
|
||||
zext_workspace_handle_v1.
|
||||
</description>
|
||||
<arg name="workspace" type="new_id" interface="zext_workspace_handle_v1"/>
|
||||
</event>
|
||||
|
||||
<event name="remove">
|
||||
<description summary="this workspace group has been destroyed">
|
||||
This event means the zext_workspace_group_handle_v1 has been destroyed.
|
||||
It is guaranteed there won't be any more events for this
|
||||
zext_workspace_group_handle_v1. The zext_workspace_group_handle_v1 becomes
|
||||
inert so any requests will be ignored except the destroy request.
|
||||
|
||||
The compositor must remove all workspaces belonging to a workspace group
|
||||
before removing the workspace group.
|
||||
</description>
|
||||
</event>
|
||||
|
||||
<request name="create_workspace">
|
||||
<description summary="create a new workspace">
|
||||
Request that the compositor create a new workspace with the given name.
|
||||
|
||||
There is no guarantee that the compositor will create a new workspace,
|
||||
or that the created workspace will have the provided name.
|
||||
</description>
|
||||
<arg name="workspace" type="string"/>
|
||||
</request>
|
||||
|
||||
<request name="destroy" type="destructor">
|
||||
<description summary="destroy the zext_workspace_handle_v1 object">
|
||||
Destroys the zext_workspace_handle_v1 object.
|
||||
|
||||
This request should be called either when the client does not want to
|
||||
use the workspace object any more or after the remove event to finalize
|
||||
the destruction of the object.
|
||||
</description>
|
||||
</request>
|
||||
</interface>
|
||||
|
||||
<interface name="zext_workspace_handle_v1" version="1">
|
||||
<description summary="a workspace handing a group of surfaces">
|
||||
A zext_workspace_handle_v1 object represents a a workspace that handles a
|
||||
group of surfaces.
|
||||
|
||||
Each workspace has a name, conveyed to the client with the name event; a
|
||||
list of states, conveyed to the client with the state event; and
|
||||
optionally a set of coordinates, conveyed to the client with the
|
||||
coordinates event. The client may request that the compositor activate or
|
||||
deactivate the workspace.
|
||||
|
||||
Each workspace can belong to only a single workspace group.
|
||||
Depepending on the compositor policy, there might be workspaces with
|
||||
the same name in different workspace groups, but these workspaces are still
|
||||
separate (e.g. one of them might be active while the other is not).
|
||||
</description>
|
||||
|
||||
<event name="name">
|
||||
<description summary="workspace name changed">
|
||||
This event is emitted immediately after the zext_workspace_handle_v1 is
|
||||
created and whenever the name of the workspace changes.
|
||||
</description>
|
||||
<arg name="name" type="string"/>
|
||||
</event>
|
||||
|
||||
<event name="coordinates">
|
||||
<description summary="workspace coordinates changed">
|
||||
This event is used to organize workspaces into an N-dimensional grid
|
||||
within a workspace group, and if supported, is emitted immediately after
|
||||
the zext_workspace_handle_v1 is created and whenever the coordinates of
|
||||
the workspace change. Compositors may not send this event if they do not
|
||||
conceptually arrange workspaces in this way. If compositors simply
|
||||
number workspaces, without any geometric interpretation, they may send
|
||||
1D coordinates, which clients should not interpret as implying any
|
||||
geometry. Sending an empty array means that the compositor no longer
|
||||
orders the workspace geometrically.
|
||||
|
||||
Coordinates have an arbitrary number of dimensions N with an uint32
|
||||
position along each dimension. By convention if N > 1, the first
|
||||
dimension is X, the second Y, the third Z, and so on. The compositor may
|
||||
chose to utilize these events for a more novel workspace layout
|
||||
convention, however. No guarantee is made about the grid being filled or
|
||||
bounded; there may be a workspace at coordinate 1 and another at
|
||||
coordinate 1000 and none in between. Within a workspace group, however,
|
||||
workspaces must have unique coordinates of equal dimensionality.
|
||||
</description>
|
||||
<arg name="coordinates" type="array"/>
|
||||
</event>
|
||||
|
||||
<event name="state">
|
||||
<description summary="the state of the workspace changed">
|
||||
This event is emitted immediately after the zext_workspace_handle_v1 is
|
||||
created and each time the workspace state changes, either because of a
|
||||
compositor action or because of a request in this protocol.
|
||||
</description>
|
||||
<arg name="state" type="array"/>
|
||||
</event>
|
||||
|
||||
<enum name="state">
|
||||
<description summary="types of states on the workspace">
|
||||
The different states that a workspace can have.
|
||||
</description>
|
||||
|
||||
<entry name="active" value="0" summary="the workspace is active"/>
|
||||
<entry name="urgent" value="1" summary="the workspace requests attention"/>
|
||||
<entry name="hidden" value="2">
|
||||
<description summary="the workspace is not visible">
|
||||
The workspace is not visible in its workspace group, and clients
|
||||
attempting to visualize the compositor workspace state should not
|
||||
display such workspaces.
|
||||
</description>
|
||||
</entry>
|
||||
</enum>
|
||||
|
||||
<event name="remove">
|
||||
<description summary="this workspace has been destroyed">
|
||||
This event means the zext_workspace_handle_v1 has been destroyed. It is
|
||||
guaranteed there won't be any more events for this
|
||||
zext_workspace_handle_v1. The zext_workspace_handle_v1 becomes inert so
|
||||
any requests will be ignored except the destroy request.
|
||||
</description>
|
||||
</event>
|
||||
|
||||
<request name="destroy" type="destructor">
|
||||
<description summary="destroy the zext_workspace_handle_v1 object">
|
||||
Destroys the zext_workspace_handle_v1 object.
|
||||
|
||||
This request should be called either when the client does not want to
|
||||
use the workspace object any more or after the remove event to finalize
|
||||
the destruction of the object.
|
||||
</description>
|
||||
</request>
|
||||
|
||||
<request name="activate">
|
||||
<description summary="activate the workspace">
|
||||
Request that this workspace be activated.
|
||||
|
||||
There is no guarantee the workspace will be actually activated, and
|
||||
behaviour may be compositor-dependent. For example, activating a
|
||||
workspace may or may not deactivate all other workspaces in the same
|
||||
group.
|
||||
</description>
|
||||
</request>
|
||||
|
||||
<request name="deactivate">
|
||||
<description summary="activate the workspace">
|
||||
Request that this workspace be deactivated.
|
||||
|
||||
There is no guarantee the workspace will be actually deactivated.
|
||||
</description>
|
||||
</request>
|
||||
|
||||
<request name="remove">
|
||||
<description summary="remove the workspace">
|
||||
Request that this workspace be removed.
|
||||
|
||||
There is no guarantee the workspace will be actually removed.
|
||||
</description>
|
||||
</request>
|
||||
</interface>
|
||||
</protocol>
|
||||
|
|
@ -3,26 +3,29 @@
|
|||
use gtk4::{
|
||||
gdk::Display,
|
||||
gio::{self, ApplicationFlags},
|
||||
glib,
|
||||
glib::{self, MainContext, Priority},
|
||||
prelude::*,
|
||||
CssProvider, StyleContext,
|
||||
};
|
||||
use once_cell::sync::OnceCell;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use tokio::sync::mpsc;
|
||||
use utils::{Activate, Workspace};
|
||||
use utils::{Activate, WorkspaceEvent};
|
||||
use wayland::State;
|
||||
use window::CosmicWorkspacesWindow;
|
||||
use calloop::channel::SyncSender;
|
||||
|
||||
mod localize;
|
||||
mod utils;
|
||||
mod wayland;
|
||||
mod wayland_source;
|
||||
mod window;
|
||||
mod workspace_button;
|
||||
mod workspace_list;
|
||||
mod workspace_object;
|
||||
|
||||
const ID: &str = "com.system76.CosmicAppletWorkspaces";
|
||||
static TX: OnceCell<mpsc::Sender<Activate>> = OnceCell::new();
|
||||
static TX: OnceCell<SyncSender<WorkspaceEvent>> = OnceCell::new();
|
||||
|
||||
pub fn localize() {
|
||||
let localizer = crate::localize::localizer();
|
||||
|
|
@ -56,18 +59,18 @@ fn main() {
|
|||
|
||||
app.connect_activate(|app| {
|
||||
load_css();
|
||||
let (tx, mut rx) = mpsc::channel::<Vec<Workspace>>(100);
|
||||
let (tx, rx) = MainContext::channel(Priority::default());
|
||||
|
||||
let wayland_tx = wayland::spawn_workspaces(tx.clone());
|
||||
let window = CosmicWorkspacesWindow::new(app);
|
||||
|
||||
TX.set(wayland_tx).unwrap();
|
||||
|
||||
let _ = glib::MainContext::default().spawn_local(async move {
|
||||
while let Some(workspace_list) = rx.recv().await {
|
||||
// TODO update the model with the new workspace list
|
||||
}
|
||||
});
|
||||
rx.attach(None, glib::clone!(@weak window => @default-return glib::prelude::Continue(true), move |workspace_event| {
|
||||
window.set_workspaces(workspace_event);
|
||||
glib::prelude::Continue(true)
|
||||
}));
|
||||
|
||||
window.show();
|
||||
});
|
||||
app.run();
|
||||
|
|
|
|||
|
|
@ -5,11 +5,12 @@ use std::path::PathBuf;
|
|||
use gtk4::glib;
|
||||
use std::future::Future;
|
||||
|
||||
pub type Activate = u32;
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Workspace {
|
||||
pub(crate) id: u32,
|
||||
pub(crate) active: bool,
|
||||
pub type Activate = String;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum WorkspaceEvent {
|
||||
Activate(String),
|
||||
Scroll(f64),
|
||||
}
|
||||
|
||||
pub fn data_path() -> PathBuf {
|
||||
|
|
|
|||
|
|
@ -1,52 +1,400 @@
|
|||
use crate::utils::{Activate, Workspace};
|
||||
use crate::{
|
||||
utils::{Activate, WorkspaceEvent},
|
||||
wayland::generated::client::zext_workspace_manager_v1::ZextWorkspaceManagerV1,
|
||||
wayland_source::WaylandSource,
|
||||
};
|
||||
use cosmic_panel_config::config::CosmicPanelConfig;
|
||||
use gtk4::glib;
|
||||
use std::{
|
||||
num::ParseIntError,
|
||||
os::unix::prelude::RawFd,
|
||||
sync::{Arc, Mutex},
|
||||
collections::HashMap, env, hash::Hash, mem, os::unix::net::UnixStream, path::PathBuf,
|
||||
sync::Arc, time::Duration,
|
||||
};
|
||||
use tokio::sync::mpsc;
|
||||
use wayland_client::{protocol::wl_registry, Display, GlobalManager};
|
||||
use wayland_backend::client::ObjectData;
|
||||
use wayland_client::{
|
||||
event_created_child,
|
||||
protocol::{
|
||||
wl_output::{self, WlOutput},
|
||||
wl_registry,
|
||||
},
|
||||
ConnectError, Proxy,
|
||||
};
|
||||
|
||||
use wayland_client::{Connection, Dispatch, QueueHandle};
|
||||
|
||||
/// Generated protocol definitions
|
||||
mod generated {
|
||||
// The generated code tends to trigger a lot of warnings
|
||||
// so we isolate it into a very permissive module
|
||||
#![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 {
|
||||
// These imports are used by the generated code
|
||||
pub(crate) use wayland_commons::map::{Object, ObjectMetadata};
|
||||
pub(crate) use wayland_commons::smallvec;
|
||||
pub(crate) use wayland_commons::wire::{Argument, ArgumentType, Message, MessageDesc};
|
||||
pub(crate) use wayland_commons::{Interface, MessageGroup};
|
||||
pub(crate) use wayland_client::protocol::wl_output;
|
||||
pub(crate) use wayland_client::sys;
|
||||
pub(crate) use wayland_client::{AnonymousObject, Main, Proxy, ProxyMap};
|
||||
include!(concat!(env!("OUT_DIR"), "/ext_workspace.rs"));
|
||||
//! Client-side API of this protocol
|
||||
use wayland_client;
|
||||
use wayland_client::protocol::*;
|
||||
|
||||
pub mod __interfaces {
|
||||
use wayland_client::protocol::__interfaces::*;
|
||||
wayland_scanner::generate_interfaces!("src/ext-workspace-unstable-v1.xml");
|
||||
}
|
||||
use self::__interfaces::*;
|
||||
|
||||
wayland_scanner::generate_client_code!("src/ext-workspace-unstable-v1.xml");
|
||||
}
|
||||
}
|
||||
|
||||
pub fn spawn_workspaces(tx: mpsc::Sender<Vec<Workspace>>) -> mpsc::Sender<Activate> {
|
||||
let (workspaces_tx, mut workspaces_rx) = mpsc::channel(100);
|
||||
if let Ok(display) = std::env::var("HOST_WAYLAND_DISPLAY")
|
||||
use generated::client::zext_workspace_manager_v1;
|
||||
|
||||
use self::generated::client::{
|
||||
zext_workspace_group_handle_v1::{self, ZextWorkspaceGroupHandleV1},
|
||||
zext_workspace_handle_v1::{self, ZextWorkspaceHandleV1},
|
||||
};
|
||||
use calloop::channel::*;
|
||||
|
||||
pub fn spawn_workspaces(tx: glib::Sender<State>) -> SyncSender<WorkspaceEvent> {
|
||||
let (workspaces_tx, mut workspaces_rx) = calloop::channel::sync_channel(100);
|
||||
|
||||
if let Ok(Ok(conn)) = std::env::var("HOST_WAYLAND_DISPLAY")
|
||||
.map_err(anyhow::Error::msg)
|
||||
.and_then(|fd| Display::connect_to_name(fd).map_err(anyhow::Error::msg))
|
||||
.map(|display_str| {
|
||||
let mut socket_path = env::var_os("XDG_RUNTIME_DIR")
|
||||
.map(Into::<PathBuf>::into)
|
||||
.ok_or(ConnectError::NoCompositor)?;
|
||||
socket_path.push(display_str);
|
||||
|
||||
Ok(UnixStream::connect(socket_path).map_err(|_| ConnectError::NoCompositor)?)
|
||||
})
|
||||
.and_then(|s| s.map(|s| Connection::from_socket(s).map_err(anyhow::Error::msg)))
|
||||
{
|
||||
std::thread::spawn(move || {
|
||||
let mut event_queue = display.create_event_queue();
|
||||
let attached_display = display.attach(event_queue.token());
|
||||
let globals = GlobalManager::new(&attached_display);
|
||||
dbg!(event_queue.sync_roundtrip(&mut (), |_, _, _| unreachable!()));
|
||||
let output = CosmicPanelConfig::load_from_env()
|
||||
.unwrap_or_default()
|
||||
.output;
|
||||
let mut event_loop = calloop::EventLoop::<State>::try_new().unwrap();
|
||||
let loop_handle = event_loop.handle();
|
||||
let event_queue = conn.new_event_queue::<State>();
|
||||
let qhandle = event_queue.handle();
|
||||
|
||||
println!("Globals: ");
|
||||
for (name, interface, version) in globals.list() {
|
||||
println!("{}: {} (version {})", name, interface, version);
|
||||
WaylandSource::new(event_queue)
|
||||
.expect("Failed to create wayland source")
|
||||
.insert(loop_handle)
|
||||
.unwrap();
|
||||
|
||||
let display = conn.display();
|
||||
display.get_registry(&qhandle, ()).unwrap();
|
||||
|
||||
let mut state = State {
|
||||
workspace_manager: None,
|
||||
workspace_groups: Vec::new(),
|
||||
configured_output: output,
|
||||
expected_output: None,
|
||||
tx,
|
||||
running: true,
|
||||
};
|
||||
let loop_handle = event_loop.handle();
|
||||
loop_handle.insert_source(workspaces_rx, |e, _, state| {
|
||||
match e {
|
||||
Event::Msg(WorkspaceEvent::Activate(id)) => {
|
||||
if let Some(w) = state
|
||||
.workspace_groups
|
||||
.iter()
|
||||
.find_map(|g| g.workspaces.iter().find(|w| w.name == id))
|
||||
{
|
||||
w.workspace_handle.activate();
|
||||
state.workspace_manager.as_ref().unwrap().commit();
|
||||
}
|
||||
}
|
||||
Event::Msg(WorkspaceEvent::Scroll(v)) => {
|
||||
if let Some((w_g, w_i)) = state
|
||||
.workspace_groups
|
||||
.iter()
|
||||
.enumerate()
|
||||
.find_map(|(g_i, g)| {
|
||||
g.workspaces
|
||||
.iter()
|
||||
.position(|w| w.state == 0)
|
||||
.map(|w_i| (g, w_i))
|
||||
})
|
||||
{
|
||||
let max_w = w_g.workspaces.len().wrapping_sub(1);
|
||||
let d_i = if v > 0.0 {
|
||||
if w_i == max_w {
|
||||
0
|
||||
} else {
|
||||
w_i.wrapping_add(1)
|
||||
}
|
||||
} else {
|
||||
if w_i == 0 {
|
||||
max_w
|
||||
} else {
|
||||
w_i.wrapping_sub(1)
|
||||
}
|
||||
};
|
||||
if let Some(w) = w_g.workspaces.get(d_i) {
|
||||
w.workspace_handle.activate();
|
||||
state.workspace_manager.as_ref().unwrap().commit();
|
||||
}
|
||||
}
|
||||
}
|
||||
Event::Closed => if let Some(workspace_manager) = &mut state.workspace_manager {
|
||||
for g in &mut state.workspace_groups {
|
||||
g.workspace_group_handle.destroy();
|
||||
}
|
||||
workspace_manager.stop();
|
||||
},
|
||||
}
|
||||
|
||||
}).unwrap();
|
||||
while state.running {
|
||||
event_loop
|
||||
.dispatch(Duration::from_millis(16), &mut state)
|
||||
.unwrap();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
eprintln!("ENV variable HOST_WAYLAND_SOCKET is missing. Exiting...");
|
||||
eprintln!("ENV variable HOST_WAYLAND_DISPLAY is missing. Exiting...");
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
workspaces_tx
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct State {
|
||||
running: bool,
|
||||
tx: glib::Sender<State>,
|
||||
configured_output: String,
|
||||
expected_output: Option<WlOutput>,
|
||||
workspace_manager: Option<zext_workspace_manager_v1::ZextWorkspaceManagerV1>,
|
||||
workspace_groups: Vec<WorkspaceGroup>,
|
||||
}
|
||||
|
||||
impl State {
|
||||
// XXX
|
||||
pub fn workspace_list(&self) -> impl Iterator<Item = (String, u32)> + '_ {
|
||||
self.workspace_groups
|
||||
.iter()
|
||||
.filter_map(|g| {
|
||||
if g.output == self.expected_output {
|
||||
Some(g.workspaces.iter().map(|w| (w.name.clone(), w.state)))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.flatten()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct WorkspaceGroup {
|
||||
workspace_group_handle: ZextWorkspaceGroupHandleV1,
|
||||
output: Option<WlOutput>,
|
||||
workspaces: Vec<Workspace>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct Workspace {
|
||||
workspace_handle: ZextWorkspaceHandleV1,
|
||||
name: String,
|
||||
coordinates: Vec<u8>,
|
||||
state: u32,
|
||||
}
|
||||
|
||||
impl Dispatch<wl_registry::WlRegistry, ()> for State {
|
||||
fn event(
|
||||
&mut self,
|
||||
registry: &wl_registry::WlRegistry,
|
||||
event: wl_registry::Event,
|
||||
_: &(),
|
||||
_: &Connection,
|
||||
qh: &QueueHandle<Self>,
|
||||
) {
|
||||
if let wl_registry::Event::Global {
|
||||
name,
|
||||
interface,
|
||||
version,
|
||||
} = event
|
||||
{
|
||||
match &interface[..] {
|
||||
"zext_workspace_manager_v1" => {
|
||||
let workspace_manager = registry
|
||||
.bind::<zext_workspace_manager_v1::ZextWorkspaceManagerV1, _, _>(
|
||||
name,
|
||||
1,
|
||||
qh,
|
||||
(),
|
||||
)
|
||||
.unwrap();
|
||||
self.workspace_manager = Some(workspace_manager);
|
||||
}
|
||||
"wl_output" => {
|
||||
registry.bind::<WlOutput, _, _>(name, 1, qh, ()).unwrap();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Dispatch<zext_workspace_manager_v1::ZextWorkspaceManagerV1, ()> for State {
|
||||
fn event(
|
||||
&mut self,
|
||||
_: &zext_workspace_manager_v1::ZextWorkspaceManagerV1,
|
||||
event: zext_workspace_manager_v1::Event,
|
||||
_: &(),
|
||||
_: &Connection,
|
||||
_: &QueueHandle<Self>,
|
||||
) {
|
||||
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 => {
|
||||
let _ = self.tx.send(self.clone());
|
||||
}
|
||||
zext_workspace_manager_v1::Event::Finished => {
|
||||
self.workspace_manager.take();
|
||||
}
|
||||
}
|
||||
// wl_compositor has no event
|
||||
}
|
||||
|
||||
event_created_child!(State, ZextWorkspaceManagerV1, [
|
||||
0 => (ZextWorkspaceGroupHandleV1, ())
|
||||
]);
|
||||
}
|
||||
|
||||
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: 4,
|
||||
})
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
event_created_child!(State, ZextWorkspaceGroupHandleV1, [
|
||||
2 => (ZextWorkspaceHandleV1, ())
|
||||
]);
|
||||
}
|
||||
|
||||
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)
|
||||
}) {
|
||||
if state.len() == 4 {
|
||||
// XXX is it little endian??
|
||||
w.state = u32::from_le_bytes(state.try_into().unwrap());
|
||||
} else {
|
||||
w.state = 3;
|
||||
}
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Dispatch<WlOutput, ()> for State {
|
||||
fn event(
|
||||
&mut self,
|
||||
o: &WlOutput,
|
||||
e: wl_output::Event,
|
||||
_: &(),
|
||||
_: &Connection,
|
||||
_: &QueueHandle<Self>,
|
||||
) {
|
||||
match e {
|
||||
wl_output::Event::Name { name } if name == self.configured_output => {
|
||||
self.expected_output.replace(o.clone());
|
||||
}
|
||||
_ => {} // ignored
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
219
applets/cosmic-applet-workspaces/src/wayland_source.rs
Normal file
219
applets/cosmic-applet-workspaces/src/wayland_source.rs
Normal file
|
|
@ -0,0 +1,219 @@
|
|||
//! Utilities for using an [`EventQueue`] from wayland-client with an event loop that performs polling with
|
||||
//! [`calloop`](https://crates.io/crates/calloop).
|
||||
|
||||
use std::{io, os::unix::prelude::RawFd};
|
||||
|
||||
use calloop::{
|
||||
generic::Generic, EventSource, InsertError, Interest, LoopHandle, Mode, Poll, PostAction,
|
||||
Readiness, RegistrationToken, Token, TokenFactory,
|
||||
};
|
||||
use nix::errno::Errno;
|
||||
use wayland_backend::client::{ReadEventsGuard, WaylandError};
|
||||
use wayland_client::{DispatchError, EventQueue};
|
||||
|
||||
/// An adapter to insert an [`EventQueue`] into a calloop [`EventLoop`](calloop::EventLoop).
|
||||
///
|
||||
/// This type implements [`EventSource`] which generates an event whenever events on the display need to be
|
||||
/// dispatched. The event queue available in the callback calloop registers may be used to dispatch pending
|
||||
/// events using [`EventQueue::dispatch_pending`].
|
||||
///
|
||||
/// [`WaylandSource::insert`] can be used to insert this source into an event loop and automatically dispatch
|
||||
/// pending events on the display.
|
||||
#[derive(Debug)]
|
||||
pub struct WaylandSource<D> {
|
||||
queue: EventQueue<D>,
|
||||
fd: Generic<RawFd>,
|
||||
read_guard: Option<ReadEventsGuard>,
|
||||
}
|
||||
|
||||
impl<D> WaylandSource<D> {
|
||||
/// Wrap an [`EventQueue`] as a [`WaylandSource`].
|
||||
pub fn new(queue: EventQueue<D>) -> Result<WaylandSource<D>, WaylandError> {
|
||||
let guard = queue.prepare_read()?;
|
||||
let fd = Generic::new(guard.connection_fd(), Interest::READ, Mode::Level);
|
||||
drop(guard);
|
||||
|
||||
Ok(WaylandSource {
|
||||
queue,
|
||||
fd,
|
||||
read_guard: None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Access the underlying event queue
|
||||
///
|
||||
/// Note that you should be careful when interacting with it if you invoke methods that
|
||||
/// interact with the wayland socket (such as `dispatch()` or `prepare_read()`). These may
|
||||
/// interfere with the proper waking up of this event source in the event loop.
|
||||
pub fn queue(&mut self) -> &mut EventQueue<D> {
|
||||
&mut self.queue
|
||||
}
|
||||
|
||||
/// Insert this source into the given event loop.
|
||||
///
|
||||
/// This adapter will pass the event loop's shared data as the `D` type for the event loop.
|
||||
pub fn insert(self, handle: LoopHandle<D>) -> Result<RegistrationToken, InsertError<Self>>
|
||||
where
|
||||
D: 'static,
|
||||
{
|
||||
handle.insert_source(self, |_, queue, data| queue.dispatch_pending(data))
|
||||
}
|
||||
}
|
||||
|
||||
impl<D> EventSource for WaylandSource<D> {
|
||||
type Event = ();
|
||||
|
||||
/// The underlying event queue.
|
||||
///
|
||||
/// You should call [`EventQueue::dispatch_pending`] inside your callback using this queue.
|
||||
type Metadata = EventQueue<D>;
|
||||
type Ret = Result<usize, DispatchError>;
|
||||
type Error = calloop::Error;
|
||||
|
||||
fn process_events<F>(
|
||||
&mut self,
|
||||
readiness: Readiness,
|
||||
token: Token,
|
||||
mut callback: F,
|
||||
) -> Result<PostAction, Self::Error>
|
||||
where
|
||||
F: FnMut(Self::Event, &mut Self::Metadata) -> Self::Ret,
|
||||
{
|
||||
let queue = &mut self.queue;
|
||||
let read_guard = &mut self.read_guard;
|
||||
|
||||
let action = self.fd.process_events(readiness, token, |_, _| {
|
||||
// 1. read events from the socket if any are available
|
||||
if let Some(guard) = read_guard.take() {
|
||||
// might be None if some other thread read events before us, concurrently
|
||||
if let Err(WaylandError::Io(err)) = guard.read() {
|
||||
if err.kind() != io::ErrorKind::WouldBlock {
|
||||
return Err(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2. dispatch any pending events in the queue
|
||||
// This is done to ensure we are not waiting for messages that are already in the buffer.
|
||||
Self::loop_callback_pending(queue, &mut callback)?;
|
||||
*read_guard = Some(Self::prepare_read(queue)?);
|
||||
|
||||
// 3. Once dispatching is finished, flush the responses to the compositor
|
||||
if let Err(WaylandError::Io(e)) = queue.flush() {
|
||||
if e.kind() != io::ErrorKind::WouldBlock {
|
||||
// in case of error, forward it and fast-exit
|
||||
return Err(e);
|
||||
}
|
||||
// WouldBlock error means the compositor could not process all our messages
|
||||
// quickly. Either it is slowed down or we are a spammer.
|
||||
// Should not really happen, if it does we do nothing and will flush again later
|
||||
}
|
||||
|
||||
Ok(PostAction::Continue)
|
||||
})?;
|
||||
|
||||
Ok(action)
|
||||
}
|
||||
|
||||
fn register(
|
||||
&mut self,
|
||||
poll: &mut Poll,
|
||||
token_factory: &mut TokenFactory,
|
||||
) -> calloop::Result<()> {
|
||||
self.fd.register(poll, token_factory)
|
||||
}
|
||||
|
||||
fn reregister(
|
||||
&mut self,
|
||||
poll: &mut Poll,
|
||||
token_factory: &mut TokenFactory,
|
||||
) -> calloop::Result<()> {
|
||||
self.fd.reregister(poll, token_factory)
|
||||
}
|
||||
|
||||
fn unregister(&mut self, poll: &mut Poll) -> calloop::Result<()> {
|
||||
self.fd.unregister(poll)
|
||||
}
|
||||
|
||||
fn pre_run<F>(&mut self, mut callback: F) -> calloop::Result<()>
|
||||
where
|
||||
F: FnMut((), &mut Self::Metadata) -> Self::Ret,
|
||||
{
|
||||
debug_assert!(self.read_guard.is_none());
|
||||
|
||||
// flush the display before starting to poll
|
||||
if let Err(WaylandError::Io(err)) = self.queue.flush() {
|
||||
if err.kind() != io::ErrorKind::WouldBlock {
|
||||
// in case of error, don't prepare a read, if the error is persistent, it'll trigger in other
|
||||
// wayland methods anyway
|
||||
log::error!("Error trying to flush the wayland display: {}", err);
|
||||
return Err(err.into());
|
||||
}
|
||||
}
|
||||
|
||||
// ensure we are not waiting for messages that are already in the buffer.
|
||||
Self::loop_callback_pending(&mut self.queue, &mut callback)?;
|
||||
self.read_guard = Some(Self::prepare_read(&mut self.queue)?);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn post_run<F>(&mut self, _: F) -> calloop::Result<()>
|
||||
where
|
||||
F: FnMut((), &mut Self::Metadata) -> Self::Ret,
|
||||
{
|
||||
// Drop implementation of ReadEventsGuard will do cleanup
|
||||
self.read_guard.take();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<D> WaylandSource<D> {
|
||||
/// Loop over the callback until all pending messages have been dispatched.
|
||||
fn loop_callback_pending<F>(queue: &mut EventQueue<D>, callback: &mut F) -> io::Result<()>
|
||||
where
|
||||
F: FnMut((), &mut EventQueue<D>) -> Result<usize, DispatchError>,
|
||||
{
|
||||
// Loop on the callback until no pending events are left.
|
||||
loop {
|
||||
match callback((), queue) {
|
||||
// No more pending events.
|
||||
Ok(0) => break Ok(()),
|
||||
|
||||
Ok(_) => continue,
|
||||
|
||||
Err(DispatchError::Backend(WaylandError::Io(err))) => {
|
||||
return Err(err);
|
||||
}
|
||||
|
||||
Err(DispatchError::Backend(WaylandError::Protocol(err))) => {
|
||||
log::error!("Protocol error received on display: {}", err);
|
||||
|
||||
break Err(Errno::EPROTO.into());
|
||||
}
|
||||
|
||||
Err(DispatchError::BadMessage { msg, interface }) => {
|
||||
log::error!(
|
||||
"Bad message on interface \"{}\": (opcode: {}, args: {:?})",
|
||||
interface,
|
||||
msg.opcode,
|
||||
msg.args,
|
||||
);
|
||||
|
||||
break Err(Errno::EPROTO.into());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn prepare_read(queue: &mut EventQueue<D>) -> io::Result<ReadEventsGuard> {
|
||||
queue.prepare_read().map_err(|err| match err {
|
||||
WaylandError::Io(err) => err,
|
||||
|
||||
WaylandError::Protocol(err) => {
|
||||
log::error!("Protocol error received on display: {}", err);
|
||||
Errno::EPROTO.into()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
// SPDX-License-Identifier: MPL-2.0-only
|
||||
|
||||
use crate::{fl, utils::Activate, workspace_list::WorkspaceList};
|
||||
use crate::{fl, utils::Activate, wayland::State, workspace_list::WorkspaceList};
|
||||
use cascade::cascade;
|
||||
use cosmic_panel_config::config::CosmicPanelConfig;
|
||||
use gtk4::{
|
||||
|
|
@ -9,7 +9,6 @@ use gtk4::{
|
|||
prelude::*,
|
||||
subclass::prelude::*,
|
||||
};
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
mod imp;
|
||||
|
||||
|
|
@ -32,7 +31,7 @@ impl CosmicWorkspacesWindow {
|
|||
..set_height_request(1);
|
||||
..set_decorated(false);
|
||||
..set_resizable(false);
|
||||
..set_title(Some(&fl!("cosmic-app-list")));
|
||||
..set_title(Some(&fl!("cosmic-applet-workspaces")));
|
||||
..add_css_class("transparent");
|
||||
};
|
||||
let config = CosmicPanelConfig::load_from_env().unwrap_or_default();
|
||||
|
|
@ -43,4 +42,9 @@ impl CosmicWorkspacesWindow {
|
|||
|
||||
self_
|
||||
}
|
||||
|
||||
pub fn set_workspaces(&self, workspaces: State) {
|
||||
let imp = imp::CosmicWorkspacesWindow::from_instance(&self);
|
||||
imp.inner.get().unwrap().set_workspaces(workspaces);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
mod imp;
|
||||
|
||||
use crate::{workspace_object::WorkspaceObject, Activate, TX};
|
||||
use crate::{utils::WorkspaceEvent, workspace_object::WorkspaceObject, Activate, TX};
|
||||
use glib::Object;
|
||||
use gtk4::{glib, prelude::*, subclass::prelude::*, ToggleButton};
|
||||
|
||||
|
|
@ -28,12 +28,25 @@ impl WorkspaceButton {
|
|||
let old_button = imp.button.take();
|
||||
self.remove(&old_button);
|
||||
|
||||
let is_active = obj.active() == 0;
|
||||
let id = obj.id();
|
||||
let new_button = ToggleButton::with_label(&format!("{}", id));
|
||||
new_button.set_active(obj.active());
|
||||
let new_button = ToggleButton::with_label(&id);
|
||||
new_button.set_sensitive(!is_active);
|
||||
if obj.active() == 0 {
|
||||
new_button.add_css_class("active");
|
||||
} else if obj.active() == 1 {
|
||||
new_button.add_css_class("alert");
|
||||
} else {
|
||||
new_button.add_css_class("inactive");
|
||||
}
|
||||
self.append(&new_button);
|
||||
new_button.connect_clicked(move |_| {
|
||||
let _ = TX.get().unwrap().send(id);
|
||||
let id_clone = id.clone();
|
||||
if !is_active {
|
||||
let _ = TX.get()
|
||||
.unwrap()
|
||||
.send(WorkspaceEvent::Activate(id_clone));
|
||||
}
|
||||
});
|
||||
|
||||
imp.button.replace(new_button);
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
use cosmic_panel_config::config::CosmicPanelConfig;
|
||||
use gtk4::subclass::prelude::*;
|
||||
use gtk4::{gio, glib};
|
||||
use gtk4::{gio, glib, EventControllerScroll};
|
||||
use gtk4::{Box, ListView};
|
||||
use once_cell::sync::OnceCell;
|
||||
use tokio::sync::mpsc;
|
||||
|
|
|
|||
|
|
@ -1,10 +1,16 @@
|
|||
// SPDX-License-Identifier: MPL-2.0-only
|
||||
|
||||
use crate::utils::Activate;
|
||||
use crate::utils::WorkspaceEvent;
|
||||
use crate::wayland::State;
|
||||
use crate::workspace_button::WorkspaceButton;
|
||||
use crate::workspace_object::WorkspaceObject;
|
||||
use crate::TX;
|
||||
use cascade::cascade;
|
||||
use cosmic_panel_config::config::CosmicPanelConfig;
|
||||
use gtk4::builders::EventControllerScrollBuilder;
|
||||
use gtk4::EventControllerScrollFlags;
|
||||
use gtk4::Inhibit;
|
||||
use gtk4::ListView;
|
||||
use gtk4::Orientation;
|
||||
use gtk4::SignalListItemFactory;
|
||||
|
|
@ -39,15 +45,52 @@ impl WorkspaceList {
|
|||
|
||||
fn layout(&self) {
|
||||
let imp = imp::WorkspaceList::from_instance(self);
|
||||
let anchor = imp.config.get().unwrap().anchor;
|
||||
|
||||
let list_view = cascade! {
|
||||
ListView::default();
|
||||
..set_orientation(Orientation::Horizontal);
|
||||
..set_orientation(anchor.into());
|
||||
..add_css_class("transparent");
|
||||
};
|
||||
self.append(&list_view);
|
||||
|
||||
let flags = EventControllerScrollFlags::BOTH_AXES;
|
||||
|
||||
let scroll_controller = EventControllerScrollBuilder::new()
|
||||
.flags(flags.union(EventControllerScrollFlags::DISCRETE))
|
||||
.build();
|
||||
|
||||
scroll_controller.connect_scroll(|_, dx, dy| {
|
||||
let _ = TX.get()
|
||||
.unwrap()
|
||||
.send(WorkspaceEvent::Scroll(dx + dy));
|
||||
Inhibit::default()
|
||||
});
|
||||
|
||||
list_view.add_controller(&scroll_controller);
|
||||
imp.list_view.set(list_view).unwrap();
|
||||
}
|
||||
|
||||
pub fn set_workspaces(&self, workspaces: State) {
|
||||
let imp = imp::WorkspaceList::from_instance(&self);
|
||||
let model = imp.model.get().unwrap();
|
||||
|
||||
let model_len = model.n_items();
|
||||
let new_results: Vec<glib::Object> = workspaces
|
||||
.workspace_list()
|
||||
.into_iter()
|
||||
.filter_map(|w| {
|
||||
// don't include hidden workspaces
|
||||
if w.1 != 2 {
|
||||
Some(WorkspaceObject::from_id_active(w.0, w.1).upcast())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
model.splice(0, model_len, &new_results[..]);
|
||||
}
|
||||
|
||||
fn setup_model(&self) {
|
||||
let imp = imp::WorkspaceList::from_instance(self);
|
||||
let model = gio::ListStore::new(WorkspaceObject::static_type());
|
||||
|
|
@ -74,7 +117,7 @@ impl WorkspaceList {
|
|||
.item()
|
||||
.expect("The item has to exist.")
|
||||
.downcast::<WorkspaceObject>()
|
||||
.expect("The item has to be a `DockObject`");
|
||||
.expect("The item has to be a `WorkspaceObject`");
|
||||
let workspace_button = list_item
|
||||
.child()
|
||||
.expect("The list item child needs to exist.")
|
||||
|
|
|
|||
|
|
@ -1,11 +1,10 @@
|
|||
// SPDX-License-Identifier: MPL-2.0-only
|
||||
|
||||
use std::cell::Cell;
|
||||
use std::cell::{Cell, RefCell};
|
||||
|
||||
use glib::{ParamFlags, ParamSpec, Value};
|
||||
use gtk4::gdk::glib::ParamSpecBoolean;
|
||||
use gtk4::glib;
|
||||
use gtk4::glib::ParamSpecUInt;
|
||||
use gtk4::glib::{self, ParamSpecString, ParamSpecUInt};
|
||||
use gtk4::prelude::*;
|
||||
use gtk4::subclass::prelude::*;
|
||||
use once_cell::sync::Lazy;
|
||||
|
|
@ -13,8 +12,8 @@ use once_cell::sync::Lazy;
|
|||
// Object holding the state
|
||||
#[derive(Default)]
|
||||
pub struct WorkspaceObject {
|
||||
pub(crate) id: Cell<u32>,
|
||||
pub(crate) active: Cell<bool>,
|
||||
pub(crate) id: RefCell<String>,
|
||||
pub(crate) active: Cell<u32>,
|
||||
}
|
||||
|
||||
// The central trait for subclassing a GObject
|
||||
|
|
@ -30,27 +29,25 @@ impl ObjectImpl for WorkspaceObject {
|
|||
fn properties() -> &'static [ParamSpec] {
|
||||
static PROPERTIES: Lazy<Vec<ParamSpec>> = Lazy::new(|| {
|
||||
vec![
|
||||
ParamSpecUInt::new(
|
||||
ParamSpecString::new(
|
||||
// Name
|
||||
"id",
|
||||
// Nickname
|
||||
"id",
|
||||
// Short description
|
||||
"id",
|
||||
// Minimum value
|
||||
u32::MIN,
|
||||
// Maximum value
|
||||
u32::MAX,
|
||||
// Default value
|
||||
0,
|
||||
None,
|
||||
// The property can be read and written to
|
||||
ParamFlags::READWRITE,
|
||||
),
|
||||
ParamSpecBoolean::new(
|
||||
ParamSpecUInt::new(
|
||||
"active",
|
||||
"active",
|
||||
"Indicates whether workspace is active",
|
||||
false,
|
||||
0,
|
||||
4,
|
||||
0,
|
||||
ParamFlags::READWRITE,
|
||||
),
|
||||
]
|
||||
|
|
@ -74,7 +71,7 @@ impl ObjectImpl for WorkspaceObject {
|
|||
|
||||
fn property(&self, _obj: &Self::Type, _id: usize, pspec: &ParamSpec) -> Value {
|
||||
match pspec.name() {
|
||||
"id" => self.id.get().to_value(),
|
||||
"id" => self.id.borrow().to_value(),
|
||||
"active" => self.active.get().to_value(),
|
||||
_ => unimplemented!(),
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,15 +13,18 @@ impl WorkspaceObject {
|
|||
glib::Object::new(&[]).unwrap()
|
||||
}
|
||||
|
||||
pub fn from_id_active(id: u32, active: bool) -> Self {
|
||||
pub fn from_id_active(id: String, active: u32) -> Self {
|
||||
glib::Object::new(&[("id", &id), ("active", &active)]).unwrap()
|
||||
}
|
||||
|
||||
pub fn id(&self) -> u32 {
|
||||
imp::WorkspaceObject::from_instance(&self).id.get()
|
||||
pub fn id(&self) -> String {
|
||||
imp::WorkspaceObject::from_instance(&self)
|
||||
.id
|
||||
.borrow()
|
||||
.clone()
|
||||
}
|
||||
|
||||
pub fn active(&self) -> bool {
|
||||
pub fn active(&self) -> u32 {
|
||||
imp::WorkspaceObject::from_instance(&self).active.get()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue