HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
path_location_win32_impl.hpp
1// Copyright Take Vos 2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
8
9#include "path_location_intf.hpp"
10#include "../metadata/metadata.hpp"
11#include "../telemetry/telemetry.hpp"
12#include "../utility/utility.hpp"
13#include "../macros.hpp"
14#include <filesystem>
15#include <string>
16
17hi_export_module(hikogui.path.path_location : impl);
18
19namespace hi::inline v1 {
20
28hi_export [[nodiscard]] inline std::filesystem::path get_path_by_id(const KNOWNFOLDERID& folder_id) noexcept
29{
30 PWSTR wpath = nullptr;
31 if (SHGetKnownFolderPath(folder_id, 0, nullptr, &wpath) != S_OK) {
32 hi_log_fatal("Could not get known folder path.");
33 }
34 hilet d = defer{[&] {
36 }};
37
38 return std::filesystem::path{wpath} / "";
39}
40
41hi_export [[nodiscard]] inline std::filesystem::path get_module_path(HMODULE module_handle) noexcept
42{
44 auto buffer_size = MAX_PATH; // initial default value = 256
45
46 // iterative buffer resizing to max value of 32768 (256*2^7)
47 for (std::size_t i = 0; i < 7; ++i) {
50 if (chars < module_path.length()) {
51 module_path.resize(chars);
52 return std::filesystem::path{module_path};
53
54 } else {
55 buffer_size *= 2;
56 }
57 }
58 hi_no_default("Could not get module path. It exceeds the buffer length of 32768 chars.");
59}
60
61hi_export [[nodiscard]] inline std::filesystem::path executable_file() noexcept
62{
63 return get_module_path(nullptr);
64}
65
66hi_export [[nodiscard]] inline std::filesystem::path data_dir() noexcept
67{
68 // "%LOCALAPPDATA%<Application Vendor><Application Name>\"
69 // FOLDERID_LocalAppData has the default path: %LOCALAPPDATA% (%USERPROFILE%\AppData\Local)
71 return local_app_data / get_application_vendor() / get_application_name() / "";
72}
73
74hi_export [[nodiscard]] inline std::filesystem::path log_dir() noexcept
75{
76 // "%LOCALAPPDATA%<Application Vendor><Application Name>\Log\"
77 return data_dir() / "Log" / "";
78}
79
80hi_export [[nodiscard]] inline std::filesystem::path preferences_file() noexcept
81{
82 // "%LOCALAPPDATA%<Application Vendor><Application Name>\preferences.json"
83 return data_dir() / "preferences.json";
84}
85
87{
88 if (auto source_path = source_dir()) {
89 // Fallback when the application is executed from its build directory.
90 co_yield executable_dir() / "resources" / "";
91 co_yield *source_path / "resources" / "";
92
93 if (auto install_path = library_install_dir()) {
94 co_yield *install_path / "resources" / "";
95
96 } else {
97 // Fallback when HikoGUI is also still in its build directory.
98 co_yield library_source_dir() / "resources" / "";
99 co_yield library_build_dir() / "resources" / "";
100 }
101 } else {
102 co_yield executable_dir() / "resources" / "";
103 co_yield data_dir() / "resources" / "";
104 }
105}
106
107hi_export [[nodiscard]] inline generator<std::filesystem::path> system_font_dirs() noexcept
108{
110}
111
112hi_export [[nodiscard]] inline generator<std::filesystem::path> font_dirs() noexcept
113{
114 for (hilet& path : resource_dirs()) {
115 co_yield path;
116 }
117 for (hilet& path : system_font_dirs()) {
118 co_yield path;
119 }
120}
121
122hi_export [[nodiscard]] inline generator<std::filesystem::path> theme_dirs() noexcept
123{
124 for (hilet& path : resource_dirs()) {
125 co_yield path;
126 }
127}
128
129} // namespace hi::inline v1
Rules for working with win32 headers.
DOXYGEN BUG.
Definition algorithm.hpp:16
hi_export std::filesystem::path get_path_by_id(const KNOWNFOLDERID &folder_id) noexcept
Convenience function for SHGetKnownFolderPath().
Definition path_location_win32_impl.hpp:28
hi_export std::filesystem::path executable_file() noexcept
Get the full path to this executable.
hi_export std::filesystem::path data_dir() noexcept
Get the full path to the directory where the application should store its data.
hi_export std::filesystem::path log_dir() noexcept
Get the full path to the directory where the application should store its log files.
std::optional< std::filesystem::path > library_install_dir() noexcept
The full path where HikoGUI is installed during compilation of the application.
Definition path_location_intf.hpp:146
hi_export generator< std::filesystem::path > resource_dirs() noexcept
The directories to search for resource files.
hi_export std::filesystem::path executable_dir() noexcept
Get the full path to the directory when this executable is located.
Definition path_location_intf.hpp:66
hi_export std::filesystem::path preferences_file() noexcept
Get the full path to application preferences file.
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
hi_export std::optional< std::filesystem::path > source_dir() noexcept
Get the full path to source code of this executable.
Definition path_location_intf.hpp:106
T resize(T... args)