Skip to content

Minimal UI Template XMake with Assets#

Example's page

This page describes an example listed in ui category.

Minimal UI boilerplate template XMake with AUI Assets.

Source Code#

Project Structure#

graph TD
    A[project_template_ui_assets_xmake] --> B[xmake.lua]
    A --> C[src/]
    C --> D[MainWindow.h]
    C --> E[MainWindow.cpp]
    C --> F[main.cpp]
    A --> G[.vscode/]
    G --> H[c_cpp_properties.json]
    A --> I[xmake/]
    I --> J[aui.lua]
    A --> K[assets/img/]
    K --> L[logo.svg]

xmake.lua#

-- Specify available build configurations
add_rules("mode.release", "mode.debug")

-- Specify compile commands output directory and LSP to analyze C++ code files and highlight IntelliSense
add_rules("plugin.compile_commands.autoupdate", {outputdir = ".vscode", lsp = "clangd"})

-- Specify C++ standard to use, as AUI uses C++20 by default
set_languages("c++20")

-- Download aui package to use for targets later
add_requires("aui")

-- Define our target executable to build
target("aui-assets-minimal-example")
    -- Add source code and headers to target
    add_files("src/*.cpp")
    add_includedirs("src")
    -- Add AUI package to target while linking only required components
    add_packages("aui", {components = {"core", "image", "views", "xml"}})
    -- Resolve linking by grouping AUI components into link groups
    add_linkgroups("aui.views", "aui.xml", "aui.image", "aui.core", {whole = true})
    -- Pack assets before building the target
    on_prepare(function(target)
        import("xmake.aui", {alias = "aui"})
        aui.assets(target)
    end)

xmake/aui.lua#

import("lib.detect.find_program")
import("core.project.config")

function _get_gen_path()
  return path.join(path.absolute(config.builddir()), ".gens", "aui")
end

function _get_aui_package(target)
  -- Get the aui package from target
  for _, pkg in pairs(target:pkgs()) do
    if pkg:name() == "aui" then
      return pkg
    end
  end
  return nil
end

function _pack_asset(assets_dir, asset_path, aui_installdir)
  local toolbox_path = path.join(aui_installdir, "bin", "aui.toolbox")

  if not os.isfile(toolbox_path) then
    toolbox_path = path.join(aui_installdir, "bin", "aui.toolbox.exe")
  end

  if not os.isfile(toolbox_path) then
    toolbox_path = find_program("aui.toolbox")
  end

  wprint("Using toolbox: " .. toolbox_path)
  os.execv(toolbox_path, {
    "pack",
    assets_dir,
    asset_path,
    path.join(_get_gen_path(), hash.sha256(asset_path) .. ".cpp")
  })
end

function assets(target)
  -- Get aui package install directory
  local aui_pkg = _get_aui_package(target)
  if not aui_pkg then
    raise("aui package not found in target")
  end

  local aui_installdir = aui_pkg:installdir()
  wprint("AUI package install directory: " .. aui_installdir)
  local assets_dir = path.join(path.absolute(target:scriptdir()), "assets")
  for _, file in ipairs(os.files(path.join(assets_dir, "**"))) do
    _pack_asset(assets_dir, file, aui_installdir)
  end

  -- Add generated files to the target
  target:add("files", path.join(_get_gen_path(), "**.cpp"))
end

src/MainWindow.h#

1
2
3
4
5
6
7
8
#pragma once

#include <AUI/Platform/AWindow.h>

class MainWindow: public AWindow {
public:
    MainWindow();
};

src/MainWindow.cpp#

#include "MainWindow.h"
#include <AUI/Util/UIBuildingHelpers.h>
#include <AUI/View/ALabel.h>
#include <AUI/View/AButton.h>
#include <AUI/Platform/APlatform.h>
#include <AUI/View/ADrawableView.h>

using namespace declarative;

MainWindow::MainWindow(): AWindow("Project template app", 300_dp, 200_dp) {
    setContents(
        Centered{
            Vertical{
                Centered { Icon { ":img/logo.svg" } with_style { FixedSize(64_dp) } },
                Centered { Label { "Hello world from AUI!" } },
                _new<AButton>("Visit GitHub repo").connect(&AView::clicked, this, [] {
                    APlatform::openUrl("https://github.com/aui-framework/aui");
                }),
                _new<AButton>("Visit docs").connect(&AView::clicked, this, [] {
                    APlatform::openUrl("https://aui-framework.github.io/");
                }),
                _new<AButton>("Submit an issue").connect(&AView::clicked, this, [] {
                    APlatform::openUrl("https://github.com/aui-framework/aui/issues/new");
                }),
            }
        }
    );
}

src/main.cpp#

1
2
3
4
5
6
7
#include <AUI/Platform/Entry.h>
#include "MainWindow.h"

AUI_ENTRY {
    _new<MainWindow>()->show();
    return 0;
};

.vscode/c_cpp_properties.json#

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.26100.0",
            "compilerPath": "cl.exe",
            "cStandard": "c17",
            "cppStandard": "c++20",
            "intelliSenseMode": "windows-msvc-x64"
        }
    ],
    "version": 4
}

Source Code#

Repository

src/MainWindow.cpp#

#include <AUI/Util/UIBuildingHelpers.h>
#include <AUI/View/ALabel.h>
#include <AUI/View/AButton.h>
#include <AUI/Platform/APlatform.h>
#include <AUI/View/ADrawableView.h>

using namespace declarative;

MainWindow::MainWindow(): AWindow("Project template app", 300_dp, 200_dp) {
    setContents(
        Centered{
            Vertical{
                Centered { Icon { ":img/logo.svg" } with_style { FixedSize(64_dp) } },
                Centered { Label { "Hello world from AUI!" } },
                _new<AButton>("Visit GitHub repo").connect(&AView::clicked, this, [] {
                    APlatform::openUrl("https://github.com/aui-framework/aui");
                }),
                _new<AButton>("Visit docs").connect(&AView::clicked, this, [] {
                    APlatform::openUrl("https://aui-framework.github.io/");
                }),
                _new<AButton>("Submit an issue").connect(&AView::clicked, this, [] {
                    APlatform::openUrl("https://github.com/aui-framework/aui/issues/new");
                }),
            }
        }
    );
}

src/MainWindow.h#

1
2
3
4
5
6
#include <AUI/Platform/AWindow.h>

class MainWindow: public AWindow {
public:
    MainWindow();
};

src/main.cpp#

1
2
3
4
5
6
#include "MainWindow.h"

AUI_ENTRY {
    _new<MainWindow>()->show();
    return 0;
};