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 "../char_maps/char_maps.hpp" // XXX #616
14#include "../win32/win32.hpp"
15#include "../macros.hpp"
16#include <filesystem>
17#include <string>
18#include <coroutine>
19
20hi_export_module(hikogui.path.path_location : impl);
21
22hi_export namespace hi {
23inline namespace v1 {
24
25[[nodiscard]] inline std::expected<std::filesystem::path, std::error_code> executable_file() noexcept
26{
27 static auto r = []() -> std::expected<std::filesystem::path, std::error_code> {
28 if (auto path = win32_GetModuleFileName()) {
29 return *path;
30 } else {
31 return std::unexpected{std::error_code{path.error()}};
32 }
33 }();
34
35 return r;
36}
37
38[[nodiscard]] inline std::expected<std::filesystem::path, std::error_code> data_dir() noexcept
39{
40 static auto r = []() -> std::expected<std::filesystem::path, std::error_code> {
41 // "%LOCALAPPDATA%<Application Vendor><Application Name>\"
42 // FOLDERID_LocalAppData has the default path: %LOCALAPPDATA% (%USERPROFILE%\AppData\Local)
43 if (auto path = win32_SHGetKnownFolderPath(FOLDERID_LocalAppData)) {
44 return *path / get_application_vendor() / get_application_name() / "";
45 } else {
46 return std::unexpected{std::error_code{path.error()}};
47 }
48 }();
49
50 return r;
51}
52
53[[nodiscard]] inline std::expected<std::filesystem::path, std::error_code> log_dir() noexcept
54{
55 // "%LOCALAPPDATA%<Application Vendor><Application Name>\Log\"
56 if (auto path = data_dir()) {
57 return *path / "Log" / "";
58 } else {
59 return std::unexpected{path.error()};
60 }
61}
62
63[[nodiscard]] inline generator<std::filesystem::path> resource_dirs() noexcept
64{
65 // Always look at the resource directory where the executable is located.
66 if (auto path = executable_dir()) {
67 co_yield *path / "resources" / "";
68 }
69
70 // Also look in the data directory of the application.
71 if (auto path = data_dir()) {
72 co_yield *path / "resources" / "";
73 }
74
75 // If the executable of the application is located in the build directory,
76 // then check the source directories for resources.
77 if (auto source_dir_ = source_dir()) {
78 co_yield *source_dir_ / "resources" / "";
79
80 // Check the in-tree HikoGUI-library build directory.
81 if (auto path = library_cmake_build_dir(); not path.empty()) {
82 co_yield path / "resources" / "";
83 }
84
85 // Check the in-tree HikoGUI-library source directory.
86 if (auto path = library_cmake_source_dir(); not path.empty()) {
87 co_yield path / "resources" / "";
88 }
89
90 // Check the HikoGUI source directory.
91 co_yield library_source_dir() / "resources" / "";
92
93 // Check the HikoGUI install directory.
94 co_yield library_source_dir() / "share" / "hikogui" / "resources" / "";
95 }
96}
97
98[[nodiscard]] inline generator<std::filesystem::path> system_font_dirs() noexcept
99{
100 if (auto path = win32_SHGetKnownFolderPath(FOLDERID_Fonts)) {
101 co_yield *path;
102 }
103}
104
105[[nodiscard]] inline generator<std::filesystem::path> font_dirs() noexcept
106{
107 for (auto const& path : resource_dirs()) {
108 co_yield path;
109 }
110 for (auto const& path : system_font_dirs()) {
111 co_yield path;
112 }
113}
114
115[[nodiscard]] inline generator<std::filesystem::path> theme_dirs() noexcept
116{
117 for (auto const& path : resource_dirs()) {
118 co_yield path;
119 }
120}
121
122} // namespace v1
123} // namespace hi::inline v1
Rules for working with win32 headers.
std::expected< std::filesystem::path, std::error_code > log_dir() noexcept
Get the full path to the directory where the application should store its log files.
Definition path_location_win32_impl.hpp:53
generator< std::filesystem::path > resource_dirs() noexcept
The directories to search for resource files.
Definition path_location_win32_impl.hpp:63
std::expected< std::filesystem::path, std::error_code > executable_dir() noexcept
Get the full path to the directory when this executable is located.
Definition path_location_intf.hpp:174
std::expected< std::filesystem::path, std::error_code > executable_file() noexcept
Get the full path to this executable.
Definition path_location_win32_impl.hpp:25
generator< std::filesystem::path > system_font_dirs() noexcept
The directories to search for system font files.
Definition path_location_win32_impl.hpp:98
std::optional< std::filesystem::path > source_dir() noexcept
Get the full path to source code of this executable.
Definition path_location_intf.hpp:219
std::expected< std::filesystem::path, std::error_code > data_dir() noexcept
Get the full path to the directory where the application should store its data.
Definition path_location_win32_impl.hpp:38
The HikoGUI namespace.
Definition array_generic.hpp:20
std::expected< std::filesystem::path, hresult_error > win32_SHGetKnownFolderPath(KNOWNFOLDERID const &folder_id) noexcept
Convenience function for SHGetKnownFolderPath().
Definition shlobj_core.hpp:26
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
T unexpected(T... args)