diff --git a/.gitignore b/.gitignore index 66fd13c..f36daf8 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,6 @@ # Dependency directories (remove the comment below to include it) # vendor/ + +# IDEA +.idea diff --git a/libs/webview/include/webview.h b/libs/webview/include/webview.h index 62ac14b..8e980b6 100644 --- a/libs/webview/include/webview.h +++ b/libs/webview/include/webview.h @@ -254,6 +254,35 @@ WEBVIEW_API void *webview_get_native_handle(webview_t w, */ WEBVIEW_API void webview_set_title(webview_t w, const char *title); +/** + * Updates the title bar of the native window. + * + * @param w The webview instance. + * @param title The title bar Show or hide. + */ +WEBVIEW_API void webview_set_title_bar(webview_t w, int b); + +/** + * Set the native window maximize. + * + * @param w The webview instance. + */ +WEBVIEW_API void webview_set_maximize(webview_t w); + +/** + * Set the native window un maximize. + * + * @param w The webview instance. + */ +WEBVIEW_API void webview_set_un_maximize(webview_t w); + +/** + * Set the native window minimize. + * + * @param w The webview instance. + */ +WEBVIEW_API void webview_set_minimize(webview_t w); + /** * Updates the size of the native window. * @@ -265,6 +294,28 @@ WEBVIEW_API void webview_set_title(webview_t w, const char *title); WEBVIEW_API void webview_set_size(webview_t w, int width, int height, webview_hint_t hints); +/** + * Updates the drag of the native window. + * + * @param w The webview instance. + */ +WEBVIEW_API void webview_start_dragging(webview_t w); + +/** + * Set Always On Top of the native window. + * + * @param w The webview instance. + * @param b bool. + */ +WEBVIEW_API void webview_set_always_on_top(webview_t w,int b); +/** + * Set Always On Top of the native window. + * + * @param w The webview instance. + * @param b bool. + */ +WEBVIEW_API void webview_set_resizable(webview_t w,int b); + /** * Navigates webview to the given URL. URL may be a properly encoded data URI. * @@ -1001,11 +1052,27 @@ if (status === 0) {\ void terminate() { terminate_impl(); } void dispatch(std::function f) { dispatch_impl(f); } void set_title(const std::string &title) { set_title_impl(title); } + void set_title_bar(int b) { set_title_bar_impl(b); } + void set_maximize() { set_maximize_impl(); } + void set_un_maximize() { set_un_maximize_impl(); } + void set_minimize() { set_minimize_impl(); } void set_size(int width, int height, webview_hint_t hints) { set_size_impl(width, height, hints); } + void start_dragging() { + start_dragging_impl(); + } + + void set_always_on_top(int b) { + set_always_on_top_impl(b); + } + + void set_resizable(int b) { + set_resizable_impl(b); + } + void set_html(const std::string &html) { set_html_impl(html); } void init(const std::string &js) { init_impl(js); } void eval(const std::string &js) { eval_impl(js); } @@ -1019,7 +1086,14 @@ if (status === 0) {\ virtual void terminate_impl() = 0; virtual void dispatch_impl(std::function f) = 0; virtual void set_title_impl(const std::string &title) = 0; + virtual void set_title_bar_impl(int b) = 0; + virtual void set_maximize_impl() = 0; + virtual void set_un_maximize_impl() = 0; + virtual void set_minimize_impl() = 0; virtual void set_size_impl(int width, int height, webview_hint_t hints) = 0; + virtual void start_dragging_impl() = 0; + virtual void set_always_on_top_impl(int b) = 0; + virtual void set_resizable_impl(int b) = 0; virtual void set_html_impl(const std::string &html) = 0; virtual void init_impl(const std::string &js) = 0; virtual void eval_impl(const std::string &js) = 0; @@ -1335,6 +1409,14 @@ class gtk_webkit_engine : public engine_base { gtk_window_set_title(GTK_WINDOW(m_window), title.c_str()); } + void set_title_bar_impl(int b) override {} + + void set_maximize_impl() override {} + + void set_un_maximize_impl() override {} + + void set_minimize_impl() override {} + void set_size_impl(int width, int height, webview_hint_t hints) override { gtk_window_set_resizable(GTK_WINDOW(m_window), hints != WEBVIEW_HINT_FIXED); if (hints == WEBVIEW_HINT_NONE) { @@ -1351,6 +1433,10 @@ class gtk_webkit_engine : public engine_base { gtk_window_set_geometry_hints(GTK_WINDOW(m_window), nullptr, &g, h); } } + void start_dragging_impl() override {} + + void set_always_on_top_impl(int b) override {} + void set_resizable_impl(int b) override {} void navigate_impl(const std::string &url) override { webkit_web_view_load_uri(WEBKIT_WEB_VIEW(m_webview), url.c_str()); @@ -1655,6 +1741,15 @@ class cocoa_wkwebview_engine : public engine_base { "stringWithUTF8String:"_sel, title.c_str())); } + + void set_title_bar_impl(int b) override {} + + void set_maximize_impl() override {} + + void set_un_maximize_impl() override {} + + void set_minimize_impl() override {} + void set_size_impl(int width, int height, webview_hint_t hints) override { objc::autoreleasepool arp; @@ -1679,6 +1774,9 @@ class cocoa_wkwebview_engine : public engine_base { } objc::msg_send(m_window, "center"_sel); } + void start_dragging_impl() override {} + void set_always_on_top_impl(int b) override {} + void set_resizable_impl(int b) override {} void navigate_impl(const std::string &url) override { objc::autoreleasepool arp; @@ -3263,6 +3361,31 @@ class win32_edge_engine : public engine_base { SetWindowTextW(m_window, widen_string(title).c_str()); } + + void set_title_bar_impl(int b) override { + auto style = GetWindowLong(m_window, GWL_STYLE); + if (b == 0){ + style &= ~WS_CAPTION; + }else{ + style |= WS_CAPTION; + } + SetWindowLong(m_window, GWL_STYLE, style); + SetWindowPos(m_window, nullptr, 0, 0, 0, 0, + SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED); + } + + void set_maximize_impl() override { + PostMessage(m_window, WM_SYSCOMMAND, SC_MAXIMIZE, 0); + } + + void set_un_maximize_impl() override { + PostMessage(m_window, WM_SYSCOMMAND, SC_RESTORE, 0); + } + + void set_minimize_impl() override { + PostMessage(m_window, WM_SYSCOMMAND, SC_MINIMIZE, 0); + } + void set_size_impl(int width, int height, webview_hint_t hints) override { auto style = GetWindowLong(m_window, GWL_STYLE); if (hints == WEBVIEW_HINT_FIXED) { @@ -3270,6 +3393,7 @@ class win32_edge_engine : public engine_base { } else { style |= (WS_THICKFRAME | WS_MAXIMIZEBOX); } + SetWindowLong(m_window, GWL_STYLE, style); if (hints == WEBVIEW_HINT_MAX) { @@ -3291,6 +3415,25 @@ class win32_edge_engine : public engine_base { } } + void start_dragging_impl() override { + ReleaseCapture(); + SendMessage(m_window, WM_SYSCOMMAND, SC_MOVE| HTCAPTION, 0); + } + + void set_always_on_top_impl(int b) override { + SetWindowPos(m_window, b == 0 ? HWND_NOTOPMOST : HWND_TOPMOST,0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); + } + + void set_resizable_impl(int b) override { + auto style = GetWindowLong(m_window, GWL_STYLE); + if (b == 0) { + style &= ~WS_THICKFRAME; + } else { + style |= WS_THICKFRAME; + } + ::SetWindowLong(m_window, GWL_STYLE, style); + } + void navigate_impl(const std::string &url) override { auto wurl = widen_string(url); m_webview->Navigate(wurl.c_str()); @@ -3547,11 +3690,39 @@ WEBVIEW_API void webview_set_title(webview_t w, const char *title) { static_cast(w)->set_title(title); } + +WEBVIEW_API void webview_set_title_bar(webview_t w, int b) { + static_cast(w)->set_title_bar(b); +} + +WEBVIEW_API void webview_set_maximize(webview_t w) { + static_cast(w)->set_maximize(); +} + +WEBVIEW_API void webview_set_un_maximize(webview_t w) { + static_cast(w)->set_un_maximize(); +} + +WEBVIEW_API void webview_set_minimize(webview_t w) { + static_cast(w)->set_minimize(); +} + WEBVIEW_API void webview_set_size(webview_t w, int width, int height, webview_hint_t hints) { static_cast(w)->set_size(width, height, hints); } +WEBVIEW_API void webview_start_dragging(webview_t w) { + static_cast(w)->start_dragging(); +} + +WEBVIEW_API void webview_set_always_on_top(webview_t w, int b) { + static_cast(w)->set_always_on_top(b); +} +WEBVIEW_API void webview_set_resizable(webview_t w, int b) { + static_cast(w)->set_resizable(b); +} + WEBVIEW_API void webview_navigate(webview_t w, const char *url) { static_cast(w)->navigate(url); } diff --git a/webview.go b/webview.go index 7cf0d9f..c39c37d 100644 --- a/webview.go +++ b/webview.go @@ -25,12 +25,12 @@ void CgoWebViewUnbind(webview_t w, const char *name); */ import "C" import ( + "encoding/json" + "errors" _ "github.com/webview/webview_go/libs/mswebview2" _ "github.com/webview/webview_go/libs/mswebview2/include" _ "github.com/webview/webview_go/libs/webview" _ "github.com/webview/webview_go/libs/webview/include" - "encoding/json" - "errors" "reflect" "runtime" "sync" @@ -86,9 +86,30 @@ type WebView interface { // thread. SetTitle(title string) + // SetTitleBar show or hide title bar + SetTitleBar(b bool) + + // SetMaximize set native window maximize + SetMaximize() + + // SetUnMaximize set native window unmaximize + SetUnMaximize() + + // SetMinimize set native window minimize + SetMinimize() + // SetSize updates native window size. See Hint constants. SetSize(w int, h int, hint Hint) + // StartDragging Drag native window. + StartDragging() + + // SetAlwaysOnTop set always on top native window. + SetAlwaysOnTop(b bool) + + // SetResizable set resizable native window. + SetResizable(b bool) + // Navigate navigates webview to the given URL. URL may be a properly encoded data. // URI. Examples: // w.Navigate("https://github.com/webview/webview") @@ -192,10 +213,38 @@ func (w *webview) SetTitle(title string) { C.webview_set_title(w.w, s) } +func (w *webview) SetTitleBar(b bool) { + C.webview_set_title_bar(w.w, boolToInt(b)) +} + +func (w *webview) SetMaximize() { + C.webview_set_maximize(w.w) +} + +func (w *webview) SetUnMaximize() { + C.webview_set_un_maximize(w.w) +} + +func (w *webview) SetMinimize() { + C.webview_set_minimize(w.w) +} + func (w *webview) SetSize(width int, height int, hint Hint) { C.webview_set_size(w.w, C.int(width), C.int(height), C.webview_hint_t(hint)) } +func (w *webview) StartDragging() { + C.webview_start_dragging(w.w) +} + +func (w *webview) SetAlwaysOnTop(b bool) { + C.webview_set_always_on_top(w.w, boolToInt(b)) +} + +func (w *webview) SetResizable(b bool) { + C.webview_set_resizable(w.w, boolToInt(b)) +} + func (w *webview) Init(js string) { s := C.CString(js) defer C.free(unsafe.Pointer(s))