]> uap-core.de Git - note.git/commitdiff
use content_type to select the file note preview type main
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Tue, 4 Aug 2026 18:51:50 +0000 (20:51 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Tue, 4 Aug 2026 18:51:50 +0000 (20:51 +0200)
application/note/src/file.rs
application/note/src/note.rs
application/note/src/window.rs

index e64a3f003cf83eb049b7c1bbb37c34ad80a3a7e2..5dbcd4de25e1ca41b793b337aa9910dc65ecd9cd 100644 (file)
@@ -25,7 +25,7 @@
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  * POSSIBILITY OF SUCH DAMAGE.
  */
-use std::ffi::{OsStr};
+
 use std::mem;
 use std::path::{Path, PathBuf};
 use std::rc::Rc;
@@ -35,7 +35,7 @@ use ui_rs::{action, doc_cast, ui_actions, UiModel};
 use ui_rs::ui::*;
 use crate::AppStates;
 use crate::note::NoteViewModel;
-use crate::window::NoteTypeTabView;
+use crate::window::{FileNotePreviewTabView, NoteTypeTabView};
 
 #[derive(UiModel)]
 pub struct FileNote {
@@ -46,6 +46,7 @@ pub struct FileNote {
 
     pub id: NoteId,
     pub nodename: String,
+    pub content_type: String,
 
     pub local_path: Option<PathBuf>,
     pub is_tmp_file: bool,
@@ -78,6 +79,7 @@ impl FileNote {
 
             id,
             nodename: String::default(),
+            content_type: String::default(),
             local_path: None,
             is_tmp_file: false,
             is_opening: false,
@@ -94,6 +96,7 @@ impl FileNote {
         let doc: UiDoc<FileNote> = UiDoc::new_from_box2(Box::new(self), |n,d| {
             n.doc = d.doc_ref();
             n.nodename = model.nodename.clone();
+            n.content_type = model.content_type.clone();
 
             n.note_type.set(NoteTypeTabView::File as i64);
             n.note_nodename.set(model.nodename.as_str());
@@ -103,6 +106,31 @@ impl FileNote {
         doc_cast!(doc, dyn NoteViewModel)
     }
 
+    fn set_preview(&mut self, path: &Path) {
+        const WEBVIEW_TYPES: &[&str] = &[
+            "text/html",
+            "application/pdf",
+        ];
+
+        if self.content_type.starts_with("image/") {
+            let result = self.image.load_image_file(path);
+            if result.is_ok() {
+                self.preview_tab.set(FileNotePreviewTabView::ImageViewer as i64);
+                return
+            }
+            self.preview_tab.set(crate::window::FileNotePreviewTabView::ImageViewer as i64);
+        } else if WEBVIEW_TYPES.contains(&self.content_type.as_str()) {
+            let url = Url::from_file_path(&path);
+            if let Ok(url) = url {
+                self.webview.load_url(url.as_str());
+                self.preview_tab.set(crate::window::FileNotePreviewTabView::WebView as i64);
+                return
+            }
+        }
+
+        self.preview_tab.set(crate::window::FileNotePreviewTabView::Empty as i64);
+    }
+
     fn query_local_path(&mut self) {
         let Some(doc) = self.doc.get_doc() else {
             return;
@@ -119,27 +147,7 @@ impl FileNote {
                     note.is_opening = false;
                     match result {
                         OpenExternResult::Ok((path, istmp)) => {
-                            // TODO: implement better file type detection
-                            //       idealy save content type in the note
-                            match path.extension().and_then(OsStr::to_str) {
-                                Some("png" | "jpeg" | "jpg" | "webp") => {
-                                    let result = note.image.load_image_file(path.as_path());
-                                    if result.is_ok() {
-                                        // TODO: replace 1 with enum value
-                                        note.preview_tab.set(1);
-                                    }
-                                },
-                                Some("html" | "htm" | "pdf") => {
-                                    let url = Url::from_file_path(&path);
-                                    if let Ok(url) = url {
-                                        note.webview.load_url(url.as_str());
-                                        // TODO: replace 2 with enum value
-                                        note.preview_tab.set(2);
-                                    }
-                                }
-                                _ => {}
-                            }
-
+                            note.set_preview(&path);
                             note.local_path = Some(path);
                             note.is_tmp_file = istmp;
                         }
index c0ed8b2b4a628dfd96d4f1afe9445cb6b142aefe..c8af18c79c325d009c066f60a7976078f37d3048 100644 (file)
@@ -29,7 +29,7 @@ use std::path::PathBuf;
 use std::rc::Rc;
 use std::sync::atomic::{AtomicU64, Ordering};
 use std::time::SystemTime;
-use sea_orm::{Iden, NotSet, Set};
+use sea_orm::{NotSet, Set};
 use sea_orm::sea_query::prelude::Utc;
 use backend::backend::{BackendHandle, BroadcastMessage, NoteContentRet, NoteId, NoteTitleUpdate, SaveNoteResult};
 use backend::lockmanager::NoteLock;
index 2ca11a53e0ea0dcf2c51a847bd31663741a53d67..44c824ca66dde6ddd2b5c24ac77ad077ae420ab1 100644 (file)
@@ -329,6 +329,13 @@ pub enum NoteTypeTabView {
     File = 2,
 }
 
+#[repr(i64)]
+pub enum FileNotePreviewTabView {
+    Empty = 0,
+    ImageViewer = 1,
+    WebView = 2
+}
+
 #[derive(Default)]
 pub struct Navigation {
     history: Vec<NavigationItem>,