Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

win32: add an option to control window title bar state #11469

Merged
merged 7 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DOCS/interface-changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ Interface changes
- remove deprecated `--vf-defaults` and `--af-defaults` options
- `--drm-connector` no longer allows selecting the card number (use `--drm-device`
instead)
- add `--title-bar` option
- add `--window-corners` option
--- mpv 0.36.0 ---
- add `--target-contrast`
- Target luminance value is now also applied when ICC profile is used.
Expand Down
15 changes: 15 additions & 0 deletions DOCS/man/options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3131,10 +3131,25 @@ Window
Focus the video window on creation and makes it the front most window. This
is on by default.

``--window-corners=<default|donotround|round|roundsmall>``
(Windows only)
Set the preference for window corner rounding.

:default: Let the system decide whether or not to round window corners
:donotround: Never round window corners
:round: Round the corners if appropriate
:roundsmall: Round the corners if appropriate, with a small radius

``--border``, ``--no-border``
Play video with window border and decorations. Since this is on by
default, use ``--no-border`` to disable the standard window decorations.

``--title-bar``, ``--no-title-bar``
(Windows only)
Play video with the window title bar. Since this is on by default,
use --no-title-bar to hide the title bar. The --no-border option takes
precedence.

``--on-all-workspaces``
(X11 and macOS only)
Show the video window on all virtual desktops.
Expand Down
13 changes: 13 additions & 0 deletions options/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#ifdef _WIN32
#include <windows.h>
#include <dwmapi.h>
#endif

