-
Notifications
You must be signed in to change notification settings - Fork 6
/
mmpWindowUtils.pas
389 lines (319 loc) · 14.5 KB
/
mmpWindowUtils.pas
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
{ MMP: Minimalist Media Player
Copyright (C) 2021-2024 Baz Cuda
https://github.com/BazzaCuda/MinimalistMediaPlayerX
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
}
unit mmpWindowUtils;
interface
uses
winApi.windows,
system.classes, system.SyncObjs,
mmpNotify.notices, mmpNotify.notifier, mmpNotify.subscriber,
mmpConsts, mmpGlobalState;
function mmpAdjustAspectRatio (const aWND: HWND; const aHeight: integer): TPoint;
function mmpArrangeAll (const aWND: HWND): boolean;
function mmpCalcWindowSize (const aStartingHeight: integer; const bMaxSize: boolean): TPoint;
function mmpCenterWindow (const aWND: HWND; const aPt: TPoint): boolean;
function mmpGreaterWindow (const aWND: HWND; aShiftState: TShiftState): integer; overload;
function mmpGreaterWindow (const aWND: HWND; const aShiftState: TShiftState; const aThumbSize: integer; const aHostType: THostType): boolean; overload;
function mmpPosWinXY (const aWND: HWND; const x: integer; const y: integer): boolean;
function mmpSetWindowPos (const aWND: HWND; aPt: TPoint): boolean;
function mmpSetWindowTop (const aWND: HWND): boolean;
function mmpSetWindowTopmost (const aWND: HWND): boolean;
function mmpSetWindowSize (const aWND: HWND; aPt: TPoint): boolean;
function mmpWinXY (const aWND: HWND): TPoint;
implementation
uses
winApi.messages,
system.types,
mmpDesktopUtils, mmpPostToAllUtils, mmpUtils,
_debugWindow;
function mmpAdjustAspectRatio(const aWND: HWND; const aHeight: integer): TPoint;
var
vWidth: integer;
MPmediaType: TMediaType;
MPvideoWidth: integer;
MPvideoHeight: integer;
function adjustWidthForAspectRatio: integer;
begin
case (MPvideoWidth <= 0) OR (MPvideoHeight <= 0) of TRUE: EXIT; end;
result := round(aHeight / MPvideoHeight * MPvideoWidth);
end;
function getMediaInfo: boolean;
begin
result := FALSE;
MPmediaType := GS.mediaType;
MPvideoWidth := notifyApp(newNotice(evMPReqVideoWidth)).integer;
MPvideoHeight := notifyApp(newNotice(evMPReqVideoHeight)).integer;
result := TRUE;
// debugFormat('MP.x:%d, MP.y:%d', [MPvideoWidth, MPvideoHeight]);
end;
begin
// FUserJ := TRUE;
result := point(0, 0);
getMediaInfo;
vWidth := adjustWidthForAspectRatio;
vWidth := vWidth + 2; // allow for the mysterious 1-pixel border that Windows insists on drawing around a borderless window
result.x := vWidth;
result.y := aHeight + 2;
end;
function mmpCalcWindowSize(const aStartingHeight: integer; const bMaxSize: boolean): TPoint;
{$J+} const vPrevVideoHeight: integer = 0; vPrevImageHeight: integer = 0; {$J-}
var
vWidth: integer;
vHeight: integer;
dy: integer;
vStartingHeight: integer;
MPmediaType: TMediaType;
MPvideoWidth: integer;
MPvideoHeight: integer;
MIhasCoverArt: boolean;
function adjustWidthForAspectRatio: integer;
begin
case (MPvideoWidth = 0) or (MPvideoHeight = 0) of TRUE: EXIT; end;
result := trunc(vHeight / MPvideoHeight * MPvideoWidth);
end;
function getMediaInfo: boolean;
begin
result := FALSE;
MPmediaType := GS.mediaType;
MPvideoWidth := notifyApp(newNotice(evMPReqVideoWidth)).integer;
MPvideoHeight := notifyApp(newNotice(evMPReqVideoHeight)).integer;
MIhasCoverArt := notifyApp(newNotice(evMIReqHasCoverArt)).tf;
result := TRUE;
// debugFormat('getMediaInfo MP.x:%d, MP.y:%d', [MPvideoWidth, MPvideoHeight]);
end;
function withinScreenLimits: boolean;
begin
var vDelta := mmpIfThenElse(GS.showingTimeline, GS.widthStreamlist, GS.widthHelp + GS.widthPlaylist); // at least one of widthHelp and widthPlaylist will be zero
result := (vWidth + vDelta <= mmpScreenWidth) and (vHeight <= mmpScreenHeight);
end;
begin
MPvideoWidth := 0; MPvideoHeight := 0;
getMediaInfo;
case MPmediaType of mtAudio: begin case MIhasCoverArt of TRUE: vWidth := 600;
FALSE: vWidth := 600; end;
case MIhasCoverArt of TRUE: vHeight := 400;
FALSE: vHeight := UI_DEFAULT_AUDIO_HEIGHT; end;
case MIhasCoverArt of TRUE: vWidth := adjustWidthForAspectRatio; end;
end;
mtVideo: begin
vStartingHeight := aStartingHeight;
case bMaxSize of TRUE: vStartingHeight := -1; end;
case (vStartingHeight <> -1) and (vStartingHeight <= UI_DEFAULT_AUDIO_HEIGHT) of TRUE: vStartingHeight := vPrevVideoHeight; end;
case (vStartingHeight <> -1) and (vStartingHeight <= UI_DEFAULT_AUDIO_HEIGHT) of TRUE: vStartingHeight := -1; end;
case vStartingHeight = -1 of
TRUE: vHeight := mmpScreenHeight - 30;
FALSE: vHeight := vStartingHeight; end;
vWidth := adjustWidthForAspectRatio;
while NOT withinScreenLimits do
begin
vHeight := vHeight - 30;
vWidth := adjustWidthForAspectRatio;
end;
vPrevVideoHeight := vHeight;
end;
mtImage: begin
vStartingHeight := aStartingHeight;
case GS.maxSize of TRUE: vStartingHeight := -1; end;
case (vStartingHeight <> -1) and (vStartingHeight <= UI_DEFAULT_AUDIO_HEIGHT) of TRUE: vStartingHeight := vPrevImageHeight; end;
case (vStartingHeight <> -1) and (vStartingHeight <= UI_DEFAULT_AUDIO_HEIGHT) of TRUE: vStartingHeight := -1; end;
case vStartingHeight = -1 of TRUE: begin
vWidth := trunc((mmpScreenHeight - 100) * 1.5);
vHeight := mmpScreenHeight - 100; end;
FALSE: begin
vWidth := trunc(vStartingHeight * 1.5);
vHeight := vStartingHeight; end;end;
while NOT withinScreenLimits do
begin
vWidth := vWidth - 30;
vHeight := vHeight - 30;
end;
vPrevImageHeight := vHeight;
end;
end;
result.x := vWidth;
result.y := vHeight;
end;
function mmpArrangeAll(const aWND: HWND): boolean;
var
vCount: integer;
vWidth,
vHeight: integer;
vScreenWidth,
vScreenHeight: integer;
vZero: integer;
vHMiddle,
vVMiddle: integer;
begin
result := FALSE;
vCount := notifyApp(newNotice(evPAReqCount)).integer;
notifyApp(newNotice(evGSAutoCenter, vCount = 1));
case GS.autoCenter of FALSE: begin
notifyApp(newNotice(evPAPostToEvery, WIN_AUTOCENTER_OFF));
notifyApp(newNotice(evPAPostToEvery, WIN_MAX_SIZE_OFF)); end;end;
var vMsg: TMessage;
vMsg := default(TMessage);
case vCount of
1: vMsg.WParam := mmpScreenWidth;
2: vMsg.WParam := mmpScreenWidth div 2;
3, 4: vMsg.LParam := mmpScreenHeight div 2;
else vMsg.WParam := mmpScreenWidth div vCount;
end;
vMsg.msg := WIN_RESIZE;
notifyApp(newNotice(evPAPostToEveryEx, vMsg));
mmpProcessMessages; // make sure this window has resized before continuing
mmpWndWidthHeight(aWND, vWidth, vHeight);
vScreenWidth := mmpScreenWidth;
vScreenHeight := mmpScreenHeight;
vHMiddle := vScreenWidth div 2;
vVMiddle := vScreenHeight div 2;
vZero := vHMiddle - vWidth;
vCount := notifyApp(newNotice(evPAReqCount)).integer;
var vHWND := 0;
case vCount = 2 of TRUE: begin
mmpPosWinXY(PA[1], vZero, (vScreenHeight - vHeight) div 2);
mmpPosWinXY(PA[2], vHMiddle, (vScreenHeight - vHeight) div 2);
case mmpOffScreen(PA[1]) of TRUE: mmpPosWinXY(PA[1], vZero, 0); end;
case mmpOffScreen(PA[2]) of TRUE: mmpPosWinXY(PA[2], vHMiddle, 0); end;
vHWND := PA[1];
end;end;
case vCount in [3, 4] of TRUE: begin
mmpPosWinXY(PA[1], vZero, 0 + 40);
mmpPosWinXY(PA[2], vHMiddle, 0 + 40); end;end;
case vCount = 3 of TRUE: mmpPosWinXY(PA[3], vHMiddle - (vWidth div 2), vHeight + 40); end;
case vCount = 4 of TRUE: begin
mmpPosWinXY(PA[3], vZero, vHeight + 40);
mmpPosWinXY(PA[4], vHMiddle, vHeight + 40); end;end;
case vCount > 4 of TRUE: for var i := 1 to vCount do mmpPosWinXY(PA[i], ((mmpScreenWidth div vCount) * (i - 1)), 100); end;
case vHWND <> 0 of TRUE: begin mmpDelay(100); mmpPosWinXY(vHWND, mmpScreenCentre - vWidth, mmpWinXY(vHWND).Y); end;end; // hack for tall, narrow, TikTok-type windows
result := TRUE;
end;
function mmpCenterWindow(const aWND: HWND; const aPt: TPoint): boolean;
// aPt is optional and provides the calculated dimensions that a window is going to have
var
vR: TRect;
vHPos: integer;
vVPos: integer;
function alreadyCentred: boolean;
begin
result := FALSE;
var vDelta := mmpIfThenElse(GS.showingTimeline, GS.widthStreamlist, GS.widthHelp + GS.widthPlaylist); // one of either widthHelp or widthPlaylist will be zero
vHPos := (mmpScreenWidth - vR.width - vDelta) div 2;
vVPos := (mmpScreenHeight - vR.height) div 2;
result := (vR.left = vHPos) and (vR.top = vVPos);
end;
begin
result := FALSE;
getWindowRect(aWND, vR);
case (aPt.x <> 0) and (aPt.y <> 0) of TRUE: begin // override the current dimensions with those provided
vR.width := aPt.x;
vR.height := aPt.y; end;end;
case alreadyCentred of TRUE: EXIT; end;
case (vHPos > 0) and (vVPos > 0) of TRUE: mmpSetWindowPos(aWND, point(vHPos, vVPos)); end;
notifyApp(newNotice(evGSAutoCenter, TRUE));
result := TRUE;
end;
function mmpGreaterWindow(const aWND: HWND; aShiftState: TShiftState): integer;
var
vR: TRect;
begin
getWindowRect(aWND, vR);
GS.notify(newNotice(evGSMaxSize, FALSE)); // pressing [M] reinstates maxSize
case ssCtrl in aShiftState of TRUE: result := vR.height - 30;
FALSE: result := vR.height + 30; end;
end;
function mmpGreaterWindow(const aWND: HWND; const aShiftState: TShiftState; const aThumbSize: integer; const aHostType: THostType): boolean;
var
dx: integer;
dy: integer;
newW: integer;
newH: integer;
vR: TRect;
function calcDeltas: boolean;
begin
case aHostType of
htMPVHost: begin
dx := 50;
dy := 30;
end;
htThumbsHost: begin
dx := aThumbSize + THUMB_MARGIN;
dy := aThumbSize + THUMB_MARGIN;
end;
end;
end;
function checkDesktop: boolean;
begin
case ssCtrl in aShiftState of TRUE: begin
case newW - dx < dx of TRUE: dx := 0; end;
case newH - dy < dy of TRUE: dy := 0; end;end;
FALSE: begin
case newW + dx > mmpScreenWidth of TRUE: dx := 0; end;
case newH + dy > mmpScreenHeight of TRUE: dy := 0; end;end;end;
end;
function calcDimensions: boolean;
begin
case ssCtrl in aShiftState of
TRUE: begin
newW := newW - dx;
newH := newH - dy;
end;
FALSE: begin
newW := newW + dx;
newH := newH + dy;
end;
end;
end;
begin
getWindowRect(aWND, vR);
newW := vR.Width;
newH := vR.height;
calcDeltas;
checkDesktop;
calcDimensions; // do what the user requested
mmpSetWindowSize(aWND, point(newW, newH)); // resize the window
end;
function mmpPosWinXY(const aWND: HWND; const x: integer; const y: integer): boolean;
begin
result := FALSE;
mmpSetWindowPos(aWND, point(x, y));
result := TRUE;
end;
function mmpSetWindowPos(const aWND: HWND; aPt: TPoint): boolean;
begin
result := FALSE;
setWindowPos(aWND, HWND_TOP, aPt.x, aPt.y, 0, 0, SWP_NOSIZE);
result := TRUE;
end;
function mmpSetWindowSize(const aWND: HWND; aPt: TPoint): boolean;
begin
result := FALSE;
setWindowPos(aWND, HWND_TOP, 0, 0, aPt.x, aPt.y, SWP_NOMOVE);
result := TRUE;
end;
function mmpSetWindowTop(const aWND: HWND): boolean;
begin
setWindowPos(aWND, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE + SWP_NOMOVE);
end;
function mmpSetWindowTopmost(const aWND: HWND): boolean;
begin
setWindowPos(aWND, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE + SWP_NOMOVE);
end;
function mmpWinXY(const aWND: HWND): TPoint;
var vR: TRect;
begin
getWindowRect(aWnd, vR);
result := vR.location;
end;
end.