Skip to content
Merged
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
8 changes: 1 addition & 7 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,11 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v7

- name: Check out WebViewWindow
uses: actions/checkout@v7
with:
repository: gwdevhub/WebViewWindow
path: WebViewWindow
submodules: true

- name: Build both targets in Docker
run: bash build.sh all
env:
WEBVIEW_WINDOW_DIR: ${{ github.workspace }}/WebViewWindow

- name: Upload the built programs
uses: actions/upload-artifact@v7
Expand Down
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
build/
bin/
.idea/

# WebViewWindow checked out beside this repo in CI, which is not part of this repo.
/WebViewWindow/
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "WebViewWindow"]
path = WebViewWindow
url = https://github.com/gwdevhub/WebViewWindow.git
9 changes: 7 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# WebViewWindow is a sibling checkout by default. Point this at another checkout if it lives elsewhere.
set(WEBVIEW_WINDOW_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../WebViewWindow" CACHE PATH "Path to the WebViewWindow checkout")
# WebViewWindow is the git submodule in this folder by default. Point this at another checkout if it lives elsewhere.
set(WEBVIEW_WINDOW_DIR "${CMAKE_CURRENT_SOURCE_DIR}/WebViewWindow" CACHE PATH "Path to the WebViewWindow checkout")

set(WEBVIEW_WINDOW_BUILD_EXAMPLE OFF CACHE BOOL "" FORCE)
add_subdirectory("${WEBVIEW_WINDOW_DIR}" WebViewWindow)
Expand Down Expand Up @@ -41,6 +41,7 @@ add_executable(discord_webview
src/Sha256.cpp
src/UpdateCheck.cpp
src/main.cpp
src/Downloads.cpp
src/PagePermissions.cpp
${DW_HOST}
)
Expand All @@ -66,4 +67,8 @@ if(DW_BUILD_TESTS)
target_include_directories(permission_policy_test PRIVATE src)
target_link_libraries(permission_policy_test PRIVATE webview_window)
add_test(NAME permission_policy COMMAND permission_policy_test)

add_executable(downloads_test tests/Downloads_test.cpp src/Downloads.cpp)
target_include_directories(downloads_test PRIVATE src)
add_test(NAME downloads COMMAND downloads_test)
endif()
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ contains none of Discord's code: the page is loaded from discord.com at run time

## Build

Needs Docker. The WebViewWindow checkout is expected next to this one (`../WebViewWindow`); point `WEBVIEW_WINDOW_DIR` at another
checkout if it lives elsewhere.
Needs Docker. WebViewWindow is a git submodule in `WebViewWindow/`, so clone with `--recurse-submodules` (or run
`git submodule update --init` after cloning). Point `WEBVIEW_WINDOW_DIR` at another checkout if it lives elsewhere.

./build.sh [linux|windows|all]

Expand Down
1 change: 1 addition & 0 deletions WebViewWindow
Submodule WebViewWindow added at b4b56f
6 changes: 3 additions & 3 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash
# Builds the program in Docker into bin/: build.sh [linux|windows|all]
# WebViewWindow is read from WEBVIEW_WINDOW_DIR, which defaults to a checkout beside this one.
# WebViewWindow is read from WEBVIEW_WINDOW_DIR, which defaults to the submodule in this repo's WebViewWindow folder.
set -euo pipefail

target="${1:-all}"
Expand All @@ -12,9 +12,9 @@ case "$target" in
esac

root="$(cd "$(dirname "$0")" && pwd)"
webview_window_dir="${WEBVIEW_WINDOW_DIR:-$root/../WebViewWindow}"
webview_window_dir="${WEBVIEW_WINDOW_DIR:-$root/WebViewWindow}"
if [ ! -f "$webview_window_dir/CMakeLists.txt" ]; then
echo "WebViewWindow isn't at $webview_window_dir. Check it out there, or set WEBVIEW_WINDOW_DIR to its folder." >&2
echo "WebViewWindow isn't at $webview_window_dir. Run: git submodule update --init, or set WEBVIEW_WINDOW_DIR to its folder." >&2
exit 1
fi

Expand Down
15 changes: 9 additions & 6 deletions docs/MISSING_FEATURES.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ are the keys of that object. The main-process side of the channels (`bundle.js`)

| Feature | Discord bridge | Status |
| --- | --- | --- |
| Desktop notifications | `ipc`: `NOTIFICATION_SHOW`, `NOTIFICATION_CLOSE`, `NOTIFICATIONS_*` | Partial: the page's Notification API is allowed for discord.com, so `Notification.permission` is `granted`. Nothing from the `ipc` channels is used, so the notifications are the WebView's own. |
| Unread badge on the taskbar or dock | `ipc`: `APP_SET_BADGE_COUNT`, `APP_DOCK_SET_BADGE`, `APP_DOCK_BOUNCE` | Missing. |
| Desktop notifications | `ipc`: `NOTIFICATION_SHOW`, `NOTIFICATION_CLOSE`, `NOTIFICATIONS_*` | Done on Linux: the page's Notification API is allowed for discord.com, and WebKitGTK presents the notification to the desktop (`WebViewWindow/tests/linux_notification_test.sh` watches the session bus for it). Windows: not checked. The `ipc` channels aren't used; WebKit and WebView2 present the notifications themselves. |
| Unread badge on the taskbar or dock | `app.setBadgeCount` (`APP_SET_BADGE_COUNT`), `APP_DOCK_SET_BADGE`, `APP_DOCK_BOUNCE` | Missing. The desktop app sets a Windows taskbar overlay from the unread count: `win.setOverlayIcon(appIcons[n], "N notifications")` with one numbered icon for counts 1 to 10, cleared at zero. It also calls `app.setBadgeCount`. This program sets neither. The count is shown only in the custom title bar, from `document.title`. |
| Tray icon state (unread, mute) | `ipc`: `SYSTEM_TRAY_SET_STATES`, `SYSTEM_TRAY_SET_ICON`, `SYSTEM_TRAY_SET_STRINGS` | Missing. The tray icon exists, but Discord doesn't update it. |
| Taskbar progress and flash | `window.setProgressBar`, `window.flashFrame` | Missing. |
| Windows taskbar thumbnail buttons | `thumbar.setThumbarButtons` | Missing (Windows only). |
Expand All @@ -58,8 +58,8 @@ are the keys of that object. The main-process side of the channels (`bundle.js`)

| Feature | Discord bridge | Status |
| --- | --- | --- |
| Downloads saved to disk | (WebView download events) | Missing. Downloads aren't handled at all. |
| Open and save dialogs | `fileManager.showOpenDialog`, `saveWithDialog`, `showItemInFolder` | Missing. |
| Downloads saved to disk | (WebView download events) | Done: saved to the Downloads folder under the suggested name, never over an existing file (`tests/Downloads_test.cpp`; `WebViewWindow/tests/linux_download_test.sh` and `windows_download_test.ps1`). |
| Open and save dialogs | `fileManager.showOpenDialog`, `saveWithDialog`, `showItemInFolder` | Missing. The page can't reach a host dialog yet: see the bridge plan below. |
| Native clipboard (images, files, cut) | `clipboard.copyImage`, `copyFile`, `cut`, `paste`, `read` | Missing. The web clipboard API is untested. |

## Settings, storage and system
Expand Down Expand Up @@ -91,9 +91,12 @@ are the keys of that object. The main-process side of the channels (`bundle.js`)

## Plan

1. Permissions for microphone, camera and notifications, by a hook in WebViewWindow (done; the hook is only in the local checkout so far).
2. Downloads and the save dialog (`fileManager`).
1. Permissions for microphone, camera and notifications, by a hook in WebViewWindow. Done.
2. Downloads. Done. A save dialog would need a page-to-host call, which is item 6.
3. Settings and `safeStorage` through the host.
4. Badges and tray state from the page, through a bridge the page can call.
5. Voice and video. Needs a decision: the engine is a Node addon, so this either waits for a different approach or stays
out of scope.
6. A bridge the page can call. Most of the features above need the web client to call `DiscordNative`, and it only does so when
the object exists. Providing it is a design decision: it would switch the web client into desktop mode, and each call it makes
would then need a real answer. This needs a decision before any more of the bridge is built.
6 changes: 6 additions & 0 deletions src/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ void Application::run() {
runEventLoop();
}

void Application::showDiscord() {
if (discord_) {
discord_->present();
}
}

void Application::showTray() {
trayShown_ = showTrayIcon(
"Discord", [this] { discord_->present(); },
Expand Down
3 changes: 3 additions & 0 deletions src/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class Application {
// tray icon, checks for an update in the background, and runs until the Discord window closes.
void run();

// Brings the Discord window to the front, for a later start that was handed to this copy.
void showDiscord();

// Settings. The choices are saved as they change.
bool keepInTaskbar() const { return keepInTaskbar_; }
bool trayShown() const { return trayShown_; }
Expand Down
12 changes: 12 additions & 0 deletions src/DiscordWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "Application.h"
#include "Host.h"
#include "PageScript.h"
#include "Downloads.h"
#include "PagePermissions.h"

namespace {
Expand Down Expand Up @@ -47,6 +48,17 @@ DiscordWindow::DiscordWindow(Application& app) : WebViewWindow(WindowOptions{
.hardwareAcceleration = HardwareAcceleration::Always,
}),
app_(app) {
// Downloads are saved to the Downloads folder, under the name the page suggests, and never over a file already there.
setDownloadChooser([](const std::string& name) -> std::optional<std::filesystem::path> {
const std::filesystem::path folder = downloadsFolder();
try {
std::filesystem::create_directories(folder);
} catch (const std::exception&) {
// Without the folder the platform's default is used.
return std::nullopt;
}
return uniqueDownloadPath(folder, name);
});
auto& methods = handler();
methods.on("moveDrag", [](WebViewWindow& window, const Json&, const Reply& reply) {
window.beginMove();
Expand Down
43 changes: 43 additions & 0 deletions src/Downloads.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "Downloads.h"

#include <cstdlib>

#ifdef _WIN32
#include <stdlib.h>
#define DW_TEXT(text) L##text
#else
#define DW_TEXT(text) text
#endif

std::filesystem::path downloadsFolder() {
#ifdef _WIN32
const wchar_t* home = _wgetenv(L"USERPROFILE");
#else
const char* home = std::getenv("HOME");
#endif
if (!home || !*home) {
return std::filesystem::path("Downloads");
}
return std::filesystem::path(home) / "Downloads";
}

std::filesystem::path uniqueDownloadPath(const std::filesystem::path& folder, const std::string& suggestedName) {
std::filesystem::path name = std::filesystem::path(suggestedName).filename();
if (name.empty() || name == "." || name == "..") {
name = "download";
}
std::filesystem::path candidate = folder / name;
// The numbered names keep the original's stem and extension: "archive.tar" becomes "archive.tar (1).gz" for "archive.tar.gz".
const std::filesystem::path::string_type stem = name.stem().native();
const std::filesystem::path::string_type extension = name.extension().native();
for (int number = 1; std::filesystem::exists(candidate); ++number) {
std::filesystem::path::string_type numbered = stem;
numbered += DW_TEXT(" (");
const std::string digits = std::to_string(number);
numbered += std::filesystem::path::string_type(digits.begin(), digits.end());
numbered += DW_TEXT(")");
numbered += extension;
candidate = folder / numbered;
}
return candidate;
}
14 changes: 14 additions & 0 deletions src/Downloads.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Where Discord's downloads are saved: the user's Downloads folder, under the name the page suggests, without replacing a file that's
// already there.
#pragma once

#include <filesystem>
#include <string>

// The user's Downloads folder.
std::filesystem::path downloadsFolder();

// A path in folder for a file called suggestedName that isn't already there: the name itself, or "name (1)", "name (2)" and so on
// when it is. The name is reduced to its file name, so a page can't choose a folder, and an empty name is saved as "download".
// Nothing is created.
std::filesystem::path uniqueDownloadPath(const std::filesystem::path& folder, const std::string& suggestedName);
11 changes: 11 additions & 0 deletions src/Host.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,21 @@
#pragma once

#include <filesystem>
#include <functional>
#include <string>

// Opens a web address in the default browser. The address is never run through a shell.
void launchUrl(const std::string& url);

// The folder the program keeps its settings in.
std::filesystem::path settingsFolder();

// One copy runs at a time. A start that finds one running asks it to show its window and then ends. True when the running copy
// answered, so this start should stop; false when none did, so this start carries on as the program.
bool forwardShowRequest();

// Accepts show requests from later starts on a thread, and calls handle on that thread for each one.
void listenForShowRequests(std::function<void()> handle);

// Stops answering later starts, so a copy started after this call runs instead of being handed to this one.
void stopListeningForShowRequests();
Loading