playa 0.1.142

Image sequence player (EXR, PNG, JPEG, TIFF, .MP4). Pure Rust with optional OpenEXR/FFmpeg support.
Documentation
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
use eframe::egui;
use egui_ltreeview::TreeView;
use std::collections::HashMap;

use super::prefs_events::SetGizmoPrefsEvent;

/// Settings categories
#[derive(Debug, Clone, Copy, PartialEq)]
enum SettingsCategory {
    General,
    UI,
    Cache,
    Gizmo,
    Compositing,
    WebServer,
}

impl SettingsCategory {
    fn as_str(&self) -> &'static str {
        match self {
            SettingsCategory::General => "General",
            SettingsCategory::UI => "UI",
            SettingsCategory::Cache => "Cache",
            SettingsCategory::Gizmo => "Gizmo",
            SettingsCategory::Compositing => "Compositing",
            SettingsCategory::WebServer => "Web Server",
        }
    }

    fn from_str(s: &str) -> Option<Self> {
        match s {
            "General" => Some(SettingsCategory::General),
            "UI" => Some(SettingsCategory::UI),
            "Cache" => Some(SettingsCategory::Cache),
            "Gizmo" => Some(SettingsCategory::Gizmo),
            "Compositing" => Some(SettingsCategory::Compositing),
            "Web Server" => Some(SettingsCategory::WebServer),
            _ => None,
        }
    }
}

/// Compositor backend selection
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[derive(Default)]
pub enum CompositorBackend {
    #[default]
    Cpu,
    Gpu,
}

/// Event emitted when compositor backend changes
#[derive(Debug, Clone)]
pub struct CompositorBackendChangedEvent {
    pub backend: CompositorBackend,
}


/// UI Layout configuration (dock splits, timeline/viewport state)
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
pub struct Layout {
    pub dock_state_json: String,
    pub timeline_zoom: f32,
    pub timeline_pan_offset: f32,
    pub timeline_outline_width: f32,
    pub timeline_view_mode: String,
    pub viewport_zoom: f32,
    pub viewport_pan: [f32; 2],
    pub viewport_mode: String,
}

impl Default for Layout {
    fn default() -> Self {
        Self {
            dock_state_json: String::new(),
            timeline_zoom: 1.0,
            timeline_pan_offset: 0.0,
            timeline_outline_width: 400.0,
            timeline_view_mode: "Split".to_string(),
            viewport_zoom: 1.0,
            viewport_pan: [0.0, 0.0],
            viewport_mode: "AutoFit".to_string(),
        }
    }
}

/// Application settings
#[derive(serde::Deserialize, serde::Serialize)]
#[serde(default)]
pub struct AppSettings {
    // Playback
    pub fps_base: f32, // Base FPS (persistent)
    pub loop_enabled: bool,

    // Shader
    pub current_shader: String,

    // UI
    pub show_help: bool,
    pub show_playlist: bool,
    pub show_attributes_editor: bool,
    pub show_frame_numbers: bool, // Show frame numbers on timeslider
    pub show_tooltips: bool,      // Show tooltips on toolbar controls (2s delay)
    pub dark_mode: bool,
    pub font_size: f32,
    pub timeline_layer_height: f32,       // Layer row height in timeline (default 32.0)
    pub timeline_name_column_width: f32,  // Name column width in outline (default 150.0)
    pub timeline_outline_top_offset: f32, // Fine-tune outline vertical alignment with canvas
    pub timeline_snap_enabled: bool,
    pub timeline_lock_work_area: bool,
    pub viewport_hover_highlight: bool,
    pub tools_selection_highlight: bool,  // Show highlight for selected layer in Move/Rotate/Scale modes
    pub timeline_hover_highlight: bool,
    pub hover_stroke_width: f32,
    pub hover_corner_length: f32,
    pub hover_opacity: f32,
    pub preload_radius: i32, // Frames to preload around playhead (-1 = all, default 100)
    pub preload_delay_ms: u64, // Delay before full preload after attr change (default 500ms)

