Skip to content

Disable some actions in secondary window #1625

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

Closed
wants to merge 8 commits into from
Closed
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
6 changes: 3 additions & 3 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,13 @@ namespace Scratch {
if (active_window == null) {
if (location_jump_manager.has_selection_range () && location_jump_manager.has_override_target ()) {
RestoreOverride restore_override = location_jump_manager.create_restore_override ();
add_window (new MainWindow.with_restore_override (true, restore_override));
add_window (new MainWindow.primary_with_restore_override (restore_override));
} else {
add_window (new MainWindow (true)); // Will restore documents if required
add_window (new MainWindow.primary ()); // Will restore documents if required
}
} else if (create_new_window) {
create_new_window = false;
add_window (new MainWindow (false)); // Will NOT restore documents in additional windows
add_window (new MainWindow.secondary ()); // Will NOT restore documents in additional windows
}

active_window.present ();
Expand Down
49 changes: 36 additions & 13 deletions src/FolderManager/FileView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,29 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane
public const string ACTION_CLOSE_OTHER_FOLDERS = "close-other-folders";
public const string ACTION_SET_ACTIVE_PROJECT = "set-active-project";

private const ActionEntry[] PRIMARY_ONLY_ACTION_ENTRIES = {
{ ACTION_SET_ACTIVE_PROJECT, action_set_active_project, "s"},
{ ACTION_RENAME_FILE, action_rename_file, "s" },
{ ACTION_RENAME_FOLDER, action_rename_folder, "s" }
};

private const ActionEntry[] ACTION_ENTRIES = {
{ ACTION_LAUNCH_APP_WITH_FILE_PATH, action_launch_app_with_file_path, "as" },
{ ACTION_SHOW_APP_CHOOSER, action_show_app_chooser, "s" },
{ ACTION_EXECUTE_CONTRACT_WITH_FILE_PATH, action_execute_contract_with_file_path, "as" },
{ ACTION_RENAME_FILE, action_rename_file, "s" },
{ ACTION_RENAME_FOLDER, action_rename_folder, "s" },
{ ACTION_DELETE, action_delete, "s" },
{ ACTION_NEW_FILE, add_new_file, "s" },
{ ACTION_NEW_FOLDER, add_new_folder, "s"},
{ ACTION_CLOSE_FOLDER, action_close_folder, "s"},
{ ACTION_CLOSE_OTHER_FOLDERS, action_close_other_folders, "s"},
{ ACTION_SET_ACTIVE_PROJECT, action_set_active_project, "s"}
{ ACTION_CLOSE_OTHER_FOLDERS, action_close_other_folders, "s"}
};

private GLib.Settings settings;
private Scratch.Services.GitManager git_manager;
private Scratch.Services.PluginsManager plugins;
private unowned Scratch.MainWindow window;

public Scratch.Services.PluginsManager plugins { get; construct; }
public bool is_primary { get; construct; }

public new signal void activate (string file);
public signal bool rename_request (File file);
Expand All @@ -64,8 +70,12 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane
public string icon_name { get; set; }
public string title { get; set; }

public FileView (Scratch.Services.PluginsManager plugins_manager) {
plugins = plugins_manager;
public FileView (Scratch.Services.PluginsManager plugins_manager, bool is_primary) {
Object (
root: new ExpandableItem (), //This does not get created by base class when chaining up
plugins: plugins_manager,
is_primary: is_primary
);
}

construct {
Expand All @@ -79,12 +89,22 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane

actions = new SimpleActionGroup ();
actions.add_action_entries (ACTION_ENTRIES, this);
if (is_primary) {
actions.add_action_entries (PRIMARY_ONLY_ACTION_ENTRIES, this);
}

insert_action_group (ACTION_GROUP, actions);

realize.connect (() => {
toplevel_action_group = get_action_group (MainWindow.ACTION_GROUP);
assert_nonnull (toplevel_action_group);
});

show_all ();

realize.connect (() => {
window = (Scratch.MainWindow) (this.get_toplevel ());
});
}

private void action_close_folder (SimpleAction action, GLib.Variant? parameter) {
Expand Down Expand Up @@ -122,7 +142,7 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane
}

//Make remaining project the active one
git_manager.active_project_path = path;
window.active_project_path = path; //Temporary fix

write_settings ();
}
Expand All @@ -138,7 +158,7 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane
return;
}

