| 1 |
/* |
| 2 |
* Copyright (c) 2017-2019 José Amuedo (https://github.com/spheras) |
| 3 |
* |
| 4 |
* This program is free software: you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation, either version 3 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
*/ |
| 17 |
|
| 18 |
/** |
| 19 |
* @class |
| 20 |
* Grid Arragement - User can move icons wherever following a grid |
| 21 |
*/ |
| 22 |
public class DesktopFolder.FolderArrangementGrid : Object, FolderArrangement { |
| 23 |
protected const int SENSITIVITY_WITH_GRID = DesktopFolder.ICON_DEFAULT_WIDTH; |
| 24 |
|
| 25 |
public bool have_margin () { |
| 26 |
return true; |
| 27 |
} |
| 28 |
|
| 29 |
public bool can_drag () { |
| 30 |
return true; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* @name get_sensitivity |
| 35 |
* @description Get the value of sensitivity, used to calculate the alignment of the items |
| 36 |
*/ |
| 37 |
public int get_sensitivity () { |
| 38 |
return SENSITIVITY_WITH_GRID; |
| 39 |
} |
| 40 |
|
| 41 |
public bool can_organize () { |
| 42 |
return true; |
| 43 |
} |
| 44 |
|
| 45 |
public bool force_organization () { |
| 46 |
return false; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* @name on_toggle_align_to_grid |
| 51 |
* @description the toggle align to grid event. The align to grid property must change |
| 52 |
*/ |
| 53 |
public void on_toggle_align_to_grid () { |
| 54 |
/* |
| 55 |
if (this.get_sensitivity () == SENSITIVITY_WITH_GRID) { |
| 56 |
this.set_sensitivity (SENSITIVITY_WITHOUT_GRID); |
| 57 |
this.manager.get_settings ().align_to_grid = false; |
| 58 |
} else { |
| 59 |
this.set_sensitivity (SENSITIVITY_WITH_GRID); |
| 60 |
this.manager.get_settings ().align_to_grid = true; |
| 61 |
} |
| 62 |
this.manager.get_settings ().save (); |
| 63 |
this.clear_all (); |
| 64 |
this.manager.sync_files (0, 0); |
| 65 |
*/ |
| 66 |
} |
| 67 |
|
| 68 |
// util grid map during te item dragging |
| 69 |
private FolderGrid <ItemSettings> drag_grid; |
| 70 |
|
| 71 |
private ItemView drag_item; |
| 72 |
private Gdk.Point ? init_item_cell; |
| 73 |
private Gdk.Point ? init_item_px; |
| 74 |
|
| 75 |
private ItemSettings drag_conflict_item; |
| 76 |
private Gdk.Point ? drag_conflict_cell; |
| 77 |
private Gdk.Point ? drag_conflict_px; |
| 78 |
private int drag_padding; |
| 79 |
|
| 80 |
public Gdk.Point ? get_init_item_cell () { |
| 81 |
return this.init_item_cell; |
| 82 |
} |
| 83 |
|
| 84 |
public bool is_dragging () { |
| 85 |
return this.drag_item != null; |
| 86 |
} |
| 87 |
|
| 88 |
public void start_drag (ItemView view) { |
| 89 |
// debug ("start_drag - start"); |
| 90 |
this.drag_item = view; |
| 91 |
this.init_item_px = Gdk.Point (); |
| 92 |
ItemSettings view_settings = view.get_manager ().get_settings (); |
| 93 |
init_item_px.x = view_settings.x; |
| 94 |
init_item_px.y = view_settings.y; |
| 95 |
this.drag_padding = view.get_manager ().get_folder ().get_settings ().arrangement_padding; |
| 96 |
this.drag_grid = FolderGrid.build_grid_structure (view.get_manager ().get_folder ().get_view (), this.drag_padding, view_settings); |
| 97 |
this.init_item_cell = this.drag_grid.get_item_cell_position (view_settings, |
| 98 |
(a, b) => { |
| 99 |
return a.name == b.name; |
| 100 |
}); |
| 101 |
// debug ("start_drag - end"); |
| 102 |
} |
| 103 |
|
| 104 |
public void motion_drag (int x, int y) { |
| 105 |
// we get the cell at pixel x,y |
| 106 |
Gdk.Point cell_at_xy = this.drag_grid.get_cell (SENSITIVITY_WITH_GRID, this.drag_padding, x, y); |
| 107 |
// debug ("motion_drag - cell(%d,%d)", cell_at_xy.x, cell_at_xy.y); |
| 108 |
|
| 109 |
// the drag item settings |
| 110 |
ItemSettings drag_item_settings = this.drag_item.get_manager ().get_settings (); |
| 111 |
|
| 112 |
// checking if we had an old conflict to restore the conflicting item |
| 113 |
if (this.drag_conflict_item != null && |
| 114 |
(cell_at_xy.x != this.drag_conflict_cell.x || |
| 115 |
cell_at_xy.y != this.drag_conflict_cell.y)) { |
| 116 |
// restoring conflict item to its original position |
| 117 |
this.drag_grid.replace (drag_item_settings, this.drag_conflict_cell, this.drag_conflict_item, this.init_item_cell); |
| 118 |
ItemManager item_conflict_manager = this.drag_item.get_manager ().get_folder ().get_item_by_filename (this.drag_conflict_item.name); |
| 119 |
|
| 120 |
if (!item_conflict_manager.is_selected ()) { |
| 121 |
UtilGtkAnimation.animate_move (item_conflict_manager.get_view (), this.drag_conflict_px, 1000); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
this.drag_conflict_item.x = this.drag_conflict_px.x; |
| 126 |
this.drag_conflict_item.y = this.drag_conflict_px.y; |
| 127 |
|
| 128 |
this.drag_conflict_item = null; |
| 129 |
this.drag_conflict_cell = null; |
| 130 |
this.drag_conflict_px = null; |
| 131 |
} else if (this.drag_conflict_item != null) { |
| 132 |
// debug ("conflict (%d,%d)", this.drag_conflict_cell.x, this.drag_conflict_cell.y); |
| 133 |
} |
| 134 |
|
| 135 |
// and now the item at that cell in the grid |
| 136 |
ItemSettings item_at_pos = this.drag_grid.get_item_at_cell (cell_at_xy); |
| 137 |
|
| 138 |
if (item_at_pos != null && item_at_pos.name != drag_item_settings.name) { |
| 139 |
// anotate the conflict |
| 140 |
this.drag_conflict_item = item_at_pos; |
| 141 |
this.drag_conflict_cell = cell_at_xy; |
| 142 |
ItemManager item_at_pos_manager = this.drag_item.get_manager ().get_folder ().get_item_by_filename (item_at_pos.name); |
| 143 |
|
| 144 |
// interchanging positions in the grid and physically |
| 145 |
this.drag_grid.replace (drag_item_settings, this.init_item_cell, item_at_pos, cell_at_xy); |
| 146 |
this.drag_conflict_px = Gdk.Point (); |
| 147 |
this.drag_conflict_px.x = item_at_pos.x; |
| 148 |
this.drag_conflict_px.y = item_at_pos.y; |
| 149 |
item_at_pos.x = init_item_px.x; |
| 150 |
item_at_pos.y = init_item_px.y; |
| 151 |
|
| 152 |
if (!item_at_pos_manager.is_selected ()) { |
| 153 |
// this.drag_item.get_manager ().get_folder ().get_view ().move_item (item_at_pos_manager.get_view (), init_item_px.x, init_item_px.y); |
| 154 |
UtilGtkAnimation.animate_move (item_at_pos_manager.get_view (), init_item_px, 500, UtilFx.AnimationMode.EASE_IN_BACK); |
| 155 |
} |
| 156 |
|
| 157 |
} |
| 158 |
|
| 159 |
// if no, then restore the item in conflict to the original position |
| 160 |
} |
| 161 |
|
| 162 |
public void end_drag () { |
| 163 |
// debug ("end_drag - start"); |
| 164 |
if (this.drag_conflict_item != null) { |
| 165 |
// debug ("end_drag - saving conflict"); |
| 166 |
ItemManager drag_conflict_manager = this.drag_item.get_manager ().get_folder ().get_item_by_filename (this.drag_conflict_item.name); |
| 167 |
drag_conflict_manager.get_folder ().get_settings ().set_item (this.drag_conflict_item); |
| 168 |
drag_conflict_manager.get_folder ().get_settings ().save (); |
| 169 |
|
| 170 |
} |
| 171 |
|
| 172 |
this.drag_grid = null; |
| 173 |
this.drag_item = null; |
| 174 |
this.init_item_cell = null; |
| 175 |
this.init_item_px = null; |
| 176 |
this.drag_conflict_item = null; |
| 177 |
this.drag_conflict_cell = null; |
| 178 |
this.drag_conflict_px = null; |
| 179 |
// debug("end_drag - end"); |
| 180 |
} |
| 181 |
|
| 182 |
} |