revue 2.71.1

A Vue-style TUI framework for Rust with CSS styling
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//! Drop zone widget for drag-and-drop targets
//!
//! A configurable drop target area that accepts dragged items.
//!
//! # Example
//!
//! ```rust,ignore
//! use revue::widget::DropZone;
//!
//! DropZone::new("Drop files here")
//!     .accepts(&["file", "text"])
//!     .on_drop(|data| {
//!         println!("Dropped: {:?}", data);
//!         true
//!     })
//! ```

use std::sync::atomic::{AtomicU64, Ordering};

use crate::event::drag::{DragData, DragId, DropTarget};
use crate::impl_view_meta;
use crate::layout::Rect;
use crate::style::Color;
use crate::widget::theme::{DISABLED_FG, LIGHT_GRAY};
use crate::widget::traits::{Draggable, RenderContext, View, WidgetProps, WidgetState};

use super::types::DropZoneStyle;

/// Atomic counter for generating unique drop zone IDs
static DROPZONE_ID_COUNTER: AtomicU64 = AtomicU64::new(1);
/// Drop zone widget
pub struct DropZone<F = fn(DragData) -> bool>
where
    F: FnMut(DragData) -> bool,
{
    /// Unique identifier
    id: DragId,
    /// Placeholder text when empty
    placeholder: String,
    /// Accepted data types
    accepts: Vec<&'static str>,
    /// Visual style
    style: DropZoneStyle,
    /// Drop handler
    on_drop: Option<F>,
    /// Is currently hovered by a drag
    hovered: bool,
    /// Can accept current drag
    can_accept_current: bool,
    /// Normal border color
    border_color: Color,
    /// Hover border color
    hover_color: Color,
    /// Accept indicator color
    accept_color: Color,
    /// Reject indicator color
    reject_color: Color,
    /// Widget state
    state: WidgetState,
    /// Widget props for CSS
    props: WidgetProps,
    /// Minimum height
    min_height: u16,
}

impl DropZone<fn(DragData) -> bool> {
    /// Create a new drop zone with placeholder text
    pub fn new(placeholder: impl Into<String>) -> Self {
        let id = DROPZONE_ID_COUNTER.fetch_add(1, Ordering::Relaxed);

        Self {
            id,
            placeholder: placeholder.into(),
            accepts: Vec::new(),
            style: DropZoneStyle::default(),
            on_drop: None,
            hovered: false,
            can_accept_current: false,
            border_color: DISABLED_FG,
            hover_color: Color::rgb(100, 150, 255),
            accept_color: Color::rgb(100, 200, 100),
            reject_color: Color::rgb(200, 100, 100),
            state: WidgetState::new(),
            props: WidgetProps::new(),
            min_height: 3,
        }
    }
}

