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
//! APIs for the construction of widgets, and a few common ones.
//!
//! This module describes two types of widget, [`NormalWidget`]s and
//! [`ActionableWidget`]s. [`NormalWidget`]s simply show information,
//! and cannot receive input or be focused. [`ActionableWidget`] is a
//! superset of [`NormalWidget`], capable of receiving input,
//! focusing, unfocusing, and showing cursors.
//!
//! The module also provides 4 native widgets, [`StatusLine`] and
//! [`LineNumbers`], which are [`NormalWidget`]s, and
//! [`FileWidget`] and [`CommandLine`] which are
//! [`ActionableWidget`]s.
//!
//! These widgets are supposed to be universal, not needing a specific
//! [`Ui`] implementation to work. As an example, the
//! [`parsec-term`](https://docs.rs/parsec-term) crate, which is a ui
//! implementation for Parsec, defines "rule" widgets, which are
//! separators that only really make sense in the context of a
//! terminal.
#[cfg(not(feature = "deadlock-detection"))]
use std::sync::RwLock;
use std::{
    any::TypeId,
    collections::HashMap,
    sync::{Arc, LazyLock},
};

use crossterm::event::KeyEvent;
#[cfg(feature = "deadlock-detection")]
use no_deadlocks::RwLock;

pub use crate::file::{File, FileCfg};
use crate::{
    data::{Data, RwData},
    input::InputMethod,
    palette,
    text::{PrintCfg, Text},
    ui::{Area, PushSpecs, Ui},
    Globals,
};

