forge
Explore
Log in

Don't you dare go Hollow

Add workspace switch animations

Browse files at this commit

b5c02253ff893c1679aebffb06aafc5aa9db6b58Ivan MolodetskikhParent: ec59533
src/layout.rsmodified+118 -26
@@ -34,6 +34,9 @@ use std::mem;
34 use std::time::Duration;34 use std::time::Duration;
35 35
36 use smithay::backend::renderer::element::surface::WaylandSurfaceRenderElement;36 use smithay::backend::renderer::element::surface::WaylandSurfaceRenderElement;
37+use smithay::backend::renderer::element::utils::{
38+ CropRenderElement, Relocate, RelocateRenderElement,
39+};
37 use smithay::backend::renderer::element::AsRenderElements;40 use smithay::backend::renderer::element::AsRenderElements;
38 use smithay::backend::renderer::gles::GlesRenderer;41 use smithay::backend::renderer::gles::GlesRenderer;
39 use smithay::desktop::space::SpaceElement;42 use smithay::desktop::space::SpaceElement;
@@ -52,7 +55,8 @@ const PADDING: i32 = 16;
52 pub struct OutputId(String);55 pub struct OutputId(String);
53 56
54 pub type WorkspaceRenderElement<R> = WaylandSurfaceRenderElement<R>;57 pub type WorkspaceRenderElement<R> = WaylandSurfaceRenderElement<R>;
55-pub type MonitorRenderElement<R> = WorkspaceRenderElement<R>;58+pub type MonitorRenderElement<R> =
59+ RelocateRenderElement<CropRenderElement<WorkspaceRenderElement<R>>>;
56 60
57 pub trait LayoutElement: SpaceElement + PartialEq + Clone {61 pub trait LayoutElement: SpaceElement + PartialEq + Clone {
58 fn request_size(&self, size: Size<i32, Logical>);62 fn request_size(&self, size: Size<i32, Logical>);
@@ -92,6 +96,8 @@ pub struct Monitor<W: LayoutElement> {
92 workspaces: Vec<Workspace<W>>,96 workspaces: Vec<Workspace<W>>,
93 /// Index of the currently active workspace.97 /// Index of the currently active workspace.
94 active_workspace_idx: usize,98 active_workspace_idx: usize,
99+ /// Animation for workspace switching.
100+ workspace_idx_anim: Option<Animation>,
95 }101 }
96 102
97 #[derive(Debug)]103 #[derive(Debug)]
@@ -237,11 +243,7 @@ impl<W: LayoutElement> MonitorSet<W> {
237 ws.set_output(Some(output.clone()));243 ws.set_output(Some(output.clone()));
238 }244 }
239 245
240- monitors.push(Monitor {246+ monitors.push(Monitor::new(output, workspaces));
241- output,
242- workspaces,
243- active_workspace_idx: 0,
244- });
245 MonitorSet::Normal {247 MonitorSet::Normal {
246 monitors,248 monitors,
247 primary_idx,249 primary_idx,
@@ -256,11 +258,7 @@ impl<W: LayoutElement> MonitorSet<W> {
256 workspace.set_output(Some(output.clone()));258 workspace.set_output(Some(output.clone()));
257 }259 }
258 260
259- let monitor = Monitor {261+ let monitor = Monitor::new(output, workspaces);
260- output,
261- workspaces,
262- active_workspace_idx: 0,
263- };
264 MonitorSet::Normal {262 MonitorSet::Normal {
265 monitors: vec![monitor],263 monitors: vec![monitor],
266 primary_idx: 0,264 primary_idx: 0,
@@ -481,7 +479,8 @@ impl<W: LayoutElement> MonitorSet<W> {
481 for (workspace_idx, ws) in mon.workspaces.iter_mut().enumerate() {479 for (workspace_idx, ws) in mon.workspaces.iter_mut().enumerate() {
482 if ws.has_window(window) {480 if ws.has_window(window) {
483 *active_monitor_idx = monitor_idx;481 *active_monitor_idx = monitor_idx;
484- mon.active_workspace_idx = workspace_idx;482+ // TODO
483+ assert_eq!(mon.active_workspace_idx, workspace_idx);
485 ws.activate_window(window);484 ws.activate_window(window);
486 break;485 break;
487 }486 }
@@ -757,17 +756,42 @@ impl<W: LayoutElement> Default for MonitorSet<W> {
757 }756 }
758 757
759 impl<W: LayoutElement> Monitor<W> {758 impl<W: LayoutElement> Monitor<W> {
759+ fn new(output: Output, workspaces: Vec<Workspace<W>>) -> Self {
760+ Self {
761+ output,
762+ workspaces,
763+ active_workspace_idx: 0,
764+ workspace_idx_anim: None,
765+ }
766+ }
767+
760 fn active_workspace(&mut self) -> &mut Workspace<W> {768 fn active_workspace(&mut self) -> &mut Workspace<W> {
761 &mut self.workspaces[self.active_workspace_idx]769 &mut self.workspaces[self.active_workspace_idx]
762 }770 }
763 771
772+ fn activate_workspace(&mut self, idx: usize) {
773+ if self.active_workspace_idx == idx {
774+ return;
775+ }
776+
777+ let current_idx = self
778+ .workspace_idx_anim
779+ .as_ref()
780+ .map(|anim| anim.value())
781+ .unwrap_or(self.active_workspace_idx as f64);
782+
783+ self.active_workspace_idx = idx;
784+
785+ self.workspace_idx_anim = Some(Animation::new(
786+ current_idx,
787+ idx as f64,
788+ Duration::from_millis(250),
789+ ));
790+ }
791+
764 pub fn add_window(&mut self, workspace_idx: usize, window: W, activate: bool) {792 pub fn add_window(&mut self, workspace_idx: usize, window: W, activate: bool) {
765 let workspace = &mut self.workspaces[workspace_idx];793 let workspace = &mut self.workspaces[workspace_idx];
766 794
767- if activate {
768- self.active_workspace_idx = workspace_idx;
769- }
770-
771 workspace.add_window(window.clone(), activate);795 workspace.add_window(window.clone(), activate);
772 796
773 if workspace_idx == self.workspaces.len() - 1 {797 if workspace_idx == self.workspaces.len() - 1 {
@@ -775,9 +799,15 @@ impl<W: LayoutElement> Monitor<W> {
775 let ws = Workspace::new(self.output.clone());799 let ws = Workspace::new(self.output.clone());
776 self.workspaces.push(ws);800 self.workspaces.push(ws);
777 }801 }
802+
803+ if activate {
804+ self.activate_workspace(workspace_idx);
805+ }
778 }806 }
779 807
780 fn clean_up_workspaces(&mut self) {808 fn clean_up_workspaces(&mut self) {
809+ assert!(self.workspace_idx_anim.is_none());
810+
781 for idx in (0..self.workspaces.len() - 1).rev() {811 for idx in (0..self.workspaces.len() - 1).rev() {
782 if self.active_workspace_idx == idx {812 if self.active_workspace_idx == idx {
783 continue;813 continue;
@@ -842,8 +872,6 @@ impl<W: LayoutElement> Monitor<W> {
842 workspace.remove_window(&window);872 workspace.remove_window(&window);
843 873
844 self.add_window(new_idx, window, true);874 self.add_window(new_idx, window, true);
845-
846- self.clean_up_workspaces();
847 }875 }
848 876
849 pub fn move_to_workspace_down(&mut self) {877 pub fn move_to_workspace_down(&mut self) {
@@ -864,18 +892,17 @@ impl<W: LayoutElement> Monitor<W> {
864 workspace.remove_window(&window);892 workspace.remove_window(&window);
865 893
866 self.add_window(new_idx, window, true);894 self.add_window(new_idx, window, true);
867-
868- self.clean_up_workspaces();
869 }895 }
870 896
871 pub fn switch_workspace_up(&mut self) {897 pub fn switch_workspace_up(&mut self) {
872- self.active_workspace_idx = self.active_workspace_idx.saturating_sub(1);898+ self.activate_workspace(self.active_workspace_idx.saturating_sub(1));
873- self.clean_up_workspaces();
874 }899 }
875 900
876 pub fn switch_workspace_down(&mut self) {901 pub fn switch_workspace_down(&mut self) {
877- self.active_workspace_idx = min(self.active_workspace_idx + 1, self.workspaces.len() - 1);902+ self.activate_workspace(min(
878- self.clean_up_workspaces();903+ self.active_workspace_idx + 1,
904+ self.workspaces.len() - 1,
905+ ));
879 }906 }
880 907
881 pub fn consume_into_column(&mut self) {908 pub fn consume_into_column(&mut self) {
@@ -897,6 +924,14 @@ impl<W: LayoutElement> Monitor<W> {
897 }924 }
898 925
899 pub fn advance_animations(&mut self, current_time: Duration) {926 pub fn advance_animations(&mut self, current_time: Duration) {
927+ if let Some(anim) = &mut self.workspace_idx_anim {
928+ anim.set_current_time(current_time);
929+ if anim.is_done() {
930+ self.workspace_idx_anim = None;
931+ self.clean_up_workspaces();
932+ }
933+ }
934+
900 for ws in &mut self.workspaces {935 for ws in &mut self.workspaces {
901 ws.advance_animations(current_time);936 ws.advance_animations(current_time);
902 }937 }
@@ -907,8 +942,65 @@ impl Monitor<Window> {
907 pub fn render_elements(942 pub fn render_elements(
908 &self,943 &self,
909 renderer: &mut GlesRenderer,944 renderer: &mut GlesRenderer,
910- ) -> Vec<WorkspaceRenderElement<GlesRenderer>> {945+ ) -> Vec<MonitorRenderElement<GlesRenderer>> {
911- self.workspaces[self.active_workspace_idx].render_elements(renderer)946+ let output_transform = self.output.current_transform();
947+ let output_mode = self.output.current_mode().unwrap();
948+ let output_size = output_transform.transform_size(output_mode.size);
949+
950+ match &self.workspace_idx_anim {
951+ Some(anim) => {
952+ let render_idx = anim.value();
953+ let below_idx = render_idx.floor() as usize;
954+ let above_idx = render_idx.ceil() as usize;
955+
956+ let offset =
957+ ((render_idx - below_idx as f64) * output_size.h as f64).round() as i32;
958+
959+ let below = self.workspaces[below_idx].render_elements(renderer);
960+ let above = self.workspaces[above_idx].render_elements(renderer);
961+
962+ let below = below.into_iter().filter_map(|elem| {
963+ Some(RelocateRenderElement::from_element(
964+ CropRenderElement::from_element(
965+ elem,
966+ 1.,
967+ Rectangle::from_loc_and_size((0, 0), output_size),
968+ )?,
969+ (0, -offset),
970+ Relocate::Relative,
971+ ))
972+ });
973+ let above = above.into_iter().filter_map(|elem| {
974+ Some(RelocateRenderElement::from_element(
975+ CropRenderElement::from_element(
976+ elem,
977+ 1.,
978+ Rectangle::from_loc_and_size((0, 0), output_size),
979+ )?,
980+ (0, -offset + output_size.h),
981+ Relocate::Relative,
982+ ))
983+ });
984+ below.chain(above).collect()
985+ }
986+ None => {
987+ let elements = self.workspaces[self.active_workspace_idx].render_elements(renderer);
988+ elements
989+ .into_iter()
990+ .filter_map(|elem| {
991+ Some(RelocateRenderElement::from_element(
992+ CropRenderElement::from_element(
993+ elem,
994+ 1.,
995+ Rectangle::from_loc_and_size((0, 0), output_size),
996+ )?,
997+ (0, 0),
998+ Relocate::Relative,
999+ ))
1000+ })
1001+ .collect()
1002+ }
1003+ }
912 }1004 }
913 }1005 }
914 1006