-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArtworkWindow.cpp
More file actions
156 lines (125 loc) · 3.38 KB
/
Copy pathArtworkWindow.cpp
File metadata and controls
156 lines (125 loc) · 3.38 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
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
#include "ArtworkWindow.h"
#include "App.h"
#include "ArtworkReplicantView.h"
#include "SettingsController.h"
#include <Catalog.h>
#include <LayoutBuilder.h>
#include <Screen.h>
#include <algorithm>
#include <cmath>
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "ArtworkWindow"
static const float kMinArtworkWindowSize = 120.0f;
ArtworkWindow::ArtworkWindow()
:
BWindow(BRect(260, 140,
260 + kDefaultArtworkWindowSize,
140 + kDefaultArtworkWindowSize), B_TRANSLATE("Artwork"),
B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS)
{
fArtworkView = new ArtworkReplicantView();
fArtworkView->SetRegisterForUpdates(true);
BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
.SetInsets(0, 0, 0, 0)
.Add(fArtworkView)
.End();
SetSizeLimits(kMinArtworkWindowSize, 100000.0f,
kMinArtworkWindowSize, 100000.0f);
HaifySettings s = SettingsController::Load();
if (s.artworkWindowX >= 0)
MoveTo(s.artworkWindowX, s.artworkWindowY);
if (s.artworkWindowW > 0 && s.artworkWindowH > 0) {
float size = std::max(s.artworkWindowW, s.artworkWindowH);
ResizeTo(size, size);
}
_EnsureOnScreen();
fLastWidth = Bounds().Width();
fLastHeight = Bounds().Height();
}
ArtworkWindow::~ArtworkWindow()
{
App* app = (App*)be_app;
if (app && app->IsQuitting())
return;
_SaveFrame(!IsHidden());
}
bool
ArtworkWindow::QuitRequested()
{
App* app = (App*)be_app;
if (app && app->IsQuitting())
return true;
if (app)
app->SetArtworkWindowOpen(false);
_SaveFrame(false);
Hide();
return false;
}
void
ArtworkWindow::FrameResized(float width, float height)
{
BWindow::FrameResized(width, height);
_EnforceSquareSize(width, height);
}
void
ArtworkWindow::SaveOpenState(bool open)
{
App* app = (App*)be_app;
if (app)
app->SetArtworkWindowOpen(open);
_SaveFrame(open);
}
void
ArtworkWindow::_EnforceSquareSize(float width, float height)
{
if (fAdjustingSize)
return;
if (std::fabs(width - height) < 1.0f) {
fLastWidth = width;
fLastHeight = height;
return;
}
float widthDelta = std::fabs(width - fLastWidth);
float heightDelta = std::fabs(height - fLastHeight);
float size = widthDelta >= heightDelta ? width : height;
if (size < kMinArtworkWindowSize)
size = kMinArtworkWindowSize;
fAdjustingSize = true;
ResizeTo(size, size);
fAdjustingSize = false;
fLastWidth = size;
fLastHeight = size;
}
void
ArtworkWindow::_EnsureOnScreen()
{
BScreen screen(this);
if (!screen.IsValid())
return;
BRect screenFrame = screen.Frame();
BRect frame = Frame();
float x = frame.left;
float y = frame.top;
if (frame.right > screenFrame.right)
x = screenFrame.right - frame.Width();
if (frame.bottom > screenFrame.bottom)
y = screenFrame.bottom - frame.Height();
if (x < screenFrame.left)
x = screenFrame.left;
if (y < screenFrame.top)
y = screenFrame.top;
if (x != frame.left || y != frame.top)
MoveTo(x, y);
}
void
ArtworkWindow::_SaveFrame(bool open)
{
BRect frame = Frame();
SettingsController::Update([&](HaifySettings& s) {
s.artworkWindowOpen = open;
s.artworkWindowX = frame.left;
s.artworkWindowY = frame.top;
s.artworkWindowW = frame.Width();
s.artworkWindowH = frame.Height();
});
}