Skip to content

Commit f5f7c40

Browse files
committed
Switch/Move to workspace shortcuts: if ws doesn't exist, switch to last instead
1 parent 7c0cd4c commit f5f7c40

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/WindowManager.vala

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ namespace Gala {
334334
Meta.KeyBinding.set_custom_handler ("move-to-workspace-right", (Meta.KeyHandlerFunc) handle_move_to_workspace);
335335

336336
for (int i = 1; i < 13; i++) {
337+
Meta.KeyBinding.set_custom_handler ("switch-to-workspace-%d".printf (i), (Meta.KeyHandlerFunc) handle_switch_to_workspace);
337338
Meta.KeyBinding.set_custom_handler ("move-to-workspace-%d".printf (i), (Meta.KeyHandlerFunc) handle_move_to_workspace);
338339
}
339340

@@ -465,8 +466,9 @@ namespace Gala {
465466
var direction = (name == "move-to-workspace-left" ? Meta.MotionDirection.LEFT : Meta.MotionDirection.RIGHT);
466467
target_workspace = active_workspace.get_neighbor (direction);
467468
} else {
468-
var workspace_number = int.parse (name.offset ("move-to-workspace-".length));
469-
var workspace_index = workspace_number - 1;
469+
var workspace_number = int.parse (name.offset ("move-to-workspace-".length)) - 1;
470+
var workspace_index = workspace_number.clamp (0, workspace_manager.n_workspaces - 1);
471+
470472
target_workspace = workspace_manager.get_workspace_by_index (workspace_index);
471473
}
472474

@@ -491,8 +493,24 @@ namespace Gala {
491493
[CCode (instance_pos = -1)]
492494
private void handle_switch_to_workspace (Meta.Display display, Meta.Window? window,
493495
Clutter.KeyEvent event, Meta.KeyBinding binding) {
494-
var direction = (binding.get_name () == "switch-to-workspace-left" ? Meta.MotionDirection.LEFT : Meta.MotionDirection.RIGHT);
495-
switch_to_next_workspace (direction, event.get_time ());
496+
unowned var name = binding.get_name ();
497+
498+
if (name == "switch-to-workspace-left" || name == "switch-to-workspace-right") {
499+
var direction = (name == "switch-to-workspace-left" ? Meta.MotionDirection.LEFT : Meta.MotionDirection.RIGHT);
500+
switch_to_next_workspace (direction, event.get_time ());
501+
} else {
502+
unowned var workspace_manager = get_display ().get_workspace_manager ();
503+
504+
var workspace_number = int.parse (name.offset ("switch-to-workspace-".length)) - 1;
505+
var workspace_index = workspace_number.clamp (0, workspace_manager.n_workspaces - 1);
506+
507+
var workspace = workspace_manager.get_workspace_by_index (workspace_index);
508+
if (workspace == null) {
509+
return;
510+
}
511+
512+
workspace.activate (event.get_time ());
513+
}
496514
}
497515

498516
[CCode (instance_pos = -1)]

0 commit comments

Comments
 (0)