Skip to content

Commit

Permalink
minor tweaks; build-time version numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
wkjarosz committed Dec 10, 2024
1 parent 2ad89ac commit 12b43fa
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 40 deletions.
20 changes: 9 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
cmake_minimum_required(VERSION 3.13)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/" "${CMAKE_SOURCE_DIR}/resources")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")

include(VersionFromGit)

string(TIMESTAMP BUILD_TIME "%Y-%m-%d %H:%M")
message(STATUS "Saving build timestamp: ${BUILD_TIME}")

version_from_git(LOG ON TIMESTAMP "%Y-%m-%d-%H:%M:%S")

project(
Expand All @@ -17,13 +14,12 @@ project(
LANGUAGES C CXX
)

if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(VERSION_LONG "${GIT_DESCRIBE} (64 bit)")
elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
set(VERSION_LONG "${GIT_DESCRIBE} (32 bit)")
endif()

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/version.cpp.in ${CMAKE_CURRENT_BINARY_DIR}/src/version.cpp @ONLY)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/src/version.cpp ${CMAKE_CURRENT_BINARY_DIR}/src/_version.cpp
COMMAND ${CMAKE_COMMAND} -D SRC_DIR=${CMAKE_CURRENT_SOURCE_DIR}/src -D BIN_DIR=${CMAKE_CURRENT_BINARY_DIR}/src -D
CMAKE_SIZEOF_VOID_P=${CMAKE_SIZEOF_VOID_P} -P ${CMAKE_SOURCE_DIR}/cmake/generate_version.cmake
COMMENT "Generating git version file"
)

include(sanitizers)

Expand Down Expand Up @@ -417,6 +413,8 @@ imgui_bundle_add_app(
)

set_target_properties(HDRView PROPERTIES OUTPUT_NAME ${output_name} CXX_STANDARD 17)
# add_dependencies(HDRView gen_version_h_target) target_sources(HDRView PRIVATE
# ${CMAKE_CURRENT_BINARY_DIR}/src/version.cpp)
if(APPLE)
target_compile_options(HDRView PRIVATE "-fobjc-arc")
target_link_libraries(HDRView PRIVATE "-framework ApplicationServices")
Expand Down
139 changes: 139 additions & 0 deletions cmake/generate_version.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
# Generates version.cpp with git information at during build (and not just configure)
#
# Based on:
#
# https://mind.be/compile-time-git-version-info-using-cmake/
#
# and
#
# https://www.mattkeeter.com/blog/2018-01-06-versioning/
#

# Find Git or bail out
find_package(Git)
if(NOT GIT_FOUND)
message(FATAL_ERROR "[VersionFromGit] Git not found")
endif(NOT GIT_FOUND)

# Git describe
execute_process(
COMMAND "${GIT_EXECUTABLE}" describe --tags --dirty
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
RESULT_VARIABLE git_result
OUTPUT_VARIABLE git_describe
ERROR_VARIABLE git_error
OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_STRIP_TRAILING_WHITESPACE
)
if(NOT git_result EQUAL 0)
message(WARNING "[VersionFromGit] Failed to execute Git: ${git_error}")
set(git_describe "")
endif()

# Get Git tag
execute_process(
COMMAND "${GIT_EXECUTABLE}" describe --tags --abbrev=0
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
RESULT_VARIABLE git_result
OUTPUT_VARIABLE git_tag
ERROR_VARIABLE git_error
OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_STRIP_TRAILING_WHITESPACE
)
if(NOT git_result EQUAL 0)
message(WARNING "[VersionFromGit] Failed to execute Git: ${git_error}")
set(git_tag "")
endif()

if(git_tag MATCHES "^v(0|[1-9][0-9]*)[.](0|[1-9][0-9]*)[.](0|[1-9][0-9]*)(-[.0-9A-Za-z-]+)?([+][.0-9A-Za-z-]+)?$")
set(version_major "${CMAKE_MATCH_1}")
set(version_minor "${CMAKE_MATCH_2}")
set(version_patch "${CMAKE_MATCH_3}")
set(identifiers "${CMAKE_MATCH_4}")
set(metadata "${CMAKE_MATCH_5}")
else()
message(WARNING "[VersionFromGit] Git tag isn't valid semantic version: [${git_tag}]")
set(version_major "0")
set(version_minor "0")
set(version_patch "0")
set(identifiers "")
set(metadata "")
endif()

if("${git_tag}" STREQUAL "${git_describe}")
set(git_at_a_tag ON)
endif()

if(NOT git_at_a_tag)
# Extract the Git hash (if one exists)
string(REGEX MATCH "g[0-9a-f]+$" git_hash "${git_describe}")
endif()

# Construct the version variables
set(version ${version_major}.${version_minor}.${version_patch})
set(semver ${version})

# Identifiers
if(identifiers MATCHES ".+")
string(SUBSTRING "${identifiers}" 1 -1 identifiers)
set(semver "${semver}-${identifiers}")
endif()