    // Workers (applied to App::workers / playback/encoding threads)
    pub workers_override: u32, // 0 = auto, N = override (applies on restart)

    // Cache & Memory
    pub cache_memory_percent: f32,      // 25-95% of available (default 75%)
    pub reserve_system_memory_gb: f32,  // Reserve for system (default 2.0 GB)
    pub cache_strategy: crate::entities::CacheStrategy, // Caching strategy (LastOnly or All)

    // Compositor backend (CPU or GPU)
    pub compositor_backend: CompositorBackend,

    // Encoding dialog
    pub encode_dialog: crate::dialogs::encode::EncodeDialogSettings,

    // Internal
    pub selected_settings_category: Option<String>,

    // REST API Server
    pub api_server_enabled: bool,
    pub api_server_port: Option<u16>,

    // Layouts (named UI configurations)
    pub layouts: HashMap<String, Layout>,
    pub current_layout: String,
}

impl Default for AppSettings {
    fn default() -> Self {
        Self {
            fps_base: 24.0,
            loop_enabled: true,
            current_shader: "default".to_string(),
            show_help: true,
            show_playlist: true,
            show_attributes_editor: true,
            show_frame_numbers: true,
            show_tooltips: true,
            dark_mode: true,
            font_size: 11.0,
            timeline_layer_height: 30.0,
            timeline_name_column_width: 80.0,
            timeline_outline_top_offset: 42.0, // fine-tuned for alignment with canvas
            timeline_snap_enabled: true,
            timeline_lock_work_area: false,
            viewport_hover_highlight: true,
            tools_selection_highlight: true,
            timeline_hover_highlight: false,
            hover_stroke_width: 2.0,
            hover_corner_length: 20.0,
            hover_opacity: 0.5,
            preload_radius: -1,
            preload_delay_ms: 500,
            workers_override: 0,
            cache_memory_percent: 75.0,
            reserve_system_memory_gb: 2.0,
            cache_strategy: crate::entities::CacheStrategy::All, // Default: cache all frames
            compositor_backend: CompositorBackend::default(),
            encode_dialog: crate::dialogs::encode::EncodeDialogSettings::default(),
            selected_settings_category: Some("UI".to_string()),
            api_server_enabled: false,
            api_server_port: Some(9876),
            layouts: HashMap::new(),
            current_layout: String::new(),
        }
    }
}

/// Render General settings category
fn render_general_settings(ui: &mut egui::Ui, _settings: &mut AppSettings) {
    ui.label("General settings will be added here.");
}

/// Render Web Server settings category
fn render_webserver_settings(ui: &mut egui::Ui, settings: &mut AppSettings) {
    ui.heading("REST API Server");
    ui.add_space(8.0);

    ui.checkbox(&mut settings.api_server_enabled, "Enable REST API server");
    ui.add_space(8.0);

    ui.horizontal(|ui| {
        ui.label("Port:");
        let mut port = settings.api_server_port.unwrap_or(9876) as i32;
        if ui.add(egui::DragValue::new(&mut port).range(1024..=65535)).changed() {
            settings.api_server_port = Some(port as u16);
        }
    });
    ui.add_space(12.0);

    if settings.api_server_enabled {
        let port = settings.api_server_port.unwrap_or(9876);
        ui.separator();
        ui.add_space(8.0);
        ui.label("Server URL:");
        ui.monospace(format!("http://0.0.0.0:{}", port));

        ui.add_space(12.0);
        ui.label("Status endpoints:");
        ui.monospace("GET  /api/status              - full status");
        ui.monospace("GET  /api/player              - player state");
        ui.monospace("GET  /api/comp                - active comp info");
        ui.monospace("GET  /api/cache               - cache stats");
        ui.monospace("GET  /api/health              - health check");

        ui.add_space(8.0);
        ui.label("Player control:");
        ui.monospace("POST /api/player/play         - start playback");
        ui.monospace("POST /api/player/pause        - pause");
        ui.monospace("POST /api/player/stop         - stop (pause + seek 0)");
        ui.monospace("POST /api/player/frame/{n}    - seek to frame");
        ui.monospace("POST /api/player/fps/{n}      - set FPS");
        ui.monospace("POST /api/player/toggle-loop  - toggle loop");
        ui.monospace("POST /api/player/next         - next frame");
        ui.monospace("POST /api/player/prev         - prev frame");

        ui.add_space(8.0);
        ui.label("Project:");
        ui.monospace("POST /api/project/load        - load sequence (JSON)");

        ui.add_space(8.0);
        ui.label("Screenshots:");
        ui.monospace("GET  /api/screenshot          - full window (with UI)");
        ui.monospace("GET  /api/screenshot/frame    - raw frame only");

        ui.add_space(8.0);
        ui.label("Other:");
        ui.monospace("POST /api/event               - emit custom event");
        ui.monospace("POST /api/app/exit            - exit application");
    } else {
        ui.add_space(8.0);
        ui.label("Enable to start the REST API server.");
    }
}

