liora-components 0.1.16

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
//! Preview module.
//!
//! This public module implements the Liora image preview overlay component. 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::image::{
    ImageRoundOptions, ImageSource, RasterImageElement, load_local_render_image,
    load_remote_render_image,
};
use crate::motion::{FadeDirection, MotionDuration, fade, pop_in};
use gpui::{
    AnyElement, App, BoxShadow, Component, Global, IntoElement, KeyBinding, ObjectFit, Pixels,
    RenderImage, RenderOnce, SharedString, Size, Window, actions, div, prelude::*, px, size,
};
use liora_core::{Config, push_portal};
use std::{path::PathBuf, sync::Arc, time::Duration};

actions!(
    preview,
    [
        #[doc = "Keyboard action that closes the active image preview overlay."]
        PreviewClose
    ]
);

/// Fluent native GPUI component for rendering Liora preview.
pub struct Preview {
    src: Option<ImageSource>,
    trigger: Option<AnyElement>,
    hover_effect: bool,
    close_on_click_outside: bool,
    close_on_escape: bool,
}

/// Fluent native GPUI component for rendering Liora active image preview.
pub struct ActiveImagePreview {
    image: Option<Arc<RenderImage>>,
    closing: bool,
    close_on_click_outside: bool,
    close_on_escape: bool,
}

impl Global for ActiveImagePreview {}

impl Preview {
    /// Creates `Preview` initialized from the supplied src.
    pub fn new(src: impl Into<SharedString>) -> Self {
        Self {
            src: Some(ImageSource::from_input(src)),
            trigger: None,
            hover_effect: true,
            close_on_click_outside: true,
            close_on_escape: true,
        }
    }

    /// Creates or applies an empty-state visual treatment.
    pub fn empty() -> Self {
        Self {
            src: None,
            trigger: None,
            hover_effect: true,
            close_on_click_outside: true,
            close_on_escape: true,
        }
    }

    /// Sets the image or preview source.
    pub fn src(mut self, src: impl Into<SharedString>) -> Self {
        self.src = Some(ImageSource::from_input(src));
        self
    }

    pub(crate) fn src_from_image_source(mut self, src: Option<ImageSource>) -> Self {
        self.src = src;
        self
    }

    /// Returns the bundled SVG file name for this Lucide icon.
    pub fn file(mut self, path: impl Into<PathBuf>) -> Self {
        self.src = Some(ImageSource::File(path.into()));
        self
    }

    /// Creates a component source from a local file path.
    pub fn local(path: impl Into<PathBuf>) -> Self {
        Self::empty().file(path)
    }

    /// Adds a child element to the component body.
    pub fn child(mut self, trigger: impl IntoElement) -> Self {
        self.trigger = Some(trigger.into_any_element());
        self
    }

    /// Sets the hover effect value used by the component.
    pub fn hover_effect(mut self, enabled: bool) -> Self {
        self.hover_effect = enabled;
        self
    }

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

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

    /// Returns the configured image source, if one is available.
    pub fn source(&self) -> Option<&ImageSource> {
        self.src.as_ref()
    }

