Skip to content
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

(2/5) Multiple Issues: Snap monitors after mouse button release #181

Merged
merged 18 commits into from
Sep 29, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
Next Next commit
(1/5) Multiple issues: Prep
This is a prep PR needed for the following PRs:
- snap widgets after mouse button release
- close gaps after mouse button release
- fix intersects after mouse button release
- align display_widget edges/center

Changes in this PR
- change the meaning of display_widget.delta:
 from distance of in widget units to distance in monitor/pixel units

The expression
```vala
delta_x = (int)(event.x_root - start_x);
delta_y = (int)(event.y_root - start_y);
```

led to the loss of information and as the distance in widgets units is
always less than the distance in monitor units. There no 1-to-1 mapping
was possible.

With the expression
```vala
display_widget.delta_x = (int) (diff_x / current_ratio);
display_widget.delta_y = (int) (diff_y / current_ratio);
```
a 1-to-1 mapping between the widget units and monitor units is possible
as the integer cast happens after the float division!

- rename move_display to end_grab as the signal is only emitted when the mouse button is released
- introduce new signal called `move_display` which is emitted when widget is moved
  • Loading branch information
felix-andreas committed Aug 27, 2019
commit 6fcd6adab6bd04f226a73221725cda8221213add
11 changes: 5 additions & 6 deletions src/Widgets/DisplayWidget.vala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@

