Skip to content

Commit 48dbc9d

Browse files
committed
Added toggleBrowserDevTools
1 parent 8701801 commit 48dbc9d

17 files changed

+127
-3
lines changed

MTA10/core/CWebDevTools.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*****************************************************************************
2+
*
3+
* PROJECT: Multi Theft Auto v1.x
4+
* (Shared logic for modifications)
5+
* LICENSE: See LICENSE in the top level directory
6+
* FILE: core/CWebDevTools.cpp
7+
* PURPOSE: Web dev tools class
8+
*
9+
*****************************************************************************/
10+
#include "StdInc.h"
11+
#include "CWebDevTools.h"
12+
13+
bool CWebDevTools::Show ( CWebView* pWebView )
14+
{
15+
auto browser = pWebView->GetCefBrowser ();
16+
17+
// Default settings are suitable
18+
CefBrowserSettings settings;
19+
20+
CefWindowInfo windowInfo;
21+
windowInfo.SetAsPopup ( NULL, "CEF Dev Tools (MTA:SA)" );
22+
23+
// Create independent CefClient (to bypass access restrictions)
24+
CefRefPtr<CefClient> client { new CDevToolsClient };
25+
26+
// ...and show the devtools
27+
browser->GetHost ()->ShowDevTools ( windowInfo, client, settings, CefPoint () );
28+
return true;
29+
}
30+
31+
bool CWebDevTools::Close ( CWebView* pWebView )
32+
{
33+
pWebView->GetCefBrowser ()->GetHost ()->CloseDevTools ();
34+
return true;
35+
}
36+

MTA10/core/CWebDevTools.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*****************************************************************************
2+
*
3+
* PROJECT: Multi Theft Auto v1.x
4+
* (Shared logic for modifications)
5+
* LICENSE: See LICENSE in the top level directory
6+
* FILE: core/CWebDevTools.h
7+
* PURPOSE: Web dev tools class
8+
*
9+
*****************************************************************************/
10+
#pragma once
11+
12+
class CWebDevTools
13+
{
14+
public:
15+
static bool Show ( CWebView* pWebView );
16+
static bool Close ( CWebView* pWebView );
17+
};
18+
19+
class CDevToolsClient : public CefClient
20+
{
21+
public:
22+
CDevToolsClient() = default;
23+
24+
IMPLEMENT_REFCOUNTING(CDevToolsClient);
25+
};

MTA10/core/CWebView.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <cef3/include/cef_parser.h>
1414
#include <cef3/include/cef_task.h>
1515
#include <cef3/include/cef_runnable.h>
16+
#include "CWebDevTools.h"
1617