    /// Returns whether this value currently has trigger.
    pub fn has_trigger(&self) -> bool {
        self.trigger.is_some()
    }

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

/// Renders the render image preview layer into native GPUI elements.
pub fn render_image_preview(window: &mut Window, cx: &mut App) {
    let Some((image, closing, close_on_click_outside, close_on_escape)) =
        cx.try_global::<ActiveImagePreview>().and_then(|preview| {
            preview.image.clone().map(|image| {
                (
                    image,
                    preview.closing,
                    preview.close_on_click_outside,
                    preview.close_on_escape,
                )
            })
        })
    else {
        return;
    };

    let theme = cx.global::<Config>().theme.clone();
    push_portal(
        move |window, _cx| {
            let viewport = window.viewport_size();
            let preview_size =
                preview_image_box_size(&image, viewport.width * 0.72, viewport.height * 0.72);
            let overlay_motion = if closing {
                FadeDirection::Out
            } else {
                FadeDirection::In
            };
            fade(
                if closing {
                    "liora-preview-overlay-exit"
                } else {
                    "liora-preview-overlay-enter"
                },
                overlay_motion,
                div()
                    .absolute()
                    .top_0()
                    .left_0()
                    .size_full()
                    .flex()
                    .items_center()
                    .justify_center()
                    .bg(gpui::black().opacity(0.55))
                    .when(close_on_escape, |s| {
                        s.on_action(|_: &PreviewClose, _, cx| {
                            close_active_preview(cx);
                        })
                    })
                    .when(close_on_click_outside, |s| {
                        s.on_mouse_down(gpui::MouseButton::Left, |_, _, cx| {
                            close_active_preview(cx);
                            cx.stop_propagation();
                        })
                    })
                    .child(pop_in(
                        if closing {
                            "liora-preview-frame-exit"
                        } else {
                            "liora-preview-frame-enter"
                        },
                        div()
                            .w(preview_size.width)
                            .h(preview_size.height)
                            .rounded(px(theme.radius.lg))
                            .border_1()
                            .border_color(gpui::white().opacity(0.28))
                            .overflow_hidden()
                            .shadow(preview_image_frame_shadow())
                            .on_mouse_down(gpui::MouseButton::Left, |_, _, cx| {
                                cx.stop_propagation();
                            })
                            .child(RasterImageElement {
                                image,
                                fit: ObjectFit::Contain,
                                grayscale: false,
                                radius: px(theme.radius.lg),
                                round: false,
                                round_options: ImageRoundOptions::without_square_crop(),
                            }),
                    )),
            )
            .into_any_element()
        },
        cx,
    );

    let _ = window;
}

fn close_active_preview_if_escape_enabled(cx: &mut App) {
    let close_on_escape = cx
        .try_global::<ActiveImagePreview>()
        .is_some_and(|preview| preview.close_on_escape);
    if close_on_escape {
        close_active_preview(cx);
    }
}

fn close_active_preview(cx: &mut App) {
    if cx.has_global::<ActiveImagePreview>() {
        let preview = cx.global_mut::<ActiveImagePreview>();
        if preview.image.is_none() || preview.closing {
            return;
        }
        preview.closing = true;
        cx.refresh_windows();

        let async_cx = cx.to_async();
        let executor = cx.background_executor().clone();
        cx.foreground_executor()
            .spawn(async move {
                executor.timer(preview_close_duration()).await;
                let _ = async_cx.update(|cx| {
                    if cx.has_global::<ActiveImagePreview>() {
                        let preview = cx.global_mut::<ActiveImagePreview>();
                        if preview.closing {
                            preview.image = None;
                            preview.closing = false;
                            cx.refresh_windows();
                        }
                    }
                });
            })
            .detach();
    }
}

fn preview_close_duration() -> Duration {
    MotionDuration::Fast.as_duration()
}

fn preview_image_box_size(
    image: &RenderImage,
    max_width: Pixels,
    max_height: Pixels,
) -> Size<Pixels> {
    let image_size = image.size(0);
    let image_width = i32::from(image_size.width).max(1) as f32;
    let image_height = i32::from(image_size.height).max(1) as f32;
    let scale = (max_width.as_f32() / image_width).min(max_height.as_f32() / image_height);

    size(px(image_width * scale), px(image_height * scale))
}

fn preview_image_frame_shadow() -> Vec<BoxShadow> {
    vec![
        BoxShadow {
            color: gpui::black().opacity(0.48),
            offset: gpui::point(px(0.0), px(28.0)),
            blur_radius: px(64.0),
            spread_radius: px(4.0),
            inset: false,
        },
        BoxShadow {
            color: gpui::black().opacity(0.34),
            offset: gpui::point(px(0.0), px(10.0)),
            blur_radius: px(24.0),
            spread_radius: px(-2.0),
            inset: false,
        },
        BoxShadow {
            color: gpui::white().opacity(0.22),
            offset: gpui::point(px(0.0), px(-2.0)),
            blur_radius: px(8.0),
            spread_radius: px(-4.0),
            inset: false,
        },
    ]
}

fn load_preview_image(
    src: &Option<ImageSource>,
    window: &mut Window,
    cx: &mut App,
) -> Option<Arc<RenderImage>> {
    match src {
        Some(ImageSource::File(path)) => load_local_render_image(path),
        Some(ImageSource::Url(url)) => load_remote_render_image(url.as_ref(), window, cx),
        None => None,
    }
}

impl RenderOnce for Preview {
    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
        let theme = cx.global::<Config>().theme.clone();
        let preview_image = load_preview_image(&self.src, window, cx);
        let mut trigger = div()
            .relative()
            .cursor_pointer()
            .child(self.trigger.unwrap_or_else(|| div().into_any_element()))
            .on_mouse_down(gpui::MouseButton::Left, move |_, _, cx| {
                if let Some(image) = preview_image.clone() {
                    if !cx.has_global::<ActiveImagePreview>() {
                        cx.set_global(ActiveImagePreview {
                            image: None,
                            closing: false,
                            close_on_click_outside: self.close_on_click_outside,
                            close_on_escape: self.close_on_escape,
                        });
                    }
                    let preview = cx.global_mut::<ActiveImagePreview>();
                    preview.image = Some(image);
                    preview.closing = false;
                    preview.close_on_click_outside = self.close_on_click_outside;
                    preview.close_on_escape = self.close_on_escape;
                    cx.refresh_windows();
                }
                cx.stop_propagation();
            });

