Skip to content

Commit

Permalink
Merge pull request #1713 from atsign-foundation/1680-two-instance-err…
Browse files Browse the repository at this point in the history
…or-message

fix: Only allow single instance of NoPorts Desktop on Windows
  • Loading branch information
XavierChanth authored Feb 3, 2025
2 parents 9bba820 + f1325e7 commit f50139e
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = "1.1.6+7";
CURRENT_PROJECT_VERSION = "1.1.6+8";
DEVELOPMENT_TEAM = 5XUSS6C2DF;
INFOPLIST_FILE = Runner/Info.plist;
INFOPLIST_KEY_CFBundleDisplayName = "Noports Desktop";
Expand Down Expand Up @@ -714,7 +714,7 @@
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = "1.1.6+7";
CURRENT_PROJECT_VERSION = "1.1.6+8";
DEVELOPMENT_TEAM = 5XUSS6C2DF;
INFOPLIST_FILE = Runner/Info.plist;
INFOPLIST_KEY_CFBundleDisplayName = "Noports Desktop";
Expand Down Expand Up @@ -742,7 +742,7 @@
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = "1.1.6+7";
CURRENT_PROJECT_VERSION = "1.1.6+8";
DEVELOPMENT_TEAM = 5XUSS6C2DF;
INFOPLIST_FILE = Runner/Info.plist;
INFOPLIST_KEY_CFBundleDisplayName = "Noports Desktop";
Expand Down
4 changes: 2 additions & 2 deletions packages/dart/npt_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ publish_to: "none" # Remove this line if you wish to publish to pub.dev
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix.
version: 1.1.6+8
version: 1.1.7+9
msix_config:
display_name: "NoPorts Desktop"
publisher_display_name: Atsign Inc
identity_name: TheCompany.NoPortsDesktop
publisher: CN=BBFE1D0B-F713-4C7F-B375-5EA851CBB1FF
msix_version: 1.1.6.0
msix_version: 1.1.7.0
logo_path: "assets/logo.png"
capabilities: internetClient
store: true
Expand Down
14 changes: 11 additions & 3 deletions packages/dart/npt_flutter/windows/runner/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "flutter_window.h"
#include "utils.h"

constexpr const wchar_t kWindowName[] = L"NoPorts Desktop";

int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
_In_ wchar_t *command_line, _In_ int show_command) {
// Attach to console when present (e.g., 'flutter run') or create a
Expand All @@ -13,21 +15,27 @@ int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
CreateAndAttachConsole();
}

HWND hwnd = ::FindWindow(kWindowClassName, kWindowName);
if (hwnd != NULL) {
::ShowWindow(hwnd, SW_NORMAL);
::SetForegroundWindow(hwnd);
return EXIT_FAILURE;
}

// Initialize COM, so that it is available for use in the library and/or
// plugins.
::CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);

flutter::DartProject project(L"data");

std::vector<std::string> command_line_arguments =
GetCommandLineArguments();
std::vector<std::string> command_line_arguments = GetCommandLineArguments();

project.set_dart_entrypoint_arguments(std::move(command_line_arguments));

FlutterWindow window(project);
Win32Window::Point origin(10, 10);
Win32Window::Size size(1280, 720);
if (!window.Create(L"npt_flutter", origin, size)) {
if (!window.Create(kWindowName, origin, size)) {
return EXIT_FAILURE;
}
window.SetQuitOnClose(true);
Expand Down
138 changes: 64 additions & 74 deletions packages/dart/npt_flutter/windows/runner/win32_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ namespace {
///
/// Redefined in case the developer's machine has a Windows SDK older than
/// version 10.0.22000.0.
/// See: https://docs.microsoft.com/windows/win32/api/dwmapi/ne-dwmapi-dwmwindowattribute
/// See:
/// https://docs.microsoft.com/windows/win32/api/dwmapi/ne-dwmapi-dwmwindowattribute
#ifndef DWMWA_USE_IMMERSIVE_DARK_MODE
#define DWMWA_USE_IMMERSIVE_DARK_MODE 20
#endif

constexpr const wchar_t kWindowClassName[] = L"FLUTTER_RUNNER_WIN32_WINDOW";

/// Registry key for app theme preference.
///
/// A value of 0 indicates apps should use dark mode. A non-zero or missing
/// value indicates apps should use light mode.
constexpr const wchar_t kGetPreferredBrightnessRegKey[] =
L"Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize";
constexpr const wchar_t kGetPreferredBrightnessRegValue[] = L"AppsUseLightTheme";
L"Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize";
constexpr const wchar_t kGetPreferredBrightnessRegValue[] =
L"AppsUseLightTheme";

// The number of Win32Window objects that currently exist.
static int g_active_window_count = 0;
Expand All @@ -45,23 +45,23 @@ void EnableFullDpiSupportIfAvailable(HWND hwnd) {
return;
}
auto enable_non_client_dpi_scaling =
reinterpret_cast<EnableNonClientDpiScaling*>(
reinterpret_cast<EnableNonClientDpiScaling *>(
GetProcAddress(user32_module, "EnableNonClientDpiScaling"));
if (enable_non_client_dpi_scaling != nullptr) {
enable_non_client_dpi_scaling(hwnd);
}
FreeLibrary(user32_module);
}

} // namespace
} // namespace

