Skip to content

Commit

Permalink
win32: adding callback on close button pressed
Browse files Browse the repository at this point in the history
  • Loading branch information
hramovnik committed Sep 29, 2021
1 parent 5b748c4 commit 06613ca
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 17 deletions.
21 changes: 21 additions & 0 deletions doc/application.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,27 @@ Method on_ui_created will be invoked after application's UI was created (usually
super # To navigate to start_path from rhoconfig.txt
end

Method on_ui_close_request will be invoked when application's UI is triggered to closed by user from the system UI
:::ruby
#in application.rb
def on_ui_close_request
Alert.show_popup(
:message=>"Do you realy whant to close?",
:title=>"Information",
:buttons => [{:id => 'ok', :title => 'Ok'},{:id => 'cancel', :title => 'Cancel'}],
:callback => "/app/Application/on_close_callback"
)
end

#in Appcliaction controller
def on_close_callback
if @params['button_id'] == "ok"
Rho::Application.quit()
end
end
**NOTE: if you redefine this function on your application class - the app would not be automatically closed after execution of the function - you should also close the application from code by yourself (just call Rho::Application.quit()). Works only on Win32**


Method on_ui_destroyed will be invoked when application's UI was destroyed (usually on app exit)
:::ruby
def on_ui_destroyed
Expand Down
8 changes: 6 additions & 2 deletions lib/framework/autocomplete/Rho.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@ def on_activate_app
def on_deactivate_app
end
def on_ui_created
end
end
def on_ui_close_request
end
def on_ui_destroyed
end
def on_sync_user_changed
Expand Down Expand Up @@ -261,7 +263,9 @@ def on_config_conflicts(conflicts)
def activate_app
end
def deactivate_app
end
end
def ui_close_request
end
def ui_created
end
def ui_destroyed
Expand Down
9 changes: 9 additions & 0 deletions lib/framework/rho/rho.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,15 @@ def ui_created
end
end

def ui_close_request
begin
get_app(APPNAME).on_ui_close_request
rescue Exception => e
trace_msg = e.backtrace.join("\n")
puts '"UI created" callback failed: ' + e.inspect + ";Trace: #{trace_msg}"
end
end

def ui_destroyed
begin
get_app(APPNAME).on_ui_destroyed
Expand Down
4 changes: 4 additions & 0 deletions lib/framework/rho/rhoapplication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ def on_ui_created
Rho::Application.processApplicationEvent({'applicationEvent'=>'UICreated'})
end

def on_ui_close_request
Rho::Application.quit()
end

def on_ui_destroyed
Rho::Application.processApplicationEvent({'applicationEvent'=>'UIDestroyed'})
end
Expand Down
38 changes: 38 additions & 0 deletions platform/shared/common/RhodesApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,16 @@ static void callback_uicreated(void *arg, String const &strQuery)
rho_http_sendresponse(arg, "");
}

static void callback_uicloserequest(void *arg, String const &strQuery)
{
#ifndef RHO_NO_RUBY_API
if (!RHODESAPP().isJSApplication() && !RHODESAPP().isNodeJSApplication())
rho_ruby_uiCloseRequest();
#endif

rho_http_sendresponse(arg, "");
}

static void callback_uidestroyed(void *arg, String const &strQuery)
{
#ifndef RHO_NO_RUBY_API
Expand Down Expand Up @@ -778,6 +788,23 @@ void CRhodesApp::callUiCreatedCallback()
m_appCallbacksQueue->addQueueCommand(new CAppCallbacksQueue::Command(CAppCallbacksQueue::ui_created));
}

int CRhodesApp::callUiCloseRequestCallback()
{
if ( m_bExit || !rho_ruby_is_started()) return 1;

String strUrl = m_strHomeUrl + "/system/uicloserequest";
NetResponse resp = getNetRequest().pullData( strUrl, NULL );
if ( !resp.isOK() )
{
LOG(ERROR) + "UI close request callback failed. Code: " + resp.getRespCode() + "; Error body: " + resp.getCharData();
return 1;
}else{
return 0;
}
return 1;

}

void CRhodesApp::callUiDestroyedCallback()
{
if ( m_bExit/* || !rho_ruby_is_started()*/ )
Expand Down Expand Up @@ -1575,6 +1602,7 @@ void CRhodesApp::initHttpServer()
m_httpServer->register_uri("/system/activateapp", callback_activateapp);
m_httpServer->register_uri("/system/deactivateapp", callback_deactivateapp);
m_httpServer->register_uri("/system/uicreated", callback_uicreated);
m_httpServer->register_uri("/system/uicloserequest", callback_uicloserequest);
m_httpServer->register_uri("/system/uidestroyed", callback_uidestroyed);
m_httpServer->register_uri("/system/loadserversources", callback_loadserversources);
m_httpServer->register_uri("/system/resetDBOnSyncUserChanged", callback_resetDBOnSyncUserChanged);
Expand Down Expand Up @@ -2927,6 +2955,16 @@ void rho_rhodesapp_callUiCreatedCallback()
}
}

int rho_rhodesapp_callUiCloseRequestCallback() //return zero if you whant to suppress closing
{
if ( rho::common::CRhodesApp::getInstance() ){
return RHODESAPP().callUiCloseRequestCallback();
}

return 1;
}


