Photon 1.0.0
Loading...
Searching...
No Matches
Core.hpp
Go to the documentation of this file.
1/*
2 * =====================================================================
3 *
4 * Photon
5 *
6 * Copyright Amlal EL Mahrouss, all rights reserved.
7 *
8 * =====================================================================
9 */
10
11#pragma once
12
13#include <Config.hpp>
14#include <Macros.hpp>
15#include <cerrno>
16#include <cstdlib>
17
18#ifdef PHOTON_END_OF_BUFFER
19#undef PHOTON_END_OF_BUFFER
20#endif // PHOTON_END_OF_BUFFER
21
22#define PHOTON_END_OF_BUFFER '\0'
23
24#define PHOTON_EMPTY_HTML "<html>\r\n<head>\r\n</head>\r\n<body>\r\n</body>\r\n</html>\r\n"
25
26PHOTON_API void photon_log(const char* str);
27PHOTON_API size_t photon_strlen(const char* str);
31
32namespace Photon
33{
34 namespace FS = std::filesystem;
35
36 using Thread = std::thread;
37 using String = std::string;
38
39 template <typename CharType> using BasicString = std::basic_string<CharType>;
40
41 using PChar = wchar_t;
42
44
45 class PHOTON_API BrowserError : public std::runtime_error
46 {
47 public:
48 BrowserError(const char* error = "ERROR_UNKNOWN") : std::runtime_error(error)
49 {
50 }
51 ~BrowserError() = default; // let the ABI define that.
52
54 BrowserError(const BrowserError&) = default;
55 };
56
58 {
59 auto err_pos = output.find("{{ " + fmt + " }}");
60
61 if (err_pos != String::npos)
62 {
63 output.replace(err_pos, ("{{ " + fmt + " }}").size(), with);
64 }
65
66 return output;
67 }
68
70 {
71 public:
72 explicit WindowHelper() = default;
73 ~WindowHelper() = default;
74
76 WindowHelper(const WindowHelper&) = default;
77
78 public:
79 static std::vector<String> kPermsList;
80
81 private:
82 NSWindow* window{nullptr};
83 NSScroller* scroller{nullptr};
84
86 void scroller_moved_(NSScroller* sender)
87 {
88 (void)sender;
89
90 double val = [scroller doubleValue]; // 0.0 (top) to 1.0 (bottom)
91 CGFloat contentHeight = NSHeight([window.contentView frame]);
92 CGFloat maxOffset = contentHeight;
93
94 NSRect f = [window.contentView frame];
95 f.origin.y = -val * maxOffset;
96 [window.contentView frame] = f;
97 }
98
99 public:
100 static constexpr const auto kWidth = 1280;
101 static constexpr const auto kHeight = 720;
102
103 NSWindow* tab(const String text)
104 {
105 if (window)
106 return window;
107
108 window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, kWidth, kHeight)
109 styleMask:(NSWindowStyleMaskTitled | NSWindowStyleMaskClosable)
110 backing:NSBackingStoreBuffered
111 defer:NO];
112
113 [window setTitle:[NSString stringWithUTF8String:((!text.empty()) ? text.c_str() : "Untitled - Photon")]];
114 [window center];
115
116 [window setBackgroundColor:[NSColor whiteColor]];
117
118 [window makeKeyAndOrderFront:nil];
119 [window setLevel:NSStatusWindowLevel];
120
121 return window;
122 }
123
124 String prompt(const String title, const String text = "")
125 {
126 NSAlert* alert = [[NSAlert alloc] init];
127
128 [alert setMessageText:[NSString stringWithUTF8String:((!title.empty()) ? title.c_str() : "Photon")]];
129 [alert setInformativeText:[NSString stringWithUTF8String:text.c_str()]];
130 [alert addButtonWithTitle:@"Ok"];
131 [alert addButtonWithTitle:@"Cancel"];
132 [alert setAlertStyle:NSAlertStyleInformational];
133
134 NSModalResponse response = [alert runModal];
135
136 return response == NSAlertFirstButtonReturn ? "YES" : "NO";
137 }
138
139 bool grant_or_fail(const String permission_name)
140 {
141 if (std::find(kPermsList.cbegin(), kPermsList.cend(), permission_name) != kPermsList.end())
142 return true;
143
144 if (this->prompt("Photon - Security", "The page is asking for: " + permission_name + "\nAccept?") == "NO")
145 return false;
146
147 kPermsList.push_back(permission_name);
148 return true;
149 }
150
151 int open(const char* app_name)
152 {
153 if (!app_name)
154 return ENOENT;
155
156 String cmd = "open -a ";
157 cmd += app_name;
158
159 return std::system(cmd.c_str());
160 }
161 };
162} // namespace Photon
163
164#define PHOTON_CRITICAL(...) spdlog::critical(__VA_ARGS__)
165#define PHOTON_ERROR(...) spdlog::error(__VA_ARGS__)
166#define PHOTON_INFO(...) spdlog::info(__VA_ARGS__)
167#define PHOTON_WARN(...) spdlog::warn(__VA_ARGS__)
PHOTON_API FILE * photon_get_logger()
PHOTON_API size_t photon_strlen(const char *str)
Definition Core.mm:16
PHOTON_API void photon_log(const char *str)
PHOTON_API time_t photon_get_epoch()
PHOTON_API bool photon_open_logger()
Definition Core.hpp:46
BrowserError(const BrowserError &)=default
BrowserError(const char *error="ERROR_UNKNOWN")
Definition Core.hpp:48
BrowserError & operator=(const BrowserError &)=default
Definition Core.hpp:70
static std::vector< String > kPermsList
Definition Core.hpp:79
bool grant_or_fail(const String permission_name)
Definition Core.hpp:139
void scroller_moved_(NSScroller *sender)
Definition Core.hpp:86
WindowHelper & operator=(const WindowHelper &)=default
WindowHelper(const WindowHelper &)=default
int open(const char *app_name)
Definition Core.hpp:151
NSWindow * tab(const String text)
Definition Core.hpp:103
String prompt(const String title, const String text="")
Definition Core.hpp:124
#define PHOTON_API
Definition Config.hpp:64
This file is also about defining core js concepts.
Definition BasePhotonWindow.hpp:22
std::thread Thread
Definition Core.hpp:36
String photon_replace_format(String fmt, String output, String with)
Definition Core.hpp:57
std::basic_string< CharType > BasicString
Definition Core.hpp:39
std::string String
Definition Core.hpp:37
BasicString< PChar > PString
Definition Core.hpp:43
wchar_t PChar
Definition Core.hpp:41
Definition bin_to_hex.h:111
Definition uuid.h:926
title
Definition tag_strings.h:6
annotation output
Definition tag_strings.h:122
annotation center
Definition tag_strings.h:147
annotation frame
Definition tag_strings.h:133