mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 21:47:43 +00:00
LibGUI: Exit the main event loop when the last window is deleted.
This behavior is the new opt-out default. If you don't want your app to exit when the last GWindow is destroyed, call this: - void GApplication::set_quit_set_quit_when_last_window_deleted(bool) Also renamed "windows()" to "reified_windows" in GWindow.cpp to reflect that it only contains GWindows that have a server-side representation. :^)
This commit is contained in:
parent
528d8d49dc
commit
fbae03b737
3 changed files with 32 additions and 18 deletions
|
@ -111,3 +111,9 @@ void GApplication::hide_tooltip()
|
||||||
if (m_tooltip_window)
|
if (m_tooltip_window)
|
||||||
m_tooltip_window->hide();
|
m_tooltip_window->hide();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void GApplication::did_delete_last_window(Badge<GWindow>)
|
||||||
|
{
|
||||||
|
if (m_quit_when_last_window_deleted)
|
||||||
|
m_event_loop->quit(0);
|
||||||
|
}
|
||||||
|
|
|
@ -6,9 +6,10 @@
|
||||||
#include <LibGUI/GShortcut.h>
|
#include <LibGUI/GShortcut.h>
|
||||||
|
|
||||||
class GAction;
|
class GAction;
|
||||||
class GKeyEvent;
|
|
||||||
class GEventLoop;
|
class GEventLoop;
|
||||||
|
class GKeyEvent;
|
||||||
class GMenuBar;
|
class GMenuBar;
|
||||||
|
class GWindow;
|
||||||
class Point;
|
class Point;
|
||||||
|
|
||||||
class GApplication {
|
class GApplication {
|
||||||
|
@ -29,10 +30,16 @@ public:
|
||||||
void show_tooltip(const StringView&, const Point& screen_location);
|
void show_tooltip(const StringView&, const Point& screen_location);
|
||||||
void hide_tooltip();
|
void hide_tooltip();
|
||||||
|
|
||||||
|
bool quit_when_last_window_deleted() const { return m_quit_when_last_window_deleted; }
|
||||||
|
void set_quit_when_last_window_deleted(bool b) { m_quit_when_last_window_deleted = b; }
|
||||||
|
|
||||||
|
void did_delete_last_window(Badge<GWindow>);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
OwnPtr<GEventLoop> m_event_loop;
|
OwnPtr<GEventLoop> m_event_loop;
|
||||||
OwnPtr<GMenuBar> m_menubar;
|
OwnPtr<GMenuBar> m_menubar;
|
||||||
HashMap<GShortcut, GAction*> m_global_shortcut_actions;
|
HashMap<GShortcut, GAction*> m_global_shortcut_actions;
|
||||||
class TooltipWindow;
|
class TooltipWindow;
|
||||||
TooltipWindow* m_tooltip_window { nullptr };
|
TooltipWindow* m_tooltip_window { nullptr };
|
||||||
|
bool m_quit_when_last_window_deleted { true };
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,30 +1,25 @@
|
||||||
#include "GWindow.h"
|
|
||||||
#include "GEvent.h"
|
|
||||||
#include "GEventLoop.h"
|
|
||||||
#include "GWidget.h"
|
|
||||||
#include <AK/HashMap.h>
|
#include <AK/HashMap.h>
|
||||||
#include <AK/StringBuilder.h>
|
#include <AK/StringBuilder.h>
|
||||||
#include <LibC/stdio.h>
|
#include <LibC/stdio.h>
|
||||||
#include <LibC/stdlib.h>
|
#include <LibC/stdlib.h>
|
||||||
#include <LibC/unistd.h>
|
#include <LibC/unistd.h>
|
||||||
#include <LibGUI/GPainter.h>
|
|
||||||
#include <LibDraw/GraphicsBitmap.h>
|
#include <LibDraw/GraphicsBitmap.h>
|
||||||
|
#include <LibGUI/GApplication.h>
|
||||||
|
#include <LibGUI/GEvent.h>
|
||||||
|
#include <LibGUI/GEventLoop.h>
|
||||||
|
#include <LibGUI/GPainter.h>
|
||||||
|
#include <LibGUI/GWidget.h>
|
||||||
|
#include <LibGUI/GWindow.h>
|
||||||
|
|
||||||
//#define UPDATE_COALESCING_DEBUG
|
//#define UPDATE_COALESCING_DEBUG
|
||||||
|
|
||||||
static HashMap<int, GWindow*>* s_windows;
|
static HashTable<GWindow*> all_windows;
|
||||||
|
static HashMap<int, GWindow*> reified_windows;
|
||||||
static HashMap<int, GWindow*>& windows()
|
|
||||||
{
|
|
||||||
if (!s_windows)
|
|
||||||
s_windows = new HashMap<int, GWindow*>;
|
|
||||||
return *s_windows;
|
|
||||||
}
|
|
||||||
|
|
||||||
GWindow* GWindow::from_window_id(int window_id)
|
GWindow* GWindow::from_window_id(int window_id)
|
||||||
{
|
{
|
||||||
auto it = windows().find(window_id);
|
auto it = reified_windows.find(window_id);
|
||||||
if (it != windows().end())
|
if (it != reified_windows.end())
|
||||||
return (*it).value;
|
return (*it).value;
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
@ -32,15 +27,21 @@ GWindow* GWindow::from_window_id(int window_id)
|
||||||
GWindow::GWindow(CObject* parent)
|
GWindow::GWindow(CObject* parent)
|
||||||
: CObject(parent)
|
: CObject(parent)
|
||||||
{
|
{
|
||||||
|
all_windows.set(this);
|
||||||
m_rect_when_windowless = { 100, 400, 140, 140 };
|
m_rect_when_windowless = { 100, 400, 140, 140 };
|
||||||
m_title_when_windowless = "GWindow";
|
m_title_when_windowless = "GWindow";
|
||||||
}
|
}
|
||||||
|
|
||||||
GWindow::~GWindow()
|
GWindow::~GWindow()
|
||||||
{
|
{
|
||||||
|
all_windows.remove(this);
|
||||||
if (m_main_widget)
|
if (m_main_widget)
|
||||||
delete m_main_widget;
|
delete m_main_widget;
|
||||||
hide();
|
hide();
|
||||||
|
|
||||||
|
if (all_windows.is_empty()) {
|
||||||
|
GApplication::the().did_delete_last_window({});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void GWindow::close()
|
void GWindow::close()
|
||||||
|
@ -87,7 +88,7 @@ void GWindow::show()
|
||||||
auto response = GWindowServerConnection::the().sync_request(request, WSAPI_ServerMessage::Type::DidCreateWindow);
|
auto response = GWindowServerConnection::the().sync_request(request, WSAPI_ServerMessage::Type::DidCreateWindow);
|
||||||
m_window_id = response.window_id;
|
m_window_id = response.window_id;
|
||||||
|
|
||||||
windows().set(m_window_id, this);
|
reified_windows.set(m_window_id, this);
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,7 +96,7 @@ void GWindow::hide()
|
||||||
{
|
{
|
||||||
if (!m_window_id)
|
if (!m_window_id)
|
||||||
return;
|
return;
|
||||||
windows().remove(m_window_id);
|
reified_windows.remove(m_window_id);
|
||||||
WSAPI_ClientMessage request;
|
WSAPI_ClientMessage request;
|
||||||
request.type = WSAPI_ClientMessage::Type::DestroyWindow;
|
request.type = WSAPI_ClientMessage::Type::DestroyWindow;
|
||||||
request.window_id = m_window_id;
|
request.window_id = m_window_id;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue