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

Add window_set_fullscreen Window API #449

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
115 changes: 112 additions & 3 deletions webview.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ extern "C" {
#endif

typedef void *webview_t;
typedef void *window_t;

// Creates a new webview instance. If debug is non-zero - developer tools will
// be enabled (if the platform supports them). Window parameter can be a
Expand Down Expand Up @@ -107,6 +108,9 @@ WEBVIEW_API void webview_bind(webview_t w, const char *name,
WEBVIEW_API void webview_return(webview_t w, const char *seq, int status,
const char *result);

// TODO doc
WEBVIEW_API void window_set_fullscreen(void *window, int fullscreen);

#ifdef __cplusplus
}
#endif
Expand Down Expand Up @@ -480,7 +484,7 @@ class gtk_webkit_engine {

gtk_widget_show_all(m_window);
}
void *window() { return (void *)m_window; }
window_t window() { return &m_window; }
void run() { gtk_main(); }
void terminate() { gtk_main_quit(); }
void dispatch(std::function<void()> f) {
Expand Down Expand Up @@ -541,6 +545,26 @@ using browser_engine = gtk_webkit_engine;

} // namespace webview

namespace window {
class linux_window {
public:
linux_window(void *wnd) { m_window = static_cast<GtkWidget *>(wnd); }

void set_fullscreen(bool fullscreen) {
if (fullscreen) {
gtk_window_fullscreen(GTK_WINDOW(m_window));
} else {
gtk_window_unfullscreen(GTK_WINDOW(m_window));
}
}

private:
GtkWidget *m_window;
};

using native_window = linux_window;
} // namespace window

#elif defined(WEBVIEW_COCOA)

//
Expand All @@ -563,6 +587,7 @@ using browser_engine = gtk_webkit_engine;
#define NSWindowStyleMaskMiniaturizable 4
#define NSWindowStyleMaskTitled 1
#define NSWindowStyleMaskClosable 2
#define NSWindowStyleMaskFullScreen (1 << 14)

#define NSApplicationActivationPolicyRegular 0

Expand Down Expand Up @@ -651,7 +676,7 @@ class cocoa_wkwebview_engine {
objc_msgSend(m_window, "makeKeyAndOrderFront:"_sel, nullptr);
}
~cocoa_wkwebview_engine() { close(); }
void *window() { return (void *)m_window; }
window_t window() { return &m_window; }
void terminate() {
close();
objc_msgSend("NSApp"_cls, "terminate:"_sel, nullptr);
Expand Down Expand Up @@ -729,6 +754,31 @@ using browser_engine = cocoa_wkwebview_engine;

} // namespace webview

namespace window {
class mac_window {
public:
mac_window(void *wnd) { m_window = (id)wnd; }

void set_fullscreen(bool fullscreen) {
auto windowStyleMask =
(unsigned long)objc_msgSend(m_window, sel_registerName("styleMask"));

auto b = ((windowStyleMask & NSWindowStyleMaskFullScreen) ==
NSWindowStyleMaskFullScreen)
? true
: false;
if (b != fullscreen) {
objc_msgSend(m_window, sel_registerName("toggleFullScreen:"), NULL);
}
}

private:
id m_window;
};

using native_window = mac_window;
} // namespace window

#elif defined(WEBVIEW_EDGE)

//
Expand Down Expand Up @@ -1082,7 +1132,7 @@ class win32_edge_engine {
}
}
}
void *window() { return (void *)m_window; }
window_t window() { return &m_window; }
void terminate() { PostQuitMessage(0); }
void dispatch(dispatch_fn_t f) {
PostThreadMessage(m_main_thread, WM_APP, 0, (LPARAM) new dispatch_fn_t(f));
Expand Down Expand Up @@ -1138,6 +1188,54 @@ class win32_edge_engine {
using browser_engine = win32_edge_engine;
} // namespace webview

namespace window {
class windows_window {
public:
windows_window(void *wnd) { m_window = *(static_cast<HWND *>(wnd)); }

void set_fullscreen(bool fullscreen) {
saved_style = GetWindowLong(m_window, GWL_STYLE);
saved_ex_style = GetWindowLong(m_window, GWL_EXSTYLE);
GetWindowRect(m_window, &saved_rect);
if (fullscreen) {
MONITORINFO monitor_info;
SetWindowLong(m_window, GWL_STYLE,
saved_style & ~(WS_CAPTION | WS_THICKFRAME));
SetWindowLong(m_window, GWL_EXSTYLE,
saved_ex_style & ~(WS_EX_DLGMODALFRAME | WS_EX_WINDOWEDGE |
WS_EX_CLIENTEDGE | WS_EX_STATICEDGE));
monitor_info.cbSize = sizeof(monitor_info);
GetMonitorInfo(MonitorFromWindow(m_window, MONITOR_DEFAULTTONEAREST),
&monitor_info);
RECT r;
r.left = monitor_info.rcMonitor.left;
r.top = monitor_info.rcMonitor.top;
r.right = monitor_info.rcMonitor.right;
r.bottom = monitor_info.rcMonitor.bottom;
SetWindowPos(m_window, NULL, r.left, r.top, r.right - r.left,
r.bottom - r.top,
SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
} else {
SetWindowLong(m_window, GWL_STYLE, saved_style);
SetWindowLong(m_window, GWL_EXSTYLE, saved_ex_style);
SetWindowPos(m_window, NULL, saved_rect.left, saved_rect.top,
saved_rect.right - saved_rect.left,
saved_rect.bottom - saved_rect.top,
SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
}
}

private:
HWND m_window;
bool is_fullscreen = false;
DWORD saved_style = 0;
DWORD saved_ex_style = 0;
RECT saved_rect;
};

using native_window = windows_window;
} // namespace window

#endif /* WEBVIEW_GTK, WEBVIEW_COCOA, WEBVIEW_EDGE */

namespace webview {
Expand Down Expand Up @@ -1227,6 +1325,13 @@ class webview : public browser_engine {
};
} // namespace webview

namespace window {
class window : public native_window {
public:
window(void *wnd) : native_window(wnd) {}
};
} // namespace window

WEBVIEW_API webview_t webview_create(int debug, void *wnd) {
return new webview::webview(debug, wnd);
}
Expand Down Expand Up @@ -1290,6 +1395,10 @@ WEBVIEW_API void webview_return(webview_t w, const char *seq, int status,
static_cast<webview::webview *>(w)->resolve(seq, status, result);
}

WEBVIEW_API void window_set_fullscreen(webview_t wnd, int fullscreen) {
static_cast<window::window *>(wnd)->set_fullscreen(fullscreen);
}

#endif /* WEBVIEW_HEADER */

#endif /* WEBVIEW_H */
2 changes: 2 additions & 0 deletions webview_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ static void test_c_api() {
webview_navigate(w, "https://github.com/zserge/webview");
webview_dispatch(w, cb_assert_arg, (void *)"arg");
webview_dispatch(w, cb_terminate, nullptr);
window_t wnd = webview_get_window(w);
window_set_fullscreen(wnd, 1);
webview_run(w);
webview_destroy(w);
}
Expand Down