public class Display.DisplayWidget : Gtk.EventBox {
public signal void set_as_primary ();
public signal void move_display (int delta_x, int delta_y);
public signal void move_display (double diff_x, double diff_y, Gdk.EventMotion event);
public signal void end_grab (int delta_x, int delta_y);
public signal void check_position ();
public signal void configuration_changed ();
public signal void active_changed ();
Expand Down Expand Up @@ -416,6 +417,7 @@ public class Display.DisplayWidget : Gtk.EventBox {
}

public override bool button_release_event (Gdk.EventButton event) {
holding = false;
if ((delta_x == 0 && delta_y == 0) || only_display) {
return false;
}
Expand All @@ -424,16 +426,13 @@ public class Display.DisplayWidget : Gtk.EventBox {
var old_delta_y = delta_y;
delta_x = 0;
delta_y = 0;
move_display (old_delta_x, old_delta_y);
holding = false;
end_grab (old_delta_x, old_delta_y);
return false;
}

public override bool motion_notify_event (Gdk.EventMotion event) {
if (holding && !only_display) {
delta_x = (int)(event.x_root - start_x);
delta_y = (int)(event.y_root - start_y);
check_position ();
move_display (event.x_root - start_x, event.y_root - start_y, event);
}

return false;
Expand Down
41 changes: 25 additions & 16 deletions src/Widgets/DisplaysOverlay.vala
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ public class Display.DisplaysOverlay : Gtk.Overlay {
allocation = Gdk.Rectangle ();
allocation.width = (int)(width * current_ratio);
allocation.height = (int)(height * current_ratio);
allocation.x = default_x_margin + (int)(x * current_ratio) + display_widget.delta_x;
allocation.y = default_y_margin + (int)(y * current_ratio) + display_widget.delta_y;
allocation.x = default_x_margin + (int) ((x + display_widget.delta_x) * current_ratio);
allocation.y = default_y_margin + (int) ((y + display_widget.delta_y) * current_ratio);
return true;
}

Expand Down Expand Up @@ -168,7 +168,7 @@ public class Display.DisplaysOverlay : Gtk.Overlay {

current_allocated_width = get_allocated_width ();
current_allocated_height = get_allocated_height ();
current_ratio = double.min ((double)(get_allocated_width () -24) / (double) added_width, (double)(get_allocated_height ()-24) / (double) added_height);
current_ratio = double.min ((double) (get_allocated_width () - 24) / (double) added_width, (double) (get_allocated_height () - 24) / (double) added_height);
default_x_margin = (int) ((get_allocated_width () - max_width * current_ratio) / 2);
default_y_margin = (int) ((get_allocated_height () - max_height * current_ratio) / 2);
}
Expand Down Expand Up @@ -214,6 +214,7 @@ public class Display.DisplaysOverlay : Gtk.Overlay {
display_widget.show_all ();
display_widget.set_as_primary.connect (() => set_as_primary (display_widget.virtual_monitor));
display_widget.check_position.connect (() => check_intersects (display_widget));
display_widget.move_display.connect ((diff_x, diff_y, event) => move_display (display_widget, diff_x, diff_y, event));
display_widget.configuration_changed.connect (() => check_configuration_changed ());
display_widget.active_changed.connect (() => {
active_displays += virtual_monitor.is_active ? 1 : -1;
Expand All @@ -226,16 +227,17 @@ public class Display.DisplaysOverlay : Gtk.Overlay {
display_widget.display_window.show_all ();
}

display_widget.move_display.connect ((delta_x, delta_y) => {
display_widget.end_grab.connect ((delta_x, delta_y) => {
if (delta_x == 0 && delta_y == 0) {
return;
}

int x, y, width, height;
display_widget.get_geometry (out x, out y, out width, out height);
display_widget.set_geometry ((int)(delta_x / current_ratio) + x, (int)(delta_y / current_ratio) + y, width, height);
display_widget.set_geometry (delta_x + x, delta_y + y, width, height);
display_widget.queue_resize_no_redraw ();
check_configuration_changed ();
check_intersects (display_widget);
snap_edges (display_widget);
verify_global_positions ();
calculate_ratio ();
Expand All @@ -246,7 +248,7 @@ public class Display.DisplaysOverlay : Gtk.Overlay {
var old_delta_y = display_widget.delta_y;
display_widget.delta_x = 0;
display_widget.delta_y = 0;
display_widget.move_display (old_delta_x, old_delta_y);
display_widget.end_grab (old_delta_x, old_delta_y);
}

private void set_as_primary (Display.VirtualMonitor new_primary) {
Expand All @@ -266,6 +268,13 @@ public class Display.DisplaysOverlay : Gtk.Overlay {
check_configuration_changed ();
}

private void move_display (DisplayWidget display_widget, double diff_x, double diff_y, Gdk.EventMotion event) {
reorder_overlay (display_widget, -1);
display_widget.delta_x = (int) (diff_x / current_ratio);
display_widget.delta_y = (int) (diff_y / current_ratio);
check_intersects (display_widget);
}

private void verify_global_positions () {
int min_x = int.MAX;
int min_y = int.MAX;
Expand Down Expand Up @@ -295,8 +304,8 @@ public class Display.DisplaysOverlay : Gtk.Overlay {
public void check_intersects (DisplayWidget source_display_widget) {
int orig_x, orig_y, src_x, src_y, src_width, src_height;
source_display_widget.get_geometry (out orig_x, out orig_y, out src_width, out src_height);
src_x = orig_x + (int)(source_display_widget.delta_x/current_ratio);
src_y = orig_y + (int)(source_display_widget.delta_y/current_ratio);
src_x = orig_x + source_display_widget.delta_x;
src_y = orig_y + source_display_widget.delta_y;
Gdk.Rectangle src_rect = {src_x, src_y, src_width, src_height};
get_children ().foreach ((child) => {
if (child is DisplayWidget) {
Expand All @@ -313,35 +322,35 @@ public class Display.DisplaysOverlay : Gtk.Overlay {
if (intersection.height == src_height) {
// on the left side
if (intersection.x <= x + width/2) {
source_display_widget.delta_x = (int) ((x - (orig_x + src_width)) * current_ratio);
source_display_widget.delta_x = x - (orig_x + src_width);
// on the right side
} else {
source_display_widget.delta_x = (int) ((x - orig_x + width) * current_ratio);
source_display_widget.delta_x = x - orig_x + width;
}
} else if (intersection.width == src_width) {
// on the bottom side
if (intersection.y <= y + height/2) {
source_display_widget.delta_y = (int) ((y - (orig_y + src_height)) * current_ratio);
source_display_widget.delta_y = y - (orig_y + src_height);
} else {
// on the upper side
source_display_widget.delta_y = (int) ((y - orig_y + height) * current_ratio);
source_display_widget.delta_y = y - orig_y + height;
}
} else {
if (intersection.width < intersection.height) {
// on the left side
if (intersection.x <= x + width/2) {
source_display_widget.delta_x = (int) ((x - (orig_x + src_width)) * current_ratio);
source_display_widget.delta_x = x - (orig_x + src_width);
// on the right side
} else {
source_display_widget.delta_x = (int) ((x - orig_x + width) * current_ratio);
source_display_widget.delta_x = x - orig_x + width;
}
} else {
// on the bottom side
if (intersection.y <= y + height/2) {
source_display_widget.delta_y = (int) ((y - (orig_y + src_height)) * current_ratio);
source_display_widget.delta_y = y - (orig_y + src_height);
} else {
// on the upper side
source_display_widget.delta_y = (int) ((y - orig_y + height) * current_ratio);
source_display_widget.delta_y = y - orig_y + height;
}
}
}
Expand Down