cosmic-comp/src/shell/element/stack.rs

1580 lines
56 KiB
Rust
Raw Normal View History

use super::{
window::{Focus, RESIZE_BORDER},
CosmicSurface,
};
2022-11-03 18:51:27 +01:00
use crate::{
2024-09-27 23:41:58 +02:00
backend::render::cursor::CursorState,
shell::{
2024-04-03 16:02:27 +02:00
focus::target::PointerFocusTarget,
2024-02-29 13:12:57 +01:00
grabs::{ReleaseMode, ResizeEdge},
layout::tiling::NodeDesc,
},
state::State,
2024-02-29 13:12:57 +01:00
utils::{
iced::{IcedElement, Program},
prelude::*,
},
2022-11-03 18:51:27 +01:00
};
use calloop::LoopHandle;
2023-06-08 13:19:30 +02:00
use cosmic::{
2024-10-24 00:15:05 +02:00
iced::{id::Id, widget as iced_widget, Alignment},
2024-02-09 16:48:03 -08:00
iced_core::{border::Radius, Background, Border, Color, Length},
2024-10-24 00:15:05 +02:00
iced_runtime::Task,
iced_widget::scrollable::AbsoluteOffset,
2024-07-26 14:59:11 +02:00
theme, widget as cosmic_widget, Apply, Element as CosmicElement, Theme,
2023-06-08 13:19:30 +02:00
};
2024-04-03 16:02:27 +02:00
use cosmic_settings_config::shortcuts;
2023-06-13 18:32:04 +02:00
use once_cell::sync::Lazy;
2024-04-03 16:02:27 +02:00
use shortcuts::action::{Direction, FocusDirection};
2022-09-28 12:01:29 +02:00
use smithay::{
backend::{
input::KeyState,
renderer::{
element::{
memory::MemoryRenderBufferRenderElement, surface::WaylandSurfaceRenderElement,
AsRenderElements,
},
ImportAll, ImportMem, Renderer,
2022-09-28 12:01:29 +02:00
},
},
desktop::{space::SpaceElement, WindowSurfaceType},
2022-09-28 12:01:29 +02:00
input::{
keyboard::{KeyboardTarget, KeysymHandle, ModifiersState},
2023-07-28 19:18:14 +02:00
pointer::{
2024-02-29 13:12:57 +01:00
AxisFrame, ButtonEvent, CursorImageStatus, GestureHoldBeginEvent, GestureHoldEndEvent,
GesturePinchBeginEvent, GesturePinchEndEvent, GesturePinchUpdateEvent,
GestureSwipeBeginEvent, GestureSwipeEndEvent, GestureSwipeUpdateEvent, MotionEvent,
PointerTarget, RelativeMotionEvent,
2023-07-28 19:18:14 +02:00
},
2024-03-26 15:20:40 -07:00
touch::{
DownEvent, MotionEvent as TouchMotionEvent, OrientationEvent, ShapeEvent, TouchTarget,
UpEvent,
},
2022-09-28 12:01:29 +02:00
Seat,
},
output::Output,
reexports::wayland_server::protocol::wl_surface::WlSurface,
2023-06-08 21:50:16 +02:00
render_elements,
2024-03-26 13:55:18 +01:00
utils::{Buffer, IsAlive, Logical, Physical, Point, Rectangle, Scale, Serial, Size},
2023-06-08 21:50:16 +02:00
wayland::seat::WaylandFocus,
2022-09-28 12:01:29 +02:00
};
use std::{
borrow::Cow,
fmt,
2022-09-28 12:01:29 +02:00
hash::Hash,
sync::{
2023-06-08 13:19:30 +02:00
atomic::{AtomicBool, AtomicU8, AtomicUsize, Ordering},
2022-09-28 12:01:29 +02:00
Arc, Mutex,
},
};
mod tab;
mod tab_text;
mod tabs;
use self::{
tab::{Tab, TabMessage},
tabs::Tabs,
};
2023-06-13 18:32:04 +02:00
static SCROLLABLE_ID: Lazy<Id> = Lazy::new(|| Id::new("scrollable"));
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct CosmicStack(pub(super) IcedElement<CosmicStackInternal>);
impl fmt::Debug for CosmicStack {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2023-06-13 18:32:04 +02:00
f.debug_struct("CosmicStack")
.field("internal", &self.0)
.finish_non_exhaustive()
}
}
2022-09-28 12:01:29 +02:00
#[derive(Debug, Clone)]
pub struct CosmicStackInternal {
windows: Arc<Mutex<Vec<CosmicSurface>>>,
2022-09-28 12:01:29 +02:00
active: Arc<AtomicUsize>,
2023-06-08 13:19:30 +02:00
activated: Arc<AtomicBool>,
group_focused: Arc<AtomicBool>,
scroll_to_focus: Arc<AtomicBool>,
2022-11-03 18:51:27 +01:00
previous_keyboard: Arc<AtomicUsize>,
2023-06-08 21:50:25 +02:00
pointer_entered: Arc<AtomicU8>,
reenter: Arc<AtomicBool>,
2023-07-28 19:18:14 +02:00
potential_drag: Arc<Mutex<Option<usize>>>,
override_alive: Arc<AtomicBool>,
2023-06-08 21:50:25 +02:00
last_seat: Arc<Mutex<Option<(Seat<State>, Serial)>>>,
geometry: Arc<Mutex<Option<Rectangle<i32, Global>>>>,
2023-05-31 20:02:39 +02:00
mask: Arc<Mutex<Option<tiny_skia::Mask>>>,
2022-09-28 12:01:29 +02:00
}
impl CosmicStackInternal {
pub fn swap_focus(&self, focus: Option<Focus>) -> Option<Focus> {
let value = focus.map_or(0, |x| x as u8);
unsafe { Focus::from_u8(self.pointer_entered.swap(value, Ordering::SeqCst)) }
2022-09-28 12:01:29 +02:00
}
pub fn current_focus(&self) -> Option<Focus> {
unsafe { Focus::from_u8(self.pointer_entered.load(Ordering::SeqCst)) }
2022-09-28 12:01:29 +02:00
}
}
pub const TAB_HEIGHT: i32 = 24;
2022-09-28 12:01:29 +02:00
#[derive(Debug, Clone)]
pub enum MoveResult {
Handled,
2023-09-29 21:33:16 +02:00
MoveOut(CosmicSurface, LoopHandle<'static, crate::state::State>),
Default,
}
2022-09-28 12:01:29 +02:00
impl CosmicStack {
pub fn new<I: Into<CosmicSurface>>(
windows: impl Iterator<Item = I>,
2023-09-29 21:33:16 +02:00
handle: LoopHandle<'static, crate::state::State>,
theme: cosmic::Theme,
) -> CosmicStack {
let windows = windows.map(Into::into).collect::<Vec<_>>();
assert!(!windows.is_empty());
2023-06-08 13:19:30 +02:00
for window in &windows {
window.try_force_undecorated(true);
window.set_tiled(true);
2023-07-06 18:20:10 +02:00
window.send_configure();
2023-06-08 13:19:30 +02:00
}
let width = windows[0].geometry().size.w;
CosmicStack(IcedElement::new(
CosmicStackInternal {
windows: Arc::new(Mutex::new(windows)),
active: Arc::new(AtomicUsize::new(0)),
2023-06-08 13:19:30 +02:00
activated: Arc::new(AtomicBool::new(false)),
group_focused: Arc::new(AtomicBool::new(false)),
scroll_to_focus: Arc::new(AtomicBool::new(false)),
previous_keyboard: Arc::new(AtomicUsize::new(0)),
pointer_entered: Arc::new(AtomicU8::new(0)),
reenter: Arc::new(AtomicBool::new(false)),
2023-07-28 19:18:14 +02:00
potential_drag: Arc::new(Mutex::new(None)),
override_alive: Arc::new(AtomicBool::new(true)),
2023-06-08 21:50:25 +02:00
last_seat: Arc::new(Mutex::new(None)),
2023-06-08 13:19:30 +02:00
geometry: Arc::new(Mutex::new(None)),
2023-05-31 20:02:39 +02:00
mask: Arc::new(Mutex::new(None)),
},
(width, TAB_HEIGHT),
handle,
theme,
))
2022-09-28 12:01:29 +02:00
}
pub fn add_window(&self, window: impl Into<CosmicSurface>, idx: Option<usize>) {
2023-06-08 13:19:30 +02:00
let window = window.into();
window.try_force_undecorated(true);
window.set_tiled(true);
self.0.with_program(|p| {
2023-06-23 16:08:23 +02:00
if let Some(mut geo) = p.geometry.lock().unwrap().clone() {
geo.loc.y += TAB_HEIGHT;
geo.size.h -= TAB_HEIGHT;
window.set_geometry(geo, TAB_HEIGHT as u32);
2023-06-23 16:08:23 +02:00
}
window.send_configure();
if let Some(idx) = idx {
p.windows.lock().unwrap().insert(idx, window);
2023-09-11 21:10:46 +02:00
let old_idx = p.active.swap(idx, Ordering::SeqCst);
if old_idx == idx {
p.reenter.store(true, Ordering::SeqCst);
p.previous_keyboard.store(old_idx, Ordering::SeqCst);
}
} else {
let mut windows = p.windows.lock().unwrap();
windows.push(window);
p.active.store(windows.len() - 1, Ordering::SeqCst);
}
p.scroll_to_focus.store(true, Ordering::SeqCst);
});
self.0
.resize(Size::from((self.active().geometry().size.w, TAB_HEIGHT)));
2023-06-08 13:19:30 +02:00
self.0.force_redraw()
}
pub fn remove_window(&self, window: &CosmicSurface) {
self.0.with_program(|p| {
let mut windows = p.windows.lock().unwrap();
2023-06-05 17:52:47 +02:00
if windows.len() == 1 {
2023-07-28 19:18:14 +02:00
p.override_alive.store(false, Ordering::SeqCst);
let window = windows.get(0).unwrap();
window.try_force_undecorated(false);
window.set_tiled(false);
2023-06-05 17:52:47 +02:00
return;
}
2023-07-31 17:36:32 +02:00
let Some(idx) = windows.iter().position(|w| w == window) else {
return;
};
if idx == p.active.load(Ordering::SeqCst) {
p.reenter.store(true, Ordering::SeqCst);
}
2023-06-08 13:19:30 +02:00
let window = windows.remove(idx);
window.try_force_undecorated(false);
window.set_tiled(false);
p.active.fetch_min(windows.len() - 1, Ordering::SeqCst);
2023-06-08 13:19:30 +02:00
});
self.0
.resize(Size::from((self.active().geometry().size.w, TAB_HEIGHT)));
2023-06-08 13:19:30 +02:00
self.0.force_redraw()
}
pub fn remove_idx(&self, idx: usize) -> Option<CosmicSurface> {
let window = self.0.with_program(|p| {
let mut windows = p.windows.lock().unwrap();
2023-06-05 17:52:47 +02:00
if windows.len() == 1 {
2023-07-28 19:18:14 +02:00
p.override_alive.store(false, Ordering::SeqCst);
let window = windows.get(0).unwrap();
window.try_force_undecorated(false);
window.set_tiled(false);
return Some(window.clone());
2023-06-05 17:52:47 +02:00
}
2023-07-28 19:18:14 +02:00
if windows.len() <= idx {
return None;
}
if idx == p.active.load(Ordering::SeqCst) {
p.reenter.store(true, Ordering::SeqCst);
}
let window = windows.remove(idx);
2023-06-08 13:19:30 +02:00
window.try_force_undecorated(false);
window.set_tiled(false);
2023-06-05 17:52:47 +02:00
p.active.fetch_min(windows.len() - 1, Ordering::SeqCst);
Some(window)
2023-06-08 13:19:30 +02:00
});
self.0
.resize(Size::from((self.active().geometry().size.w, TAB_HEIGHT)));
self.0.force_redraw();
window
}
pub fn len(&self) -> usize {
self.0.with_program(|p| p.windows.lock().unwrap().len())
}
pub fn handle_focus(&self, direction: FocusDirection, swap: Option<NodeDesc>) -> bool {
2023-06-08 13:19:30 +02:00
let result = self.0.with_program(|p| match direction {
FocusDirection::Left => {
if !p.group_focused.load(Ordering::SeqCst) {
2023-06-08 21:50:25 +02:00
if let Ok(old) =
p.active
.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |val| {
val.checked_sub(1)
})
{
p.previous_keyboard.store(old, Ordering::SeqCst);
p.scroll_to_focus.store(true, Ordering::SeqCst);
2023-06-08 21:50:25 +02:00
true
} else {
false
}
2023-06-08 13:19:30 +02:00
} else {
false
}
}
FocusDirection::Right => {
if !p.group_focused.load(Ordering::SeqCst) {
2023-06-05 17:52:47 +02:00
let max = p.windows.lock().unwrap().len();
2023-06-08 21:50:25 +02:00
if let Ok(old) =
p.active
.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |val| {
if val < max - 1 {
Some(val + 1)
} else {
None
}
})
{
p.previous_keyboard.store(old, Ordering::SeqCst);
p.scroll_to_focus.store(true, Ordering::SeqCst);
2023-06-08 21:50:25 +02:00
true
} else {
false
}
2023-06-08 13:19:30 +02:00
} else {
false
2023-06-05 17:52:47 +02:00
}
}
FocusDirection::Out if swap.is_none() => {
2023-06-08 13:19:30 +02:00
if !p.group_focused.swap(true, Ordering::SeqCst) {
p.windows.lock().unwrap().iter().for_each(|w| {
w.set_activated(false);
w.send_configure();
});
true
} else {
false
}
}
FocusDirection::In if swap.is_none() => {
2023-06-08 13:19:30 +02:00
if !p.group_focused.swap(false, Ordering::SeqCst) {
p.windows.lock().unwrap().iter().for_each(|w| {
w.set_activated(true);
w.send_configure();
});
true
} else {
false
}
}
_ => false,
});
if result {
self.0
.resize(Size::from((self.active().geometry().size.w, TAB_HEIGHT)));
2023-06-08 13:19:30 +02:00
self.0.force_update();
}
result
2023-06-05 17:52:47 +02:00
}
pub fn handle_move(&self, direction: Direction) -> MoveResult {
let loop_handle = self.0.loop_handle();
let result = self.0.with_program(|p| {
if p.group_focused.load(Ordering::SeqCst) {
return MoveResult::Default;
}
let active = p.active.load(Ordering::SeqCst);
let mut windows = p.windows.lock().unwrap();
let next = match direction {
Direction::Left => active.checked_sub(1),
Direction::Right => (active + 1 < windows.len()).then_some(active + 1),
Direction::Down | Direction::Up => None,
};
if let Some(val) = next {
let old = p.active.swap(val, Ordering::SeqCst);
windows.swap(old, val);
p.previous_keyboard.store(old, Ordering::SeqCst);
p.scroll_to_focus.store(true, Ordering::SeqCst);
MoveResult::Handled
} else {
if windows.len() == 1 {
return MoveResult::Default;
}
let window = windows.remove(active);
if active == windows.len() {
p.active.store(active - 1, Ordering::SeqCst);
p.scroll_to_focus.store(true, Ordering::SeqCst);
}
window.try_force_undecorated(false);
window.set_tiled(false);
MoveResult::MoveOut(window, loop_handle)
}
});
if !matches!(result, MoveResult::Default) {
self.0
.resize(Size::from((self.active().geometry().size.w, TAB_HEIGHT)));
self.0.force_update();
}
result
}
pub fn active(&self) -> CosmicSurface {
self.0
.with_program(|p| p.windows.lock().unwrap()[p.active.load(Ordering::SeqCst)].clone())
}
2023-04-01 20:35:58 +04:00
pub fn has_active(&self, window: &CosmicSurface) -> bool {
self.0
.with_program(|p| &p.windows.lock().unwrap()[p.active.load(Ordering::SeqCst)] == window)
}
pub fn whole_stack_focused(&self) -> bool {
self.0
.with_program(|p| p.group_focused.load(Ordering::SeqCst))
}
pub fn set_active(&self, window: &CosmicSurface) {
self.0.with_program(|p| {
if let Some(val) = p.windows.lock().unwrap().iter().position(|w| w == window) {
let old = p.active.swap(val, Ordering::SeqCst);
p.previous_keyboard.store(old, Ordering::SeqCst);
}
2023-06-08 13:19:30 +02:00
});
self.0
.resize(Size::from((self.active().geometry().size.w, TAB_HEIGHT)));
2023-06-08 13:19:30 +02:00
self.0.force_redraw()
}
pub fn surfaces(&self) -> impl Iterator<Item = CosmicSurface> {
self.0.with_program(|p| {
p.windows
.lock()
.unwrap()
.iter()
.cloned()
.collect::<Vec<_>>()
.into_iter()
})
}
pub fn focus_under(
&self,
mut relative_pos: Point<f64, Logical>,
2024-10-10 23:18:04 +02:00
surface_type: WindowSurfaceType,
2024-06-18 19:23:16 -07:00
) -> Option<(PointerFocusTarget, Point<f64, Logical>)> {
self.0.with_program(|p| {
let mut stack_ui = None;
let geo = p.windows.lock().unwrap()[p.active.load(Ordering::SeqCst)].geometry();
2024-10-10 23:18:04 +02:00
if surface_type.contains(WindowSurfaceType::TOPLEVEL) {
let point_i32 = relative_pos.to_i32_round::<i32>();
if (point_i32.x - geo.loc.x >= -RESIZE_BORDER && point_i32.x - geo.loc.x < 0)
|| (point_i32.y - geo.loc.y >= -RESIZE_BORDER && point_i32.y - geo.loc.y < 0)
|| (point_i32.x - geo.loc.x >= geo.size.w
&& point_i32.x - geo.loc.x < geo.size.w + RESIZE_BORDER)
|| (point_i32.y - geo.loc.y >= geo.size.h
&& point_i32.y - geo.loc.y < geo.size.h + TAB_HEIGHT + RESIZE_BORDER)
{
stack_ui = Some((
PointerFocusTarget::StackUI(self.clone()),
Point::from((0., 0.)),
));
}
2024-10-10 23:18:04 +02:00
if point_i32.y - geo.loc.y < TAB_HEIGHT {
stack_ui = Some((
PointerFocusTarget::StackUI(self.clone()),
Point::from((0., 0.)),
));
}
}
relative_pos.y -= TAB_HEIGHT as f64;
let active_window = &p.windows.lock().unwrap()[p.active.load(Ordering::SeqCst)];
2024-10-10 23:18:04 +02:00
stack_ui.or_else(|| {
active_window
.0
.surface_under(relative_pos, surface_type)
.map(|(surface, surface_offset)| {
(
PointerFocusTarget::WlSurface {
surface,
toplevel: Some(active_window.clone().into()),
},
surface_offset.to_f64() + Point::from((0., TAB_HEIGHT as f64)),
)
})
})
})
}
pub fn offset(&self) -> Point<i32, Logical> {
Point::from((0, TAB_HEIGHT))
2022-09-28 12:01:29 +02:00
}
pub fn pending_size(&self) -> Option<Size<i32, Logical>> {
self.0.with_program(|p| {
p.geometry
.lock()
.unwrap()
.clone()
.map(|geo| geo.size.as_logical())
})
}
pub fn set_geometry(&self, geo: Rectangle<i32, Global>) {
self.0.with_program(|p| {
2023-01-23 18:25:01 +01:00
let loc = (geo.loc.x, geo.loc.y + TAB_HEIGHT);
let size = (geo.size.w, geo.size.h - TAB_HEIGHT);
2023-06-08 13:19:30 +02:00
let win_geo = Rectangle::from_loc_and_size(loc, size);
for window in p.windows.lock().unwrap().iter() {
window.set_geometry(win_geo, TAB_HEIGHT as u32);
}
2023-06-08 13:19:30 +02:00
*p.geometry.lock().unwrap() = Some(geo);
p.mask.lock().unwrap().take();
});
}
pub fn on_commit(&self, surface: &WlSurface) {
if let Some(surface) = self.surfaces().find(|w| w == surface) {
surface.0.on_commit();
if self.active() == surface {
self.0
.resize(Size::from((surface.geometry().size.w, TAB_HEIGHT)));
}
}
2022-09-28 12:01:29 +02:00
}
2022-11-03 18:51:27 +01:00
fn keyboard_leave_if_previous(
&self,
seat: &Seat<State>,
data: &mut State,
serial: Serial,
) -> usize {
self.0.with_program(|p| {
let active = p.active.load(Ordering::SeqCst);
let previous = p.previous_keyboard.swap(active, Ordering::SeqCst);
if previous != active || p.reenter.swap(false, Ordering::SeqCst) {
2023-06-08 13:19:30 +02:00
let windows = p.windows.lock().unwrap();
if let Some(previous_surface) = windows.get(previous) {
if previous != active {
KeyboardTarget::leave(previous_surface, seat, data, serial);
}
2023-06-08 13:19:30 +02:00
}
2023-06-08 21:50:25 +02:00
KeyboardTarget::enter(
&windows[active],
seat,
data,
Vec::new(), /* TODO */
serial,
)
}
active
})
2022-11-03 18:51:27 +01:00
}
2023-06-14 17:29:08 +02:00
pub(in super::super) fn focus_stack(&self) {
self.0
.with_program(|p| p.group_focused.store(true, Ordering::SeqCst));
}
2023-09-29 21:33:16 +02:00
pub(in super::super) fn loop_handle(&self) -> LoopHandle<'static, crate::state::State> {
2023-06-05 17:52:47 +02:00
self.0.loop_handle()
}
2024-09-27 23:41:58 +02:00
pub fn popup_render_elements<R, C>(
&self,
renderer: &mut R,
location: Point<i32, Physical>,
scale: Scale<f64>,
alpha: f32,
2024-09-27 23:41:58 +02:00
) -> Vec<C>
where
R: Renderer + ImportAll + ImportMem,
<R as Renderer>::TextureId: Send + Clone + 'static,
C: From<CosmicStackRenderElement<R>>,
{
let window_loc = location + Point::from((0, (TAB_HEIGHT as f64 * scale.y) as i32));
self.0.with_program(|p| {
let windows = p.windows.lock().unwrap();
let active = p.active.load(Ordering::SeqCst);
windows[active]
.popup_render_elements::<R, CosmicStackRenderElement<R>>(
renderer, window_loc, scale, alpha,
)
.into_iter()
.map(C::from)
.collect()
})
}
pub fn render_elements<R, C>(
&self,
renderer: &mut R,
location: Point<i32, Physical>,
scale: Scale<f64>,
alpha: f32,
) -> Vec<C>
where
R: Renderer + ImportAll + ImportMem,
<R as Renderer>::TextureId: Send + Clone + 'static,
C: From<CosmicStackRenderElement<R>>,
{
let offset = self
.0
.with_program(|p| {
p.windows.lock().unwrap()[p.active.load(Ordering::SeqCst)]
.geometry()
.loc
})
.to_physical_precise_round(scale);
let stack_loc = location + offset;
let window_loc = location + Point::from((0, (TAB_HEIGHT as f64 * scale.y) as i32));
2024-09-27 23:41:58 +02:00
let mut elements = AsRenderElements::<R>::render_elements::<CosmicStackRenderElement<R>>(
&self.0, renderer, stack_loc, scale, alpha,
);
2024-09-27 23:41:58 +02:00
elements.extend(self.0.with_program(|p| {
let windows = p.windows.lock().unwrap();
let active = p.active.load(Ordering::SeqCst);
2023-11-22 17:13:03 +01:00
2024-09-27 23:41:58 +02:00
windows[active].render_elements::<R, CosmicStackRenderElement<R>>(
renderer, window_loc, scale, alpha,
)
}));
2024-09-27 23:41:58 +02:00
elements.into_iter().map(C::from).collect()
}
pub(crate) fn set_theme(&self, theme: cosmic::Theme) {
self.0.set_theme(theme);
}
pub(crate) fn force_redraw(&self) {
self.0.force_redraw();
}
fn start_drag(&self, data: &mut State, seat: &Seat<State>, serial: Serial) {
if let Some(dragged_out) = self
.0
.with_program(|p| p.potential_drag.lock().unwrap().take())
{
if let Some(surface) = self
.0
.with_program(|p| p.windows.lock().unwrap().get(dragged_out).cloned())
{
let seat = seat.clone();
surface.try_force_undecorated(false);
surface.send_configure();
if let Some(surface) = surface.wl_surface().map(Cow::into_owned) {
let _ = data.common.event_loop_handle.insert_idle(move |state| {
let res = state.common.shell.write().unwrap().move_request(
&surface,
&seat,
serial,
ReleaseMode::NoMouseButtons,
true,
&state.common.config,
&state.common.event_loop_handle,
&state.common.xdg_activation_state,
false,
);
if let Some((grab, focus)) = res {
if grab.is_touch_grab() {
seat.get_touch().unwrap().set_grab(state, grab, serial);
} else {
seat.get_pointer()
.unwrap()
.set_grab(state, grab, serial, focus);
}
}
});
}
}
}
}
pub fn min_size(&self) -> Option<Size<i32, Logical>> {
self.surfaces()
.fold(None, |min_size, window| {
let win_min_size = window.min_size_without_ssd();
match (min_size, win_min_size) {
(None, None) => None,
(None, x) | (x, None) => x,
(Some(min1), Some(min2)) => {
Some((min1.w.max(min2.w), min1.h.max(min2.h)).into())
}
}
})
.map(|size| size + (0, TAB_HEIGHT).into())
}
pub fn max_size(&self) -> Option<Size<i32, Logical>> {
let theoretical_max = self
.surfaces()
.fold(None, |max_size, window| {
let win_max_size = window.max_size_without_ssd();
match (max_size, win_max_size) {
(None, None) => None,
(None, x) | (x, None) => x,
(Some(max1), Some(max2)) => Some(
(
if max1.w == 0 {
max2.w
} else if max2.w == 0 {
max1.w
} else {
max1.w.min(max2.w)
},
if max1.h == 0 {
max2.h
} else if max2.h == 0 {
max1.h
} else {
max1.h.min(max2.h)
},
)
.into(),
),
}
})
.map(|size| size + (0, TAB_HEIGHT).into());
// The problem is, with accumulated sizes, the minimum size could be larger than our maximum...
let min_size = self.min_size();
match (theoretical_max, min_size) {
(None, _) => None,
(Some(max), None) => Some(max),
(Some(max), Some(min)) => Some((max.w.max(min.w), max.h.max(min.h)).into()),
}
}
}
2023-06-08 21:50:25 +02:00
#[derive(Debug, Clone, Copy)]
pub enum Message {
DragStart,
2023-12-07 19:53:41 +00:00
Menu,
2023-12-12 15:35:23 +00:00
TabMenu(usize),
2023-07-28 19:18:14 +02:00
PotentialTabDragStart(usize),
Activate(usize),
2023-06-08 21:50:25 +02:00
Close(usize),
ScrollForward,
2023-06-13 18:32:04 +02:00
ScrollBack,
Scrolled,
}
impl TabMessage for Message {
fn activate(idx: usize) -> Self {
Message::Activate(idx)
}
fn scroll_back() -> Self {
Message::ScrollBack
}
fn scroll_further() -> Self {
Message::ScrollForward
}
fn populate_scroll(&mut self, mut current_offset: AbsoluteOffset) -> Option<AbsoluteOffset> {
match self {
Message::ScrollBack => Some({
current_offset.x -= 10.;
current_offset
}),
Message::ScrollForward => Some({
current_offset.x += 10.;
current_offset
}),
_ => None,
}
}
fn scrolled() -> Self {
Message::Scrolled
}
2023-06-08 21:50:25 +02:00
}
impl Program for CosmicStackInternal {
2023-06-08 21:50:25 +02:00
type Message = Message;
fn update(
&mut self,
message: Self::Message,
2023-09-29 21:33:16 +02:00
loop_handle: &LoopHandle<'static, crate::state::State>,
2024-10-24 00:15:05 +02:00
) -> Task<Self::Message> {
2023-06-08 21:50:25 +02:00
match message {
Message::DragStart => {
if let Some((seat, serial)) = self.last_seat.lock().unwrap().clone() {
2023-12-11 16:14:00 +00:00
let active = self.active.load(Ordering::SeqCst);
if let Some(surface) = self.windows.lock().unwrap()[active]
.wl_surface()
.map(Cow::into_owned)
{
2023-09-29 21:33:16 +02:00
loop_handle.insert_idle(move |state| {
2024-04-10 15:49:08 +02:00
let res = state.common.shell.write().unwrap().move_request(
&surface,
&seat,
serial,
ReleaseMode::NoMouseButtons,
false,
2024-04-10 15:49:08 +02:00
&state.common.config,
&state.common.event_loop_handle,
&state.common.xdg_activation_state,
false,
);
2024-04-10 15:49:08 +02:00
if let Some((grab, focus)) = res {
if grab.is_touch_grab() {
seat.get_touch().unwrap().set_grab(state, grab, serial);
} else {
seat.get_pointer()
.unwrap()
.set_grab(state, grab, serial, focus);
}
}
2023-06-08 21:50:25 +02:00
});
}
}
}
2023-07-28 19:18:14 +02:00
Message::PotentialTabDragStart(idx) => {
*self.potential_drag.lock().unwrap() = Some(idx);
}
Message::Activate(idx) => {
2023-07-28 19:18:14 +02:00
*self.potential_drag.lock().unwrap() = None;
if let Some(surface) = self.windows.lock().unwrap().get(idx).cloned() {
loop_handle.insert_idle(move |state| {
2024-04-10 15:49:08 +02:00
if let Some(mapped) = state
.common
.shell
.read()
.unwrap()
.element_for_surface(&surface)
{
mapped.stack_ref().unwrap().set_active(&surface);
}
});
self.scroll_to_focus.store(true, Ordering::SeqCst);
2023-06-13 18:32:04 +02:00
}
2023-06-08 21:50:25 +02:00
}
Message::Close(idx) => {
if let Some(val) = self.windows.lock().unwrap().get(idx) {
val.close()
}
}
Message::Scrolled => {
self.scroll_to_focus.store(false, Ordering::SeqCst);
2023-06-13 18:32:04 +02:00
}
2023-12-07 19:53:41 +00:00
Message::Menu => {
if let Some((seat, serial)) = self.last_seat.lock().unwrap().clone() {
2023-12-11 16:14:00 +00:00
let active = self.active.load(Ordering::SeqCst);
if let Some(surface) = self.windows.lock().unwrap()[active]
.wl_surface()
.map(Cow::into_owned)
{
2023-12-07 19:53:41 +00:00
loop_handle.insert_idle(move |state| {
2024-04-10 15:49:08 +02:00
let shell = state.common.shell.read().unwrap();
if let Some(mapped) = shell.element_for_surface(&surface).cloned() {
2023-12-20 20:19:42 +00:00
let position = if let Some((output, set)) =
2024-04-10 15:49:08 +02:00
shell.workspaces.sets.iter().find(|(_, set)| {
2023-12-20 20:19:42 +00:00
set.sticky_layer.mapped().any(|m| m == &mapped)
}) {
set.sticky_layer
2023-12-07 19:53:41 +00:00
.element_geometry(&mapped)
.unwrap()
.loc
2023-12-20 20:19:42 +00:00
.to_global(output)
2024-04-10 15:49:08 +02:00
} else if let Some(workspace) = shell.space_for(&mapped) {
2024-02-23 17:25:40 +01:00
let Some(elem_geo) = workspace.element_geometry(&mapped) else {
return;
};
elem_geo.loc.to_global(&workspace.output)
2023-12-20 20:19:42 +00:00
} else {
return;
};
let mut cursor = seat
.get_pointer()
.unwrap()
.current_location()
.to_i32_round();
cursor.y -= TAB_HEIGHT;
2024-04-10 15:49:08 +02:00
let res = shell.menu_request(
&surface,
&seat,
serial,
cursor - position.as_logical(),
true,
2024-04-10 15:49:08 +02:00
&state.common.config,
&state.common.event_loop_handle,
);
2024-04-10 15:49:08 +02:00
std::mem::drop(shell);
if let Some((grab, focus)) = res {
seat.get_pointer()
.unwrap()
.set_grab(state, grab, serial, focus);
}
2023-12-12 15:35:23 +00:00
}
});
}
}
}
Message::TabMenu(idx) => {
if let Some((seat, serial)) = self.last_seat.lock().unwrap().clone() {
if let Some(surface) = self.windows.lock().unwrap()[idx]
.wl_surface()
.map(Cow::into_owned)
{
2023-12-12 15:35:23 +00:00
loop_handle.insert_idle(move |state| {
2024-04-10 15:49:08 +02:00
let shell = state.common.shell.read().unwrap();
if let Some(mapped) = shell.element_for_surface(&surface).cloned() {
if let Some(workspace) = shell.space_for(&mapped) {
2024-02-23 17:25:40 +01:00
let Some(elem_geo) = workspace.element_geometry(&mapped) else {
return;
};
let position = elem_geo.loc.to_global(&workspace.output);
2024-04-10 15:49:08 +02:00
2023-12-12 15:35:23 +00:00
let mut cursor = seat
.get_pointer()
.unwrap()
.current_location()
.to_i32_round();
cursor.y -= TAB_HEIGHT;
2024-04-10 15:49:08 +02:00
let res = shell.menu_request(
2023-12-12 15:35:23 +00:00
&surface,
&seat,
serial,
cursor - position.as_logical(),
false,
2024-04-10 15:49:08 +02:00
&state.common.config,
&state.common.event_loop_handle,
2023-12-07 19:53:41 +00:00
);
2024-04-10 15:49:08 +02:00
std::mem::drop(shell);
if let Some((grab, focus)) = res {
seat.get_pointer()
.unwrap()
.set_grab(state, grab, serial, focus);
}
2023-12-07 19:53:41 +00:00
}
}
});
}
}
}
_ => unreachable!(),
2023-06-08 21:50:25 +02:00
}
2024-10-24 00:15:05 +02:00
Task::none()
2023-06-08 21:50:25 +02:00
}
2023-06-08 13:19:30 +02:00
fn view(&self) -> CosmicElement<'_, Self::Message> {
let windows = self.windows.lock().unwrap();
2023-09-12 17:48:13 +02:00
if self.geometry.lock().unwrap().is_none() {
2023-06-08 13:19:30 +02:00
return iced_widget::row(Vec::new()).into();
};
let active = self.active.load(Ordering::SeqCst);
let group_focused = self.group_focused.load(Ordering::SeqCst);
let elements = vec![
2023-10-02 19:37:23 +02:00
cosmic_widget::icon::from_name("window-stack-symbolic")
.size(16)
.prefer_svg(true)
.icon()
2024-10-24 00:15:05 +02:00
.class(if group_focused {
theme::Svg::custom(|theme| iced_widget::svg::Style {
color: Some(if theme.cosmic().is_dark {
Color::BLACK
2023-06-08 13:19:30 +02:00
} else {
Color::WHITE
}),
2023-06-08 13:19:30 +02:00
})
} else {
2023-10-02 19:37:23 +02:00
theme::Svg::Default
})
.apply(iced_widget::container)
.padding([4, 24])
2024-10-24 00:15:05 +02:00
.align_y(Alignment::Center)
.apply(iced_widget::mouse_area)
.on_press(Message::DragStart)
2023-12-07 19:53:41 +00:00
.on_right_press(Message::Menu)
2023-06-08 13:19:30 +02:00
.into(),
CosmicElement::new(
Tabs::new(
2023-06-26 21:05:31 +02:00
windows.iter().enumerate().map(|(i, w)| {
let user_data = w.user_data();
user_data.insert_if_missing(Id::unique);
Tab::new(
w.title(),
w.app_id(),
user_data.get::<Id>().unwrap().clone(),
)
2023-07-28 19:18:14 +02:00
.on_press(Message::PotentialTabDragStart(i))
2023-12-12 15:35:23 +00:00
.on_right_click(Message::TabMenu(i))
2023-06-26 21:05:31 +02:00
.on_close(Message::Close(i))
}),
active,
windows[active].is_activated(false),
group_focused,
)
.id(SCROLLABLE_ID.clone())
.force_visible(
self.scroll_to_focus
.load(Ordering::SeqCst)
.then_some(active),
)
.height(Length::Fill)
.width(Length::Fill),
),
2024-10-24 00:15:05 +02:00
iced_widget::horizontal_space()
.width(Length::Fixed(0.0))
2023-07-26 17:13:22 +02:00
.apply(iced_widget::container)
.padding([64, 24])
2023-06-08 21:50:25 +02:00
.apply(iced_widget::mouse_area)
.on_press(Message::DragStart)
2023-12-07 19:53:41 +00:00
.on_right_press(Message::Menu)
2023-06-08 21:50:25 +02:00
.into(),
];
2023-06-08 13:19:30 +02:00
2024-08-02 20:48:33 +02:00
let radius = if windows[active].is_maximized(false) {
Radius::from(0.0)
} else {
Radius::from([8.0, 8.0, 0.0, 0.0])
};
let group_focused = self.group_focused.load(Ordering::SeqCst);
2023-06-08 13:19:30 +02:00
iced_widget::row(elements)
.height(TAB_HEIGHT as u16)
2024-10-24 00:15:05 +02:00
.width(Length::Fill)
2023-06-08 13:19:30 +02:00
.apply(iced_widget::container)
2024-10-24 00:15:05 +02:00
.align_y(Alignment::Center)
.class(theme::Container::custom(move |theme| {
2024-08-02 20:48:33 +02:00
let background = if group_focused {
Some(Background::Color(theme.cosmic().accent_color().into()))
} else {
Some(Background::Color(tab::primary_container_color(
theme.cosmic(),
)))
};
2024-10-24 00:15:05 +02:00
iced_widget::container::Style {
2023-10-02 19:37:23 +02:00
icon_color: Some(Color::from(theme.cosmic().background.on)),
text_color: Some(Color::from(theme.cosmic().background.on)),
2024-08-02 20:48:33 +02:00
background,
2024-02-09 16:48:03 -08:00
border: Border {
2024-08-02 20:48:33 +02:00
radius,
2024-02-09 16:48:03 -08:00
width: 0.0,
color: Color::TRANSPARENT,
},
shadow: Default::default(),
2024-08-02 20:48:33 +02:00
}
}))
2023-06-08 13:19:30 +02:00
.into()
2022-11-03 18:51:27 +01:00
}
2023-05-31 20:02:39 +02:00
2023-06-08 13:19:30 +02:00
fn foreground(
&self,
pixels: &mut tiny_skia::PixmapMut<'_>,
damage: &[Rectangle<i32, Buffer>],
2023-06-08 13:19:30 +02:00
scale: f32,
2024-07-26 14:59:11 +02:00
theme: &Theme,
2023-06-08 13:19:30 +02:00
) {
if self.group_focused.load(Ordering::SeqCst) {
let border = Rectangle::from_loc_and_size(
(0, ((TAB_HEIGHT as f32 * scale) - scale).floor() as i32),
(pixels.width() as i32, scale.ceil() as i32),
2023-06-08 13:19:30 +02:00
);
let mut paint = tiny_skia::Paint::default();
2024-07-26 14:59:11 +02:00
let (b, g, r, a) = theme.cosmic().accent_color().into_components();
2023-06-08 13:19:30 +02:00
paint.set_color(tiny_skia::Color::from_rgba(r, g, b, a).unwrap());
for rect in damage {
if let Some(overlap) = rect.intersection(border) {
pixels.fill_rect(
tiny_skia::Rect::from_xywh(
overlap.loc.x as f32,
overlap.loc.y as f32,
overlap.size.w as f32,
overlap.size.h as f32,
)
.unwrap(),
&paint,
Default::default(),
None,
2023-06-08 13:19:30 +02:00
)
}
}
2023-05-31 20:02:39 +02:00
}
}
2022-09-28 12:01:29 +02:00
}
impl IsAlive for CosmicStack {
fn alive(&self) -> bool {
2023-07-28 19:18:14 +02:00
self.0.with_program(|p| {
p.override_alive.load(Ordering::SeqCst)
&& p.windows.lock().unwrap().iter().any(IsAlive::alive)
})
2022-09-28 12:01:29 +02:00
}
}
impl SpaceElement for CosmicStack {
fn bbox(&self) -> Rectangle<i32, Logical> {
self.0.with_program(|p| {
let mut bbox =
SpaceElement::bbox(&p.windows.lock().unwrap()[p.active.load(Ordering::SeqCst)]);
2024-02-29 13:12:57 +01:00
bbox.loc -= Point::from((RESIZE_BORDER, RESIZE_BORDER));
bbox.size += Size::from((RESIZE_BORDER * 2, RESIZE_BORDER * 2));
bbox.size.h += TAB_HEIGHT;
bbox
})
2022-09-28 12:01:29 +02:00
}
fn is_in_input_region(&self, point: &Point<f64, Logical>) -> bool {
2024-10-10 23:18:04 +02:00
self.focus_under(*point, WindowSurfaceType::ALL).is_some()
2022-09-28 12:01:29 +02:00
}
fn set_activate(&self, activated: bool) {
SpaceElement::set_activate(&self.0, activated);
2024-03-22 18:22:58 +01:00
let changed = self.0.with_program(|p| {
2023-06-08 13:19:30 +02:00
if !p.group_focused.load(Ordering::SeqCst) {
p.windows
.lock()
.unwrap()
.iter()
.for_each(|w| SpaceElement::set_activate(w, activated))
}
2024-03-22 18:22:58 +01:00
p.activated.swap(activated, Ordering::SeqCst) != activated
2023-06-08 13:19:30 +02:00
});
2024-03-22 18:22:58 +01:00
if changed {
self.0.force_redraw();
}
2022-09-28 12:01:29 +02:00
}
fn output_enter(&self, output: &Output, overlap: Rectangle<i32, Logical>) {
SpaceElement::output_enter(&self.0, output, overlap);
self.0.with_program(|p| {
p.windows
.lock()
.unwrap()
.iter()
.for_each(|w| SpaceElement::output_enter(w, output, overlap))
})
2022-09-28 12:01:29 +02:00
}
fn output_leave(&self, output: &Output) {
SpaceElement::output_leave(&self.0, output);
self.0.with_program(|p| {
p.windows
.lock()
.unwrap()
.iter()
.for_each(|w| SpaceElement::output_leave(w, output))
})
2022-09-28 12:01:29 +02:00
}
fn geometry(&self) -> Rectangle<i32, Logical> {
self.0.with_program(|p| {
let mut geo =
SpaceElement::geometry(&p.windows.lock().unwrap()[p.active.load(Ordering::SeqCst)]);
geo.size.h += TAB_HEIGHT;
geo
})
2022-09-28 12:01:29 +02:00
}
fn z_index(&self) -> u8 {
self.0.with_program(|p| {
SpaceElement::z_index(&p.windows.lock().unwrap()[p.active.load(Ordering::SeqCst)])
})
2022-09-28 12:01:29 +02:00
}
fn refresh(&self) {
2023-06-08 13:19:30 +02:00
SpaceElement::refresh(&self.0);
self.0.with_program(|p| {
let mut windows = p.windows.lock().unwrap();
2023-06-05 17:52:47 +02:00
// don't let the stack become empty
let old_active = p.active.load(Ordering::SeqCst);
let active = windows[old_active].clone();
2023-06-05 17:52:47 +02:00
windows.retain(IsAlive::alive);
if windows.is_empty() {
windows.push(active);
}
let len = windows.len();
let _ = p
.active
.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |active| {
2023-06-05 17:52:47 +02:00
(active >= len).then_some(len - 1)
});
2024-02-27 13:43:35 +01:00
let active = p.active.load(Ordering::SeqCst);
if old_active != active {
self.0
.resize(Size::from((self.active().geometry().size.w, TAB_HEIGHT)));
self.0.force_redraw();
}
2024-02-27 13:43:35 +01:00
windows.iter().enumerate().for_each(|(i, w)| {
if i == active {
w.set_suspended(false);
} else {
w.set_suspended(true);
}
w.send_configure();
SpaceElement::refresh(w)
});
2023-06-08 13:19:30 +02:00
});
2022-09-28 12:01:29 +02:00
}
}
impl KeyboardTarget<State> for CosmicStack {
fn enter(
&self,
seat: &Seat<State>,
data: &mut State,
keys: Vec<KeysymHandle<'_>>,
serial: Serial,
) {
self.0.with_program(|p| {
let active = p.active.load(Ordering::SeqCst);
p.previous_keyboard.store(active, Ordering::SeqCst);
KeyboardTarget::enter(
&p.windows.lock().unwrap()[p.active.load(Ordering::SeqCst)],
seat,
data,
keys,
serial,
)
})
2022-09-28 12:01:29 +02:00
}
fn leave(&self, seat: &Seat<State>, data: &mut State, serial: Serial) {
2022-11-03 18:51:27 +01:00
let active = self.keyboard_leave_if_previous(seat, data, serial);
2023-06-08 13:19:30 +02:00
self.0.force_redraw();
self.0.with_program(|p| {
2023-06-08 13:19:30 +02:00
p.group_focused.store(false, Ordering::SeqCst);
KeyboardTarget::leave(&p.windows.lock().unwrap()[active], seat, data, serial)
})
2022-09-28 12:01:29 +02:00
}
fn key(
&self,
seat: &Seat<State>,
data: &mut State,
key: KeysymHandle<'_>,
state: KeyState,
serial: Serial,
time: u32,
) {
2022-11-03 18:51:27 +01:00
let active = self.keyboard_leave_if_previous(seat, data, serial);
self.0.with_program(|p| {
2023-06-08 13:19:30 +02:00
if !p.group_focused.load(Ordering::SeqCst) {
KeyboardTarget::key(
&p.windows.lock().unwrap()[active],
seat,
data,
key,
state,
serial,
time,
)
}
})
2022-09-28 12:01:29 +02:00
}
fn modifiers(
&self,
seat: &Seat<State>,
data: &mut State,
modifiers: ModifiersState,
serial: Serial,
) {
2022-11-03 18:51:27 +01:00
let active = self.keyboard_leave_if_previous(seat, data, serial);
self.0.with_program(|p| {
2023-06-08 13:19:30 +02:00
if !p.group_focused.load(Ordering::SeqCst) {
KeyboardTarget::modifiers(
&p.windows.lock().unwrap()[active],
seat,
data,
modifiers,
serial,
)
}
})
2022-09-28 12:01:29 +02:00
}
}
impl PointerTarget<State> for CosmicStack {
fn enter(&self, seat: &Seat<State>, data: &mut State, event: &MotionEvent) {
2023-06-08 21:50:25 +02:00
let mut event = event.clone();
self.0.with_program(|p| {
2023-06-08 21:50:25 +02:00
let active_window = &p.windows.lock().unwrap()[p.active.load(Ordering::SeqCst)];
let Some(next) = Focus::under(active_window, TAB_HEIGHT, event.location) else {
return;
2024-02-29 13:12:57 +01:00
};
let _old_focus = p.swap_focus(Some(next));
2024-02-29 13:12:57 +01:00
let mut cursor_state = seat
2024-02-29 13:12:57 +01:00
.user_data()
.get::<CursorState>()
.unwrap()
.lock()
2024-02-29 13:12:57 +01:00
.unwrap();
cursor_state.set_shape(next.cursor_shape());
let cursor_status = seat.user_data().get::<Mutex<CursorImageStatus>>().unwrap();
*cursor_status.lock().unwrap() = CursorImageStatus::default_named();
});
event.location -= self.0.with_program(|p| {
p.windows.lock().unwrap()[p.active.load(Ordering::SeqCst)]
.geometry()
.loc
.to_f64()
});
PointerTarget::enter(&self.0, seat, data, &event)
2022-09-28 12:01:29 +02:00
}
2022-09-28 12:01:29 +02:00
fn motion(&self, seat: &Seat<State>, data: &mut State, event: &MotionEvent) {
2023-06-08 21:50:25 +02:00
let mut event = event.clone();
self.0.with_program(|p| {
2024-03-26 13:55:18 +01:00
let active = p.active.load(Ordering::SeqCst);
2023-06-08 21:50:25 +02:00
let active_window = &p.windows.lock().unwrap()[active];
let Some(next) = Focus::under(active_window, TAB_HEIGHT, event.location) else {
return;
2024-02-29 13:12:57 +01:00
};
let _previous = p.swap_focus(Some(next));
2024-02-29 13:12:57 +01:00
let mut cursor_state = seat
2024-02-29 13:12:57 +01:00
.user_data()
.get::<CursorState>()
.unwrap()
.lock()
2024-02-29 13:12:57 +01:00
.unwrap();
cursor_state.set_shape(next.cursor_shape());
let cursor_status = seat.user_data().get::<Mutex<CursorImageStatus>>().unwrap();
*cursor_status.lock().unwrap() = CursorImageStatus::default_named();
2024-02-29 13:12:57 +01:00
});
2024-03-26 13:55:18 +01:00
let active_window_geo = self.0.with_program(|p| {
p.windows.lock().unwrap()[p.active.load(Ordering::SeqCst)].geometry()
});
2024-02-29 13:12:57 +01:00
event.location -= active_window_geo.loc.to_f64();
PointerTarget::motion(&self.0, seat, data, &event);
if event.location.y < 0.0
|| event.location.y > TAB_HEIGHT as f64
|| event.location.x < 64.0
|| event.location.x > (active_window_geo.size.w as f64 - 64.0)
{
self.start_drag(data, seat, event.serial);
2022-11-03 18:51:27 +01:00
}
2022-09-28 12:01:29 +02:00
}
fn relative_motion(
&self,
_seat: &Seat<State>,
_data: &mut State,
_event: &RelativeMotionEvent,
) {
}
2022-09-28 12:01:29 +02:00
fn button(&self, seat: &Seat<State>, data: &mut State, event: &ButtonEvent) {
match self.0.with_program(|p| p.current_focus()) {
Some(Focus::Header) => {
2023-06-08 21:50:25 +02:00
self.0.with_program(|p| {
*p.last_seat.lock().unwrap() = Some((seat.clone(), event.serial));
});
PointerTarget::button(&self.0, seat, data, event)
}
Some(x) => {
2024-02-29 13:12:57 +01:00
let serial = event.serial;
let seat = seat.clone();
let Some(surface) = self.0.with_program(|p| {
let window = &p.windows.lock().unwrap()[p.active.load(Ordering::SeqCst)];
window.wl_surface().map(Cow::into_owned)
2024-02-29 13:12:57 +01:00
}) else {
return;
};
self.0.loop_handle().insert_idle(move |state| {
2024-04-10 15:49:08 +02:00
let res = state.common.shell.write().unwrap().resize_request(
2024-02-29 13:12:57 +01:00
&surface,
&seat,
serial,
match x {
Focus::ResizeTop => ResizeEdge::TOP,
Focus::ResizeTopLeft => ResizeEdge::TOP_LEFT,
Focus::ResizeTopRight => ResizeEdge::TOP_RIGHT,
Focus::ResizeBottom => ResizeEdge::BOTTOM,
Focus::ResizeBottomLeft => ResizeEdge::BOTTOM_LEFT,
Focus::ResizeBottomRight => ResizeEdge::BOTTOM_RIGHT,
Focus::ResizeLeft => ResizeEdge::LEFT,
Focus::ResizeRight => ResizeEdge::RIGHT,
Focus::Header => unreachable!(),
2024-02-29 13:12:57 +01:00
},
false,
2024-04-10 15:49:08 +02:00
);
if let Some((grab, focus)) = res {
if grab.is_touch_grab() {
seat.get_touch().unwrap().set_grab(state, grab, serial);
} else {
seat.get_pointer()
.unwrap()
.set_grab(state, grab, serial, focus);
}
}
2024-02-29 13:12:57 +01:00
});
}
None => {}
}
2022-09-28 12:01:29 +02:00
}
2022-09-28 12:01:29 +02:00
fn axis(&self, seat: &Seat<State>, data: &mut State, frame: AxisFrame) {
match self.0.with_program(|p| p.current_focus()) {
Some(Focus::Header) => PointerTarget::axis(&self.0, seat, data, frame),
_ => {}
}
2022-09-28 12:01:29 +02:00
}
fn frame(&self, seat: &Seat<State>, data: &mut State) {
match self.0.with_program(|p| p.current_focus()) {
Some(Focus::Header) => PointerTarget::frame(&self.0, seat, data),
_ => {}
}
}
2022-09-28 12:01:29 +02:00
fn leave(&self, seat: &Seat<State>, data: &mut State, serial: Serial, time: u32) {
self.0.with_program(|p| {
let mut cursor_state = seat
.user_data()
.get::<CursorState>()
.unwrap()
.lock()
.unwrap();
2024-09-09 16:21:27 +02:00
cursor_state.unset_shape();
let _previous = p.swap_focus(None);
});
PointerTarget::leave(&self.0, seat, data, serial, time);
if let Some(dragged_out) = self
.0
.with_program(|p| p.potential_drag.lock().unwrap().take())
{
if let Some(surface) = self
.0
.with_program(|p| p.windows.lock().unwrap().get(dragged_out).cloned())
{
let seat = seat.clone();
surface.try_force_undecorated(false);
surface.send_configure();
if let Some(surface) = surface.wl_surface().map(Cow::into_owned) {
let _ = data.common.event_loop_handle.insert_idle(move |state| {
2024-04-10 15:49:08 +02:00
let res = state.common.shell.write().unwrap().move_request(
&surface,
&seat,
2024-04-10 15:49:08 +02:00
serial,
ReleaseMode::NoMouseButtons,
true,
2024-04-10 15:49:08 +02:00
&state.common.config,
&state.common.event_loop_handle,
&state.common.xdg_activation_state,
false,
2024-04-10 15:49:08 +02:00
);
if let Some((grab, focus)) = res {
if grab.is_touch_grab() {
seat.get_touch().unwrap().set_grab(state, grab, serial);
} else {
seat.get_pointer()
.unwrap()
.set_grab(state, grab, serial, focus);
}
}
});
}
}
}
2022-09-28 12:01:29 +02:00
}
fn gesture_swipe_begin(
&self,
_seat: &Seat<State>,
_data: &mut State,
_event: &GestureSwipeBeginEvent,
) {
}
fn gesture_swipe_update(
&self,
_seat: &Seat<State>,
_data: &mut State,
_event: &GestureSwipeUpdateEvent,
) {
}
fn gesture_swipe_end(
&self,
_seat: &Seat<State>,
_data: &mut State,
_event: &GestureSwipeEndEvent,
) {
}
fn gesture_pinch_begin(
&self,
_seat: &Seat<State>,
_data: &mut State,
_event: &GesturePinchBeginEvent,
) {
}
fn gesture_pinch_update(
&self,
_seat: &Seat<State>,
_data: &mut State,
_event: &GesturePinchUpdateEvent,
) {
}
fn gesture_pinch_end(
&self,
_seat: &Seat<State>,
_data: &mut State,
_event: &GesturePinchEndEvent,
) {
}
fn gesture_hold_begin(
&self,
_seat: &Seat<State>,
_data: &mut State,
_event: &GestureHoldBeginEvent,
) {
}
fn gesture_hold_end(
&self,
_seat: &Seat<State>,
_data: &mut State,
_event: &GestureHoldEndEvent,
) {
}
2022-09-28 12:01:29 +02:00
}
2024-03-26 15:20:40 -07:00
impl TouchTarget<State> for CosmicStack {
fn down(&self, seat: &Seat<State>, data: &mut State, event: &DownEvent, seq: Serial) {
let mut event = event.clone();
let active_window_geo = self.0.with_program(|p| {
*p.last_seat.lock().unwrap() = Some((seat.clone(), event.serial));
2024-03-26 15:20:40 -07:00
p.windows.lock().unwrap()[p.active.load(Ordering::SeqCst)].geometry()
});
event.location -= active_window_geo.loc.to_f64();
TouchTarget::down(&self.0, seat, data, &event, seq)
}
fn up(&self, seat: &Seat<State>, data: &mut State, event: &UpEvent, seq: Serial) {
TouchTarget::up(&self.0, seat, data, &event, seq)
}
fn motion(&self, seat: &Seat<State>, data: &mut State, event: &TouchMotionEvent, seq: Serial) {
let mut event = event.clone();
let active_window_geo = self.0.with_program(|p| {
p.windows.lock().unwrap()[p.active.load(Ordering::SeqCst)].geometry()
});
event.location -= active_window_geo.loc.to_f64();
TouchTarget::motion(&self.0, seat, data, &event, seq);
if event.location.y < 0.0
|| event.location.y > TAB_HEIGHT as f64
|| event.location.x < 64.0
|| event.location.x > (active_window_geo.size.w as f64 - 64.0)
{
self.start_drag(data, seat, seq);
}
2024-03-26 15:20:40 -07:00
}
fn frame(&self, seat: &Seat<State>, data: &mut State, seq: Serial) {
TouchTarget::frame(&self.0, seat, data, seq)
}
fn cancel(&self, seat: &Seat<State>, data: &mut State, seq: Serial) {
TouchTarget::cancel(&self.0, seat, data, seq)
}
fn shape(&self, seat: &Seat<State>, data: &mut State, event: &ShapeEvent, seq: Serial) {
TouchTarget::shape(&self.0, seat, data, event, seq)
}
fn orientation(
&self,
_seat: &Seat<State>,
_data: &mut State,
_event: &OrientationEvent,
_seq: Serial,
) {
}
}
2023-06-08 21:50:16 +02:00
render_elements! {
pub CosmicStackRenderElement<R> where R: ImportAll + ImportMem;
Header = MemoryRenderBufferRenderElement<R>,
Window = WaylandSurfaceRenderElement<R>,
2022-09-28 12:01:29 +02:00
}