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

347 lines
12 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::{
app::Core,
applet::padded_control,
cosmic_config::{Config, ConfigSet, CosmicConfigEntry},
iced::{
wayland::popup::{destroy_popup, get_popup},
window::Id,
Command, Length, Limits, Subscription,
},
iced_style::application,
iced_widget::{column, row},
widget::{
container, divider, segmented_button,
segmented_button::{Entity, SingleSelectModel},
segmented_control, spin_button, text,
},
Element, Theme,
};
2024-02-12 10:10:56 -05:00
use cosmic_comp_config::{CosmicCompConfig, TileBehavior};
use cosmic_protocols::workspace::v1::client::zcosmic_workspace_handle_v1::TilingState;
2023-09-18 00:24:21 -07:00
use cosmic_time::{anim, chain, id, Timeline};
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),
}
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
}
fn init(
core: Core,
_flags: Self::Flags,
) -> (Self, Command<cosmic::app::Message<Self::Message>>) {
2023-10-18 18:29:51 -04:00
let mut gaps = spin_button::Model::default().max(99).min(0).step(1);
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;
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(),
};
(window, Command::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
}
fn update(&mut self, message: Self::Message) -> Command<cosmic::app::Message<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();
self.popup.replace(new_id);
let mut popup_settings =
self.core
2023-09-18 08:31:27 -07:00
.applet
2023-12-11 14:45:36 -05:00
.get_popup_settings(Id::MAIN, new_id, None, None, None);
popup_settings.positioner.size_limits = Limits::NONE
2024-02-12 10:10:56 -05:00
.max_width(400.0)
.min_width(300.0)
.min_height(200.0)
.max_height(1080.0);
get_popup(popup_settings)
}
}
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
}
}
Command::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 })
.on_press(Message::TogglePopup)
.into()
}
fn view_window(&self, _id: Id) -> Element<Self::Message> {
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(),),
padded_control(column![
text(fl!("new-workspace")).size(14),
new_workspace_behavior_button,
]),
2024-02-12 10:10:56 -05:00
padded_control(divider::horizontal::default()),
2023-10-19 17:05:13 -04:00
padded_control(row!(
text(fl!("navigate-windows")).size(14).width(Length::Fill),
text(format!("{} + {}", fl!("super"), fl!("arrow-keys"))).size(14),
)),
padded_control(row!(
text(fl!("move-window")).size(14).width(Length::Fill),
text(format!(
"{} + {} + {}",
fl!("shift"),
fl!("super"),
fl!("arrow-keys")
))
.size(14),
)),
padded_control(row!(
text(fl!("toggle-floating-window"))
.size(14)
.width(Length::Fill),
text(format!("{} + G", fl!("super"))).size(14),
)),
padded_control(divider::horizontal::default()),
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
),
]
.padding([8, 0]);
2023-10-18 18:29:51 -04:00
self.core.applet.popup_container(content_list).into()
}
fn style(&self) -> Option<<Theme as application::StyleSheet>::Style> {
2023-09-18 08:31:27 -07:00
Some(cosmic::applet::style())
}
}