diff --git a/crates/ui-egui/src/docking.rs b/crates/ui-egui/src/docking.rs index 761928ec..fd2cc368 100644 --- a/crates/ui-egui/src/docking.rs +++ b/crates/ui-egui/src/docking.rs @@ -29,6 +29,8 @@ pub(crate) use shown_group_tab::HEADER_TITLE_LEFT; pub enum Panel { Viewport, Assets, + /// A second Assets panel with its own search, type and folder (the Assets panel's folder button) + AssetsSecond, // Saved layouts name the bake panels by their earlier names #[serde(rename = "BakeMaps")] MeshMapSettings, @@ -82,7 +84,7 @@ impl Panel { use crate::state::Mode; match self { Self::MeshMapSettings | Self::CommonSettings | Self::MeshMapBakers | Self::BakingLog => mode == Mode::Baking, - Self::Assets | Self::Layers | Self::Properties | Self::TextureSetSettings | Self::Export => mode == Mode::Painting, + Self::Assets | Self::AssetsSecond | Self::Layers | Self::Properties | Self::TextureSetSettings | Self::Export => mode == Mode::Painting, Self::History => mode != Mode::Rendering, Self::Shader => mode != Mode::Baking, Self::Renderer => mode == Mode::Rendering, @@ -90,9 +92,10 @@ impl Panel { } } - pub const ALL: [Panel; 16] = [ + pub const ALL: [Panel; 17] = [ Panel::Viewport, Panel::Assets, + Panel::AssetsSecond, Panel::MeshMapSettings, Panel::CommonSettings, Panel::MeshMapBakers, @@ -113,6 +116,8 @@ impl Panel { match self { Panel::Viewport => "Viewport", Panel::Assets => "Assets", + // The second panel shows every type at first, so it's named for that, as the reference names it + Panel::AssetsSecond => "All", Panel::MeshMapSettings => "Mesh Map Settings", Panel::CommonSettings => "Common Settings", Panel::MeshMapBakers => "Mesh Map Bakers", @@ -194,12 +199,12 @@ pub fn default_workspace() -> Workspace { } } - for panel in [MeshMapSettings, CommonSettings, MeshMapBakers] { + for (panel, anchor) in [(MeshMapSettings, TextureSetList), (CommonSettings, TextureSetList), (MeshMapBakers, TextureSetList), (AssetsSecond, Assets)] { workspace.hidden.push(( panel, Location { floating: None, - anchor: Some(TextureSetList), + anchor: Some(anchor), placement: Placement::Tab { before: None }, size: None, stack: None, @@ -328,7 +333,7 @@ impl Dock { /// Texture sets over the bakers, the two settings tabs, the log under both, then the viewport pub(crate) fn for_baking() -> Self { let mut workspace = default_workspace(); - for panel in [Panel::Assets, Panel::Layers, Panel::TextureSetSettings, Panel::Properties] { + for panel in [Panel::Assets, Panel::AssetsSecond, Panel::Layers, Panel::TextureSetSettings, Panel::Properties] { if let Ok(location) = workspace.layout.location(&panel) { workspace.hidden.push((panel, location)); } @@ -558,7 +563,8 @@ fn min_size(panel: Panel) -> Vec2 { /// The body of every panel except the viewport, in the dock or on the rail pub(crate) fn panel_body(app: &mut TextureCraftApp, ui: &mut Ui, panel: Panel) { // No project open: the library and the log work, the other panels about a project stay empty - let works_empty = matches!(panel, Panel::Assets | Panel::Log | Panel::Layers | Panel::TextureSetList | Panel::Properties | Panel::Export); + let works_empty = + matches!(panel, Panel::Assets | Panel::AssetsSecond | Panel::Log | Panel::Layers | Panel::TextureSetList | Panel::Properties | Panel::Export); if !app.session.project_open && !works_empty { return; } @@ -568,6 +574,7 @@ pub(crate) fn panel_body(app: &mut TextureCraftApp, ui: &mut Ui, panel: Panel) { egui::Frame::new().inner_margin(margin).show(ui, |ui| match panel { Panel::MeshMapSettings => crate::bake_mode::mesh_map_settings_panel(app, ui), Panel::Assets => crate::shelf::body(app, ui), + Panel::AssetsSecond => crate::shelf_second::body(app, ui), Panel::CommonSettings => crate::bake_mode::common_settings_panel(app, ui), Panel::MeshMapBakers => crate::bake_mode::mesh_map_bakers_panel(app, ui), Panel::TextureSetList if app.ui.mode == crate::state::Mode::Baking => crate::bake_mode::texture_sets(app, ui), diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index df72f333..77ec52f8 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -146,6 +146,8 @@ mod shelf_folders; mod shelf_menu; pub mod shelf_panes; pub mod shelf_preview; +pub mod shelf_query; +pub mod shelf_second; mod shelf_tabs; mod shelf_toolbar; pub mod shelf_updates; diff --git a/crates/ui-egui/src/shelf_query.rs b/crates/ui-egui/src/shelf_query.rs new file mode 100644 index 00000000..9e9d4b67 --- /dev/null +++ b/crates/ui-egui/src/shelf_query.rs @@ -0,0 +1,24 @@ +//! One Assets panel's query: search, type, folder and favorites only + +use serde::{Deserialize, Serialize}; + +/// One Assets panel's search, type and folder, and whether it shows only favorites +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(default)] +pub struct ShelfQuery { + /// The search text + pub filter: String, + /// The picked type: an index into the shelf's types, or past them for every type + pub tab: usize, + /// The library folder: a library id, then folder names (empty: all libraries) + pub path: Vec, + /// Only favorites (the Favorites saved search) + pub favorites_only: bool, +} + +impl Default for ShelfQuery { + /// Every asset of every type + fn default() -> Self { + Self { filter: String::new(), tab: crate::shelf_tabs::NO_TYPE, path: Vec::new(), favorites_only: false } + } +} diff --git a/crates/ui-egui/src/shelf_second.rs b/crates/ui-egui/src/shelf_second.rs new file mode 100644 index 00000000..4c129816 --- /dev/null +++ b/crates/ui-egui/src/shelf_second.rs @@ -0,0 +1,23 @@ +//! The second Assets panel, opened by the folder button: the same panel with its own query + +use egui::Ui; + +use crate::TextureCraftApp; +use crate::shelf_query::ShelfQuery; + +/// Swap `query` with the first Assets panel's query in `ui` +fn swap_query(ui: &mut crate::state::UiState, query: &mut ShelfQuery) { + std::mem::swap(&mut ui.shelf_filter, &mut query.filter); + std::mem::swap(&mut ui.shelf_tab, &mut query.tab); + std::mem::swap(&mut ui.shelf_path, &mut query.path); + std::mem::swap(&mut ui.shelf_favorites_only, &mut query.favorites_only); +} + +/// The second Assets panel's body: the Assets panel drawn with its own query +pub(crate) fn body(app: &mut TextureCraftApp, ui: &mut Ui) { + let mut query = std::mem::take(&mut app.ui.shelf_second); + swap_query(&mut app.ui, &mut query); + ui.push_id("assets_second", |ui| crate::shelf::body(app, ui)); + swap_query(&mut app.ui, &mut query); + app.ui.shelf_second = query; +} diff --git a/crates/ui-egui/src/shelf_toolbar.rs b/crates/ui-egui/src/shelf_toolbar.rs index 99a80cb6..c61d37bc 100644 --- a/crates/ui-egui/src/shelf_toolbar.rs +++ b/crates/ui-egui/src/shelf_toolbar.rs @@ -1,4 +1,4 @@ -//! The shelf's bottom toolbar: the side pane toggles on the left; refresh with its update settings, reset query and import on the right +//! The shelf's bottom toolbar: the side pane toggles on the left; refresh, reset query, new panel and import on the right use egui::Ui; use egui_phosphor::regular as ic; @@ -26,6 +26,9 @@ pub(crate) fn show(app: &mut TextureCraftApp, ui: &mut Ui) { if icon_button(ui, ic::PLUS, tr("Import resources..."), false, size).clicked() { crate::menus::invoke(app, &ui.ctx().clone(), "resource.import"); } + if icon_button(ui, ic::FOLDER, tr("New Assets panel"), false, size).clicked() { + app.ui.dock.focus(crate::docking::Panel::AssetsSecond); + } if icon_button(ui, ic::X_CIRCLE, tr("Reset query"), false, size).clicked() { reset_query(app); } diff --git a/crates/ui-egui/src/state.rs b/crates/ui-egui/src/state.rs index 7caf4d29..3cdf54db 100644 --- a/crates/ui-egui/src/state.rs +++ b/crates/ui-egui/src/state.rs @@ -593,6 +593,9 @@ pub struct UiState { /// The asset shelf shows only favorites (the Favorites saved search) #[serde(default)] pub shelf_favorites_only: bool, + /// The second Assets panel's search, type and folder, swapped in while it draws + #[serde(default)] + pub shelf_second: crate::shelf_query::ShelfQuery, /// The asset shelf's automatic updates (its refresh popup) #[serde(default)] pub shelf_updates: crate::shelf_updates::ShelfUpdates, @@ -757,6 +760,7 @@ impl UiState { self.shelf_saved_searches = keep.shelf_saved_searches; self.shelf_favorites = keep.shelf_favorites; self.shelf_favorites_only = keep.shelf_favorites_only; + self.shelf_second = keep.shelf_second; self.shelf_custom_previews = keep.shelf_custom_previews; self.shelf_updates = keep.shelf_updates; } @@ -795,6 +799,7 @@ impl Default for UiState { shelf_saved_searches: Vec::new(), shelf_favorites: Vec::new(), shelf_favorites_only: false, + shelf_second: crate::shelf_query::ShelfQuery::default(), shelf_custom_previews: Vec::new(), shelf_updates: crate::shelf_updates::ShelfUpdates::default(), shelf_panes_split: default_shelf_panes_split(), diff --git a/crates/ui-egui/tests/it/main.rs b/crates/ui-egui/tests/it/main.rs index 7354a597..85bf32ed 100644 --- a/crates/ui-egui/tests/it/main.rs +++ b/crates/ui-egui/tests/it/main.rs @@ -84,6 +84,7 @@ mod shelf_drop; mod shelf_hover_preview; mod shelf_menu; mod shelf_panel; +mod shelf_second; mod shelf_updates; mod shift_lines_2d; mod shift_lines_3d; diff --git a/crates/ui-egui/tests/it/shelf_second.rs b/crates/ui-egui/tests/it/shelf_second.rs new file mode 100644 index 00000000..7095f6bc --- /dev/null +++ b/crates/ui-egui/tests/it/shelf_second.rs @@ -0,0 +1,44 @@ +//! The Assets panel's folder button opens a second Assets panel with its own search + +use egui::Role; +use egui_kittest::Harness; +use egui_kittest::kittest::Queryable; +use texturecraft_ui_egui::TextureCraftApp; +use texturecraft_ui_egui::docking::Panel; + +/// The sample project at 1600 x 960 +fn sample_app() -> Harness<'static, TextureCraftApp> { + let session = texturecraft_engine::sample_session(256, 64).unwrap(); + let mut harness = Harness::builder().with_size(egui::vec2(1600.0, 960.0)).build_eframe(|cc| TextureCraftApp::new(cc, session)); + harness.run_steps(4); + harness +} + +/// Type `text` into the search field of the Assets panel shown left of the 3D view +fn search_shown_panel(harness: &mut Harness<'_, TextureCraftApp>, text: &str) { + let left_of_view = harness.state().viewport_rect.min.x; + harness.get_all_by_role(Role::TextInput).find(|field| field.rect().max.x < left_of_view).expect("the shown Assets panel has a search field").click(); + harness.run_steps(2); + harness.event(egui::Event::Text(text.into())); + harness.run_steps(2); +} + +#[test] +fn the_folder_button_opens_a_second_assets_panel_that_searches_on_its_own() { + let mut harness = sample_app(); + assert!(!harness.state().ui.dock.is_open(Panel::AssetsSecond), "one Assets panel at first"); + + harness.get_by_label("New Assets panel").click(); + harness.run_steps(3); + assert!(harness.state().ui.dock.is_visible(Panel::AssetsSecond), "the folder button shows a second Assets panel"); + + search_shown_panel(&mut harness, "brick"); + let ui = &harness.state().ui; + assert_eq!((ui.shelf_second.filter.as_str(), ui.shelf_filter.as_str()), ("brick", ""), "the second panel's search leaves the first panel's alone"); + + harness.state_mut().ui.dock.focus(Panel::Assets); + harness.run_steps(3); + search_shown_panel(&mut harness, "rust"); + let ui = &harness.state().ui; + assert_eq!((ui.shelf_second.filter.as_str(), ui.shelf_filter.as_str()), ("brick", "rust"), "the first panel's search leaves the second panel's alone"); +}