| 1 |
// Fragments - drag_overlay.rs |
| •2 |
// Copyright (C) 2022-2025 Felix Häcker <[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 <https://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
// Inspired by [Loupe](https://gitlab.gnome.org/GNOME/loupe) and |
| 18 |
// [Amberol](https://gitlab.gnome.org/World/amberol) |
| 19 |
|
| 20 |
use adw::subclass::prelude::*; |
| 21 |
use glib::Properties; |
| 22 |
use gtk::glib; |
| 23 |
use gtk::prelude::*; |
| 24 |
|
| 25 |
mod imp { |
| 26 |
use super::*; |
| 27 |
|
| 28 |
#[derive(Debug, Default, Properties)] |
| 29 |
#[properties(wrapper_type = super::FrgDragOverlay)] |
| 30 |
pub struct FrgDragOverlay { |
| 31 |
#[property(set = Self::set_child)] |
| 32 |
pub child: Option<gtk::Widget>, |
| 33 |
#[property(get)] |
| 34 |
drop_target: gtk::DropTarget, |
| 35 |
overlay: gtk::Overlay, |
| 36 |
revealer: gtk::Revealer, |
| 37 |
} |
| 38 |
|
| 39 |
#[glib::object_subclass] |
| 40 |
impl ObjectSubclass for FrgDragOverlay { |
| 41 |
const NAME: &'static str = "FrgDragOverlay"; |
| 42 |
type Type = super::FrgDragOverlay; |
| 43 |
type ParentType = adw::Bin; |
| 44 |
} |
| 45 |
|
| 46 |
#[glib::derived_properties] |
| 47 |
impl ObjectImpl for FrgDragOverlay { |
| 48 |
fn constructed(&self) { |
| 49 |
self.parent_constructed(); |
| 50 |
|
| 51 |
self.overlay.set_parent(&*self.obj()); |
| 52 |
self.overlay.add_overlay(&self.revealer); |
| 53 |
|
| 54 |
self.revealer.set_can_target(false); |
| 55 |
self.revealer |
| 56 |
.set_transition_type(gtk::RevealerTransitionType::Crossfade); |
| 57 |
self.revealer.set_reveal_child(false); |
| 58 |
|
| 59 |
self.drop_target.set_actions(gtk::gdk::DragAction::COPY); |
| •60 |
self.drop_target.connect_current_drop_notify(glib::clone!( |
| 61 |
#[weak(rename_to = revealer)] |
| 62 |
self.revealer, |
| 63 |
move |target| { |
| 64 |
let reveal = target.current_drop().is_some(); |
| 65 |
revealer.set_reveal_child(reveal); |
| •66 |
} |
| 67 |
)); |
| 68 |
|
| 69 |
let bin = adw::Bin::new(); |
| 70 |
bin.set_can_target(false); |
| 71 |
bin.add_css_class("dragging-area-highlight"); |
| 72 |
|
| 73 |
self.revealer.set_child(Some(&bin)); |
| 74 |
} |
| 75 |
|
| 76 |
fn dispose(&self) { |
| 77 |
self.overlay.unparent(); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
impl WidgetImpl for FrgDragOverlay {} |
| 82 |
|
| 83 |
impl BinImpl for FrgDragOverlay {} |
| 84 |
|
| 85 |
impl FrgDragOverlay { |
| 86 |
pub fn set_child(&self, child: Option<gtk::Widget>) { |
| 87 |
self.overlay.set_child(child.as_ref()); |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
glib::wrapper! { |
| 93 |
pub struct FrgDragOverlay( |
| 94 |
ObjectSubclass<imp::FrgDragOverlay>) |
| •95 |
@extends gtk::Widget, adw::Bin, |
| 96 |
@implements gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget; |
| 97 |
} |