/// Render UI settings category
fn render_ui_settings(ui: &mut egui::Ui, settings: &mut AppSettings) {
    ui.heading("Appearance");
    ui.add_space(8.0);

    ui.label("Font Size:");
    ui.add(
        egui::Slider::new(&mut settings.font_size, 10.0..=18.0)
            .suffix(" px")
            .step_by(0.5),
    );
    ui.add_space(8.0);

    ui.label("Timeline Layer Height:");
    ui.add(
        egui::Slider::new(&mut settings.timeline_layer_height, 20.0..=64.0)
            .suffix(" px")
            .step_by(2.0),
    );
    ui.add_space(8.0);

    ui.label("Timeline Name Column Width:");
    ui.add(
        egui::Slider::new(&mut settings.timeline_name_column_width, 80.0..=300.0)
            .suffix(" px")
            .step_by(10.0),
    );
    ui.add_space(8.0);

    ui.label("Timeline Outline Top Offset:");
    ui.add(
        egui::Slider::new(&mut settings.timeline_outline_top_offset, 0.0..=120.0)
            .suffix(" px")
            .step_by(1.0),
    );
    ui.add_space(16.0);

    ui.checkbox(&mut settings.dark_mode, "Dark Mode");
    ui.checkbox(&mut settings.show_tooltips, "Show Tooltips (2s delay on toolbar controls)");
    ui.checkbox(&mut settings.viewport_hover_highlight, "Viewport hover highlight (Select mode)");
    ui.checkbox(&mut settings.tools_selection_highlight, "Tools selection highlight (Move/Rotate/Scale)");
    ui.checkbox(&mut settings.timeline_hover_highlight, "Timeline hover highlight");
    ui.horizontal(|ui| {
        ui.label("Hover stroke:");
        ui.add(egui::Slider::new(&mut settings.hover_stroke_width, 1.0..=5.0).suffix("px").step_by(0.5));
    });
    ui.horizontal(|ui| {
        ui.label("Hover corner:");
        ui.add(egui::Slider::new(&mut settings.hover_corner_length, 5.0..=50.0).suffix("px").step_by(1.0));
    });
    ui.horizontal(|ui| {
        ui.label("Hover opacity:");
        ui.add(egui::Slider::new(&mut settings.hover_opacity, 0.1..=1.0).step_by(0.1));
    });

    ui.add_space(16.0);
    ui.heading("Performance");
    ui.add_space(8.0);

    ui.label("Worker Threads Override (0 = Auto):");
    ui.add(
        egui::DragValue::new(&mut settings.workers_override)
            .speed(1.0)
            .range(0..=256),
    );
    ui.label("Takes effect on next launch. Defaults to ~75% of CPU cores.");

    ui.add_space(8.0);
    ui.label("Preload/cache settings moved to Settings → Cache.");
}

