-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.mm
173 lines (146 loc) · 5.9 KB
/
main.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "Photino/App/App.h"
#include "PhotinoShared/Log.h"
using namespace Photino;
using namespace PhotinoShared;
int main()
{
Log::WriteLine("Starting execution");
App *app = new App();
app
->Events()
->AddEventAction(AppEvents::WillRun, [](App *sender, std::string *empty)
{
Log::WriteLine("Application is about to run.");
});
// Main Window
Window *mainWindow = new Window("Main Window");
mainWindow
->Events()
->AddEventAction(WindowEvents::WindowDidResize, [](Window *sender, std::string *empty)
{
WindowSize size = sender->GetSize();
char buffer[100];
std::sprintf(buffer, "{\"width\":%d,\"height\":%d}", size.width, size.height);
std::string data(buffer);
Log::WriteLine("Window did resize to: " + size.ToString());
sender->WebView()->SendScriptEvent("window-did-resize", data);
})
->AddEventAction(WindowEvents::WindowDidMove, [](Window *sender, std::string *empty)
{
WindowLocation location = sender->GetLocation();
char buffer[100];
std::sprintf(buffer, "{\"left\":%d,\"top\":%d}", location.left, location.top);
std::string data(buffer);
Log::WriteLine("Window did move to: " + location.ToString());
sender->WebView()->SendScriptEvent("window-did-move", data);
})
->AddEventAction(WindowEvents::WindowShouldClose, [](Window *sender, std::string *shouldClose)
{
Log::WriteLine("Window should close soon.");
*shouldClose = "NO"; // prevent default
Alert *alert = new Alert(
sender->NativeWindow(),
"Are you sure you want to close the app?",
"Warning",
NSAlertStyleWarning,
"Close",
"close");
alert->AddButton("Cancel", "cancel");
alert->Open([=](std::string response)
{
if (response == "close")
{
sender->ForceClose();
delete sender;
}
delete alert;
});
})
->AddEventAction(WindowEvents::WindowWillClose, [](Window *sender, std::string *empty)
{
Log::WriteLine("Window will close now.");
});
Photino::WebView *mainWindowWebView = mainWindow->WebView();
mainWindowWebView
->Events()
->AddEventAction(WebViewEvents::WillLoadResource, [](Photino::WebView *sender, std::string *resourceURL)
{
Log::WriteLine("Resource will load: " + *resourceURL);
})
->AddEventAction(WebViewEvents::DidLoadResource, [&](Photino::WebView *sender, std::string *empty)
{
Log::WriteLine("Resource did load.");
})
->AddEventAction(WebViewEvents::DidReceiveScriptMessage, [=](Photino::WebView *sender, std::string *message)
{
if (message->size() <= 30)
{
Log::WriteLine("Received message: " + *message);
sender->SendScriptMessage("Hey " + *message + "!");
}
else
{
Alert *alert = new Alert(
sender->NativeWindow(),
"Your message is too long!",
"Warning",
NSAlertStyleCritical,
"Confirm",
"confirmed");
alert->AddButton("Abort", "aborted");
alert->AddButton("Report", "reported");
alert
->Events()
->AddEventAction(AlertEvents::WillOpen, [](Alert *sender, std::string *empty)
{
Log::WriteLine("Alert will open.");
})
->AddEventAction(AlertEvents::DidOpen, [](Alert *sender, std::string *empty)
{
Log::WriteLine("Alert did open.");
})
->AddEventAction(AlertEvents::WillClose, [](Alert *sender, std::string *empty)
{
Log::WriteLine("Alert will close.");
});
alert->Open([=](std::string response)
{
Log::WriteLine("User " + response + " event.");
if (response == "reported")
{
// Second Window
Window *secondWindow = new Window("Error Report");
secondWindow
->SetLocation(mainWindow->GetLocation())
->Offset(20, 20)
->WebView()
->LoadHtmlString("Thank you for your feedback!");
}
delete alert;
});
}
})
// ->AddEventAction(WebViewEvents::WillSendScriptMessage, [](Photino::WebView *sender, std::string *message)
// {
// Log::WriteLine("Overwrite message: " + *message);
// *message = "Hi John!";
// Log::WriteLine("New message: " + *message);
// })
->AddEventAction(WebViewEvents::CloseScriptConfirm, [](Photino::WebView *sender, std::string *isConfirmedString)
{
Log::WriteLine("Closed confirmation dialog with: " + *isConfirmedString);
});
mainWindowWebView
->LoadResource("Assets/index.html");
// Second Window
// Window *secondWindow = new Window("Second Window");
// secondWindow
// ->WebView()
// ->LoadResource("http://www.tryphotino.io");
// app->AddWindow(mainWindow)
// ->AddWindow(secondWindow);
app->Run();
delete app;
Log::WriteLine("Stopping execution");
return 0;
}