Skip to content

Commit

Permalink
Remember the editor window position and size across restarts
Browse files Browse the repository at this point in the history
This closes godotengine#5114.
  • Loading branch information
Calinou committed Feb 19, 2020
1 parent 4581a93 commit 9680ad5
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
32 changes: 32 additions & 0 deletions editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2780,6 +2780,7 @@ void EditorNode::_exit_editor() {
exiting = true;
resource_preview->stop(); //stop early to avoid crashes
_save_docks();
_save_window_config();

// Dim the editor window while it's quitting to make it clearer that it's busy
dim_editor(true, true);
Expand Down Expand Up @@ -4256,6 +4257,35 @@ void EditorNode::_save_docks_to_config(Ref<ConfigFile> p_layout, const String &p
}
}

void EditorNode::_save_window_config() {
window_config->set_value("window", "maximized", OS::get_singleton()->is_window_maximized() || OS::get_singleton()->is_window_fullscreen());
// Save the window position relative to the current monitor
window_config->set_value("window", "position", OS::get_singleton()->get_window_position() - OS::get_singleton()->get_screen_position());
window_config->set_value("window", "size", OS::get_singleton()->get_window_size());

This comment has been minimized.

Copy link
@avril-gh

avril-gh May 2, 2020

that's exactly what this issue been about.
Thanks


Error err = window_config->save(EditorSettings::get_singleton()->get_window_config());
ERR_FAIL_COND_MSG(err != OK, "Couldn't save the editor window configuration file.");
}

void EditorNode::_load_window_config() {
window_config.instance();
String window_config_path = EditorSettings::get_singleton()->get_window_config();

DirAccessRef dar = DirAccess::open(window_config_path.get_base_dir());
if (dar->file_exists(window_config_path.get_file())) {
window_config->load(window_config_path);

// Set the window configuration that was previously saved
OS::get_singleton()->set_window_maximized((bool)window_config->get_value("window", "maximized"));

if (!(bool)window_config->get_value("window", "maximized")) {
// Load the window position relative to the current monitor
OS::get_singleton()->set_window_position((Vector2)window_config->get_value("window", "position") + OS::get_singleton()->get_screen_position());
OS::get_singleton()->set_window_size((Vector2)window_config->get_value("window", "size"));
}
}
}

void EditorNode::_save_open_scenes_to_config(Ref<ConfigFile> p_layout, const String &p_section) {
Array scenes;
for (int i = 0; i < editor_data.get_edited_scene_count(); i++) {
Expand Down Expand Up @@ -5641,6 +5671,8 @@ EditorNode::EditorNode() {
if (!EditorSettings::get_singleton())
EditorSettings::create();

_load_window_config();

FileAccess::set_backup_save(EDITOR_GET("filesystem/on_save/safe_save_on_backup_then_rename"));

{
Expand Down
5 changes: 5 additions & 0 deletions editor/editor_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ class EditorNode : public Node {
PopupMenu *editor_layouts;
EditorLayoutsDialog *layout_dialog;

Ref<ConfigFile> window_config;

ConfirmationDialog *custom_build_manage_templates;
ConfirmationDialog *install_android_build_template;
ConfirmationDialog *remove_android_build_template;
Expand Down Expand Up @@ -597,6 +599,9 @@ class EditorNode : public Node {
void _update_dock_slots_visibility();
void _dock_tab_changed(int p_tab);

void _save_window_config();
void _load_window_config();

bool restoring_scenes;
void _save_open_scenes_to_config(Ref<ConfigFile> p_layout, const String &p_section);
void _load_open_scenes_from_config(Ref<ConfigFile> p_layout, const String &p_section);
Expand Down
5 changes: 5 additions & 0 deletions editor/editor_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1465,6 +1465,11 @@ String EditorSettings::get_editor_layouts_config() const {
return get_settings_dir().plus_file("editor_layouts.cfg");
}

String EditorSettings::get_window_config() const {

return get_cache_dir().plus_file("window.cfg");
}

// Shortcuts

void EditorSettings::add_shortcut(const String &p_name, Ref<ShortCut> &p_shortcut) {
Expand Down
1 change: 1 addition & 0 deletions editor/editor_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ class EditorSettings : public Resource {

Vector<String> get_script_templates(const String &p_extension, const String &p_custom_path = String());
String get_editor_layouts_config() const;
String get_window_config() const;

void add_shortcut(const String &p_name, Ref<ShortCut> &p_shortcut);
bool is_shortcut(const String &p_name, const Ref<InputEvent> &p_event) const;
Expand Down

0 comments on commit 9680ad5

Please sign in to comment.