Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,4 @@ First public preview. The API is not stable yet.
- Timeline editor, curve fields and editors, and plots: the same tracks, curves or plot can show in several places at once, each with its own zoom, selection and (for a curve field) editor window. `TimelineView`, `CurveView` and `PlotView` are each a data part (`TimelineData`, `CurveData`, `PlotData`) and a view state (`TimelineViewState`, `CurveViewState`, `PlotViewState`) in one; `TimelineEditor`, `CurveField`, `CurveEditor`, `Plot` and the helpers that use a view take one of them or `{ data, state }`. The apply, edit-making and data-only helpers take the data part (`ApplyTimelineEdit`, `ApplyCurveEdit`, `ApplyPlotEdit`, `AddKeyEdit`, `AddCurveKeyEdit`, `MovePlotHandleEdit` and the like); `SaveViewState` and `RestoreViewState` take the view state. An edit raises the data's `revision`, and each view lets go of what's gone the next time it draws (`PruneTimelineSelection`, `PruneCurveSelection`). A timeline editor's rows are scoped to it (`TimelineRowID`). The showcase's Timeline and Curves panels, and each Inspector's curve fields and Levels plot, have views of their own.
- Saved layouts keep two panels of one kind: `ApplyDockLayout`, `RestoreUIState` for a `PersistDocking` layout, and `RestoreDocumentLayout` give each its own place in the tree and its own `saveState` back, matched in tree order. `RestoreDocumentLayout` takes a `makePanel`, as `ApplyDockLayout` does, for the panels a document was saved with that it doesn't have now. Several states of one key are saved as an array. `PanelsInTree` lists a dock tree's panels in order. The showcase makes a document's second Viewport, Timeline and the like again when it starts.
- `Panel::onChanged`: called after a frame in which the user pressed, released or dragged the mouse, scrolled, or typed in the panel, once its handlers have run. Hovering doesn't count. An app with several panels showing the same data rebuilds the others from it (`InvalidatePanel`), so their cached copies show the change. The showcase does it for panels of one kind, and for Viewports and Hierarchies, which both show the scene.
- The library gives back the static IDs it makes. A panel `CreatePanel` made an ID for releases it when it closes: its tab, its window, its document, or a layout applied without it. `ClosePanel` closes a panel wherever it is, `ReleaseDockingLayout` closes a whole layout, and `CloseAllDocuments` closes a document set. `Cleanup` releases the application's own window IDs (menus, tooltips, pickers, toasts, popovers) and closes its torn-off panel windows. The showcase closes its documents at exit and holds no static ID when it's done.
7 changes: 7 additions & 0 deletions docs/controls/tabs-docking-toolbars.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,14 @@ To change the layout from code:

- `FloatPanel(layout, panelID, screenPos)` moves a panel into a window of its own.
- `CloseDockedPanel(layout, leaf, tabIndex)` closes a docked panel.
- `ClosePanel(layout, panelID)` closes a panel wherever it is in the layout.
- `CloseFloatingDockWindow(index)` closes a floating window and its panels.
- `DisableAllPanels(layout)` hides every panel, for example before a theme reload.
- `ReleaseDockingLayout(layout)` closes a whole layout for good: its panels, its torn-off
windows, and its tree. Call it before you destroy a layout you made, or at exit.

A closed panel gives its ID back when `CreatePanel` made it (the overload without an ID).
One you gave `CreatePanel` stays yours to release.

