Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
stuerp committed Dec 2, 2024
1 parent 3b274c9 commit 1c31c14
Show file tree
Hide file tree
Showing 8 changed files with 347 additions and 229 deletions.
77 changes: 77 additions & 0 deletions HostObjectImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,37 @@ const std::string Stringify(const char * s)
return t;
}

/// <summary>
/// Escapes all restricted JSON characters in a string.
/// </summary>
const std::wstring Stringify(const std::wstring & s)
{
std::wstring t;

t.reserve(s.length());

for (auto & Iter : s)
{
switch (Iter)
{
case '"': t += '\\'; t += '\"'; break;
case '\\': t += '\\'; t += '\\'; break;
case '\b': t += '\\'; t += 'b'; break;
case '\t': t += '\\'; t += 't'; break;
case '\n': t += '\\'; t += 'n'; break;
case '\f': t += '\\'; t += 'f'; break;
case '\r': t += '\\'; t += 'r'; break;
default:
if ('\x00' <= Iter && Iter <= '\x1f')
t += ::FormatText(L"\\u04x", Iter).c_str();
else
t += Iter;
}
}

return t;
}

/// <summary>
/// Converts a metadb handle list to a JSON string.
/// </summary>
Expand All @@ -756,3 +787,49 @@ std::wstring ToJSON(const metadb_handle_list & hItems)

return Result;
}

/// <summary>
/// Converts a bit array to a JSON string.
/// </summary>
std::wstring ToJSON(const bit_array & mask, t_size count)
{
std::wstring Result = L"[";
bool IsFirstItem = true;

for (t_size Iter = mask.find_first(true, 0, count); Iter < count; Iter = mask.find_next(true, Iter, count))
{
if (!IsFirstItem)
Result.append(L",");

Result.append(::FormatText(LR"(%d)", Iter).c_str());

IsFirstItem = false;
}

Result.append(L"]");

return Result;
}

/// <summary>
/// Converts a t_size array to a JSON string.
/// </summary>
std::wstring ToJSON(const t_size * array, t_size count)
{
std::wstring Result = L"[";
bool IsFirstItem = true;

for (t_size i = 0; i < count; ++i)
{
if (!IsFirstItem)
Result.append(L",");

Result.append(::FormatText(LR"(%d)", (int) *array++).c_str());

IsFirstItem = false;
}

Result.append(L"]");

return Result;
}
4 changes: 4 additions & 0 deletions HostObjectImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,8 @@ class HostObject : public Microsoft::WRL::RuntimeClass<Microsoft::WRL::RuntimeCl

extern void ToBase64(const BYTE * data, DWORD size, BSTR * base64);
extern const std::string Stringify(const char * s);
extern const std::wstring Stringify(const std::wstring & s);

extern std::wstring ToJSON(const metadb_handle_list & hItems);
extern std::wstring ToJSON(const bit_array & mask, t_size count);
extern std::wstring ToJSON(const t_size * array, t_size count);
32 changes: 18 additions & 14 deletions PlaylistTemplate.html
Original file line number Diff line number Diff line change
Expand Up @@ -285,27 +285,37 @@
});

// Called when items have been added to the specified playlist.
function onPlaylistItemsAdded(playlistIndex)
function onPlaylistItemsAdded(playlistIndex, startIndex, locations)
{
document.getElementById("onPlaylistItemsAddedResult").textContent = "Items added to playlist " + playlistIndex + " (" + Now() + ")";
document.getElementById("onPlaylistItemsAddedResult").textContent = "Items added at index " + startIndex + " to playlist " + playlistIndex + " (" + Now() + ")";

const Items = JSON.parse(locations);

document.getElementById("readDirectoryResult").innerHTML = ArrayToTable(Items);
}

// Called when the items of the specified playlist have been reordered.
function onPlaylistItemsReordered(playlistIndex)
function onPlaylistItemsReordered(playlistIndex, items)
{
document.getElementById("onPlaylistItemsReorderedResult").textContent = "Items of playlist " + playlistIndex + " have been reordered (" + Now() + ")";
document.getElementById("onPlaylistItemsReorderedResult").textContent = "Items of playlist " + playlistIndex + " have been reordered as " + items + " (" + Now() + ")";
}

// Called when removing items from the specified playlist.
function onPlaylistItemsRemoving(playlistIndex)
function onPlaylistItemsRemoving(playlistIndex, removedItems, newCount)
{
document.getElementById("onPlaylistItemsRemovingResult").textContent = "Items being removed from playlist " + playlistIndex + " (" + Now() + ")";
document.getElementById("onPlaylistItemsRemovingResult").textContent = "Items " + removedItems + " being removed from playlist " + playlistIndex + " (" + Now() + ")";
}

// Called when items have been removed from the specified playlist.
function onPlaylistItemsRemoved(playlistIndex)
function onPlaylistItemsRemoved(playlistIndex, removedItems, newCount)
{
document.getElementById("onPlaylistItemsRemovedResult").textContent = "Items removed from playlist " + playlistIndex + " (" + Now() + ")";
document.getElementById("onPlaylistItemsRemovedResult").textContent = "Items " + removedItems + " removed from playlist " + playlistIndex + " (" + Now() + ")";
}