#include "options.h"
Expand Down Expand Up @@ -114,6 +115,7 @@ static const m_option_t mp_vo_opt_list[] = {
{"ontop-level", OPT_CHOICE(ontop_level, {"window", -1}, {"system", -2},
{"desktop", -3}), M_RANGE(0, INT_MAX)},
{"border", OPT_BOOL(border)},
{"title-bar", OPT_BOOL(title_bar)},
{"on-all-workspaces", OPT_BOOL(all_workspaces)},
{"geometry", OPT_GEOMETRY(geometry)},
{"autofit", OPT_SIZE_BOX(autofit)},
Expand Down Expand Up @@ -189,6 +191,16 @@ static const m_option_t mp_vo_opt_list[] = {
{"window-affinity", OPT_CHOICE(window_affinity, {"default", WDA_NONE},
{"excludefromcapture", WDA_EXCLUDEFROMCAPTURE}, {"monitor", WDA_MONITOR})},
{"vo-mmcss-profile", OPT_STRING(mmcss_profile)},
// For old MinGW-w64 compatibility
#define DWMWCP_DEFAULT 0
#define DWMWCP_DONOTROUND 1
#define DWMWCP_ROUND 2
#define DWMWCP_ROUNDSMALL 3
{"window-corners", OPT_CHOICE(window_corners,
{"default", DWMWCP_DEFAULT},
{"donotround", DWMWCP_DONOTROUND},
{"round", DWMWCP_ROUND},
{"roundsmall", DWMWCP_ROUNDSMALL})},
#endif
#if HAVE_EGL_ANDROID
{"android-surface-size", OPT_SIZE_BOX(android_surface_size)},
Expand Down Expand Up @@ -216,6 +228,7 @@ const struct m_sub_options vo_sub_opts = {
.native_fs = true,
.taskbar_progress = true,
.border = true,
.title_bar = true,
.appid = "mpv",
.content_type = -1,
.WinID = -1,
Expand Down
2 changes: 2 additions & 0 deletions options/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ typedef struct mp_vo_opts {
int ontop_level;
bool fullscreen;
bool border;
bool title_bar;
bool all_workspaces;
bool window_minimized;
bool window_maximized;
Expand Down Expand Up @@ -65,6 +66,7 @@ typedef struct mp_vo_opts {

int window_affinity;
char *mmcss_profile;
int window_corners;

double override_display_fps;
double timing_offset;
Expand Down
60 changes: 57 additions & 3 deletions video/out/w32_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ EXTERN_C IMAGE_DOS_HEADER __ImageBase;
#define DWMWA_USE_IMMERSIVE_DARK_MODE 20
#endif

#define DWMWA_WINDOW_CORNER_PREFERENCE 33

#ifndef DPI_ENUMS_DECLARED
typedef enum MONITOR_DPI_TYPE {
MDT_EFFECTIVE_DPI = 0,
Expand Down Expand Up @@ -167,7 +169,7 @@ struct vo_w32_state {
HANDLE avrt_handle;
};

static void add_window_borders(struct vo_w32_state *w32, HWND hwnd, RECT *rc)
static void adjust_window_rect(struct vo_w32_state *w32, HWND hwnd, RECT *rc)
{
if (w32->api.pAdjustWindowRectExForDpi) {
w32->api.pAdjustWindowRectExForDpi(rc,
Expand All @@ -178,6 +180,15 @@ static void add_window_borders(struct vo_w32_state *w32, HWND hwnd, RECT *rc)
}
}

static void add_window_borders(struct vo_w32_state *w32, HWND hwnd, RECT *rc)
{
RECT win = *rc;
adjust_window_rect(w32, hwnd, rc);
// Adjust for title bar height that will be hidden in WM_NCCALCSIZE
if (w32->opts->border && !w32->opts->title_bar && !w32->current_fs)
rc->top -= rc->top - win.top;
}

// basically a reverse AdjustWindowRect (win32 doesn't appear to have this)
static void subtract_window_borders(struct vo_w32_state *w32, HWND hwnd, RECT *rc)
{
Expand Down Expand Up @@ -809,6 +820,13 @@ static DWORD update_style(struct vo_w32_state *w32, DWORD style)
return style;
}

static LONG get_title_bar_height(struct vo_w32_state *w32)
{
RECT rc = {0};
adjust_window_rect(w32, w32->window, &rc);
return -rc.top;
}

static void update_window_style(struct vo_w32_state *w32)
{
if (w32->parent)
Expand Down Expand Up @@ -855,6 +873,18 @@ static void fit_window_on_screen(struct vo_w32_state *w32)
if (w32->opts->border)
subtract_window_borders(w32, w32->window, &screen);

// Check for invisible borders and adjust the work area size
RECT frame, window;
if (GetWindowRect(w32->window, &window) &&
SUCCEEDED(DwmGetWindowAttribute(w32->window, DWMWA_EXTENDED_FRAME_BOUNDS,
&frame, sizeof(RECT))))
{
screen.left -= frame.left - window.left;
screen.top -= frame.top - window.top;
screen.right += window.right - frame.right;
screen.bottom += window.bottom - frame.bottom;
}

bool adjusted = fit_rect_size(&w32->windowrc, rect_w(screen), rect_h(screen));

if (w32->windowrc.top < screen.top) {
Expand Down Expand Up @@ -1007,13 +1037,23 @@ static void update_window_state(struct vo_w32_state *w32)
signal_events(w32, VO_EVENT_RESIZE);
}

static void update_corners_pref(const struct vo_w32_state *w32) {
if (w32->parent)
return;

int pref = w32->current_fs ? 0 : w32->opts->window_corners;
DwmSetWindowAttribute(w32->window, DWMWA_WINDOW_CORNER_PREFERENCE,
&pref, sizeof(pref));
}

static void reinit_window_state(struct vo_w32_state *w32)
{
if (w32->parent)
return;

// The order matters: fs state should be updated prior to changing styles
update_fullscreen_state(w32);
update_corners_pref(w32);
update_window_style(w32);

// fit_on_screen is applied at most once when/if applicable (normal win).
Expand Down Expand Up @@ -1230,7 +1270,7 @@ static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam,
break;
case WM_NCHITTEST:
// Provide sizing handles for borderless windows
if (!w32->opts->border && !w32->current_fs) {
if ((!w32->opts->border || !w32->opts->title_bar) && !w32->current_fs) {
return borderless_nchittest(w32, GET_X_LPARAM(lParam),
GET_Y_LPARAM(lParam));
}
Expand Down Expand Up @@ -1351,6 +1391,15 @@ static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam,
case WM_SETTINGCHANGE:
update_dark_mode(w32);
break;
case WM_NCCALCSIZE:
// Apparently removing WS_CAPTION disables some window animation, instead
// just reduce non-client size to remove title bar.
if (wParam && lParam && w32->opts->border && !w32->opts->title_bar &&
!w32->current_fs && !w32->parent)
{
((LPNCCALCSIZE_PARAMS) lParam)->rgrc[0].top -= get_title_bar_height(w32);
}
break;
}

if (message == w32->tbtnCreatedMsg) {
Expand Down Expand Up @@ -1622,6 +1671,7 @@ static void *gui_thread(void *ptr)
}

update_dark_mode(w32);
update_corners_pref(w32);
if (w32->opts->window_affinity)
update_affinity(w32);

Expand Down Expand Up @@ -1802,13 +1852,17 @@ static int gui_thread_control(struct vo_w32_state *w32, int request, void *arg)
update_affinity(w32);
} else if (changed_option == &vo_opts->ontop) {
update_window_state(w32);
} else if (changed_option == &vo_opts->border) {
} else if (changed_option == &vo_opts->border ||
changed_option == &vo_opts->title_bar)
{
update_window_style(w32);
update_window_state(w32);
} else if (changed_option == &vo_opts->window_minimized) {
update_minimized_state(w32);
} else if (changed_option == &vo_opts->window_maximized) {
update_maximized_state(w32);
} else if (changed_option == &vo_opts->window_corners) {
update_corners_pref(w32);
}
}

Expand Down