-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLoadingDots.cpp
More file actions
93 lines (74 loc) · 1.76 KB
/
Copy pathLoadingDots.cpp
File metadata and controls
93 lines (74 loc) · 1.76 KB
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
/*
* LoadingDots.cpp - Animated three-dot loading indicator
* Distributed under the terms of the MIT License.
*/
#include "LoadingDots.h"
#include <InterfaceDefs.h>
#include <Message.h>
#include <Messenger.h>
LoadingDots::LoadingDots(const char* name)
:
BView(name, B_WILL_DRAW),
fActive(false),
fCurrentDot(0),
fRunner(NULL)
{
SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
}
LoadingDots::~LoadingDots()
{
delete fRunner;
}
void
LoadingDots::Start()
{
if (fActive)
return;
fActive = true;
fCurrentDot = 0;
delete fRunner;
BMessage msg(kMsgDotsPulse);
fRunner = new BMessageRunner(BMessenger(this), &msg, 450000);
Invalidate();
}
void
LoadingDots::Stop()
{
fActive = false;
fCurrentDot = 0;
delete fRunner;
fRunner = NULL;
Invalidate();
}
void
LoadingDots::Draw(BRect updateRect)
{
BRect bounds = Bounds();
rgb_color bg = ui_color(B_PANEL_BACKGROUND_COLOR);
SetHighColor(bg);
FillRect(bounds);
if (!fActive)
return;
rgb_color dimColor = tint_color(bg, B_DISABLED_LABEL_TINT);
rgb_color litColor = ui_color(B_STATUS_BAR_COLOR);
float dotSize = floorf(bounds.Height() * 0.35f);
float spacing = dotSize * 1.8f;
float totalWidth = kDotCount * dotSize + (kDotCount - 1) * (spacing - dotSize);
float startX = bounds.left + (bounds.Width() - totalWidth) / 2.0f;
float centerY = bounds.top + bounds.Height() / 2.0f;
for (int32 i = 0; i < kDotCount; i++) {
float cx = startX + i * spacing + dotSize / 2.0f;
SetHighColor(i == fCurrentDot ? litColor : dimColor);
FillEllipse(BPoint(cx, centerY), dotSize / 2.0f, dotSize / 2.0f);
}
}
void
LoadingDots::MessageReceived(BMessage* message)
{
if (message->what == kMsgDotsPulse && fActive) {
fCurrentDot = (fCurrentDot + 1) % kDotCount;
Invalidate();
} else {
BView::MessageReceived(message);
}
}