chore: inital commit
Split project into a Cargo workspace with two crates: - core/ (noctua_core): UI-independent document library - ui/ (noctua_ui): COSMIC application scaffold (empty) justfile for workspace (run, run-cli, check, test, build)
This commit is contained in:
commit
c85f35310e
31 changed files with 9768 additions and 0 deletions
56
core/src/document/core.rs
Normal file
56
core/src/document/core.rs
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
// src/document/core.rs
|
||||
//
|
||||
// Core domain models for document representation.
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct RawImage {
|
||||
pub data: Arc<Vec<u8>>,
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct PageLayout {
|
||||
pub page_starts: Vec<f64>,
|
||||
pub page_sizes: Vec<(f64, f64)>,
|
||||
pub gap: f64,
|
||||
pub total_height: f64,
|
||||
pub max_width: f64,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ViewBox {
|
||||
pub x: f64,
|
||||
pub y: f64,
|
||||
pub width: f64,
|
||||
pub height: f64,
|
||||
}
|
||||
|
||||
impl PageLayout {
|
||||
pub fn new(page_sizes: Vec<(f64, f64)>, gap: f64) -> Self {
|
||||
let mut page_starts = Vec::with_capacity(page_sizes.len());
|
||||
let mut y = 0.0_f64;
|
||||
let mut max_width = 0.0_f64;
|
||||
|
||||
for &(w, h) in &page_sizes {
|
||||
page_starts.push(y);
|
||||
y += h + gap;
|
||||
if w > max_width {
|
||||
max_width = w;
|
||||
}
|
||||
}
|
||||
|
||||
let total_height = if !page_sizes.is_empty() { y - gap } else { 0.0 };
|
||||
|
||||
Self {
|
||||
page_starts,
|
||||
page_sizes,
|
||||
gap,
|
||||
total_height,
|
||||
max_width,
|
||||
}
|
||||
}
|
||||
}
|
||||
38
core/src/document/format.rs
Normal file
38
core/src/document/format.rs
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
// src/document/format.rs
|
||||
//
|
||||
// Single source of truth for document format detection.
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
/// The three document formats Noctua understands.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum DocumentFormat {
|
||||
/// Raster images – anything libcosmic's image crate can decode.
|
||||
Raster,
|
||||
/// Vector graphics (SVG).
|
||||
Vector,
|
||||
/// Portable documents (PDF).
|
||||
Portable,
|
||||
}
|
||||
|
||||
/// Detects the document format from the file extension.
|
||||
///
|
||||
/// Returns `None` for unknown extensions so directory scans can skip
|
||||
/// unsupported files early. At load time the underlying library
|
||||
/// (ImageReader, usvg, pdfium) is the final authority.
|
||||
pub fn detect_format(path: &Path) -> Option<DocumentFormat> {
|
||||
match path
|
||||
.extension()
|
||||
.and_then(|e| e.to_str())
|
||||
.map(|e| e.to_lowercase())
|
||||
.as_deref()
|
||||
{
|
||||
Some("pdf") => Some(DocumentFormat::Portable),
|
||||
Some("svg") => Some(DocumentFormat::Vector),
|
||||
Some("png" | "jpg" | "jpeg" | "webp" | "gif" | "bmp" | "tiff" | "tif") => {
|
||||
Some(DocumentFormat::Raster)
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
72
core/src/document/loader.rs
Normal file
72
core/src/document/loader.rs
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
// src/document/loader.rs
|
||||
//
|
||||
// Document loading: format dispatch and directory scanning.
|
||||
|
||||
use crate::document::format::{detect_format, DocumentFormat};
|
||||
use crate::document::portable::PortableModel;
|
||||
use crate::document::raster::RasterModel;
|
||||
use crate::document::session::{SessionData, SessionItem};
|
||||
use crate::document::vector::VectorModel;
|
||||
use crate::document::{core::PageLayout, DocumentContent};
|
||||
use crate::error::{Error, Result};
|
||||
use std::path::Path;
|
||||
|
||||
/// Loads a document from a path and returns its content.
|
||||
pub fn load_document(path: &Path) -> Result<DocumentContent> {
|
||||
if !path.exists() {
|
||||
return Err(Error::NotFound(format!("{:?}", path)));
|
||||
}
|
||||
|
||||
match detect_format(path) {
|
||||
Some(DocumentFormat::Portable) => {
|
||||
// TODO: Replace with actual pdfium-render loading.
|
||||
Ok(DocumentContent::Portable(PortableModel {
|
||||
path: path.to_path_buf(),
|
||||
page_count: 3,
|
||||
layout: PageLayout::new(vec![(595.0, 842.0); 3], 10.0),
|
||||
}))
|
||||
}
|
||||
Some(DocumentFormat::Vector) => {
|
||||
// TODO: Replace with actual usvg parsing.
|
||||
Ok(DocumentContent::Vector(VectorModel {
|
||||
path: path.to_path_buf(),
|
||||
view_box: None,
|
||||
}))
|
||||
}
|
||||
Some(DocumentFormat::Raster) => {
|
||||
// TODO: Replace with actual image-crate loading.
|
||||
Ok(DocumentContent::Raster(RasterModel {
|
||||
path: path.to_path_buf(),
|
||||
dimensions: (1920, 1080),
|
||||
}))
|
||||
}
|
||||
None => Err(Error::Unsupported(format!(
|
||||
"extension: {}",
|
||||
path.extension()
|
||||
.and_then(|e| e.to_str())
|
||||
.unwrap_or("(none)")
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
/// Scans a directory for supported files and populates the session.
|
||||
pub fn scan_directory_into(session: &mut SessionData, dir: &Path) {
|
||||
if let Ok(entries) = std::fs::read_dir(dir) {
|
||||
let mut paths: Vec<_> = entries
|
||||
.filter_map(|e| e.ok())
|
||||
.map(|e| e.path())
|
||||
.filter(|p| p.is_file() && detect_format(p).is_some())
|
||||
.collect();
|
||||
|
||||
paths.sort();
|
||||
|
||||
session.items = paths.into_iter().map(|p| SessionItem::new(p, 0)).collect();
|
||||
}
|
||||
|
||||
session.current_index = if session.items.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(0)
|
||||
};
|
||||
}
|
||||
223
core/src/document/manager.rs
Normal file
223
core/src/document/manager.rs
Normal file
|
|
@ -0,0 +1,223 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
// src/document/manager.rs
|
||||
//
|
||||
// Document Manager – single entry point via DocumentCommand.
|
||||
|
||||
use crate::document::loader::{load_document, scan_directory_into};
|
||||
use crate::document::session::{SessionData, SessionKind};
|
||||
use crate::document::store;
|
||||
use crate::document::DocumentContent;
|
||||
use crate::error::Error;
|
||||
use log::warn;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug)]
|
||||
pub enum DocumentState {
|
||||
Empty,
|
||||
Loading,
|
||||
Loaded(DocumentContent),
|
||||
Error(Error),
|
||||
}
|
||||
|
||||
/// All actions the UI can request from the manager.
|
||||
/// The manager executes; the UI decides.
|
||||
#[derive(Debug)]
|
||||
pub enum DocumentCommand {
|
||||
/// Create a new session. Does nothing if a session with that name already exists.
|
||||
CreateSession {
|
||||
name: String,
|
||||
kind: SessionKind,
|
||||
path: Option<PathBuf>,
|
||||
},
|
||||
/// Scan a directory into an existing session, replacing its current items.
|
||||
ScanDirectory { session: String, dir: PathBuf },
|
||||
/// Load a file and make it the active item in the given session.
|
||||
AddFileToSession { session: String, path: PathBuf },
|
||||
/// Set the active session by name.
|
||||
SetActive(String),
|
||||
/// Open a session file (.ron): loads all sessions and activates the DirectoryBrowser.
|
||||
OpenSession(PathBuf),
|
||||
/// Navigate to the next item in the active session.
|
||||
NavigateNext,
|
||||
/// Navigate to the previous item in the active session.
|
||||
NavigatePrevious,
|
||||
/// Select a specific item by index in the active session.
|
||||
SelectItem(usize),
|
||||
}
|
||||
|
||||
pub struct DocumentManager {
|
||||
sessions: Vec<SessionData>,
|
||||
pub active_session_name: Option<String>,
|
||||
pub state: DocumentState,
|
||||
}
|
||||
|
||||
impl Default for DocumentManager {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
sessions: Vec::new(),
|
||||
active_session_name: None,
|
||||
state: DocumentState::Empty,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DocumentManager {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
/// The single public entry point for all state changes.
|
||||
pub fn handle(&mut self, command: DocumentCommand) {
|
||||
match command {
|
||||
DocumentCommand::CreateSession { name, kind, path } => {
|
||||
if self.session(&name).is_none() {
|
||||
self.sessions
|
||||
.push(SessionData::new(&name, kind, path, Vec::new()));
|
||||
}
|
||||
}
|
||||
|
||||
DocumentCommand::ScanDirectory { session, dir } => {
|
||||
if let Some(s) = self.session_mut(&session) {
|
||||
scan_directory_into(s, &dir);
|
||||
}
|
||||
}
|
||||
|
||||
DocumentCommand::AddFileToSession { session, path } => {
|
||||
self.active_session_name = Some(session);
|
||||
self.load_file(&path);
|
||||
}
|
||||
|
||||
DocumentCommand::SetActive(name) => {
|
||||
if self.session(&name).is_some() {
|
||||
self.active_session_name = Some(name);
|
||||
}
|
||||
}
|
||||
|
||||
DocumentCommand::OpenSession(path) => {
|
||||
self.load_all_sessions(&path);
|
||||
}
|
||||
|
||||
DocumentCommand::NavigateNext => self.next(),
|
||||
DocumentCommand::NavigatePrevious => self.previous(),
|
||||
DocumentCommand::SelectItem(index) => self.select_item(index),
|
||||
}
|
||||
}
|
||||
|
||||
/// Read-only access to a session by name.
|
||||
pub fn session(&self, name: &str) -> Option<&SessionData> {
|
||||
self.sessions.iter().find(|s| s.name == name)
|
||||
}
|
||||
|
||||
// Mutable access to a session by name.
|
||||
fn session_mut(&mut self, name: &str) -> Option<&mut SessionData> {
|
||||
self.sessions.iter_mut().find(|s| s.name == name)
|
||||
}
|
||||
|
||||
// Mutable access to the currently active session.
|
||||
// The name is cloned to satisfy the borrow checker – it is a cheap String copy.
|
||||
fn active_session_mut(&mut self) -> Option<&mut SessionData> {
|
||||
let name = self.active_session_name.clone()?;
|
||||
self.sessions.iter_mut().find(|s| s.name == name)
|
||||
}
|
||||
|
||||
// Loads all sessions from a RON file and activates the DirectoryBrowser.
|
||||
fn load_all_sessions(&mut self, path: &Path) {
|
||||
match store::load_sessions(path) {
|
||||
Ok(mut loaded) => {
|
||||
// Re-scan every DirectoryBrowser's source directory on load.
|
||||
// Validate every DocumentCollection: remove items whose files no longer exist.
|
||||
for session in loaded.iter_mut() {
|
||||
match session.kind {
|
||||
SessionKind::DirectoryBrowser => {
|
||||
if let Some(dir) = session.path.clone() {
|
||||
scan_directory_into(session, &dir);
|
||||
}
|
||||
}
|
||||
SessionKind::DocumentCollection => {
|
||||
session.items.retain(|item| {
|
||||
if item.path.exists() {
|
||||
true
|
||||
} else {
|
||||
warn!(
|
||||
"Session '{}': {}",
|
||||
session.name,
|
||||
Error::NotFound(item.path.display().to_string())
|
||||
);
|
||||
false
|
||||
}
|
||||
});
|
||||
|
||||
// Reset index if it pointed to a now-removed item.
|
||||
session.current_index = match session.current_index {
|
||||
Some(i) if i < session.items.len() => Some(i),
|
||||
_ if !session.items.is_empty() => Some(0),
|
||||
_ => None,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Activate the DirectoryBrowser as the default entry point.
|
||||
let browser_name = loaded
|
||||
.iter()
|
||||
.find(|s| s.kind == SessionKind::DirectoryBrowser)
|
||||
.map(|s| s.name.clone());
|
||||
|
||||
self.sessions = loaded;
|
||||
self.active_session_name = browser_name;
|
||||
self.state = DocumentState::Empty;
|
||||
}
|
||||
Err(e) => {
|
||||
self.state = DocumentState::Error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn select_item(&mut self, index: usize) {
|
||||
if let Some(session) = self.active_session_mut() {
|
||||
if index < session.items.len() {
|
||||
session.current_index = Some(index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn next(&mut self) {
|
||||
if let Some(session) = self.active_session_mut() {
|
||||
if let Some(idx) = session.current_index {
|
||||
if idx + 1 < session.items.len() {
|
||||
session.current_index = Some(idx + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn previous(&mut self) {
|
||||
if let Some(session) = self.active_session_mut() {
|
||||
if let Some(idx) = session.current_index {
|
||||
if idx > 0 {
|
||||
session.current_index = Some(idx - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn load_file(&mut self, path: &Path) {
|
||||
self.state = DocumentState::Loading;
|
||||
|
||||
match load_document(path) {
|
||||
Ok(content) => {
|
||||
// Align current_index to the loaded file's position in the session.
|
||||
if let Some(session) = self.active_session_mut() {
|
||||
if let Some(idx) = session.items.iter().position(|item| item.path == path) {
|
||||
session.current_index = Some(idx);
|
||||
}
|
||||
}
|
||||
self.state = DocumentState::Loaded(content);
|
||||
}
|
||||
Err(e) => {
|
||||
self.state = DocumentState::Error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
26
core/src/document/mod.rs
Normal file
26
core/src/document/mod.rs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
// src/document/mod.rs
|
||||
//
|
||||
// Document abstraction and format definitions.
|
||||
|
||||
pub mod core;
|
||||
pub mod format;
|
||||
pub mod loader;
|
||||
pub mod manager;
|
||||
pub mod portable;
|
||||
pub mod raster;
|
||||
pub mod session;
|
||||
pub mod store;
|
||||
pub mod vector;
|
||||
|
||||
use self::portable::PortableModel;
|
||||
use self::raster::RasterModel;
|
||||
use self::vector::VectorModel;
|
||||
|
||||
/// Represents the physical content of a loaded document.
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum DocumentContent {
|
||||
Raster(RasterModel),
|
||||
Vector(VectorModel),
|
||||
Portable(PortableModel),
|
||||
}
|
||||
14
core/src/document/portable.rs
Normal file
14
core/src/document/portable.rs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
// src/document/portable.rs
|
||||
//
|
||||
// Portable document format model (PDF).
|
||||
|
||||
use crate::document::core::PageLayout;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct PortableModel {
|
||||
pub path: PathBuf,
|
||||
pub page_count: usize,
|
||||
pub layout: PageLayout,
|
||||
}
|
||||
12
core/src/document/raster.rs
Normal file
12
core/src/document/raster.rs
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
// src/document/raster.rs
|
||||
//
|
||||
// Raster image document model (JPEG, PNG, etc.).
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct RasterModel {
|
||||
pub path: PathBuf,
|
||||
pub dimensions: (u32, u32),
|
||||
}
|
||||
80
core/src/document/session.rs
Normal file
80
core/src/document/session.rs
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
// src/document/session.rs
|
||||
//
|
||||
// A session is the unit of work in Noctua. It groups a set of document items
|
||||
// under a name and tracks which item is currently active.
|
||||
//
|
||||
// There are two kinds of session:
|
||||
//
|
||||
// DirectoryBrowser
|
||||
// Represents a directory on disk. Items are populated by scanning the
|
||||
// directory at open time and re-scanned each time the session file is
|
||||
// loaded – the persisted item list is intentionally ignored on load.
|
||||
// The source directory is stored in `path`.
|
||||
//
|
||||
// DocumentCollection
|
||||
// A curated, ordered list of documents (or individual pages within them).
|
||||
// Items are persisted exactly as stored. Missing files are detected and
|
||||
// removed with a warning when the session file is loaded.
|
||||
// `path` is None for this kind.
|
||||
//
|
||||
// The on-disk representation of a session list is managed by `store.rs`.
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::path::PathBuf;
|
||||
|
||||
/// Determines how a session's items are managed.
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub enum SessionKind {
|
||||
/// Items are derived from a directory scan; `path` holds the source directory.
|
||||
DirectoryBrowser,
|
||||
/// Items are a curated list of documents or pages; `path` is None.
|
||||
DocumentCollection,
|
||||
}
|
||||
|
||||
/// A single entry in a session – a file path and an optional page within that file.
|
||||
///
|
||||
/// `page_index` is 0 for single-page formats (raster, SVG) and non-zero for
|
||||
/// multi-page documents (PDF, multi-frame TIFF) where each page is its own item.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct SessionItem {
|
||||
pub path: PathBuf,
|
||||
pub page_index: usize,
|
||||
}
|
||||
|
||||
impl SessionItem {
|
||||
pub fn new(path: PathBuf, page_index: usize) -> Self {
|
||||
Self { path, page_index }
|
||||
}
|
||||
}
|
||||
|
||||
/// The full state of a session.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct SessionData {
|
||||
pub name: String,
|
||||
pub kind: SessionKind,
|
||||
/// Source directory for `DirectoryBrowser` sessions; `None` for `DocumentCollection`.
|
||||
#[serde(default)]
|
||||
pub path: Option<PathBuf>,
|
||||
pub items: Vec<SessionItem>,
|
||||
/// Index of the currently active item, or `None` if the session is empty.
|
||||
pub current_index: Option<usize>,
|
||||
}
|
||||
|
||||
impl SessionData {
|
||||
pub fn new(
|
||||
name: &str,
|
||||
kind: SessionKind,
|
||||
path: Option<PathBuf>,
|
||||
items: Vec<SessionItem>,
|
||||
) -> Self {
|
||||
let current_index = if items.is_empty() { None } else { Some(0) };
|
||||
Self {
|
||||
name: name.to_string(),
|
||||
kind,
|
||||
path,
|
||||
items,
|
||||
current_index,
|
||||
}
|
||||
}
|
||||
}
|
||||
49
core/src/document/store.rs
Normal file
49
core/src/document/store.rs
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
// src/document/store.rs
|
||||
//
|
||||
// RON-based persistence for session data: load and save session lists.
|
||||
//
|
||||
// For the session concept and the meaning of each field see `session.rs`.
|
||||
//
|
||||
// On disk a session file contains a RON list of SessionData records:
|
||||
//
|
||||
// [
|
||||
// (
|
||||
// name: "Pictures",
|
||||
// kind: DirectoryBrowser,
|
||||
// path: Some("/home/user/Pictures"),
|
||||
// items: [],
|
||||
// current_index: None,
|
||||
// ),
|
||||
// (
|
||||
// name: "Project Docs",
|
||||
// kind: DocumentCollection,
|
||||
// path: None,
|
||||
// items: [
|
||||
// (path: "/home/user/docs/spec.pdf", page_index: 0),
|
||||
// (path: "/home/user/docs/notes.pdf", page_index: 2),
|
||||
// ],
|
||||
// current_index: Some(0),
|
||||
// ),
|
||||
// ]
|
||||
|
||||
use crate::document::session::SessionData;
|
||||
use crate::error::{Error, Result};
|
||||
use std::path::Path;
|
||||
|
||||
/// Loads all sessions from a RON file.
|
||||
pub fn load_sessions(path: &Path) -> Result<Vec<SessionData>> {
|
||||
let content = std::fs::read_to_string(path).map_err(Error::Io)?;
|
||||
|
||||
ron::from_str(&content)
|
||||
.map_err(|e| Error::Session(format!("Failed to parse session file {:?}: {}", path, e)))
|
||||
}
|
||||
|
||||
/// Saves all sessions to a RON file.
|
||||
#[allow(dead_code)]
|
||||
pub fn save_sessions(path: &Path, sessions: &[SessionData]) -> Result<()> {
|
||||
let content = ron::ser::to_string_pretty(sessions, ron::ser::PrettyConfig::default())
|
||||
.map_err(|e| Error::Session(format!("Failed to serialize sessions: {}", e)))?;
|
||||
|
||||
std::fs::write(path, content).map_err(Error::Io)
|
||||
}
|
||||
13
core/src/document/vector.rs
Normal file
13
core/src/document/vector.rs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
// src/document/vector.rs
|
||||
//
|
||||
// Vector image document model (SVG).
|
||||
|
||||
use crate::document::core::ViewBox;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct VectorModel {
|
||||
pub path: PathBuf,
|
||||
pub view_box: Option<ViewBox>,
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue