Medlar/crates/app/src/drop.rs

61 lines
2.6 KiB
Rust

//! Things let go over a window (FEATURES 3.9): files (lists of links, `.metalink`) and, where the windowing system
//! passes them on as a path, links. The toolkit hands over no dropped text, so a link dragged from a browser's address
//! bar arrives only where the browser makes a file or `://` path of it; paste into the Add window otherwise. A full-window state shows while something is over the window.
use crate::fonts;
use crate::theme::{Theme, alpha};
use crate::tokens::{radius, size, space, spring, text};
use crate::views::add::Request;
use crate::ui::widgets::cr;
use eframe::egui::{Align2, Context, Id, LayerId, Order, Stroke, StrokeKind};
use medlar_core::tr;
#[allow(deprecated)]
fn id() -> Id {
Id::new("drop overlay")
}
/// What was dropped, as asks for the Add window. Text counts when it holds links.
pub fn requests(ctx: &Context) -> Vec<Request> {
let files = ctx.input(|i| i.raw.dropped_files.clone());
let mut out = vec![];
let mut texts: Vec<String> = vec![];
for f in files {
let path = f.path().to_path_buf();
let shown = path.to_string_lossy();
// Some sources hand a link over as if it were a file.
if shown.contains("://") {
texts.push(shown.into_owned());
} else {
out.push(Request { list: Some(path), ..Request::default() });
}
}
if !texts.is_empty() {
out.push(Request { text: texts.join("\n"), ..Request::default() });
}
out
}
/// Something is over the window.
pub fn hovering(ctx: &Context) -> bool {
ctx.input(|i| !i.raw.hovered_files.is_empty())
}
/// How much the window dims under the drop state.
const DIM: f32 = 0.86;
/// The full-window state: the window dims, a frame and a line say what happens. It fades in and out.
pub fn overlay(ctx: &Context, t: &Theme) {
let shown = crate::motion::spring(id().with("shown"), f32::from(hovering(ctx)), spring::EFFECT).clamp(0.0, 1.0);
if shown <= 0.0 {
return;
}
let rect = ctx.content_rect();
let p = ctx.layer_painter(LayerId::new(Order::Foreground, id()));
p.rect_filled(rect, 0.0, alpha(t.surface, DIM * shown));
let frame = rect.shrink(space::L);
p.rect_stroke(frame, cr(radius::L), Stroke::new(size::FOCUS_RING, alpha(t.primary, shown)), StrokeKind::Inside);
p.text(frame.center(), Align2::CENTER_CENTER, tr!("drop.here"), fonts::med(text::TITLE), alpha(t.on_surface, shown));
let below = frame.center() + eframe::egui::vec2(0.0, text::TITLE + space::M);
p.text(below, Align2::CENTER_CENTER, tr!("drop.what"), fonts::ui(text::BODY), alpha(t.on_variant, shown));
}