git_manager.active_project_path = path;
window.active_project_path = path;

write_settings ();
}
Expand Down Expand Up @@ -195,7 +215,7 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane

public void collapse_other_projects () {
unowned string path;
path = git_manager.active_project_path;
path = window.active_project_path;

foreach (var child in root.children) {
var project_folder = ((ProjectFolderItem) child);
Expand Down Expand Up @@ -592,10 +612,13 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane
yield;
}

private bool is_open (File folder) {
foreach (var child in root.children)
if (folder.path == ((Item) child).path)
private bool is_open (File folder) requires (root != null) {
foreach (var child in root.children) {
if (folder.path == ((Item) child).path) {
return true;
}
}

return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/FolderManager/FolderItem.vala
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ namespace Scratch.FolderManager {
} else {
var root = get_root_folder ();
if (root != null &&
root.monitored_repo != null) {
root.has_monitored_repo) {
//When toggled closed, update status to reflect hidden contents
root.update_item_status (this);
}
Expand Down
4 changes: 4 additions & 0 deletions src/FolderManager/Item.vala
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ namespace Scratch.FolderManager {
file.rename (new_name);
}

public bool equal (Item b) {
return path == b.path;
}

public void trash () {
file.trash ();
}
Expand Down
13 changes: 10 additions & 3 deletions src/FolderManager/ProjectFolderItem.vala
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ namespace Scratch.FolderManager {

public signal void closed ();

public Scratch.Services.MonitoredRepository? monitored_repo { get; private set; default = null; }
// public Scratch.Services.MonitoredRepository? monitored_repo { get; private set; default = null; }
private Scratch.Services.MonitoredRepository? monitored_repo;
public bool has_monitored_repo {
get {
return monitored_repo != null;
}
}
// Cache the visible item in the project.
private List<VisibleItem?> visible_item_list = null;

Expand All @@ -41,6 +47,7 @@ namespace Scratch.FolderManager {
}
}

public bool is_readonly { get; private set; }
private Ggit.Repository? git_repo {
get {
return (is_git_repo ? monitored_repo.git_repo : null);
Expand Down Expand Up @@ -74,7 +81,7 @@ namespace Scratch.FolderManager {
}

construct {
monitored_repo = Scratch.Services.GitManager.get_instance ().add_project (this);
is_readonly = Scratch.Services.GitManager.get_instance ().add_project (this, out monitored_repo);
notify["name"].connect (branch_or_name_changed);
if (monitored_repo != null) {
checkout_local_branch_action = new SimpleAction.stateful (
Expand Down Expand Up @@ -142,7 +149,7 @@ namespace Scratch.FolderManager {
}

MenuItem set_active_folder_item;
if (is_git_repo) {
if (is_git_repo && !is_readonly) {
set_active_folder_item = new GLib.MenuItem (
_("Set as Active Project"),
GLib.Action.print_detailed_name (
Expand Down
67 changes: 46 additions & 21 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ namespace Scratch {
public const int FONT_SIZE_MIN = 7;
private const uint MAX_SEARCH_TEXT_LENGTH = 255;

public bool is_primary { get; construct; } //TODO should this be mutable?
public Scratch.Application app { get; private set; }
public bool restore_docs { get; construct; }
public RestoreOverride restore_override { get; construct set; }
public string active_project_path { get; set; }

public string default_globalsearch_path {
owned get {
if (document_view.current_document != null) {
Expand All @@ -35,7 +38,7 @@ namespace Scratch {
}
}

return git_manager.active_project_path;
return active_project_path;
}
}

Expand Down Expand Up @@ -87,7 +90,6 @@ namespace Scratch {
public const string ACTION_ADD_MARK = "action_add_mark";
public const string ACTION_PREVIOUS_MARK = "action_previous_mark";
public const string ACTION_NEXT_MARK = "action_next_mark";

public const string ACTION_UNDO = "action-undo";
public const string ACTION_REDO = "action-redo";
public const string ACTION_REVERT = "action-revert";
Expand Down Expand Up @@ -130,14 +132,19 @@ namespace Scratch {

private Services.GitManager git_manager;

private const ActionEntry[] ACTION_ENTRIES = {
private const ActionEntry[] PRIMARY_ONLY_ACTION_ENTRIES = {
{ ACTION_CLONE_REPO, action_clone_repo },
{ ACTION_OPEN_FOLDER, action_open_folder, "s" },
{ ACTION_TOGGLE_TERMINAL, action_toggle_terminal, null, "false"},
{ ACTION_OPEN_IN_TERMINAL, action_open_in_terminal, "s"},
{ ACTION_NEW_BRANCH, action_new_branch, "s" },
};
private const ActionEntry[] ACTION_ENTRIES = {
{ ACTION_FIND, action_find, "s"},
{ ACTION_FIND_NEXT, action_find_next },
{ ACTION_FIND_PREVIOUS, action_find_previous },
{ ACTION_FIND_GLOBAL, action_find_global, "s" },
{ ACTION_OPEN, action_open },
{ ACTION_OPEN_FOLDER, action_open_folder, "s" },
{ ACTION_COLLAPSE_ALL_FOLDERS, action_collapse_all_folders },
{ ACTION_ORDER_FOLDERS, action_order_folders },
{ ACTION_PREFERENCES, action_preferences },
Expand All @@ -151,7 +158,6 @@ namespace Scratch {
{ ACTION_NEW_TAB, action_new_tab },
{ ACTION_NEW_FROM_CLIPBOARD, action_new_tab_from_clipboard },
{ ACTION_DUPLICATE_TAB, action_duplicate_tab },
{ ACTION_PREFERENCES, action_preferences },
{ ACTION_UNDO, action_undo },
{ ACTION_REDO, action_redo },
{ ACTION_SHOW_REPLACE, action_show_replace },
Expand All @@ -165,13 +171,10 @@ namespace Scratch {
{ ACTION_ZOOM_OUT, action_zoom_out},
{ ACTION_TOGGLE_COMMENT, action_toggle_comment },
{ ACTION_TOGGLE_SIDEBAR, action_toggle_sidebar, null, "true" },
{ ACTION_TOGGLE_TERMINAL, action_toggle_terminal, null, "false"},
{ ACTION_OPEN_IN_TERMINAL, action_open_in_terminal, "s"},
{ ACTION_TOGGLE_OUTLINE, action_toggle_outline, null, "false" },
{ ACTION_NEXT_TAB, action_next_tab },
{ ACTION_PREVIOUS_TAB, action_previous_tab },
{ ACTION_CLEAR_LINES, action_clear_lines },
{ ACTION_NEW_BRANCH, action_new_branch, "s" },
{ ACTION_ADD_MARK, action_add_mark},
{ ACTION_PREVIOUS_MARK, action_previous_mark},
{ ACTION_NEXT_MARK, action_next_mark},
Expand All @@ -186,17 +189,26 @@ namespace Scratch {
{ ACTION_OPEN_IN_NEW_WINDOW, action_open_in_new_window, "s" },
};

public MainWindow (bool restore_docs) {
public MainWindow.primary () {
Object (
is_primary: true,
icon_name: Constants.PROJECT_NAME,
restore_docs: true
);
}
public MainWindow.secondary () {
Object (
is_primary: false,
icon_name: Constants.PROJECT_NAME,
restore_docs: restore_docs
restore_docs: false
);
}

public MainWindow.with_restore_override (bool restore_docs, RestoreOverride restore_override) {
public MainWindow.primary_with_restore_override (RestoreOverride restore_override) {
Object (
is_primary: true,
icon_name: Constants.PROJECT_NAME,
restore_docs: restore_docs,
restore_docs: true,
restore_override: restore_override
);
}
Expand Down Expand Up @@ -279,6 +291,9 @@ namespace Scratch {

actions = new SimpleActionGroup ();
actions.add_action_entries (ACTION_ENTRIES, this);
if (is_primary) {
actions.add_action_entries (PRIMARY_ONLY_ACTION_ENTRIES, this);
}
insert_action_group (ACTION_GROUP, actions);

foreach (var action in action_accelerators.get_keys ()) {
Expand Down Expand Up @@ -349,12 +364,22 @@ namespace Scratch {
outline_action.set_state (saved_state.get_boolean ("outline-visible"));
update_toolbar_button (ACTION_TOGGLE_OUTLINE, saved_state.get_boolean ("outline-visible"));

var terminal_action = Utils.action_from_group (ACTION_TOGGLE_TERMINAL, actions);
terminal_action.set_state (saved_state.get_boolean ("terminal-visible"));
update_toolbar_button (ACTION_TOGGLE_TERMINAL, saved_state.get_boolean ("terminal-visible"));

if (is_primary) {
var terminal_action = Utils.action_from_group (ACTION_TOGGLE_TERMINAL, actions);
terminal_action.set_state (saved_state.get_boolean ("terminal-visible"));
update_toolbar_button (ACTION_TOGGLE_TERMINAL, saved_state.get_boolean ("terminal-visible"));
} else {
terminal.visible = false;
}

Unix.signal_add (Posix.Signal.INT, quit_source_func, Priority.HIGH);
Unix.signal_add (Posix.Signal.TERM, quit_source_func, Priority.HIGH);

bind_property ("active-project-path", sidebar.choose_project_button, "active-project-path", BIDIRECTIONAL);
if (is_primary) {
settings.bind ("active-project-path", this, "active-project-path", DEFAULT);
}
}

private void update_style () {
Expand Down Expand Up @@ -482,7 +507,7 @@ namespace Scratch {

sidebar = new Code.Sidebar ();

folder_manager_view = new FolderManager.FileView (plugins);
folder_manager_view = new FolderManager.FileView (plugins, is_primary);

sidebar.add_tab (folder_manager_view);
folder_manager_view.show_all ();
Expand Down Expand Up @@ -1012,7 +1037,7 @@ namespace Scratch {
return;
}

var new_window = new MainWindow (false);
var new_window = new MainWindow.secondary ();
var file = File.new_for_path (path);
var doc = new Scratch.Services.Document (new_window.actions, file);

Expand Down Expand Up @@ -1045,8 +1070,8 @@ namespace Scratch {

private void action_clone_repo (SimpleAction action, Variant? param) {
var default_projects_folder = Scratch.settings.get_string ("default-projects-folder");
if (default_projects_folder == "" && git_manager.active_project_path != "") {
default_projects_folder = Path.get_dirname (git_manager.active_project_path);
if (default_projects_folder == "" && active_project_path != "") {
default_projects_folder = Path.get_dirname (active_project_path);
}

var default_remote = Scratch.settings.get_string ("default-remote");
Expand Down Expand Up @@ -1338,7 +1363,7 @@ namespace Scratch {
Utils.action_from_group (ACTION_TOGGLE_SHOW_FIND, actions).set_enabled (is_current_doc);
Utils.action_from_group (ACTION_FIND_NEXT, actions).set_enabled (is_current_doc);
Utils.action_from_group (ACTION_FIND_PREVIOUS, actions).set_enabled (is_current_doc);
var can_global_search = is_current_doc || git_manager.active_project_path != null;
var can_global_search = is_current_doc || active_project_path != null;
Utils.action_from_group (ACTION_FIND_GLOBAL, actions).set_enabled (can_global_search);

return Source.REMOVE;
Expand Down Expand Up @@ -1518,7 +1543,7 @@ namespace Scratch {
}

if (path == "") { // Happens when keyboard accelerator is used
path = git_manager.active_project_path;
path = active_project_path;
if (use_build_dir) {
path = git_manager.get_default_build_dir (path);
}
Expand Down
Loading