image_viewer.rs

  1use gpui::{
  2    canvas, div, fill, img, opaque_grey, point, size, AnyElement, AppContext, Bounds, Context,
  3    EventEmitter, FocusHandle, FocusableView, Img, InteractiveElement, IntoElement, Model,
  4    ObjectFit, ParentElement, Render, Styled, Task, View, ViewContext, VisualContext, WeakView,
  5    WindowContext,
  6};
  7use persistence::IMAGE_VIEWER;
  8use ui::prelude::*;
  9
 10use project::{Project, ProjectEntryId, ProjectPath};
 11use std::{ffi::OsStr, path::PathBuf};
 12use workspace::{
 13    item::{Item, ProjectItem, SerializableItem, TabContentParams},
 14    ItemId, Pane, Workspace, WorkspaceId,
 15};
 16
 17const IMAGE_VIEWER_KIND: &str = "ImageView";
 18
 19pub struct ImageItem {
 20    path: PathBuf,
 21    project_path: ProjectPath,
 22}
 23
 24impl project::Item for ImageItem {
 25    fn try_open(
 26        project: &Model<Project>,
 27        path: &ProjectPath,
 28        cx: &mut AppContext,
 29    ) -> Option<Task<gpui::Result<Model<Self>>>> {
 30        let path = path.clone();
 31        let project = project.clone();
 32
 33        let ext = path
 34            .path
 35            .extension()
 36            .and_then(OsStr::to_str)
 37            .unwrap_or_default();
 38
 39        // Only open the item if it's a binary image (no SVGs, etc.)
 40        // Since we do not have a way to toggle to an editor
 41        if Img::extensions().contains(&ext) && !ext.contains("svg") {
 42            Some(cx.spawn(|mut cx| async move {
 43                let abs_path = project
 44                    .read_with(&cx, |project, cx| project.absolute_path(&path, cx))?
 45                    .ok_or_else(|| anyhow::anyhow!("Failed to find the absolute path"))?;
 46
 47                cx.new_model(|_| ImageItem {
 48                    path: abs_path,
 49                    project_path: path,
 50                })
 51            }))
 52        } else {
 53            None
 54        }
 55    }
 56
 57    fn entry_id(&self, _: &AppContext) -> Option<ProjectEntryId> {
 58        None
 59    }
 60
 61    fn project_path(&self, _: &AppContext) -> Option<ProjectPath> {
 62        Some(self.project_path.clone())
 63    }
 64}
 65
 66pub struct ImageView {
 67    path: PathBuf,
 68    focus_handle: FocusHandle,
 69}
 70
 71impl Item for ImageView {
 72    type Event = ();
 73
 74    fn tab_content(&self, params: TabContentParams, _cx: &WindowContext) -> AnyElement {
 75        let title = self
 76            .path
 77            .file_name()
 78            .unwrap_or_else(|| self.path.as_os_str())
 79            .to_string_lossy()
 80            .to_string();
 81        Label::new(title)
 82            .single_line()
 83            .color(params.text_color())
 84            .italic(params.preview)
 85            .into_any_element()
 86    }
 87
 88    fn clone_on_split(
 89        &self,
 90        _workspace_id: Option<WorkspaceId>,
 91        cx: &mut ViewContext<Self>,
 92    ) -> Option<View<Self>>
 93    where
 94        Self: Sized,
 95    {
 96        Some(cx.new_view(|cx| Self {
 97            path: self.path.clone(),
 98            focus_handle: cx.focus_handle(),
 99        }))
100    }
101}
102
103impl SerializableItem for ImageView {
104    fn serialized_item_kind() -> &'static str {
105        IMAGE_VIEWER_KIND
106    }
107
108    fn deserialize(
109        _project: Model<Project>,
110        _workspace: WeakView<Workspace>,
111        workspace_id: WorkspaceId,
112        item_id: ItemId,
113        cx: &mut ViewContext<Pane>,
114    ) -> Task<gpui::Result<View<Self>>> {
115        cx.spawn(|_pane, mut cx| async move {
116            let image_path = IMAGE_VIEWER
117                .get_image_path(item_id, workspace_id)?
118                .ok_or_else(|| anyhow::anyhow!("No image path found"))?;
119
120            cx.new_view(|cx| ImageView {
121                path: image_path,
122                focus_handle: cx.focus_handle(),
123            })
124        })
125    }
126
127    fn cleanup(
128        workspace_id: WorkspaceId,
129        alive_items: Vec<ItemId>,
130        cx: &mut WindowContext,
131    ) -> Task<gpui::Result<()>> {
132        cx.spawn(|_| IMAGE_VIEWER.delete_unloaded_items(workspace_id, alive_items))
133    }
134
135    fn serialize(
136        &mut self,
137        workspace: &mut Workspace,
138        item_id: ItemId,
139        _closing: bool,
140        cx: &mut ViewContext<Self>,
141    ) -> Option<Task<gpui::Result<()>>> {
142        let workspace_id = workspace.database_id()?;
143
144        Some(cx.background_executor().spawn({
145            let image_path = self.path.clone();
146            async move {
147                IMAGE_VIEWER
148                    .save_image_path(item_id, workspace_id, image_path)
149                    .await
150            }
151        }))
152    }
153
154    fn should_serialize(&self, _event: &Self::Event) -> bool {
155        false
156    }
157}
158
159impl EventEmitter<()> for ImageView {}
160impl FocusableView for ImageView {
161    fn focus_handle(&self, _cx: &AppContext) -> FocusHandle {
162        self.focus_handle.clone()
163    }
164}
165
166impl Render for ImageView {
167    fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
168        let checkered_background = |bounds: Bounds<Pixels>, _, cx: &mut WindowContext| {
169            let square_size = 32.0;
170
171            let start_y = bounds.origin.y.0;
172            let height = bounds.size.height.0;
173            let start_x = bounds.origin.x.0;
174            let width = bounds.size.width.0;
175
176            let mut y = start_y;
177            let mut x = start_x;
178            let mut color_swapper = true;
179            // draw checkerboard pattern
180            while y <= start_y + height {
181                // Keeping track of the grid in order to be resilient to resizing
182                let start_swap = color_swapper;
183                while x <= start_x + width {
184                    let rect =
185                        Bounds::new(point(px(x), px(y)), size(px(square_size), px(square_size)));
186
187                    let color = if color_swapper {
188                        opaque_grey(0.6, 0.4)
189                    } else {
190                        opaque_grey(0.7, 0.4)
191                    };
192
193                    cx.paint_quad(fill(rect, color));
194                    color_swapper = !color_swapper;
195                    x += square_size;
196                }
197                x = start_x;
198                color_swapper = !start_swap;
199                y += square_size;
200            }
201        };
202
203        let checkered_background = canvas(|_, _| (), checkered_background)
204            .border_2()
205            .border_color(cx.theme().styles.colors.border)
206            .size_full()
207            .absolute()
208            .top_0()
209            .left_0();
210
211        div()
212            .track_focus(&self.focus_handle)
213            .size_full()
214            .child(checkered_background)
215            .child(
216                div()
217                    .flex()
218                    .justify_center()
219                    .items_center()
220                    .w_full()
221                    // TODO: In browser based Tailwind & Flex this would be h-screen and we'd use w-full
222                    .h_full()
223                    .child(
224                        img(self.path.clone())
225                            .object_fit(ObjectFit::ScaleDown)
226                            .max_w_full()
227                            .max_h_full(),
228                    ),
229            )
230    }
231}
232
233impl ProjectItem for ImageView {
234    type Item = ImageItem;
235
236    fn for_project_item(
237        _project: Model<Project>,
238        item: Model<Self::Item>,
239        cx: &mut ViewContext<Self>,
240    ) -> Self
241    where
242        Self: Sized,
243    {
244        Self {
245            path: item.read(cx).path.clone(),
246            focus_handle: cx.focus_handle(),
247        }
248    }
249}
250
251pub fn init(cx: &mut AppContext) {
252    workspace::register_project_item::<ImageView>(cx);
253    workspace::register_serializable_item::<ImageView>(cx)
254}
255
256mod persistence {
257    use anyhow::Result;
258    use std::path::PathBuf;
259
260    use db::{define_connection, query, sqlez::statement::Statement, sqlez_macros::sql};
261    use workspace::{ItemId, WorkspaceDb, WorkspaceId};
262
263    define_connection! {
264        pub static ref IMAGE_VIEWER: ImageViewerDb<WorkspaceDb> =
265            &[sql!(
266                CREATE TABLE image_viewers (
267                    workspace_id INTEGER,
268                    item_id INTEGER UNIQUE,
269
270                    image_path BLOB,
271
272                    PRIMARY KEY(workspace_id, item_id),
273                    FOREIGN KEY(workspace_id) REFERENCES workspaces(workspace_id)
274                    ON DELETE CASCADE
275                ) STRICT;
276            )];
277    }
278
279    impl ImageViewerDb {
280        query! {
281           pub async fn update_workspace_id(
282                new_id: WorkspaceId,
283                old_id: WorkspaceId,
284                item_id: ItemId
285            ) -> Result<()> {
286                UPDATE image_viewers
287                SET workspace_id = ?
288                WHERE workspace_id = ? AND item_id = ?
289            }
290        }
291
292        query! {
293            pub async fn save_image_path(
294                item_id: ItemId,
295                workspace_id: WorkspaceId,
296                image_path: PathBuf
297            ) -> Result<()> {
298                INSERT OR REPLACE INTO image_viewers(item_id, workspace_id, image_path)
299                VALUES (?, ?, ?)
300            }
301        }
302
303        query! {
304            pub fn get_image_path(item_id: ItemId, workspace_id: WorkspaceId) -> Result<Option<PathBuf>> {
305                SELECT image_path
306                FROM image_viewers
307                WHERE item_id = ? AND workspace_id = ?
308            }
309        }
310
311        pub async fn delete_unloaded_items(
312            &self,
313            workspace: WorkspaceId,
314            alive_items: Vec<ItemId>,
315        ) -> Result<()> {
316            let placeholders = alive_items
317                .iter()
318                .map(|_| "?")
319                .collect::<Vec<&str>>()
320                .join(", ");
321
322            let query = format!("DELETE FROM image_viewers WHERE workspace_id = ? AND item_id NOT IN ({placeholders})");
323
324            self.write(move |conn| {
325                let mut statement = Statement::prepare(conn, query)?;
326                let mut next_index = statement.bind(&workspace, 1)?;
327                for id in alive_items {
328                    next_index = statement.bind(&id, next_index)?;
329                }
330                statement.exec()
331            })
332            .await
333        }
334    }
335}