1
//! Clipboard Manager
2
//!
3
//! Manages clipboard content flow between the system clipboard and
4
//! the application.
5
//!
6
//! ## Architecture
7
//!
8
//! The clipboard manager acts as a bridge between system clipboard
9
//! operations and user callbacks:
10
//!
11
//! 1. **Paste Flow**: System clipboard → ClipboardManager → User Callback → TextInputManager
12
//!
13
//!    - When Ctrl+V is pressed, event_v2 reads system clipboard and calls `set_paste_content()`
14
//!    - User's On::Paste callback can inspect content via `get_clipboard_content()`
15
//!    - User can modify/block paste by not calling the default paste action
16
//!    - After callback, content is cleared for next operation
17
//!
18
//! 2. **Copy Flow**: Selection → User Callback → ClipboardManager → System clipboard
19
//!
20
//!    - When Ctrl+C is pressed, user's On::Copy callback fires
21
//!    - Callback can inspect selected content and override via `set_copy_content()`
22
//!    - event_v2 calls `get_copy_content()` to get final content (override or default)
23
//!    - Content is written to system clipboard via platform sync
24
//!    - After callback, content is cleared for next operation
25
//!
26
//! 3. **Cut Flow**: Same as Copy + delete selection
27

            
28
use crate::managers::selection::ClipboardContent;
29

            
30
/// Manages clipboard content flow between system clipboard and application
31
///
32
/// This manager temporarily holds clipboard content during clipboard operations,
33
/// allowing user callbacks to inspect and modify content before it's committed
34
/// to the system clipboard or pasted into the document.
35
#[derive(Debug, Clone, Default)]
36
pub struct ClipboardManager {
37
    /// Content from system clipboard when paste is triggered
38
    /// Available to user callbacks via `CallbackInfo::get_clipboard_content()`
39
    pending_paste_content: Option<ClipboardContent>,
40

            
41
    /// Content to be written to system clipboard after copy/cut
42
    /// Set by user callbacks via `CallbackInfo::set_copy_content()`
43
    pending_copy_content: Option<ClipboardContent>,
44
}
45

            
46
impl ClipboardManager {
47
    /// Create a new empty clipboard manager
48
2321
    pub fn new() -> Self {
49
2321
        Self {
50
2321
            pending_paste_content: None,
51
2321
            pending_copy_content: None,
52
2321
        }
53
2321
    }
54

            
55
    // Paste Operations (System → Application)
56

            
57
    /// Sets content from the system clipboard (called before paste callbacks).
58
    pub fn set_paste_content(&mut self, content: ClipboardContent) {
59
        self.pending_paste_content = Some(content);
60
    }
61

            
62
    /// Returns the pending paste content, if any.
63
    pub fn get_paste_content(&self) -> Option<&ClipboardContent> {
64
        self.pending_paste_content.as_ref()
65
    }
66

            
67
    // Copy Operations (Application → System)
68

            
69
    /// Sets content to be copied to the system clipboard.
70
    pub fn set_copy_content(&mut self, content: ClipboardContent) {
71
        self.pending_copy_content = Some(content);
72
    }
73

            
74
    /// Returns the pending copy content, if any.
75
    pub fn get_copy_content(&self) -> Option<&ClipboardContent> {
76
        self.pending_copy_content.as_ref()
77
    }
78

            
79
    /// Takes the copy content, consuming it.
80
    pub fn take_copy_content(&mut self) -> Option<ClipboardContent> {
81
        self.pending_copy_content.take()
82
    }
83

            
84
    // Lifecycle Management
85

            
86
    /// Clears all pending clipboard content.
87
    pub fn clear(&mut self) {
88
        self.pending_paste_content = None;
89
        self.pending_copy_content = None;
90
    }
91

            
92
    /// Clears only paste content.
93
    pub fn clear_paste(&mut self) {
94
        self.pending_paste_content = None;
95
    }
96

            
97
    /// Clears only copy content.
98
    pub fn clear_copy(&mut self) {
99
        self.pending_copy_content = None;
100
    }
101

            
102
    /// Returns `true` if there's pending paste content.
103
    pub fn has_paste_content(&self) -> bool {
104
        self.pending_paste_content.is_some()
105
    }
106

            
107
    /// Returns `true` if there's pending copy content.
108
    pub fn has_copy_content(&self) -> bool {
109
        self.pending_copy_content.is_some()
110
    }
111
}