/// Render Cache settings category
fn render_cache_settings(ui: &mut egui::Ui, settings: &mut AppSettings) {
    ui.heading("Preload");
    ui.add_space(8.0);

    ui.label("Preload Radius (frames):");
    ui.horizontal(|ui| {
        if settings.preload_radius < 0 {
            ui.label("All");
            if ui.small_button("Set limit").clicked() {
                settings.preload_radius = 100;
            }
        } else {
            ui.add(
                egui::Slider::new(&mut settings.preload_radius, 10..=500)
                    .step_by(10.0),
            );
            if ui.small_button("All").clicked() {
                settings.preload_radius = -1;
            }
        }
    });
    ui.label("Frames to preload around playhead (-1 = entire comp).");

    ui.add_space(8.0);
    ui.label("Preload Delay (ms):");
    ui.add(
        egui::Slider::new(&mut settings.preload_delay_ms, 0..=2000)
            .suffix(" ms")
            .step_by(50.0),
    );
    ui.label("Delay before full preload after attribute change. 0 = immediate.");

    ui.add_space(16.0);
    ui.heading("Cache & Memory");
    ui.add_space(8.0);

    ui.label("Cache Memory Limit (% of available):");
    ui.add(
        egui::Slider::new(&mut settings.cache_memory_percent, 25.0..=95.0)
            .suffix("%")
            .step_by(5.0),
    );
    ui.label("Maximum memory used for frame caching.");

    ui.add_space(8.0);
    ui.label("Reserve for System (GB):");
    ui.add(
        egui::Slider::new(&mut settings.reserve_system_memory_gb, 0.5..=8.0)
            .suffix(" GB")
            .step_by(0.5),
    );
    ui.label("Minimum memory reserved for OS and other apps.");

    ui.add_space(8.0);
    ui.label("Cache Strategy:");
    ui.horizontal(|ui| {
        use crate::entities::CacheStrategy;
        ui.radio_value(&mut settings.cache_strategy, CacheStrategy::All, "All Frames");
        ui.radio_value(&mut settings.cache_strategy, CacheStrategy::LastOnly, "Last Only");
    });
    ui.label("All Frames: Maximum performance, more memory usage.");
    ui.label("Last Only: Minimal memory, only last accessed frame per comp.");
}

/// Render Gizmo settings category (stored in the current Project)
fn render_gizmo_settings(
    ui: &mut egui::Ui,
    project: Option<&crate::entities::Project>,
    event_bus: Option<&crate::core::event_bus::EventBus>,
) {
    ui.heading("Gizmo");
    ui.add_space(8.0);

    let Some(project) = project else {
        ui.label("No active project - gizmo settings are stored per project.");
        return;
    };

    let Some(bus) = event_bus else {
        ui.label("Event bus unavailable - cannot apply changes.");
        return;
    };

    let current = project.gizmo_prefs();
    let mut next = current.clone();
    let mut changed = false;

    ui.label("These settings are saved inside the Project (attrs.prefs). ");
    ui.add_space(8.0);

    ui.horizontal(|ui| {
        ui.label("Size");
        changed |= ui
            .add(egui::Slider::new(&mut next.pref_manip_size, 20.0..=250.0).suffix(" px"))
            .changed();
    });

    ui.horizontal(|ui| {
        ui.label("Stroke width");
        changed |= ui
            .add(egui::Slider::new(&mut next.pref_manip_stroke_width, 0.5..=8.0).suffix(" px"))
            .changed();
    });

    ui.horizontal(|ui| {
        ui.label("Inactive alpha");
        changed |= ui
            .add(egui::Slider::new(&mut next.pref_manip_inactive_alpha, 0.0..=1.0))
            .changed();
    });

    ui.horizontal(|ui| {
        ui.label("Highlight alpha");
        changed |= ui
            .add(egui::Slider::new(&mut next.pref_manip_highlight_alpha, 0.0..=1.0))
            .changed();
    });

    if changed {
        bus.emit(SetGizmoPrefsEvent(next));
        ui.ctx().request_repaint();
    }
}