```cpp
(void)FloatPanel(layout, log, int2{ styleManager.Px(200), styleManager.Px(200) });
Expand Down Expand Up @@ -416,6 +422,7 @@ for (const SavedDocumentWindow& window : TakeSavedDocuments("documents"))
Document& CreateDocument(DocumentSet& set, std::string name, std::string extension,
std::string kind, IconID icon);
void CloseDocument(DocumentSet& set, int index);
void CloseAllDocuments(DocumentSet& set); // at exit, say
void Documents(ID id, DocumentSet& set, LayoutOverride&& constraints = {});
void DocumentBar(ID id, DocumentSet& set, StyleRef style, LayoutOverride&& constraints = LayoutOverride::Auto());
void DocumentLine(const DocumentSet& set, LayoutOverride&& constraints = LayoutOverride::Auto());
Expand Down
10 changes: 10 additions & 0 deletions docs/overview/ids.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ idManager.ReleaseStaticID(windowID); // when the thing it names is gone for go

- `IsStaticIDValid(id)` says whether the ID is still current in its slot.
- A released ID stays invalid even if its slot is reused.
- The ones the library makes go back by themselves:
- a panel's, from `CreatePanel` without an ID, when the panel closes;
- a document's, and its dock tree's, when it closes (`CloseDocument`,
`CloseAllDocuments`);
- a layout's tree's and panels', with `ReleaseDockingLayout`;
- the application's own (its menus, tooltips, pickers, toasts and torn-off windows) at
`Cleanup`.
- Some state of yours gets an ID from the control that shows it, the first time it's shown:
`CommandSearchState::listWindowID`, `CurveViewState::popupID`. Release those when the
state goes.

## Ranged IDs

Expand Down
10 changes: 9 additions & 1 deletion examples/showcase/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,14 @@ namespace tachyon
graphicsContext->DestroyImageAtEnd(std::exchange(pastedImage, nullptr), DeviceScope::Application);
}

// Every static ID the showcase made goes back (the Hierarchy panels' with them).
// The documents close, in every window: their panels are hidden and destroyed (the
// Hierarchy panels' row IDs with them), and their IDs and trees' go back.
CloseAllDocuments(showcase.documents);
for (const auto& window : showcase.documentWindows)
CloseAllDocuments(window->documents);
ReleaseDockingLayout(m_capture.layout);

// Every static ID the showcase made goes back.
idManager.ReleaseStaticID(showcase.windowLayoutID1);
idManager.ReleaseStaticID(showcase.windowLayoutID2);
idManager.ReleaseStaticID(showcase.exitDialogID);
Expand Down Expand Up @@ -207,6 +214,7 @@ namespace tachyon
release(m_capture.windowID);
release(m_capture.areaID);
release(m_capture.documentsID);
release(showcase.commandSearch.listWindowID); // the search's results window, made on its first frame
#if TACHYON_D3D12
// The scene renderer's Direct3D 12 objects, once the GPU is done with them (Vulkan's go
// with the backend's own).
Expand Down
3 changes: 2 additions & 1 deletion examples/showcase/capture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -629,9 +629,10 @@ namespace tachyon
showcase.reloadDocument = nullptr;
if (c.panelID != ID::Invalid())
{
// Hidden, as it was shown, by hand; then closed for good, its ID with it.
if (Panel& panel = c.layout.panels[c.panelID]; panel.onDisable)
panel.onDisable();
c.layout.panels.erase(c.panelID);
ClosePanel(c.layout, c.panelID);
c.panelID = ID::Invalid();
}
c.frames = 0;
Expand Down
20 changes: 19 additions & 1 deletion src/gui_controls/controls_docking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -949,20 +949,38 @@ namespace tachyon

// Closing is permanent: the panel and its state are destroyed (a new
// panel can be created later), after its onDisable if it was shown.
ClosePanel(layout, panelID);
}

void GUIApplication::ClosePanel(DockingLayout& layout, ID panelID)
{
if (layout.root)
(void)RemovePanel(layout.root, panelID, &idManager);
auto it = layout.panels.find(panelID);
if (it == layout.panels.end()) return;
if (it->second.internal.enabled && it->second.onDisable) it->second.onDisable();
if (it->second.internal.ownsID) idManager.ReleaseStaticID(panelID);
layout.panels.erase(it);
}

void GUIApplication::ErasePanels(DockingLayout& layout)
{
DisableAllPanels(layout);
for (const auto& [id, panel] : layout.panels)
if (panel.internal.ownsID)
idManager.ReleaseStaticID(id);
layout.panels.clear();
}

void GUIApplication::CloseFloatingDockWindow(size_t index)
{
if (index >= m_floatingDockWindows.size()) return;
auto& fw = *m_floatingDockWindows[index];
// Closing the window closes its panels for good: they're disabled and
// destroyed with the window's layout, and every ID it held is released.
DisableAllPanels(fw.layout);
ErasePanels(fw.layout);
ReleaseDockTreeIDs(fw.layout.root.get(), &idManager);
fw.layout.root.reset();
idManager.ReleaseStaticID(fw.windowID);
m_floatingDockWindows.erase(m_floatingDockWindows.begin() + (ptrdiff_t)index);
}
Expand Down
22 changes: 17 additions & 5 deletions src/gui_controls/controls_documents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ namespace tachyon
std::erase_if(m_persistDocks, [&](const PersistedDock& dock) { return dock.target == &layout; });
}

void GUIApplication::ReleaseDockingLayout(DockingLayout& layout)
{
ForgetDockingLayout(layout);
ErasePanels(layout);
ReleaseDockTreeIDs(layout.root.get(), &idManager);
layout.root.reset();
}

void GUIApplication::CloseDocument(DocumentSet& set, int index)
{
if (index < 0 || index >= int(set.documents.size()))
Expand All @@ -157,11 +165,9 @@ namespace tachyon
if (m_documentMove && m_documentMove->document == &document)
m_documentMove.reset();

// Its torn-off panels go with it; they would dock back into a layout that's gone.
ForgetDockingLayout(document.layout);

DisableAllPanels(document.layout);
ReleaseDockTreeIDs(document.layout.root.get(), &idManager);
// Its panels, and its torn-off ones, go with it; they would dock back into a layout
// that's gone.
ReleaseDockingLayout(document.layout);
idManager.ReleaseStaticID(document.internal.dockID);
if (document.id.GetMode() == IDMode::Static)
idManager.ReleaseStaticID(document.id);
Expand All @@ -173,6 +179,12 @@ namespace tachyon
set.active = std::clamp(set.active, 0, std::max(0, int(set.documents.size()) - 1));
}

void GUIApplication::CloseAllDocuments(DocumentSet& set)
{
while (!set.documents.empty())
CloseDocument(set, int(set.documents.size()) - 1);
}

void GUIApplication::Documents(ID id, DocumentSet& set, LayoutOverride&& constraints)
{
RegisterElement(tachyon::Element{ .id = id, .styleRef = theme.documentsStyleRef, .state = guiStateManager.GetCurrentState(id) },
Expand Down
34 changes: 34 additions & 0 deletions src/gui_controls/gui_application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,42 @@ namespace tachyon
GUIApplication::Cleanup();
}

void GUIApplication::ReleaseOwnIDs()
{
// Torn-off windows: their panels are hidden and destroyed with them, and their IDs and
// trees' go back.
while (!m_floatingDockWindows.empty())
CloseFloatingDockWindow(m_floatingDockWindows.size() - 1);

auto release = [this](ID& id) {
if (id != ID::Invalid() && id.GetMode() == IDMode::Static)
idManager.ReleaseStaticID(id);
id = ID::Invalid();
};
release(tooltipWindowID);
release(contextMenuWindowID);
release(tabDragGhostWindowID);
release(m_colorPickerSub->State().windowID);
release(m_gradientEditorSub->State().windowID);
for (ShownToast& toast : m_toasts)
release(toast.id);
m_toasts.clear();
release(m_toastStackID);
m_tasksPopovers.ForEach([&](ID, TasksPopover& popover) { release(popover.windowID); });
m_tasksPopovers.Clear();
m_autocomplete.ForEach([&](ID, AutocompleteState& state) { release(state.listWindowID); });
m_autocomplete.Clear();
for (RetiredWindow& retired : m_retiredWindows)
release(retired.id);
m_retiredWindows.clear();
menuManager.ReleaseWindowIDs(idManager);
}

void GUIApplication::Cleanup()
{
// The IDs it made for itself go back while what holds them is still here.
if (m_initialized)
ReleaseOwnIDs();
// A theme switch still crossfading holds images of its windows.
if (graphicsContext != nullptr)
EndThemeCrossfade();
Expand Down
19 changes: 18 additions & 1 deletion src/gui_controls/gui_application.h
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,8 @@ namespace tachyon
std::function<void()> onEnable = {},
std::function<void()> onDisable = {});

/// As above, with a new static ID, which it returns.
/// As above, with a new static ID, which it returns. The ID goes back when the panel is
/// closed (its tab, its window, its document, or a layout applied without it).
tachyon::ID CreatePanel(std::unordered_map<tachyon::ID, tachyon::Panel>& panels, std::string title, tachyon::IconID icon,
std::function<void()> onPanelGUI,
std::string panelType = {},
Expand All @@ -326,6 +327,7 @@ namespace tachyon
ID id = idManager.CreateStaticID();
CreatePanel(panels, id, std::move(title), icon, std::move(onPanelGUI),
std::move(panelType), std::move(onEnable), std::move(onDisable));
panels[id].internal.ownsID = true;
return id;
}

Expand Down Expand Up @@ -358,12 +360,20 @@ namespace tachyon
/// Before an app destroys `layout`: closes the windows of the panels torn off it (they
/// would dock back into it) and stops saving it.
void ForgetDockingLayout(DockingLayout& layout);
/// Closes a layout for good, as ForgetDockingLayout does, and empties it: its panels are
/// hidden (onDisable) and destroyed, and the IDs CreatePanel made for them and its
/// tree's go back. Call it before the app destroys a layout it made, or at exit.
void ReleaseDockingLayout(DockingLayout& layout);

/// Closes the window for good: its panels are disabled and destroyed.
void CloseFloatingDockWindow(size_t index);

/// Closes the panel at `tabIndex` of a docked leaf, as the tab's Close action does.
void CloseDockedPanel(DockingLayout& layout, DockNode& leaf, int tabIndex);
/// Closes a panel of `layout` for good, wherever it is: out of its tree (its leaf
/// closing up when it empties), hidden first (onDisable) if it's shown, and destroyed,
/// its ID back when CreatePanel made it.
void ClosePanel(DockingLayout& layout, ID panelID);

/// Adds a document at the end of `set`, with new static IDs and an empty layout, which
/// the caller fills (CreatePanel, MakeLeaf). The first one added becomes the active one.
Expand All @@ -375,6 +385,8 @@ namespace tachyon
/// active; when it's the one closed, the next one (else the one before) takes over.
/// Call from OnGUI, not from an input handler.
void CloseDocument(DocumentSet& set, int index);
/// Closes every document of `set`, as CloseDocument does each: at exit, say.
void CloseAllDocuments(DocumentSet& set);

/// Moves a panel from sourceNode/sourceLayout to targetLayout, fixing up selectedTab.
static void TransferPanel(tachyon::ID panelID, tachyon::DockNode* sourceNode,
Expand Down Expand Up @@ -3366,6 +3378,11 @@ namespace tachyon

/// Internal: called by the docking each frame.
void NotePanelContentID(ID panelId, ID contentID) { m_panelContentIDs[panelId] = contentID; }
/// At Cleanup: gives back the static IDs the application made for itself (its popup,
/// tooltip, menu and picker windows, toasts) and closes its torn-off panel windows.
void ReleaseOwnIDs();
/// Destroys every panel of `layout`, as ClosePanel does each, leaving its tree.
void ErasePanels(DockingLayout& layout);
/// Hands the frame's input to the element handlers, then calls onChanged of the docked
/// panels the user acted in.
void DispatchInput(ID overID);
Expand Down
1 change: 1 addition & 0 deletions src/gui_controls/gui_application_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ namespace tachyon
using GUIApplication::ApplyPendingStyleReload;
using GUIApplication::InvalidateCachedScopesForInput;
using GUIApplication::DispatchInput;
using GUIApplication::ReleaseOwnIDs;
using GUIApplication::CollectPropagatedAncestors;
using GUIApplication::NoteTargetAncestors;
using GUIApplication::NoteContextMenuOwner;
Expand Down
3 changes: 3 additions & 0 deletions src/gui_controls/gui_docking_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ namespace tachyon
{
/// onEnable fired without a matching onDisable.
bool enabled = false;
/// CreatePanel made its ID: it goes back when the panel is closed. One the app gave
/// is the app's to release.
bool ownsID = false;
} internal; ///< not part of the API

/// Scroll position, kept here so a hidden tab keeps it (ScrollManagement prunes
Expand Down
11 changes: 11 additions & 0 deletions src/gui_controls/gui_menu_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <gui_core/gui_id_manager.h>

#include <algorithm>
#include <utility>
#include <assert.h>

namespace tachyon
Expand All @@ -37,6 +38,16 @@ namespace tachyon
}


void MenuManager::ReleaseWindowIDs(IDManager& idManager)
{
for (Level& level : levels)
if (level.windowID != ID::Invalid())
idManager.ReleaseStaticID(std::exchange(level.windowID, ID::Invalid()));
for (const PopupWindow& popup : popupWindows)
idManager.ReleaseStaticID(popup.windowID);
popupWindows.clear();
}

ID MenuManager::PopupWindowFor(ID triggerID, IDManager& idManager)
{
++popupWindowUses;
Expand Down
2 changes: 2 additions & 0 deletions src/gui_controls/gui_menu_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ namespace tachyon
uint64_t popupWindowUses = 0;
/// The popup window ID of `triggerID`'s menu.
ID PopupWindowFor(ID triggerID, IDManager& idManager);
/// Gives back the window IDs its levels and popups were given: at exit.
void ReleaseWindowIDs(IDManager& idManager);

bool buildingMenuBar = false; ///< true while a menu bar is being built
/// Menu-bar triggers in order, registered during construction for Left/Right navigation.
Expand Down
12 changes: 9 additions & 3 deletions src/gui_controls/gui_ui_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,14 @@ namespace tachyon
});
if (!root)
{
// Never shown: only their IDs, when CreatePanel made them, to give back.
for (const ID id : made)
layout.panels.erase(id);
if (const auto it = layout.panels.find(id); it != layout.panels.end())
{
if (it->second.internal.ownsID)
idManager.ReleaseStaticID(id);
layout.panels.erase(it);
}
return false;
}
std::vector<ID> all;
Expand Down Expand Up @@ -312,15 +318,15 @@ namespace tachyon
if (!root)
{
for (const ID id : made)
layout.panels.erase(id);
ClosePanel(layout, id);
return false;
}

DisableAllPanels(layout);
std::vector<ID> all;
for (const auto& [id, panel] : layout.panels) all.push_back(id);
for (const ID id : UnplacedPanels(*root, all))
layout.panels.erase(id);
ClosePanel(layout, id);
ReleaseDockTreeIDs(layout.root.get(), &idManager);
layout.root = std::move(root);
return true;
Expand Down
4 changes: 3 additions & 1 deletion src/gui_controls/gui_widget_state_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ namespace tachyon
int current = 0; ///< highlighted match, moved by Up / Down
bool focusRequested = false; ///< set by FocusCommandSearch, taken next frame
bool ranCommand = false; ///< a command ran since the last CommandSearch call
tachyon::ID listWindowID = tachyon::ID::Invalid(); ///< internal: the results popup
/// Internal: the results popup's window, made the first time it's shown. Yours to
/// release (IDManager::ReleaseStaticID) when the state goes.
tachyon::ID listWindowID = tachyon::ID::Invalid();
};

// ---- Drop position for tree reorder drops ----
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ list(APPEND GUI_CORE_TEST_FILES
controls_reorderable_test.cpp
color_field_test.cpp
controls_docking_test.cpp
static_id_release_test.cpp
controls_documents_test.cpp
node_view_two_views_test.cpp
timeline_two_views_test.cpp
Expand Down
Loading
Loading