pub(crate) mod calls;
use iced_core::{clipboard, window};
use icedwright_types::{VirtualDuration, VirtualInstant};
use crate::effects::UntypedTaskId;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Writer {
Widget(window::Id),
Task(UntypedTaskId),
Test,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ClipboardWrite {
pub kind: clipboard::Kind,
pub contents: String,
pub at: VirtualInstant,
pub by: Writer,
pub denied: bool,
}
#[derive(Debug, Clone)]
pub struct FakeClipboard {
standard: Option<String>,
primary: Option<String>,
denied: bool,
writes: Vec<ClipboardWrite>,
writer: Writer,
at: VirtualInstant,
}
impl Default for FakeClipboard {
fn default() -> Self {
Self {
standard: None,
primary: None,
denied: false,
writes: Vec::new(),
writer: Writer::Test,
at: VirtualInstant::__from_offset(VirtualDuration::ZERO),
}
}
}
impl FakeClipboard {
#[must_use]
pub fn with_text(text: impl Into<String>) -> Self {
Self {
standard: Some(text.into()),
..Self::default()
}
}
#[must_use]
pub fn text(&self) -> Option<&str> {
self.standard.as_deref()
}
#[must_use]
pub fn primary(&self) -> Option<&str> {
self.primary.as_deref()
}
pub fn set_text(&mut self, text: impl Into<String>) {
self.standard = Some(text.into());
}
pub fn set_primary(&mut self, text: impl Into<String>) {
self.primary = Some(text.into());
}
pub fn clear(&mut self) {
self.standard = None;
self.primary = None;
}
pub fn deny(&mut self, denied: bool) {
self.denied = denied;
}
#[must_use]
pub fn writes(&self) -> &[ClipboardWrite] {
&self.writes
}
pub(crate) fn attribute(&mut self, writer: Writer, at: VirtualInstant) {
self.writer = writer;
self.at = at;
}
pub(crate) fn read_kind(&self, kind: clipboard::Kind) -> Option<String> {
if self.denied {
return None;
}
match kind {
clipboard::Kind::Standard => self.standard.clone(),
clipboard::Kind::Primary => self.primary.clone(),
}
}
pub(crate) fn write_kind(&mut self, kind: clipboard::Kind, contents: String) {
self.writes.push(ClipboardWrite {
kind,
contents: contents.clone(),
at: self.at,
by: self.writer,
denied: self.denied,
});
if self.denied {
return;
}
match kind {
clipboard::Kind::Standard => self.standard = Some(contents),
clipboard::Kind::Primary => self.primary = Some(contents),
}
}
}
impl iced_core::Clipboard for FakeClipboard {
fn read(&self, kind: clipboard::Kind) -> Option<String> {
self.read_kind(kind)
}
fn write(&mut self, kind: clipboard::Kind, contents: String) {
self.write_kind(kind, contents);
}
}
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub enum WindowRequest {
Drag,
DragResize(window::Direction),
ShowSystemMenu,
RequestUserAttention(Option<window::UserAttention>),
SetIcon,
MousePassthrough(bool),
AutomaticTabbing(bool),
}
impl PartialEq for WindowRequest {
fn eq(&self, other: &Self) -> bool {
use WindowRequest::{
AutomaticTabbing, Drag, DragResize, MousePassthrough, RequestUserAttention, SetIcon,
ShowSystemMenu,
};
match (self, other) {
(Drag, Drag) | (ShowSystemMenu, ShowSystemMenu) | (SetIcon, SetIcon) => true,
(DragResize(a), DragResize(b)) => format!("{a:?}") == format!("{b:?}"),
(RequestUserAttention(a), RequestUserAttention(b)) => {
format!("{a:?}") == format!("{b:?}")
}
(MousePassthrough(a), MousePassthrough(b))
| (AutomaticTabbing(a), AutomaticTabbing(b)) => a == b,
_ => false,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use iced_core::Clipboard as _;
#[test]
fn standard_and_primary_are_separate() {
let mut clipboard = FakeClipboard::with_text("copied");
clipboard.write(clipboard::Kind::Primary, "selected".to_owned());
assert_eq!(clipboard.text(), Some("copied"));
assert_eq!(clipboard.primary(), Some("selected"));
assert_eq!(
clipboard.read(clipboard::Kind::Standard).as_deref(),
Some("copied")
);
assert_eq!(
clipboard.read(clipboard::Kind::Primary).as_deref(),
Some("selected")
);
clipboard.clear();
assert_eq!(clipboard.text(), None);
assert_eq!(clipboard.primary(), None);
}
#[test]
fn deny_drops_writes_records_them_and_reads_nothing() {
let mut clipboard = FakeClipboard::with_text("kept");
clipboard.deny(true);
clipboard.write(clipboard::Kind::Standard, "dropped".to_owned());
assert_eq!(clipboard.read(clipboard::Kind::Standard), None);
clipboard.deny(false);
assert_eq!(clipboard.text(), Some("kept"));
assert_eq!(clipboard.writes().len(), 1);
assert!(clipboard.writes()[0].denied);
assert_eq!(clipboard.writes()[0].contents, "dropped");
}
#[test]
fn writes_carry_their_writer_and_instant() {
let mut clipboard = FakeClipboard::default();
let window = window::Id::unique();
let at = VirtualInstant::__from_offset(VirtualDuration::from_millis(40));
clipboard.attribute(Writer::Widget(window), at);
clipboard.write(clipboard::Kind::Standard, "a".to_owned());
let task = UntypedTaskId::new(7);
clipboard.attribute(Writer::Task(task), at);
clipboard.write(clipboard::Kind::Primary, "b".to_owned());
let by: Vec<Writer> = clipboard.writes().iter().map(|w| w.by).collect();
assert_eq!(by, vec![Writer::Widget(window), Writer::Task(task)]);
assert!(clipboard.writes().iter().all(|w| w.at == at));
}
}