# Metadata TODO Split and join (add Git hash inbetween)
if(metadata MATCHES ".+")
string(SUBSTRING "${metadata}" 1 -1 metadata)
# Split
string(REPLACE "." ";" metadata "${metadata}")
endif()

if(NOT git_at_a_tag)

list(APPEND metadata "${git_hash}")

# Timestamp
if(DEFINED ARG_TIMESTAMP)
string(
TIMESTAMP
timestamp "${ARG_TIMESTAMP}"
UTC
)
list(APPEND metadata "${timestamp}")
endif(DEFINED ARG_TIMESTAMP)

endif()

# Join
string(REPLACE ";" "." metadata "${metadata}")

if(metadata MATCHES ".+")
set(semver "${semver}+${metadata}")
endif()

# Log the results
message(
STATUS
"Version: ${version}
Git tag: [${git_tag}]
Git hash: [${git_hash}]
Decorated: [${git_describe}]
Identifiers: [${identifiers}]
Metadata: [${metadata}]
SemVer: [${semver}]"
)

# Set variables
set(GIT_TAG ${git_tag})
set(GIT_HASH ${git_hash})
set(GIT_DESCRIBE ${git_describe})
set(SEMVER ${semver})
set(VERSION ${version})
set(VERSION_MAJOR ${version_major})
set(VERSION_MINOR ${version_minor})
set(VERSION_PATCH ${version_patch})

string(TIMESTAMP BUILD_TIME "%Y-%m-%d %H:%M")
message(STATUS "Saving build timestamp: ${BUILD_TIME}")

# Multiply CMAKE_SIZEOF_VOID_P by 8 to get the bitness
math(EXPR BITNESS "${CMAKE_SIZEOF_VOID_P} * 8")
set(VERSION_LONG "${GIT_DESCRIBE} (${BITNESS} bit)")

configure_file("${SRC_DIR}/version.cpp.in" "${BIN_DIR}/version.cpp" @ONLY)
63 changes: 35 additions & 28 deletions src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,17 @@ void IconButton(const Action &a)
ImGui::WrappedTooltip(fmt::format("{}", a.name, ImGui::GetKeyChordNameTranslated(a.chord)).c_str());
}

void Checkbox(const Action &a)
{
ImGui::Checkbox(a.name.c_str(), a.p_selected);
if (!a.tooltip.empty() || a.chord)
{
string parenthesized_chord = a.chord ? fmt::format("({})", ImGui::GetKeyChordNameTranslated(a.chord)) : "";
string tooltip = fmt::format("{}{}", a.tooltip, parenthesized_chord);
ImGui::WrappedTooltip(tooltip.c_str());
}
}

static HDRViewApp *g_hdrview = nullptr;