/// Render Compositing settings category
fn render_compositing_settings(
    ui: &mut egui::Ui,
    settings: &mut AppSettings,
    event_bus: Option<&crate::core::event_bus::EventBus>,
) {
    ui.heading("Backend");
    ui.add_space(8.0);

    let prev_backend = settings.compositor_backend;
    ui.horizontal(|ui| {
        ui.label("Compositor:");
        ui.radio_value(&mut settings.compositor_backend, CompositorBackend::Cpu, "CPU");
        ui.radio_value(&mut settings.compositor_backend, CompositorBackend::Gpu, "GPU");
    });
    // Emit event if changed
    if settings.compositor_backend != prev_backend
        && let Some(bus) = event_bus {
            bus.emit(CompositorBackendChangedEvent {
                backend: settings.compositor_backend,
            });
        }
    ui.label("GPU compositor uses OpenGL for 10-50x faster multi-layer blending.");
    ui.label("Requires OpenGL 3.0+. Falls back to CPU on errors.");

    ui.add_space(16.0);
    ui.heading("Safety");
    ui.add_space(8.0);
    ui.label("Cycle detection is always enabled.");
    ui.label("Prevents infinite loops when compositions reference each other.");
}

/// Render settings window
pub fn render_settings_window(
    ctx: &egui::Context,
    show_settings: &mut bool,
    settings: &mut AppSettings,
    project: Option<&crate::entities::Project>,
    event_bus: Option<&crate::core::event_bus::EventBus>,
) {
    // Get selected category from settings or use default
    let mut selected = settings
        .selected_settings_category
        .as_ref()
        .and_then(|s| SettingsCategory::from_str(s))
        .unwrap_or(SettingsCategory::UI);

    egui::Window::new("Settings")
        .id(egui::Id::new("settings_window"))
        .open(show_settings)
        .default_size([700.0, 500.0])
        .min_size([500.0, 400.0])
        .resizable(true)
        .collapsible(false)
        .show(ctx, |ui| {
            egui::ScrollArea::both()
                .auto_shrink([false; 2])
                .show(ui, |ui| {
                    ui.horizontal(|ui| {
                        // Left panel: TreeView (200px fixed width)
                        ui.vertical(|ui| {
                            ui.set_width(200.0);
                            ui.add_space(4.0);

                            let tree_id = ui.make_persistent_id("settings_tree_view");
                            let (_response, actions) = TreeView::new(tree_id).show(ui, |builder| {
                                builder.leaf(0, SettingsCategory::General.as_str());
                                builder.leaf(1, SettingsCategory::UI.as_str());
                                builder.leaf(2, SettingsCategory::Cache.as_str());
                                builder.leaf(3, SettingsCategory::Gizmo.as_str());
                                builder.leaf(4, SettingsCategory::Compositing.as_str());
                                builder.leaf(5, SettingsCategory::WebServer.as_str());
                            });

                            // Handle selection from actions
                            for action in actions {
                                if let egui_ltreeview::Action::SetSelected(node_ids) = action
                                    && let Some(&node_id) = node_ids.first()
                                {
                                    selected = match node_id {
                                        0 => SettingsCategory::General,
                                        1 => SettingsCategory::UI,
                                        2 => SettingsCategory::Cache,
                                        3 => SettingsCategory::Gizmo,
                                        4 => SettingsCategory::Compositing,
                                        5 => SettingsCategory::WebServer,
                                        _ => selected,
                                    };
                                }
                            }
                        });

                        ui.separator();

                        // Right panel: content for selected category
                        ui.vertical(|ui| {
                            ui.add_space(8.0);

                            match selected {
                                SettingsCategory::General => render_general_settings(ui, settings),
                                SettingsCategory::UI => render_ui_settings(ui, settings),
                                SettingsCategory::Cache => render_cache_settings(ui, settings),
                                SettingsCategory::Gizmo => render_gizmo_settings(ui, project, event_bus),
                                SettingsCategory::Compositing => render_compositing_settings(ui, settings, event_bus),
                                SettingsCategory::WebServer => render_webserver_settings(ui, settings),
                            }
                        });
                    });
                });
        });

    // Save selected category
    settings.selected_settings_category = Some(selected.as_str().to_string());
}