feat: Added font picker to the settings (#1570)

This commit is contained in:
Nik
2024-02-24 22:46:52 +01:00
committed by GitHub
parent 8bf7aa9ceb
commit ec69849749
10 changed files with 179 additions and 21 deletions

View File

@@ -413,16 +413,18 @@ namespace hex {
bool FilePicker::draw(const std::string &name) {
bool changed = false;
if (ImGui::InputText("##font_path", m_value)) {
auto pathString = wolv::util::toUTF8String(m_path);
if (ImGui::InputText("##font_path", pathString)) {
changed = true;
}
ImGui::SameLine();
if (ImGuiExt::IconButton("...", ImGui::GetStyleColorVec4(ImGuiCol_Text))) {
return fs::openFileBrowser(fs::DialogMode::Open, { { "TTF Font", "ttf" }, { "OTF Font", "otf" } },
changed = fs::openFileBrowser(fs::DialogMode::Open, { { "TTF Font", "ttf" }, { "OTF Font", "otf" } },
[&](const std::fs::path &path) {
m_value = wolv::util::toUTF8String(path);
pathString = wolv::util::toUTF8String(path);
});
}
@@ -430,19 +432,23 @@ namespace hex {
ImGuiExt::TextFormatted("{}", name);
if (changed) {
m_path = pathString;
}
return changed;
}
void FilePicker::load(const nlohmann::json &data) {
if (data.is_string()) {
m_value = data.get<std::string>();
m_path = data.get<std::fs::path>();
} else {
log::warn("Invalid data type loaded from settings for file picker!");
}
}
nlohmann::json FilePicker::store() {
return m_value;
return m_path;
}
bool Label::draw(const std::string& name) {

View File

@@ -677,14 +677,23 @@ namespace hex {
return value;
}
static std::optional<std::fs::path> fileToOpen;
static std::optional<std::fs::path> s_fileToOpen;
extern "C" void openFile(const char *path) {
log::info("Opening file: {0}", path);
fileToOpen = path;
s_fileToOpen = path;
}
std::optional<std::fs::path> getInitialFilePath() {
return fileToOpen;
return s_fileToOpen;
}
static std::map<std::fs::path, std::string> s_fonts;
extern "C" void registerFont(const char *fontName, const char *fontPath) {
s_fonts[fontPath] = fontName;
}
const std::map<std::fs::path, std::string>& getFonts() {
return s_fonts;
}
namespace {

View File

@@ -3,8 +3,9 @@
#include <CoreFoundation/CFBundle.h>
#include <ApplicationServices/ApplicationServices.h>
#include <Foundation/NSUserDefaults.h>
#include <Foundation/Foundation.h>
#include <AppKit/NSScreen.h>
#include <CoreFoundation/CoreFoundation.h>
#include <CoreText/CoreText.h>
#include <string.h>
#include <stdlib.h>
@@ -23,6 +24,7 @@
}
void openFile(const char *path);
void registerFont(const char *fontName, const char *fontPath);
void openWebpageMacos(const char *url) {
CFURLRef urlRef = CFURLCreateWithBytes(NULL, (uint8_t*)(url), strlen(url), kCFStringEncodingASCII, NULL);
@@ -64,6 +66,28 @@
return (cocoaWindow.styleMask & NSWindowStyleMaskFullScreen) == NSWindowStyleMaskFullScreen;
}
void enumerateFontsMacos(void) {
CFArrayRef fontDescriptors = CTFontManagerCopyAvailableFontFamilyNames();
CFIndex count = CFArrayGetCount(fontDescriptors);
for (CFIndex i = 0; i < count; i++) {
CFStringRef fontName = (CFStringRef)CFArrayGetValueAtIndex(fontDescriptors, i);
// Get font path
CFDictionaryRef attributes = (__bridge CFDictionaryRef)@{ (__bridge NSString *)kCTFontNameAttribute : (__bridge NSString *)fontName };
CTFontDescriptorRef descriptor = CTFontDescriptorCreateWithAttributes(attributes);
CFURLRef fontURL = CTFontDescriptorCopyAttribute(descriptor, kCTFontURLAttribute);
CFStringRef fontPath = CFURLCopyFileSystemPath(fontURL, kCFURLPOSIXPathStyle);
registerFont([(__bridge NSString *)fontName UTF8String], [(__bridge NSString *)fontPath UTF8String]);
CFRelease(descriptor);
CFRelease(fontURL);
}
CFRelease(fontDescriptors);
}
@interface HexDocument : NSDocument
@end