refactor: tiling applet

This commit is contained in:
Ashley Wulber 2024-02-12 10:10:56 -05:00 committed by Ashley Wulber
parent 34c4bada71
commit 9c9e3986a1
9 changed files with 735 additions and 220 deletions

View file

@ -1,6 +1,8 @@
use crate::window::Window;
mod localize;
mod wayland;
mod wayland_subscription;
mod window;
fn main() -> cosmic::iced::Result {

View file

@ -0,0 +1,235 @@
use calloop::channel::*;
use cctk::{
sctk::{
self,
output::{OutputHandler, OutputState},
reexports::{calloop, calloop_wayland_source::WaylandSource, client as wayland_client},
registry::{ProvidesRegistryState, RegistryState},
},
wayland_client::WEnum,
workspace::{WorkspaceHandler, WorkspaceState},
};
use cosmic::iced::futures;
use cosmic_protocols::workspace::v1::client::zcosmic_workspace_handle_v1::{self, TilingState};
use futures::{channel::mpsc, executor::block_on, SinkExt};
use std::{
os::{
fd::{FromRawFd, RawFd},
unix::net::UnixStream,
},
time::Duration,
};
use tracing::error;
use wayland_client::{
globals::registry_queue_init,
protocol::wl_output::{self, WlOutput},
};
use wayland_client::{Connection, QueueHandle};
pub fn spawn_workspaces(tx: mpsc::Sender<TilingState>) -> SyncSender<TilingState> {
let (workspaces_tx, workspaces_rx) = calloop::channel::sync_channel(100);
let socket = std::env::var("X_PRIVILEGED_WAYLAND_SOCKET")
.ok()
.and_then(|fd| {
fd.parse::<RawFd>()
.ok()
.map(|fd| unsafe { UnixStream::from_raw_fd(fd) })
});
let conn = if let Some(socket) = socket {
Connection::from_socket(socket)
} else {
Connection::connect_to_env()
}
.map_err(anyhow::Error::msg);
if let Ok(conn) = conn {
std::thread::spawn(move || {
let configured_output = std::env::var("COSMIC_PANEL_OUTPUT")
.ok()
.unwrap_or_default();
let mut event_loop = calloop::EventLoop::<State>::try_new().unwrap();
let loop_handle = event_loop.handle();
let (globals, event_queue) = registry_queue_init(&conn).unwrap();
let qhandle = event_queue.handle();
WaylandSource::new(conn, event_queue)
.insert(loop_handle)
.unwrap();
let registry_state = RegistryState::new(&globals);
let mut state = State {
// Must be before `WorkspaceState`
output_state: OutputState::new(&globals, &qhandle),
configured_output,
workspace_state: WorkspaceState::new(&registry_state, &qhandle),
registry_state,
expected_output: None,
tx,
running: true,
have_workspaces: false,
};
let loop_handle = event_loop.handle();
loop_handle
.insert_source(workspaces_rx, |e, _, state| match e {
Event::Msg(autotile) => {
if let Some(w) =
state
.workspace_state
.workspace_groups()
.iter()
.find_map(|g| {
if let Some(o) = state.expected_output.as_ref() {
if !g.outputs.contains(o) {
return None;
}
}
g.workspaces.iter().find(|w| {
w.state.contains(&WEnum::Value(
zcosmic_workspace_handle_v1::State::Active,
))
})
})
{
w.handle.set_tiling_state(autotile);
state
.workspace_state
.workspace_manager()
.get()
.unwrap()
.commit();
}
}
Event::Closed => {
if let Ok(workspace_manager) =
state.workspace_state.workspace_manager().get()
{
for g in state.workspace_state.workspace_groups() {
g.handle.destroy();
}
workspace_manager.stop();
}
}
})
.unwrap();
while state.running {
event_loop
.dispatch(Duration::from_millis(16), &mut state)
.unwrap();
}
});
} else {
eprintln!("ENV variable WAYLAND_DISPLAY is missing. Exiting...");
std::process::exit(1);
}
workspaces_tx
}
#[derive(Debug)]
pub struct State {
running: bool,
tx: mpsc::Sender<TilingState>,
configured_output: String,
expected_output: Option<WlOutput>,
output_state: OutputState,
registry_state: RegistryState,
workspace_state: WorkspaceState,
have_workspaces: bool,
}
impl State {
pub fn tiling_state(&self) -> Option<TilingState> {
self.workspace_state
.workspace_groups()
.iter()
.find_map(|g| {
if g.outputs
.iter()
.any(|o| Some(o) == self.expected_output.as_ref())
{
g.workspaces.iter().find_map(|w| {
if w.state
.contains(&WEnum::Value(zcosmic_workspace_handle_v1::State::Active))
{
w.tiling.and_then(|e| match e {
WEnum::Value(v) => Some(v),
_ => {
error!("No tiling state for the workspace");
None
}
})
} else {
None
}
})
} else {
None
}
})
}
}
impl ProvidesRegistryState for State {
fn registry(&mut self) -> &mut RegistryState {
&mut self.registry_state
}
sctk::registry_handlers![OutputState,];
}
impl OutputHandler for State {
fn output_state(&mut self) -> &mut OutputState {
&mut self.output_state
}
fn new_output(
&mut self,
_conn: &Connection,
_qh: &QueueHandle<Self>,
output: wl_output::WlOutput,
) {
let info = self.output_state.info(&output).unwrap();
if info.name.as_deref() == Some(&self.configured_output) {
self.expected_output = Some(output);
if self.have_workspaces {
if let Some(s) = self.tiling_state() {
let _ = block_on(self.tx.send(s));
}
}
}
}
fn update_output(
&mut self,
_conn: &Connection,
_qh: &QueueHandle<Self>,
_output: wl_output::WlOutput,
) {
}
fn output_destroyed(
&mut self,
_conn: &Connection,
_qh: &QueueHandle<Self>,
_output: wl_output::WlOutput,
) {
}
}
impl WorkspaceHandler for State {
fn workspace_state(&mut self) -> &mut WorkspaceState {
&mut self.workspace_state
}
fn done(&mut self) {
self.have_workspaces = true;
if let Some(s) = self.tiling_state() {
let _ = block_on(self.tx.send(s));
}
}
}
cctk::delegate_workspace!(State);
sctk::delegate_output!(State);
sctk::delegate_registry!(State);

View file

@ -0,0 +1,91 @@
use crate::wayland::{self};
use cctk::sctk::reexports::calloop::channel::SyncSender;
use cosmic::iced::{
self,
futures::{self, channel::mpsc, SinkExt, StreamExt},
subscription,
};
use cosmic_protocols::workspace::v1::client::zcosmic_workspace_handle_v1::TilingState;
use once_cell::sync::Lazy;
use tokio::sync::Mutex;
pub static WAYLAND_RX: Lazy<Mutex<Option<mpsc::Receiver<TilingState>>>> =
Lazy::new(|| Mutex::new(None));
#[derive(Debug, Clone)]
pub enum WorkspacesUpdate {
State(TilingState),
Started(SyncSender<TilingState>),
Errored,
}
pub fn workspaces() -> iced::Subscription<WorkspacesUpdate> {
subscription::channel(
std::any::TypeId::of::<WorkspacesUpdate>(),
50,
move |mut output| async move {
let mut state = State::Waiting;
loop {
state = start_listening(state, &mut output).await;
}
},
)
}
async fn start_listening(
state: State,
output: &mut futures::channel::mpsc::Sender<WorkspacesUpdate>,
) -> State {
match state {
State::Waiting => {
let mut guard = WAYLAND_RX.lock().await;
let rx = {
if guard.is_none() {
if let Ok(WorkspacesWatcher { rx, tx }) = WorkspacesWatcher::new() {
*guard = Some(rx);
_ = output.send(WorkspacesUpdate::Started(tx)).await;
} else {
_ = output.send(WorkspacesUpdate::Errored).await;
return State::Error;
}
}
guard.as_mut().unwrap()
};
if let Some(w) = rx.next().await {
_ = output.send(WorkspacesUpdate::State(w)).await;
State::Waiting
} else {
_ = output.send(WorkspacesUpdate::Errored).await;
State::Error
}
}
State::Error => cosmic::iced::futures::future::pending().await,
}
}
pub enum State {
Waiting,
Error,
}
pub struct WorkspacesWatcher {
rx: mpsc::Receiver<TilingState>,
tx: SyncSender<TilingState>,
}
impl WorkspacesWatcher {
pub fn new() -> anyhow::Result<Self> {
let (tx, rx) = mpsc::channel(20);
let tx = wayland::spawn_workspaces(tx);
Ok(Self { tx, rx })
}
pub fn get_sender(&self) -> SyncSender<TilingState> {
self.tx.clone()
}
pub async fn workspaces(&mut self) -> Option<TilingState> {
self.rx.next().await
}
}

View file

@ -1,34 +1,46 @@
use crate::fl;
use crate::wayland_subscription::WorkspacesUpdate;
use crate::{fl, wayland_subscription};
use cctk::sctk::reexports::calloop::channel::SyncSender;
use cosmic::app::Core;
use cosmic::applet::{menu_button, padded_control};
use cosmic::cosmic_config::{ConfigGet, ConfigSet};
use cosmic::applet::padded_control;
use cosmic::cosmic_config::{Config, ConfigSet, CosmicConfigEntry};
use cosmic::iced::wayland::popup::{destroy_popup, get_popup};
use cosmic::iced::window::Id;
use cosmic::iced::{Command, Length, Limits, Subscription};
use cosmic::iced_core::Alignment;
use cosmic::iced_style::application;
use cosmic::iced_widget::{column, row};
use cosmic::widget::{container, divider, spin_button, text};
use cosmic::widget::segmented_button::{Entity, SingleSelectModel};
use cosmic::widget::{
container, divider, segmented_button, segmented_selection, spin_button, text,
};
use cosmic::{Element, Theme};
use cosmic_comp_config::{CosmicCompConfig, TileBehavior};
use cosmic_protocols::workspace::v1::client::zcosmic_workspace_handle_v1::TilingState;
use cosmic_time::{anim, chain, id, Timeline};
use once_cell::sync::Lazy;
use std::thread;
use std::time::Instant;
use tracing::error;
const ID: &str = "com.system76.CosmicAppletTiling";
//const ON: &str = "com.system76.CosmicAppletTiling.On";
const OFF: &str = "com.system76.CosmicAppletTiling.Off";
static TILE_WINDOWS: Lazy<id::Toggler> = Lazy::new(id::Toggler::unique);
static ACTIVE_HINT: Lazy<id::Toggler> = Lazy::new(id::Toggler::unique);
#[derive(Default)]
pub struct Window {
core: Core,
popup: Option<Id>,
timeline: Timeline,
tile_windows: bool,
active_hint: spin_button::Model<i32>,
gaps: spin_button::Model<i32>,
config: CosmicCompConfig,
config_helper: Config,
autotile_behavior_model: segmented_button::SingleSelectModel,
new_workspace_behavior_model: segmented_button::SingleSelectModel,
autotile_global_entity: Entity,
new_workspace_entity: Entity,
/// may not match the config value if behavior is per-workspace
autotiled: bool,
workspace_tx: Option<SyncSender<TilingState>>,
}
#[derive(Clone, Debug)]
@ -37,12 +49,11 @@ pub enum Message {
PopupClosed(Id),
Frame(Instant),
ToggleTileWindows(chain::Toggler, bool),
HandleActiveHint(spin_button::Message),
HandleGaps(spin_button::Message),
ViewAllShortcuts,
OpenFloatingWindowExceptions,
OpenWindowManagementSettings,
Ignore,
ToggleActiveHint(chain::Toggler, bool),
MyConfigUpdate(CosmicCompConfig),
TileMode(Entity),
WorkspaceUpdate(WorkspacesUpdate),
NewWorkspace(Entity),
}
impl cosmic::Application for Window {
@ -67,11 +78,55 @@ impl cosmic::Application for Window {
gaps.value = core.system_theme().cosmic().gaps.1 as i32;
let mut active_hint = spin_button::Model::default().max(99).min(0).step(1);
active_hint.value = core.system_theme().cosmic().active_hint as i32;
let config_helper =
Config::new("com.system76.CosmicComp", CosmicCompConfig::VERSION).unwrap();
let config = CosmicCompConfig::get_entry(&config_helper).unwrap_or_else(|(errs, c)| {
for err in errs {
error!(?err, "Error loading config");
}
c
});
let mut autotile_behavior_model = SingleSelectModel::default();
let autotile_global_entity = autotile_behavior_model
.insert()
.text(fl!("all-workspaces"))
.id();
let per = autotile_behavior_model
.insert()
.text(fl!("per-workspace"))
.id();
autotile_behavior_model.activate(match config.autotile_behavior {
TileBehavior::Global => autotile_global_entity,
TileBehavior::PerWorkspace => per,
});
let mut new_workspace_behavior_model = SingleSelectModel::default();
let new_workspace_entity = new_workspace_behavior_model
.insert()
.text(fl!("tiled"))
.id();
let floating = new_workspace_behavior_model
.insert()
.text(fl!("floating"))
.id();
new_workspace_behavior_model.activate(if config.autotile {
new_workspace_entity
} else {
floating
});
let window = Self {
core,
gaps,
active_hint,
..Default::default()
popup: None,
timeline: Default::default(),
autotiled: config.autotile,
config,
config_helper,
autotile_behavior_model,
new_workspace_behavior_model,
autotile_global_entity,
new_workspace_entity,
workspace_tx: None,
};
(window, Command::none())
}
@ -85,11 +140,33 @@ impl cosmic::Application for Window {
.timeline
.as_subscription()
.map(|(_, now)| Message::Frame(now));
Subscription::batch(vec![timeline])
Subscription::batch(vec![
timeline,
self.core
.watch_config::<CosmicCompConfig>("com.system76.CosmicComp")
.map(|u| Message::MyConfigUpdate(u.config)),
wayland_subscription::workspaces().map(|e| Message::WorkspaceUpdate(e)),
])
}
fn update(&mut self, message: Self::Message) -> Command<cosmic::app::Message<Self::Message>> {
match message {
Message::WorkspaceUpdate(msg) => match msg {
WorkspacesUpdate::State(state) => {
self.autotiled = matches!(state, TilingState::TilingEnabled);
self.timeline.set_chain(if self.autotiled {
cosmic_time::chain::Toggler::on(TILE_WINDOWS.clone(), 1.0)
} else {
cosmic_time::chain::Toggler::off(TILE_WINDOWS.clone(), 1.0)
});
}
WorkspacesUpdate::Started(tx) => {
self.workspace_tx = Some(tx);
}
WorkspacesUpdate::Errored => {
error!("Workspaces subscription failed...");
}
},
Message::TogglePopup => {
return if let Some(p) = self.popup.take() {
destroy_popup(p)
@ -101,7 +178,7 @@ impl cosmic::Application for Window {
.applet
.get_popup_settings(Id::MAIN, new_id, None, None, None);
popup_settings.positioner.size_limits = Limits::NONE
.max_width(372.0)
.max_width(400.0)
.min_width(300.0)
.min_height(200.0)
.max_height(1080.0);
@ -116,103 +193,106 @@ impl cosmic::Application for Window {
Message::Frame(now) => self.timeline.now(now),
Message::ToggleTileWindows(chain, toggled) => {
self.timeline.set_chain(chain).start();
self.tile_windows = toggled
self.autotiled = toggled;
if matches!(self.config.autotile_behavior, TileBehavior::Global) {
self.config.autotile = toggled;
if toggled {
self.new_workspace_behavior_model.activate_position(0);
} else {
self.new_workspace_behavior_model.activate_position(1);
}
let helper = self.config_helper.clone();
thread::spawn(move || {
if let Err(err) = helper.set("autotile", toggled) {
error!(?err, "Failed to set autotile {toggled}");
}
});
} else {
// set via protocol
if let Some(tx) = self.workspace_tx.as_ref() {
let state = if toggled {
TilingState::TilingEnabled
} else {
TilingState::FloatingOnly
};
if let Err(err) = tx.send(state) {
error!("Failed to send the tiling state update. {err:?}")
}
}
}
}
Message::HandleActiveHint(msg) => {
match msg {
spin_button::Message::Increment => {
self.active_hint.update(spin_button::Message::Increment)
Message::ToggleActiveHint(chain, toggled) => {
self.timeline.set_chain(chain).start();
self.config.active_hint = toggled;
let helper = self.config_helper.clone();
thread::spawn(move || {
if let Err(err) = helper.set("active_hint", toggled) {
error!(?err, "Failed to set active_hint {toggled}");
}
spin_button::Message::Decrement => {
self.active_hint.update(spin_button::Message::Decrement)
});
}
Message::MyConfigUpdate(c) => {
if matches!(c.autotile_behavior, TileBehavior::Global) {
if c.autotile != self.config.autotile {
self.timeline.set_chain(if c.autotile {
cosmic_time::chain::Toggler::on(TILE_WINDOWS.clone(), 1.0)
} else {
cosmic_time::chain::Toggler::off(TILE_WINDOWS.clone(), 1.0)
});
}
self.autotile_behavior_model.activate_position(0);
} else {
if c.autotile != self.config.autotile {
self.new_workspace_behavior_model
.activate_position(if c.autotile { 0 } else { 1 });
}
self.autotile_behavior_model.activate_position(1);
}
if c.active_hint != self.config.active_hint {
self.timeline.set_chain(if c.active_hint {
cosmic_time::chain::Toggler::on(ACTIVE_HINT.clone(), 1.0)
} else {
cosmic_time::chain::Toggler::off(ACTIVE_HINT.clone(), 1.0)
});
}
self.config = c;
}
Message::TileMode(e) => {
let behavior = if e == self.autotile_global_entity {
TileBehavior::Global
} else {
TileBehavior::PerWorkspace
};
let is_dark = self.core.system_theme().cosmic().is_dark;
let active_hint = self.active_hint.value;
return Command::perform(
async move {
let config = if is_dark {
cosmic::cosmic_theme::ThemeBuilder::dark_config()
} else {
cosmic::cosmic_theme::ThemeBuilder::light_config()
};
let Ok(config) = config else {
return;
};
if let Err(err) = ConfigSet::set(&config, "active_hint", active_hint) {
error!(?err, "Error setting active_hint");
}
let config = if is_dark {
cosmic::theme::CosmicTheme::dark_config()
} else {
cosmic::theme::CosmicTheme::light_config()
};
let Ok(config) = config else {
return;
};
if let Err(err) = ConfigSet::set(&config, "active_hint", active_hint) {
error!(?err, "Error setting active_hint");
}
},
|_| cosmic::app::Message::App(Message::Ignore),
);
}
Message::HandleGaps(msg) => {
match msg {
spin_button::Message::Increment => {
self.gaps.update(spin_button::Message::Increment)
self.config.autotile_behavior = behavior;
self.autotile_behavior_model.activate(e);
let helper = self.config_helper.clone();
let need_to_reset_autotile = self.autotiled != self.config.autotile
&& matches!(behavior, TileBehavior::Global);
if need_to_reset_autotile {
self.autotiled = self.config.autotile;
}
thread::spawn(move || {
if let Err(err) = helper.set("autotile_behavior", behavior) {
error!(?err, "Failed to set autotile_behavior {behavior:?}");
}
spin_button::Message::Decrement => {
self.gaps.update(spin_button::Message::Decrement)
}
};
let is_dark = self.core.system_theme().cosmic().is_dark;
let gaps = self.gaps.value;
return Command::perform(
async move {
let config = if is_dark {
cosmic::cosmic_theme::ThemeBuilder::dark_config()
} else {
cosmic::cosmic_theme::ThemeBuilder::light_config()
};
let Ok(config) = config else {
return;
};
let Ok(mut c_gaps) = ConfigGet::get::<(u32, u32)>(&config, "gaps") else {
error!("Error getting gaps");
return;
};
c_gaps.1 = gaps as u32;
if let Err(err) = ConfigSet::set(&config, "gaps", c_gaps) {
error!(?err, "Error setting gaps");
}
let config = if is_dark {
cosmic::theme::CosmicTheme::dark_config()
} else {
cosmic::theme::CosmicTheme::light_config()
};
let Ok(config) = config else {
return;
};
if let Err(err) = ConfigSet::set(&config, "gaps", c_gaps) {
error!(?err, "Error setting gaps");
}
},
|_| cosmic::app::Message::App(Message::Ignore),
);
});
}
Message::NewWorkspace(e) => {
let autotile_new = self.new_workspace_entity == e;
self.config.autotile = autotile_new;
self.new_workspace_behavior_model.activate(e);
// set the config autotile behavior
let helper = self.config_helper.clone();
thread::spawn(move || {
if let Err(err) = helper.set("autotile", autotile_new) {
error!(?err, "Failed to set autotile {autotile_new:?}");
}
});
}
Message::ViewAllShortcuts => println!("View all shortcuts..."),
Message::OpenFloatingWindowExceptions => println!("Floating window exceptions..."),
Message::OpenWindowManagementSettings => println!("Window management settings..."),
Message::Ignore => {}
}
Command::none()
}
@ -226,22 +306,47 @@ impl cosmic::Application for Window {
}
fn view_window(&self, _id: Id) -> Element<Self::Message> {
let cosmic = self.core.system_theme().cosmic();
let active_hint = cosmic.active_hint;
let gaps = cosmic.gaps.1;
let space_xxs = self.core.system_theme().cosmic().space_xxs();
let mut new_workspace_behavior_button =
segmented_selection::horizontal(&self.new_workspace_behavior_model);
if matches!(self.config.autotile_behavior, TileBehavior::PerWorkspace) {
new_workspace_behavior_button =
new_workspace_behavior_button.on_activate(|e| Message::NewWorkspace(e));
}
let content_list = column![
padded_control(container(
anim!(
TILE_WINDOWS,
&self.timeline,
fl!("tile-windows"),
self.tile_windows,
if matches!(self.config.autotile_behavior, TileBehavior::Global) {
fl!("tile-windows")
} else {
fl!("tile-current")
},
self.autotiled,
|chain, enable| { Message::ToggleTileWindows(chain, enable) },
)
.text_size(14)
.width(Length::Fill),
))
.width(Length::Fill),
padded_control(
column![
divider::horizontal::default(),
column![
text(fl!("autotile-behavior")).size(14),
segmented_selection::horizontal(&self.autotile_behavior_model)
.on_activate(|e| Message::TileMode(e))
],
divider::horizontal::default(),
column![
text(fl!("new-workspace")).size(14),
new_workspace_behavior_button,
]
]
.spacing(space_xxs)
),
padded_control(divider::horizontal::default()),
padded_control(row!(
text(fl!("navigate-windows")).size(14).width(Length::Fill),
text(format!("{} + {}", fl!("super"), fl!("arrow-keys"))).size(14),
@ -264,26 +369,16 @@ impl cosmic::Application for Window {
)),
padded_control(divider::horizontal::default()),
padded_control(
row!(
text(fl!("active-hint")).size(14).width(Length::Fill),
spin_button(active_hint.to_string(), Message::HandleActiveHint),
anim!(
ACTIVE_HINT,
&self.timeline,
fl!("active-hint"),
self.config.active_hint,
|chain, enable| { Message::ToggleActiveHint(chain, enable) },
)
.align_items(Alignment::Center),
.text_size(14)
.width(Length::Fill),
),
padded_control(
row!(
text(fl!("gaps")).size(14).width(Length::Fill),
spin_button(gaps.to_string(), Message::HandleGaps),
)
.align_items(Alignment::Center),
),
padded_control(divider::horizontal::default()),
menu_button(text(fl!("view-all-shortcuts")).size(14))
.on_press(Message::ViewAllShortcuts),
menu_button(text(fl!("floating-window-exceptions")).size(14))
.on_press(Message::OpenFloatingWindowExceptions),
menu_button(text(fl!("window-management-settings")).size(14))
.on_press(Message::OpenWindowManagementSettings)
]
.padding([8, 0]);