Skip to content

Latest commit

 

History

History
145 lines (115 loc) · 4.84 KB

app.md

File metadata and controls

145 lines (115 loc) · 4.84 KB

4. The app

The app class

The app class owns the panels, the window and the dock layout. The dock layout describes how the panels are arranged in the window. At the end of my_app.h:

class MyApp : public tachyon::GUIApplication
{
public:
    tachyon::Result<void> Initialize(const InitializeOptions& options = {}) override;
    void OnGUI() override;
    void Cleanup() override;

    CounterPanel  counter;
    GreetingPanel greeting;

private:
    tachyon::ID            m_windowID = tachyon::ID::Invalid();
    tachyon::ID            m_dockID   = tachyon::ID::Invalid();
    tachyon::docking::DockingLayout m_layout;
    tachyon::docking::Context       m_docking;   // docking's state: the panels' windows, drags
};

Close the namespace with }.

Setting up once

Initialize runs once, after the library has started. In my_app.cpp:

tachyon::Result<void> MyApp::Initialize(const InitializeOptions& options)
{
    if (tachyon::Result<void> result = GUIApplication::Initialize(options); !result)
        return result;
    docking::Install(*this, m_docking);   // docking works at points of the frame OnGUI doesn't reach

    // The window and the dock layout keep their state between frames, so they get
    // IDs that stay the same: static IDs, made once.
    m_windowID = idManager.CreateStaticID();
    m_dockID   = idManager.CreateStaticID();

    const tachyon::IconID noIcon = tachyon::IconID::Invalid;
    const tachyon::ID left  = docking::CreatePanel(*this, m_docking, m_layout.panels, "Counter",  noIcon, [this] { counter.Draw(*this); });
    const tachyon::ID right = docking::CreatePanel(*this, m_docking, m_layout.panels, "Greeting", noIcon, [this] { greeting.Draw(*this); });
    m_layout.root = docking::MakeSplit(docking::DockSplitMode::LeftPinned, 240,
        docking::MakeLeaf({ left }),
        docking::MakeLeaf({ right }));
    return {};
}
  • The window and the dock layout remember their position, size and arrangement by ID.
    • So they need static IDs: IDs made once, which stay the same.
    • Call-site IDs, made from the file and line of a call, won't do here.
  • Docking is a library of its own (TachyonUI::gui_docking, namespace tachyon::docking):
    • its functions take the app, then a docking::Context the app owns (m_docking here)
    • docking::Install hooks it into the frame, once, after GUIApplication::Initialize
    • my_app.cpp shortens its name with namespace docking = tachyon::docking;
  • CreatePanel registers a dockable panel with a title and a draw callback.
  • The dock tree gives the counter a 240 px left column and the greeting the rest.
  • Users can drag tabs to rearrange the panels, or drag a tab out of the window to float its panel.

Cleaning up

Before the library cleans up, docking closes the windows of the panels that were floated:

void MyApp::Cleanup()
{
    docking::Cleanup(*this, m_docking);   // the floated panels' windows, before the library's own
    GUIApplication::Cleanup();
}

Every frame

void MyApp::OnGUI()
{
    if (BeginWindow(m_windowID, "My app", theme.mainWindowStyleRef,
                    tachyon::LayoutOverride::FixedSize(640, 360).Centered()))
    {
        docking::BeginDockingContainer(*this, m_docking, m_dockID, &m_layout, tachyon::LayoutOverride::Auto());
        docking::EndDockingContainer(*this, m_docking);
        docking::EmitFloatingDockWindows(*this, m_docking);
    }
    EndWindow();
}
  • The docking container draws the visible panels through their callbacks.
  • EmitFloatingDockWindows draws the floated ones.

Close the namespace with }.

main

In main.cpp, after #include "my_app.h" and #include <cstdio>:

int main()
{
    my_app::MyApp app;
    tachyon::Result<void> result = app.Initialize();
    if (result)
        result = app.Run();   // until the last window closes
    app.Cleanup();
    if (!result)
    {
        std::fprintf(stderr, "%s\n", result.GetError().message.c_str());
        return 1;
    }
    return 0;
}

On failure, the Result<void> says why. For example, the GPU may be missing required features.

Run it

Build as in step 1, then run it from any folder:

  • build\Debug\my_app.exe on Windows
  • build/Debug/my_app on Linux and macOS

Where next

  • Controls: every control, its options and CSS class.
  • CSS and theming: your own style file, dark and light.
  • Overview: IDs, Begin and End, the frame.
  • Integration: your own Vulkan device, window backend or main loop.

Previous: 3. Fields | Tutorial