liora-components 0.1.11

Enterprise-style native GPUI component library for Liora applications.
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
//! Dialog module.
//!
//! This public module implements the Liora modal dialog component with controlled close policies. It keeps the reusable
//! component logic inside `liora-components` rather than Gallery or Docs so
//! downstream GPUI applications can compose the same behavior with their own
//! app state, assets, and release policy.
//!
//! ## Usage model
//!
//! Components in this module render native GPUI element trees. Stateless builder
//! values can be constructed inline, while controls with focus, selection,
//! popup, drag, or editing state should be stored as `gpui::Entity<T>` fields in
//! the parent view so state survives GPUI render passes.
//!
//! ## Design contract
//!
//! The implementation should use Liora theme tokens from `liora-core` and
//! `liora-theme`, keep accessibility-oriented keyboard/pointer behavior close to
//! the component, and avoid app-specific Gallery/Docs resources in this SDK
//! crate.

use crate::gpui_compat::element_id;
use crate::motion::{fade_in, pop_in};
use gpui::{
    AnyElement, App, Context, FocusHandle, Focusable, IntoElement, KeyBinding, MouseButton, Render,
    SharedString, Window, actions, div, prelude::*, px,
};
use liora_core::Config;
use liora_icons::Icon;
use liora_icons_lucide::IconName;
use std::{collections::HashMap, sync::Arc};

type DialogCloseCallback = Arc<dyn Fn(&mut Window, &mut App) + 'static>;

#[derive(Default)]
struct ActiveDialogRuntime {
    close_on_escape: HashMap<SharedString, bool>,
    on_close: HashMap<SharedString, DialogCloseCallback>,
}

impl gpui::Global for ActiveDialogRuntime {}

struct DialogEscapeInterceptorInstalled;
impl gpui::Global for DialogEscapeInterceptorInstalled {}

#[derive(Debug, Clone, PartialEq, Eq)]
enum DialogEscapeDecision {
    Close(SharedString),
    Block,
    Ignore,
}

fn ensure_dialog_runtime(cx: &mut App) {
    if !cx.has_global::<ActiveDialogRuntime>() {
        cx.set_global(ActiveDialogRuntime::default());
    }

    if !cx.has_global::<DialogEscapeInterceptorInstalled>() {
        cx.intercept_keystrokes(|event, window, cx| {
            if event.keystroke.key == "escape" {
                close_top_dialog_from_window(window, cx);
            }
        })
        .detach();
        cx.set_global(DialogEscapeInterceptorInstalled);
    }
}

fn register_dialog_runtime(
    id: SharedString,
    close_on_escape: bool,
    on_close: DialogCloseCallback,
    cx: &mut App,
) {
    ensure_dialog_runtime(cx);
    let runtime = cx.global_mut::<ActiveDialogRuntime>();
    runtime.close_on_escape.insert(id.clone(), close_on_escape);
    runtime.on_close.insert(id, on_close);
}

fn unregister_dialog_runtime(id: &SharedString, cx: &mut App) {
    if !cx.has_global::<ActiveDialogRuntime>() {
        return;
    }
    let runtime = cx.global_mut::<ActiveDialogRuntime>();
    runtime.close_on_escape.remove(id);
    runtime.on_close.remove(id);
}

fn clear_dialog_runtime(cx: &mut App) {
    if !cx.has_global::<ActiveDialogRuntime>() {
        return;
    }
    let runtime = cx.global_mut::<ActiveDialogRuntime>();
    runtime.close_on_escape.clear();
    runtime.on_close.clear();
}

fn active_modal_top_id(cx: &App) -> Option<SharedString> {
    cx.try_global::<liora_core::ActiveModal>()
        .and_then(|modal| modal.0.last().map(|entry| entry.id.clone()))
}

fn dialog_escape_decision_for(
    runtime: Option<&ActiveDialogRuntime>,
    active_modal_id: Option<&SharedString>,
) -> DialogEscapeDecision {
    let Some(runtime) = runtime else {
        return DialogEscapeDecision::Ignore;
    };
    let Some(id) = active_modal_id else {
        return DialogEscapeDecision::Ignore;
    };
    if !runtime.close_on_escape.contains_key(id) && !runtime.on_close.contains_key(id) {
        return DialogEscapeDecision::Ignore;
    }
    if runtime.close_on_escape.get(id).copied().unwrap_or(true) {
        DialogEscapeDecision::Close(id.clone())
    } else {
        DialogEscapeDecision::Block
    }
}

fn dialog_escape_decision(cx: &App) -> DialogEscapeDecision {
    let active_modal_id = active_modal_top_id(cx);
    dialog_escape_decision_for(
        cx.try_global::<ActiveDialogRuntime>(),
        active_modal_id.as_ref(),
    )
}

fn close_dialog_by_id(id: SharedString, window: &mut Window, cx: &mut App) {
    let callback = cx
        .try_global::<ActiveDialogRuntime>()
        .and_then(|runtime| runtime.on_close.get(&id).cloned());
    if let Some(callback) = callback {
        callback(window, cx);
    } else {
        unregister_dialog_runtime(&id, cx);
        liora_core::clear_modal(&id, cx);
        cx.refresh_windows();
    }
}

fn close_top_dialog_from_window(window: &mut Window, cx: &mut App) {
    match dialog_escape_decision(cx) {
        DialogEscapeDecision::Close(id) => {
            close_dialog_by_id(id, window, cx);
            cx.stop_propagation();
        }
        DialogEscapeDecision::Block => {
            cx.stop_propagation();
        }
        DialogEscapeDecision::Ignore => {}
    }
}

fn close_top_dialog_if_escape_enabled(cx: &mut App) {
    match dialog_escape_decision(cx) {
        DialogEscapeDecision::Close(id) => {
            if let Some(window) = cx.active_window() {
                cx.defer(move |cx| {
                    let _ = window.update(cx, |_, window, cx| {
                        close_dialog_by_id(id, window, cx);
                    });
                });
            }
        }
        DialogEscapeDecision::Block => {}
        DialogEscapeDecision::Ignore => cx.propagate(),
    }
}

actions!(
    dialog,
    [
        #[doc = "Keyboard action that closes the active dialog when dismissal is allowed."]
        DialogClose
    ]
);

/// Fluent native GPUI component for rendering Liora dialog.
pub struct Dialog {
    id: SharedString,
    title: SharedString,
    content: Arc<dyn Fn(&mut Window, &mut Context<DialogView>) -> AnyElement + 'static>,
    close_on_click_outside: bool,
    close_on_escape: bool,
    animated: bool,
    on_close: Arc<dyn Fn(&mut Window, &mut App) + 'static>,
}

/// Fluent native GPUI component for rendering Liora dialog view.
pub struct DialogView {
    id: SharedString,
    title: SharedString,
    content: Arc<dyn Fn(&mut Window, &mut Context<Self>) -> AnyElement + 'static>,
    close_on_click_outside: bool,
    close_on_escape: bool,
    animated: bool,
    on_close: Arc<dyn Fn(&mut Window, &mut App) + 'static>,
    focus_handle: FocusHandle,
    focus_requested: bool,
}

impl DialogView {
    fn new(
        id: SharedString,
        title: SharedString,
        content: Arc<dyn Fn(&mut Window, &mut Context<Self>) -> AnyElement + 'static>,
        close_on_click_outside: bool,
        close_on_escape: bool,
        animated: bool,
        on_close: impl Fn(&mut Window, &mut App) + 'static,
        focus_handle: FocusHandle,
        focus_requested: bool,
    ) -> Self {
        Self {
            id,
            title,
            content,
            close_on_click_outside,
            close_on_escape,
            animated,
            on_close: Arc::new(on_close),
            focus_handle,
            focus_requested,
        }
    }
}

impl Focusable for DialogView {
    fn focus_handle(&self, _cx: &App) -> FocusHandle {
        self.focus_handle.clone()
    }
}

impl Render for DialogView {
    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
        let theme = cx.global::<Config>().theme.clone();
        let id = self.id.clone();
        let title = self.title.clone();
        let content_fn = self.content.clone();
        let on_close = self.on_close.clone();
        let on_close_for_close_button = on_close.clone();
        let on_close_for_outside_click = on_close.clone();
        let on_close_for_escape = on_close;
        let close_on_click_outside = self.close_on_click_outside;
        let close_on_escape = self.close_on_escape;
        let animated = self.animated;
        let focus_handle = self.focus_handle(cx);
        if !self.focus_requested && animated {
            self.focus_requested = true;
            let focus_handle = focus_handle.clone();
            window.defer(cx, move |window, cx| window.focus(&focus_handle, cx));
        } else if !self.focus_requested {
            self.focus_requested = true;
        }

        let panel = div()
            .w_full()
            .max_w(px(420.0))
            .min_w(px(0.0))
            .mx_4()
            .bg(theme.neutral.card)
            .cursor_default()
            .rounded(px(theme.radius.md))
            .shadow_xl()
            .on_mouse_move(|_, _, cx| {
                cx.stop_propagation();
            })
            .on_mouse_down(MouseButton::Left, |_, _, cx| {
                cx.stop_propagation();
            }) // Consume click so it doesn't trigger the background
            .child(
                div()
                    .p_4()
                    .min_w(px(0.0))
                    .border_b_1()
                    .border_color(theme.neutral.border)
                    .flex()
                    .justify_between()
                    .items_center()
                    .child(
                        div()
                            .min_w(px(0.0))
                            .flex_1()
                            .font_weight(gpui::FontWeight::BOLD)
                            .text_color(theme.neutral.text_1)
                            .whitespace_normal()
                            .child(title),
                    )
                    .child(
                        div()
                            .id(element_id(format!("{id}-close-btn")))
                            .cursor_pointer()
                            .child(
                                Icon::new(IconName::X)
                                    .size(px(16.0))
                                    .color(theme.neutral.icon),
                            )
                            .on_mouse_down(MouseButton::Left, move |_, window, cx| {
                                on_close_for_close_button(window, cx);
                            }),
                    ),
            )
            .child(
                div()
                    .p_4()
                    .min_w(px(0.0))
                    .text_color(theme.neutral.text_2)
                    .overflow_hidden()
                    .child(content_fn(window, cx)),
            );

        let overlay = div()
            .id(id.clone())
            .track_focus(&focus_handle)
            .absolute()
            .size_full()
            .cursor_default()
            .bg(theme.neutral.overlay)
            .flex()
            .items_center()
            .justify_center()
            .on_mouse_move(|_, _, cx| {
                cx.stop_propagation();
            })
            .when(close_on_click_outside, |s| {
                s.on_mouse_down(MouseButton::Left, {
                    let on_close = on_close_for_outside_click.clone();
                    move |_, window, cx| {
                        on_close(window, cx);
                    }
                })
            })
            .when(close_on_escape, |s| {
                s.on_action(cx.listener({
                    let on_close = on_close_for_escape.clone();
                    move |_, _action: &DialogClose, window, cx| {
                        on_close(window, cx);
                    }
                }))
            });

        if animated {
            fade_in(
                element_id(format!("{id}-overlay-motion")),
                overlay.child(pop_in(element_id(format!("{id}-panel-motion")), panel)),
            )
            .into_any_element()
        } else {
            overlay.child(panel).into_any_element()
        }
    }
}

#[cfg(test)]
mod motion_tests {
    #[test]
    fn dialog_panel_is_responsive_and_text_wraps() {
        let source = include_str!("dialog.rs")
            .split("#[cfg(test)]")
            .next()
            .unwrap();

        assert!(source.contains(".w_full()"));
        assert!(source.contains(".max_w(px(420.0))"));
        assert!(source.contains(".min_w(px(0.0))"));
        assert!(source.contains(".mx_4()"));
        assert!(source.contains(".overflow_hidden()"));
        assert!(source.contains(".whitespace_normal()"));
    }

    #[test]
    fn dialog_uses_liora_motion_on_overlay_and_panel() {
        let source = include_str!("dialog.rs")
            .split("#[cfg(test)]")
            .next()
            .unwrap();

        assert!(source.contains("fade_in("));
        assert!(source.contains("pop_in("));
        assert!(source.contains("panel-motion"));
    }

    #[test]
    fn dialog_can_skip_intro_motion_for_latency_sensitive_surfaces() {
        let source = include_str!("dialog.rs");
        let impl_source = source
            .rsplit("\nimpl Dialog {")
            .next()
            .expect("Dialog builder implementation should exist");

        assert!(source.contains("animated: bool"));
        assert!(source.contains("if animated {"));
        assert!(source.contains("overlay.child(panel).into_any_element()"));
        assert!(impl_source.contains("pub fn animated(mut self, animated: bool) -> Self"));
        assert!(impl_source.contains("pub fn immediate(self) -> Self"));
        assert!(impl_source.contains("self.animated(false)"));
        assert!(
            impl_source.contains("pub fn show_in_window(self, window: &mut Window, cx: &mut App)")
        );
        assert!(impl_source.contains("window.focus(&focus_handle, cx)"));
        assert!(impl_source.contains("window.refresh()"));
    }

    #[test]
    fn dialog_exposes_on_close_for_host_state_cleanup() {
        let source = include_str!("dialog.rs");
        let impl_source = source
            .rsplit("\nimpl Dialog {")
            .next()
            .expect("Dialog builder implementation should exist");

        assert!(source.contains("on_close: Arc<dyn Fn(&mut Window, &mut App)"));
        assert!(impl_source.contains("pub fn on_close("));
        assert!(impl_source.contains("user_on_close(window, cx);"));
    }

    #[test]
    fn dialog_supports_focus_and_global_escape_dismissal() {
        let full_source = include_str!("dialog.rs");
        let source = full_source.split("#[cfg(test)]").next().unwrap();

        assert!(source.contains("impl Focusable for DialogView"));
        assert!(source.contains("focus_handle: FocusHandle"));
        assert!(source.contains(".track_focus(&focus_handle)"));
        assert!(source.contains("!self.focus_requested && animated"));
        assert!(source.contains("window.defer(cx, move |window, cx|"));
        assert!(source.contains("window.focus(&focus_handle, cx)"));
        assert!(source.contains("s.on_action(cx.listener({"));
        assert!(source.contains("struct ActiveDialogRuntime"));
        assert!(source.contains("struct DialogEscapeInterceptorInstalled"));
        assert!(source.contains("cx.intercept_keystrokes"));
        assert!(source.contains(".detach();"));
        assert!(full_source.contains("cx.on_action(|_: &DialogClose, cx|"));
        assert!(source.contains("close_top_dialog_from_window"));
        assert!(source.contains("close_top_dialog_if_escape_enabled"));
        assert!(full_source.contains("unregister_dialog_runtime(&id_for_close, cx)"));
        assert!(full_source.contains("cx.refresh_windows();"));
    }
}

#[cfg(test)]
mod runtime_tests {
    use super::*;

    fn runtime_with(entries: &[(&str, bool)]) -> ActiveDialogRuntime {
        let mut runtime = ActiveDialogRuntime::default();
        for (id, close_on_escape) in entries {
            let id: SharedString = id.to_string().into();
            runtime.close_on_escape.insert(id, *close_on_escape);
        }
        runtime
    }

    #[test]
    fn dialog_escape_decision_ignores_without_active_dialog_match() {
        assert_eq!(
            dialog_escape_decision_for(None, Some(&"dialog-a".into())),
            DialogEscapeDecision::Ignore
        );

        let runtime = runtime_with(&[("dialog-a", true)]);
        assert_eq!(
            dialog_escape_decision_for(Some(&runtime), Some(&"tour-a".into())),
            DialogEscapeDecision::Ignore
        );
        assert_eq!(
            dialog_escape_decision_for(Some(&runtime), None),
            DialogEscapeDecision::Ignore
        );
    }

    #[test]
    fn dialog_escape_decision_respects_top_dialog_close_policy() {
        let runtime = runtime_with(&[("dialog-a", true), ("dialog-b", false)]);
        assert_eq!(
            dialog_escape_decision_for(Some(&runtime), Some(&"dialog-b".into())),
            DialogEscapeDecision::Block
        );
        assert_eq!(
            dialog_escape_decision_for(Some(&runtime), Some(&"dialog-a".into())),
            DialogEscapeDecision::Close("dialog-a".into())
        );

        let runtime = runtime_with(&[("dialog-a", false), ("dialog-b", true)]);
        assert_eq!(
            dialog_escape_decision_for(Some(&runtime), Some(&"dialog-b".into())),
            DialogEscapeDecision::Close("dialog-b".into())
        );
    }

    #[test]
    fn dialog_escape_decision_uses_active_modal_as_source_of_truth() {
        let runtime = runtime_with(&[("dialog-a", true), ("stale-dialog", false)]);

        assert_eq!(
            dialog_escape_decision_for(Some(&runtime), Some(&"dialog-a".into())),
            DialogEscapeDecision::Close("dialog-a".into())
        );
        assert_eq!(
            dialog_escape_decision_for(Some(&runtime), Some(&"tour-a".into())),
            DialogEscapeDecision::Ignore
        );
    }

    #[test]
    fn dialog_escape_interceptor_is_process_once_not_runtime_state() {
        let source = include_str!("dialog.rs")
            .split("#[cfg(test)]")
            .next()
            .unwrap();
        let runtime_struct = source
            .split("struct ActiveDialogRuntime")
            .nth(1)
            .and_then(|part| {
                part.split("impl gpui::Global for ActiveDialogRuntime")
                    .next()
            })
            .expect("ActiveDialogRuntime should be defined before its Global impl");

        assert!(source.contains("struct DialogEscapeInterceptorInstalled"));
        assert!(source.contains("!cx.has_global::<DialogEscapeInterceptorInstalled>()"));
        assert!(source.contains("cx.set_global(DialogEscapeInterceptorInstalled)"));
        assert!(source.contains(".detach();"));
        assert!(!runtime_struct.contains("Subscription"));
        assert!(!runtime_struct.contains("escape_interceptor"));
        assert!(!runtime_struct.contains("stack"));
    }
}

