Merge pull request #7119 from texjer/feat/camera-raw-refocus

Camera Raw: coming back to PhotoCraft brings its window to the front
This commit is contained in:
Tanish Manku authored and GitHub committed 2026-11-10 23:58:29 -07:00
commit db1b516452
4 files changed
+104 -3

No files matched your search

@@ -82,6 +82,8 @@ pub struct CameraRawDialog {
pub(crate) detail: crate::camera_raw_detail_ui::DetailPreview,
display: std::sync::Arc<photocraft_engine::display_color::CanvasDisplay>,
pub(crate) viewport: Option<ERect>,
/// Whether the main window had focus last frame (see [`show`]).
main_focused: bool,
pub(crate) float: bool,
tex: Option<TextureHandle>,
before_tex: Option<TextureHandle>,
@@ -206,14 +206,28 @@ pub fn show(app: &mut PhotocraftApp, ctx: &egui::Context) {
// Camera Raw is modal, as in Photoshop: the main window is dimmed and takes no input while it
// is open (in a separate window, or drawn over it where there are no extra windows).
let main = ctx.content_rect();
egui::Area::new(egui::Id::unique("camera-raw-modal")).order(egui::Order::Foreground).fixed_pos(main.min).show(ctx, |ui| {
let (r, _) = ui.allocate_exact_size(main.size(), Sense::click_and_drag());
let scrim = egui::Area::new(egui::Id::unique("camera-raw-modal")).order(egui::Order::Foreground).fixed_pos(main.min).show(ctx, |ui| {
let (r, response) = ui.allocate_exact_size(main.size(), Sense::click_and_drag());
ui.painter().rect_filled(r, 0.0, Tokens::get(ui.ctx()).scrim);
response
});
let window = egui::ViewportId::from_hash_of("camera-raw-window");
// Coming back to PhotoCraft (⌘Tab, the Dock) can bring the main window over Camera Raw,
// leaving only the dimmed window that takes no input: the main window gaining focus, or a
// click on it, brings Camera Raw back to the front.
let main_focused = ctx.input(|i| i.viewport().focused.unwrap_or(false));
if let Some(d) = app.camera_raw.as_mut() {
if raise_camera_raw(d.main_focused, main_focused, scrim.inner.clicked() || scrim.inner.drag_started()) {
for command in raise_commands() {
ctx.send_viewport_cmd_to(window, command);
}
}
d.main_focused = main_focused;
}
// A window of its own (title bar, moved and resized like any window), like Adobe Camera Raw.
let builder = crate::os_window::builder().with_title(window_title).with_inner_size([1360.0, 900.0]).with_min_inner_size([900.0, 600.0]);
let mut close = false;
ctx.show_viewport_immediate(egui::ViewportId::from_hash_of("camera-raw-window"), builder, |ui, class| {
ctx.show_viewport_immediate(window, builder, |ui, class| {
if ui.ctx().input(|i| i.viewport().close_requested()) {
close = true;
}
@@ -224,6 +238,18 @@ pub fn show(app: &mut PhotocraftApp, ctx: &egui::Context) {
}
}
/// Whether to bring Camera Raw's window to the front: when the main window has just gained focus
/// (it came forward over Camera Raw's) or is clicked.
fn raise_camera_raw(was_focused: bool, focused: bool, clicked: bool) -> bool {
(focused && !was_focused) || clicked
}
/// Brings Camera Raw's window to the front, restoring it first if it was minimized (a minimized
/// window ignores Focus).
fn raise_commands() -> [egui::ViewportCommand; 2] {
[egui::ViewportCommand::Minimized(false), egui::ViewportCommand::Focus]
}
impl CameraRawDialog {
/// "RAW Develop (name)" for the open-time dialog, "RAW Develop Filter (layer)" otherwise.
fn window_title(&self) -> String {
@@ -551,3 +577,26 @@ fn draw(app: &mut PhotocraftApp, ctx: &egui::Context, own_window: bool) {
_ => {}
}
}
#[cfg(test)]
mod tests {
use super::*;
/// Coming back to PhotoCraft can put the main window over Camera Raw's: the main window
/// gaining focus brings Camera Raw's window to the front, once per activation, and so does a
/// click on the dimmed main window.
#[test]
fn the_main_window_gaining_focus_brings_camera_raw_back_to_the_front() {
assert!(raise_camera_raw(false, true, false), "the main window came forward");
assert!(!raise_camera_raw(true, true, false), "only once, so Camera Raw's own focus isn't fought");
assert!(!raise_camera_raw(true, false, false) && !raise_camera_raw(false, false, false));
assert!(raise_camera_raw(true, true, true), "a click on the dimmed main window");
}
/// Focus does nothing for a minimized window, which would leave only the dimmed main window
/// that takes no input: Camera Raw's is restored first.
#[test]
fn raising_camera_raw_restores_it_when_minimized() {
assert_eq!(raise_commands(), [egui::ViewportCommand::Minimized(false), egui::ViewportCommand::Focus]);
}
}
@@ -198,6 +198,7 @@ pub(super) fn open_pixels(
undo: Vec::new(),
redo: Vec::new(),
edit_origin: None,
main_focused: false,
};
d.render(ctx);
app.camera_raw = Some(d);
@@ -91,6 +91,55 @@ fn closing_the_camera_raw_window_cancels() {
assert_eq!(app.session.active().unwrap().history.past_len(), steps);
}
/// Coming back to PhotoCraft (⌘Tab, the Dock) can put the main window over Camera Raw's, or
/// leave Camera Raw minimized: the main window gaining focus, or a click on its dimmed surface,
/// restores Camera Raw's window and focuses it, once per activation.
#[test]
fn the_main_window_gaining_focus_or_a_click_raises_the_camera_raw_window() {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());
let ctx = egui::Context::default();
PhotocraftApp::setup_context(&ctx, crate::theme::ThemeKind::Pro);
ctx.set_embed_viewports(false);
// A multi-window backend for this thread: Camera Raw gets a window of its own, as on a desktop,
// and the commands its window receives are recorded.
let sent = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
let record = sent.clone();
egui::Context::set_immediate_viewport_renderer(move |ctx, immediate| {
let id = immediate.ids.this;
let mut callback = immediate.viewport_ui_cb;
let input = egui::RawInput { viewport_id: id, viewports: std::iter::once((id, egui::ViewportInfo::default())).collect(), ..Default::default() };
let mut out = ctx.run_ui(input, |ui| callback(ui));
out.textures_delta.clear();
if let Some(v) = out.viewport_output.get(&id) {
record.lock().unwrap().extend(v.commands.iter().cloned());
}
});
app.run("file.new", json!({"width": 64, "height": 48})).unwrap();
open(&mut app, &ctx).unwrap();
let window = egui::ViewportId::from_hash_of("camera-raw-window");
let raised = [egui::ViewportCommand::Minimized(false), egui::ViewportCommand::Focus];
let frame = |app: &mut PhotocraftApp, focused: bool, events: Vec<egui::Event>| {
let mut input =
egui::RawInput { screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(800.0, 600.0))), events, ..Default::default() };
input.viewports.entry(egui::ViewportId::ROOT).or_default().focused = Some(focused);
let mut out = ctx.run_ui(input, |ui| show(app, ui.ctx()));
out.textures_delta.clear();
let mut commands: Vec<_> = std::mem::take(&mut *sent.lock().unwrap());
commands.extend(out.viewport_output.get(&window).map(|v| v.commands.clone()).unwrap_or_default());
// Only the raising commands: the window also gets its own setup (title, size) from egui.
commands.retain(|c| matches!(c, egui::ViewportCommand::Minimized(_) | egui::ViewportCommand::Focus));
commands
};
assert!(frame(&mut app, false, Vec::new()).is_empty(), "Camera Raw's window is in front");
assert_eq!(frame(&mut app, true, Vec::new()), raised, "the main window came forward");
assert!(frame(&mut app, true, Vec::new()).is_empty(), "once per activation, so Camera Raw's own focus isn't fought");
let at = egui::pos2(400.0, 300.0);
let button = |pressed| egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers: egui::Modifiers::NONE };
frame(&mut app, true, vec![egui::Event::PointerMoved(at), button(true)]);
let after_click = frame(&mut app, true, vec![button(false)]);
assert_eq!(after_click, raised, "a click on the dimmed main window");
}
/// A layer with tonal variation, a selection over part of it and the dialog open on it.
fn worker_app() -> (PhotocraftApp, egui::Context) {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());