2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-02-26 10:50:04 -07:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2020-02-16 09:17:49 +01:00
|
|
|
#include <LibCore/EventLoop.h>
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/Dialog.h>
|
|
|
|
|
#include <LibGUI/Event.h>
|
2022-06-19 21:50:36 -04:00
|
|
|
#include <LibGfx/Palette.h>
|
2019-03-19 00:01:02 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
namespace GUI {
|
|
|
|
|
|
2021-07-14 12:01:31 +02:00
|
|
|
Dialog::Dialog(Window* parent_window, ScreenPosition screen_position)
|
2020-03-04 20:53:51 +01:00
|
|
|
: Window(parent_window)
|
2021-07-14 12:01:31 +02:00
|
|
|
, m_screen_position(screen_position)
|
2019-03-19 00:01:02 +01:00
|
|
|
{
|
2022-08-18 11:00:08 -04:00
|
|
|
set_window_mode(WindowMode::Blocking);
|
2019-03-19 00:01:02 +01:00
|
|
|
}
|
|
|
|
|
|
2022-05-13 13:10:27 +01:00
|
|
|
Dialog::ExecResult Dialog::exec()
|
2019-03-19 00:01:02 +01:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(!m_event_loop);
|
2020-02-02 12:34:39 +01:00
|
|
|
m_event_loop = make<Core::EventLoop>();
|
2021-07-14 12:01:31 +02:00
|
|
|
|
|
|
|
|
switch (m_screen_position) {
|
2023-05-13 05:07:09 -04:00
|
|
|
case ScreenPosition::DoNotPosition:
|
|
|
|
|
break;
|
2022-05-13 14:19:18 +01:00
|
|
|
case ScreenPosition::CenterWithinParent:
|
2023-05-13 05:07:09 -04:00
|
|
|
if (auto parent = find_parent_window(); parent && parent->is_visible()) {
|
|
|
|
|
center_within(*parent);
|
|
|
|
|
constrain_to_desktop();
|
|
|
|
|
break;
|
2020-08-16 13:05:42 +02:00
|
|
|
}
|
2023-05-13 05:07:09 -04:00
|
|
|
[[fallthrough]];
|
2022-05-13 14:19:18 +01:00
|
|
|
case ScreenPosition::Center:
|
2023-05-13 05:07:09 -04:00
|
|
|
center_on_screen();
|
2021-07-14 12:01:31 +02:00
|
|
|
break;
|
2019-03-19 02:20:00 +01:00
|
|
|
}
|
2021-07-14 12:01:31 +02:00
|
|
|
|
2019-03-19 00:01:02 +01:00
|
|
|
show();
|
2019-03-19 02:20:00 +01:00
|
|
|
auto result = m_event_loop->exec();
|
|
|
|
|
m_event_loop = nullptr;
|
2021-01-11 13:32:26 +01:00
|
|
|
dbgln("{}: Event loop returned with result {}", *this, result);
|
2019-09-22 00:46:29 +02:00
|
|
|
remove_from_parent();
|
2022-05-13 13:10:27 +01:00
|
|
|
return static_cast<ExecResult>(result);
|
2019-03-19 00:01:02 +01:00
|
|
|
}
|
|
|
|
|
|
2022-05-13 13:10:27 +01:00
|
|
|
void Dialog::done(ExecResult result)
|
2019-03-19 00:01:02 +01:00
|
|
|
{
|
2022-11-17 09:17:30 -05:00
|
|
|
Window::close();
|
|
|
|
|
|
2019-03-20 22:31:21 +01:00
|
|
|
if (!m_event_loop)
|
|
|
|
|
return;
|
2019-03-19 00:01:02 +01:00
|
|
|
m_result = result;
|
2022-11-16 07:35:02 -05:00
|
|
|
on_done(m_result);
|
|
|
|
|
|
2022-05-13 13:10:27 +01:00
|
|
|
dbgln("{}: Quit event loop with result {}", *this, to_underlying(result));
|
|
|
|
|
m_event_loop->quit(to_underlying(result));
|
2019-03-19 00:01:02 +01:00
|
|
|
}
|
2019-07-26 16:13:59 +02:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void Dialog::event(Core::Event& event)
|
2020-01-01 11:58:37 +11:00
|
|
|
{
|
2022-09-07 08:04:17 -04:00
|
|
|
if (event.type() == Event::KeyDown) {
|
2020-02-02 15:07:41 +01:00
|
|
|
auto& key_event = static_cast<KeyEvent&>(event);
|
2020-01-01 11:58:37 +11:00
|
|
|
if (key_event.key() == KeyCode::Key_Escape) {
|
2022-05-13 13:10:27 +01:00
|
|
|
done(ExecResult::Cancel);
|
2020-01-21 21:37:22 +01:00
|
|
|
event.accept();
|
2020-01-01 11:58:37 +11:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
Window::event(event);
|
2020-01-01 11:58:37 +11:00
|
|
|
}
|
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
void Dialog::close()
|
2019-07-26 16:13:59 +02:00
|
|
|
{
|
2022-05-13 13:10:27 +01:00
|
|
|
done(ExecResult::Cancel);
|
2019-07-26 16:13:59 +02:00
|
|
|
}
|
2020-02-02 15:07:41 +01:00
|
|
|
|
|
|
|
|
}
|