impl Dialog {
    /// Registers GPUI key bindings required for keyboard interaction.
    pub fn register_key_bindings(cx: &mut App) {
        ensure_dialog_runtime(cx);
        cx.bind_keys([KeyBinding::new("escape", DialogClose, None)]);
        cx.on_action(|_: &DialogClose, cx| {
            close_top_dialog_if_escape_enabled(cx);
        });
    }

    /// Creates `Dialog` with default theme-driven styling and no optional callbacks attached.
    pub fn new() -> Self {
        Self {
            id: liora_core::unique_id("dialog"),
            title: SharedString::default(),
            content: Arc::new(|_, _| div().child("Dialog Content").into_any_element()),
            close_on_click_outside: true,
            close_on_escape: true,
            animated: true,
            on_close: Arc::new(|_, _| {}),
        }
    }

    /// Assigns a stable element id used by GPUI state, hit testing, and automated interaction tests.
    pub fn id(mut self, id: impl Into<SharedString>) -> Self {
        self.id = id.into();
        self
    }

    /// Sets the primary title text displayed by the component.
    pub fn title(mut self, title: impl Into<SharedString>) -> Self {
        self.title = title.into();
        self
    }

    /// Toggles whether the popup closes when click outside occurs.
    pub fn close_on_click_outside(mut self, c: bool) -> Self {
        self.close_on_click_outside = c;
        self
    }

    /// Toggles whether the popup closes when escape occurs.
    pub fn close_on_escape(mut self, c: bool) -> Self {
        self.close_on_escape = c;
        self
    }

    /// Toggles whether the dialog uses its default intro motion.
    pub fn animated(mut self, animated: bool) -> Self {
        self.animated = animated;
        self
    }

    /// Disables intro motion so latency-sensitive host flows can appear immediately.
    pub fn immediate(self) -> Self {
        self.animated(false)
    }

    /// Registers a callback invoked when the dialog is dismissed by its built-in close controls.
    pub fn on_close(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
        self.on_close = Arc::new(f);
        self
    }

    /// Sets the rendered content element or text for this component.
    pub fn content<F, E>(mut self, f: F) -> Self
    where
        F: Fn(&mut Window, &mut Context<DialogView>) -> E + 'static,
        E: IntoElement,
    {
        self.content = Arc::new(move |window, cx| f(window, cx).into_any_element());
        self
    }

    /// Performs the show operation used by this component.
    pub fn show(self, cx: &mut App) {
        self.show_with_window(None, cx);
    }

    /// Shows the dialog from an existing window and focuses the dialog before the next render pass.
    pub fn show_in_window(self, window: &mut Window, cx: &mut App) {
        self.show_with_window(Some(window), cx);
    }

    fn show_with_window(self, focus_window: Option<&mut Window>, cx: &mut App) {
        let focus_requested = focus_window.is_some() || !self.animated;
        let id = self.id;
        let title = self.title;
        let content = self.content;
        let close_on_click_outside = self.close_on_click_outside;
        let close_on_escape = self.close_on_escape;
        let animated = self.animated;
        let user_on_close = self.on_close;

        let id_for_close = id.clone();
        let close_callback: DialogCloseCallback = Arc::new(move |window, cx| {
            unregister_dialog_runtime(&id_for_close, cx);
            liora_core::clear_modal(&id_for_close, cx);
            user_on_close(window, cx);
            cx.refresh_windows();
        });
        let id_for_view = id.clone();
        let close_callback_for_view = close_callback.clone();
        let view = cx.new(move |cx| {
            let focus_handle = cx.focus_handle();
            DialogView::new(
                id_for_view.clone(),
                title,
                content,
                close_on_click_outside,
                close_on_escape,
                animated,
                move |window, cx| {
                    close_callback_for_view(window, cx);
                },
                focus_handle,
                focus_requested,
            )
        });
        let view_for_focus = view.clone();

        register_dialog_runtime(id.clone(), close_on_escape, close_callback, cx);
        liora_core::set_active_modal(id, view.into(), cx);
        if let Some(window) = focus_window {
            let focus_handle = view_for_focus.read(cx).focus_handle(cx);
            window.focus(&focus_handle, cx);
            window.refresh();
        }
        cx.refresh_windows();
    }

    /// Performs the close operation used by this component.
    pub fn close(cx: &mut App) {
        clear_dialog_runtime(cx);
        liora_core::clear_active_modal(cx);
        cx.refresh_windows();
    }

    /// Performs the close id operation used by this component.
    pub fn close_id(id: impl Into<SharedString>, cx: &mut App) {
        let id = id.into();
        unregister_dialog_runtime(&id, cx);
        liora_core::clear_modal(&id, cx);
        cx.refresh_windows();
    }
}