mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 08:47:34 +00:00
LibGUI: Add window remember state that loads and saves from config
This commit is contained in:
parent
e0e67a2b27
commit
613557a535
2 changed files with 47 additions and 0 deletions
|
@ -10,6 +10,7 @@
|
||||||
#include <AK/JsonObject.h>
|
#include <AK/JsonObject.h>
|
||||||
#include <AK/NeverDestroyed.h>
|
#include <AK/NeverDestroyed.h>
|
||||||
#include <AK/ScopeGuard.h>
|
#include <AK/ScopeGuard.h>
|
||||||
|
#include <LibConfig/Client.h>
|
||||||
#include <LibCore/EventLoop.h>
|
#include <LibCore/EventLoop.h>
|
||||||
#include <LibCore/MimeData.h>
|
#include <LibCore/MimeData.h>
|
||||||
#include <LibGUI/Action.h>
|
#include <LibGUI/Action.h>
|
||||||
|
@ -111,6 +112,8 @@ Window::~Window()
|
||||||
void Window::close()
|
void Window::close()
|
||||||
{
|
{
|
||||||
hide();
|
hide();
|
||||||
|
if (m_save_size_and_position_on_close)
|
||||||
|
save_size_and_position(m_save_domain, m_save_group);
|
||||||
if (on_close)
|
if (on_close)
|
||||||
on_close();
|
on_close();
|
||||||
}
|
}
|
||||||
|
@ -522,6 +525,43 @@ void Window::propagate_shortcuts(KeyEvent& event, Widget* widget, ShortcutPropag
|
||||||
event.ignore();
|
event.ignore();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Window::restore_size_and_position(StringView domain, StringView group, Optional<Gfx::IntSize> fallback_size, Optional<Gfx::IntPoint> fallback_position)
|
||||||
|
{
|
||||||
|
int x = Config::read_i32(domain, group, "X"sv, INT_MIN);
|
||||||
|
int y = Config::read_i32(domain, group, "Y"sv, INT_MIN);
|
||||||
|
if (x != INT_MIN && y != INT_MIN) {
|
||||||
|
move_to(x, y);
|
||||||
|
} else if (fallback_position.has_value()) {
|
||||||
|
move_to(fallback_position.release_value());
|
||||||
|
}
|
||||||
|
|
||||||
|
int width = Config::read_i32(domain, group, "Width"sv, INT_MIN);
|
||||||
|
int height = Config::read_i32(domain, group, "Height"sv, INT_MIN);
|
||||||
|
if (width != INT_MIN && height != INT_MIN) {
|
||||||
|
resize(width, height);
|
||||||
|
} else if (fallback_size.has_value()) {
|
||||||
|
resize(fallback_size.release_value());
|
||||||
|
}
|
||||||
|
|
||||||
|
set_maximized(Config::read_bool(domain, group, "Maximized"sv, false));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Window::save_size_and_position(StringView domain, StringView group) const
|
||||||
|
{
|
||||||
|
Config::write_i32(domain, group, "X"sv, x());
|
||||||
|
Config::write_i32(domain, group, "Y"sv, y());
|
||||||
|
Config::write_i32(domain, group, "Width"sv, width());
|
||||||
|
Config::write_i32(domain, group, "Height"sv, height());
|
||||||
|
Config::write_bool(domain, group, "Maximized"sv, is_maximized());
|
||||||
|
}
|
||||||
|
|
||||||
|
void Window::save_size_and_position_on_close(StringView domain, StringView group)
|
||||||
|
{
|
||||||
|
m_save_size_and_position_on_close = true;
|
||||||
|
m_save_domain = domain;
|
||||||
|
m_save_group = group;
|
||||||
|
}
|
||||||
|
|
||||||
void Window::handle_key_event(KeyEvent& event)
|
void Window::handle_key_event(KeyEvent& event)
|
||||||
{
|
{
|
||||||
if (!m_focused_widget && event.type() == Event::KeyDown && event.key() == Key_Tab && !event.ctrl() && !event.alt() && !event.super()) {
|
if (!m_focused_widget && event.type() == Event::KeyDown && event.key() == Key_Tab && !event.ctrl() && !event.alt() && !event.super()) {
|
||||||
|
|
|
@ -240,6 +240,10 @@ public:
|
||||||
|
|
||||||
void propagate_shortcuts(KeyEvent& event, Widget* widget, ShortcutPropagationBoundary = ShortcutPropagationBoundary::Application);
|
void propagate_shortcuts(KeyEvent& event, Widget* widget, ShortcutPropagationBoundary = ShortcutPropagationBoundary::Application);
|
||||||
|
|
||||||
|
void restore_size_and_position(StringView domain, StringView group = "Window"sv, Optional<Gfx::IntSize> fallback_size = {}, Optional<Gfx::IntPoint> fallback_position = {});
|
||||||
|
void save_size_and_position(StringView domain, StringView group = "Window"sv) const;
|
||||||
|
void save_size_and_position_on_close(StringView domain, StringView group = "Window"sv);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Window(Core::EventReceiver* parent = nullptr);
|
Window(Core::EventReceiver* parent = nullptr);
|
||||||
virtual void wm_event(WMEvent&);
|
virtual void wm_event(WMEvent&);
|
||||||
|
@ -323,6 +327,9 @@ private:
|
||||||
bool m_blocks_emoji_input { false };
|
bool m_blocks_emoji_input { false };
|
||||||
bool m_resizing { false };
|
bool m_resizing { false };
|
||||||
bool m_auto_shrink { false };
|
bool m_auto_shrink { false };
|
||||||
|
bool m_save_size_and_position_on_close { false };
|
||||||
|
StringView m_save_domain;
|
||||||
|
StringView m_save_group;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue