| 1 | /* |
| 2 | * Copyright 2020-2024 Andrey Kutejko <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 3 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 16 | * |
| 17 | * For more details see the file COPYING. |
| 18 | */ |
| 19 | |
| 20 | use crate::image::Image; |
| 21 | use gtk::{gdk, graphene, prelude::*}; |
| 22 | use std::error::Error; |
| 23 | use std::path::Path; |
| 24 | |
| 25 | #[derive(Clone)] |
| 26 | pub struct Bubble { |
| 27 | paintable: gdk::Paintable, |
| 28 | } |
| 29 | |
| 30 | impl Bubble { |
| 31 | pub fn from_file(filename: &Path) -> Result<Self, Box<dyn Error>> { |
| 32 | Ok(Self { |
| 33 | paintable: Image::from_file(filename)?.to_paintable(), |
| 34 | }) |
| 35 | } |
| 36 | |
| 37 | pub fn draw(&self, snapshot: >k::Snapshot, x: f64, y: f64) { |
| 38 | let x = x as f32; |
| 39 | let y = y as f32; |
| 40 | let width = self.paintable.intrinsic_width() as f32; |
| 41 | let height = self.paintable.intrinsic_height() as f32; |
| 42 | |
| 43 | let bubble_width = width / 2.0; |
| 44 | let bubble_height = height / 2.0; |
| 45 | |
| 46 | let clip_rect = graphene::Rect::new( |
| 47 | if x < bubble_width { |
| 48 | x |
| 49 | } else { |
| 50 | x - bubble_width |
| 51 | }, |
| 52 | if y < bubble_height { |
| 53 | y |
| 54 | } else { |
| 55 | y - bubble_height |
| 56 | }, |
| 57 | bubble_width, |
| 58 | bubble_height, |
| 59 | ); |
| 60 | |
| 61 | snapshot.push_clip(&clip_rect); |
| 62 | snapshot.save(); |
| 63 | snapshot.translate(&graphene::Point::new(x - bubble_width, y - bubble_height)); |
| 64 | self.paintable |
| 65 | .snapshot(snapshot, width as f64, height as f64); |
| 66 | snapshot.restore(); |
| 67 | snapshot.pop(); |
| 68 | } |
| 69 | } |