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
6 changes: 3 additions & 3 deletions src/message_dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ class MessageDialog : public Dialog {
/**
* @brief Whether the native backend supports the extended dialog controls.
* @note Platform availability:
* - macOS: ❌ Not supported.
* - Windows: ✅ WinUI3 backend.
* - Linux: ❌ Not supported.
* - macOS: ✅ Supported (NSAlert; non-blocking sheet when parent window is set).
* - Windows: ✅ Supported (Win32 TaskDialogIndirect and WinUI3 backends).
* - Linux: ✅ Supported (GTK 3 GtkMessageDialog and GTK 4 GtkAlertDialog).
* - Android: ❌ Not supported.
* - iOS: ❌ Not supported.
* - OpenHarmony: ✅ Native ArkUI controls after UI host initialization; API 23+.
Expand Down
239 changes: 188 additions & 51 deletions src/platform/linux/message_dialog_linux.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,42 @@
#include "../message_dialog_state.h"
#include <algorithm>
#include <memory>
#include "../../dialog.h"
#include "../../message_dialog.h"

#include <gtk/gtk.h>

namespace nativeapi {

// Private implementation class for MessageDialog
namespace {

GtkWindow* ResolveParentWindow(const std::shared_ptr<Window>& parent, bool* valid) {
*valid = true;
if (!parent) {
return nullptr;
}
GtkWidget* widget = static_cast<GtkWidget*>(parent->GetNativeObject());
if (!widget || !GTK_IS_WINDOW(widget)) {
*valid = false;
return nullptr;
}
return GTK_WINDOW(widget);
}

} // namespace

// Private implementation class for MessageDialog (GTK 3)
class MessageDialog::Impl {
public:
MessageDialogState state_;
void RefreshExtended() {}

Impl(const std::string& title, const std::string& message)
: title_(title), message_(message), dialog_(nullptr), is_open_(false) {
: title_(title),
message_(message),
dialog_(nullptr),
input_entry_(nullptr),
checkbox_(nullptr),
progress_bar_(nullptr) {
// Ensure GTK is initialized
if (!gdk_display_get_default()) {
gtk_init_check(nullptr, nullptr);
Expand All @@ -28,116 +52,221 @@ class MessageDialog::Impl {

void SetTitle(const std::string& title) {
title_ = title;
// Title will be applied when dialog is opened
if (dialog_ && GTK_IS_WINDOW(dialog_)) {
gtk_window_set_title(GTK_WINDOW(dialog_), title_.c_str());
}
}

std::string GetTitle() const { return title_; }

void SetMessage(const std::string& message) {
message_ = message;
// Message will be applied when dialog is opened
if (dialog_ && GTK_IS_MESSAGE_DIALOG(dialog_)) {
gtk_message_dialog_set_markup(GTK_MESSAGE_DIALOG(dialog_), message_.c_str());
}
}

std::string GetMessage() const { return message_; }

void RefreshExtended() {
if (!dialog_) {
return;
}
if (input_entry_) {
const char* current = gtk_entry_get_text(GTK_ENTRY(input_entry_));
if (!current || state_.input != current) {
gtk_entry_set_text(GTK_ENTRY(input_entry_), state_.input.c_str());
}
gtk_widget_set_visible(input_entry_, state_.input_enabled ? TRUE : FALSE);
}
if (checkbox_) {
gtk_button_set_label(GTK_BUTTON(checkbox_), state_.checkbox.c_str());
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox_), state_.checked ? TRUE : FALSE);
gtk_widget_set_visible(checkbox_, !state_.checkbox.empty() ? TRUE : FALSE);
}
if (progress_bar_) {
ApplyProgress(progress_bar_, state_.progress);
}
}

bool Open(DialogModality modality) {
if (state_.open) {
return false;
}
// Ensure GTK is initialized
if (!gdk_display_get_default()) {
if (!gtk_init_check(nullptr, nullptr)) {
return false;
}
}

// For modal dialogs, always create a new dialog since gtk_dialog_run destroys it
// For non-modal dialogs, close existing dialog if open before creating new one
// This ensures title and message are always up to date
bool parent_valid = true;
GtkWindow* parent = ResolveParentWindow(state_.parent, &parent_valid);
if (!parent_valid) {
return false;
}

if (dialog_ && GTK_IS_WIDGET(dialog_)) {
// Destroy old dialog if exists
gtk_widget_destroy(dialog_);
dialog_ = nullptr;
is_open_ = false;
}

// Create a new message dialog
GtkDialogFlags flags =
(modality != DialogModality::None) ? GTK_DIALOG_MODAL : GTK_DIALOG_DESTROY_WITH_PARENT;
dialog_ = gtk_message_dialog_new(
nullptr, // No parent window
GTK_DIALOG_MODAL, // Default to modal (will be overridden for non-modal)
GTK_MESSAGE_INFO, // Message type
GTK_BUTTONS_OK, // Buttons
"%s", // Format string
message_.c_str());

parent, flags, GTK_MESSAGE_INFO, GTK_BUTTONS_NONE, "%s", message_.c_str());
if (!dialog_) {
return false;
}

// Set title
gtk_window_set_title(GTK_WINDOW(dialog_), title_.c_str());

// Connect destroy signal to track when dialog is closed by user
// Add configured buttons (close, secondary, primary -> primary on the right).
bool added_any = false;
gint default_response = GTK_RESPONSE_NONE;
auto add_button = [&](const std::string& label, gint response_id, MessageDialogResult res) {
if (label.empty()) {
return;
}
gtk_dialog_add_button(GTK_DIALOG(dialog_), label.c_str(), response_id);
added_any = true;
if (res == state_.default_button) {
default_response = response_id;
}
};
add_button(state_.close, kResponseClose, MessageDialogResult::Close);
add_button(state_.secondary, kResponseSecondary, MessageDialogResult::Secondary);
add_button(state_.primary, kResponsePrimary, MessageDialogResult::Primary);
if (!added_any) {
gtk_dialog_add_button(GTK_DIALOG(dialog_), "OK", kResponseClose);
default_response = kResponseClose;
}
if (default_response != GTK_RESPONSE_NONE) {
gtk_dialog_set_default_response(GTK_DIALOG(dialog_), default_response);
}

// Add optional extended controls to the message area.
input_entry_ = nullptr;
checkbox_ = nullptr;
progress_bar_ = nullptr;
GtkWidget* area = gtk_message_dialog_get_message_area(GTK_MESSAGE_DIALOG(dialog_));
if (area) {
if (state_.input_enabled) {
input_entry_ = gtk_entry_new();
gtk_entry_set_text(GTK_ENTRY(input_entry_), state_.input.c_str());
gtk_entry_set_activates_default(GTK_ENTRY(input_entry_), TRUE);
g_signal_connect(input_entry_, "changed", G_CALLBACK(OnInputChanged), this);
gtk_box_pack_start(GTK_BOX(area), input_entry_, FALSE, FALSE, 4);
gtk_widget_show(input_entry_);
}
if (!state_.checkbox.empty()) {
checkbox_ = gtk_check_button_new_with_label(state_.checkbox.c_str());
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox_), state_.checked ? TRUE : FALSE);
g_signal_connect(checkbox_, "toggled", G_CALLBACK(OnCheckboxToggled), this);
gtk_box_pack_start(GTK_BOX(area), checkbox_, FALSE, FALSE, 4);
gtk_widget_show(checkbox_);
}
if (state_.progress != -2) {
progress_bar_ = gtk_progress_bar_new();
ApplyProgress(progress_bar_, state_.progress);
gtk_box_pack_start(GTK_BOX(area), progress_bar_, FALSE, FALSE, 4);
gtk_widget_show(progress_bar_);
}
}

state_.result = MessageDialogResult::None;
g_signal_connect(dialog_, "response", G_CALLBACK(OnResponse), this);
g_signal_connect(dialog_, "destroy", G_CALLBACK(OnDestroy), this);

// Handle modality
switch (modality) {
case DialogModality::None:
// Non-modal: show the dialog without blocking
gtk_window_set_modal(GTK_WINDOW(dialog_), FALSE);
state_.open = true;
gtk_widget_show(dialog_);
is_open_ = true;
break;

case DialogModality::Application:
case DialogModality::Window:
// Modal: block until user responds
gtk_window_set_modal(GTK_WINDOW(dialog_), TRUE);
is_open_ = true;

// Run the dialog modally - this blocks until user responds
// Note: gtk_dialog_run automatically destroys the dialog when done
gtk_dialog_run(GTK_DIALOG(dialog_));

// After gtk_dialog_run returns, the dialog has been dismissed and destroyed
// The OnDestroy callback will set dialog_ to nullptr
is_open_ = false;
state_.open = true;
{
const gint response_id = gtk_dialog_run(GTK_DIALOG(dialog_));
if (state_.result == MessageDialogResult::None) {
state_.result = MapResponse(response_id);
}
}
if (dialog_ && GTK_IS_WIDGET(dialog_)) {
gtk_widget_destroy(dialog_);
dialog_ = nullptr;
}
state_.open = false;
break;
}

return true;
}

bool Close() {
if (!dialog_ || !is_open_) {
if (!dialog_ || !state_.open) {
return false;
}

// Close the dialog programmatically
gtk_dialog_response(GTK_DIALOG(dialog_), GTK_RESPONSE_CLOSE);

// If it's a modal dialog, we need to destroy it manually
// (gtk_dialog_run already destroyed it)
if (dialog_) {
if (dialog_ && GTK_IS_WIDGET(dialog_)) {
gtk_widget_destroy(dialog_);
dialog_ = nullptr;
}

is_open_ = false;
state_.open = false;
return true;
}

private:
std::string title_;
std::string message_;
GtkWidget* dialog_;
bool is_open_;
static constexpr gint kResponsePrimary = 1;
static constexpr gint kResponseSecondary = 2;
static constexpr gint kResponseClose = 3;

static MessageDialogResult MapResponse(gint response_id) {
switch (response_id) {
case kResponsePrimary:
return MessageDialogResult::Primary;
case kResponseSecondary:
return MessageDialogResult::Secondary;
default:
return MessageDialogResult::Close;
}
}

static void ApplyProgress(GtkWidget* bar, double value) {
if (value == -2) {
gtk_widget_hide(bar);
return;
}
gtk_widget_show(bar);
if (value == -1) {
gtk_progress_bar_pulse(GTK_PROGRESS_BAR(bar));
} else {
gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(bar), std::clamp(value, 0.0, 1.0));
}
}

static void OnInputChanged(GtkEditable* editable, gpointer user_data) {
auto* impl = static_cast<Impl*>(user_data);
const char* text = gtk_entry_get_text(GTK_ENTRY(editable));
impl->state_.input = text ? text : "";
}

static void OnCheckboxToggled(GtkToggleButton* button, gpointer user_data) {
auto* impl = static_cast<Impl*>(user_data);
impl->state_.checked = gtk_toggle_button_get_active(button) != FALSE;
}

static void OnResponse(GtkDialog* dialog, gint response_id, gpointer user_data) {
Impl* impl = static_cast<Impl*>(user_data);
impl->state_.result = MapResponse(response_id);
impl->state_.open = false;

// Mark as closed
impl->is_open_ = false;

// For non-modal dialogs, destroy the dialog when user responds
if (!gtk_window_get_modal(GTK_WINDOW(dialog))) {
gtk_widget_destroy(GTK_WIDGET(dialog));
impl->dialog_ = nullptr;
Expand All @@ -146,13 +275,21 @@ class MessageDialog::Impl {

static void OnDestroy(GtkWidget* widget, gpointer user_data) {
Impl* impl = static_cast<Impl*>(user_data);

// Clear the dialog pointer when it's destroyed
if (impl->dialog_ == widget) {
impl->dialog_ = nullptr;
impl->is_open_ = false;
impl->input_entry_ = nullptr;
impl->checkbox_ = nullptr;
impl->progress_bar_ = nullptr;
impl->state_.open = false;
}
}

std::string title_;
std::string message_;
GtkWidget* dialog_;
GtkWidget* input_entry_;
GtkWidget* checkbox_;
GtkWidget* progress_bar_;
};

// MessageDialog implementation
Expand Down Expand Up @@ -197,7 +334,7 @@ bool MessageDialog::Close() {
return pimpl_->Close();
}

bool MessageDialog::IsExtendedSupported() { return false; }
bool MessageDialog::IsExtendedSupported() { return true; }
#include "../message_dialog_extensions.inc"

} // namespace nativeapi
Loading
Loading