Skip to content

Move docs into another window when another one closes #1620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,41 @@ namespace Scratch {
return windows.length () > 0 ? windows.last ().data as MainWindow : null;
}

public async void handle_quit_window (MainWindow window_to_close) {
if (!yield window_to_close.check_unsaved_changes ()) {
return;
}

unowned List<Gtk.Window> windows = get_windows ();
var n_windows = windows.length ();

if (n_windows == 1) {
// Close app as usual
window_to_close.before_quit (); // Update settings
quit ();
} else {
// Find window to move docs into
unowned var windows_head = windows.first ();
var target_window = (MainWindow)(windows_head.data);
if (target_window == window_to_close) {
target_window = (MainWindow)(windows_head.next.data);
}

// Reopen each doc in target window (we know they have been saved)
var doc_list = window_to_close.document_view.docs.copy ();
foreach (var doc in doc_list) {
var new_doc = new Services.Document (
target_window.actions,
doc.file
);
yield target_window.open_document (new_doc, false);
}

remove_window (window_to_close);
window_to_close.destroy ();
}
}

public static int main (string[] args) {
return new Application ().run (args);
}
Expand Down
17 changes: 6 additions & 11 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ namespace Scratch {
}

// Check that there no unsaved changes and all saves are successful
private async bool check_unsaved_changes () {
public async bool check_unsaved_changes () {
document_view.is_closing = true;
foreach (var doc in document_view.docs) {
if (!yield (doc.do_close (true))) {
Expand Down Expand Up @@ -845,7 +845,7 @@ namespace Scratch {
}
}

private void update_saved_state () {
private void update_window_state_setting () {
// Save window state
var state = get_window ().get_state ();
if (Gdk.WindowState.MAXIMIZED in state) {
Expand All @@ -872,9 +872,9 @@ namespace Scratch {
}

// For exit cleanup
private void handle_quit () {
document_view.save_opened_files ();
update_saved_state ();
public void before_quit () {
document_view.update_opened_files_setting ();
update_window_state_setting ();
}

public void set_default_zoom () {
Expand Down Expand Up @@ -962,12 +962,7 @@ namespace Scratch {
}

private void action_quit () {
handle_quit ();
check_unsaved_changes.begin ((obj, res) => {
if (check_unsaved_changes.end (res)) {
app.quit ();
}
});
app.handle_quit_window (this);
}

private void action_open () {
Expand Down
10 changes: 5 additions & 5 deletions src/Widgets/DocumentView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public class Scratch.Widgets.DocumentView : Gtk.Box {
var should_close = doc.do_close.end (res);
// Ensure removed doc is saved by handling this first
if (!is_closing) {
save_opened_files ();
update_opened_files_setting ();
}
//`page-detached` handler will perform rest of necessary cleanup
tab_view.close_page_finish (tab, should_close);
Expand Down Expand Up @@ -337,7 +337,7 @@ public class Scratch.Widgets.DocumentView : Gtk.Box {
if (range != SelectionRange.EMPTY) {
Idle.add_full (GLib.Priority.LOW, () => { // This helps ensures new tab is drawn before opening document.
current_document.source_view.select_range (range);
save_opened_files ();
update_opened_files_setting ();

return false;
});
Expand All @@ -364,7 +364,7 @@ public class Scratch.Widgets.DocumentView : Gtk.Box {
doc.source_view.cursor_position = cursor_position;
}

save_opened_files ();
update_opened_files_setting ();
}

public void next_document () {
Expand Down Expand Up @@ -399,7 +399,7 @@ public class Scratch.Widgets.DocumentView : Gtk.Box {
}
}

public void save_opened_files () {
public void update_opened_files_setting () {
if (privacy_settings.get_boolean ("remember-recent-files")) {
var vb = new VariantBuilder (new VariantType ("a(si)"));
docs.foreach ((doc) => {
Expand Down Expand Up @@ -542,7 +542,7 @@ public class Scratch.Widgets.DocumentView : Gtk.Box {
current_document = doc;
}

save_opened_files ();
update_opened_files_setting ();
}

private unowned Hdy.TabView? on_doc_to_new_window (Hdy.TabView tab_view) {
Expand Down