/// An area where text will be printed to the screen.
pub trait PassiveWidget<U>: Send + Sync + 'static
where
    U: Ui,
{
    fn build(
        globals: Globals<U>,
        on_file: bool,
    ) -> (Widget<U>, impl Fn() -> bool + 'static, PushSpecs)
    where
        Self: Sized;

    /// Updates the widget, allowing the modification of its
    /// [`Area`][Ui::Area].
    ///
    /// This function will be called when Parsec determines that the
    /// [`WidgetNode`]
    ///
    /// [`Session`]: crate::session::Session
    fn update(&mut self, area: &U::Area);

    /// The text that this widget prints out.
    fn text(&self) -> &Text;

    fn print_cfg(&self) -> &PrintCfg {
        static CFG: LazyLock<PrintCfg> = LazyLock::new(PrintCfg::default);

        &CFG
    }

    fn print(&mut self, area: &U::Area) {
        area.print(self.text(), self.print_cfg(), palette::painter())
    }

    fn once(globals: Globals<U>)
    where
        Self: Sized;

    fn name() -> &'static str
    where
        Self: Sized,
    {
        static NAMES: LazyLock<RwLock<HashMap<TypeId, &'static str>>> =
            LazyLock::new(|| RwLock::new(HashMap::new()));
        let mut names = NAMES.write().unwrap();
        let type_id = TypeId::of::<Self>();

        if let Some(name) = names.get(&type_id) {
            name
        } else {
            let verbose = std::any::type_name::<Self>();
            let mut name = String::new();

            for path in verbose.split_inclusive(['<', '>']) {
                for segment in path.split("::") {
                    if segment.chars().any(|char| char.is_ascii_uppercase()) {
                        name.push_str(segment);
                    }
                }
            }

            names.insert(type_id, name.leak());
            names.get(&type_id).unwrap()
        }
    }
}

#[allow(refining_impl_trait)]
pub trait WidgetCfg<U>: Sized
where
    U: Ui,
{
    type Widget: PassiveWidget<U>;
    fn build(
        self,
        globals: Globals<U>,
        on_file: bool,
    ) -> (Widget<U>, impl Fn() -> bool + 'static, PushSpecs);
}

/// A widget that can receive input and show [`Cursor`]s.
pub trait ActiveWidget<U>: PassiveWidget<U>
where
    U: Ui,
{
    /// A mutable reference to the [`Text`] printed by this cursor.
    fn mut_text(&mut self) -> &mut Text;

    /// Actions to do whenever this [`ActionableWidget`] is focused.
    fn on_focus(&mut self, _area: &U::Area);

    /// Actions to do whenever this [`ActionableWidget`] is unfocused.
    fn on_unfocus(&mut self, _area: &U::Area);
}

#[allow(private_interfaces)]
trait ActiveHolder<U>: Send + Sync
where
    U: Ui,
{
    // General widget methods
    fn passive_widget(&self) -> &RwData<dyn PassiveWidget<U>>;

    /// Updates the widget, allowing the modification of its
    /// [`Area`][Ui::Area].
    ///
    /// This function will be called when Parsec determines that the
    /// [`WidgetNode`]
    ///
    /// [`Session`]: crate::session::Session
    fn update_and_print(&self, area: &U::Area);

    fn update(&self, area: &U::Area);

    fn type_name(&self) -> &'static str;

    // Active widget methods
    fn active_widget(&self) -> &RwData<dyn ActiveWidget<U>>;

    fn input(&self) -> &RwData<dyn InputMethod<U>>;

    fn send_key(&self, key: KeyEvent, area: &U::Area, globals: Globals<U>);

    fn on_focus(&self, area: &U::Area);

    fn on_unfocus(&self, area: &U::Area);
}

struct InnerActiveWidget<W, I, U>
where
    W: ActiveWidget<U>,
    I: InputMethod<U, Widget = W>,
    U: Ui,
{
    widget: RwData<W>,
    dyn_active: RwData<dyn ActiveWidget<U>>,
    dyn_passive: RwData<dyn PassiveWidget<U>>,
    input: RwData<I>,
    dyn_input: RwData<dyn InputMethod<U>>,
}

impl<W, I, U> ActiveHolder<U> for InnerActiveWidget<W, I, U>
where
    W: ActiveWidget<U>,
    I: InputMethod<U, Widget = W>,
    U: Ui,
{
    fn passive_widget(&self) -> &RwData<dyn PassiveWidget<U>> {
        &self.dyn_passive
    }

    fn update_and_print(&self, area: &U::Area) {
        let mut widget = self.widget.raw_write();
        widget.update(area);
        widget.print(area);
    }

    fn update(&self, area: &<U as Ui>::Area) {
        self.widget.raw_write().update(area)
    }

    fn type_name(&self) -> &'static str {
        W::name()
    }

    fn active_widget(&self) -> &RwData<dyn ActiveWidget<U>> {
        &self.dyn_active
    }

    fn input(&self) -> &RwData<dyn InputMethod<U>> {
        &self.dyn_input
    }

    fn send_key(&self, key: KeyEvent, area: &U::Area, globals: Globals<U>) {
        let mut input = self.input.write();

        if let Some(cursors) = input.cursors() {
            self.widget.write().mut_text().remove_cursor_tags(cursors);
        }

        input.send_key(key, &self.widget, area, globals);

        if let Some(cursors) = input.cursors() {
            let mut widget = self.widget.write();
            widget.mut_text().add_cursor_tags(cursors);

            area.scroll_around_point(widget.text(), cursors.main().caret(), widget.print_cfg());

            widget.update(area);
            widget.print(area);
        }
    }

    fn on_focus(&self, area: &<U as Ui>::Area) {
        self.input.mutate(|input| input.on_focus(area));
        self.widget.mutate(|widget| widget.on_focus(area));
    }

    fn on_unfocus(&self, area: &<U as Ui>::Area) {
        self.input.mutate(|input| input.on_unfocus(area));
        self.widget.mutate(|widget| widget.on_unfocus(area));
    }
}

impl<W, I, U> Clone for InnerActiveWidget<W, I, U>
where
    W: ActiveWidget<U>,
    I: InputMethod<U, Widget = W>,
    U: Ui,
{
    fn clone(&self) -> Self {
        Self {
            widget: self.widget.clone(),
            dyn_active: self.dyn_active.clone(),
            dyn_passive: self.dyn_passive.clone(),
            input: self.input.clone(),
            dyn_input: self.dyn_input.clone(),
        }
    }
}

#[allow(private_interfaces)]
pub enum Widget<U>
where
    U: Ui,
{
    Passive(RwData<dyn PassiveWidget<U>>, &'static str),
    Active(Arc<dyn ActiveHolder<U>>),
}

impl<U> Clone for Widget<U>
where
    U: Ui,
{
    fn clone(&self) -> Self {
        match self {
            Self::Passive(widget, name) => Self::Passive(widget.clone(), name),
            Self::Active(widget) => Self::Active(widget.clone()),
        }
    }
}

impl<U> Widget<U>
where
    U: Ui,
{
    pub fn passive<W>(widget: W) -> Self
    where
        W: PassiveWidget<U>,
    {
        Widget::Passive(
            RwData::new_unsized::<W>(Arc::new(RwLock::new(widget))),
            W::name(),
        )
    }

    pub fn active<W, I>(widget: W, input: RwData<I>) -> Self
    where
        W: ActiveWidget<U>,
        I: InputMethod<U, Widget = W>,
    {
        let dyn_active: RwData<dyn ActiveWidget<U>> =
            RwData::new_unsized::<W>(Arc::new(RwLock::new(widget)));
        let dyn_passive = dyn_active.clone().to_passive();

        let input_data = input.inner_arc().clone() as Arc<RwLock<dyn InputMethod<U>>>;

        let inner = InnerActiveWidget {
            widget: dyn_active.clone().try_downcast::<W>().unwrap(),
            dyn_active,
            dyn_passive,
            input,
            dyn_input: RwData::new_unsized::<I>(input_data),
        };

        Widget::Active(Arc::new(inner))
    }

    pub fn update_and_print(&self, area: &U::Area) {
        match self {
            Widget::Passive(widget, _) => {
                let mut widget = widget.raw_write();
                widget.update(area);
                widget.print(area);
            }
            Widget::Active(holder) => {
                holder.update_and_print(area);
            }
        }
    }

    /// Returns the downcast ref of this [`Widget`].
    pub fn downcast<W>(&self) -> Option<RwData<W>>
    where
        W: PassiveWidget<U>,
    {
        match self {
            Widget::Passive(widget, _) => widget.clone().try_downcast::<W>().ok(),
            Widget::Active(holder) => holder.active_widget().clone().try_downcast::<W>().ok(),
        }
    }

    pub fn data_is<W>(&self) -> bool
    where
        W: 'static,
    {
        match self {
            Widget::Passive(widget, _) => widget.data_is::<W>(),
            Widget::Active(holder) => holder.active_widget().data_is::<W>(),
        }
    }

    pub fn inspect_as<W, B>(&self, f: impl FnOnce(&W) -> B) -> Option<B>
    where
        W: PassiveWidget<U>,
    {
        match self {
            Widget::Passive(widget, _) => widget.inspect_as::<W, B>(f),
            Widget::Active(holder) => holder.active_widget().inspect_as::<W, B>(f),
        }
    }

    pub fn as_passive(&self) -> &RwData<dyn PassiveWidget<U>> {
        match self {
            Widget::Passive(widget, _) => widget,
            Widget::Active(holder) => holder.passive_widget(),
        }
    }

    pub fn as_active(&self) -> Option<(&RwData<dyn ActiveWidget<U>>, &RwData<dyn InputMethod<U>>)> {
        match self {
            Widget::Active(holder) => Some((holder.active_widget(), holder.input())),
            _ => None,
        }
    }

    pub fn input(&self) -> Option<&RwData<dyn InputMethod<U>>> {
        match self {
            Widget::Passive(..) => None,
            Widget::Active(holder) => Some(holder.input()),
        }
    }

    pub fn ptr_eq<W, D>(&self, other: &D) -> bool
    where
        W: ?Sized,
        D: Data<W> + ?Sized,
    {
        match self {
            Widget::Passive(widget, _) => widget.ptr_eq(other),
            Widget::Active(holder) => holder.active_widget().ptr_eq(other),
        }
    }

    pub fn update(&self, area: &U::Area) {
        match self {
            Widget::Passive(widget, _) => widget.raw_write().update(area),
            Widget::Active(holder) => holder.update(area),
        }
    }

    pub fn type_name(&self) -> &'static str {
        match self {
            Widget::Passive(_, name) => name,
            Widget::Active(holder) => holder.type_name(),
        }
    }

    pub(crate) fn on_focus(&self, area: &U::Area) {
        match self {
            Widget::Passive(..) => {}
            Widget::Active(holder) => holder.on_focus(area),
        }
    }

    pub(crate) fn on_unfocus(&self, area: &U::Area) {
        match self {
            Widget::Passive(..) => {}
            Widget::Active(holder) => holder.on_unfocus(area),
        }
    }

    pub(crate) fn send_key(&self, key: KeyEvent, area: &U::Area, globals: Globals<U>) {
        match self {
            Widget::Passive(..) => unreachable!("Sending keys to passive widgets is impossible"),
            Widget::Active(holder) => holder.send_key(key, area, globals),
        }
    }

    pub(crate) fn raw_inspect<B>(&self, f: impl FnOnce(&dyn PassiveWidget<U>) -> B) -> B {
        match self {
            Widget::Passive(widget, _) => f(&*widget.raw_read()),
            Widget::Active(holder) => f(&*holder.active_widget().raw_read()),
        }
    }
}