impl<F> DropZone<F>
where
    F: FnMut(DragData) -> bool,
{
    /// Set accepted data types
    pub fn accepts(mut self, types: &[&'static str]) -> Self {
        self.accepts = types.to_vec();
        self
    }

    /// Accept all data types
    pub fn accepts_all(mut self) -> Self {
        self.accepts.clear();
        self
    }

    /// Set visual style
    pub fn style(mut self, style: DropZoneStyle) -> Self {
        self.style = style;
        self
    }

    /// Set border color
    pub fn border_color(mut self, color: Color) -> Self {
        self.border_color = color;
        self
    }

    /// Set hover color
    pub fn hover_color(mut self, color: Color) -> Self {
        self.hover_color = color;
        self
    }

    /// Set minimum height
    pub fn min_height(mut self, height: u16) -> Self {
        self.min_height = height;
        self
    }

    /// Set drop handler
    pub fn on_drop<G>(self, handler: G) -> DropZone<G>
    where
        G: FnMut(DragData) -> bool,
    {
        DropZone {
            id: self.id,
            placeholder: self.placeholder,
            accepts: self.accepts,
            style: self.style,
            on_drop: Some(handler),
            hovered: self.hovered,
            can_accept_current: self.can_accept_current,
            border_color: self.border_color,
            hover_color: self.hover_color,
            accept_color: self.accept_color,
            reject_color: self.reject_color,
            state: self.state,
            props: self.props,
            min_height: self.min_height,
        }
    }

    /// Set hovered state (called by drag system)
    pub fn set_hovered(&mut self, hovered: bool, can_accept: bool) {
        self.hovered = hovered;
        self.can_accept_current = can_accept;
    }

    /// Get unique ID
    pub fn id(&self) -> DragId {
        self.id
    }

    /// Create a DropTarget for registration
    pub fn as_target(&self, bounds: Rect) -> DropTarget {
        DropTarget::new(self.id, bounds).accepts(&self.accepts)
    }

    /// Get current border color based on state
    fn current_border_color(&self) -> Color {
        if self.hovered {
            if self.can_accept_current {
                self.accept_color
            } else {
                self.reject_color
            }
        } else {
            self.border_color
        }
    }

    /// Get border characters based on style
    fn border_chars(&self) -> (char, char, char, char, char, char) {
        match self.style {
            DropZoneStyle::Solid => ('', '', '', '', '', ''),
            DropZoneStyle::Dashed => ('', '', '', '', '', ''),
            DropZoneStyle::Highlight | DropZoneStyle::Minimal => (' ', ' ', ' ', ' ', ' ', ' '),
        }
    }
}

impl<F> View for DropZone<F>
where
    F: FnMut(DragData) -> bool,
{
    fn render(&self, ctx: &mut RenderContext) {
        let area = ctx.area;
        let height = area.height.max(self.min_height);
        let color = self.current_border_color();

        match self.style {
            DropZoneStyle::Solid | DropZoneStyle::Dashed => {
                let (tl, tr, bl, br, h, v) = self.border_chars();

                // Top border
                if let Some(cell) = ctx.get_mut(0, 0) {
                    cell.symbol = tl;
                    cell.fg = Some(color);
                }
                for x in 1..area.width.saturating_sub(1) {
                    if let Some(cell) = ctx.get_mut(x, 0) {
                        cell.symbol = h;
                        cell.fg = Some(color);
                    }
                }
                if area.width > 1 {
                    if let Some(cell) = ctx.get_mut(area.width - 1, 0) {
                        cell.symbol = tr;
                        cell.fg = Some(color);
                    }
                }

                // Bottom border
                let bottom_y = height.saturating_sub(1);
                if let Some(cell) = ctx.get_mut(0, bottom_y) {
                    cell.symbol = bl;
                    cell.fg = Some(color);
                }
                for x in 1..area.width.saturating_sub(1) {
                    if let Some(cell) = ctx.get_mut(x, bottom_y) {
                        cell.symbol = h;
                        cell.fg = Some(color);
                    }
                }
                if area.width > 1 {
                    if let Some(cell) = ctx.get_mut(area.width - 1, bottom_y) {
                        cell.symbol = br;
                        cell.fg = Some(color);
                    }
                }

                // Side borders
                for y in 1..bottom_y {
                    if let Some(cell) = ctx.get_mut(0, y) {
                        cell.symbol = v;
                        cell.fg = Some(color);
                    }
                    if area.width > 1 {
                        if let Some(cell) = ctx.get_mut(area.width - 1, y) {
                            cell.symbol = v;
                            cell.fg = Some(color);
                        }
                    }
                }
            }
            DropZoneStyle::Highlight => {
                // Fill with background color when hovered
                if self.hovered {
                    let bg = if self.can_accept_current {
                        Color::rgb(30, 60, 30)
                    } else {
                        Color::rgb(60, 30, 30)
                    };
                    for y in 0..height {
                        for x in 0..area.width {
                            if let Some(cell) = ctx.get_mut(x, y) {
                                cell.bg = Some(bg);
                            }
                        }
                    }
                }
            }
            DropZoneStyle::Minimal => {
                // Just show indicator on left edge
                let indicator = if self.hovered {
                    if self.can_accept_current {
                        ''
                    } else {
                        ''
                    }
                } else {
                    ''
                };
                for y in 0..height {
                    if let Some(cell) = ctx.get_mut(0, y) {
                        cell.symbol = indicator;
                        cell.fg = Some(color);
                    }
                }
            }
        }

        // Placeholder text
        let text_y = height / 2;
        let text_x: u16 = 2;
        let max_len = area.width.saturating_sub(4) as usize;

        let display_text = if self.hovered {
            if self.can_accept_current {
                "Drop here!"
            } else {
                "Cannot drop here"
            }
        } else {
            &self.placeholder
        };

        let text_color = if self.hovered { color } else { LIGHT_GRAY };

        ctx.draw_text_clipped(text_x, text_y, display_text, text_color, max_len as u16);
    }

    impl_view_meta!("DropZone");
}

// Builder methods (manually implemented due to generic type parameter)
impl DropZone<fn(DragData) -> bool> {
    /// Set the focused state
    pub fn focused(mut self, focused: bool) -> Self {
        self.state.focused = focused;
        self
    }

    /// Set the disabled state
    pub fn disabled(mut self, disabled: bool) -> Self {
        self.state.disabled = disabled;
        self
    }

    /// Set the foreground color
    pub fn fg(mut self, color: Color) -> Self {
        self.state.fg = Some(color);
        self
    }

    /// Set the background color
    pub fn bg(mut self, color: Color) -> Self {
        self.state.bg = Some(color);
        self
    }

    /// Check if the widget is focused
    pub fn is_focused(&self) -> bool {
        self.state.focused
    }

    /// Check if the widget is disabled
    pub fn is_disabled(&self) -> bool {
        self.state.disabled
    }

    /// Set the focused state (mutable)
    pub fn set_focused(&mut self, focused: bool) {
        self.state.focused = focused;
    }

    /// Get hovered state for testing
    #[doc(hidden)]
    pub fn is_hovered(&self) -> bool {
        self.hovered
    }

    /// Get can_accept_current state for testing
    #[doc(hidden)]
    pub fn can_accept_current(&self) -> bool {
        self.can_accept_current
    }

    /// Get min_height for testing
    #[doc(hidden)]
    pub fn get_min_height(&self) -> u16 {
        self.min_height
    }
}

// StyledView trait for CSS class management
impl crate::widget::traits::StyledView for DropZone<fn(DragData) -> bool> {
    fn set_id(&mut self, id: impl Into<String>) {
        self.props.id = Some(id.into());
    }

    fn add_class(&mut self, class: impl Into<String>) {
        let class_str = class.into();
        if !self.props.classes.contains(&class_str) {
            self.props.classes.push(class_str);
        }
    }

    fn remove_class(&mut self, class: &str) {
        self.props.classes.retain(|c| c != class);
    }

    fn toggle_class(&mut self, class: &str) {
        if self.props.classes.contains(&class.to_string()) {
            self.remove_class(class);
        } else {
            self.add_class(class);
        }
    }

    fn has_class(&self, class: &str) -> bool {
        self.props.classes.contains(&class.to_string())
    }
}

impl<F> Draggable for DropZone<F>
where
    F: FnMut(DragData) -> bool,
{
    fn can_drop(&self) -> bool {
        true
    }

    fn accepted_types(&self) -> &[&'static str] {
        &self.accepts
    }

    fn on_drag_enter(&mut self, data: &DragData) {
        self.hovered = true;
        self.can_accept_current = self.can_accept(data);
    }

    fn on_drag_leave(&mut self) {
        self.hovered = false;
        self.can_accept_current = false;
    }

    fn on_drop(&mut self, data: DragData) -> bool {
        self.hovered = false;
        self.can_accept_current = false;

        if let Some(ref mut handler) = self.on_drop {
            handler(data)
        } else {
            false
        }
    }

    fn drop_bounds(&self, area: Rect) -> Rect {
        area
    }
}