// Manages the Win32Window's window class registration.
class WindowClassRegistrar {
public:
public:
~WindowClassRegistrar() = default;

// Returns the singleton registrar instance.
static WindowClassRegistrar* GetInstance() {
static WindowClassRegistrar *GetInstance() {
if (!instance_) {
instance_ = new WindowClassRegistrar();
}
Expand All @@ -70,23 +70,23 @@ class WindowClassRegistrar {

// Returns the name of the window class, registering the class if it hasn't
// previously been registered.
const wchar_t* GetWindowClass();
const wchar_t *GetWindowClass();

// Unregisters the window class. Should only be called if there are no
// instances of the window.
void UnregisterWindowClass();

private:
private:
WindowClassRegistrar() = default;

static WindowClassRegistrar* instance_;
static WindowClassRegistrar *instance_;

bool class_registered_ = false;
};

WindowClassRegistrar* WindowClassRegistrar::instance_ = nullptr;
WindowClassRegistrar *WindowClassRegistrar::instance_ = nullptr;

const wchar_t* WindowClassRegistrar::GetWindowClass() {
const wchar_t *WindowClassRegistrar::GetWindowClass() {
if (!class_registered_) {
WNDCLASS window_class{};
window_class.hCursor = LoadCursor(nullptr, IDC_ARROW);
Expand All @@ -111,21 +111,18 @@ void WindowClassRegistrar::UnregisterWindowClass() {
class_registered_ = false;
}

Win32Window::Win32Window() {
++g_active_window_count;
}
Win32Window::Win32Window() { ++g_active_window_count; }

Win32Window::~Win32Window() {
--g_active_window_count;
Destroy();
}

bool Win32Window::Create(const std::wstring& title,
const Point& origin,
const Size& size) {
bool Win32Window::Create(const std::wstring &title, const Point &origin,
const Size &size) {
Destroy();

const wchar_t* window_class =
const wchar_t *window_class =
WindowClassRegistrar::GetInstance()->GetWindowClass();

const POINT target_point = {static_cast<LONG>(origin.x),
Expand All @@ -149,73 +146,68 @@ bool Win32Window::Create(const std::wstring& title,
return OnCreate();
}

bool Win32Window::Show() {
return ShowWindow(window_handle_, SW_SHOWNORMAL);
}
bool Win32Window::Show() { return ShowWindow(window_handle_, SW_SHOWNORMAL); }

// static
LRESULT CALLBACK Win32Window::WndProc(HWND const window,
UINT const message,
LRESULT CALLBACK Win32Window::WndProc(HWND const window, UINT const message,
WPARAM const wparam,
LPARAM const lparam) noexcept {
if (message == WM_NCCREATE) {
auto window_struct = reinterpret_cast<CREATESTRUCT*>(lparam);
auto window_struct = reinterpret_cast<CREATESTRUCT *>(lparam);
SetWindowLongPtr(window, GWLP_USERDATA,
reinterpret_cast<LONG_PTR>(window_struct->lpCreateParams));

auto that = static_cast<Win32Window*>(window_struct->lpCreateParams);
auto that = static_cast<Win32Window *>(window_struct->lpCreateParams);
EnableFullDpiSupportIfAvailable(window);
that->window_handle_ = window;
} else if (Win32Window* that = GetThisFromHandle(window)) {
} else if (Win32Window *that = GetThisFromHandle(window)) {
return that->MessageHandler(window, message, wparam, lparam);
}

return DefWindowProc(window, message, wparam, lparam);
}

LRESULT
Win32Window::MessageHandler(HWND hwnd,
UINT const message,
WPARAM const wparam,
Win32Window::MessageHandler(HWND hwnd, UINT const message, WPARAM const wparam,
LPARAM const lparam) noexcept {
switch (message) {
case WM_DESTROY:
window_handle_ = nullptr;
Destroy();
if (quit_on_close_) {
PostQuitMessage(0);
}
return 0;

case WM_DPICHANGED: {
auto newRectSize = reinterpret_cast<RECT*>(lparam);
LONG newWidth = newRectSize->right - newRectSize->left;
LONG newHeight = newRectSize->bottom - newRectSize->top;

SetWindowPos(hwnd, nullptr, newRectSize->left, newRectSize->top, newWidth,
newHeight, SWP_NOZORDER | SWP_NOACTIVATE);

return 0;
case WM_DESTROY:
window_handle_ = nullptr;
Destroy();
if (quit_on_close_) {
PostQuitMessage(0);
}
case WM_SIZE: {
RECT rect = GetClientArea();
if (child_content_ != nullptr) {
// Size and position the child window.
MoveWindow(child_content_, rect.left, rect.top, rect.right - rect.left,
rect.bottom - rect.top, TRUE);
}
return 0;
return 0;

case WM_DPICHANGED: {
auto newRectSize = reinterpret_cast<RECT *>(lparam);
LONG newWidth = newRectSize->right - newRectSize->left;
LONG newHeight = newRectSize->bottom - newRectSize->top;

SetWindowPos(hwnd, nullptr, newRectSize->left, newRectSize->top, newWidth,
newHeight, SWP_NOZORDER | SWP_NOACTIVATE);

return 0;
}
case WM_SIZE: {
RECT rect = GetClientArea();
if (child_content_ != nullptr) {
// Size and position the child window.
MoveWindow(child_content_, rect.left, rect.top, rect.right - rect.left,
rect.bottom - rect.top, TRUE);
}
return 0;
}

case WM_ACTIVATE:
if (child_content_ != nullptr) {
SetFocus(child_content_);
}
return 0;
case WM_ACTIVATE:
if (child_content_ != nullptr) {
SetFocus(child_content_);
}
return 0;

case WM_DWMCOLORIZATIONCOLORCHANGED:
UpdateTheme(hwnd);
return 0;
case WM_DWMCOLORIZATIONCOLORCHANGED:
UpdateTheme(hwnd);
return 0;
}

return DefWindowProc(window_handle_, message, wparam, lparam);
Expand All @@ -233,8 +225,8 @@ void Win32Window::Destroy() {
}
}

Win32Window* Win32Window::GetThisFromHandle(HWND const window) noexcept {
return reinterpret_cast<Win32Window*>(
Win32Window *Win32Window::GetThisFromHandle(HWND const window) noexcept {
return reinterpret_cast<Win32Window *>(
GetWindowLongPtr(window, GWLP_USERDATA));
}

Expand All @@ -255,9 +247,7 @@ RECT Win32Window::GetClientArea() {
return frame;
}

HWND Win32Window::GetHandle() {
return window_handle_;
}
HWND Win32Window::GetHandle() { return window_handle_; }

void Win32Window::SetQuitOnClose(bool quit_on_close) {
quit_on_close_ = quit_on_close;
Expand All @@ -275,10 +265,10 @@ void Win32Window::OnDestroy() {
void Win32Window::UpdateTheme(HWND const window) {
DWORD light_mode;
DWORD light_mode_size = sizeof(light_mode);
LSTATUS result = RegGetValue(HKEY_CURRENT_USER, kGetPreferredBrightnessRegKey,
kGetPreferredBrightnessRegValue,
RRF_RT_REG_DWORD, nullptr, &light_mode,
&light_mode_size);
LSTATUS result =
RegGetValue(HKEY_CURRENT_USER, kGetPreferredBrightnessRegKey,
kGetPreferredBrightnessRegValue, RRF_RT_REG_DWORD, nullptr,
&light_mode, &light_mode_size);

if (result == ERROR_SUCCESS) {
BOOL enable_dark_mode = light_mode == 0;
Expand Down
Loading

0 comments on commit f50139e

Please sign in to comment.