1718
CWebView::CWebView ( unsigned int uiWidth, unsigned int uiHeight, bool bIsLocal, CWebBrowserItem* pWebBrowserRenderItem, bool bTransparent )
1819
{
@@ -359,6 +360,14 @@ void CWebView::HandleAjaxRequest ( const SString& strURL, CAjaxResourceHandler *
359360
g_pCore->GetWebCore ()->AddEventToEventQueue ( func, this, "AjaxResourceRequest" );
360361
}
361362

363+
bool CWebView::ToggleDevTools ( bool visible )
364+
{
365+
if ( visible )
366+
return CWebDevTools::Show ( this );
367+
368+
return CWebDevTools::Close ( this );
369+
}
370+
362371
bool CWebView::VerifyFile ( const SString& strPath )
363372
{
364373
return m_pEventsInterface->Events_OnResourceFileCheck ( strPath );

MTA10/core/CWebView.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ class CWebView : public CWebViewInterface, private CefClient, private CefRenderH
8181
virtual bool HasAjaxHandler ( const SString& strURL );
8282
virtual void HandleAjaxRequest ( const SString& strURL, class CAjaxResourceHandler * pHandler );
8383

84+
virtual bool ToggleDevTools ( bool visible ) override;
85+
8486
// CefClient methods
8587
virtual CefRefPtr<CefRenderHandler> GetRenderHandler() override { return this; };
8688
virtual CefRefPtr<CefLoadHandler> GetLoadHandler() override { return this; };

MTA10/core/_Core 2008.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@
317317
<ClCompile Include="CVertexStreamBoundingBoxManager.cpp" />
318318
<ClCompile Include="CVideoModeManager.cpp" />
319319
<ClCompile Include="CWebCore.cpp" />
320+
<ClCompile Include="CWebDevTools.cpp" />
320321
<ClCompile Include="CWebsiteRequests.cpp" />
321322
<ClCompile Include="CWebView.cpp" />
322323
<ClCompile Include="StdInc.cpp">
@@ -475,6 +476,7 @@
475476
<ClInclude Include="CVertexStreamBoundingBoxManager.h" />
476477
<ClInclude Include="CVideoModeManager.h" />
477478
<ClInclude Include="CWebCore.h" />
479+
<ClInclude Include="CWebDevTools.h" />
478480
<ClInclude Include="CWebsiteRequests.h" />
479481
<ClInclude Include="CWebView.h" />
480482
<ClInclude Include="StdInc.h" />

MTA10/core/_Core 2008.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,9 @@
356356
<ClCompile Include="CAjaxResourceHandler.cpp">
357357
<Filter>Source Files</Filter>
358358
</ClCompile>
359+
<ClCompile Include="CWebDevTools.cpp">
360+
<Filter>Source Files</Filter>
361+
</ClCompile>
359362
</ItemGroup>
360363
<ItemGroup>
361364
<ClInclude Include="CAdditionalVertexStreamManager.h">
@@ -1252,5 +1255,8 @@
12521255
<ClInclude Include="CAjaxResourceHandler.h">
12531256
<Filter>Header Files</Filter>
12541257
</ClInclude>
1258+
<ClInclude Include="CWebDevTools.h">
1259+
<Filter>Header Files</Filter>
1260+
</ClInclude>
12551261
</ItemGroup>
12561262
</Project>

MTA10/mods/shared_logic/CClientWebBrowser.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,11 @@ bool CClientWebBrowser::RemoveAjaxHandler ( const SString& strURL )
294294
return m_mapAjaxCallback.erase ( strURL ) == 1;
295295
}
296296

297+
bool CClientWebBrowser::ToggleDevTools ( bool visible )
298+
{
299+
return m_pWebView->ToggleDevTools ( visible );
300+
}
301+
297302

298303
CClientGUIWebBrowser::CClientGUIWebBrowser ( bool isLocal, bool isTransparent, uint width, uint height, CClientManager* pManager, CLuaMain* pLuaMain, CGUIElement* pCGUIElement, ElementID ID )
299304
: CClientGUIElement ( pManager, pLuaMain, pCGUIElement, ID )

MTA10/mods/shared_logic/CClientWebBrowser.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ class CClientWebBrowser : public CClientTexture, public CWebBrowserEventsInterfa
5858

5959
bool AddAjaxHandler ( const SString& strURL, ajax_callback_t& handler );
6060
bool RemoveAjaxHandler ( const SString& strURL );
61+
62+
bool ToggleDevTools ( bool visible );
6163

6264
// CWebBrowserEventsInterface implementation
6365
void Events_OnCreated () override;

MTA10/mods/shared_logic/lua/CLuaFunctionDefs.Browser.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,33 @@ int CLuaFunctionDefs::GetBrowserSource ( lua_State* luaVM )
588588
return 1;
589589
}
590590

591+
int CLuaFunctionDefs::ToggleBrowserDevTools ( lua_State* luaVM )
592+
{
593+
// bool toggleBrowserDevTools ( browser webBrowser, bool visible )
594+
CClientWebBrowser* pWebBrowser; bool visible;
595+
596+
CScriptArgReader argStream ( luaVM );
597+
argStream.ReadUserData ( pWebBrowser );
598+
argStream.ReadBool ( visible );
599+
600+
if ( !argStream.HasErrors () )
601+
{
602+
if ( g_pCore->GetWebCore ()->IsTestModeEnabled () )
603+
{
604+
lua_pushboolean ( luaVM, pWebBrowser->ToggleDevTools ( visible ) );
605+
return 1;
606+
}
607+
else
608+
m_pScriptDebugging->LogCustom ( luaVM, "toggleBrowserDevtools can only be used in development mode" );
609+
610+
}
611+
else
612+
m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );
613+
614+
lua_pushboolean ( luaVM, false );
615+
return 1;
616+
}
617+
591618
int CLuaFunctionDefs::GUICreateBrowser ( lua_State* luaVM )
592619
{
593620
// element guiCreateBrowser ( float x, float y, float width, float height, bool isLocal, bool isTransparent, bool relative, [element parent = nil] )

MTA10/mods/shared_logic/lua/CLuaFunctionDefs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,7 @@ class CLuaFunctionDefs
10441044
LUA_DECLARE ( GetBrowserSettings );
10451045
LUA_DECLARE ( GetBrowserSource );
10461046
LUA_DECLARE ( SetBrowserAjaxHandler );
1047+
LUA_DECLARE ( ToggleBrowserDevTools );
10471048
LUA_DECLARE ( GUICreateBrowser );
10481049
LUA_DECLARE ( GUIGetBrowser );
10491050

0 commit comments

Comments
 (0)