Skip to content

Commit

Permalink
Adding menu support. Fixes tryphotino#44.
Browse files Browse the repository at this point in the history
  • Loading branch information
sinterdev committed Nov 26, 2024
1 parent 57552aa commit 915e92e
Show file tree
Hide file tree
Showing 9 changed files with 1,402 additions and 5 deletions.
234 changes: 233 additions & 1 deletion Photino.Native/Exports.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "Photino.Dialog.h"
#include "Photino.Errors.h"
#include "Photino.Menu.h"
#include "Photino.h"
#include <exception>

#ifdef _WIN32
#define EXPORTED __declspec(dllexport)
Expand Down Expand Up @@ -292,6 +295,235 @@ extern "C"
return inst->GetDialog()->ShowMessage(title, text, buttons, icon);
}

//Error
EXPORTED void Photino_ClearErrorMessage()
{
ClearErrorMessage();
}

EXPORTED void Photino_GetErrorMessage(int length, char* buffer)
{
GetErrorMessage(length, buffer);
}

EXPORTED void Photino_GetErrorMessageLength(int* length)
{
*length = GetErrorMessageLength();
}

#ifdef _WIN32
//Menu
EXPORTED PhotinoErrorKind Photino_Menu_Create(Menu** menu)
{
try
{
*menu = new Menu();
}
catch (std::exception exception)
{
SetErrorMessage(exception.what());
return PhotinoErrorKind::GenericError;
}

return PhotinoErrorKind::NoError;
}

EXPORTED PhotinoErrorKind Photino_Menu_Destroy(Menu* menu)
{
try
{
menuRenderer.Destroy(menu);
}
catch (std::exception exception)
{
SetErrorMessage(exception.what());
return PhotinoErrorKind::GenericError;
}

return PhotinoErrorKind::NoError;
}

EXPORTED PhotinoErrorKind Photino_Menu_AddMenuItem(Menu* menu, MenuItem* menuItem)
{
try
{
menu->Add(menuItem);
}
catch (std::exception exception)
{
SetErrorMessage(exception.what());
return PhotinoErrorKind::GenericError;
}

return PhotinoErrorKind::NoError;
}

EXPORTED PhotinoErrorKind Photino_Menu_AddMenuSeparator(Menu* menu, MenuSeparator* menuSeparator)
{
try
{
menu->Add(menuSeparator);
}
catch (std::exception exception)
{
SetErrorMessage(exception.what());
return PhotinoErrorKind::GenericError;
}

return PhotinoErrorKind::NoError;
}

EXPORTED PhotinoErrorKind Photino_Menu_AddOnClicked(Menu* menu, OnClickedCallback onClicked)
{
try
{
menu->AddOnClicked(onClicked);
}
catch (std::exception exception)
{
SetErrorMessage(exception.what());
return PhotinoErrorKind::GenericError;
}

return PhotinoErrorKind::NoError;
}

EXPORTED PhotinoErrorKind Photino_Menu_Hide(Menu* menu)
{
try
{
menu->Hide();
}
catch (std::exception exception)
{
SetErrorMessage(exception.what());
return PhotinoErrorKind::GenericError;
}

return PhotinoErrorKind::NoError;
}

EXPORTED PhotinoErrorKind Photino_Menu_RemoveOnClicked(Menu* menu, OnClickedCallback onClicked)
{
try
{
menu->RemoveOnClicked(onClicked);
}
catch (std::exception exception)
{
SetErrorMessage(exception.what());
return PhotinoErrorKind::GenericError;
}

return PhotinoErrorKind::NoError;
}

EXPORTED PhotinoErrorKind Photino_Menu_Show(Menu* menu, Photino* window, int x, int y)
{
try
{
menu->Show(window, x, y);
}
catch (std::exception exception)
{
SetErrorMessage(exception.what());
return PhotinoErrorKind::GenericError;
}

return PhotinoErrorKind::NoError;
}

EXPORTED PhotinoErrorKind Photino_MenuItem_Create(MenuItemOptions options, MenuItem** menuItem)
{
try
{
*menuItem = new MenuItem(options);
}
catch (std::exception exception)
{
SetErrorMessage(exception.what());
return PhotinoErrorKind::GenericError;
}

return PhotinoErrorKind::NoError;
}

EXPORTED PhotinoErrorKind Photino_MenuItem_AddMenuItem(MenuItem* menuItem, MenuItem* newMenuItem)
{
try
{
menuItem->Add(newMenuItem);
}
catch (std::exception exception)
{
SetErrorMessage(exception.what());
return PhotinoErrorKind::GenericError;
}

return PhotinoErrorKind::NoError;
}

EXPORTED PhotinoErrorKind Photino_MenuItem_AddMenuSeparator(MenuItem* menuItem, MenuSeparator* menuSeparator)
{
try
{
menuItem->Add(menuSeparator);
}
catch (std::exception exception)
{
SetErrorMessage(exception.what());
return PhotinoErrorKind::GenericError;
}

return PhotinoErrorKind::NoError;
}