void init_hdrview(float exposure, float gamma, bool dither, bool sRGB, bool force_sdr, const vector<string> &in_files)
Expand Down Expand Up @@ -295,7 +306,8 @@ HDRViewApp::HDRViewApp(float exposure, float gamma, bool dither, bool sRGB, bool
add_action({"Show pixel values", g_blank_icon, ImGuiMod_Ctrl | ImGuiKey_P, 0, []() {}, []() { return true; },
false, &m_draw_pixel_info});

add_action({"sRGB", g_blank_icon, 0, 0, []() {}, []() { return true; }, false, &m_sRGB});
add_action({"sRGB", g_blank_icon, 0, 0, []() {}, []() { return true; }, false, &m_sRGB,
"Use the sRGB non-linear response curve (instead of gamma correction)"});
add_action({"Decrease gamma", g_blank_icon, ImGuiKey_G, ImGuiInputFlags_Repeat, [this]()
{ m_gamma_live = m_gamma = std::max(0.02f, m_gamma - 0.02f); }, [this]() { return !m_sRGB; }});
add_action({"Increase gamma", g_blank_icon, ImGuiMod_Shift | ImGuiKey_G, ImGuiInputFlags_Repeat, [this]()
Expand Down Expand Up @@ -776,7 +788,8 @@ void HDRViewApp::draw_menus()
ImGui::Separator();

MenuItem(m_actions["Reset tonemapping"]);
MenuItem(m_actions["Clamp to LDR"]);
if (m_params.rendererBackendOptions.requestFloatBuffer)
MenuItem(m_actions["Clamp to LDR"]);

ImGui::EndMenu();
}
Expand Down Expand Up @@ -1203,8 +1216,7 @@ void HDRViewApp::draw_file_window()
ImGui::SameLine();

auto image_num_str = fmt::format("{}", visible_img_number);
ImGui::AlignCursor(image_num_str, 1.0f);
ImGui::TextUnformatted(image_num_str.c_str());
ImGui::TextAligned(image_num_str, 1.0f);

ImGui::TableNextColumn();
ImGui::TextUnformatted(is_current ? ICON_FA_EYE : (is_reference ? ICON_FA_EYE_LOW_VISION : ""));
Expand All @@ -1220,8 +1232,7 @@ void HDRViewApp::draw_file_window()
filename = filename.substr(1);
ellipsis = "...";
}
ImGui::AlignCursor(ellipsis + filename, 1.f);
ImGui::TextUnformatted(ellipsis + filename);
ImGui::TextAligned(ellipsis + filename, 1.f);

if (show_channels && img->groups.size() > 1)
{
Expand All @@ -1243,11 +1254,10 @@ void HDRViewApp::draw_file_window()
ImGui::TableNextRow();

ImGui::TableNextColumn();
string hotkey = is_current && layer.groups[g] < 10
? fmt::format(ICON_FA_ANGLE_UP "{}", mod(layer.groups[g] + 1, 10))
: "";
ImGui::AlignCursor(hotkey, 1.0f);
ImGui::TextUnformatted(hotkey);
string shortcut = is_current && layer.groups[g] < 10
? fmt::format(ICON_FA_ANGLE_UP "{}", mod(layer.groups[g] + 1, 10))
: "";
ImGui::TextAligned(shortcut, 1.0f);

ImGui::TableNextColumn();
if (ImGui::Selectable(
Expand Down Expand Up @@ -1622,8 +1632,7 @@ void HDRViewApp::draw_top_toolbar()
IconButton(m_actions["Reset tonemapping"]);
ImGui::SameLine();

ImGui::Checkbox("sRGB", &m_sRGB);
ImGui::WrappedTooltip("Use the sRGB non-linear response curve (instead of gamma correction).");
Checkbox(m_actions["sRGB"]);
ImGui::SameLine();

ImGui::BeginDisabled(m_sRGB);
Expand All @@ -1642,24 +1651,25 @@ void HDRViewApp::draw_top_toolbar()

if (m_params.rendererBackendOptions.requestFloatBuffer)
{
ImGui::Checkbox("Clamp to LDR", &m_clamp_to_LDR);
Checkbox(m_actions["Clamp to LDR"]);
ImGui::SameLine();
}

ImGui::Checkbox("Grid", &m_draw_grid);
Checkbox(m_actions["Show pixel grid"]);
ImGui::SameLine();

ImGui::Checkbox("Pixel values", &m_draw_pixel_info);
Checkbox(m_actions["Show pixel values"]);
ImGui::SameLine();
}

void HDRViewApp::draw_background()
{
auto &io = ImGui::GetIO();
process_hotkeys();
process_shortcuts();

try
{
auto &io = ImGui::GetIO();

//
// calculate the viewport sizes
// fbsize is the size of the window in physical pixels while accounting for dpi factor on retina screens.
Expand Down Expand Up @@ -1887,18 +1897,15 @@ void HDRViewApp::draw_command_palette()
string txt;
txt = "Navigate (" ICON_FA_ARROW_UP ICON_FA_ARROW_DOWN ")";
ImGui::TableNextColumn();
ImGui::AlignCursor(txt, 0.f);
ImGui::TextUnformatted(txt);
ImGui::TextAligned(txt, 0.f);

ImGui::TableNextColumn();
txt = "Use (return)";
ImGui::AlignCursor(txt, 0.5f);
ImGui::TextUnformatted(txt);
ImGui::TextAligned(txt, 0.5f);

ImGui::TableNextColumn();
txt = "Dismiss (esc)";
ImGui::AlignCursor(txt, 1.f);
ImGui::TextUnformatted(txt);
ImGui::TextAligned(txt, 1.f);

// ImGui::PopStyleColor();

Expand All @@ -1910,22 +1917,22 @@ void HDRViewApp::draw_command_palette()
}
}

void HDRViewApp::process_hotkeys()
void HDRViewApp::process_shortcuts()
{
if (ImGui::GetIO().WantCaptureKeyboard)
{
spdlog::trace("Not processing hotkeys because ImGui wants to capture the keyboard (frame: {})",
spdlog::trace("Not processing shortcuts because ImGui wants to capture the keyboard (frame: {})",
ImGui::GetFrameCount());
return;
}

spdlog::trace("Processing hotkeys (frame: {})", ImGui::GetFrameCount());
spdlog::trace("Processing shortcuts (frame: {})", ImGui::GetFrameCount());

for (auto &a : m_actions)
if (a.second.chord)
if (a.second.enabled() && ImGui::GlobalShortcut(a.second.chord, a.second.flags))
{
spdlog::trace("Processing hotkey for action '{}' (frame: {})", a.first, ImGui::GetFrameCount());
spdlog::trace("Processing shortcut for action '{}' (frame: {})", a.first, ImGui::GetFrameCount());
a.second.callback();
if (a.second.p_selected)
*a.second.p_selected = !*a.second.p_selected;
Expand Down
3 changes: 2 additions & 1 deletion src/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ struct Action
function<bool()> enabled = []() { return true; };
bool needs_menu = false;
bool *p_selected = nullptr;
string tooltip = "";
};

class HDRViewApp
Expand Down Expand Up @@ -189,7 +190,7 @@ class HDRViewApp
void draw_top_toolbar();
void draw_menus();
void draw_status_bar();
void process_hotkeys();
void process_shortcuts();
bool process_event(void *event);
void set_image_textures();
void draw_command_palette();
Expand Down

0 comments on commit 12b43fa

Please sign in to comment.