        if self.hover_effect {
            trigger = trigger.hover(|s| s.border_color(theme.primary.base).shadow_lg());
        }

        trigger
    }
}

impl IntoElement for Preview {
    type Element = Component<Self>;

    fn into_element(self) -> Self::Element {
        Component::new(self)
    }
}

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

    fn test_render_image(width: u32, height: u32) -> RenderImage {
        RenderImage::new([::image::Frame::new(::image::RgbaImage::new(width, height))])
    }

    #[test]
    fn preview_image_box_size_matches_contained_image_bounds() {
        let wide = test_render_image(400, 200);
        let wide_size = preview_image_box_size(&wide, px(300.0), px(300.0));
        assert_eq!(wide_size.width, px(300.0));
        assert_eq!(wide_size.height, px(150.0));

        let tall = test_render_image(200, 400);
        let tall_size = preview_image_box_size(&tall, px(300.0), px(300.0));
        assert_eq!(tall_size.width, px(150.0));
        assert_eq!(tall_size.height, px(300.0));
    }

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

        assert!(production.contains("PreviewClose"));
        assert!(
            production.contains("Keyboard action that closes the active image preview overlay")
        );
        assert!(production.contains("KeyBinding::new(\"escape\", PreviewClose, None)"));
        assert!(production.contains("cx.on_action(|_: &PreviewClose"));
        assert!(production.contains(".on_action(|_: &PreviewClose"));
        assert!(production.contains("fn close_active_preview"));
        assert!(production.contains("close_on_click_outside"));
        assert!(production.contains("pub fn close_on_click_outside("));
        assert!(production.contains("preview_close_duration"));
        assert!(production.contains("closing: bool"));
        assert!(production.contains("fn preview_image_box_size"));
        assert!(production.contains(".w(preview_size.width)"));
        assert!(production.contains(".h(preview_size.height)"));
        assert!(production.contains(".shadow(preview_image_frame_shadow())"));
        assert!(production.contains("FadeDirection::Out"));
        assert!(production.contains("pop_in("));
        assert!(
            !production.contains(
                ".w(viewport.width * 0.72)\n                        .h(viewport.height * 0.72)"
            ),
            "preview should not consume clicks in the whole max viewport box; only the fitted image box should stop backdrop close"
        );
    }

    #[test]
    fn preview_frame_shadow_keeps_3d_border_depth() {
        let shadow = preview_image_frame_shadow();

        assert_eq!(shadow.len(), 3);
        assert_eq!(shadow[0].offset.y, px(28.0));
        assert_eq!(shadow[0].blur_radius, px(64.0));
        assert_eq!(shadow[1].offset.y, px(10.0));
        assert_eq!(shadow[2].offset.y, px(-2.0));
    }
}