niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·055a94d·Ivan Molodetskikh·1 year ago·2247 commits
| 1 | use std::cell::RefCell; |
| 2 | use std::cmp::{max, min}; |
| 3 | use std::collections::HashMap; |
| 4 | use std::f64::consts::TAU; |
| 5 | use std::iter::zip; |
| 6 | use std::rc::Rc; |
| 7 | |
| 8 | use anyhow::Context; |
| 9 | use arrayvec::ArrayVec; |
| 10 | use niri_config::{Action, Config}; |
| 11 | use niri_ipc::SizeChange; |
| 12 | use pango::{Alignment, FontDescription}; |
| 13 | use pangocairo::cairo::{self, ImageSurface}; |
| 14 | use smithay::backend::allocator::Fourcc; |
| 15 | use smithay::backend::input::TouchSlot; |
| 16 | use smithay::backend::renderer::element::utils::{Relocate, RelocateRenderElement}; |
| 17 | use smithay::backend::renderer::element::Kind; |
| 18 | use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture}; |
| 19 | use smithay::backend::renderer::{ExportMem, Texture as _}; |
| 20 | use smithay::input::keyboard::{Keysym, ModifiersState}; |
| 21 | use smithay::output::{Output, WeakOutput}; |
| 22 | use smithay::utils::{Buffer, Physical, Point, Rectangle, Scale, Size, Transform}; |
| 23 | |
| 24 | use crate::animation::{Animation, Clock}; |
| 25 | use crate::layout::floating::DIRECTIONAL_MOVE_PX; |
| 26 | use crate::niri_render_elements; |
| 27 | use crate::render_helpers::primary_gpu_texture::PrimaryGpuTextureRenderElement; |
| 28 | use crate::render_helpers::solid_color::{SolidColorBuffer, SolidColorRenderElement}; |
| 29 | use crate::render_helpers::texture::{TextureBuffer, TextureRenderElement}; |
| 30 | use crate::render_helpers::{render_to_texture, RenderTarget}; |
| 31 | use crate::utils::to_physical_precise_round; |
| 32 | |
| 33 | const SELECTION_BORDER: i32 = 2; |
| 34 | |
| 35 | const PADDING: i32 = 8; |
| 36 | const RADIUS: i32 = 16; |
| 37 | const FONT: &str = "sans 14px"; |
| 38 | const BORDER: i32 = 4; |
| 39 | const TEXT_HIDE_P: &str = |
| 40 | "Press <span face='mono' bgcolor='#2C2C2C'> Space </span> to save the screenshot.\n\ |
| 41 | Press <span face='mono' bgcolor='#2C2C2C'> P </span> to hide the pointer."; |
| 42 | const TEXT_SHOW_P: &str = |
| 43 | "Press <span face='mono' bgcolor='#2C2C2C'> Space </span> to save the screenshot.\n\ |
| 44 | Press <span face='mono' bgcolor='#2C2C2C'> P </span> to show the pointer."; |
| 45 | |
| 46 | // Ideally the screenshot UI should support cross-output selections. However, that poses some |
| 47 | // technical challenges when the outputs have different scales and such. So, this implementation |
| 48 | // allows only single-output selections for now. |
| 49 | // |
| 50 | // As a consequence of this, selection coordinates are in output-local coordinate space. |
| 51 | #[allow(clippy::large_enum_variant)] |
| 52 | pub enum ScreenshotUi { |
| 53 | Closed { |
| 54 | last_selection: Option<(WeakOutput, Rectangle<i32, Physical>)>, |
| 55 | clock: Clock, |
| 56 | config: Rc<RefCell<Config>>, |
| 57 | }, |
| 58 | Open { |
| 59 | selection: (Output, Point<i32, Physical>, Point<i32, Physical>), |
| 60 | output_data: HashMap<Output, OutputData>, |
| 61 | button: Button, |
| 62 | show_pointer: bool, |
| 63 | open_anim: Animation, |
| 64 | clock: Clock, |
| 65 | config: Rc<RefCell<Config>>, |
| 66 | }, |
| 67 | } |
| 68 | |
| 69 | /// State for moving the selection (as opposed to just drawing). |
| 70 | pub struct MoveState { |
| 71 | // Cursor offset from selection.1 when starting the move. |
| 72 | pointer_offset: Point<i32, Physical>, |
| 73 | // If the move is initiated by a touch, this is the slot. If `None`, the move was initiated by |
| 74 | // holding Space. |
| 75 | touch_slot: Option<TouchSlot>, |
| 76 | } |
| 77 | |
| 78 | pub enum Button { |
| 79 | Up, |
| 80 | Down { |
| 81 | touch_slot: Option<TouchSlot>, |
| 82 | on_capture_button: bool, |
| 83 | last_pos: (Output, Point<i32, Physical>), |
| 84 | move_state: Option<MoveState>, |
| 85 | }, |
| 86 | } |
| 87 | |
| 88 | pub struct OutputData { |
| 89 | size: Size<i32, Physical>, |
| 90 | scale: f64, |
| 91 | transform: Transform, |
| 92 | // Output, screencast, screen capture. |
| 93 | screenshot: [OutputScreenshot; 3], |
| 94 | buffers: [SolidColorBuffer; 8], |
| 95 | locations: [Point<i32, Physical>; 8], |
| 96 | panel: Option<(TextureBuffer<GlesTexture>, TextureBuffer<GlesTexture>)>, |
| 97 | } |
| 98 | |
| 99 | pub struct OutputScreenshot { |
| 100 | texture: GlesTexture, |
| 101 | buffer: PrimaryGpuTextureRenderElement, |
| 102 | pointer: Option<PrimaryGpuTextureRenderElement>, |
| 103 | } |
| 104 | |
| 105 | niri_render_elements! { |
| 106 | ScreenshotUiRenderElement => { |
| 107 | Screenshot = PrimaryGpuTextureRenderElement, |
| 108 | SolidColor = SolidColorRenderElement, |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | impl Button { |
| 113 | fn is_down(&self) -> bool { |
| 114 | matches!(self, Self::Down { .. }) |
| 115 | } |
| 116 | |
| 117 | fn is_dragging_selection(&self) -> bool { |
| 118 | matches!( |
| 119 | self, |
| 120 | Self::Down { |
| 121 | on_capture_button: false, |
| 122 | .. |
| 123 | } |
| 124 | ) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | impl ScreenshotUi { |
| 129 | pub fn new(clock: Clock, config: Rc<RefCell<Config>>) -> Self { |
| 130 | Self::Closed { |
| 131 | last_selection: None, |
| 132 | clock, |
| 133 | config, |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | pub fn open( |
| 138 | &mut self, |
| 139 | renderer: &mut GlesRenderer, |
| 140 | // Output, screencast, screen capture. |
| 141 | screenshots: HashMap<Output, [OutputScreenshot; 3]>, |
| 142 | default_output: Output, |
| 143 | show_pointer: bool, |
| 144 | ) -> bool { |
| 145 | if screenshots.is_empty() { |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | let Self::Closed { |
| 150 | last_selection, |
| 151 | clock, |
| 152 | config, |
| 153 | } = self |
| 154 | else { |
| 155 | return false; |
| 156 | }; |
| 157 | |
| 158 | let last_selection = last_selection |
| 159 | .take() |
| 160 | .and_then(|(weak, sel)| weak.upgrade().map(|output| (output, sel))); |
| 161 | let selection = match last_selection { |
| 162 | Some(selection) if screenshots.contains_key(&selection.0) => selection, |
| 163 | _ => { |
| 164 | let output = default_output; |
| 165 | let output_transform = output.current_transform(); |
| 166 | let output_mode = output.current_mode().unwrap(); |
| 167 | let size = output_transform.transform_size(output_mode.size); |
| 168 | ( |
| 169 | output, |
| 170 | Rectangle::new( |
| 171 | Point::from((size.w / 4, size.h / 4)), |
| 172 | Size::from((size.w / 2, size.h / 2)), |
| 173 | ), |
| 174 | ) |
| 175 | } |
| 176 | }; |
| 177 | |
| 178 | let selection = ( |
| 179 | selection.0, |
| 180 | selection.1.loc, |
| 181 | selection.1.loc + selection.1.size - Size::from((1, 1)), |
| 182 | ); |
| 183 | |
| 184 | let output_data = screenshots |
| 185 | .into_iter() |
| 186 | .map(|(output, screenshot)| { |
| 187 | let transform = output.current_transform(); |
| 188 | let output_mode = output.current_mode().unwrap(); |
| 189 | let size = transform.transform_size(output_mode.size); |
| 190 | let scale = output.current_scale().fractional_scale(); |
| 191 | let buffers = [ |
| 192 | SolidColorBuffer::new((0., 0.), [1., 1., 1., 1.]), |
| 193 | SolidColorBuffer::new((0., 0.), [1., 1., 1., 1.]), |
| 194 | SolidColorBuffer::new((0., 0.), [1., 1., 1., 1.]), |
| 195 | SolidColorBuffer::new((0., 0.), [1., 1., 1., 1.]), |
| 196 | SolidColorBuffer::new((0., 0.), [0., 0., 0., 0.5]), |
| 197 | SolidColorBuffer::new((0., 0.), [0., 0., 0., 0.5]), |
| 198 | SolidColorBuffer::new((0., 0.), [0., 0., 0., 0.5]), |
| 199 | SolidColorBuffer::new((0., 0.), [0., 0., 0., 0.5]), |
| 200 | ]; |
| 201 | let locations = [Default::default(); 8]; |
| 202 | |
| 203 | let mut render_panel_ = |text| { |
| 204 | render_panel(renderer, scale, text) |
| 205 | .map_err(|err| warn!("error rendering help panel: {err:?}")) |
| 206 | .ok() |
| 207 | }; |
| 208 | let panel_show = render_panel_(TEXT_SHOW_P); |
| 209 | let panel_hide = render_panel_(TEXT_HIDE_P); |
| 210 | let panel = Option::zip(panel_show, panel_hide); |
| 211 | |
| 212 | let data = OutputData { |
| 213 | size, |
| 214 | scale, |
| 215 | transform, |
| 216 | screenshot, |
| 217 | buffers, |
| 218 | locations, |
| 219 | panel, |
| 220 | }; |
| 221 | (output, data) |
| 222 | }) |
| 223 | .collect(); |
| 224 | |
| 225 | let open_anim = { |
| 226 | let c = config.borrow(); |
| 227 | Animation::new(clock.clone(), 0., 1., 0., c.animations.screenshot_ui_open.0) |
| 228 | }; |
| 229 | |
| 230 | *self = Self::Open { |
| 231 | selection, |
| 232 | output_data, |
| 233 | button: Button::Up, |
| 234 | show_pointer, |
| 235 | open_anim, |
| 236 | clock: clock.clone(), |
| 237 | config: config.clone(), |
| 238 | }; |
| 239 | |
| 240 | self.update_buffers(); |
| 241 | |
| 242 | true |
| 243 | } |
| 244 | |
| 245 | pub fn close(&mut self) -> bool { |
| 246 | let Self::Open { |
| 247 | selection, |
| 248 | clock, |
| 249 | config, |
| 250 | .. |
| 251 | } = self |
| 252 | else { |
| 253 | return false; |
| 254 | }; |
| 255 | |
| 256 | let last_selection = Some(( |
| 257 | selection.0.downgrade(), |
| 258 | rect_from_corner_points(selection.1, selection.2), |
| 259 | )); |
| 260 | |
| 261 | *self = Self::Closed { |
| 262 | last_selection, |
| 263 | clock: clock.clone(), |
| 264 | config: config.clone(), |
| 265 | }; |
| 266 | |
| 267 | true |
| 268 | } |
| 269 | |
| 270 | pub fn toggle_pointer(&mut self) { |
| 271 | if let Self::Open { show_pointer, .. } = self { |
| 272 | *show_pointer = !*show_pointer; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | pub fn is_open(&self) -> bool { |
| 277 | matches!(self, ScreenshotUi::Open { .. }) |
| 278 | } |
| 279 | |
| 280 | pub fn set_space_down(&mut self, down: bool) { |
| 281 | if let Self::Open { |
| 282 | selection, |
| 283 | button: |
| 284 | Button::Down { |
| 285 | move_state, |
| 286 | last_pos, |
| 287 | .. |
| 288 | }, |
| 289 | .. |
| 290 | } = self |
| 291 | { |
| 292 | if down { |
| 293 | if move_state.is_none() { |
| 294 | *move_state = Some(MoveState { |
| 295 | pointer_offset: last_pos.1 - selection.1, |
| 296 | touch_slot: None, |
| 297 | }); |
| 298 | } |
| 299 | } else { |
| 300 | // Only clear if moving with Space. |
| 301 | if let Some(MoveState { |
| 302 | touch_slot: None, .. |
| 303 | }) = move_state |
| 304 | { |
| 305 | *move_state = None; |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | pub fn move_left(&mut self) { |
| 312 | let Self::Open { |
| 313 | selection: (output, a, b), |
| 314 | output_data, |
| 315 | .. |
| 316 | } = self |
| 317 | else { |
| 318 | return; |
| 319 | }; |
| 320 | |
| 321 | let data = &output_data[output]; |
| 322 | |
| 323 | let delta: i32 = to_physical_precise_round(data.scale, DIRECTIONAL_MOVE_PX); |
| 324 | let delta = min(delta, min(a.x, b.x)); |
| 325 | a.x -= delta; |
| 326 | b.x -= delta; |
| 327 | |
| 328 | self.update_buffers(); |
| 329 | } |
| 330 | |
| 331 | pub fn move_right(&mut self) { |
| 332 | let Self::Open { |
| 333 | selection: (output, a, b), |
| 334 | output_data, |
| 335 | .. |
| 336 | } = self |
| 337 | else { |
| 338 | return; |
| 339 | }; |
| 340 | |
| 341 | let data = &output_data[output]; |
| 342 | |
| 343 | let delta: i32 = to_physical_precise_round(data.scale, DIRECTIONAL_MOVE_PX); |
| 344 | let delta = min(delta, data.size.w - max(a.x, b.x) - 1); |
| 345 | a.x += delta; |
| 346 | b.x += delta; |
| 347 | |
| 348 | self.update_buffers(); |
| 349 | } |
| 350 | |
| 351 | pub fn move_up(&mut self) { |
| 352 | let Self::Open { |
| 353 | selection: (output, a, b), |
| 354 | output_data, |
| 355 | .. |
| 356 | } = self |
| 357 | else { |
| 358 | return; |
| 359 | }; |
| 360 | |
| 361 | let data = &output_data[output]; |
| 362 | |
| 363 | let delta: i32 = to_physical_precise_round(data.scale, DIRECTIONAL_MOVE_PX); |
| 364 | let delta = min(delta, min(a.y, b.y)); |
| 365 | a.y -= delta; |
| 366 | b.y -= delta; |
| 367 | |
| 368 | self.update_buffers(); |
| 369 | } |
| 370 | |
| 371 | pub fn move_down(&mut self) { |
| 372 | let Self::Open { |
| 373 | selection: (output, a, b), |
| 374 | output_data, |
| 375 | .. |
| 376 | } = self |
| 377 | else { |
| 378 | return; |
| 379 | }; |
| 380 | |
| 381 | let data = &output_data[output]; |
| 382 | |
| 383 | let delta: i32 = to_physical_precise_round(data.scale, DIRECTIONAL_MOVE_PX); |
| 384 | let delta = min(delta, data.size.h - max(a.y, b.y) - 1); |
| 385 | a.y += delta; |
| 386 | b.y += delta; |
| 387 | |
| 388 | self.update_buffers(); |
| 389 | } |
| 390 | |
| 391 | /// Moves the screenshot selection to a different output. |
| 392 | /// |
| 393 | /// This preserves the relative position while keeping logical size. It is (intentionally) very |
| 394 | /// similar to how floating windows move across monitors, but with one difference: floating |
| 395 | /// windows can go partially outside the view, while the screenshot selection cannot. So, we |
| 396 | /// clamp it to new output bounds, trying to preserve the size if possible. |
| 397 | pub fn move_to_output(&mut self, new_output: Output) { |
| 398 | let Self::Open { |
| 399 | selection, |
| 400 | output_data, |
| 401 | .. |
| 402 | } = self |
| 403 | else { |
| 404 | return; |
| 405 | }; |
| 406 | |
| 407 | let (current_output, current_a, current_b) = selection; |
| 408 | |
| 409 | if current_output == &new_output { |
| 410 | return; |
| 411 | } |
| 412 | |
| 413 | let Some(target_data) = output_data.get(&new_output) else { |
| 414 | return; |
| 415 | }; |
| 416 | |
| 417 | let current_data = &output_data[current_output]; |
| 418 | |
| 419 | let current_rect: Rectangle<_, Physical> = Rectangle::new( |
| 420 | Point::from((current_a.x.min(current_b.x), current_a.y.min(current_b.y))), |
| 421 | Size::from(( |
| 422 | (current_a.x.max(current_b.x) - current_a.x.min(current_b.x) + 1), |
| 423 | (current_a.y.max(current_b.y) - current_a.y.min(current_b.y) + 1), |
| 424 | )), |
| 425 | ); |
| 426 | let current_rect = current_rect.to_f64(); |
| 427 | |
| 428 | let rel_x = current_rect.loc.x / current_data.size.w as f64; |
| 429 | let rel_y = current_rect.loc.y / current_data.size.h as f64; |
| 430 | |
| 431 | let factor = target_data.scale / current_data.scale; |
| 432 | let mut new_width = (current_rect.size.w * factor).round() as i32; |
| 433 | let mut new_height = (current_rect.size.h * factor).round() as i32; |
| 434 | |
| 435 | new_width = new_width.clamp(1, target_data.size.w); |
| 436 | new_height = new_height.clamp(1, target_data.size.h); |
| 437 | |
| 438 | let new_x = (rel_x * target_data.size.w as f64).round() as i32; |
| 439 | let new_y = (rel_y * target_data.size.h as f64).round() as i32; |
| 440 | |
| 441 | let max_x = target_data.size.w - new_width; |
| 442 | let max_y = target_data.size.h - new_height; |
| 443 | let new_x = new_x.clamp(0, max_x); |
| 444 | let new_y = new_y.clamp(0, max_y); |
| 445 | |
| 446 | let new_rect = Rectangle::new( |
| 447 | Point::from((new_x, new_y)), |
| 448 | Size::from((new_width, new_height)), |
| 449 | ); |
| 450 | |
| 451 | *selection = ( |
| 452 | new_output, |
| 453 | new_rect.loc, |
| 454 | new_rect.loc + new_rect.size - Size::from((1, 1)), |
| 455 | ); |
| 456 | |
| 457 | self.update_buffers(); |
| 458 | } |
| 459 | |
| 460 | pub fn set_width(&mut self, change: SizeChange) { |
| 461 | let Self::Open { |
| 462 | selection: (output, a, b), |
| 463 | output_data, |
| 464 | .. |
| 465 | } = self |
| 466 | else { |
| 467 | return; |
| 468 | }; |
| 469 | |
| 470 | let data = &output_data[output]; |
| 471 | |
| 472 | let available_size = f64::from(data.size.w); |
| 473 | let current_size = max(a.x, b.x) + 1 - min(a.x, b.x); |
| 474 | |
| 475 | let new_size = match change { |
| 476 | SizeChange::SetFixed(fixed) => to_physical_precise_round(data.scale, fixed), |
| 477 | SizeChange::SetProportion(prop) => { |
| 478 | let prop = (prop / 100.).clamp(0., 1.); |
| 479 | (available_size * prop).round() as i32 |
| 480 | } |
| 481 | SizeChange::AdjustFixed(delta) => { |
| 482 | let delta = to_physical_precise_round(data.scale, delta); |
| 483 | current_size.saturating_add(delta) |
| 484 | } |
| 485 | SizeChange::AdjustProportion(delta) => { |
| 486 | let current_prop = f64::from(current_size) / available_size; |
| 487 | let prop = (current_prop + delta / 100.).clamp(0., 1.); |
| 488 | (available_size * prop).round() as i32 |
| 489 | } |
| 490 | }; |
| 491 | let new_size = new_size.clamp(1, data.size.w - min(a.x, b.x)) - 1; |
| 492 | a.x = min(a.x, b.x); |
| 493 | b.x = a.x + new_size; |
| 494 | |
| 495 | self.update_buffers(); |
| 496 | } |
| 497 | |
| 498 | pub fn set_height(&mut self, change: SizeChange) { |
| 499 | let Self::Open { |
| 500 | selection: (output, a, b), |
| 501 | output_data, |
| 502 | .. |
| 503 | } = self |
| 504 | else { |
| 505 | return; |
| 506 | }; |
| 507 | |
| 508 | let data = &output_data[output]; |
| 509 | |
| 510 | let available_size = f64::from(data.size.h); |
| 511 | let current_size = max(a.y, b.y) + 1 - min(a.y, b.y); |
| 512 | |
| 513 | let new_size = match change { |
| 514 | SizeChange::SetFixed(fixed) => to_physical_precise_round(data.scale, fixed), |
| 515 | SizeChange::SetProportion(prop) => { |
| 516 | let prop = (prop / 100.).clamp(0., 1.); |
| 517 | (available_size * prop).round() as i32 |
| 518 | } |
| 519 | SizeChange::AdjustFixed(delta) => { |
| 520 | let delta = to_physical_precise_round(data.scale, delta); |
| 521 | current_size.saturating_add(delta) |
| 522 | } |
| 523 | SizeChange::AdjustProportion(delta) => { |
| 524 | let current_prop = f64::from(current_size) / available_size; |
| 525 | let prop = (current_prop + delta / 100.).clamp(0., 1.); |
| 526 | (available_size * prop).round() as i32 |
| 527 | } |
| 528 | }; |
| 529 | let new_size = new_size.clamp(1, data.size.h - min(a.y, b.y)) - 1; |
| 530 | a.y = min(a.y, b.y); |
| 531 | b.y = a.y + new_size; |
| 532 | |
| 533 | self.update_buffers(); |
| 534 | } |
| 535 | |
| 536 | pub fn advance_animations(&mut self) {} |
| 537 | |
| 538 | pub fn are_animations_ongoing(&self) -> bool { |
| 539 | let Self::Open { open_anim, .. } = self else { |
| 540 | return false; |
| 541 | }; |
| 542 | |
| 543 | !open_anim.is_done() |
| 544 | } |
| 545 | |
| 546 | fn update_buffers(&mut self) { |
| 547 | let Self::Open { |
| 548 | selection, |
| 549 | output_data, |
| 550 | .. |
| 551 | } = self |
| 552 | else { |
| 553 | panic!("screenshot UI must be open to update buffers"); |
| 554 | }; |
| 555 | |
| 556 | let (selection_output, a, b) = selection; |
| 557 | let mut rect = rect_from_corner_points(*a, *b); |
| 558 | |
| 559 | for (output, data) in output_data { |
| 560 | let buffers = &mut data.buffers; |
| 561 | let locations = &mut data.locations; |
| 562 | let size = data.size; |
| 563 | let scale = data.scale; |
| 564 | |
| 565 | if output == selection_output { |
| 566 | // Check if the selection is still valid. If not, reset it back to default. |
| 567 | if !Rectangle::from_size(size).contains_rect(rect) { |
| 568 | rect = Rectangle::new( |
| 569 | Point::from((size.w / 4, size.h / 4)), |
| 570 | Size::from((size.w / 2, size.h / 2)), |
| 571 | ); |
| 572 | *a = rect.loc; |
| 573 | *b = rect.loc + rect.size - Size::from((1, 1)); |
| 574 | } |
| 575 | |
| 576 | let border = to_physical_precise_round(scale, SELECTION_BORDER); |
| 577 | |
| 578 | let resize = move |buffer: &mut SolidColorBuffer, w: i32, h: i32| { |
| 579 | let size = Size::<_, Physical>::from((w, h)); |
| 580 | buffer.resize(size.to_f64().to_logical(scale)); |
| 581 | }; |
| 582 | |
| 583 | resize(&mut buffers[0], rect.size.w + border * 2, border); |
| 584 | resize(&mut buffers[1], rect.size.w + border * 2, border); |
| 585 | resize(&mut buffers[2], border, rect.size.h); |
| 586 | resize(&mut buffers[3], border, rect.size.h); |
| 587 | |
| 588 | resize(&mut buffers[4], size.w, rect.loc.y); |
| 589 | resize(&mut buffers[5], size.w, size.h - rect.loc.y - rect.size.h); |
| 590 | resize(&mut buffers[6], rect.loc.x, rect.size.h); |
| 591 | resize( |
| 592 | &mut buffers[7], |
| 593 | size.w - rect.loc.x - rect.size.w, |
| 594 | rect.size.h, |
| 595 | ); |
| 596 | |
| 597 | locations[0] = Point::from((rect.loc.x - border, rect.loc.y - border)); |
| 598 | locations[1] = Point::from((rect.loc.x - border, rect.loc.y + rect.size.h)); |
| 599 | locations[2] = Point::from((rect.loc.x - border, rect.loc.y)); |
| 600 | locations[3] = Point::from((rect.loc.x + rect.size.w, rect.loc.y)); |
| 601 | |
| 602 | locations[5] = Point::from((0, rect.loc.y + rect.size.h)); |
| 603 | locations[6] = Point::from((0, rect.loc.y)); |
| 604 | locations[7] = Point::from((rect.loc.x + rect.size.w, rect.loc.y)); |
| 605 | } else { |
| 606 | buffers[0].resize((0., 0.)); |
| 607 | buffers[1].resize((0., 0.)); |
| 608 | buffers[2].resize((0., 0.)); |
| 609 | buffers[3].resize((0., 0.)); |
| 610 | |
| 611 | buffers[4].resize(size.to_f64().to_logical(data.scale)); |
| 612 | buffers[5].resize((0., 0.)); |
| 613 | buffers[6].resize((0., 0.)); |
| 614 | buffers[7].resize((0., 0.)); |
| 615 | } |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | pub fn render_output( |
| 620 | &self, |
| 621 | output: &Output, |
| 622 | target: RenderTarget, |
| 623 | ) -> ArrayVec<ScreenshotUiRenderElement, 11> { |
| 624 | let _span = tracy_client::span!("ScreenshotUi::render_output"); |
| 625 | |
| 626 | let Self::Open { |
| 627 | output_data, |
| 628 | show_pointer, |
| 629 | button, |
| 630 | open_anim, |
| 631 | .. |
| 632 | } = self |
| 633 | else { |
| 634 | panic!("screenshot UI must be open to render it"); |
| 635 | }; |
| 636 | |
| 637 | let mut elements = ArrayVec::new(); |
| 638 | |
| 639 | let Some(output_data) = output_data.get(output) else { |
| 640 | return elements; |
| 641 | }; |
| 642 | |
| 643 | let scale = output_data.scale; |
| 644 | let progress = open_anim.clamped_value().clamp(0., 1.) as f32; |
| 645 | |
| 646 | // The help panel goes on top. |
| 647 | if let Some((show, hide)) = &output_data.panel { |
| 648 | let buffer = if *show_pointer { hide } else { show }; |
| 649 | let alpha = if button.is_dragging_selection() { |
| 650 | 0.3 |
| 651 | } else { |
| 652 | 0.9 |
| 653 | }; |
| 654 | let location = panel_location(output_data, buffer.texture().size()) |
| 655 | .to_f64() |
| 656 | .to_logical(scale); |
| 657 | |
| 658 | let elem = PrimaryGpuTextureRenderElement(TextureRenderElement::from_texture_buffer( |
| 659 | buffer.clone(), |
| 660 | location, |
| 661 | alpha * progress, |
| 662 | None, |
| 663 | None, |
| 664 | Kind::Unspecified, |
| 665 | )); |
| 666 | elements.push(elem.into()); |
| 667 | } |
| 668 | |
| 669 | let buf_loc = zip(&output_data.buffers, &output_data.locations); |
| 670 | elements.extend(buf_loc.map(|(buffer, loc)| { |
| 671 | SolidColorRenderElement::from_buffer( |
| 672 | buffer, |
| 673 | loc.to_f64().to_logical(scale), |
| 674 | progress, |
| 675 | Kind::Unspecified, |
| 676 | ) |
| 677 | .into() |
| 678 | })); |
| 679 | |
| 680 | // The screenshot itself goes last. |
| 681 | let index = match target { |
| 682 | RenderTarget::Output => 0, |
| 683 | RenderTarget::Screencast => 1, |
| 684 | RenderTarget::ScreenCapture => 2, |
| 685 | }; |
| 686 | let screenshot = &output_data.screenshot[index]; |
| 687 | |
| 688 | if *show_pointer { |
| 689 | if let Some(pointer) = screenshot.pointer.clone() { |
| 690 | elements.push(pointer.into()); |
| 691 | } |
| 692 | } |
| 693 | elements.push(screenshot.buffer.clone().into()); |
| 694 | |
| 695 | elements |
| 696 | } |
| 697 | |
| 698 | pub fn capture( |
| 699 | &self, |
| 700 | renderer: &mut GlesRenderer, |
| 701 | ) -> anyhow::Result<(Size<i32, Physical>, Vec<u8>)> { |
| 702 | let _span = tracy_client::span!("ScreenshotUi::capture"); |
| 703 | |
| 704 | let Self::Open { |
| 705 | selection, |
| 706 | output_data, |
| 707 | show_pointer, |
| 708 | .. |
| 709 | } = self |
| 710 | else { |
| 711 | panic!("screenshot UI must be open to capture"); |
| 712 | }; |
| 713 | |
| 714 | let data = &output_data[&selection.0]; |
| 715 | let rect = rect_from_corner_points(selection.1, selection.2); |
| 716 | |
| 717 | let screenshot = &data.screenshot[0]; |
| 718 | |
| 719 | // Composite the pointer on top if needed. |
| 720 | let mut tex_rect = None; |
| 721 | if *show_pointer { |
| 722 | if let Some(pointer) = screenshot.pointer.clone() { |
| 723 | let scale = pointer.0.buffer().texture_scale(); |
| 724 | let offset = rect.loc.upscale(-1); |
| 725 | |
| 726 | let mut elements = ArrayVec::<_, 2>::new(); |
| 727 | elements.push(pointer); |
| 728 | elements.push(screenshot.buffer.clone()); |
| 729 | let elements = elements.iter().rev().map(|elem| { |
| 730 | RelocateRenderElement::from_element(elem, offset, Relocate::Relative) |
| 731 | }); |
| 732 | |
| 733 | let res = render_to_texture( |
| 734 | renderer, |
| 735 | rect.size, |
| 736 | scale, |
| 737 | Transform::Normal, |
| 738 | Fourcc::Abgr8888, |
| 739 | elements, |
| 740 | ); |
| 741 | match res { |
| 742 | Ok((texture, _)) => { |
| 743 | tex_rect = Some((texture, Rectangle::from_size(rect.size))); |
| 744 | } |
| 745 | Err(err) => { |
| 746 | warn!("error compositing pointer onto screenshot: {err:?}"); |
| 747 | } |
| 748 | } |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | let (texture, rect) = tex_rect.unwrap_or_else(|| (screenshot.texture.clone(), rect)); |
| 753 | // The size doesn't actually matter because we're not transforming anything. |
| 754 | let buf_rect = rect |
| 755 | .to_logical(1) |
| 756 | .to_buffer(1, Transform::Normal, &Size::from((1, 1))); |
| 757 | |
| 758 | let mapping = renderer |
| 759 | .copy_texture(&texture, buf_rect, Fourcc::Abgr8888) |
| 760 | .context("error copying texture")?; |
| 761 | let copy = renderer |
| 762 | .map_texture(&mapping) |
| 763 | .context("error mapping texture")?; |
| 764 | |
| 765 | Ok((rect.size, copy.to_vec())) |
| 766 | } |
| 767 | |
| 768 | pub fn action(&self, raw: Keysym, mods: ModifiersState) -> Option<Action> { |
| 769 | let Self::Open { button, .. } = self else { |
| 770 | return None; |
| 771 | }; |
| 772 | |
| 773 | // Pressing Space while the button is down goes into origin moving rather than capture. |
| 774 | if matches!(button, Button::Down { .. }) && raw == Keysym::space { |
| 775 | return None; |
| 776 | } |
| 777 | |
| 778 | action(raw, mods) |
| 779 | } |
| 780 | |
| 781 | pub fn selection_output(&self) -> Option<&Output> { |
| 782 | if let Self::Open { |
| 783 | selection: (output, _, _), |
| 784 | .. |
| 785 | } = self |
| 786 | { |
| 787 | Some(output) |
| 788 | } else { |
| 789 | None |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | pub fn output_size(&self, output: &Output) -> Option<(Size<i32, Physical>, f64, Transform)> { |
| 794 | if let Self::Open { output_data, .. } = self { |
| 795 | let data = output_data.get(output)?; |
| 796 | Some((data.size, data.scale, data.transform)) |
| 797 | } else { |
| 798 | None |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | /// The pointer has moved to `point` relative to the current selection output. |
| 803 | pub fn pointer_motion(&mut self, point: Point<i32, Physical>, slot: Option<TouchSlot>) { |
| 804 | let Self::Open { |
| 805 | selection, |
| 806 | output_data, |
| 807 | button: |
| 808 | Button::Down { |
| 809 | touch_slot, |
| 810 | on_capture_button, |
| 811 | last_pos, |
| 812 | move_state, |
| 813 | }, |
| 814 | .. |
| 815 | } = self |
| 816 | else { |
| 817 | return; |
| 818 | }; |
| 819 | |
| 820 | if *touch_slot != slot { |
| 821 | return; |
| 822 | } |
| 823 | |
| 824 | last_pos.1 = point; |
| 825 | |
| 826 | if *on_capture_button { |
| 827 | return; |
| 828 | } |
| 829 | |
| 830 | if let Some(move_state) = move_state { |
| 831 | // The cursor offset is relative to selection.1. |
| 832 | let delta = point - (selection.1 + move_state.pointer_offset); |
| 833 | |
| 834 | let desired = rect_from_corner_points(selection.1 + delta, selection.2 + delta); |
| 835 | let bounds = Rectangle::from_size(output_data[&selection.0].size - desired.size); |
| 836 | let clamped_loc = desired.loc.constrain(bounds); |
| 837 | |
| 838 | let delta = clamped_loc - rect_from_corner_points(selection.1, selection.2).loc; |
| 839 | selection.1 += delta; |
| 840 | selection.2 += delta; |
| 841 | } else { |
| 842 | selection.2 = point; |
| 843 | } |
| 844 | |
| 845 | self.update_buffers(); |
| 846 | } |
| 847 | |
| 848 | pub fn pointer_down( |
| 849 | &mut self, |
| 850 | output: Output, |
| 851 | point: Point<i32, Physical>, |
| 852 | slot: Option<TouchSlot>, |
| 853 | ) -> bool { |
| 854 | let Self::Open { |
| 855 | selection, |
| 856 | output_data, |
| 857 | show_pointer, |
| 858 | button, |
| 859 | .. |
| 860 | } = self |
| 861 | else { |
| 862 | return false; |
| 863 | }; |
| 864 | |
| 865 | // Check if this is a second touch (different slot) while already dragging. |
| 866 | if let Some(new_slot) = slot { |
| 867 | if let Button::Down { |
| 868 | on_capture_button: false, |
| 869 | move_state, |
| 870 | last_pos, |
| 871 | .. |
| 872 | } = button |
| 873 | { |
| 874 | if move_state.is_none() { |
| 875 | *move_state = Some(MoveState { |
| 876 | pointer_offset: last_pos.1 - selection.1, |
| 877 | touch_slot: Some(new_slot), |
| 878 | }); |
| 879 | } |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | if button.is_down() { |
| 884 | return false; |
| 885 | } |
| 886 | |
| 887 | let Some(output_data) = output_data.get(&output) else { |
| 888 | return false; |
| 889 | }; |
| 890 | |
| 891 | if let Some((show, hide)) = &output_data.panel { |
| 892 | let buffer = if *show_pointer { hide } else { show }; |
| 893 | let panel_size = buffer.texture().size(); |
| 894 | let location = panel_location(output_data, panel_size); |
| 895 | |
| 896 | if is_within_capture_button(output_data.scale, panel_size, point - location) { |
| 897 | *button = Button::Down { |
| 898 | touch_slot: slot, |
| 899 | on_capture_button: true, |
| 900 | last_pos: (output, point), |
| 901 | move_state: None, |
| 902 | }; |
| 903 | return false; |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | *button = Button::Down { |
| 908 | touch_slot: slot, |
| 909 | on_capture_button: false, |
| 910 | last_pos: (output.clone(), point), |
| 911 | move_state: None, |
| 912 | }; |
| 913 | *selection = (output, point, point); |
| 914 | |
| 915 | self.update_buffers(); |
| 916 | |
| 917 | true |
| 918 | } |
| 919 | |
| 920 | pub fn pointer_up(&mut self, slot: Option<TouchSlot>) -> Option<bool> { |
| 921 | let Self::Open { |
| 922 | selection, |
| 923 | output_data, |
| 924 | button, |
| 925 | show_pointer, |
| 926 | .. |
| 927 | } = self |
| 928 | else { |
| 929 | return None; |
| 930 | }; |
| 931 | |
| 932 | let Button::Down { |
| 933 | touch_slot, |
| 934 | on_capture_button, |
| 935 | ref last_pos, |
| 936 | ref mut move_state, |
| 937 | .. |
| 938 | } = *button |
| 939 | else { |
| 940 | return None; |
| 941 | }; |
| 942 | |
| 943 | // Check if this is a move touch and if so, stop the move. |
| 944 | if let Some(state) = move_state { |
| 945 | if state.touch_slot.is_some_and(|m_slot| Some(m_slot) == slot) { |
| 946 | *move_state = None; |
| 947 | return None; |
| 948 | } |
| 949 | }; |
| 950 | |
| 951 | if touch_slot != slot { |
| 952 | return None; |
| 953 | } |
| 954 | |
| 955 | let last_pos = last_pos.clone(); |
| 956 | *button = Button::Up; |
| 957 | |
| 958 | // Check if we released still on the capture button. |
| 959 | if on_capture_button { |
| 960 | let (output, point) = last_pos; |
| 961 | |
| 962 | #[allow(clippy::question_mark)] |
| 963 | let Some(output_data) = output_data.get(&output) else { |
| 964 | return None; |
| 965 | }; |
| 966 | |
| 967 | if let Some((show, hide)) = &output_data.panel { |
| 968 | let buffer = if *show_pointer { hide } else { show }; |
| 969 | let panel_size = buffer.texture().size(); |
| 970 | let location = panel_location(output_data, panel_size); |
| 971 | |
| 972 | if is_within_capture_button(output_data.scale, panel_size, point - location) { |
| 973 | return Some(true); |
| 974 | } |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | // Check if the resulting selection is zero-sized, and try to come up with a small |
| 979 | // default rectangle. |
| 980 | let (output, a, b) = selection; |
| 981 | let mut rect = rect_from_corner_points(*a, *b); |
| 982 | if rect.size.is_empty() || rect.size == Size::from((1, 1)) { |
| 983 | let data = &output_data[output]; |
| 984 | rect = Rectangle::new( |
| 985 | Point::from((rect.loc.x - 16, rect.loc.y - 16)), |
| 986 | Size::from((32, 32)), |
| 987 | ) |
| 988 | .intersection(Rectangle::from_size(data.size)) |
| 989 | .unwrap_or_default(); |
| 990 | *a = rect.loc; |
| 991 | *b = rect.loc + rect.size - Size::from((1, 1)); |
| 992 | } |
| 993 | |
| 994 | self.update_buffers(); |
| 995 | |
| 996 | Some(false) |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | impl OutputScreenshot { |
| 1001 | pub fn from_textures( |
| 1002 | renderer: &mut GlesRenderer, |
| 1003 | scale: Scale<f64>, |
| 1004 | texture: GlesTexture, |
| 1005 | pointer: Option<(GlesTexture, Rectangle<i32, Physical>)>, |
| 1006 | ) -> Self { |
| 1007 | let buffer = PrimaryGpuTextureRenderElement(TextureRenderElement::from_texture_buffer( |
| 1008 | TextureBuffer::from_texture( |
| 1009 | renderer, |
| 1010 | texture.clone(), |
| 1011 | scale, |
| 1012 | Transform::Normal, |
| 1013 | Vec::new(), |
| 1014 | ), |
| 1015 | (0., 0.), |
| 1016 | 1., |
| 1017 | None, |
| 1018 | None, |
| 1019 | Kind::Unspecified, |
| 1020 | )); |
| 1021 | |
| 1022 | let pointer = pointer.map(|(texture, geo)| { |
| 1023 | PrimaryGpuTextureRenderElement(TextureRenderElement::from_texture_buffer( |
| 1024 | TextureBuffer::from_texture( |
| 1025 | renderer, |
| 1026 | texture, |
| 1027 | scale, |
| 1028 | Transform::Normal, |
| 1029 | Vec::new(), |
| 1030 | ), |
| 1031 | geo.to_f64().to_logical(scale).loc, |
| 1032 | 1., |
| 1033 | None, |
| 1034 | None, |
| 1035 | Kind::Unspecified, |
| 1036 | )) |
| 1037 | }); |
| 1038 | |
| 1039 | Self { |
| 1040 | texture, |
| 1041 | buffer, |
| 1042 | pointer, |
| 1043 | } |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | fn action(raw: Keysym, mods: ModifiersState) -> Option<Action> { |
| 1048 | if raw == Keysym::Escape { |
| 1049 | return Some(Action::CancelScreenshot); |
| 1050 | } |
| 1051 | |
| 1052 | if mods.alt || mods.shift { |
| 1053 | return None; |
| 1054 | } |
| 1055 | |
| 1056 | if !mods.ctrl && (raw == Keysym::space || raw == Keysym::Return) { |
| 1057 | return Some(Action::ConfirmScreenshot { |
| 1058 | write_to_disk: true, |
| 1059 | }); |
| 1060 | } |
| 1061 | if mods.ctrl && raw == Keysym::c { |
| 1062 | return Some(Action::ConfirmScreenshot { |
| 1063 | write_to_disk: false, |
| 1064 | }); |
| 1065 | } |
| 1066 | |
| 1067 | if !mods.ctrl && raw == Keysym::p { |
| 1068 | return Some(Action::ScreenshotTogglePointer); |
| 1069 | } |
| 1070 | |
| 1071 | None |
| 1072 | } |
| 1073 | |
| 1074 | pub fn rect_from_corner_points( |
| 1075 | a: Point<i32, Physical>, |
| 1076 | b: Point<i32, Physical>, |
| 1077 | ) -> Rectangle<i32, Physical> { |
| 1078 | let x1 = min(a.x, b.x); |
| 1079 | let y1 = min(a.y, b.y); |
| 1080 | let x2 = max(a.x, b.x); |
| 1081 | let y2 = max(a.y, b.y); |
| 1082 | // We're adding + 1 because the pointer is clamped to output size - 1, so to get the full |
| 1083 | // screen worth of selection we must add back that + 1. |
| 1084 | Rectangle::from_extremities((x1, y1), (x2 + 1, y2 + 1)) |
| 1085 | } |
| 1086 | |
| 1087 | fn panel_location(output_data: &OutputData, panel_size: Size<i32, Buffer>) -> Point<i32, Physical> { |
| 1088 | let scale = output_data.scale; |
| 1089 | let padding: i32 = to_physical_precise_round(scale, PADDING); |
| 1090 | let x = max(0, (output_data.size.w - panel_size.w) / 2); |
| 1091 | let y = max(0, output_data.size.h - panel_size.h - padding * 2); |
| 1092 | Point::from((x, y)) |
| 1093 | } |
| 1094 | |
| 1095 | fn is_within_capture_button( |
| 1096 | scale: f64, |
| 1097 | panel_size: Size<i32, Buffer>, |
| 1098 | pos_within_panel: Point<i32, Physical>, |
| 1099 | ) -> bool { |
| 1100 | let padding: i32 = to_physical_precise_round(scale, PADDING); |
| 1101 | let radius = to_physical_precise_round::<i32>(scale, RADIUS) - 2; |
| 1102 | |
| 1103 | let xc = padding + radius; |
| 1104 | let yc = panel_size.h / 2; |
| 1105 | let pos = pos_within_panel; |
| 1106 | |
| 1107 | (pos.x - xc) * (pos.x - xc) + (pos.y - yc) * (pos.y - yc) <= radius * radius |
| 1108 | } |
| 1109 | |
| 1110 | fn render_panel( |
| 1111 | renderer: &mut GlesRenderer, |
| 1112 | scale: f64, |
| 1113 | text: &str, |
| 1114 | ) -> anyhow::Result<TextureBuffer<GlesTexture>> { |
| 1115 | let _span = tracy_client::span!("screenshot_ui::render_panel"); |
| 1116 | |
| 1117 | let padding: i32 = to_physical_precise_round(scale, PADDING); |
| 1118 | // Keep the border width even to avoid blurry edges. |
| 1119 | let border_width = (f64::from(BORDER) / 2. * scale).round() * 2.; |
| 1120 | let half_border_width = (border_width / 2.) as i32; |
| 1121 | let radius: i32 = to_physical_precise_round(scale, RADIUS); |
| 1122 | let circle_stroke: f64 = to_physical_precise_round(scale, 2.); |
| 1123 | |
| 1124 | // Add 2 px of spacing to separate the backgrounds of the "Space" and "P" keys. |
| 1125 | let spacing = to_physical_precise_round::<i32>(scale, 2) * 1024; |
| 1126 | |
| 1127 | let mut font = FontDescription::from_string(FONT); |
| 1128 | font.set_absolute_size(to_physical_precise_round(scale, font.size())); |
| 1129 | |
| 1130 | let surface = ImageSurface::create(cairo::Format::ARgb32, 0, 0)?; |
| 1131 | let cr = cairo::Context::new(&surface)?; |
| 1132 | let layout = pangocairo::functions::create_layout(&cr); |
| 1133 | layout.context().set_round_glyph_positions(false); |
| 1134 | layout.set_font_description(Some(&font)); |
| 1135 | layout.set_alignment(Alignment::Left); |
| 1136 | layout.set_markup(text); |
| 1137 | layout.set_spacing(spacing); |
| 1138 | |
| 1139 | let (mut width, mut height) = layout.pixel_size(); |
| 1140 | |
| 1141 | width += padding + radius * 2 + padding - half_border_width + padding; |
| 1142 | height = max(height, radius * 2); |
| 1143 | height += padding * 2; |
| 1144 | |
| 1145 | let surface = ImageSurface::create(cairo::Format::ARgb32, width, height)?; |
| 1146 | let cr = cairo::Context::new(&surface)?; |
| 1147 | cr.set_source_rgb(0.1, 0.1, 0.1); |
| 1148 | cr.paint()?; |
| 1149 | |
| 1150 | let padding = f64::from(padding); |
| 1151 | let half_border_width = f64::from(half_border_width); |
| 1152 | let r = f64::from(radius); |
| 1153 | |
| 1154 | let yc = f64::from(height / 2); |
| 1155 | |
| 1156 | cr.new_sub_path(); |
| 1157 | cr.arc(padding + r, yc, r, 0., TAU); |
| 1158 | cr.set_source_rgb(1., 1., 1.); |
| 1159 | cr.fill()?; |
| 1160 | |
| 1161 | cr.new_sub_path(); |
| 1162 | cr.arc(padding + r, yc, r - circle_stroke, 0., TAU); |
| 1163 | cr.set_source_rgb(0.1, 0.1, 0.1); |
| 1164 | cr.fill()?; |
| 1165 | |
| 1166 | cr.new_sub_path(); |
| 1167 | cr.arc(padding + r, yc, r - circle_stroke * 2., 0., TAU); |
| 1168 | cr.set_source_rgb(1., 1., 1.); |
| 1169 | cr.fill()?; |
| 1170 | |
| 1171 | cr.move_to(padding + r * 2. + padding - half_border_width, padding); |
| 1172 | |
| 1173 | let layout = pangocairo::functions::create_layout(&cr); |
| 1174 | layout.context().set_round_glyph_positions(false); |
| 1175 | layout.set_font_description(Some(&font)); |
| 1176 | layout.set_alignment(Alignment::Left); |
| 1177 | layout.set_markup(text); |
| 1178 | layout.set_spacing(spacing); |
| 1179 | |
| 1180 | cr.set_source_rgb(1., 1., 1.); |
| 1181 | pangocairo::functions::show_layout(&cr, &layout); |
| 1182 | |
| 1183 | cr.move_to(0., 0.); |
| 1184 | cr.line_to(width.into(), 0.); |
| 1185 | cr.line_to(width.into(), height.into()); |
| 1186 | cr.line_to(0., height.into()); |
| 1187 | cr.line_to(0., 0.); |
| 1188 | cr.set_source_rgb(0.3, 0.3, 0.3); |
| 1189 | cr.set_line_width(border_width); |
| 1190 | cr.stroke()?; |
| 1191 | drop(cr); |
| 1192 | |
| 1193 | let data = surface.take_data().unwrap(); |
| 1194 | let buffer = TextureBuffer::from_memory( |
| 1195 | renderer, |
| 1196 | &data, |
| 1197 | Fourcc::Argb8888, |
| 1198 | (width, height), |
| 1199 | false, |
| 1200 | scale, |
| 1201 | Transform::Normal, |
| 1202 | Vec::new(), |
| 1203 | )?; |
| 1204 | |
| 1205 | Ok(buffer) |
| 1206 | } |