void rho_rhodesapp_callUiDestroyedCallback()
{
if ( rho::common::CRhodesApp::getInstance() )
Expand Down
2 changes: 2 additions & 0 deletions platform/shared/common/RhodesApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ class CRhodesApp : public CRhodesAppBase
void callBluetoothCallback(String strCallbackUrl, const char* body);
void callAppActiveCallback(boolean bActive);
void callUiCreatedCallback();
int callUiCloseRequestCallback();
void callUiDestroyedCallback();
void callPopupCallback(String strCallbackUrl, const String &id, const String &title);
boolean callTimerCallback(const String& strUrl, const String& strData);
Expand Down Expand Up @@ -406,6 +407,7 @@ void rho_rhodesapp_callDateTimeCallback(const char* strCallbackUrl, long lDateTi
void rho_rhodesapp_callBluetoothCallback(const char* strCallbackUrl, const char* body);
void rho_rhodesapp_callAppActiveCallback(int nActive);
void rho_rhodesapp_callUiCreatedCallback();
int rho_rhodesapp_callUiCloseRequestCallback();
void rho_rhodesapp_callUiDestroyedCallback();
void rho_rhodesapp_callScreenOffCallback();
void rho_rhodesapp_callScreenOnCallback();
Expand Down
37 changes: 23 additions & 14 deletions platform/shared/qt/rhodes/newVersion/QtMainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,20 +310,28 @@ void QtMainWindow::showEvent(QShowEvent *)

void QtMainWindow::closeEvent(QCloseEvent *ce)
{
if ( rho_ruby_is_started() )
rb_thread_wakeup(rb_thread_main());

if (mainWindowCallback) mainWindowCallback->onWindowClose();
tabbarRemoveAllTabs(false);
if (m_logView)
m_logView->close();
if (!acceptClosing){
acceptClosing = rho_rhodesapp_callUiCloseRequestCallback() == 0 ? false : true;
}

QTimer::singleShot(1, this, [&]{rho_rhodesapp_callUiDestroyedCallback();});
QTimer::singleShot(1000, this, [&]{rho::common::CRhodesApp::Destroy();});
QTimer::singleShot(3000, this, [this, ce]{
ce->accept();
QMainWindow::closeEvent(ce);
});
if (acceptClosing){
if ( rho_ruby_is_started() )
rb_thread_wakeup(rb_thread_main());

if (mainWindowCallback) mainWindowCallback->onWindowClose();
tabbarRemoveAllTabs(false);
if (m_logView)
m_logView->close();

QTimer::singleShot(1, this, [&]{rho_rhodesapp_callUiDestroyedCallback();});
QTimer::singleShot(1000, this, [&]{rho::common::CRhodesApp::Destroy();});
QTimer::singleShot(3000, this, [this, ce]{
ce->accept();
QMainWindow::closeEvent(ce);
});
}else{
ce->setAccepted(false);
}

}

Expand Down Expand Up @@ -389,7 +397,7 @@ void QtMainWindow::on_actionRotate180_triggered()

void QtMainWindow::on_actionExit_triggered()
{
this->close();
this->close();
}

bool QtMainWindow::internalUrlProcessing(const QUrl& url)
Expand Down Expand Up @@ -956,6 +964,7 @@ void QtMainWindow::on_actionAbout_triggered()

void QtMainWindow::exitCommand()
{
acceptClosing = true;
this->close();
}

Expand Down
2 changes: 1 addition & 1 deletion platform/shared/qt/rhodes/newVersion/QtMainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class QtMainWindow : public QMainWindow
void doAlertCallback(CAlertParams* params, int btnNum, CAlertParams::CAlertButton &button);
void internalSetProxy();
QMenuBar* createMenu();

bool acceptClosing = false;

private:
IMainWindowCallback* mainWindowCallback;
Expand Down
7 changes: 7 additions & 0 deletions platform/shared/ruby/ext/rho/rhoruby.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ static ID onConfigConflicts_mid;
static ID activateApp_mid;
static ID deactivateApp_mid;
static ID uiCreated_mid;
static ID uiCloseRequest_mid;
static ID uiDestroyed_mid;
static ID loadServerSources_mid;
static ID loadAllSyncSources_mid;
Expand Down Expand Up @@ -362,6 +363,7 @@ void RhoRubyStart()
CONST_ID(activateApp_mid, "activate_app");
CONST_ID(deactivateApp_mid, "deactivate_app");
CONST_ID(uiCreated_mid, "ui_created");
CONST_ID(uiCloseRequest_mid, "ui_close_request");
CONST_ID(uiDestroyed_mid, "ui_destroyed");
CONST_ID(loadServerSources_mid,"load_server_sources");
CONST_ID(loadAllSyncSources_mid,"load_all_sync_sources");
Expand Down Expand Up @@ -505,6 +507,11 @@ void rho_ruby_uiCreated()
rb_funcall(framework, uiCreated_mid, 0);
}

void rho_ruby_uiCloseRequest()
{
rb_funcall(framework, uiCloseRequest_mid, 0);
}

void rho_ruby_uiDestroyed()
{
rb_funcall(framework, uiDestroyed_mid, 0);
Expand Down
1 change: 1 addition & 0 deletions platform/shared/ruby/ext/rho/rhoruby.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ void rho_ruby_call_config_conflicts();
void rho_ruby_activateApp();
void rho_ruby_deactivateApp();
void rho_ruby_uiCreated();
void rho_ruby_uiCloseRequest();
void rho_ruby_uiDestroyed();
void rho_ruby_loadserversources(const char* szData);
void rho_ruby_loadallsyncsources();
Expand Down

0 comments on commit 06613ca

Please sign in to comment.