// Called when the selected items changed.
function onPlaylistSelectedItemsChanged(playlistIndex, selection)
{
document.getElementById("onPlaylistSelectedItemsChangedResult").textContent = "Selected items changed to " + selection + " in playlist " + playlistIndex + " (" + Now() + ")";
}

// Called when some playlist items of the specified playlist have been modified.
Expand Down Expand Up @@ -362,12 +372,6 @@
document.getElementById("onPlaylistUnlockedResult").textContent = "Playlist " + playlistIndex + " has been unlocked (" + Now() + ")";
}

// Called when the selected items changed.
function onPlaylistSelectedItemsChanged(playlistIndex)
{
document.getElementById("onPlaylistSelectedItemsChangedResult").textContent = "Selected items changed in playlist " + playlistIndex + " (" + Now() + ")";
}

// Called when the focused item of a playlist changed.
function onPlaylistFocusedItemChanged(playlistIndex, fromIndex, toIndex)
{
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ v0.2.0.0-alpha5, 2024-12-xx, *"I'm getting framed..."*
* playbackOrder: Gets or sets the playback order (0 = default, 1 = repeat playlist, 2 = repeat track, 3 = random, 4 = shuffle tracks, 5 = shuffle albums, 6 = shuffle folders). (alpha2)

* Callbacks
* onPlaylistItemsAdded(playlistIndex): Called when items have been added to the specified playlist. (alpha4)
* onPlaylistItemsReordered(playlistIndex): Called when the items of the specified playlist have been reordered. (alpha4)
* onPlaylistItemsRemoving(playlistIndex): Called when removing items of the specified playlist. (alpha4)
* onPlaylistItemsRemoved(playlistIndex): Called when items of the specified playlist have been removed. (alpha4)
* onPlaylistItemsAdded(playlistIndex, startindex, locations): Called when items have been added to the specified playlist. (alpha5)
* onPlaylistItemsReordered(playlistIndex, items): Called when the items of the specified playlist have been reordered. (alpha5)
* onPlaylistItemsRemoving(playlistIndex, removedItems, newCount): Called when removing items of the specified playlist. (alpha5)
* onPlaylistItemsRemoved(playlistIndex, removedItems, newCount): Called when items of the specified playlist have been removed. (alpha5)
* onPlaylistItemsModified(playlistIndex): Called when some playlist items of the specified playlist have been modified. (alpha4)
* onPlaylistItemsModifiedFromPlayback(playlistIndex): Called when some playlist items of the specified playlist have been modified from playback. (alpha4)
* onPlaylistItemsReplaced(playlistIndex): Called when items of the specified playlist have been replaced. (alpha4)
Expand Down
208 changes: 0 additions & 208 deletions UIElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -553,211 +553,3 @@ void UIElement::on_volume_change(float newValue) // in dBFS
}

#pragma endregion

#pragma region playlist_callback

/// <summary>
/// Called when items have been added to the specified playlist.
/// </summary>
void UIElement::on_items_added(t_size playlistIndex, t_size startIndex, metadb_handle_list_cref data, const bit_array & selection)
{
const std::wstring Script = ::FormatText(L"onPlaylistItemsAdded(%d)", (int) playlistIndex);

ExecuteScript(Script);
}

/// <summary>
/// Called when the items of the specified playlist have been reordered.
/// </summary>
void UIElement::on_items_reordered(t_size playlistIndex, const t_size * order, t_size count)
{
const std::wstring Script = ::FormatText(L"onPlaylistItemsReordered(%d)", (int) playlistIndex);

ExecuteScript(Script);
}

/// <summary>
/// Called when removing items of the specified playlist.
/// </summary>
void UIElement::on_items_removing(t_size playlistIndex, const bit_array & mask, t_size oldCount, t_size newCount)
{
const std::wstring Script = ::FormatText(L"onPlaylistItemsRemoving(%d)", (int) playlistIndex);

ExecuteScript(Script);
}

/// <summary>
/// Called when items of the active playlist have been removed.
/// </summary>
void UIElement::on_items_removed(t_size playlistIndex, const bit_array & mask, t_size oldCount, t_size newCount)
{
const std::wstring Script = ::FormatText(L"onPlaylistItemsRemoved(%d)", (int) playlistIndex);

ExecuteScript(Script);
}

/// <summary>
/// Called when the selected items changed.
/// </summary>
void UIElement::on_items_selection_change(t_size playlistIndex, const bit_array & affectedItems, const bit_array & state)
{
const std::wstring Script = ::FormatText(L"onPlaylistSelectedItemsChanged(%d)", (int) playlistIndex);

ExecuteScript(Script);
}

/// <summary>
/// Called when the focused item of a playlist changed.
/// </summary>
void UIElement::on_item_focus_change(t_size playlistIndex, t_size fromIndex, t_size toIndex)
{
const std::wstring Script = ::FormatText(L"onPlaylistFocusedItemChanged(%d, %d, %d)", (int) playlistIndex, (int) fromIndex, (int) toIndex);

ExecuteScript(Script);
}

