This repository has been archived by the owner on Jan 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
297 lines (242 loc) · 8.61 KB
/
main.cpp
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include "ui/Application.hpp"
#include "ui/Window.hpp"
#include "ui/WindowToplevel.hpp"
#include "ui/WindowShell.hpp"
#include "ui/View.hpp"
#include "ui/widget/Label.hpp"
#include "ui/widget/Image.hpp"
#include <iostream>
#include <thread>
#include <queue>
#include <ctime>
#include <iomanip>
#include <sstream>
#include <random>
#include <include/core/SkFont.h>
#include <include/core/SkTextBlob.h>
class HoverView : public UI::View
{
using UI::View::View;
virtual void view_did_load()
{
UI::View::view_did_load();
this->opacity = 0;
this->layer->opacity.set(0);
}
int x_before = 0;
virtual void on_mouse_click()
{
this->x_before = this->frame.x();
}
virtual void on_mouse_up(int x, int y)
{
UI::View::animate(250, [this]()
{
UI::Shape::Rect rect = this->frame;
rect.set_x(this->x_before);
this->set_frame(rect); });
}
virtual void on_mouse_drag(SkPoint delta)
{
UI::Shape::Rect rect = this->frame;
rect.set_x(x_before + delta.x());
this->set_frame(rect);
}
virtual void on_mouse_enter()
{
UI::View::animate(250, [this]()
{ this->set_opacity(150); });
}
virtual void on_mouse_exit()
{
UI::View::animate(500, [this]()
{ this->set_opacity(0); });
}
};
class ShellView : public UI::View
{
using UI::View::View;
UI::Label *time_label;
UI::Label *program_label;
UI::Image *image;
HoverView *menu_button;
HoverView *program_view;
virtual void view_did_load()
{
UI::View::view_did_load();
this->background_color = SkColorSetARGB(230, 47, 53, 69);
menu_button = new HoverView(UI::Shape::Rect(8, 0, 42, 42));
this->add_subview(menu_button);
menu_button->background_color = SkColorSetARGB(255, 150, 150, 150);
menu_button->set_background_radius(8);
program_view = new HoverView(UI::Shape::Rect(this->menu_button->frame.width() + this->menu_button->frame.x() + 12, 0, 130, 42));
this->add_subview(program_view);
program_view->background_color = SkColorSetARGB(255, 150, 150, 150);
program_view->set_background_radius(8);
this->image = new UI::Image("/usr/share/icons/hicolor/22x22/apps/firefox.png");
this->program_view->add_subview(image);
program_label = new UI::Label();
program_label->set_contents("Firefox");
program_label->set_font_size(12);
program_label->color = SK_ColorWHITE;
this->program_view->add_subview(program_label);
time_label = new UI::Label();
time_label->set_contents("5:00 AM");
time_label->color = SK_ColorWHITE;
this->add_subview(time_label);
}
virtual void layout_subviews()
{
auto t = std::time(nullptr);
auto tm = *std::localtime(&t);
std::ostringstream oss;
oss << std::put_time(&tm, "%I:%M %p");
std::string str = oss.str();
if (str.front() == '0')
{
std::cout << "begins with 0\n";
str.erase(0, 1);
}
this->time_label->set_contents(str);
this->time_label->size_to_fit();
this->program_label->size_to_fit();
int program_width = this->program_view->frame.width();
int program_height = this->program_view->frame.height();
this->image->set_frame(UI::Shape::Rect(12, program_height / 2 - this->image->frame.height() / 2, 22, 22));
this->program_label->set_frame(UI::Shape::Rect(this->image->frame.x() + 22 + 6, program_height / 2 - this->program_label->frame.height() / 2, this->program_label->frame.width(), this->program_label->frame.height()));
int width = this->time_label->frame.width();
int height = this->time_label->frame.height();
int x = this->frame.width() - width - 12;
int y = this->frame.height() / 2 - height / 2;
this->time_label->set_frame(UI::Shape::Rect(x, y, width, height));
}
};
class ScrollView : public UI::View
{
using UI::View::View;
UI::View *scroll_indicator;
virtual void view_did_load()
{
UI::View::view_did_load();
this->scroll_indicator = new UI::View(0, 0, 10, 10);
this->scroll_indicator->background_radius = 16;
this->clip_to_bounds = true;
this->drop_shadow = true;
this->background_radius = 32;
std::random_device rd; // obtain a random number from hardware
std::mt19937 gen(rd()); // seed the generator
std::uniform_int_distribution<> distr(0, 255); // define the range
for (int i = 0; i < 10; i++)
{
int r = distr(gen);
int b = distr(gen);
int g = distr(gen);
UI::View *new_view = new UI::View(0, 0, 10, 10);
new_view->background_color = SkColorSetRGB(r, g, b);
this->add_subview(new_view);
}
this->add_subview(this->scroll_indicator);
}
int prev_y = 0;
const static int height = 64;
virtual void layout_subviews()
{
for (int i = 0; i < 10; i++)
{
UI::View *view = children.at(i);
view->layer->opacity.set(150);
view->set_frame(UI::Shape::Rect(0, height * i, this->frame.width(), 64));
}
int width = this->frame.width();
int height = this->frame.height();
this->scroll_indicator->set_frame(UI::Shape::Rect(width - 16, 0, 16, 64));
}
int scroll_end()
{
int d = std::min(0, (int)(this->layer->bounds.y()));
d = std::max(-(int)(height * children.size()) + this->frame.height(), d);
this->set_bounds(UI::Shape::Rect(0, d, this->bounds.width(), this->bounds.height()));
}
virtual void on_mouse_up(int x, int y)
{
// UI::View::animate(250, [this]()
// { this->scroll_end(); });
}
virtual void on_mouse_drag(SkPoint delta)
{
// this->layer->bounds.set_y(this->bounds.y() + delta.y());
}
virtual void on_mouse_scroll(bool discrete, int delta, bool is_scrolling)
{
if (!discrete)
{
if (is_scrolling)
{
this->layer->bounds.set_y(this->layer->bounds.y() + (delta / 64));
}
else
{
std::cout << "Animate scroll\n";
UI::View::animate(250, [this]()
{ this->scroll_end(); });
}
}
else
{
int d = (delta < 0) ? 64 : -64;
this->set_bounds(UI::Shape::Rect(0, this->bounds.y() + d, this->bounds.width(), this->bounds.height()));
}
}
};
class RootView : public UI::View
{
using UI::View::View;
UI::View *test_view;
UI::Label *label;
ShellView *shell_view;
ScrollView *scroll_view;
virtual void view_did_load()
{
UI::View::view_did_load();
this->background_color = SkColorSetRGB(33, 33, 33);
// this->label = new UI::Label();
// this->label->set_contents("Edging.");
// this->label->set_font_size(48);
// this->label->color = SK_ColorWHITE;
// this->add_subview(label);
this->shell_view = new ShellView(0, 0, 10, 10);
this->add_subview(shell_view);
// this->scroll_view = new ScrollView(0, 0, 100, 100);
// this->add_subview(scroll_view);
}
virtual void layout_subviews()
{
int width = this->frame.width();
int height = this->frame.height();
std::cout << width << "\n";
// this->label->size_to_fit();
// int label_width = this->label->frame.width();
// int label_height = this->label->frame.height();
// int x = width / 2 - label_width / 2;
// int y = 32;
// this->label->set_frame(UI::Shape::Rect(x, y, label_width, label_height));
this->shell_view->set_frame(UI::Shape::Rect(0, 0, width, 48));
// this->scroll_view->set_frame(UI::Shape::Rect(width / 4, height / 4, width / 2, height / 2));
}
};
class MyWindowDelegate : public UI::WindowDelegate
{
virtual void did_finish_launching(UI::Window *window)
{
window->add_root_view(new RootView(0, 0, window->get_width(), 48));
}
};
int main()
{
UI::Application *app = UI::Application::get_instance();
UI::WindowShell *shell_surface = new UI::WindowShell(app->registry->wl_outputs.at(0));
// UI::WindowToplevel *shell_surface = new UI::WindowToplevel("PanosUI", 500, 500);
shell_surface->delegate = new MyWindowDelegate();
app->add_window(shell_surface);
app->run();
}