EXPORTED PhotinoErrorKind Photino_MenuItem_Destroy(MenuItem* menuItem)
{
try
{
delete menuItem;
}
catch (std::exception exception)
{
SetErrorMessage(exception.what());
return PhotinoErrorKind::GenericError;
}

return PhotinoErrorKind::NoError;
}

EXPORTED PhotinoErrorKind Photino_MenuSeparator_Create(MenuSeparator** menuSeparator)
{
try
{
*menuSeparator = new MenuSeparator();
}
catch (std::exception exception)
{
SetErrorMessage(exception.what());
return PhotinoErrorKind::GenericError;
}

return PhotinoErrorKind::NoError;
}

EXPORTED PhotinoErrorKind Photino_MenuSeparator_Destroy(MenuSeparator* menuSeparator)
{
try
{
delete menuSeparator;
}
catch (std::exception exception)
{
SetErrorMessage(exception.what());
return PhotinoErrorKind::GenericError;
}

return PhotinoErrorKind::NoError;
}
#endif // _WIN32



//Callbacks
Expand Down Expand Up @@ -334,4 +566,4 @@ extern "C"
{
instance->Invoke(callback);
}
}
}
25 changes: 25 additions & 0 deletions Photino.Native/Photino.Errors.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "Photino.Errors.h"
#include <mutex>
#include <string>

static thread_local std::string _lastError;

void ClearErrorMessage() noexcept
{
_lastError.clear();
}

int GetErrorMessageLength() noexcept
{
return _lastError.length();
}

void GetErrorMessage(int length, char* buffer) noexcept
{
_lastError.copy(buffer, length, 0);
}

void SetErrorMessage(std::string message) noexcept
{
_lastError = message;
}
35 changes: 35 additions & 0 deletions Photino.Native/Photino.Errors.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once
#include <string>

/// <summary>
/// The kind of error message that occurred.
/// </summary>
enum class PhotinoErrorKind
{
NoError = 0,
GenericError
};

/// <summary>
/// Clears the last error message to occur.
/// </summary>
void ClearErrorMessage() noexcept;

/// <summary>
/// Gets the length of the message of the last error to occur.
/// </summary>
/// <returns>The length of the last error to occur on this thread.</returns>
int GetErrorMessageLength() noexcept;

/// <summary>
/// Gets the message of the last error to occur.
/// </summary>
/// <param name="length">The length of the buffer.</param>
/// <param name="buffer">The buffer to which the message should be written.</param>
void GetErrorMessage(int length, char* buffer) noexcept;

/// <summary>
/// Sets the message of the last error to occur.
/// </summary>
/// <param name="message">The message.</param>
void SetErrorMessage(std::string message) noexcept;
9 changes: 9 additions & 0 deletions Photino.Native/Photino.Menu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#ifdef __APPLE__
// TODO: Implement this.
#elif __linux__
// TODO: Implement this.
#else
#include "Photino.Windows.Menu.h"
#endif
5 changes: 5 additions & 0 deletions Photino.Native/Photino.Native.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -243,15 +243,18 @@ COPY .\$(OutDir)WebView2Loader.dll ..\Photino.Test\bin\ARM64\Debug\net8.0\</Comm
<ItemGroup>
<ClCompile Include="Exports.cpp" />
<ClCompile Include="Dependencies\wintoastlib.cpp" />
<ClCompile Include="Photino.Errors.cpp" />
<ClCompile Include="Photino.Linux.cpp" />
<ClCompile Include="Photino.Linux.Dialog.cpp" />
<ClCompile Include="Photino.Windows.cpp" />
<ClCompile Include="Photino.Windows.DarkMode.cpp" />
<ClCompile Include="Photino.Windows.Dialog.cpp" />
<ClCompile Include="Photino.Windows.Menu.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Dependencies\wintoastlib.h" />
<ClInclude Include="json.hpp" />
<ClInclude Include="Photino.Errors.h" />
<ClInclude Include="Photino.h" />
<ClInclude Include="Photino.Mac.AppDelegate.h" />
<ClInclude Include="Photino.Mac.NavigationDelegate.h" />
Expand All @@ -260,6 +263,8 @@ COPY .\$(OutDir)WebView2Loader.dll ..\Photino.Test\bin\ARM64\Debug\net8.0\</Comm
<ClInclude Include="Photino.Mac.WindowDelegate.h" />
<ClInclude Include="Photino.Mac.UrlSchemeHandler.h" />
<ClInclude Include="Photino.Dialog.h" />
<ClInclude Include="Photino.Menu.h" />
<ClInclude Include="Photino.Windows.Menu.h" />
<ClInclude Include="Photino.Windows.ToastHandler.h" />
<ClInclude Include="Photino.Windows.DarkMode.h" />
<ClInclude Include="resource.h" />
Expand Down
Loading

0 comments on commit 915e92e

Please sign in to comment.