ssd.rs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. use smithay::{
  2. backend::renderer::{
  3. element::{
  4. solid::{SolidColorBuffer, SolidColorRenderElement},
  5. AsRenderElements, Kind,
  6. },
  7. Renderer,
  8. },
  9. desktop::WindowSurface,
  10. input::Seat,
  11. utils::{Logical, Point, Serial},
  12. wayland::shell::xdg::XdgShellHandler,
  13. };
  14. use std::cell::{RefCell, RefMut};
  15. use crate::{state::Backend, CompositronState};
  16. use super::WindowElement;
  17. pub struct WindowState {
  18. pub is_ssd: bool,
  19. pub header_bar: HeaderBar,
  20. }
  21. #[derive(Debug, Clone)]
  22. pub struct HeaderBar {
  23. pub pointer_loc: Option<Point<f64, Logical>>,
  24. pub width: u32,
  25. pub close_button_hover: bool,
  26. pub maximize_button_hover: bool,
  27. pub background: SolidColorBuffer,
  28. pub close_button: SolidColorBuffer,
  29. pub maximize_button: SolidColorBuffer,
  30. }
  31. const BG_COLOR: [f32; 4] = [0.75f32, 0.9f32, 0.78f32, 1f32];
  32. const MAX_COLOR: [f32; 4] = [1f32, 0.965f32, 0.71f32, 1f32];
  33. const CLOSE_COLOR: [f32; 4] = [1f32, 0.66f32, 0.612f32, 1f32];
  34. const MAX_COLOR_HOVER: [f32; 4] = [0.71f32, 0.624f32, 0f32, 1f32];
  35. const CLOSE_COLOR_HOVER: [f32; 4] = [0.75f32, 0.11f32, 0.016f32, 1f32];
  36. pub const HEADER_BAR_HEIGHT: i32 = 32;
  37. const BUTTON_HEIGHT: u32 = HEADER_BAR_HEIGHT as u32;
  38. const BUTTON_WIDTH: u32 = 32;
  39. impl HeaderBar {
  40. pub fn pointer_enter(&mut self, loc: Point<f64, Logical>) {
  41. self.pointer_loc = Some(loc);
  42. }
  43. pub fn pointer_leave(&mut self) {
  44. self.pointer_loc = None;
  45. }
  46. pub fn clicked<BackendData: Backend>(
  47. &mut self,
  48. seat: &Seat<CompositronState<BackendData>>,
  49. state: &mut CompositronState<BackendData>,
  50. window: &WindowElement,
  51. serial: Serial,
  52. ) {
  53. match self.pointer_loc.as_ref() {
  54. Some(loc) if loc.x >= (self.width - BUTTON_WIDTH) as f64 => {
  55. match window.0.underlying_surface() {
  56. WindowSurface::Wayland(w) => w.send_close(),
  57. #[cfg(feature = "xwayland")]
  58. WindowSurface::X11(w) => {
  59. let _ = w.close();
  60. }
  61. };
  62. }
  63. Some(loc) if loc.x >= (self.width - (BUTTON_WIDTH * 2)) as f64 => {
  64. match window.0.underlying_surface() {
  65. WindowSurface::Wayland(w) => state.maximize_request(w.clone()),
  66. #[cfg(feature = "xwayland")]
  67. WindowSurface::X11(w) => {
  68. let surface = w.clone();
  69. state
  70. .handle
  71. .insert_idle(move |data| data.maximize_request_x11(&surface));
  72. }
  73. };
  74. }
  75. Some(_) => {
  76. match window.0.underlying_surface() {
  77. WindowSurface::Wayland(w) => {
  78. let seat = seat.clone();
  79. let toplevel = w.clone();
  80. state.handle.insert_idle(move |data| {
  81. data.move_request_xdg(&toplevel, &seat, serial)
  82. });
  83. }
  84. #[cfg(feature = "xwayland")]
  85. WindowSurface::X11(w) => {
  86. let window = w.clone();
  87. state
  88. .handle
  89. .insert_idle(move |data| data.move_request_x11(&window));
  90. }
  91. };
  92. }
  93. _ => {}
  94. };
  95. }
  96. pub fn touch_down<BackendData: Backend>(
  97. &mut self,
  98. seat: &Seat<CompositronState<BackendData>>,
  99. state: &mut CompositronState<BackendData>,
  100. window: &WindowElement,
  101. serial: Serial,
  102. ) {
  103. match self.pointer_loc.as_ref() {
  104. Some(loc) if loc.x >= (self.width - BUTTON_WIDTH) as f64 => {}
  105. Some(loc) if loc.x >= (self.width - (BUTTON_WIDTH * 2)) as f64 => {}
  106. Some(_) => {
  107. match window.0.underlying_surface() {
  108. WindowSurface::Wayland(w) => {
  109. let seat = seat.clone();
  110. let toplevel = w.clone();
  111. state.handle.insert_idle(move |data| {
  112. data.move_request_xdg(&toplevel, &seat, serial)
  113. });
  114. }
  115. #[cfg(feature = "xwayland")]
  116. WindowSurface::X11(w) => {
  117. let window = w.clone();
  118. state
  119. .handle
  120. .insert_idle(move |data| data.move_request_x11(&window));
  121. }
  122. };
  123. }
  124. _ => {}
  125. };
  126. }
  127. pub fn touch_up<BackendData: Backend>(
  128. &mut self,
  129. _seat: &Seat<CompositronState<BackendData>>,
  130. state: &mut CompositronState<BackendData>,
  131. window: &WindowElement,
  132. _serial: Serial,
  133. ) {
  134. match self.pointer_loc.as_ref() {
  135. Some(loc) if loc.x >= (self.width - BUTTON_WIDTH) as f64 => {
  136. match window.0.underlying_surface() {
  137. WindowSurface::Wayland(w) => w.send_close(),
  138. #[cfg(feature = "xwayland")]
  139. WindowSurface::X11(w) => {
  140. let _ = w.close();
  141. }
  142. };
  143. }
  144. Some(loc) if loc.x >= (self.width - (BUTTON_WIDTH * 2)) as f64 => {
  145. match window.0.underlying_surface() {
  146. WindowSurface::Wayland(w) => state.maximize_request(w.clone()),
  147. #[cfg(feature = "xwayland")]
  148. WindowSurface::X11(w) => {
  149. let surface = w.clone();
  150. state
  151. .handle
  152. .insert_idle(move |data| data.maximize_request_x11(&surface));
  153. }
  154. };
  155. }
  156. _ => {}
  157. };
  158. }
  159. pub fn redraw(&mut self, width: u32) {
  160. if width == 0 {
  161. self.width = 0;
  162. return;
  163. }
  164. self.background
  165. .update((width as i32, HEADER_BAR_HEIGHT), BG_COLOR);
  166. let mut needs_redraw_buttons = false;
  167. if width != self.width {
  168. needs_redraw_buttons = true;
  169. self.width = width;
  170. }
  171. if self
  172. .pointer_loc
  173. .as_ref()
  174. .map(|l| l.x >= (width - BUTTON_WIDTH) as f64)
  175. .unwrap_or(false)
  176. && (needs_redraw_buttons || !self.close_button_hover)
  177. {
  178. self.close_button.update(
  179. (BUTTON_WIDTH as i32, BUTTON_HEIGHT as i32),
  180. CLOSE_COLOR_HOVER,
  181. );
  182. self.close_button_hover = true;
  183. } else if !self
  184. .pointer_loc
  185. .as_ref()
  186. .map(|l| l.x >= (width - BUTTON_WIDTH) as f64)
  187. .unwrap_or(false)
  188. && (needs_redraw_buttons || self.close_button_hover)
  189. {
  190. self.close_button
  191. .update((BUTTON_WIDTH as i32, BUTTON_HEIGHT as i32), CLOSE_COLOR);
  192. self.close_button_hover = false;
  193. }
  194. if self
  195. .pointer_loc
  196. .as_ref()
  197. .map(|l| {
  198. l.x >= (width - BUTTON_WIDTH * 2) as f64 && l.x <= (width - BUTTON_WIDTH) as f64
  199. })
  200. .unwrap_or(false)
  201. && (needs_redraw_buttons || !self.maximize_button_hover)
  202. {
  203. self.maximize_button
  204. .update((BUTTON_WIDTH as i32, BUTTON_HEIGHT as i32), MAX_COLOR_HOVER);
  205. self.maximize_button_hover = true;
  206. } else if !self
  207. .pointer_loc
  208. .as_ref()
  209. .map(|l| {
  210. l.x >= (width - BUTTON_WIDTH * 2) as f64 && l.x <= (width - BUTTON_WIDTH) as f64
  211. })
  212. .unwrap_or(false)
  213. && (needs_redraw_buttons || self.maximize_button_hover)
  214. {
  215. self.maximize_button
  216. .update((BUTTON_WIDTH as i32, BUTTON_HEIGHT as i32), MAX_COLOR);
  217. self.maximize_button_hover = false;
  218. }
  219. }
  220. }
  221. impl<R: Renderer> AsRenderElements<R> for HeaderBar {
  222. type RenderElement = SolidColorRenderElement;
  223. fn render_elements<C: From<Self::RenderElement>>(
  224. &self,
  225. _renderer: &mut R,
  226. location: Point<i32, smithay::utils::Physical>,
  227. scale: smithay::utils::Scale<f64>,
  228. alpha: f32,
  229. ) -> Vec<C> {
  230. let header_end_offset: Point<i32, Logical> = Point::from((self.width as i32, 0));
  231. let button_offset: Point<i32, Logical> = Point::from((BUTTON_WIDTH as i32, 0));
  232. vec![
  233. SolidColorRenderElement::from_buffer(
  234. &self.close_button,
  235. location + (header_end_offset - button_offset).to_physical_precise_round(scale),
  236. scale,
  237. alpha,
  238. Kind::Unspecified,
  239. )
  240. .into(),
  241. SolidColorRenderElement::from_buffer(
  242. &self.maximize_button,
  243. location
  244. + (header_end_offset - button_offset.upscale(2))
  245. .to_physical_precise_round(scale),
  246. scale,
  247. alpha,
  248. Kind::Unspecified,
  249. )
  250. .into(),
  251. SolidColorRenderElement::from_buffer(
  252. &self.background,
  253. location,
  254. scale,
  255. alpha,
  256. Kind::Unspecified,
  257. )
  258. .into(),
  259. ]
  260. }
  261. }
  262. impl WindowElement {
  263. pub fn decoration_state(&self) -> RefMut<'_, WindowState> {
  264. self.user_data().insert_if_missing(|| {
  265. RefCell::new(WindowState {
  266. is_ssd: false,
  267. header_bar: HeaderBar {
  268. pointer_loc: None,
  269. width: 0,
  270. close_button_hover: false,
  271. maximize_button_hover: false,
  272. background: SolidColorBuffer::default(),
  273. close_button: SolidColorBuffer::default(),
  274. maximize_button: SolidColorBuffer::default(),
  275. },
  276. })
  277. });
  278. self.user_data()
  279. .get::<RefCell<WindowState>>()
  280. .unwrap()
  281. .borrow_mut()
  282. }
  283. pub fn set_ssd(&self, ssd: bool) {
  284. self.decoration_state().is_ssd = ssd;
  285. }
  286. }