Skip to content

Commit

Permalink
Improve canvas zoom (#435)
Browse files Browse the repository at this point in the history
* Edit maximum zoom value

* Simplify the zoom actions

* Use delta for mouse zoom

* Fix zoom with mouse wheel

* Force rounded zoom values
  • Loading branch information
Alecaddd authored Sep 3, 2020
1 parent cba933a commit 0e9a9ca
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 46 deletions.
14 changes: 12 additions & 2 deletions src/Layouts/MainCanvas.vala
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,15 @@ public class Akira.Layouts.MainCanvas : Gtk.Grid {
if (delta_y < -SCROLL_DISTANCE) {
// Scroll UP.
if (is_ctrl) {
// Divide the delta if it's too high. This fixes the zoom with
// the mouse wheel.
if (delta_y <= -1) {
delta_y /= 10;
}
// Get the current zoom before zooming.
double old_zoom = canvas.get_scale ();
// Zoom in.
window.headerbar.zoom.zoom_in ();
window.event_bus.update_scale (delta_y * -1);
// Adjust zoom based on cursor position.
zoom_on_cursor (event, old_zoom);
} else if (is_shift) {
Expand All @@ -129,10 +134,15 @@ public class Akira.Layouts.MainCanvas : Gtk.Grid {
} else if (delta_y > SCROLL_DISTANCE) {
// Scroll DOWN.
if (is_ctrl) {
// Divide the delta if it's too high. This fixes the zoom with
// the mouse wheel.
if (delta_y >= 1) {
delta_y /= 10;
}
// Get the current zoom before zooming.
double old_zoom = canvas.get_scale ();
// Zoom out.
window.headerbar.zoom.zoom_out ();
window.event_bus.update_scale (-delta_y);
// Adjust zoom based on cursor position.
zoom_on_cursor (event, old_zoom);
} else if (is_shift) {
Expand Down
36 changes: 19 additions & 17 deletions src/Lib/Canvas.vala
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public class Akira.Lib.Canvas : Goo.Canvas {
nob_manager = new Managers.NobManager (this);
hover_manager = new Managers.HoverManager (this);

window.event_bus.request_zoom.connect (on_request_zoom);
window.event_bus.update_scale.connect (on_update_scale);
window.event_bus.set_scale.connect (on_set_scale);
window.event_bus.request_change_cursor.connect (on_request_change_cursor);
window.event_bus.request_change_mode.connect (on_request_change_mode);
Expand Down Expand Up @@ -344,29 +344,31 @@ public class Akira.Lib.Canvas : Goo.Canvas {
grab_focus (get_root_item ());
}

private void on_request_zoom (string direction) {
switch (direction) {
case "in":
current_scale += 0.1;
break;
case "out":
if (current_scale == 0.1) {
break;
}
current_scale -= 0.1;
break;
case "reset":
current_scale = 1.0;
break;
private void on_update_scale (double zoom) {
// Force the zoom value to 8% if we're currently at a 2% scale in order
// to go back to 10% and increase from there.
if (current_scale == 0.02 && zoom == 0.1) {
zoom = 0.08;
}

set_scale (current_scale);
window.event_bus.zoom ();
current_scale += zoom;
// Prevent the canvas from shrinking below 2%;
if (current_scale < 0.02) {
current_scale = 0.02;
}

// Prevent the canvas from growing above 5000%;
if (current_scale > 50) {
current_scale = 50;
}

window.event_bus.set_scale (current_scale);
}

private void on_set_scale (double scale) {
current_scale = scale;
set_scale (scale);
window.event_bus.zoom ();
}

private void on_request_change_cursor (Gdk.CursorType? cursor_type) {
Expand Down
25 changes: 3 additions & 22 deletions src/Partials/ZoomButton.vala
Original file line number Diff line number Diff line change
Expand Up @@ -83,36 +83,17 @@ public class Akira.Partials.ZoomButton : Gtk.Grid {
}

public void zoom_out () {
var zoom = int.parse (zoom_default_button.label) - 10;
zoom_out_button.sensitive = (zoom > 10);
if (zoom < 10) {
return;
}

zoom_in_button.sensitive = true;
zoom_default_button.label = "%.0f%%".printf (zoom);

window.event_bus.request_zoom ("out");
window.event_bus.update_scale (-0.5);
}

public void zoom_in () {
var zoom = int.parse (zoom_default_button.label) + 10;
zoom_in_button.sensitive = (zoom < 1000);
if (zoom > 1000) {
return;
}

zoom_out_button.sensitive = true;
zoom_default_button.label = "%.0f%%".printf (zoom);

window.event_bus.request_zoom ("in");
window.event_bus.update_scale (0.5);
}

public void zoom_reset () {
zoom_in_button.sensitive = true;
zoom_out_button.sensitive = true;
zoom_default_button.label = "100%";

window.event_bus.request_zoom ("reset");
window.event_bus.set_scale (1);
}
}
14 changes: 10 additions & 4 deletions src/Services/ActionManager.vala
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class Akira.Services.ActionManager : Object {
public const string ACTION_EXPORT_GRAB = "action_export_grab";
public const string ACTION_QUIT = "action_quit";
public const string ACTION_ZOOM_IN = "action_zoom_in";
public const string ACTION_ZOOM_IN_2 = "action_zoom_in_2";
public const string ACTION_ZOOM_OUT = "action_zoom_out";
public const string ACTION_ZOOM_RESET = "action_zoom_reset";
public const string ACTION_MOVE_UP = "action_move_up";
Expand Down Expand Up @@ -87,6 +88,7 @@ public class Akira.Services.ActionManager : Object {
{ ACTION_EXPORT_GRAB, action_export_grab },
{ ACTION_QUIT, action_quit },
{ ACTION_ZOOM_IN, action_zoom_in },
{ ACTION_ZOOM_IN_2, action_zoom_in_2 },
{ ACTION_ZOOM_OUT, action_zoom_out },
{ ACTION_MOVE_UP, action_move_up },
{ ACTION_MOVE_DOWN, action_move_down },
Expand Down Expand Up @@ -129,7 +131,7 @@ public class Akira.Services.ActionManager : Object {
action_accelerators.set (ACTION_EXPORT_ARTBOARDS, "<Control><Alt>a");
action_accelerators.set (ACTION_EXPORT_GRAB, "<Control><Alt>g");
action_accelerators.set (ACTION_QUIT, "<Control>q");
action_accelerators.set (ACTION_ZOOM_IN, "<Control>equal");
action_accelerators.set (ACTION_ZOOM_IN_2, "<Control>equal");
action_accelerators.set (ACTION_ZOOM_IN, "<Control>plus");
action_accelerators.set (ACTION_ZOOM_OUT, "<Control>minus");
action_accelerators.set (ACTION_ZOOM_RESET, "<Control>0");
Expand Down Expand Up @@ -306,15 +308,19 @@ public class Akira.Services.ActionManager : Object {
}

private void action_zoom_in () {
window.headerbar.zoom.zoom_in ();
window.event_bus.update_scale (0.1);
}

private void action_zoom_in_2 () {
action_zoom_in ();
}

private void action_zoom_out () {
window.headerbar.zoom.zoom_out ();
window.event_bus.update_scale (-0.1);
}

private void action_zoom_reset () {
window.headerbar.zoom.zoom_reset ();
window.event_bus.set_scale (1);
}

private void action_move_up () {
Expand Down
2 changes: 1 addition & 1 deletion src/Services/EventBus.vala
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public class Akira.Services.EventBus : Object {
public signal void request_change_cursor (Gdk.CursorType? cursor_type);
public signal void request_change_mode (Lib.Canvas.EditMode mode);
public signal void request_escape ();
public signal void request_zoom (string direction);
public signal void set_focus_on_canvas ();
public signal void update_nob_size ();
public signal void update_scale (double scale);
public signal void set_scale (double scale);
public signal void zoom ();
public signal void canvas_notification (string message);
Expand Down

0 comments on commit 0e9a9ca

Please sign in to comment.