cosmic-applets/cosmic-applet-tiling/src/window.rs

362 lines
13 KiB
Rust
Raw Normal View History

2024-05-06 15:39:04 +02:00
// Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: GPL-3.0-only
2024-07-09 15:17:44 +02:00
use crate::{
fl, wayland::AppRequest, wayland_subscription, wayland_subscription::WorkspacesUpdate,
};
2024-02-12 10:10:56 -05:00
use cctk::sctk::reexports::calloop::channel::SyncSender;
2024-07-09 15:17:44 +02:00
use cosmic::{
2025-08-12 21:38:51 +02:00
Element, Task, app,
2024-07-09 15:17:44 +02:00
app::Core,
applet::{menu_button, padded_control},
2024-07-09 15:17:44 +02:00
cosmic_config::{Config, ConfigSet, CosmicConfigEntry},
cosmic_theme::Spacing,
2024-07-09 15:17:44 +02:00
iced::{
2025-08-12 21:38:51 +02:00
Length, Subscription,
2024-10-30 22:51:08 -04:00
platform_specific::shell::wayland::commands::popup::{destroy_popup, get_popup},
2024-07-09 15:17:44 +02:00
window::Id,
},
iced_widget::{column, row},
2025-03-14 13:14:51 -04:00
surface, theme,
2024-07-09 15:17:44 +02:00
widget::{
container, divider,
segmented_button::{self, Entity, SingleSelectModel},
segmented_control, text,
2024-07-09 15:17:44 +02:00
},
};
2024-02-12 10:10:56 -05:00
use cosmic_comp_config::{CosmicCompConfig, TileBehavior};
use cosmic_protocols::workspace::v2::client::zcosmic_workspace_handle_v2::TilingState;
2025-08-12 21:38:51 +02:00
use cosmic_time::{Timeline, anim, chain, id};
2024-07-09 15:17:44 +02:00
use std::{thread, time::Instant};
2023-10-18 18:29:51 -04:00
use tracing::error;
const ID: &str = "com.system76.CosmicAppletTiling";
const ON: &str = "com.system76.CosmicAppletTiling.On";
2023-09-18 00:24:21 -07:00
const OFF: &str = "com.system76.CosmicAppletTiling.Off";
pub struct Window {
core: Core,
popup: Option<Id>,
2023-09-18 00:24:21 -07:00
timeline: Timeline,
2024-02-12 10:10:56 -05:00
config: CosmicCompConfig,
config_helper: Config,
new_workspace_behavior_model: segmented_button::SingleSelectModel,
new_workspace_entity: Entity,
/// may not match the config value if behavior is per-workspace
autotiled: bool,
workspace_tx: Option<SyncSender<AppRequest>>,
tile_windows: id::Toggler,
active_hint: id::Toggler,
}
#[derive(Clone, Debug)]
pub enum Message {
TogglePopup,
PopupClosed(Id),
2023-09-18 00:24:21 -07:00
Frame(Instant),
ToggleTileWindows(chain::Toggler, bool),
2024-02-12 10:10:56 -05:00
ToggleActiveHint(chain::Toggler, bool),
2024-02-12 11:40:20 -05:00
MyConfigUpdate(Box<CosmicCompConfig>),
2024-02-12 10:10:56 -05:00
WorkspaceUpdate(WorkspacesUpdate),
NewWorkspace(Entity),
OpenSettings,
2025-03-14 13:14:51 -04:00
Surface(surface::Action),
}
impl cosmic::Application for Window {
type Executor = cosmic::SingleThreadExecutor;
type Flags = ();
type Message = Message;
const APP_ID: &'static str = ID;
fn core(&self) -> &Core {
&self.core
}
fn core_mut(&mut self) -> &mut Core {
&mut self.core
}
2025-03-14 13:14:51 -04:00
fn init(core: Core, _flags: Self::Flags) -> (Self, app::Task<Self::Message>) {
2024-02-12 10:10:56 -05:00
let config_helper =
Config::new("com.system76.CosmicComp", CosmicCompConfig::VERSION).unwrap();
2024-05-10 11:39:11 -04:00
let mut config = CosmicCompConfig::get_entry(&config_helper).unwrap_or_else(|(errs, c)| {
2024-02-12 10:10:56 -05:00
for err in errs {
error!(?err, "Error loading config");
}
c
});
2024-05-10 11:39:11 -04:00
// Global is removed in favor of per-workspace
if let Err(err) = config.set_autotile_behavior(&config_helper, TileBehavior::PerWorkspace) {
error!(?err, "Failed to set autotile behavior to PerWorkspace");
}
2024-02-12 10:10:56 -05:00
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
});
2023-11-16 18:32:31 +00:00
let window = Self {
core,
2024-02-12 10:10:56 -05:00
popup: None,
timeline: Default::default(),
autotiled: config.autotile,
config,
config_helper,
new_workspace_behavior_model,
new_workspace_entity,
workspace_tx: None,
tile_windows: id::Toggler::unique(),
active_hint: id::Toggler::unique(),
};
2024-10-30 22:51:08 -04:00
(window, Task::none())
}
2023-09-18 00:24:21 -07:00
fn on_close_requested(&self, id: Id) -> Option<Message> {
Some(Message::PopupClosed(id))
}
2023-09-18 00:24:21 -07:00
fn subscription(&self) -> Subscription<Self::Message> {
let timeline = self
.timeline
.as_subscription()
.map(|(_, now)| Message::Frame(now));
2024-02-12 10:10:56 -05:00
Subscription::batch(vec![
timeline,
self.core
.watch_config::<CosmicCompConfig>("com.system76.CosmicComp")
2024-02-12 11:40:20 -05:00
.map(|u| Message::MyConfigUpdate(Box::new(u.config))),
wayland_subscription::workspaces().map(Message::WorkspaceUpdate),
2024-02-12 10:10:56 -05:00
])
2023-09-18 00:24:21 -07:00
}
2025-03-14 13:14:51 -04:00
fn update(&mut self, message: Self::Message) -> app::Task<Self::Message> {
match message {
2024-02-12 10:10:56 -05:00
Message::WorkspaceUpdate(msg) => match msg {
WorkspacesUpdate::State(state) => {
self.autotiled = matches!(state, TilingState::TilingEnabled);
if self.popup.is_some() {
self.timeline
.set_chain(if self.autotiled {
cosmic_time::chain::Toggler::on(self.tile_windows.clone(), 1.0)
} else {
cosmic_time::chain::Toggler::off(self.tile_windows.clone(), 1.0)
})
.start();
}
2024-02-12 10:10:56 -05:00
}
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)
} else {
self.timeline = Timeline::default();
self.tile_windows = id::Toggler::unique();
self.active_hint = id::Toggler::unique();
2023-12-11 14:45:36 -05:00
let new_id = Id::unique();
2024-10-30 22:51:08 -04:00
self.popup = Some(new_id);
2025-08-12 21:38:51 +02:00
let popup_settings = self.core.applet.get_popup_settings(
2024-10-30 22:51:08 -04:00
self.core.main_window_id().unwrap(),
new_id,
Some((1, 1)),
None,
None,
);
get_popup(popup_settings)
2025-08-12 21:38:51 +02:00
};
}
Message::PopupClosed(id) => {
if self.popup.as_ref() == Some(&id) {
self.popup = None;
}
}
2023-09-18 00:24:21 -07:00
Message::Frame(now) => self.timeline.now(now),
Message::ToggleTileWindows(chain, toggled) => {
self.timeline.set_chain(chain).start();
2024-02-12 10:10:56 -05:00
self.autotiled = toggled;
2024-05-10 11:39:11 -04:00
// set via protocol
if let Some(tx) = self.workspace_tx.as_ref() {
let state = if toggled {
TilingState::TilingEnabled
2024-02-12 10:10:56 -05:00
} else {
2024-05-10 11:39:11 -04:00
TilingState::FloatingOnly
};
2023-10-18 18:29:51 -04:00
if let Err(err) = tx.send(AppRequest::TilingState(state)) {
2024-05-10 11:39:11 -04:00
error!("Failed to send the tiling state update. {err:?}")
2023-10-18 18:29:51 -04:00
}
2024-02-12 10:10:56 -05:00
}
}
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}");
2023-10-18 18:29:51 -04:00
}
2024-02-12 10:10:56 -05:00
});
}
Message::MyConfigUpdate(c) => {
2024-05-10 11:39:11 -04:00
if c.autotile != self.config.autotile {
self.new_workspace_behavior_model
.activate_position(if c.autotile { 0 } else { 1 });
2024-02-12 10:10:56 -05:00
}
2024-05-10 11:39:11 -04:00
2024-02-12 10:10:56 -05:00
if c.active_hint != self.config.active_hint {
if self.popup.is_some() {
self.timeline
.set_chain(if c.active_hint {
cosmic_time::chain::Toggler::on(self.active_hint.clone(), 1.0)
} else {
cosmic_time::chain::Toggler::off(self.active_hint.clone(), 1.0)
})
.start();
}
2024-02-12 10:10:56 -05:00
}
2023-10-18 18:29:51 -04:00
2024-02-12 11:40:20 -05:00
self.config = *c;
2024-02-12 10:10:56 -05:00
}
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();
if let Some(tx) = self.workspace_tx.as_ref() {
let state = if autotile_new {
TilingState::TilingEnabled
} else {
TilingState::FloatingOnly
};
if let Err(err) = tx.send(AppRequest::DefaultBehavior(state)) {
error!("Failed to send the tiling state update. {err:?}")
}
}
2024-02-12 10:10:56 -05:00
thread::spawn(move || {
if let Err(err) = helper.set("autotile", autotile_new) {
error!(?err, "Failed to set autotile {autotile_new:?}");
}
});
2023-09-18 00:24:21 -07:00
}
Message::OpenSettings => {
let mut cmd = std::process::Command::new("cosmic-settings");
cmd.arg("window-management");
tokio::spawn(cosmic::process::spawn(cmd));
}
2025-03-17 22:18:25 -04:00
Message::Surface(a) => {
return cosmic::task::message(cosmic::Action::Cosmic(
cosmic::app::Action::Surface(a),
));
}
}
2024-10-30 22:51:08 -04:00
Task::none()
}
fn view(&self) -> Element<Self::Message> {
self.core
2023-09-18 08:31:27 -07:00
.applet
.icon_button(if self.autotiled { ON } else { OFF })
2024-08-13 19:03:34 +02:00
.on_press_down(Message::TogglePopup)
.into()
}
fn view_window(&self, _id: Id) -> Element<Self::Message> {
let Spacing {
space_xxxs,
space_xxs,
space_s,
..
} = theme::active().cosmic().spacing;
let new_workspace_behavior_button =
2024-05-10 11:39:11 -04:00
segmented_control::horizontal(&self.new_workspace_behavior_model)
.on_activate(Message::NewWorkspace);
2023-10-19 17:05:13 -04:00
let content_list = column![
padded_control(container(
anim!(
self.tile_windows,
2023-10-19 17:05:13 -04:00
&self.timeline,
2024-05-10 11:39:11 -04:00
fl!("tile-current"),
2024-02-12 10:10:56 -05:00
self.autotiled,
2023-10-19 17:05:13 -04:00
|chain, enable| { Message::ToggleTileWindows(chain, enable) },
2023-09-18 11:40:26 -07:00
)
2023-10-19 17:05:13 -04:00
.text_size(14)
.width(Length::Fill),
2023-12-05 14:19:44 -05:00
))
.width(Length::Fill),
padded_control(divider::horizontal::default()).padding([space_xxs, space_s]),
padded_control(
column![
text::body(fl!("new-workspace")),
new_workspace_behavior_button,
]
.spacing(space_xxxs)
),
padded_control(divider::horizontal::default()).padding([space_xxs, space_s]),
2023-10-19 17:05:13 -04:00
padded_control(row!(
text::body(fl!("navigate-windows")).width(Length::Fill),
text::body(format!("{} + {}", fl!("super"), fl!("arrow-keys"))),
2023-10-19 17:05:13 -04:00
)),
padded_control(row!(
text::body(fl!("move-window")).width(Length::Fill),
text::body(format!(
2023-10-19 17:05:13 -04:00
"{} + {} + {}",
fl!("shift"),
fl!("super"),
fl!("arrow-keys")
)),
2023-10-19 17:05:13 -04:00
)),
padded_control(row!(
text::body(fl!("toggle-floating-window")).width(Length::Fill),
text::body(format!("{} + G", fl!("super"))),
2023-10-19 17:05:13 -04:00
)),
padded_control(divider::horizontal::default()).padding([space_xxs, space_s]),
2023-10-19 17:05:13 -04:00
padded_control(
2024-02-12 10:10:56 -05:00
anim!(
self.active_hint,
2024-02-12 10:10:56 -05:00
&self.timeline,
fl!("active-hint"),
self.config.active_hint,
|chain, enable| { Message::ToggleActiveHint(chain, enable) },
2023-09-18 08:31:27 -07:00
)
2024-02-12 10:10:56 -05:00
.text_size(14)
.width(Length::Fill),
2023-10-19 17:05:13 -04:00
),
padded_control(divider::horizontal::default()).padding([space_xxs, space_s]),
menu_button(text::body(fl!("window-management-settings")))
.on_press(Message::OpenSettings)
2023-10-19 17:05:13 -04:00
]
.padding([8, 0]);
self.core.applet.popup_container(content_list).into()
}
2024-10-30 22:51:08 -04:00
fn style(&self) -> Option<cosmic::iced_runtime::Appearance> {
2023-09-18 08:31:27 -07:00
Some(cosmic::applet::style())
}
}