/// <summary>
/// Called when the specified item of a playlist has been ensured to be visible.
/// </summary>
void UIElement::on_item_ensure_visible(t_size playlistIndex, t_size itemIndex)
{
const std::wstring Script = ::FormatText(L"onPlaylistItemEnsureVisible(%d, %d)", (int) playlistIndex, (int) itemIndex);

ExecuteScript(Script);
}

/// <summary>
/// Called when some playlist items of the specified playlist have been modified.
/// </summary>
void UIElement::on_items_modified(t_size playlistIndex, const bit_array & mask)
{
const std::wstring Script = ::FormatText(L"onPlaylistItemsModified(%d)", (int) playlistIndex);

ExecuteScript(Script);
}

/// <summary>
/// Called when some playlist items of the specified playlist have been modified from playback.
/// </summary>
void UIElement::on_items_modified_fromplayback(t_size playlistIndex, const bit_array & mask, play_control::t_display_level displayLevel)
{
const std::wstring Script = ::FormatText(L"onPlaylistItemsModifiedFromPlayback(%d)", (int) playlistIndex);

ExecuteScript(Script);
}

/// <summary>
/// Called when items of the specified playlist have been replaced.
/// </summary>
void UIElement::on_items_replaced(t_size playlistIndex, const bit_array & mask, const pfc::list_base_const_t<playlist_callback::t_on_items_replaced_entry> & replacedItems)
{
const std::wstring Script = ::FormatText(L"onPlaylistItemsReplaced(%d)", (int) playlistIndex);

ExecuteScript(Script);
}

/// <summary>
/// Called when the active playlist changes.
/// </summary>
void UIElement::on_playlist_activate(t_size oldPlaylistIndex, t_size newPlaylistIndex)
{
const std::wstring Script = ::FormatText(L"onPlaylistActivated(%d, %d)", (int) oldPlaylistIndex, (int) newPlaylistIndex);

ExecuteScript(Script);
}

/// <summary>
/// Called when a new playlist has been created.
/// </summary>
void UIElement::on_playlist_created(t_size playlistIndex, const char * name, t_size size)
{
const std::wstring Script = ::FormatText(L"onPlaylistCreated(%d, \"%s\")", (int) playlistIndex, ::UTF8ToWide(name, size).c_str());

ExecuteScript(Script);
}

/// <summary>
/// Called when the playlists have beenn reordered.
/// </summary>
void UIElement::on_playlists_reorder(const t_size * order, t_size count)
{
const std::wstring Script = L"onPlaylistsReordered()";

ExecuteScript(Script);
}

/// <summary>
/// Called when playlists are being removed.
/// </summary>
void UIElement::on_playlists_removing(const bit_array & mask, t_size oldCount, t_size newCount)
{
const std::wstring Script = L"onPlaylistsRemoving()";

ExecuteScript(Script);
}

/// <summary>
/// Called when playlists have been removed.
/// </summary>
void UIElement::on_playlists_removed(const bit_array & mask, t_size oldCount, t_size newcount)
{
const std::wstring Script = L"onPlaylistsRemoved()";

ExecuteScript(Script);
}

/// <summary>
/// Called when the specified playlist has been renamed.
/// </summary>
void UIElement::on_playlist_renamed(t_size playlistIndex, const char * name, t_size size)
{
const std::wstring Script = ::FormatText(L"onPlaylistRenamed(%d, \"%s\")", (int) playlistIndex, ::UTF8ToWide(name, size).c_str());

ExecuteScript(Script);
}

/// <summary>
/// Called when the specified playlist has been locked or unlocked.
/// </summary>
void UIElement::on_playlist_locked(t_size playlistIndex, bool isLocked)
{
const std::wstring Script = ::FormatText(isLocked ? L"onPlaylistLocked(%d)" : L"onPlaylistUnlocked(%d)", (int) playlistIndex);

ExecuteScript(Script);
}

/// <summary>
/// Called when the default format has been changed.
/// </summary>
void UIElement::on_default_format_changed()
{
const std::wstring Script = L"onDefaultFormatChanged()";

ExecuteScript(Script);
}

/// <summary>
/// Called when the playback order changed.
/// </summary>
void UIElement::on_playback_order_changed(t_size playbackOrderIndex)
{
const std::wstring Script = ::FormatText(L"onPlaybackOrderChanged(%d)", (int) playbackOrderIndex);

ExecuteScript(Script);
}

/// <summary>
/// Executes a script.
/// </summary>
void UIElement::ExecuteScript(const std::wstring & script) const noexcept
{
if (_WebView == nullptr)
return;

HRESULT hr = _WebView->ExecuteScript(script.c_str(), nullptr);

if (!SUCCEEDED(hr))
console::print(::GetErrorMessage(hr, ::FormatText(STR_COMPONENT_BASENAME " failed to call %s", ::WideToUTF8(script).c_str())).c_str());
}

#pragma endregion
Loading

0 comments on commit 1c31c14

Please sign in to comment.